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

Add support for KTX2 loader #266

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/utils/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ 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 needsMeshopt = json.extensionsUsed && json.extensionsUsed.includes('KHR_meshopt_compression');
const needsKTX2Loader = json.extensionsRequired && json.extensionsRequired.includes('KHR_texture_basisu');

// Browse for duplicates
const duplicates = {
Expand Down Expand Up @@ -454,6 +458,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) : ''}

${
Expand Down Expand Up @@ -489,8 +496,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'
}
, ${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/\`);
ktx2Loader.detectSupport(gl);
loader.setKTX2Loader(ktx2Loader);
}` : ''}
)${options.types ? ' as GLTFResult' : ''}`
: ''
} ${printAnimations(animations)}
return (
Expand All @@ -500,7 +515,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
Expand Down