cybie
October 1, 2010, 10:02pm
1
Not having much luck with this. I am aware that Internal-PrepassLigthing.shader is doing this, but when I simply copy the code over, I didn’t get what I want.
Do I need to do anything on the script side (such as setting the matrix parameter _CameraToWorld)?
Thanks
Can you post the code you’ve got so far?
cybie
October 4, 2010, 5:51pm
3
Sure can.
Shader "fog" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
float3 ray : TEXCOORD1;
};
float4 MyComputeScreenPos (float4 pos) {
float4 o = pos * 0.5f;
#if defined(UNITY_HALF_TEXEL_OFFSET)
o.xy = float2(o.x, o.y*_ProjectionParams.x) + o.w * _ScreenParams.zw;
#else
o.xy = float2(o.x, o.y*_ProjectionParams.x) + o.w;
#endif
o.zw = pos.zw;
return o;
}
v2f vert (appdata v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = MyComputeScreenPos (o.pos);
o.ray = mul (UNITY_MATRIX_MV, v.vertex).xyz * float3(-1,-1,1);
// v.texcoord is equal to 0 when we are drawing 3D light shapes and
// contains a ray pointing from the camera to one of near plane's
// corners in camera space when we are drawing a full screen quad.
o.ray = lerp(o.ray, v.texcoord, v.texcoord.z != 0);
return o;
}
uniform sampler2D _MainTex;
sampler2D _CameraDepthTexture;
float4x4 _CameraToWorld;
float4 frag (v2f i) : COLOR
{
float4 original = tex2D(_MainTex, i.uv.xy);
i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
float2 uv = i.uv.xy / i.uv.w;
float depth = tex2D (_CameraDepthTexture, uv).r;
depth = Linear01Depth (depth);
float4 viewPos = float4(i.ray * depth,1);
float3 worldPos = mul (_CameraToWorld, viewPos).xyz;
float4 output;
if (worldPos.y < 20.0)
output.rgb = original.rgb * 0.5f;
else
output.rgb = original.rgb;
output.a = 1.0;
return output;
}
ENDCG
}
}
Fallback off
}
And the script side is just this
Graphics.Blit (source, destination, material);
For some reason the output image is upside down.
SashaRX
November 11, 2010, 9:58am
4
Have you found a solution?