trois icon indicating copy to clipboard operation
trois copied to clipboard

InstancedMesh use GLTF

Open oneWaveAdrian opened this issue 3 years ago • 3 comments

Discovered another GLTF oddity, when trying to load a GLTF instead of a standard Geometry, the <InstancedMesh /> will not load correct. Should work according to Three.js example, so I think the

https://github.com/troisjs/trois/blob/master/src/meshes/InstancedMesh.ts

needs to be updated to reflect that. I know I could do it with raw THREE code, but I think it would make more sense syntax wise if <InstancedMesh /> supports GLTF & FBX

oneWaveAdrian avatar Jun 22 '21 22:06 oneWaveAdrian

I don't know if this helps, but I made a temporary workaround using GLTF's @load event as such:

<GltfModel
  src="assets/models/common/potplant.glb"
  :visible="false"
  @load="instanceMe($event, 5, 'room1')"
 />
import {
  InstancedMesh,
  MathUtils,
  Object3D,
  DynamicDrawUsage,
} from 'three'

[...]

methods:{
instanceMe(gltf, count, groupname) {
      const geo = gltf.children[0].geometry.clone()
      const mat = gltf.children[0].material
      let instanced = new InstancedMesh(geo, mat, count)

      instanced.instanceMatrix.setUsage(DynamicDrawUsage)
      const { randFloat: rnd, randFloatSpread: rndFS } = MathUtils
      const dummy = new Object3D()
      for (let i = 0; i < count; i++) {
        dummy.position.set(rndFS(10), rndFS(10), rndFS(10))
        dummy.updateMatrix()
        instanced.setMatrixAt(i, dummy.matrix)
      }
      instanced.instanceMatrix.needsUpdate = true
      if (groupname) {
        this.$refs.scene.scene.children.forEach((e) => {
          if (e.name === groupname) e.add(instanced)
        })
      } else {
        this.$refs.scene.scene.add(instanced)
      }
    },
[...]
}

It works but it's not pretty - would really appreciate a proper Trois solution for this 😅

oneWaveAdrian avatar Jun 23 '21 02:06 oneWaveAdrian

Good idea, I can take this one on - thanks for the workaround in the meantime!

SaFrMo avatar Jun 23 '21 18:06 SaFrMo

Hey @oneWaveAdrian ! Apologies for the delay on this, I thought I'd have more time but haven't had a chance to tackle it yet. I'm unassigning it from myself for now and will take another look when I have some more time - other suggestions or PRs welcome in the meantime. Thanks!

SaFrMo avatar Jul 02 '21 20:07 SaFrMo