From 14ff8a32f26b44770ad40255865df7042574ac1c Mon Sep 17 00:00:00 2001 From: Marcel Wiessler Date: Sun, 30 Jun 2024 12:15:54 +0200 Subject: [PATCH 1/2] Add support for KTX2 loader --- src/utils/parser.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/utils/parser.js b/src/utils/parser.js index 82329b1..d975ef1 100644 --- a/src/utils/parser.js +++ b/src/utils/parser.js @@ -16,6 +16,9 @@ function parse(gltf, { fileName = 'model', ...options } = {}) { // Collect all objects const objects = [] gltf.scene.traverse((child) => objects.push(child)) + const json = gltf.parser.json; + const needsDraco = json.extensionsRequired && json.extensionsRequired.includes('KHR_draco_mesh_compression'); + const needsKTX2Loader = json.extensionsRequired && json.extensionsRequired.includes('KHR_texture_basisu'); // Browse for duplicates const duplicates = { @@ -454,6 +457,9 @@ ${parseExtras(gltf.parser.json.asset && gltf.parser.json.asset.extras)}*/` ${scene.includes('OrthographicCamera') ? 'OrthographicCamera,' : ''} ${hasAnimations ? 'useAnimations' : ''} } from '@react-three/drei' ${options.types ? 'import { GLTF } from "three-stdlib"' : ''} + ${needsKTX2Loader ? 'import { REVISION } from "three"' : ''} + ${needsKTX2Loader ? 'import { useThree } from "@react-three/fiber"' : ''} + ${needsKTX2Loader ? 'import { KTX2Loader } from "three-stdlib"' : ''} ${options.types ? printTypes(objects, animations) : ''} ${ @@ -489,8 +495,16 @@ ${parseExtras(gltf.parser.json.asset && gltf.parser.json.asset.extras)}*/` } ${ !options.instanceall ? `const { nodes, materials${hasAnimations ? ', animations' : ''} } = useGLTF('${url}'${ - options.draco ? `, ${JSON.stringify(options.draco)}` : '' - })${options.types ? ' as GLTFResult' : ''}` + needsDraco ? ", true" : options.draco ? `, ${JSON.stringify(options.draco)}` : ', false' + } + , false, ${needsKTX2Loader ? `(loader) => { + const { gl } = useThree(); + const THREE_PATH = \`https://unpkg.com/three@0.$\{REVISION\}.x\`; + const ktx2Loader = new KTX2Loader().setTranscoderPath(\`$\{THREE_PATH\}/examples/jsm/libs/basis/\`); + ktx2Loader.detectSupport(gl); + loader.setKTX2Loader(ktx2Loader); + }` : ''} + )${options.types ? ' as GLTFResult' : ''}` : '' } ${printAnimations(animations)} return ( @@ -500,7 +514,7 @@ ${parseExtras(gltf.parser.json.asset && gltf.parser.json.asset.extras)}*/` ) } -useGLTF.preload('${url}')` +${ /* Can't use preload with KTX2 */ needsKTX2Loader ? '' : `useGLTF.preload('${url}')` }` console.log(header) const output = header + '\n' + result From 25172903bb785bebb047c9136736b56f8b290980 Mon Sep 17 00:00:00 2001 From: Marcel Wiessler Date: Sun, 30 Jun 2024 12:23:11 +0200 Subject: [PATCH 2/2] enable meshopt loader if necessary --- src/utils/parser.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/parser.js b/src/utils/parser.js index d975ef1..36262a9 100644 --- a/src/utils/parser.js +++ b/src/utils/parser.js @@ -18,6 +18,7 @@ function parse(gltf, { fileName = 'model', ...options } = {}) { gltf.scene.traverse((child) => objects.push(child)) const json = gltf.parser.json; const needsDraco = json.extensionsRequired && json.extensionsRequired.includes('KHR_draco_mesh_compression'); + const needsMeshopt = json.extensionsUsed && json.extensionsUsed.includes('KHR_meshopt_compression'); const needsKTX2Loader = json.extensionsRequired && json.extensionsRequired.includes('KHR_texture_basisu'); // Browse for duplicates @@ -497,7 +498,7 @@ ${parseExtras(gltf.parser.json.asset && gltf.parser.json.asset.extras)}*/` ? `const { nodes, materials${hasAnimations ? ', animations' : ''} } = useGLTF('${url}'${ needsDraco ? ", true" : options.draco ? `, ${JSON.stringify(options.draco)}` : ', false' } - , false, ${needsKTX2Loader ? `(loader) => { + , ${needsMeshopt ? 'true' : 'false'}, ${needsKTX2Loader ? `(loader) => { const { gl } = useThree(); const THREE_PATH = \`https://unpkg.com/three@0.$\{REVISION\}.x\`; const ktx2Loader = new KTX2Loader().setTranscoderPath(\`$\{THREE_PATH\}/examples/jsm/libs/basis/\`);