diff --git a/cli.js b/cli.js index 6d471ed..91d3517 100755 --- a/cli.js +++ b/cli.js @@ -20,6 +20,7 @@ const cli = meow( --types, -t Add Typescript definitions --keepnames, -k Keep original names --keepgroups, -K Keep (empty) groups, disable pruning + --bones, -b Lay out bones declaratively (default: false) --meta, -m Include metadata (as userData) --shadows, s Let meshes cast and receive shadows --printwidth, w Prettier printWidth (default: 120) @@ -47,6 +48,7 @@ const cli = meow( types: { type: 'boolean', shortFlag: 't' }, keepnames: { type: 'boolean', shortFlag: 'k' }, keepgroups: { type: 'boolean', shortFlag: 'K' }, + bones: { type: 'boolean', shortFlag: 'b', default: false }, shadows: { type: 'boolean', shortFlag: 's' }, printwidth: { type: 'number', shortFlag: 'p', default: 1000 }, meta: { type: 'boolean', shortFlag: 'm' }, @@ -61,7 +63,7 @@ const cli = meow( degraderesolution: { type: 'number', shortFlag: 'Q', default: 512 }, simplify: { type: 'boolean', shortFlag: 'S', default: false }, keepmeshes: { type: 'boolean', shortFlag: 'j', default: false }, - keepmaterials: { type: 'boolean', shortFlag: 'M', default: false }, + keepmaterials: { type: 'boolean', shortFlag: 'M', default: false }, format: { type: 'string', shortFlag: 'f', default: 'webp' }, exportdefault: { type: 'boolean', shortFlag: 'E' }, weld: { type: 'number', default: 0.0001 }, diff --git a/src/utils/parser.js b/src/utils/parser.js index d68627d..82329b1 100644 --- a/src/utils/parser.js +++ b/src/utils/parser.js @@ -109,7 +109,7 @@ function parse(gltf, { fileName = 'model', ...options } = {}) { const types = [...new Set([...meshes, ...bones].map((o) => getType(o)))] const contextType = `type ContextType = Record `JSX.IntrinsicElements['${type}']`).join(' | ')} - >>`; + >>` return `\ntype GLTFResult = GLTF & { nodes: { @@ -339,7 +339,7 @@ function parse(gltf, { fileName = 'model', ...options } = {}) { } // Bail out on lights and bones - if (type === 'bone') { + if (!options.bones && type === 'bone') { return `` } @@ -356,7 +356,8 @@ function parse(gltf, { fileName = 'model', ...options } = {}) { result = `' : '/>'}\n` // Add children and return - if (children.length) result += children + `` + if (children.length) { + if (type === 'bone') result += children + `` + else result += children + `` + } return result } @@ -445,8 +449,8 @@ ${parseExtras(gltf.parser.json.asset && gltf.parser.json.asset.extras)}*/` const result = `${options.types ? `\nimport * as THREE from 'three'` : ''} import React, { useRef ${hasInstances ? ', useMemo, useContext, createContext' : ''} } from 'react' import { useGLTF, ${hasInstances ? 'Merged, ' : ''} ${ - scene.includes('PerspectiveCamera') ? 'PerspectiveCamera,' : '' - } + scene.includes('PerspectiveCamera') ? 'PerspectiveCamera,' : '' + } ${scene.includes('OrthographicCamera') ? 'OrthographicCamera,' : ''} ${hasAnimations ? 'useAnimations' : ''} } from '@react-three/drei' ${options.types ? 'import { GLTF } from "three-stdlib"' : ''} @@ -458,8 +462,8 @@ ${parseExtras(gltf.parser.json.asset && gltf.parser.json.asset.extras)}*/` const context = createContext(${options.types ? '{} as ContextType' : ''}) export function Instances({ children, ...props }${options.types ? ': JSX.IntrinsicElements["group"]' : ''}) { const { nodes } = useGLTF('${url}'${options.draco ? `, ${JSON.stringify(options.draco)}` : ''})${ - options.types ? ' as GLTFResult' : '' - } + options.types ? ' as GLTFResult' : '' + } const instances = useMemo(() => ({ ${Object.values(duplicates.geometries) .map((v) => `${v.name}: ${v.node}`) @@ -467,7 +471,9 @@ ${parseExtras(gltf.parser.json.asset && gltf.parser.json.asset.extras)}*/` }), [nodes]) return ( - {(instances${options.types ? ': ContextType' : ''}) => } + {(instances${ + options.types ? ': ContextType' : '' + }) => } ) } @@ -476,17 +482,17 @@ ${parseExtras(gltf.parser.json.asset && gltf.parser.json.asset.extras)}*/` } export ${options.exportdefault ? 'default' : ''} function Model(props${ - options.types ? ": JSX.IntrinsicElements['group']" : '' - }) { + options.types ? ": JSX.IntrinsicElements['group']" : '' + }) { ${hasInstances ? 'const instances = useContext(context);' : ''} ${ - hasAnimations ? `const group = ${options.types ? 'useRef()' : 'useRef()'};` : '' - } ${ - !options.instanceall - ? `const { nodes, materials${hasAnimations ? ', animations' : ''} } = useGLTF('${url}'${ - options.draco ? `, ${JSON.stringify(options.draco)}` : '' - })${options.types ? ' as GLTFResult' : ''}` - : '' - } ${printAnimations(animations)} + hasAnimations ? `const group = ${options.types ? 'useRef()' : 'useRef()'};` : '' + } ${ + !options.instanceall + ? `const { nodes, materials${hasAnimations ? ', animations' : ''} } = useGLTF('${url}'${ + options.draco ? `, ${JSON.stringify(options.draco)}` : '' + })${options.types ? ' as GLTFResult' : ''}` + : '' + } ${printAnimations(animations)} return ( ${scene}