Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to change config from props #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,17 @@ Import into your project:
import OrbitControlsView from 'expo-three-orbit-controls';
```

## ✌️ Modifications by WebBeard

- Possibility to enable / disable OrbitControls from props
- Possibility to change all the config properties from props.

Example :
```tsx
<OrbitControlsView style={{ flex: 1 }} camera={camera} enabled={true} config={{enableRotate: false, minPolarAngle: Math.PI/2, maxPolarAngle: Math.PI/2, enableZoom: false}}>
// GLView, etc
<OrbitControlsView>
```

Check out the [example](./example/App.tsx) for more info.

44 changes: 44 additions & 0 deletions src/OrbitControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @author WestLangley / http://github.com/WestLangley
* @author erich666 / http://erichaines.com
* @author ScieCode / http://github.com/sciecode
* @author WebBeard / http://github.com/WebBeard
*/

import {
Expand Down Expand Up @@ -169,10 +170,53 @@ export class OrbitControls extends EventDispatcher {
isOrthographicCamera?: boolean;
isPerspectiveCamera?: boolean;
},
enabled,
config,
ref?: any
) {
super();

// Set to false to disable this control
this.enabled = enabled;
// "target" sets the location of focus, where the object orbits around
this.target = new Vector3();
// How far you can dolly in and out ( PerspectiveCamera only )
this.minDistance = config.hasOwnProperty('minDistance') ? config.minDistance : 0;
this.maxDistance = config.hasOwnProperty('maxDistance') ? config.maxDistance : Infinity;
// How far you can zoom in and out ( OrthographicCamera only )
this.minZoom = config.hasOwnProperty('minZoom') ? config.minZoom : 0;
this.maxZoom = config.hasOwnProperty('maxZoom') ? config.maxZoom : Infinity;
// How far you can orbit vertically, upper and lower limits.
// Range is 0 to Math.PI radians.
this.minPolarAngle = config.hasOwnProperty('minPolarAngle') ? config.minPolarAngle : 0; // radians
this.maxPolarAngle = config.hasOwnProperty('maxPolarAngle') ? config.maxPolarAngle : Math.PI; // radians
// How far you can orbit horizontally, upper and lower limits.
// If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
this.minAzimuthAngle = config.hasOwnProperty('minAzimuthAngle') ? config.minAzimuthAngle : -Infinity; // radians
this.maxAzimuthAngle = config.hasOwnProperty('maxAzimuthAngle') ? config.maxAzimuthAngle : Infinity; // radians
// Set to true to enable damping (inertia)
// If damping is enabled, you must call controls.update() in your animation loop
this.enableDamping = config.hasOwnProperty('enableDamping') ? config.enableDamping : false;
this.dampingFactor = config.hasOwnProperty('dampingFactor') ? config.dampingFactor : 0.05;
// This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
// Set to false to disable zooming
this.enableZoom = config.hasOwnProperty('enableZoom') ? config.enableZoom : true;
this.zoomSpeed = config.hasOwnProperty('zoomSpeed') ? config.zoomSpeed : 1.0;
// Set to false to disable rotating
this.enableRotate = config.hasOwnProperty('enableRotate') ? config.enableRotate : true;
this.rotateSpeed = config.hasOwnProperty('rotateSpeed') ? config.rotateSpeed : 1.0;
// Set to false to disable panning
this.enablePan = config.hasOwnProperty('enablePan') ? config.enablePan : true;
this.panSpeed = config.hasOwnProperty('panSpeed') ? config.panSpeed : 1.0;
this.screenSpacePanning = config.hasOwnProperty('screenSpacePanning') ? config.screenSpacePanning : false; // if true, pan in screen-space
this.keyPanSpeed = config.hasOwnProperty('keyPanSpeed') ? config.keyPanSpeed : 7.0; // pixels moved per arrow key push
// Set to true to automatically rotate around the target
// If auto-rotate is enabled, you must call controls.update() in your animation loop
this.autoRotate = config.hasOwnProperty('autoRotate') ? config.autoRotate : false;
this.autoRotateSpeed = config.hasOwnProperty('autoRotateSpeed') ? config.autoRotateSpeed : 2.0; // 30 seconds per round when fps is 60
// Set to false to disable use of the keys
this.enableKeys = config.hasOwnProperty('enableKeys') ? config.enableKeys : true;

if (ref && Platform.OS === 'web' && typeof window !== 'undefined') {
this.domElement = getNode(ref) || window.document;
}
Expand Down
8 changes: 4 additions & 4 deletions src/OrbitControlsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function polyfillEventTouches(nativeEvent) {
}

const OrbitControlsView = React.forwardRef(
({ camera, ...props }: OrbitControlsViewProps, ref) => {
({ camera, enabled = true, config = {}, ...props }: OrbitControlsViewProps, ref) => {
const [size, setSize] = React.useState<null | {
width: number;
height: number;
Expand All @@ -29,11 +29,11 @@ const OrbitControlsView = React.forwardRef(
const viewRef = React.useRef(null);

const controls: OrbitControls | null = React.useMemo(() => {
if (camera && viewRef?.current) {
return new OrbitControls(camera as any, viewRef.current);
if (camera && enabled && config && viewRef?.current) {
return new OrbitControls(camera as any, enabled, config, viewRef.current);
}
return null;
}, [camera, viewRef?.current]);
}, [camera, enabled, config, viewRef?.current]);

React.useImperativeHandle(
ref,
Expand Down
4 changes: 1 addition & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
{
"extends": "expo-module-scripts/tsconfig.base",
"compilerOptions": {
"target": "es2020",
"outDir": "./build",
"jsx": "react"
"outDir": "./build"
},
"include": ["./src"],
"exclude": ["**/__mocks__/*", "**/__tests__/*"]
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3902,7 +3902,7 @@ expo-sqlite@~8.1.0:
"@types/websql" "^0.0.27"
lodash "^4.17.15"

[email protected]:
expo-three@^5.5.1:
version "5.5.1"
resolved "https://registry.yarnpkg.com/expo-three/-/expo-three-5.5.1.tgz#05caff7692f4ca84a3cb14115afe2824a7dce52e"
integrity sha512-OVWbBG2IKrnlBgTczqMdWQgNuMcll8JtIgsZgB48Ted7g8s6L3+70YRrbS44ZzteKs5l0+LFfT+xMGuE06KLEA==
Expand Down