Comments (10)
same bug
from camera-controls.
from camera-controls.
I needed this functionality as well, I took your demo and got it working: https://codesandbox.io/p/github/vinkovsky/switching-cameras-demo/csb-jzt6zp/draft/gallant-cannon?file=%2Fapp%2Fcamera-view.tsx. I commented out the automatic switching and just bound to 't' to toggle between to make debugging easier. its pretty satisfying to use 😄 . I also am using the getPosition()
from camera-controls rather than .position
as it's my understanding that's more correct.
from camera-controls.
@seanhouli Hello! Thanks for your reply. Unfortunately couldn't open your solution :(
from camera-controls.
@seanhouli Hello! Thanks for your reply. Unfortunately couldn't open your solution :(
Can you try again? I have not used codesandbox a ton, but under sharing it does say it's publicly accessible.
Also, upon playing with it more, there does seem to be issues with the swapping code itself giving strange results when you stress test it, probably because camera-controls has its own quirks
edit: sent a friend the same link and could open it just fine
from camera-controls.
@seanhouli still not working
from camera-controls.
@vinkovsky Here are the contents of the camera-view.tsx file if you still cannot use the link.
import { useEffect, useMemo } from "react";
import { useFrame, useThree } from "@react-three/fiber";
import {
PerspectiveCamera as PerspectiveCameraImpl,
OrthographicCamera as OrthographicCameraImpl,
Vector3,
Vector2,
Vector4,
Quaternion,
Matrix4,
Spherical,
Box3,
Raycaster,
Sphere,
EventDispatcher,
Scene,
} from "three";
import CameraControlsImpl from "camera-controls";
export function Camera() {
const gl = useThree((state) => state.gl);
const set = useThree((state) => state.set);
const get = useThree((state) => state.get);
const scene = useThree((state) => state.scene);
const controls = useMemo(
() => new Controls(gl.domElement, scene),
[gl.domElement, scene],
);
useEffect(() => {
const onkeydown = (e: KeyboardEvent) => {
console.log(e.key);
if (e.key.toLowerCase() === "t") {
controls.currentCamera === controls.perspectiveCamera
? controls.setOrthographicCamera()
: controls.setPerspectiveCamera();
}
};
window.addEventListener("keydown", onkeydown);
return () => {
window.removeEventListener("keydown", onkeydown);
};
}, [controls]);
useEffect(() => {
if (scene.children.length) {
fitCameraToSceneBoundingSphere(controls.object, scene);
}
console.log("scene.children.length", scene.children.length);
}, [controls.object, scene.children]);
useFrame((_, delta) => {
controls.update(delta);
gl.render(scene, controls.currentCamera);
}, -1);
useEffect(() => {
const oldControls = get().controls;
const oldCamera = get().camera;
set({
controls: controls.object as unknown as EventDispatcher,
camera: controls.currentCamera,
});
return () => set({ controls: oldControls, camera: oldCamera });
}, [controls, get, set]);
return (
<>
<primitive object={controls.object} />
<primitive object={controls.currentCamera} />
</>
);
}
// https://gist.github.com/nickyvanurk/9ac33a6aff7dd7bd5cd5b8a20d4db0dc
class Controls {
perspectiveCamera: PerspectiveCameraImpl;
orthographicCamera: OrthographicCameraImpl;
currentCamera: PerspectiveCameraImpl | OrthographicCameraImpl;
object: CameraControlsImpl;
constructor(
private container: HTMLElement,
private scene: Scene,
) {
this.perspectiveCamera = new PerspectiveCameraImpl(
70,
this.container.clientWidth / this.container.clientHeight,
0.1,
4000,
);
this.orthographicCamera = new OrthographicCameraImpl(
this.container.clientWidth / -2,
this.container.clientWidth / 2,
this.container.clientHeight / 2,
this.container.clientHeight / -2,
0.1,
4000,
);
this.currentCamera = this.perspectiveCamera;
const subsetOfTHREE = {
Vector2: Vector2,
Vector3: Vector3,
Vector4: Vector4,
Quaternion: Quaternion,
Matrix4: Matrix4,
Spherical: Spherical,
Box3: Box3,
Sphere: Sphere,
Raycaster: Raycaster,
};
CameraControlsImpl.install({ THREE: subsetOfTHREE });
this.object = new CameraControlsImpl(this.currentCamera, this.container);
this.object.setPosition(1, 1, 1);
this.object.setTarget(0, 0, 0);
this.object.restThreshold = 0.1;
this.object.dollyToCursor = true;
}
dispose() {
this.object.disconnect();
this.object.dispose();
}
update(delta: number) {
// if (this.object.polarAngle <= 0.01) {
// if (this.currentCamera.type === "PerspectiveCamera") {
// this.setOrthographicCamera();
// }
// } else if (this.currentCamera.type === "OrthographicCamera") {
// this.setPerspectiveCamera();
// }
this.object.update(delta);
}
private updateOrthographicCameraFrustum() {
const { distance } = this.object;
const halfWidth =
frustumWidthAtDistance(this.perspectiveCamera, distance) / 2;
const halfHeight =
frustumHeightAtDistance(this.perspectiveCamera, distance) / 2;
const halfSize = { x: halfWidth, y: halfHeight };
this.orthographicCamera.top = halfSize.y;
this.orthographicCamera.bottom = -halfSize.y;
this.orthographicCamera.left = -halfSize.x;
this.orthographicCamera.right = halfSize.x;
}
updateFrustum() {
this.perspectiveCamera.aspect =
this.container.clientWidth / this.container.clientHeight;
this.updateOrthographicCameraFrustum();
this.currentCamera.updateProjectionMatrix();
}
private lastPerspectivePosition: Vector3 = new Vector3();
setOrthographicCamera() {
const perspectivePos = this.object.getPosition(new Vector3());
this.updateOrthographicCameraFrustum();
this.currentCamera = this.orthographicCamera;
this.object.camera = this.currentCamera;
this.object.setPosition(
perspectivePos.x,
perspectivePos.y,
perspectivePos.z,
);
this.orthographicCamera.updateProjectionMatrix();
this.lastPerspectivePosition.copy(perspectivePos);
this.object.mouseButtons.wheel = CameraControlsImpl.ACTION.ZOOM;
}
setPerspectiveCamera() {
const orthoPos = this.object.getPosition(new Vector3());
const oldY = this.lastPerspectivePosition.y;
this.currentCamera = this.perspectiveCamera;
this.perspectiveCamera.updateProjectionMatrix();
this.object.camera = this.currentCamera;
this.object.setPosition(
orthoPos.x,
oldY / this.orthographicCamera.zoom,
orthoPos.z,
);
this.object.mouseButtons.wheel = CameraControlsImpl.ACTION.DOLLY;
}
}
function frustumHeightAtDistance(
camera: PerspectiveCameraImpl,
distance: number,
) {
const vFov = (camera.fov * Math.PI) / 180;
return Math.tan(vFov / 2) * distance * 2;
}
function frustumWidthAtDistance(
camera: PerspectiveCameraImpl,
distance: number,
) {
return frustumHeightAtDistance(camera, distance) * camera.aspect;
}
export const fitCameraToSceneBoundingSphere = (
controls: CameraControlsImpl,
scene: Scene,
) => {
const boundingBox = new Box3();
const center = new Vector3();
const sphere = new Sphere();
const defaultRadius = 1;
const bbox = boundingBox.setFromObject(scene);
if (controls.fitToSphere) {
// https://stackoverflow.com/a/63243915/11416728
controls.fitToSphere(
bbox.getBoundingSphere(sphere.set(bbox.getCenter(center), defaultRadius)),
true,
);
}
};
from camera-controls.
@vinkovsky it seems that reverting back to your original version that uses .position
is actually working better for me, no idea why. Here are the changes I have made
setOrthographicCamera() {
this.object.cancel()
// .... same as yours
this.object.mouseButtons.wheel = CameraControlsImpl.ACTION.ZOOM
}
setPerspectiveCamera() {
this.object.cancel()
// .... same as yours
this.object.mouseButtons.wheel = CameraControlsImpl.ACTION.DOLLY
}
still seeing weirdness when stress-testing hard and swapping back and forth, panning, orbiting aggressively
from camera-controls.
@seanhouli Thanks for the code snippet! I appreciate it. Yes, I also noticed differences in behavior between .position and .getPosition(). Thats why i used the first one
from camera-controls.
@seanhouli genius! it work!
from camera-controls.
Related Issues (20)
- Speed up setLookAt or lerpLookAt HOT 5
- Invalid "main" entry in v2.8.x HOT 10
- Add focus function HOT 7
- Error 'ThreeCameraControls.install is not a function' HOT 2
- Method to cancel ongoing transitions HOT 5
- Mouse wheel zoom doesn't work with pressed Shift on Mac HOT 1
- The automated release is failing 🚨 HOT 1
- Update three.js version to 162 HOT 2
- Not update in worker HOT 5
- Dolly to Cursor and focalOffset HOT 5
- I want to replace camera-controls with the default controls in my project, here is an example, how can I do it to be compatible correctly? HOT 2
- fitToBox on a plane: how to prevent plane to get perpendicular to view? HOT 1
- Dolly to cursor reaches limit HOT 4
- Dolly to cursor does not maintain steady speed HOT 1
- Roll/tilt control HOT 1
- Cannot dispatch custom event HOT 4
- It would be nice to have setFov method with smooth transition HOT 2
- Transition updateCameraUp HOT 1
- Control functions skip frames when frameloop = demand HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from camera-controls.