Hi there, I am creating a threejs scene in which a user can import as many models as he want.
The issue is that if I upload a different modal then normal it rotates itself in different directions.
Is there a snippet or way to make the model rotate to a specific direction regardless of it original rotation?
here my modal code:
function loadLeePerrySmith() {
const loader = new GLTFLoader();
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath("/examples/jsm/libs/draco/");
loader.setDRACOLoader(dracoLoader);
loader.load(
"https://cdn.glitch.me/dc99a522-a347-4f3e-a04f-210e55dccd91/teethtall1.glb?v=1709205022462",
function (gltf) {
mesh = gltf.scene.children[0];
mesh.material = new THREE.MeshPhongMaterial({
specular: 0x111111,
shininess: 5,
side: THREE.DoubleSide,
clippingPlanes: [localPlane],
alphaToCoverage: true,
});
mesh.rotation.x = Math.PI * -0.5;
mesh.scale.set(1, 1, 1);
mesh.updateMatrixWorld(true);
let bounds = new THREE.Box3();
bounds.setFromObject(mesh);
console.log(bounds)
mesh.position.sub(bounds.getCenter(new THREE.Vector3()));
{
let bounds = new THREE.Box3().setFromObject(mesh);
let size = bounds.getSize(new THREE.Vector3());
let { max } = Math;
let maxsz = max(size.x, max(size.y, size.z));
//Rescale the model..
mesh.scale.multiplyScalar(100 / maxsz);
mesh.needsAutoUpdate = true;
mesh.updateMatrixWorld();
bounds.setFromObject(mesh);
mesh.position.sub(bounds.getCenter(new THREE.Vector3()));
}
stretchMesh = () => {
if (!mesh.geometry.savePosition) {
mesh.geometry.savePosition =
mesh.geometry.attributes.position.array.slice(0);
mesh.originalBounds = mesh.geometry.boundingBox.clone();
}
let bx = mesh.originalBounds; //mesh.geometry.boundingBox;
console.log(bx);
let vin = mesh.geometry.savePosition;
let vout = mesh.geometry.attributes.position.array;
let location = startingPlane.constant;
let v0 = new THREE.Vector3();
let localNormal = new THREE.Vector3();
localNormal
.copy(startingPlane.normal)
.multiplyScalar(location)
.add(mesh.position);
mesh.worldToLocal(localNormal);
let nd = localNormal.length();
localNormal.normalize();
for (let i = 0; i < vin.length; i += 3) {
let x = vin[i];
let y = vin[i + 1];
let z = vin[i + 2];
v0.set(x, y, z);
let dsc = -v0.dot(localNormal) - nd;
//imma fix this.. 1 sec
//if (z < bx.min.z + location)
if (dsc > 0) z -= length;
vout[i] = x;
vout[i + 1] = y;
vout[i + 2] = z;
}
mesh.geometry.attributes.position.needsUpdate = true;
};
scene.add(mesh);
}
);
}
The centering and scaling of the modal is also doing something same special thanks to Thrax for that.