Coder Social home page Coder Social logo

dhruvalraval / three-customshadermaterial Goto Github PK

View Code? Open in Web Editor NEW

This project forked from farazzshaikh/three-customshadermaterial

0.0 0.0 0.0 199.6 MB

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

License: MIT License

JavaScript 8.77% HTML 2.25% Shell 0.45% CSS 6.31% TypeScript 82.22%

three-customshadermaterial's Introduction


Logo

Custom Shader Material

Extend Three.js standard materials with your own shaders!


Waves

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.

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={MeshPhysicalMaterial}
        vertexShader={/* glsl */ ` ... `}
        fragmentShader={/* glsl */ ` ... `}
        uniforms={{
          uTime: {
            value: 0,
          },
        }}
        flatShading
        color={0xff00ff}
        // ...
      />
    </mesh>
  )
}
Show Typescript example
import CustomShaderMaterial, { CSM } from 'three-custom-shader-material'

function Cube() {
  const materialRef = useRef<CSM | null>(null)

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

  return (
    <mesh>
      <boxGeometry />
      <CustomShaderMaterial
        ref={materialRef}
        baseMaterial={MeshPhysicalMaterial}
        vertexShader={/* glsl */ ` ... `}
        fragmentShader={/* glsl */ ` ... `}
        uniforms={{
          uTime: {
            value: 0,
          },
        }}
        flatShading
        color={0xff00ff}
        // ...
      />
    </mesh>
  )
}
Show VanillaJS example
import { CSM } from 'three-custom-shader-material'

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

  return new THREE.Mesh(geometry, material)
}

Installation

npm

npm install three-custom-shader-material

Yarn

yarn add three-custom-shader-material

Output Variables

CSM provides the following output variables:

Variable Required Type Description Available In Notes
csm_Position โŒ vec3 Custom vertex position. Vertex Shader csm_Position will be multiplied by projectionMatrix and modelViewPosition furthur down the line.
csm_Normal โŒ vec3 Custom vertex normals. Vertex Shader
csm_PointSize โŒ float Custom gl_Point size. Vertex Shader
csm_DiffuseColor โŒ vec4 Custom diffuse color. Fragment Shader

You must use these variables like you would use standard GLSL output variables.

// gl_Position = projectionMatrix * modelViewPosition * position * vec3(2.0);
csm_Position = position * vec3(2.0);

Roadmap

  • Support MeshToonMaterial
  • Support MeshLambertMaterial
  • Support MeshDepthMaterial
  • gl_FragColor override
  • Make csm_Position independent from modelViewPosition and projectionMatrix

Credits

Icon made by Freepik from www.flaticon.com.

three-customshadermaterial's People

Contributors

farazzshaikh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.