Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/data_extern/programs/Cg/pssm.cg @ 12202

Last change on this file since 12202 was 12115, checked in by wiesep, 6 years ago

Changed folder structure, deletet some unused files and cleaned up code

File size: 4.9 KB
Line 
1void shadow_caster_vs(
2        float4 position                 : POSITION,
3
4        out float4 oPosition    : POSITION,
5        out float2 oDepth               : TEXCOORD0,
6
7        uniform float4x4 wvpMat)
8{
9        // this is the view space position
10        oPosition = mul(wvpMat, position);
11
12        // depth info for the fragment.
13        oDepth.x = oPosition.z;
14        oDepth.y = oPosition.w;
15
16        // clamp z to zero. seem to do the trick. :-/
17        //oPosition.z = max(oPosition.z, 0);
18}
19
20void shadow_caster_ps(
21        float2 depth            : TEXCOORD0,
22
23        out float4 oColour      : COLOR,
24
25        uniform float4 pssmSplitPoints)
26{
27        float finalDepth = depth.x / depth.y;
28        oColour = float4(finalDepth, finalDepth, finalDepth, 1);
29}
30
31
32
33void shadow_receiver_vs(
34        float4 position                         : POSITION,
35        float3 normal                           : NORMAL,
36        float2 uv                                       : TEXCOORD0,
37
38        out float4 oPosition            : POSITION,
39        out float3 oUv                          : TEXCOORD0,
40        out float3 oLightDir            : TEXCOORD1,
41        out float3 oHalfAngle           : TEXCOORD2,
42        out float4 oLightPosition0      : TEXCOORD3,
43        out float4 oLightPosition1      : TEXCOORD4,
44        out float4 oLightPosition2      : TEXCOORD5,
45        out float3 oNormal                      : TEXCOORD6,
46
47        uniform float4 lightPosition,                           // object space
48        uniform float3 eyePosition,                                     // object space
49        uniform float4x4 worldViewProjMatrix,
50        uniform float4x4 texWorldViewProjMatrix0,
51        uniform float4x4 texWorldViewProjMatrix1,
52        uniform float4x4 texWorldViewProjMatrix2)
53{
54        // calculate output position
55        oPosition = mul(worldViewProjMatrix, position);
56
57        // pass the main uvs straight through unchanged
58        oUv.xy = uv;
59        oUv.z = oPosition.z;
60
61        // calculate tangent space light vector
62        // Get object space light direction
63        oLightDir = normalize(lightPosition.xyz - (position * lightPosition.w).xyz);
64
65        // Calculate half-angle in tangent space
66        float3 eyeDir = normalize(eyePosition - position.xyz);
67        oHalfAngle = normalize(eyeDir + oLightDir);     
68
69        // Calculate the position of vertex in light space
70        oLightPosition0 = mul(texWorldViewProjMatrix0, position);
71        oLightPosition1 = mul(texWorldViewProjMatrix1, position);
72        oLightPosition2 = mul(texWorldViewProjMatrix2, position);
73
74        oNormal = normal;
75}
76
77float shadowPCF(sampler2D shadowMap, float4 shadowMapPos, float2 offset)
78{
79        shadowMapPos = shadowMapPos / shadowMapPos.w;
80        float2 uv = shadowMapPos.xy;
81        float3 o = float3(offset, -offset.x) * 0.3f;
82
83        // Note: We using 2x2 PCF. Good enough and is alot faster.
84        float c =       (shadowMapPos.z <= tex2D(shadowMap, uv.xy - o.xy).r) ? 1 : 0; // top left
85        c +=            (shadowMapPos.z <= tex2D(shadowMap, uv.xy + o.xy).r) ? 1 : 0; // bottom right
86        c +=            (shadowMapPos.z <= tex2D(shadowMap, uv.xy + o.zy).r) ? 1 : 0; // bottom left
87        c +=            (shadowMapPos.z <= tex2D(shadowMap, uv.xy - o.zy).r) ? 1 : 0; // top right
88        //float c =     (shadowMapPos.z <= tex2Dlod(shadowMap, uv.xyyy - o.xyyy).r) ? 1 : 0; // top left
89        //c +=          (shadowMapPos.z <= tex2Dlod(shadowMap, uv.xyyy + o.xyyy).r) ? 1 : 0; // bottom right
90        //c +=          (shadowMapPos.z <= tex2Dlod(shadowMap, uv.xyyy + o.zyyy).r) ? 1 : 0; // bottom left
91        //c +=          (shadowMapPos.z <= tex2Dlod(shadowMap, uv.xyyy - o.zyyy).r) ? 1 : 0; // top right
92        return c / 4;
93}
94
95// to put it simply, this does 100% per pixel diffuse lighting
96void shadow_receiver_ps(
97        float3 uv                               : TEXCOORD0,
98        float3 OSlightDir                       : TEXCOORD1,
99        float3 OShalfAngle              : TEXCOORD2,
100        float4 LightPosition0   : TEXCOORD3,
101        float4 LightPosition1   : TEXCOORD4,
102        float4 LightPosition2   : TEXCOORD5,
103        float3 normal                   : TEXCOORD6,
104
105        out float4 oColour              : COLOR,
106
107        uniform float4 invShadowMapSize0,
108        uniform float4 invShadowMapSize1,
109        uniform float4 invShadowMapSize2,
110        uniform float4 pssmSplitPoints,
111        uniform sampler2D diffuse,
112        uniform sampler2D specular,
113        uniform sampler2D normalMap,
114        uniform sampler2D shadowMap0,
115        uniform sampler2D shadowMap1,
116        uniform sampler2D shadowMap2,
117        uniform float4 lightDiffuse,
118        uniform float4 lightSpecular,
119        uniform float4 ambient
120        )
121{
122        // calculate shadow
123        float shadowing = 1.0f;
124        float4 splitColour;
125        if (uv.z <= pssmSplitPoints.y)
126        {
127                splitColour = float4(0.1, 0, 0, 1);
128                shadowing = shadowPCF(shadowMap0, LightPosition0, invShadowMapSize0.xy);
129        }
130        else if (uv.z <= pssmSplitPoints.z)
131        {
132                splitColour = float4(0, 0.1, 0, 1);
133                shadowing = shadowPCF(shadowMap1, LightPosition1, invShadowMapSize1.xy);
134        }
135        else
136        {
137                splitColour = float4(0.1, 0.1, 0, 1);
138                shadowing = shadowPCF(shadowMap2, LightPosition2, invShadowMapSize2.xy);
139        }
140
141        // retrieve normalised light vector, expand from range-compressed
142        float3 lightVec = normalize(OSlightDir);
143
144        // retrieve half angle and normalise through cube map
145        float3 halfAngle = normalize(OShalfAngle);
146
147        // get diffuse colour
148        float4 diffuseColour = tex2D(diffuse, uv.xy);
149
150        // specular
151        float4 specularColour = tex2D(specular, uv.xy);
152        float shininess = specularColour.w;
153        specularColour.w = 1;
154
155        // calculate lit value.
156        float4 lighting = lit(dot(normal, lightVec), dot(normal, halfAngle), shininess * 128) * shadowing;
157
158        // final lighting with diffuse and spec
159        oColour = (diffuseColour * clamp(ambient + lightDiffuse * lighting.y, 0, 1)) + (lightSpecular * specularColour * lighting.z);
160        oColour.w = diffuseColour.w;
161
162        //oColour += splitColour;
163}
164
Note: See TracBrowser for help on using the repository browser.