Search⌘ K
AI Features

Using Our Own Shaders with ShaderMaterial

Explore how to use the THREE.ShaderMaterial in Three.js to create custom vertex and fragment shaders. Learn the basics of passing properties, uniforms, and handling shader programs to control the rendering and appearance of 3D objects, enhancing your graphics with tailored visual effects.

The THREE.ShaderMaterial 

THREE.ShaderMaterial is one of the most versatile and complex materials available in Three.js. With this material, we can pass in our own custom shaders that are directly run in the WebGL context. A shader converts Three.js JavaScript meshes into pixels on the screen. With these custom shaders, we can define exactly how our object should be rendered and how to override or alter the defaults from Three.js. In this lesson, we won’t go into too much detail on how to write custom shaders; instead, we will just show a couple of examples. 

Properties of THREE.ShaderMaterial

The THREE.ShaderMaterial has several properties we can set. With THREE.ShaderMaterial, Three.js passes in all the information regarding these properties to our custom shaders, but we still have to process the information to create colors and vertex positions. The following are the properties of THREE.Material that are passed into the shader and that we can interpret for ourselves: 

  • wireframe: This renders the material as a wireframe. This is great for debugging purposes. 

  • shading: This defines how shading is applied. The possible values are THREE.SmoothShading and THREE.FlatShading. This property isn’t enabled in the example for this material.

  • ...