# \[engine\] Crossfade two materials?

**URL:** <https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802>\
**Category:** Help & Support\
**Tags:** materials, engine-only\
**Created:** [August 29, 2024, 8:54am UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802 "2024-08-29T08:54:53Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![trusktr](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/trusktr/32/4876_2.png) [@trusktr](https://forum.playcanvas.com/u/trusktr)\
**Post date:** [August 29, 2024, 8:54am UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/1 "2024-08-29T08:54:53Z")

</div>

Is there an example of cross fading two materials on a mesh? (With engine code)

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [August 29, 2024, 8:59am UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/2 "2024-08-29T08:59:24Z")

</div>

Cross fading materials is not directly supported.

---

<div class="post-metadata">

**Author:** ![trusktr](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/trusktr/32/4876_2.png) [@trusktr](https://forum.playcanvas.com/u/trusktr)\
**Post date:** [August 29, 2024, 9:02am UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/3 "2024-08-29T09:02:14Z")

</div>

Ah ok. Basically like this demo but cross fading:

[https://playcanvas.com/project/437442/overview/switching-materials-at-runtime](https://playcanvas.com/project/437442/overview/switching-materials-at-runtime)

Requires merging the shader code into one?

---

<div class="post-metadata">

**Author:** ![LeXXik](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/lexxik/32/6953_2.png) [@LeXXik](https://forum.playcanvas.com/u/LeXXik)\
**Post date:** [August 29, 2024, 9:31am UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/4 "2024-08-29T09:31:16Z")

</div>

It requires a new shader that samples both textures and mixes them based on some time/alpha value:

> **[WebGL Using 2 or More Textures](https://webglfundamentals.org/webgl/lessons/webgl-2-textures.html)**
>
> How to use 2 or more textures in WebGL

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [August 29, 2024, 9:32am UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/5 "2024-08-29T09:32:27Z")

</div>

Going the path of merging shaders would be ideal, but hard, at least if you want to support many material properties. If you would only want to cross fade the diffuse texture for example, that’s a lot more doable.

Possibly [x.com](https://x.com/shaderfrog) could help here, I think it supports PlayCanvas and shader blending.

Otherwise perhaps render the same mesh twice with alpha blending to implement fade? This would have other limitations, but might be enough in some cases.

---

<div class="post-metadata">

**Author:** ![trusktr](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/trusktr/32/4876_2.png) [@trusktr](https://forum.playcanvas.com/u/trusktr)\
**Post date:** [February 16, 2025, 10:04pm UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/6 "2025-02-16T22:04:14Z")

</div>

Thanks. I understand that stuff, and I know how to patch shaders in Three.js to inject custom GLSL code. I’m just a n00b to the Playcanvas API.

Can you please provide some pointers on how to patch Playcanvas shader strings using only `engine` (not the editor, i.e. I am importing `engine` into a plain JS file and making the entire app in the JS file)?

If you could post a CodePen (or similar) showing the most minimal Playcanvas example with a shader patch that would be really helpful.

Once I know the Playcanvas-specific way to patch, I can most likely be on my way after that (f.e. merge shader codes, add extra textures/attributes/etc, fix naming conflicts, interpolate values, etc).

---

<div class="post-metadata">

**Author:** ![trusktr](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/trusktr/32/4876_2.png) [@trusktr](https://forum.playcanvas.com/u/trusktr)\
**Post date:** [February 16, 2025, 10:05pm UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/7 "2025-02-16T22:05:22Z")

</div>

> [@mvaligursky](#):
>
> Otherwise perhaps render the same mesh twice with alpha blending to implement fade?

Thanks, I am aware of that approach, that’s the easy way. 🙂 I’m interested in a single shader for particular reasons.

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [February 18, 2025, 10:17pm UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/8 "2025-02-18T22:17:01Z")

</div>

If for engine V2, the start of documentation for shader chunks can be found here [Shaders | PlayCanvas Developer Site](https://developer.playcanvas.com/user-manual/graphics/shaders/)

With some engine only examples here [PlayCanvas Examples](https://playcanvas.vercel.app/#/graphics/shader-burn)

Unless I’m missing something from what you are asking?

---

<div class="post-metadata">

**Author:** ![trusktr](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/trusktr/32/4876_2.png) [@trusktr](https://forum.playcanvas.com/u/trusktr)\
**Post date:** [February 19, 2025, 4:19am UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/9 "2025-02-19T04:19:21Z")

</div>

Cool, I see that creates a new material with a custom shader. How would I now, for example, patch an existing material? Let’s say for example you load that same example, but rather than replace the `meshInstance.material` with the new material, how would you patch the existing material’s shader code? For example, let’s say you want to replace `gl_FragColor = ...;` (or custom `out` variable) with `gl_FragColor = vec4(1, 0, 0, 1);` (or similar with a custom `out`)?

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [February 19, 2025, 8:28am UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/10 "2025-02-19T08:28:21Z")

</div>

In v1, you assigned a shader to a chunk in the material [How can I make the custom shader stop? - #12 by yaustar](https://forum.playcanvas.com/t/how-can-i-make-the-custom-shader-stop/27704/12)

Not sure in V2 it’s the same. @mvaligursky

---

<div class="post-metadata">

**Author:** ![mvaligursky](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mvaligursky/32/11681_2.png) [@mvaligursky](https://forum.playcanvas.com/u/mvaligursky)\
**Post date:** [February 19, 2025, 7:40pm UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/11 "2025-02-19T19:40:59Z")

</div>

yes v2 is the same.

---

<div class="post-metadata">

**Author:** ![trusktr](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/trusktr/32/4876_2.png) [@trusktr](https://forum.playcanvas.com/u/trusktr)\
**Post date:** [February 20, 2025, 10:49pm UTC](https://forum.playcanvas.com/t/engine-crossfade-two-materials/36802/12 "2025-02-20T22:49:14Z")

</div>

@yaustar @mvaligursky thanks. So looks like basically we replace the official chunk with a new string, for example like this:

```js
material.chunks.opacityPS = material.chunks.opacityPS.replace('something', 'otherthing')

```

Would be nice to get a plain engine example going (I’m not using the editor, but planning to make a PlayCanvas renderer to the [Lume 3D HTML framework](https://lume.io/) for writing 3D scenes in React/Svelte/Vue/Solid/Angular/etc) so eventually I will need to patch some shaders to introduce some features (such as cross-fading additional textures, or other things).

Something like this idea (doesn’t exist yet):

```html
<lume-scene webgl>

  <lume-box position="1 2 3">

    <!-- override the default material -->
    <lume-physical-material diffuse-blend="30%" slot="material">

      <!-- Idea: put multipe textures into the diffuse slot, to be blended. -->
      <lume-diffuse-texture src="a.jpg" slot="diffuse"></lume-diffuse-texture>
      <lume-diffuse-texture src="b.jpg" slot="diffuse"></lume-diffuse-texture>

    </lume-physical-material>

  </lume-box>

  <lume-glft-model src="foo.glb"></lume-glft-model>

</lume-scene>

```

Or something.
