1 | // input
|
---|
2 | struct vtxin
|
---|
3 | {
|
---|
4 | float4 position: POSITION;
|
---|
5 | float4 normal: NORMAL;
|
---|
6 | float4 color: COLOR0;
|
---|
7 | float4 texCoord: TEXCOORD0;
|
---|
8 | };
|
---|
9 |
|
---|
10 |
|
---|
11 | // vtx output
|
---|
12 | struct vtxout
|
---|
13 | {
|
---|
14 | float4 position: POSITION;
|
---|
15 | float4 texCoord: TEXCOORD0;
|
---|
16 |
|
---|
17 | float4 color: COLOR0;
|
---|
18 | float4 eyePos: TEXCOORD1; // eye position
|
---|
19 | float4 normal: TEXCOORD2;
|
---|
20 | float4 worldPos: TEXCOORD3;
|
---|
21 | float4 oldWorldPos: TEXCOORD4;
|
---|
22 | };
|
---|
23 |
|
---|
24 |
|
---|
25 | // fragment input
|
---|
26 | struct fragin
|
---|
27 | {
|
---|
28 | float4 color: COLOR0;
|
---|
29 | float4 texCoord: TEXCOORD0;
|
---|
30 |
|
---|
31 | float4 winPos: WPOS;
|
---|
32 | float4 eyePos: TEXCOORD1; // eye position
|
---|
33 | float4 normal: TEXCOORD2;
|
---|
34 | float4 worldPos: TEXCOORD3;
|
---|
35 | float4 oldWorldPos: TEXCOORD4;
|
---|
36 | };
|
---|
37 |
|
---|
38 |
|
---|
39 | struct pixel
|
---|
40 | {
|
---|
41 | float4 col: COLOR0;
|
---|
42 | float3 norm: COLOR1;
|
---|
43 | float3 offsVec: COLOR2;
|
---|
44 | };
|
---|
45 |
|
---|
46 |
|
---|
47 | #pragma position_invariant vtx
|
---|
48 |
|
---|
49 | vtxout vtx(vtxin IN,
|
---|
50 | uniform float4x4 viewMatrix,
|
---|
51 | uniform float4x4 modelMatrix,
|
---|
52 | uniform float4x4 oldModelMatrix)
|
---|
53 | {
|
---|
54 | vtxout OUT;
|
---|
55 |
|
---|
56 | OUT.color = IN.color;
|
---|
57 | OUT.texCoord = IN.texCoord;
|
---|
58 |
|
---|
59 | // transform the vertex position into eye space
|
---|
60 | OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position);
|
---|
61 | // transform the vertex position into post projection space
|
---|
62 | OUT.position = mul(glstate.matrix.mvp, IN.position);
|
---|
63 |
|
---|
64 | // the normal has to be correctly transformed with the inverse transpose
|
---|
65 | OUT.normal = mul(glstate.matrix.invtrans.modelview[0], IN.normal);
|
---|
66 |
|
---|
67 | // transform the old vertex position into world space
|
---|
68 | OUT.worldPos = mul(modelMatrix, IN.position);
|
---|
69 | // transform the old vertex position into world space
|
---|
70 | OUT.oldWorldPos = mul(oldModelMatrix, IN.position);
|
---|
71 |
|
---|
72 | return OUT;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | pixel fragtex(fragin IN,
|
---|
77 | uniform sampler2D tex,
|
---|
78 | uniform float4x4 viewMatrix
|
---|
79 | )
|
---|
80 | {
|
---|
81 | float4 texColor = tex2D(tex, IN.texCoord.xy);
|
---|
82 |
|
---|
83 | // account for alpha blending
|
---|
84 | if (texColor.w < 0.5f) discard;
|
---|
85 |
|
---|
86 | pixel pix;
|
---|
87 |
|
---|
88 | // save color in first render target
|
---|
89 | // hack: use combination of emmisive + diffuse (emmisive used as constant ambient term)
|
---|
90 | pix.col = (glstate.material.emission + glstate.material.diffuse) * texColor;
|
---|
91 | // save world space normal in rt => transform back into world space by
|
---|
92 | // multiplying with inverse view. since transforming normal with T means to
|
---|
93 | // multiply with the inverse transpose of T, we multiple with
|
---|
94 | // Transp(Inv(Inv(view))) = Transp(view)
|
---|
95 | pix.norm = mul(transpose(viewMatrix), IN.normal).xyz;
|
---|
96 | // compute eye linear depth
|
---|
97 | pix.col.w = length(IN.eyePos.xyz);
|
---|
98 |
|
---|
99 | // the scene entity id
|
---|
100 | //pix.id = glstate.fog.color.xyz;
|
---|
101 | // the offset to the world pos from old frame
|
---|
102 | pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
|
---|
103 |
|
---|
104 | return pix;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | pixel frag(fragin IN, uniform float4x4 viewMatrix)
|
---|
109 | {
|
---|
110 | pixel pix;
|
---|
111 | // hack: use comination of emmisive + diffuse (emmisive used as constant ambient term)
|
---|
112 | pix.col = glstate.material.diffuse + glstate.material.emission;
|
---|
113 | // save world space normal in rt => transform back into world space by
|
---|
114 | // multiplying with inverse view. since transforming normal with T means to
|
---|
115 | // multiply with the inverse transpose of T, we multiple with Transp(Inv(Inv(view))) = Transp(view)
|
---|
116 | pix.norm = mul(transpose(viewMatrix), IN.normal).xyz;
|
---|
117 | // eye space depth
|
---|
118 | pix.col.w = length(IN.eyePos.xyz);
|
---|
119 | // the scene entity id
|
---|
120 | //pix.id = glstate.fog.color.xyz;
|
---|
121 | // the offset to the world pos from old frame
|
---|
122 | pix.offsVec = IN.oldWorldPos.xyz - IN.worldPos.xyz;
|
---|
123 |
|
---|
124 | return pix;
|
---|
125 | } |
---|