DepthEffectShader.shader 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Shader "Confirmat/NewImageEffectShader"
  2. {
  3. Properties
  4. {
  5. [HideInInspector] _MainTex ("Texture", 2D) = "white" {}
  6. }
  7. SubShader
  8. {
  9. // No culling or depth
  10. Cull Off ZWrite Off ZTest Always
  11. Pass
  12. {
  13. CGPROGRAM
  14. #pragma vertex vert
  15. #pragma fragment frag
  16. #pragma target 5.0 // EXTRA FOR STORING
  17. #include "UnityCG.cginc"
  18. struct appdata
  19. {
  20. float4 vertex : POSITION;
  21. float2 uv : TEXCOORD0;
  22. };
  23. struct v2f
  24. {
  25. float2 uv : TEXCOORD0;
  26. float4 vertex : SV_POSITION;
  27. float4 scrPos : TEXCOORD1;// EXTRA FOR STORING
  28. };
  29. v2f vert (appdata v)
  30. {
  31. v2f o;
  32. o.vertex = UnityObjectToClipPos(v.vertex);
  33. o.uv = v.uv;
  34. o.scrPos = ComputeScreenPos(o.vertex); // EXTRA FOR STORING
  35. return o;
  36. }
  37. sampler2D _MainTex;
  38. sampler2D _CameraDepthTexture;
  39. // EXTRA FOR STORING
  40. //Fragment Shader
  41. int _width;
  42. int _height;
  43. RWStructuredBuffer<float> _myBuffer : register(u1);
  44. fixed4 frag (v2f i) : SV_Target
  45. {
  46. //get depth from depth texture
  47. float depth = tex2D(_CameraDepthTexture, i.uv).r;
  48. depth = Linear01Depth(depth);
  49. depth = depth * _ProjectionParams.z; //depth as distance from camera in units
  50. //EXTRA FOR STORING
  51. int x = int(i.scrPos.x * _width);
  52. int y = int(i.scrPos.y * _height);
  53. //_myBuffer[(y * _width) + x] = depth; // buffer should be written here, but nothing happens!
  54. _myBuffer[((_height - 1 - y) * _width) + x] = depth; // buffer should be written here, but nothing happens!
  55. return depth;
  56. }
  57. ENDCG
  58. }
  59. }
  60. }