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

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))));

Sounds like a problem somewhere with temp textures or something. Did you file a bug, and which version of Unity?

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 :slight_smile:

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.

I figured it out - I was right: it was my fault :slight_smile: 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.

Awesome, glad it was a quick fix.