trois
trois copied to clipboard
InstancedMesh use GLTF
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
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 😅
Good idea, I can take this one on - thanks for the workaround in the meantime!
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!