• Stars
    star
    878
  • Rank 51,998 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🧩 Extend Three.js standard materials with your own shaders!

Custom Shader Material

Hero

Extend Three.js standard materials with your own shaders!


Waves Points Caustics

The demo is real, you can click it! It contains full code, too. πŸ“¦


Chat on Twitter


Custom Shader Material (CSM) lets you extend Three.js' material library with your own Vertex and Fragment shaders. It Supports both Vanilla and React!

Show Vanilla example
import CustomShaderMaterial from 'three-custom-shader-material/vanilla'

function Box() {
  const geometry = new THREE.BoxGeometry()
  const material = new CustomShaderMaterial({
    baseMaterial: THREE.MeshPhysicalMaterial,
    vertexShader: /* glsl */ ` ... `,
    fragmentShader: /* glsl */ ` ... `,
    uniforms: {
      uTime: {
        value: 0,
      },
    },
    flatShading: true,
    color: 0xff00ff,
  })

  return new THREE.Mesh(geometry, material)
}
Show React example
import CustomShaderMaterial from 'three-custom-shader-material'

function Cube() {
  const materialRef = useRef()

  useFrame((state) => {
    if (materialRef.current) {
      materialRef.current.uniforms.uTime.value = state.clock.elapsedTime
    }
  })

  return (
    <mesh>
      <boxGeometry />
      <CustomShaderMaterial
        ref={materialRef}
        baseMaterial={THREE.MeshPhysicalMaterial}
        vertexShader={/* glsl */ ` ... `}
        fragmentShader={/* glsl */ ` ... `}
        uniforms={{
          uTime: {
            value: 0,
          },
        }}
        flatShading
        color={0xff00ff}
        // ...
      />
    </mesh>
  )
}

Installation

npm install three-custom-shader-material
yarn add three-custom-shader-material

Output Variables

CSM provides the following output variables, all of them are optional but you MUST use these variables like you would use standard GLSL output variables to see results.

Variable Type Description Available In Notes
csm_Position vec3 Custom vertex position. Vertex Shader csm_Position will be projected furthur down the line. Thus, no projection is needed here.
csm_PositionRaw vec3 Direct equivalent of gl_Position. Vertex Shader
csm_Normal vec3 Custom vertex normals. Vertex Shader
csm_PointSize float Custom gl_PointSize. Vertex Shader Only available in PointsMaterial
- - - - -
csm_DiffuseColor vec4 Custom diffuse color. Fragment Shader
csm_FragColor vec4 Direct equivalent of gl_FragColor. Fragment Shader csm_FragColor will override any shading applied by a base material. To preserve shading and other effects like roughness and metalness, use csm_DiffuseColor
csm_Emissive vec3 Custom emissive color. Fragment Shader Only available in MeshPhysicalMaterial and MeshStandardMaterial
csm_Roughness float Custom roughness. Fragment Shader Only available in MeshPhysicalMaterial and MeshStandardMaterial
csm_Metalness float Custom metalness. Fragment Shader Only available in MeshPhysicalMaterial and MeshStandardMaterial
csm_AO float Custom AO. Fragment Shader Only available in materials with an aoMap.
csm_Bump vec3 Custom Bump. Fragment Shader Only available in materials with a bumpMap.
// gl_Position = projectionMatrix * modelViewPosition * position * vec3(2.0);
csm_Position = position * vec3(2.0);

Custom overrides

You can define any custom overrides you'd like using the patchMap prop. The prop is used as shown below.

const material = new CustomShaderMaterial({
   baseMaterial: THREE.MeshPhysicalMaterial,
   vertexShader: ` ... `,
   fragmentShader: ... `,
   uniforms: {...},
   patchMap={{
      "<KEYWORD>": {        // The keyword you will assign to in your custom shader
        "TO_REPLACE":       // The chunk you'd like to replace.
          "REPLACED_WITH"   // The chunk you'd like put in place of `TO_REPLACE`
      }
   }}
})

Extending already extended materials

CSM allows you to extend other CSM instances or materials that already use onBeforeCompile to extend base functionality such as MeshTransmissionMaterial. It is as simple as passing a ref to that material in as the baseMaterial:

Show Vanilla example
import CustomShaderMaterial from 'three-custom-shader-material/vanilla'

function Box() {
  const material1 = new CustomShaderMaterial({
    baseMaterial: THREE.MeshPhysicalMaterial,
    //...Any props
  })
  const material2 = new CustomShaderMaterial({
    baseMaterial: material1,
    //...Any props
  })

  // OR
  const material1 = new MeshTransmissionMaterial()
  const material2 = new CustomShaderMaterial({
    baseMaterial: material1,
    //...Any props
  })
}
Show React example
import CustomShaderMaterial from 'three-custom-shader-material'
import CustomShaderMaterialImpl from 'three-custom-shader-material/vanilla'

function Cube() {
  const [materialRef, setMaterialRef] = useState()
  // OR
  const materialRef = useMemo(
    () =>
      new CustomShaderMaterialImpl({
        baseMaterial: THREE.MeshPhysicalMaterial,
      }),
    []
  )

  return (
    <>
      <CustomShaderMaterial
        ref={setMaterialRef}
        baseMaterial={THREE.MeshPhysicalMaterial}
        //...Any props
      />

      {materialRef && (
        <CustomShaderMaterial
          baseMaterial={materialRef}
          //...Any props
        />
      )}
    </>
  )
}

Gotchas

  • When extending already extended material, variables, uniforms, attributes, varyings and functions are NOT scoped to the material they are defined in. Thus, you WILL get redefinition errors if you do not manually scope these identifiers.

  • When using an instance of CSM as the baseMaterial, or chining multiple CSM instances, or when extending any material that uses onBeforeCompile the injection order is as follows:

    void main() {
      // shader A
      // shader B
      // shader C
      // shader D
    
      // original shader
    }

    The last injected shader will contain values set in all the shaders injected before it.

Performance

CSM is great for small overrides. Even though doable, if you find yourself lost in a patchMap, it's often simpler to just make a ShaderMaterial with the necessary #includes.

CSM has a non-negligible startup cost, i.e it does take more milliseconds than a plain ShaderMaterial to load. However, once loaded it is practically free. It also uses caching so reusing materials will not incur any extra cost. Couple important notes about performance:

  • Changing these props will rebuild the material

    • baseMaterial
    • fragmentShader
    • vertexShader
    • uniforms
    • cacheKey
  • <meshPhysicalMaterial /> and <CSM baseMaterial={meshPhysicalMaterial}> are the same, and will use the same cached shader program. The default cache key is such:

    hash(stripSpaces(fragmentShader) + stripSpaces(vertexShader) + serializedUniforms)

    You can provide your own cache key function via the cacheKey prop.

Development

git clone https://github.com/FarazzShaikh/THREE-CustomShaderMaterial
cd THREE-CustomShaderMaterial
yarn
yarn dev

Will watch for changes in package/ and will hot-reload examples/debug.

License

MIT License

Copyright (c) 2021 Faraz Shaikh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

More Repositories

1

Terrain-Builder

πŸ” Procedural terrain using Three.js and perlin noise, Now Accelerated by your GPU!
JavaScript
358
star
2

glNoise

A collection of GLSL noise functions for use with WebGL with an easy to use API.
GLSL
228
star
3

three-shader-baker

🍰 Bake your Three.js shaders or materials into textures!
TypeScript
104
star
4

react-three-paper

A paper-thin (~800 bytes) and position-aware wrapper for ThreeJS in React.
TypeScript
86
star
5

three-volumetric-clouds

Volumetric clouds in WebGL (Three.js) following "Nebus, Evolved" by Andrew Schneider at Guerrilla Games
TypeScript
58
star
6

three-noise

Simple gradient noise library for use with Three.js. Now with fBm!
JavaScript
41
star
7

experiments

A centralized location for my WebGL and other demos.
JavaScript
41
star
8

react-three-scissor

Multiple scenes, one canvas! WebGL Scissoring implementation for React Three Fiber.
TypeScript
23
star
9

RN-SimpleAnimations

Simple Animations is a simple abstraction of the React Native Animated API.
JavaScript
22
star
10

LunarLander

A Sci-Fi Survival Shooter, made entirely using the DOM and JavaScript
JavaScript
21
star
11

farazzshaikh.com

Personal portfolio site of Faraz Shaikh.
TypeScript
12
star
12

THREE-MultipassShaderHelper

Create multi-pass shaders in ThreeJS, intuitively.
JavaScript
10
star
13

blog

Personal blog site of Faraz Shaikh.
Sass
7
star
14

mdx-components

A Collection of MDX Components.
JavaScript
5
star
15

WebAssemblr

A dead simple way to call WebAssembly form your script.
TypeScript
4
star
16

web-terminal

[Work In Progress] A terminal emulator for the web.
Go
1
star
17

JS-CodeClinic

Materials from an Online Session of Javascript.
JavaScript
1
star
18

farazzshaikh

1
star