Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Custom water shader disappears when turning off shadows in the scene

Discussion in 'Shaders' started by BertusWillem, Feb 26, 2020.

  1. BertusWillem

    BertusWillem

    Joined:
    Sep 18, 2014
    Posts:
    2
    Hello,

    I tried this tutorial: https://roystan.net/articles/toon-water.html

    Well i downloaded the completed files becouse that works aswell. Anyway the shader is not visable when i turn the shadows off in my scene. I can figure out why, can anyone help me with this problem.

    Thanks in advanced!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    That shader uses the
    _CameraDepthTexture
    to do the shoreline foam and depth based coloring. By default Unity does not generate that texture, but certain built-in features also make of the depth texture and so will enable it even if it has not been explicitly enabled. Those two features are Soft Particles in the Quality settings, and the main directional light's shadows (especially when using shadow cascades). If you do not have soft particles or a shadow casting directional light in the scene, you will need to enable the depth texture on your camera manually with a script on your camera.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. [RequireComponent(typeof(Camera))]
    5. public class SetDepthTextureMode : MonoBehaviour
    6. {
    7.     public bool EnableDepthTexture;
    8.  
    9.     void SetDepthTextureMode()
    10.     {
    11.         Camera cam = GetComponent<Camera>();
    12.         if (EnableDepthTexture)
    13.             cam.depthTextureMode |= DepthTextureMode.Depth;
    14.         else
    15.             cam.depthTextureMode &= ~DepthTextureMode.Depth;
    16.     }
    17.  
    18.     void OnEnable()
    19.     {
    20.         SetDepthTextureMode();
    21.     }
    22.  
    23. #if UNITY_EDITOR
    24.     void OnValidate()
    25.     {
    26.         SetDepthTextureMode();
    27.     }
    28. #endif
    29. }
     
    BertusWillem likes this.
  3. BertusWillem

    BertusWillem

    Joined:
    Sep 18, 2014
    Posts:
    2
    Thanks bgolus, that helped alot!