Coder Social home page Coder Social logo

Comments (7)

ammanvedi avatar ammanvedi commented on May 20, 2024 5

just got this working here is my final code, seems to be working nicely thank you to both above i was trying to get a nice solution for dollyToCursor with OrbitControls with no luck and this one is perfect, i can go to bed now XD

import React from 'react';
import * as THREE from 'three';
import { extend, ReactThreeFiber, useFrame, useThree } from '@react-three/fiber';
import CameraControls from 'camera-controls';

CameraControls.install({ THREE: THREE });

extend({ CameraControls });

declare global {
    namespace JSX {
        interface IntrinsicElements {
            cameraControls: ReactThreeFiber.Object3DNode<
                CameraControls,
                typeof CameraControls
            >;
        }
    }
}

export const CustomOrbitControls: React.FC<any> = React.forwardRef((props, ref) => {
    const { camera, gl } = useThree();
    useFrame((state, delta) => {
        if (ref.current) {
            ref.current.update(delta);
        }
    });
    return (
        <cameraControls
            ref={ref}
            args={[camera, gl.domElement]}
            dollyToCursor
            mouseButtons={{
                left: CameraControls.ACTION.TRUCK,
                middle: CameraControls.ACTION.NONE,
                right: CameraControls.ACTION.NONE,
                shiftLeft: CameraControls.ACTION.NONE,
                wheel: CameraControls.ACTION.ZOOM,
            }}
        />
    );
});

from camera-controls.

yomotsu avatar yomotsu commented on May 20, 2024 3

Seems delta of every frame in your app is too small. delta is supposed to be 0.01666... sec or so.
Screen Shot 2020-02-29 at 10 52 27 AM

You can use getElapsedTime() to get correct delta, rather than getDelta().

const CControls: React.FC<any> = () => {
  const ccontrolRef = useRef<any>(null);
  const { camera, gl, clock, scene } = useThree();
  let lastTime = 0;
  useFrame(() => {
    if (ccontrolRef.current) {
      const elapsed = clock.getElapsedTime();
      const delta = elapsed - lastTime;
      console.log( delta );
      //   ccontrolRef.current.update();
      const hasControlsUpdated = ccontrolRef.current.update(delta);
      if (hasControlsUpdated) gl.render(scene, camera);

      lastTime = elapsed;
    }
  });
  return <cameraControls ref={ccontrolRef} args={[camera, gl.domElement]} />;
};

If you call getDelta() somewhere else, the delta time will be the diff between "somewhere" and the useFrame.
I think this is not a problem of camera-controls.

from camera-controls.

kurosh-z avatar kurosh-z commented on May 20, 2024 1

I don't know about internals of react-three-fiber but I don't use getDelta() in my app yet!
anyway that definitely solved the problem!
thanks again for investigating the issue.

from camera-controls.

jfabraxas avatar jfabraxas commented on May 20, 2024 1

FYI: Second arg of useFrame callback is delta:

 useFrame((state, delta) => {
    if (ccontrolRef.current) {
      const hasControlsUpdated = ccontrolRef.current.update(delta);
      if (hasControlsUpdated) gl.render(scene, camera);
    }
  });

rf3 doc

from camera-controls.

yomotsu avatar yomotsu commented on May 20, 2024

Did you try without camera controls?
Did you also try with threejs official orbit controls?

Then if still it happens, can you provide a simplified working example on jsfiddle or somewhere? Otherwise I need to prepare the environment to reproduce.

from camera-controls.

kurosh-z avatar kurosh-z commented on May 20, 2024

Thank you for the speedy reply!
yes I have used OrbitControls and it works fine!
here is the codesanbox version with some simplifications. you can click on the button to switch the controllers.
https://codesandbox.io/s/blissful-chandrasekhar-ew99r

from camera-controls.

yaroslavnikiforov avatar yaroslavnikiforov commented on May 20, 2024

You don't need this line:
if (hasControlsUpdated) gl.render(scene, camera);

from camera-controls.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.