this is three js code convert into react three fiber " let camera, scene, renderer;…
- User
this is three js code convert into react three fiber " let camera, scene, renderer;
const mouse = new THREE.Vector2(); const target = new THREE.Vector2(); const windowHalf = new THREE.Vector2( window.innerWidth / 2, window.innerHeight / 2 );
init(); animate();
function init() {
codecamera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 500 ); camera.position.z = 50; scene = new THREE.Scene(); const geometry = new THREE.BoxBufferGeometry(2, 2, 2); const material = new THREE.MeshNormalMaterial(); for ( let i = 0; i < 150; i ++ ) { const object = new THREE.Mesh( geometry, material ); object.position.x = Math.random() * 80 - 40; object.position.y = Math.random() * 80 - 40; object.position.z = Math.random() * 80 - 40; object.rotation.x = Math.random() * 2 * Math.PI; object.rotation.y = Math.random() * 2 * Math.PI; object.rotation.z = Math.random() * 2 * Math.PI; scene.add( object ); } renderer = new THREE.WebGLRenderer( { antialias: true } ); renderer.setSize( window.innerWidth, window.innerHeight ); document.body.appendChild( renderer.domElement ); document.addEventListener( 'mousemove', onMouseMove, false ); document.addEventListener( 'wheel', onMouseWheel, false ); window.addEventListener( 'resize', onResize, false );}
function onMouseMove( event ) {
codemouse.x = ( event.clientX - windowHalf.x ); mouse.y = ( event.clientY - windowHalf.x );}
function onMouseWheel( event ) {
camera.position.z += event.deltaY * 0.01; // move camera along z-axis
}
function onResize( event ) {
codeconst width = window.innerWidth; const height = window.innerHeight; - Assistant
- User
import { Canvas, useFrame } from "@react-three/fiber"; import { Environment, Loader, PerspectiveCamera } from "@react-three/drei"; import { Suspense, useRef } from "react"; import { Scene } from "./components/models/Scene"; import Water from "./components/models/Water"; import { PerspectiveCamera as PC, Vector2 } from "three";
const Camera = ({ mouse }: { mouse: React.MutableRefObject }) => { const target = useRef(new Vector2()); const cameraRef = useRef(null);
useFrame(() => { target.current.x = (1 - mouse.current.x) * 0.0002; target.current.y = (1 - mouse.current.y) * 0.0002;
codeconst camera = cameraRef.current!; camera.rotation.x += 0.05 * (target.current.y - camera.rotation.x); camera.rotation.y += 0.05 * (target.current.x - camera.rotation.y);});
return ( <PerspectiveCamera ref={cameraRef} makeDefault position={[-2.5, 1, 0]} rotation={[0, -Math.PI * 0.4, 0]} /> ); };
- Assistant
- User
camera.y rotation not start on -math.pi *0.4 this
- Assistant
If you want the initial
yrotation of your camera to start at-Math.PI * 0.4, and it isn't doing so, we should check a few things. The likely causes could be related to how React Three Fiber sets up the camera or the order of operations within your component setup.