Last active
September 3, 2019 15:29
-
-
Save slembcke/86bfc7505046d68a0dc768970f891c17 to your computer and use it in GitHub Desktop.
Screenspace coordinates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CGPROGRAM | |
// Pass a clean copy of the VP matrix. | |
// Shader.SetGlobalMatrix("_MyMatrixVP", Camera.current.projectionMatrix*Camera.current.worldToCameraMatrix); | |
// Unity applies poorly documented magic to UNITY_MATRIX_VP that you don't want. | |
float4x4 _MyMatrixVP; | |
struct VertexOutput { | |
// Pass the screen space coordinate as a new varying. | |
// Don't re-use SV_POSITION in your frag shader, it only works on non-DirectX platforms IIRC. | |
float4 screenSpace : TEXCOORD1; | |
}; | |
VertexOutput VShader(VertexInput v){ | |
VertexOutput o; | |
// Multiply by MVP matrix, mostly as normal. | |
float4 screenSpace = mul(_MyMatrixVP, mul(unity_ObjectToWorld, float4(v.position, 1.0))); | |
// Add this line if you want your screen space coords to go from (0, 1) instead of (-1, 1) for texturing. | |
o.screenSpace = screenSpace + screenSpace.w; | |
} | |
fixed4 FShader(VertexOutput v) : SV_Target { | |
// In your Fragment shader either use projective texturing: | |
float3 color = tex2Dproj(_SFLightMap, v.screenSpace); | |
// Or manually divide by the perspective coordinate: | |
float3 color = tex2D(_SFLightMap, v.screenSpace.xy/v.screenSpace.w); | |
} | |
ENDCG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment