Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Screenspace coordinates
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.