pmndrs / gltfjsx

🎮 Turns GLTFs into JSX components
https://gltf.pmnd.rs
MIT License
4.41k stars 290 forks source link

Prepass to de-duplicate object names at JSX generation time #231

Open donmccurdy opened 7 months ago

donmccurdy commented 7 months ago

Currently gltfjsx relies on matching the exact implementation of GLTFLoader's createUniqueName ...

https://github.com/pmndrs/gltfjsx/blob/8bf87e1913b941ae16a1c57817d08e865f8568f6/src/bin/GLTFLoader.js#L1911C1-L1924

... to ensure that names in JSX match names GLTFLoader will output, given the same file. However, it's difficult for three.js to guarantee indefinitely that the same .glb file will always result in the same names in future releases. When the file contains duplicate names, GLTFLoader must assign suffixes like _1 or _2, and refactoring or performance changes in GLTFLoader could change the order in which suffixes are assigned.

I'd like to add a step to gltfjsx, pre-processing the input to ensure unique names, so that GLTFLoader's createUniqueName will have nothing to de-duplicate at runtime. That would be an additional step in src/utils/transform.js. Are others OK with this change?

Related:

donmccurdy commented 7 months ago

Picking up from https://github.com/pmndrs/react-three-fiber/issues/2498, another issue to consider is that three.js must clone materials when they're reused by incompatible meshes. Presence of vertex colors and vertex tangents would be one example of this. The same prepass could do this change as well, outputting a GLB with N copies of a reused material, each having a unique name.

CodyJasonBennett commented 7 months ago

I don't see a better alternative than mutating the GLB. I'd have to concur with this.