Search Unity

Any reason a shader that samples depth might not work until you disable and enable the object?

Discussion in 'Shaders' started by kritoa, May 16, 2017.

  1. kritoa

    kritoa

    Joined:
    Apr 21, 2017
    Posts:
    60
    I have a very simple shader (it samples depth and calculates a custom fog color) which used to work just fine, but now, while it still works in the editor, when I hit Play, the shader does not function correctly. If I disable and reenable the cube that the shader is attached to, it starts working again.

    Any idea why this may be failing?

    The line that seems to function incorrectly until disabling/reenabling:

    float depth = LinearEyeDepth (UNITY_SAMPLE_DEPTH (tex2Dproj (_CameraDepthTexture, UNITY_PROJ_COORD (i.projPos))));
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Sounds like a problem somewhere with temp textures or something. Did you file a bug, and which version of Unity?
     
  3. kritoa

    kritoa

    Joined:
    Apr 21, 2017
    Posts:
    60
    I'm in 5.6.0f3, though it has been working correctly with this version of Unity for awhile.

    I did not file a bug because I generally assume things are my fault :)

    I am not sure what you mean by "temp textures". My fog shader *is* reading a texture, but it is a texture on disk, not created dynamically, and that texture read seems to be working just fine. The thing that is failing is the depth texture read.
     
  4. kritoa

    kritoa

    Joined:
    Apr 21, 2017
    Posts:
    60
    I figured it out - I was right: it was my fault :) I had a script on my fog object to turn on the depthTextureMode of the camera (Camera.main.depthTextureMode = DepthTextureMode.Depth). I had this occurring in the OnEnable() method, but at some point the order of object creation changed and, well, I'm not sure what happened (maybe the script was enabled before the Camera was instantiated?). Putting this in the Start() method instead fixed it.
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Awesome, glad it was a quick fix.