5.4.0 • Published 13 days ago

three-custom-shader-material v5.4.0

Weekly downloads
3
License
MIT
Repository
github
Last release
13 days ago

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

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)
}
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.

VariableTypeDescriptionAvailable InNotes
csm_Positionvec3Custom vertex position.Vertex Shadercsm_Position will be projected furthur down the line. Thus, no projection is needed here.
csm_DiffuseColorvec4Custom diffuse color.Fragment Shader
csm_Normalvec3Custom vertex normals.Vertex Shader
csm_PointSizefloatCustom gl_PointSize.Vertex ShaderOnly available in PointsMaterial
csm_FragColorvec4Custom gl_FragColor.Fragment Shadercsm_FragColor will override any shading applied by a base material. To preserve shading and other effects like roughness and metalness, use csm_DiffuseColor
csm_Emissivevec3Custom emissive color.Fragment ShaderOnly available in MeshPhysicalMaterial and MeshStandardMaterial
csm_RoughnessfloatCustom roughness.Fragment ShaderOnly available in MeshPhysicalMaterial and MeshStandardMaterial
csm_MetalnessfloatCustom metalness.Fragment ShaderOnly available in MeshPhysicalMaterial and MeshStandardMaterial
// 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`
      }
   }}
})

Development

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

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.
6.0.0-rc.1

13 days ago

6.0.0-rc.0

13 days ago

5.4.0

10 months ago

5.3.8

11 months ago

5.3.2

1 year ago

5.3.0

1 year ago

5.2.1

1 year ago

5.2.0

1 year ago

5.3.7

1 year ago

5.3.6

1 year ago

5.3.5

1 year ago

5.3.4

1 year ago

5.1.2

1 year ago

5.1.1

1 year ago

5.1.0

1 year ago

5.0.0

1 year ago

5.0.0-next.4

1 year ago

5.0.0-next.1

2 years ago

5.0.0-next.2

2 years ago

3.4.0

2 years ago

3.4.4

2 years ago

3.4.3

2 years ago

3.4.2

2 years ago

3.4.1

2 years ago

4.0.0

2 years ago

3.5.3

2 years ago

3.5.2

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

5.0.0-next.0

2 years ago

3.3.6

2 years ago

3.3.5

2 years ago

3.3.4

2 years ago

3.3.3

2 years ago

3.2.9

2 years ago

3.2.8

2 years ago

3.3.1

2 years ago

3.2.13

2 years ago

3.3.0

2 years ago

3.2.12

2 years ago

3.3.2

2 years ago

3.2.11

2 years ago

3.2.10

2 years ago

3.2.7

2 years ago

3.2.6

2 years ago

3.2.5

2 years ago

3.2.4

2 years ago

2.4.3

2 years ago

3.2.2

2 years ago

3.1.3

2 years ago

3.2.1

2 years ago

3.1.2

2 years ago

3.2.0

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.1.5

2 years ago

3.2.3

2 years ago

3.1.4

2 years ago

3.0.0

2 years ago

2.4.2

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.1

3 years ago

2.3.0

3 years ago

2.2.1

3 years ago

2.0.3

3 years ago

2.2.0

3 years ago

2.1.1

3 years ago

2.0.2

3 years ago

2.2.2

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.0

4 years ago