Hello, so far I’ve been creating my geometries exclusively in ThreeJS, deforming them, and then animating them.
Now I want to load an OBJ or GLTF and then generate a variety of these elements from it.
If I simply add the model to the scene, it works perfectly.
var objLoader = new OBJLoader();
objLoader.setMaterials(materials);
objLoader.load('/obj/myObject.obj', function (object) {
scene.add(object);
object.position.z = 100;
object.position.y = 10;
object.rotation.x = 250;
});
Unfortunately, I can’t figure out how to load the model into a variable that I can then add to any Object3D. Here’s a simplified example of a box:
const createBox = () => {
let boxGeometry = new THREE.BoxGeometry(1, 1, 1);
let boxMaterial = new THREE.MeshStandardMaterial({
color: 0x33ff33,
shading: THREE.FlatShading,
});
let boxMesh = new THREE.Mesh(boxGeometry, boxMaterial);
let box3D = new THREE.Object3D();
box3D.add(boxMesh);
return box3D;
}
I can now generate as many of this box as I want and add them to the scene.
How does this work with the .obj file?
Can I somehow load the .obj into a variable, then I could extract the individual geometries and create a mesh with material? Or is there a way to get an Object3D from the loader?
I was now able to load the files and then modify them.
Alternatively, since the project is implemented in VueJs, I loaded all the .obj files into an array before starting the scene and then parsed them with the objloader.