I would like to convert a DepthInformation into a position. Somehow I can't do it even after several attempts.
I try to get the position of objects fragment from the DepthInformation that i get from an Depthpeeling shader.
This is the code writing the information in a RenderTexture
Shader "Hidden/OIT/Depth Peeling/Depth Peeling" {
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Main Tex", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump" {}
}
SubShader {
Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="Transparent"}
Pass {
Tags { "LightMode"="ForwardBase" }
ZWrite On
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
sampler2D _PrevDepthTex;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float3 lightDir: TEXCOORD0;
float3 viewDir : TEXCOORD1;
float2 uv : TEXCOORD2;
float4 screenPos: TEXCOORD3;
float depth : TEXCOORD4;
};
struct PixelOutput {
fixed4 col : COLOR0;
fixed4 depth : COLOR1;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
TANGENT_SPACE_ROTATION;
// Transform the light direction from object space to tangent space
o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
// Transform the view direction from object space to tangent space
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
o.screenPos = ComputeScreenPos(o.pos);
o.depth = COMPUTE_DEPTH_01;
return o;
}
PixelOutput frag(v2f i) : SV_Target {
float depth = i.depth;
float prevDepth = DecodeFloatRGBA(tex2Dproj(_PrevDepthTex, UNITY_PROJ_COORD(i.screenPos)));
clip(depth - (prevDepth + 0.00001));
fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);
fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uv));
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, tangentLightDir));
PixelOutput o;
o.col = fixed4(ambient + diffuse, _Color.a);
o.depth = EncodeFloatRGBA(i.depth);
return o;
}
ENDCG
}
}
FallBack "Diffuse/VertexLit"
}
and this is the function where i try to decode it again to a world position:
float4 DepthTextureToPos(sampler2D depthSampler,float2 uvCoord,float4 screenPos) {
float depth = DecodeFloatRGBA(tex2Dproj(depthSampler, UNITY_PROJ_COORD(screenPos)));
// H is the viewport position at this pixel in the range -1 to 1.
float4 H = float4((uvCoord.x) * 2 - 1, (uvCoord.y) * 2 - 1, depth, 1.0);
float4 D = mul(_ViewProjectInverse, H);
//return D / D.w;
float4 objPos= mul(unity_WorldToObject, (D / D.w));
return objPos;
}
the uv Coords have this as input:
float2 texc = i.screenPosition.xy / i.screenPosition.w;
the screenPos has this as Input:
ComputeScreenPos(o.vertex);