Add gltf object in mesh

Hello, how can I add the gltf object into the mesh;
here is my code

var objectLoader = new THREE.GLTFLoader();
objectLoader.load("model/slk.glb", function(gltf) {

  gltf.scene.rotateY(Math.PI / 2);
  gltf.scene.scale.set(1.5, 1.5, 1.5);
  gltf.scene.position.set(0, 0, -1.5); = 'obje';

  obj = gltf.scene;


var basicMaterial = new THREE.MeshBasicMaterial({
  color: 0xff0000

var issMesh = new THREE.Mesh(obj, basicMaterial);
issMesh.position.set(0, 5, -5);

you can’t put a scene into a meshes geometry slot. you must traverse your scene, fish out the geometry that you want to use, then wrap it into a mesh.

const robot = scene.getObjectByName("robot")
const mesh = new THREE.Mesh(robot.geometry, new THREE.MeshStandardMaterial(...))

the problem with this is that you loose all intermediary transforms.

to my knowledge the only tool that can do it with ease is gltfjsx, react users treat gltf’s that way as if they were svg’s: plain three will most likely not have something like that due to it’s imperative nature, for react it’s easy on the other hand to express a gltf as a nested graph of objects.

1 Like