Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Instance" plus "meshopt" seems to accidentally delete instanced meshes when used with NodeJS and ThreeJS #1584

Open
kitaedesigns opened this issue Dec 12, 2024 · 5 comments · May be fixed by #1586
Labels
bug Something isn't working package:functions

Comments

@kitaedesigns
Copy link

kitaedesigns commented Dec 12, 2024

"Instance" command seems to delete meshes sometimes where post optimization the instanced assets will disappear from the model. The file in question works with gltf.report but not with gltf-transform being used in three.js via WebIO. It's strange because other assets coming from Blender seem to be fine and utilize mesh_gpu_instancing correctly. This particular asset was generated with Unreal, perhaps something in their node hierarchy is messing things up. I've provided a link to the asset below. Removing Instance() from my code has the asset appear correctly.

ASSET
https://www.dropbox.com/scl/fi/fxv1ipfo9ujwgqjim3odf/TrackerlessStudio.glb?rlkey=2uerxpwfzxw8equyqorauxi1d&st=fih69joj&dl=0

  • Version: [ gltf-transform v4.1.1]
  • Environment: [ Browser, Node.js, Three.js ]

CODE

import { Document, WebIO } from '@gltf-transform/core';
import { reorder, quantize, meshopt, dedup, draco, flatten, instance, join, palette, prune, resample, simplify, sparse, textureCompress, weld, unlit, metalRough } from '@gltf-transform/functions';
import {KHRDracoMeshCompression, ALL_EXTENSIONS } from '@gltf-transform/extensions'
import draco3d from 'draco3dgltf';
import { EXTMeshoptCompression } from '@gltf-transform/extensions';
import { MeshoptDecoder, MeshoptEncoder, MeshoptSimplifier } from 'meshoptimizer';


// Read the GLB file using glTF-Transform's WebIO
const io = new WebIO({credentials: 'include'})
	.registerExtensions(ALL_EXTENSIONS)
	.registerDependencies({
		'meshopt.decoder': MeshoptDecoder,
		'meshopt.encoder': MeshoptEncoder,
		'draco3d.decoder': await draco3d.createDecoderModule(), // Optional.
		'draco3d.encoder': await draco3d.createEncoderModule(), // Optional.
	});

const file_in = await io.readBinary(uint8Array);  // Uint8Array → Document

file_in.createExtension(EXTMeshoptCompression)
	.setRequired(true)
	.setEncoderOptions({ method: EXTMeshoptCompression.EncoderMethod.FILTER });

await file_in.transform(
	// Remove duplicate meshes, materials, textures, etc.
	dedup(),
	// instance({ min: 5 }),
	palette({ min: 5 }),
	flatten(),
	join(), 
	weld(),

	resample(), 
	prune(),
	sparse({ ratio: 0.2 }), 
	meshopt({encoder: MeshoptEncoder, level: 'high'}),
	reorder({encoder: MeshoptEncoder}),
	quantize()
);
@kitaedesigns kitaedesigns added the bug Something isn't working label Dec 12, 2024
@kitaedesigns
Copy link
Author

Tested this with the CLI and got the same result, might have something to do with 'meshopt' and the other transforms happening in the same document.

image
image

@kitaedesigns kitaedesigns changed the title "Instance" command seems to accidentally delete instanced meshes when used with NodeJS and ThreeJS "Instance" plus "meshopt" seems to accidentally delete instanced meshes when used with NodeJS and ThreeJS Dec 13, 2024
@kitaedesigns
Copy link
Author

Tested with gltf.report again running all optimize commands and then exporting with Meshopt and it produces the same error.

@kitaedesigns
Copy link
Author

I can't get it to happen with an asset exported out of Blender that has instanced meshes, so I think it may just be a bug with Unreal's GLTF Exporter.

@donmccurdy
Copy link
Owner

@kitaedesigns thanks! Fix in progress:

Possible workarounds in the meantime would be either of:

  • (A) skip the 'instance' step
  • (B) skip compression or use Draco instead

@kitaedesigns
Copy link
Author

Thanks @donmccurdy !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working package:functions
Projects
None yet
2 participants