diff --git a/src/lab/QuestCubeLab.tsx b/src/lab/QuestCubeLab.tsx index 794a3b2..d5201d5 100644 --- a/src/lab/QuestCubeLab.tsx +++ b/src/lab/QuestCubeLab.tsx @@ -112,7 +112,12 @@ const CubeControlado = ({ ry = gpRight?.axes[3] ?? 0; lt = gpLeft?.buttons[1]?.value ?? 0; rt = gpRight?.buttons[1]?.value ?? 0; - resetBtn = gpRight?.buttons[4]?.pressed || gpRight?.buttons[5]?.pressed || false; + resetBtn = + gpRight?.buttons[4]?.pressed || + gpRight?.buttons[5]?.pressed || + gpLeft?.buttons[4]?.pressed || + gpLeft?.buttons[5]?.pressed || + false; } else { // Desktop nativo lx = gpDesktop!.axes[0] ?? 0; @@ -171,17 +176,35 @@ const CubeControlado = ({ { - posRef.current.x = pos.x; - posRef.current.y = Math.max(0.5, pos.y); - posRef.current.z = pos.z; + onSync={(deltaPos, deltaQuat, deltaScale) => { + // Matriz atual do cubo + const mMesh = new THREE.Matrix4().compose( + new THREE.Vector3(posRef.current.x, posRef.current.y, posRef.current.z), + new THREE.Quaternion().setFromEuler(new THREE.Euler(rotRef.current.x, rotRef.current.y, rotRef.current.z, 'YXZ')), + new THREE.Vector3(scaleRef.current, scaleRef.current, scaleRef.current) + ); - const euler = new THREE.Euler().setFromQuaternion(quat, 'YXZ'); + // Matriz delta vinda do arrasto (XRGrabbable group) + const mGroup = new THREE.Matrix4().compose(deltaPos, deltaQuat, deltaScale); + + // Multiplica o offset (group) pelo local (mesh) + const mCombined = new THREE.Matrix4().multiplyMatrices(mGroup, mMesh); + + const finalPos = new THREE.Vector3(); + const finalQuat = new THREE.Quaternion(); + const finalScale = new THREE.Vector3(); + mCombined.decompose(finalPos, finalQuat, finalScale); + + posRef.current.x = finalPos.x; + posRef.current.y = Math.max(0.5, finalPos.y); + posRef.current.z = finalPos.z; + + const euler = new THREE.Euler().setFromQuaternion(finalQuat, 'YXZ'); rotRef.current.x = euler.x; rotRef.current.y = euler.y; rotRef.current.z = euler.z; - scaleRef.current = scale.x; + scaleRef.current = finalScale.x; }} >