I’m having issues rotating about the y-axis after making a lerp of the camera using camera-controls. Basically the rotation is not maintained about the same axis. I can share more info as the discussion goes.
this is some code from my index.js, maybe this is where the issue is .
It’s pretty urgent, any ideas are greatly appreciated.
import "./App.css";
import * as THREE from "three";
import { Suspense, useState,useRef } from "react";
import useStateSnapshots from "use-state-snapshots";
import { Canvas, extend, useThree, useFrame } from "@react-three/fiber";
import CameraControls from "camera-controls";
import Lights from "./Models/Lights";
import BoyHairstyle from "./Models/HairStyle/Boy";
import Face from "./Models/Face";
import { ElementObject, elements_objects } from "./Elements/swatch";
CameraControls.install({ THREE });
extend({ CameraControls });
function Foo() {
const { camera } = useThree();
camera.rotation.order = "YXZ";
console.log(camera.position);
}
function Controls({
zoom,
focus,
clicked,
setData,
pos = new THREE.Vector3(),
look = new THREE.Vector3(),
}) {
const ref = useRef();
const camera = useThree((state) => state.camera);
const gl = useThree((state) => state.gl);
useFrame((state, delta) => {
if (clicked) {
pos.set(focus[zoom].x, focus[zoom].y, focus[zoom].z);
look.set(focus[zoom].x, focus[zoom].y, focus[zoom].z - 2);
state.camera.position.lerp(pos, 1);
state.camera.updateProjectionMatrix();
ref.current.setLookAt(
state.camera.position.x,
state.camera.position.y,
state.camera.position.z,
look.x,
look.y,
look.z,
true
);
console.log("ENTERED");
setData((prevState) => ({
...prevState,
zoom: !clicked,
}));
}
ref.current.update(delta);
});
return <cameraControls ref={ref} args={[camera, gl.domElement]} />;
}
function Configurator({ history, checkGender }) {
const focusPosition = [
{
x: 0.0653451230347078,
y: 1.2835181387688754,
z: 2.8239068138582737,
},
{
x: 0,
y: 0,
z: 5,
},
];
console.log("RERENDERED CONFIGURATOR");
const [state, setState, pointer, setPointer] = useStateSnapshots(
{
appliedFace: "F1",
appliedHairColor: null,
appliedBoyHairStyle: "BHS2",
colorBHS: null,
textureBHS: null,
},
1000,
10
);
const [data, setData] = useState({
activeOption: 0,
boyColorActive: false,
activeElement: 0,
zoom: false,
});
return (
<>
<div className="App">
<div>
{elements_objects.map((options, index) => (
<ElementObject
key={index}
id={index}
options={options}
active={data.activeElement}
onClick={() => {
setData((prevState) => ({
...prevState,
activeElement: index,
zoom: !data.zoom,
}));
}}
/>
))}
</div>
<div>
<Suspense fallback={null}>
<Canvas flat>
<Foo />
<CameraHelper />
<Lights />
<Suspense fallback={null}>
<Face appliedFace={state.appliedFace} />
<BoyHairstyle
appliedBoyHairStyle={state.appliedBoyHairStyle}
appliedFace={state.appliedFace}
textureBHS={state.textureBHS}
colorBHS={state.colorBHS}
/>
</Suspense>
<Controls
zoom={data.activeElement}
focus={focusPosition}
clicked={data.zoom}
setData={setData}
/>
</Canvas>
</Suspense>
</div>
</div>
</>
);
}
export default Configurator;