1 | ///////////////////////////////////////////////////////////////////////////////// |
---|
2 | // |
---|
3 | // varianceshadowcasterfp.glsles |
---|
4 | // |
---|
5 | // Hamilton Chong |
---|
6 | // (c) 2006 |
---|
7 | // GLSL ES by David Rogers |
---|
8 | // |
---|
9 | // This is an example fragment shader for shadow caster objects. |
---|
10 | // |
---|
11 | ///////////////////////////////////////////////////////////////////////////////// |
---|
12 | |
---|
13 | |
---|
14 | // Define outputs from vertex shader. |
---|
15 | struct VertexOut |
---|
16 | { |
---|
17 | float4 position : POSITION; // can't rely on access to this |
---|
18 | float4 pos : TEXCOORD0; // position of fragment (in homogeneous coordinates) |
---|
19 | float4 normal : TEXCOORD1; // un-normalized normal in object space |
---|
20 | float4 modelPos : TEXCOORD2; // coordinates of model in object space at this point |
---|
21 | }; |
---|
22 | |
---|
23 | struct FragmentOut |
---|
24 | { |
---|
25 | float4 color : COLOR0; |
---|
26 | }; |
---|
27 | |
---|
28 | FragmentOut main( VertexOut In, // fragment to process |
---|
29 | uniform float uDepthOffset, // offset amount (constant in eye space) |
---|
30 | uniform float4x4 uProjection // projection matrix |
---|
31 | ) |
---|
32 | { |
---|
33 | FragmentOut Out; |
---|
34 | |
---|
35 | // compute the "normalized device coordinates" (no viewport applied yet) |
---|
36 | float4 postproj = In.pos / In.pos.w; |
---|
37 | |
---|
38 | // get the normalized normal of the geometry seen at this point |
---|
39 | float4 normal = normalize(In.normal); |
---|
40 | |
---|
41 | |
---|
42 | // -- Computing Depth Bias Quantities ----------------------------- |
---|
43 | |
---|
44 | // We now compute the change in z that would signify a push in the z direction |
---|
45 | // by 1 unit in eye space. Note that eye space z is related in a nonlinear way to |
---|
46 | // screen space z, so this is not just a constant. |
---|
47 | // ddepth below is how much screen space z at this point would change for that push. |
---|
48 | // NOTE: computation of ddepth likely differs from OpenGL's glPolygonOffset "unit" |
---|
49 | // computation, which is allowed to be vendor specific. |
---|
50 | float4 dpwdz = mul(uProjection, float4(0.0, 0.0, 1.0, 0.0)); |
---|
51 | float4 dpdz = (dpwdz - (postproj * dpwdz.w)) / In.pos.w; |
---|
52 | float ddepth = abs(dpdz.z); |
---|
53 | |
---|
54 | // -- End depth bias helper section -------------------------------- |
---|
55 | |
---|
56 | // We now compute the depth of the fragment. This is the actual depth value plus |
---|
57 | // our depth bias. The depth bias depends on how uncertain we are about the z value |
---|
58 | // plus some constant push in the z direction. The exact coefficients to use are |
---|
59 | // up to you, but at least it should be somewhat intuitive now what the tradeoffs are. |
---|
60 | float depthval = postproj.z /* + (0.5 * dzlen)*/ + (uDepthOffset * ddepth); |
---|
61 | depthval = (0.5 * depthval) + 0.5; // put into [0,1] range instead of [-1,1] |
---|
62 | |
---|
63 | |
---|
64 | Out.color = float4(depthval, depthval * depthval, depthval, 0.0); |
---|
65 | return Out; |
---|
66 | } |
---|
67 | ///////////////////////////////////////////////////////////////////////////////// |
---|
68 | // |
---|
69 | // varianceshadowcasterfp.glsles |
---|
70 | // |
---|
71 | // Hamilton Chong |
---|
72 | // (c) 2006 |
---|
73 | // GLSL ES by David Rogers |
---|
74 | // |
---|
75 | // This is an example fragment shader for shadow caster objects. |
---|
76 | // |
---|
77 | ///////////////////////////////////////////////////////////////////////////////// |
---|
78 | |
---|
79 | |
---|
80 | // Define outputs from vertex shader. |
---|
81 | struct VertexOut |
---|
82 | { |
---|
83 | float4 position : POSITION; // can't rely on access to this |
---|
84 | float4 pos : TEXCOORD0; // position of fragment (in homogeneous coordinates) |
---|
85 | float4 normal : TEXCOORD1; // un-normalized normal in object space |
---|
86 | float4 modelPos : TEXCOORD2; // coordinates of model in object space at this point |
---|
87 | }; |
---|
88 | |
---|
89 | struct FragmentOut |
---|
90 | { |
---|
91 | float4 color : COLOR0; |
---|
92 | }; |
---|
93 | |
---|
94 | FragmentOut main( VertexOut In, // fragment to process |
---|
95 | uniform float uDepthOffset, // offset amount (constant in eye space) |
---|
96 | uniform float4x4 uProjection // projection matrix |
---|
97 | ) |
---|
98 | { |
---|
99 | FragmentOut Out; |
---|
100 | |
---|
101 | // compute the "normalized device coordinates" (no viewport applied yet) |
---|
102 | float4 postproj = In.pos / In.pos.w; |
---|
103 | |
---|
104 | // get the normalized normal of the geometry seen at this point |
---|
105 | float4 normal = normalize(In.normal); |
---|
106 | |
---|
107 | |
---|
108 | // -- Computing Depth Bias Quantities ----------------------------- |
---|
109 | |
---|
110 | // We now compute the change in z that would signify a push in the z direction |
---|
111 | // by 1 unit in eye space. Note that eye space z is related in a nonlinear way to |
---|
112 | // screen space z, so this is not just a constant. |
---|
113 | // ddepth below is how much screen space z at this point would change for that push. |
---|
114 | // NOTE: computation of ddepth likely differs from OpenGL's glPolygonOffset "unit" |
---|
115 | // computation, which is allowed to be vendor specific. |
---|
116 | float4 dpwdz = mul(uProjection, float4(0.0, 0.0, 1.0, 0.0)); |
---|
117 | float4 dpdz = (dpwdz - (postproj * dpwdz.w)) / In.pos.w; |
---|
118 | float ddepth = abs(dpdz.z); |
---|
119 | |
---|
120 | // -- End depth bias helper section -------------------------------- |
---|
121 | |
---|
122 | // We now compute the depth of the fragment. This is the actual depth value plus |
---|
123 | // our depth bias. The depth bias depends on how uncertain we are about the z value |
---|
124 | // plus some constant push in the z direction. The exact coefficients to use are |
---|
125 | // up to you, but at least it should be somewhat intuitive now what the tradeoffs are. |
---|
126 | float depthval = postproj.z /* + (0.5 * dzlen)*/ + (uDepthOffset * ddepth); |
---|
127 | depthval = (0.5 * depthval) + 0.5; // put into [0,1] range instead of [-1,1] |
---|
128 | |
---|
129 | |
---|
130 | Out.color = float4(depthval, depthval * depthval, depthval, 0.0); |
---|
131 | return Out; |
---|
132 | } |
---|