pmndrs / gltfjsx

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

remove empty <group> tags #138

Closed ghost closed 2 years ago

ghost commented 2 years ago

I've noticed after converting that sometimes there are a lot of unused tags. For example, this tree: https://sketchfab.com/3d-models/elm-tree-low-poly-1445aa109a344dc6963a123fd0fe48ac results in:

    <group ref={group} {...props} dispose={null}>
      <group rotation={[-Math.PI / 2, 0, 0]}>
        <group rotation={[Math.PI / 2, 0, 0]}>
          <group position={[-25.42, 37.95, -5.86]} rotation={[-0.14, -0.42, 1.02]} scale={[100, 100, 100]}>
            <group rotation={[Math.PI / 2, 0, 0]} />
          </group>
          <group position={[-25.42, 37.95, -5.86]} rotation={[0.36, 0.11, -0.89]} scale={[100, 100, 100]}>
            <group rotation={[Math.PI / 2, 0, 0]} />
          </group>
          <group position={[-318.77, 438.34, -322.15]} rotation={[-Math.PI / 2, 0, 0]} scale={[100, 100, 100]}>
            <mesh geometry={nodes['Icosphere001_mattree-elm_0'].geometry} material={materials['mat.tree-elm']} />
          </group>
          <group position={[407.62, 590.39, -100.55]} rotation={[1.89, 0.88, -2.05]} scale={[100, 100, 100]}>
            <group rotation={[Math.PI / 2, 0, 0]} />
          </group>
          <group position={[748.11, 534.37, 650.76]} rotation={[3.13, 0.76, 2.69]} scale={[100, 100, 100]} />
        </group>
      </group>
    </group>

But I've manually cleaned it up to this and it's still working fine.

    <group ref={group} {...props} dispose={null}>
      <group
        position={[-318.77, 438.34, -322.15]}
        rotation={[-Math.PI / 2, 0, 0]}
        scale={[100, 100, 100]}
      >
        <mesh
          geometry={nodes["Icosphere001_mattree-elm_0"].geometry}
          material={materials["mat.tree-elm"]}
          castShadow
        />
      </group>
    </group>

Using this command for the conversion npx gltfjsx scene.gltf --types

It could be the result of a 'messy' export or conversion in the model, I've come across this often. Perhaps the tool can do a post cleanup, removing empty/ useless groups.

drcmda commented 2 years ago

you can try the "-a" options now, or "--aggressive". and it really is aggressive, i'm a bit scared it could kick something over when making it the default. it goes further than your manual attempt:

<group ref={group} {...props} dispose={null}>
  <mesh
    geometry={nodes['Icosphere001_mattree-elm_0'].geometry}
    material={materials['mat.tree-elm']}
    position={[-318.77, 438.34, -322.15]}
    rotation={[-Math.PI / 2, 0, 0]}
    scale={100}
  />
</group>
ghost commented 2 years ago

Thanks! Tested it with another model and the output is a lot cleaner.

    <group ref={group} {...props} dispose={null}>
      <mesh geometry={nodes['Icosphere000_mattree-laurel_0'].geometry} material={materials['mat.tree-laurel']} rotation={[-Math.PI / 2, 0, 0]} scale={100} />
    </group>
donmccurdy commented 2 years ago

Related: https://github.com/donmccurdy/glTF-Transform/issues/463