I’ve been looking at the RenderDepth.js and RenderDepth.shader files in the ShaderReplacement package. At the end of the OnPreCull function I attempted to add the following code:
Then I added the Update() function in the RenderDepth.js file with the simple line
Debug.Log(myTexture2D.GetPixel(5, 5));
First off, this value never changes and I can’t seem to figure out why. When you move the camera around it just stays a constant. Secondly, it runs super slow, probably around 2 FPS. Essentially what I’d like to do is similar to this piece of C++ and OpenGL code. Is there is a simple way to accomplish this in Unity? Any suggestions would be helpful, thanks.
float * p_DepthBufferValues = new float[viewportWidth * viewportHeight]
glReadPixels(0, 0, viewportWidth, viewportHeight, GL_DEPTH_COMPONENT, GL_FLOAT, reinterpret_cast<void *>(p_DepthBufferValues));
...
//loop over all the values in p_DepthBufferValues and do some computation with them (this would be done each frame)
//this computation is a "stand alone" value and will never be used by, or sent back to, the GPU
....
delete[] p_DepthBufferValues;
Why are you doing it on PreCull? the camera hasn’t rendered anything and for the most part reading pixels should be deferred to the end of the frame. So unless you use a co-routine you probably will not get desired results. I could be wrong but it’s probably something to investigate to get you started.
In short, I think you need a co-routine to read the contents of that buffer if you want to do additional rendering tasks before next frame.
I get the same result regardless of where I put the code. Whether it’s in precull or after the scene has been rendered. Which means it’s still not getting a new value as the scene changes.
I am definitely not a shader expert, but as far as I could tell from looking at the code the current version of the RenderDepth.shader doesn’t do anything at all on OpenGL.
No guarantee it will work for you, but I was able to get valid depth data on my Mac using this shader instead. (It also encodes the normal, which you might not want.)
Shader "Export Depth" {
SubShader {
Tags { "RenderType" = "Opaque" }
Pass {
Fog { Mode Off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// vertex input: position, normal
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : POSITION;
float4 data : COLOR;
};
v2f vert (appdata v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.data = EncodeDepthNormal(COMPUTE_DEPTH_01, v.normal);
return o;
}
half4 frag( v2f i ) : COLOR {
return i.data;
}
ENDCG
}
}
}