# Tone mapping/color issues with custom ShaderMaterial

**URL:** <https://discourse.threejs.org/t/tone-mapping-color-issues-with-custom-shadermaterial/77261>\
**Category:** Questions\
**Tags:** color, shaders, textures\
**Created:** [January 29, 2025, 4:06pm UTC](https://discourse.threejs.org/t/tone-mapping-color-issues-with-custom-shadermaterial/77261 "2025-01-29T16:06:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GibsDev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/gibsdev/32/41122_2.png) [@GibsDev](https://discourse.threejs.org/u/GibsDev)\
**Post date:** [January 29, 2025, 4:06pm UTC](https://discourse.threejs.org/t/tone-mapping-color-issues-with-custom-shadermaterial/77261/1 "2025-01-29T16:06:39Z")

</div>

I’ve got a basic code sandbox that demonstrates the issue I’m having here:  
[https://codesandbox.io/p/sandbox/n9jshy](https://codesandbox.io/p/sandbox/n9jshy)

The colors of the equirectangular projection seem to be off from the original scene. They seem darker/more saturated compared to the original scene. You can really see this in the orange, purple, and green cube faces.

I’m assuming this has something to do with the fact that the projected image is being applied as a texture to a plane and rendered again on the plane mesh. Or maybe the problem is related to the shader? Is there a more direct way that I could be writing the equirectangular projection directly to the canvas instead of mapping it on to a plane first?

Any insight here would be appreciated!

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [January 29, 2025, 7:23pm UTC](https://discourse.threejs.org/t/tone-mapping-color-issues-with-custom-shadermaterial/77261/2 "2025-01-29T19:23:51Z")

</div>

After assigning gl\_FragColor you’ll need the following two lines in your ShaderMaterial fragment shader:

```javascript
#include <tonemapping_fragment>
#include <colorspace_fragment>

```

These handle conversion to the canvas color space and tone mapping (if any).

> EDIT: Updated to fix the order of the shader includes.

---

<div class="post-metadata">

**Author:** ![GibsDev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/gibsdev/32/41122_2.png) [@GibsDev](https://discourse.threejs.org/u/GibsDev)\
**Post date:** [January 29, 2025, 7:28pm UTC](https://discourse.threejs.org/t/tone-mapping-color-issues-with-custom-shadermaterial/77261/3 "2025-01-29T19:28:04Z")

</div>

That works perfectly! Thank you!

---

<div class="post-metadata">

**Author:** ![GibsDev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/gibsdev/32/41122_2.png) [@GibsDev](https://discourse.threejs.org/u/GibsDev)\
**Post date:** [January 29, 2025, 7:46pm UTC](https://discourse.threejs.org/t/tone-mapping-color-issues-with-custom-shadermaterial/77261/4 "2025-01-29T19:46:52Z")

</div>

This does seem to cause a problem if the toneMapping changes on the renderers though. It’s almost like the tone mapping gets applied twice.

---

<div class="post-metadata">

**Author:** ![GibsDev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/gibsdev/32/41122_2.png) [@GibsDev](https://discourse.threejs.org/u/GibsDev)\
**Post date:** [January 29, 2025, 8:25pm UTC](https://discourse.threejs.org/t/tone-mapping-color-issues-with-custom-shadermaterial/77261/5 "2025-01-29T20:25:59Z")

</div>

It looks like this can be fixed by changing the order of these includes:

```javascript
#include <tonemapping_fragment>
#include <colorspace_fragment>

```

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [January 29, 2025, 11:32pm UTC](https://discourse.threejs.org/t/tone-mapping-color-issues-with-custom-shadermaterial/77261/6 "2025-01-29T23:32:05Z")

</div>

Oops, yes you’re correct – the tonemapping chunk should come first thanks.
