question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Removing Generics from Meshes causes a lot of headaches

See original GitHub issue

Describe the bug

For some context, i’m using react-three-fiber. My use case is when i’m loading a GLTF model, to TS it’s a black box, So I know it’s a mesh & can typecast the return of the gltf to be Mesh. However, when I want to edit the properties of the mesh materials, I now have to typecast the material because Mesh is no longer a generic. This is quite a headache. I’m not saying i’m right, it’d be good to get some clarity on why this commit was done?

Old Code

type GLTFResult = GLTF &
  ObjectMap & {
    nodes: {
      [name: string]: Mesh<BufferGeometry, MeshStandardMaterial>
    }
  }

  const { nodes } = useGLTF(
    `${ENV_MODEL_PATH}/rain/RainPond_Droplets_Polyredux.gltf`
  ) as GLTFResult

  const instanceMeshes = React.useMemo(
    () =>
      meshes
        .filter((mesh) => mesh.material)
        .map((mesh, i) => {
          mesh.material.metalness = 0 // all good here, no typecast needed
          mesh.material.transparent = true
          mesh.material.onBeforeCompile = compileMaterialForInstance

          mesh.geometry.setAttribute(
            'instOpacity',
            new InstancedBufferAttribute(
              new Float32Array(new Array(INSTANCE_COUNT).fill(0)),
              1
            )
          )

          return (
            <instancedMesh
              ref={(ref: THREE.InstancedMesh) =>
                setKeyValue(instanceRefs.current, i, ref)
              }
              key={mesh.name}
              args={[mesh.geometry, mesh.material, INSTANCE_COUNT]}
            />
          )
        }),
    [meshes]
  )

New Code

type GLTFResult = GLTF &
  ObjectMap & {
    nodes: {
      [name: string]: Mesh
    }
  }

  const { nodes } = useGLTF(
    `${ENV_MODEL_PATH}/rain/RainPond_Droplets_Polyredux.gltf`
  ) as GLTFResult

  const instanceMeshes = React.useMemo(
    () =>
      meshes
        .filter((mesh) => mesh.material)
        .map((mesh, i) => {
          mesh.material.metalness = 0 // this now throws an error because metalness does not exist on type Material[]
          mesh.material.transparent = true
          mesh.material.onBeforeCompile = compileMaterialForInstance

          mesh.geometry.setAttribute(
            'instOpacity',
            new InstancedBufferAttribute(
              new Float32Array(new Array(INSTANCE_COUNT).fill(0)),
              1
            )
          )

          return (
            <instancedMesh
              ref={(ref: THREE.InstancedMesh) =>
                setKeyValue(instanceRefs.current, i, ref)
              }
              key={mesh.name}
              args={[mesh.geometry, mesh.material, INSTANCE_COUNT]}
            />
          )
        }),
    [meshes]
  )

Expected behavior

Ideally i’d like to pass the Generics back in, because even if it was able to figure out if it was Material or Material[] I know the material is MeshStandardMaterial so metalness will still throw a typescript error.

disclaimer I’m happy to work on a solution if we want to fix this.

Platform:

  • Three.js version: r125

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:19 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
mrdoobcommented, Jan 29, 2021

I suppose if someone fixes something they should surely update the .d.ts file at the same time?

And that’s how you force JavaScript people learn TypeScript. Makes contributing harder.

I’m not liking this at all.

0reactions
joshuaelliscommented, Jan 29, 2021

Fair enough.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chronic headache caused by a titanium fixation plate - NCBI
We report two patients with chronic postcraniotomy headache who showed rapid alleviation of pain after removal of titanium miniplates.
Read more >
Treating migraine headaches | Choosing Wisely
To treat migraines, you be prescribed an opioid (narcotic) or a barbiturate (sedative) called butalbital. Think twice about using these drugs. Here's why:...
Read more >
Headaches after Traumatic Brain Injury
Sometimes the very medicines used to treat headaches can actually cause headaches. When pain medicines are taken daily on a regular schedule, missing...
Read more >
Management of Primary Headache During Pregnancy
In contrast, secondary headaches are caused by an underlying disorder, ... Duplicate citations will be removed prior to screening.
Read more >
Nicotine Headache: The Link Between Smoking and Headaches
A nicotine headache can be caused by nicotine withdrawal, ... Usually, by removing the stimulus (nicotine), headaches will be relieved.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found