typescriptreact-three-fiber

Shaders with Typescript and React three fiber


I'm trying to use shaders with React-three-fiber and Typescript. Shader file:

import { ShaderMaterial } from "three"
import { extend } from "react-three-fiber"

class CustomMaterial extends ShaderMaterial {
  constructor() {
    super({
      vertexShader: `...`,
      fragmentShader: `...`,
      uniforms: [...]
    })
  }
}

extend({ CustomMaterial })

and the component file:

<mesh
  key={el.name}
  material={el.material}
  receiveShadow
  castShadow
>
  <bufferGeometry attach="geometry" {...el.geometry} />
  <customMaterial attach="material" />
</mesh>

I'm getting error:

Property 'customMaterial' does not exist on type 'JSX.IntrinsicElements'.


Solution

  • Try:

    declare global {
      namespace JSX {
        interface IntrinsicElements {
          customMaterial: ReactThreeFiber.Object3DNode<CustomMaterial, typeof CustomMaterial>
        }
      }
    }
    

    You might also need to stick in the following in your imports:

    import { extend } from 'react-three-fiber'
    ...
    extend ({ CustomMaterial })