1 | // input |
---|
2 | struct vtxin |
---|
3 | { |
---|
4 | float4 position: POSITION; |
---|
5 | float3 normal: NORMAL; |
---|
6 | float4 color: COLOR0; |
---|
7 | float4 texCoord: TEXCOORD0; |
---|
8 | }; |
---|
9 | |
---|
10 | // vtx output |
---|
11 | struct vtxout |
---|
12 | { |
---|
13 | float4 position: POSITION; // eye space |
---|
14 | float4 texCoord: TEXCOORD0; |
---|
15 | |
---|
16 | float4 color: COLOR0; |
---|
17 | float4 worldPos: TEXCOORD1; // world position |
---|
18 | float3 normal: TEXCOORD2; |
---|
19 | float4 mypos: TEXCOORD3; |
---|
20 | }; |
---|
21 | |
---|
22 | |
---|
23 | // fragment input |
---|
24 | struct fragin |
---|
25 | { |
---|
26 | float4 color: COLOR0; |
---|
27 | float4 position: POSITION; // eye space |
---|
28 | float4 texCoord: TEXCOORD0; |
---|
29 | |
---|
30 | float4 projPos: WPOS; |
---|
31 | float4 worldPos: TEXCOORD1; // world position |
---|
32 | float3 normal: TEXCOORD2; |
---|
33 | float4 mypos: TEXCOORD3; |
---|
34 | }; |
---|
35 | |
---|
36 | |
---|
37 | struct pixel |
---|
38 | { |
---|
39 | float4 col: COLOR0; |
---|
40 | //float4 pos: COLOR1; |
---|
41 | float4 pos: COLOR1; |
---|
42 | float4 norm: COLOR2; |
---|
43 | }; |
---|
44 | |
---|
45 | #pragma position_invariant vtx |
---|
46 | |
---|
47 | vtxout vtx(vtxin IN, |
---|
48 | const uniform float4x4 ModelViewProj, |
---|
49 | uniform float4x4 ModelView) |
---|
50 | { |
---|
51 | vtxout OUT; |
---|
52 | |
---|
53 | OUT.color = IN.color; |
---|
54 | OUT.texCoord = IN.texCoord; |
---|
55 | |
---|
56 | //OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position); |
---|
57 | OUT.worldPos = mul(ModelView, IN.position); |
---|
58 | // transform the vertex position into eye space |
---|
59 | OUT.position = mul(glstate.matrix.mvp, IN.position); |
---|
60 | |
---|
61 | OUT.normal = IN.normal; |
---|
62 | OUT.mypos = OUT.position; |
---|
63 | |
---|
64 | return OUT; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | // bilinear interpolation |
---|
69 | inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
|
---|
70 | {
|
---|
71 | float3 x1 = lerp(bl, tl, w.y);
|
---|
72 | float3 x2 = lerp(br, tr, w.y);
|
---|
73 | float3 v = lerp(x1, x2, w.x);
|
---|
74 |
|
---|
75 | return v;
|
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | pixel fragtex(fragin IN, |
---|
80 | uniform sampler2D dirtTex, |
---|
81 | uniform float maxDepth, |
---|
82 | uniform sampler2D tex, |
---|
83 | uniform float3 eyePos, |
---|
84 | uniform float3 bl,
|
---|
85 | uniform float3 br,
|
---|
86 | uniform float3 tl, |
---|
87 | uniform float3 tr |
---|
88 | ) |
---|
89 | { |
---|
90 | float4 texColor = tex2D(tex, IN.texCoord.xy); |
---|
91 | |
---|
92 | // account for alpha blending |
---|
93 | if (texColor.w < 0.5f) discard; |
---|
94 | |
---|
95 | pixel pix; |
---|
96 | |
---|
97 | // save color in first render target |
---|
98 | // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term) |
---|
99 | pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor; |
---|
100 | // save world space normal in third rt |
---|
101 | pix.norm.xyz = IN.normal; |
---|
102 | // store projection coordinates with positions (used for ssao) |
---|
103 | pix.norm.w = IN.projPos.w; |
---|
104 | |
---|
105 | const float4 projPos = IN.mypos / IN.mypos.w; |
---|
106 | |
---|
107 | // hack: squeeze some information about ambient into the texture |
---|
108 | //pix.col.w = glstate.material.emission.x; |
---|
109 | |
---|
110 | // compute eye linear depth |
---|
111 | const float2 screenCoord = projPos.xy * 0.5f + 0.5f; |
---|
112 | const float magView = length(Interpol(screenCoord, bl, br, tl, tr)); |
---|
113 | pix.col.w = length(eyePos - IN.worldPos.xyz) * maxDepth / magView; |
---|
114 | |
---|
115 | // save world position in second render target |
---|
116 | pix.pos = IN.worldPos * maxDepth; |
---|
117 | // store the projected depth |
---|
118 | pix.pos.w = projPos.z; |
---|
119 | |
---|
120 | return pix; |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | pixel frag(fragin IN, |
---|
125 | uniform float maxDepth, |
---|
126 | uniform float3 eyePos, |
---|
127 | uniform float3 bl,
|
---|
128 | uniform float3 br, |
---|
129 | uniform float3 tl, |
---|
130 | uniform float3 tr) |
---|
131 | { |
---|
132 | pixel pix; |
---|
133 | |
---|
134 | // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term) |
---|
135 | pix.col = glstate.material.diffuse + glstate.material.emission; |
---|
136 | |
---|
137 | pix.norm.xyz = IN.normal; |
---|
138 | |
---|
139 | // store projection coordinates with positions (used for ssao) |
---|
140 | pix.norm.w = IN.mypos.w; |
---|
141 | |
---|
142 | const float4 projPos = IN.mypos / IN.mypos.w; |
---|
143 | |
---|
144 | // hack: squeeze some information about the ambient term into the target |
---|
145 | //pix.col.w = glstate.material.emission.x; |
---|
146 | |
---|
147 | // compute eye linear depth |
---|
148 | float2 screenCoord = projPos.xy * 0.5f + 0.5f; |
---|
149 | const float magView = length(Interpol(screenCoord, bl, br, tl, tr)); |
---|
150 | pix.col.w = length(eyePos - IN.worldPos.xyz) * maxDepth / magView; |
---|
151 | |
---|
152 | pix.pos = IN.worldPos * maxDepth; |
---|
153 | // store the projected depth |
---|
154 | pix.pos.w = projPos.z; |
---|
155 | |
---|
156 | return pix; |
---|
157 | } |
---|