View difference between Paste ID: zXi0hmsU and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
1 | + | //----------------------------------------------------------------------------- |
2 | // Copyright (c) 2008 dhpoware. All Rights Reserved. | |
3 | // | |
4 | // Permission is hereby granted, free of charge, to any person obtaining a | |
5 | // copy of this software and associated documentation files (the "Software"), | |
6 | // to deal in the Software without restriction, including without limitation | |
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
8 | // and/or sell copies of the Software, and to permit persons to whom the | |
9 | // Software is furnished to do so, subject to the following conditions: | |
10 | // | |
11 | // The above copyright notice and this permission notice shall be included in | |
12 | // all copies or substantial portions of the Software. | |
13 | // | |
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
20 | // IN THE SOFTWARE. | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
23 | //----------------------------------------------------------------------------- | |
24 | // Globals. | |
25 | //----------------------------------------------------------------------------- | |
26 | ||
27 | float4x4 world; | |
28 | float4x4 view; | |
29 | float4x4 projection; | |
30 | float4x4 lightViewProjection; | |
31 | float4x4 textureScaleBias; | |
32 | ||
33 | float3 lightDir; | |
34 | float4 lightColor; | |
35 | float4 materialAmbient; | |
36 | float4 materialDiffuse; | |
37 | ||
38 | float depthBias; | |
39 | float texelSize; | |
40 | ||
41 | //----------------------------------------------------------------------------- | |
42 | // Textures. | |
43 | //----------------------------------------------------------------------------- | |
44 | ||
45 | texture shadowMap; | |
46 | sampler shadowMapSampler = sampler_state | |
47 | { | |
48 | Texture = <shadowMap>; | |
49 | MinFilter = Point; | |
50 | MagFilter = Point; | |
51 | MipFilter = None; | |
52 | AddressU = Clamp; | |
53 | AddressV = Clamp; | |
54 | }; | |
55 | ||
56 | //----------------------------------------------------------------------------- | |
57 | // Vertex shaders. | |
58 | //----------------------------------------------------------------------------- | |
59 | ||
60 | void VS_Lambert(in float4 inPosition : POSITION, | |
61 | void VS_LambertWithShadows(in float4 inPosition : POSITION, | |
62 | in float2 inTexCoord : TEXCOORD, | |
63 | in float3 inNormal : NORMAL, | |
64 | out float4 outPosition : POSITION, | |
65 | out float4 outLightSpacePos : TEXCOORD0, | |
66 | out float2 outShadowTexCoord : TEXCOORD1, | |
67 | out float2 outTexCoord : TEXCOORD2, | |
68 | out float3 outNormal : TEXCOORD3, | |
69 | out float3 outLightDir : TEXCOORD4) | |
70 | { | |
71 | float4x4 worldViewProjection = mul(mul(world, view), projection); | |
72 | float4 lightSpacePos = mul(mul(inPosition, world), lightViewProjection); | |
73 | float4 shadowCoord = mul(lightSpacePos, textureScaleBias); | |
74 | ||
75 | outPosition = mul(inPosition, worldViewProjection); | |
76 | outLightSpacePos = lightSpacePos; | |
77 | outShadowTexCoord = shadowCoord.xy / shadowCoord.w; | |
78 | outTexCoord = inTexCoord; | |
79 | outNormal = mul(inNormal, (float3x3)world); | |
80 | outLightDir = -lightDir; | |
81 | } | |
82 | ||
83 | //----------------------------------------------------------------------------- | |
84 | // Pixel shaders. | |
85 | //----------------------------------------------------------------------------- | |
86 | ||
87 | float PS_ShadowMapLookup(sampler shadowMap, float2 texCoord, float depth) | |
88 | { | |
89 | return (tex2D(shadowMap, texCoord).r + 0.1 < depth) ? 0.0f : 1.0f; | |
90 | } | |
91 | ||
92 | void PS_LambertWithShadows(in float4 inLightSpacePos : TEXCOORD0, | |
93 | in float2 inShadowTexCoord : TEXCOORD1, | |
94 | in float2 inTexCoord : TEXCOORD2, | |
95 | in float3 inNormal : TEXCOORD3, | |
96 | in float3 inLightDir : TEXCOORD4, | |
97 | out float4 outColor : COLOR) | |
98 | { | |
99 | float3 l = normalize(inLightDir); | |
100 | float3 n = normalize(inNormal); | |
101 | float nDotL = saturate(dot(n, l)); | |
102 | ||
103 | float depth = inLightSpacePos.z / inLightSpacePos.w; | |
104 | float shadowOcclusion = PS_ShadowMapLookup(shadowMapSampler, inShadowTexCoord, depth); | |
105 | ||
106 | outColor = (materialAmbient * lightColor) + | |
107 | (materialDiffuse * lightColor * nDotL) * shadowOcclusion; | |
108 | } | |
109 | ||
110 | ||
111 | technique LambertWithShadows | |
112 | { | |
113 | pass | |
114 | { | |
115 | VertexShader = compile vs_2_0 VS_LambertWithShadows(); | |
116 | PixelShader = compile ps_2_0 PS_LambertWithShadows(); | |
117 | } | |
118 | } |