# Dynamically modify defines in post-processing

**URL:** <https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898>\
**Category:** Questions\
**Created:** [April 18, 2025, 1:42am UTC](https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898 "2025-04-18T01:42:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![KallkaGo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kallkago/32/45789_2.png) [@KallkaGo](https://forum.babylonjs.com/u/KallkaGo)\
**Post date:** [April 18, 2025, 1:42am UTC](https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898/1 "2025-04-18T01:42:44Z")

</div>

I tried to use the tweak pane in post-processing to dynamically modify the #defines in the shader, but the screen flickered. How can i fix it ?

outline shader

```auto
void main() {

  vec4 baseColor = texture2D(uSceneTexture, vUV);

  #ifndef ENABLE

  gl_FragColor = baseColor;

  return; 

  #endif

  vec2 edge = texture2D(textureSampler, vUV).rg;
  vec2 mask = texture2D(uMaskTexture, vUV).rg;

  #ifndef X_RAY

  edge.y = 0.;

  #endif

  edge *= (uEdgeStrength * mask.x);

  vec3 color = edge.x * uLineColor + edge.y * uLineColor;

  float visibilityFactor = 0.0;

  float alpha = max(max(edge.x, edge.y), visibilityFactor);

  vec4 finalColor = vec4(0.0, 0.0, 0.0, 1.0);

  #ifdef ALPHA

  finalColor = vec4(color, alpha);

  #else

  finalColor = vec4(color, max(alpha, baseColor.a));

  #endif

  finalColor = blend(baseColor, finalColor, uOpacity);

  gl_FragColor = finalColor;

}

```

undate function

```auto
 private updateDefines(composer: PostProcess) {
    const defines = [];

    if (this.params.x_ray) {
      defines.push('#define X_RAY');
    }

    if (this.params.mode) {
      defines.push(`#define ${this.params.mode}`);
    }

    if (this.params.enable) {
      defines.push('#define ENABLE');
    }

    composer.updateEffect(defines.join('\n'));
  }

```

---

<div class="post-metadata">

**Author:** ![CrashMaster](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/crashmaster/32/23662_2.png) [@CrashMaster](https://forum.babylonjs.com/u/CrashMaster)\
**Post date:** [April 18, 2025, 7:09am UTC](https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898/2 "2025-04-18T07:09:58Z")

</div>

Changing the defines at runtime will probably require a shader compilation, which is not ideal 🤔

Why not use a uniform like `uniform int colorMode;` and use its value instead to switch between your colors?

---

<div class="post-metadata">

**Author:** ![KallkaGo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kallkago/32/45789_2.png) [@KallkaGo](https://forum.babylonjs.com/u/KallkaGo)\
**Post date:** [April 18, 2025, 11:13am UTC](https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898/3 "2025-04-18T11:13:09Z")

</div>

I want to change the blend function by changing defines, but it seems that it will compile in parallel and cause the screen to flash. 😂

---

<div class="post-metadata">

**Author:** ![CrashMaster](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/crashmaster/32/23662_2.png) [@CrashMaster](https://forum.babylonjs.com/u/CrashMaster)\
**Post date:** [April 18, 2025, 1:09pm UTC](https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898/4 "2025-04-18T13:09:43Z")

</div>

Yes that’s to be expected, that’s why you want to avoid shader recompilation as much as possible. Does using a uniform not work for you? 🤔

---

<div class="post-metadata">

**Author:** ![KallkaGo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kallkago/32/45789_2.png) [@KallkaGo](https://forum.babylonjs.com/u/KallkaGo)\
**Post date:** [April 18, 2025, 1:28pm UTC](https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898/5 "2025-04-18T13:28:45Z")

</div>

No, I personally think it is not beautiful and intuitive to control functions through uniforms.  
just like this

```auto
vec4 blend(const in vec4 x, const in vec4 y, const in float opacity) {
  vec4 result;

  if(uMode == 1) {  
        // Alpha blend mode  
    result = mix(x, y, min(y.a, opacity));
  } else if(uMode == 2) {  
        // Screen blend mode  
    result = mix(x, x + y - min(x * y, 1.0), opacity);
  } else {  
        // Normal blend mode (default)  
    result = mix(x, y, opacity);
  }

  return result;
}

```

---

<div class="post-metadata">

**Author:** ![CrashMaster](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/crashmaster/32/23662_2.png) [@CrashMaster](https://forum.babylonjs.com/u/CrashMaster)\
**Post date:** [April 18, 2025, 1:37pm UTC](https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898/6 "2025-04-18T13:37:21Z")

</div>

That’s very subjective 😆 I have another solution for you then: pre-compile all possible versions of your shader and then swap them at runtime depending on which one you need.

I am curious though, why do you not find using uniforms for this use case beautiful or intuitive?

---

<div class="post-metadata">

**Author:** ![Lemcat](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/lemcat/32/44469_2.png) [@Lemcat](https://forum.babylonjs.com/u/Lemcat)\
**Post date:** [April 18, 2025, 1:52pm UTC](https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898/7 "2025-04-18T13:52:20Z")

</div>

Maybe it’s better to use step functions there instead of conditions. In terms of performance.

---

<div class="post-metadata">

**Author:** ![KallkaGo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kallkago/32/45789_2.png) [@KallkaGo](https://forum.babylonjs.com/u/KallkaGo)\
**Post date:** [April 18, 2025, 1:58pm UTC](https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898/8 "2025-04-18T13:58:45Z")

</div>

It may be related to my personal coding habits. When I use threejs to develop, I tend to change defines dynamically. 🤣

---

<div class="post-metadata">

**Author:** ![KallkaGo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kallkago/32/45789_2.png) [@KallkaGo](https://forum.babylonjs.com/u/KallkaGo)\
**Post date:** [April 18, 2025, 1:59pm UTC](https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898/9 "2025-04-18T13:59:17Z")

</div>

Yes, thanks for your suggestion.

---

<div class="post-metadata">

**Author:** ![Lemcat](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/lemcat/32/44469_2.png) [@Lemcat](https://forum.babylonjs.com/u/Lemcat)\
**Post date:** [April 18, 2025, 2:07pm UTC](https://forum.babylonjs.com/t/dynamically-modify-defines-in-post-processing/57898/10 "2025-04-18T14:07:54Z")

</div>

Maybe something like this:

```auto
uniform float uModeWeights[3]; // [1,0,0] for mode 0, [0,1,0] for mode 1, etc.

vec4 blend(const in vec4 x, const in vec4 y, const in float opacity) {
    return uModeWeights[0] * mix(x, y, opacity)
         + uModeWeights[1] * mix(x, y, min(y.a, opacity))
         + uModeWeights[2] * mix(x, x + y - min(x * y, 1.0), opacity);
}

```

Or something like:

```auto
vec4 blend(const in vec4 x, const in vec4 y, const in float opacity) {
    // Pre-calculate all possible blend results
    vec4 normalBlend = mix(x, y, opacity);
    vec4 screenBlend = mix(x, x + y - min(x * y, 1.0), opacity);
    vec4 alphaBlend = mix(x, y, min(y.a, opacity));
    
    // Calculate weights for each mode (0 or 1)
    float mode1Weight = abs(sign(float(uMode) - 1.5)) - 0.5; // ~1.0 when uMode == 1
    float mode2Weight = abs(sign(float(uMode) - 2.5)) - 0.5; // ~1.0 when uMode == 2
    float defaultWeight = 1.0 - mode1Weight - mode2Weight; // ~1.0 otherwise
    
    // Combine results using weights
    return mode1Weight * alphaBlend 
         + mode2Weight * screenBlend 
         + defaultWeight * normalBlend;
}

```
