# How to combine gradient shader material with mesh origin material

**URL:** <https://discourse.threejs.org/t/how-to-combine-gradient-shader-material-with-mesh-origin-material/21454>\
**Category:** Questions\
**Tags:** materials, shaders\
**Created:** [December 10, 2020, 8:51am UTC](https://discourse.threejs.org/t/how-to-combine-gradient-shader-material-with-mesh-origin-material/21454 "2020-12-10T08:51:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chenxiaoleizi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/chenxiaoleizi/32/5519_2.png) [@chenxiaoleizi](https://discourse.threejs.org/u/chenxiaoleizi)\
**Post date:** [December 10, 2020, 8:51am UTC](https://discourse.threejs.org/t/how-to-combine-gradient-shader-material-with-mesh-origin-material/21454/1 "2020-12-10T08:51:02Z")

</div>

I have a shader material.

```
function createGradient(originColor){
    return new THREE.ShaderMaterial({
        uniforms: {
          originColor: {
            value: originColor
          },
          center: {
            value: new THREE.Vector3(-5, -20, 0)
          },
          radius: {
            value: 20.0
          }
        },
        vertexShader: `
          varying vec3 vPosition;

          void main() {
            vPosition = position;
            gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
          }
        `,

        fragmentShader: `
          uniform vec3 originColor; 
          uniform vec3 center; 
          uniform float radius;
      
          varying vec3 vPosition;

          void main() {
            float dis = distance(vPosition, center);
            float bound1 = radius / 3.0;
            float bound2 = (radius / 3.0) * 2.0;
            float r, g, b;
            if (dis < bound1) {
              float rate = dis / bound1;
              r = 1.0;
              g = mix(0.0, 1.0, rate);
              b = 0.0;
            } 

            if (dis >= bound1 && dis < bound2) {
              float rate = (dis - bound1) / bound1;
              r = mix(1.0, 0.0, rate);
              g = 1.0;
              b = mix(0.0, 1.0, rate);
            }

            if (dis >= bound2 && dis <= radius) {
              float rate = (dis - bound2) / bound1;
              r = 0.0;
              g = mix(1.0, 0.0, rate);
              b = 1.0;
            }
            if (dis > radius) {
              r = originColor.r;
              g = originColor.g;
              b = originColor.b;
            }
            gl_FragColor = vec4(r, g, b, 1.0);
          }`
      });
}

```

When switch to the gradient material.

`mesh.material = createGradient(mesh.material.color)`  
so out of the gradient radius use mesh original color.

 ![Snipaste_2020-12-10_16-45-47](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/7/77608f05526139297292df1bd21b0b0e8887dcdf.png)  
but lost original material oapcity and light.  
 ![Snipaste_2020-12-10_16-50-08](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/7/701e1da0e8b049cc8114e9565d25d473906cf7c0.png)  
 ![Snipaste_2020-12-10_16-47-11](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/c/c7b74b1929ae0d78a8e3de1dd87a48d5e390055f.png)  
Is there a easy to combine the gradient and original material?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [December 10, 2020, 4:56pm UTC](https://discourse.threejs.org/t/how-to-combine-gradient-shader-material-with-mesh-origin-material/21454/2 "2020-12-10T16:56:44Z")

</div>

> [@chenxiaoleizi](#):
>
> Is there a easy to combine the gradient and original material?

Combination of materials is not possible. Have you considered to inject your shader code via [Material.onBeforeCompile()](https://threejs.org/docs/index.html#api/en/materials/Material.onBeforeCompile) into built-in materials? This approach is demonstrated in [webgl\_materials\_modified](https://threejs.org/examples/webgl_materials_modified).

---

<div class="post-metadata">

**Author:** ![gonnavis](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/gonnavis/32/11428_2.png) [@gonnavis](https://discourse.threejs.org/u/gonnavis)\
**Post date:** [December 10, 2020, 5:14pm UTC](https://discourse.threejs.org/t/how-to-combine-gradient-shader-material-with-mesh-origin-material/21454/3 "2020-12-10T17:14:38Z")

</div>

I also created two gists for this purpose

> <https://gist.github.com/gonnavis/024e099b6582034618199ef873752123>

> <https://gist.github.com/gonnavis/9d5e4801ce473abab8eb87ae3780c1ab>

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [December 10, 2020, 8:52pm UTC](https://discourse.threejs.org/t/how-to-combine-gradient-shader-material-with-mesh-origin-material/21454/4 "2020-12-10T20:52:29Z")

</div>

Not so long ago, there was a question on discord about a force field and how to show its border on objects in a scene.  
I wrote an example, that uses `.onBeforeCompile()` for `MeshStandardMaterial`:

https://codepen.io/prisoner849/embed/preview/NWrJmrG?height=300&slug-hash=NWrJmrG&default-tabs=js,result&host=https://codepen.io

The solution also works for instanced meshes:

https://codepen.io/prisoner849/embed/preview/dyXLOrp?height=300&slug-hash=dyXLOrp&default-tabs=js,result&host=https://codepen.io

---

<div class="post-metadata">

**Author:** ![chenxiaoleizi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/chenxiaoleizi/32/5519_2.png) [@chenxiaoleizi](https://discourse.threejs.org/u/chenxiaoleizi)\
**Post date:** [December 11, 2020, 5:11am UTC](https://discourse.threejs.org/t/how-to-combine-gradient-shader-material-with-mesh-origin-material/21454/5 "2020-12-11T05:11:34Z")

</div>

thanks! i will try `Material.onBeforeCompile`

---

<div class="post-metadata">

**Author:** ![chenxiaoleizi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/chenxiaoleizi/32/5519_2.png) [@chenxiaoleizi](https://discourse.threejs.org/u/chenxiaoleizi)\
**Post date:** [December 11, 2020, 5:14am UTC](https://discourse.threejs.org/t/how-to-combine-gradient-shader-material-with-mesh-origin-material/21454/6 "2020-12-11T05:14:50Z")

</div>

looks very nice! thanks for your example 🍺

---

<div class="post-metadata">

**Author:** ![chenxiaoleizi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/chenxiaoleizi/32/5519_2.png) [@chenxiaoleizi](https://discourse.threejs.org/u/chenxiaoleizi)\
**Post date:** [December 11, 2020, 5:15am UTC](https://discourse.threejs.org/t/how-to-combine-gradient-shader-material-with-mesh-origin-material/21454/7 "2020-12-11T05:15:53Z")

</div>

thanks for your code! i will tri it.

---

<div class="post-metadata">

**Author:** ![chenxiaoleizi](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/chenxiaoleizi/32/5519_2.png) [@chenxiaoleizi](https://discourse.threejs.org/u/chenxiaoleizi)\
**Post date:** [December 15, 2020, 2:57am UTC](https://discourse.threejs.org/t/how-to-combine-gradient-shader-material-with-mesh-origin-material/21454/8 "2020-12-15T02:57:09Z")

</div>

Hi! i create the gradient successful by follow the example [https://codepen.io/prisoner849/pen/NWrJmrG?editors=0010](https://codepen.io/prisoner849/pen/NWrJmrG?editors=0010)

 ![img](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/2X/8/8dde7c94e5d22512e685fd655ef379106ef75bb6.jpeg)  
many thanks!  
And when i want to remove the gradient. I tried to restore the shader in `onBeforeCompile` by replace the shader code back to default `StandardMaterial` shader code, but it not works, because the `onBeforeCompile` only execute once.  
But i remove the gradient by blow code.

```javascript
let cachedMaterial = new Map();
if (mode === 0) {
        // remove gradient by restore material from cache
        mesh.material = this.cachedMaterial.get(mesh)
        mesh.material.needsUpdate = true
      } else {
        // save origin material to cache
        if (!this.cachedMaterial.has(mesh.material)) {
          this.cachedMaterial.set(mesh, mesh.material)
        }

        mesh.material = mesh.material.clone();
        mesh.material.onBeforeCompile = onBeforeCompile.bind(null, mode, length, position);
        mesh.material.needsUpdate = true;
  }

```

Is this the right to remove the gradient to origin material?
