Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------------------------------------------------
- // File: BasicRender.fx
- //
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //------------------------------------------------------------------------------
- float3 lightPosition; // Position of light
- float4 lightDiffuse; // Light's diffuse color
- texture tex0; // Color texture for mesh
- texture cubeShadowMap; // Shadow map texture for lighting
- float4x4 worldMat; // World matrix for object
- float4x4 worldViewProjMat;
- float geometry_scale;
- float depth_bias;
- //------------------------------------------------------------------------------
- // Texture samplers
- //------------------------------------------------------------------------------
- sampler sampler0 =
- sampler_state
- {
- Texture = <tex0>;
- MipFilter = LINEAR;
- MinFilter = LINEAR;
- MagFilter = LINEAR;
- };
- sampler cubeShadowMapSampler =
- sampler_state
- {
- Texture = <cubeShadowMap>;
- MinFilter = POINT;
- MagFilter = POINT;
- MipFilter = POINT;
- AddressU = Clamp;
- AddressV = Clamp;
- };
- //------------------------------------------------------------------------------
- // Vertex shader output structure
- //------------------------------------------------------------------------------
- struct VS_OUTPUT
- {
- float4 Position : POSITION0; // vertex position
- float2 TextureUV : TEXCOORD0; // vertex texture coords
- float3 wNormal : TEXCOORD1;
- float4 vPos : TEXCOORD2;
- float3 wPos : TEXCOORD3;
- float4 Colour : COLOR0;
- };
- struct PS_INPUT
- {
- float2 TextureUV : TEXCOORD0; // vertex texture coords
- float3 wNormal : TEXCOORD1;
- float4 vPos : TEXCOORD2;
- float3 wPos : TEXCOORD3;
- float4 Colour : COLOR0;
- };
- struct VS_SHADOW_OUTPUT
- {
- float4 Position : POSITION;
- float2 Depth : TEXCOORD0;
- };
- VS_SHADOW_OUTPUT RenderShadowMapVS(float4 vPos: POSITION)
- {
- VS_SHADOW_OUTPUT Out;
- float4 posH = mul(vPos, worldViewProjMat);
- Out.Position = posH;
- Out.Depth = posH.zw;
- return Out;
- }
- float4 RenderShadowMapPS( VS_SHADOW_OUTPUT In ) : COLOR0
- {
- return In.Depth.x;
- }
- struct PS_OUTPUT
- {
- float4 RGBColor : COLOR0; // Pixel color
- };
- VS_OUTPUT RenderShadowsVS(
- float3 position : POSITION,
- float3 normal : NORMAL,
- float2 TexCoord0 : TEXCOORD0,
- float4 colour : COLOR0)
- {
- VS_OUTPUT Output;
- //transform the input position to the output
- Output.Position = mul(float4(position, 1.0), worldViewProjMat);
- //transform the normal to world space
- Output.wNormal = mul(normal, worldMat);
- //do not transform the position needed for the
- //shadow map determination
- Output.vPos = float4(position,1.0);
- Output.wPos = mul(float4(position, 1.0), worldMat).xyz;
- //pass the texture coordinate as-is
- Output.TextureUV = TexCoord0;
- Output.Colour = colour;
- //return the output structure
- return Output;
- }
- PS_OUTPUT RenderShadowsPS( PS_INPUT In )
- {
- PS_OUTPUT Output;
- float4 vTotalLightDiffuse = float4(0,0,0,0);
- float3 lightVecW = normalize(lightPosition - In.wPos);
- float diffuseLightIntensity = dot(In.wNormal, lightVecW);
- vTotalLightDiffuse += lightDiffuse * max(0, diffuseLightIntensity);
- float4 vPos = mul(In.vPos, worldMat);
- float shadowdepth = texCUBE(cubeShadowMapSampler, -lightVecW.xyz).x;
- float pixelDepth = length(lightPosition - vPos);
- if (shadowdepth > pixelDepth)
- {
- // we're in shadow, cut the light
- vTotalLightDiffuse = float4(0,0,0,1);
- }
- Output.RGBColor = tex2D(sampler0, In.TextureUV) * In.Colour * vTotalLightDiffuse;
- return Output;
- }
- PS_OUTPUT DiffuseOnlyPS(VS_OUTPUT In)
- {
- PS_OUTPUT Output;
- //calculate per-pixel diffuse
- float3 directionToLight = normalize(lightPosition - In.vPos);
- float diffuseIntensity = saturate( dot(directionToLight, In.wNormal));
- float4 diffuse = lightDiffuse * diffuseIntensity;
- float4 color = diffuse;
- color.a = 1.0;
- Output.RGBColor = tex2D(sampler0, In.TextureUV) * color;
- return Output;
- }
- PS_OUTPUT TextureOnlyPS(float2 TextureUV : TEXCOORD0)
- {
- PS_OUTPUT Output;
- Output.RGBColor = tex2D(sampler0, TextureUV);
- return Output;
- }
- struct AmbientVSOutput
- {
- float4 Pos : POSITION;
- float2 Tex : TEXCOORD0;
- float4 Colour : COLOR0;
- };
- AmbientVSOutput ambient_VS(
- float4 inPosition : POSITION,
- float4 inColour : COLOR0,
- float2 tex : TEXCOORD0)
- {
- AmbientVSOutput output;
- output.Pos = mul(inPosition, worldViewProjMat);
- output.Tex = tex;
- output.Colour = inColour;
- return output;
- }
- float4 ambient_PS(AmbientVSOutput input) : COLOR0
- {
- float4 ambient = float4(0.1f, 0.1f, 0.1f, 1.0f);
- return ambient * (input.Colour * tex2D(sampler0, input.Tex));
- }
- technique cubicShadowMapping
- {
- pass P0
- {
- ZEnable = true;
- ZWriteEnable = true;
- AlphaBlendEnable = true;
- SrcBlend = One;
- DestBlend = One;
- VertexShader = compile vs_2_0 RenderShadowsVS();
- PixelShader = compile ps_2_0 RenderShadowsPS();
- }
- }
- technique depthMap
- {
- pass P0
- {
- CullMode = NONE;
- ZEnable = true;
- ZWriteEnable = true;
- AlphaBlendEnable = true;
- SrcBlend = One;
- DestBlend = One;
- VertexShader = compile vs_2_0 RenderShadowMapVS();
- PixelShader = compile ps_2_0 RenderShadowMapPS();
- }
- }
- technique ambient
- {
- pass P0
- {
- ZEnable = true;
- AlphaBlendEnable = false;
- ZWriteEnable = true;
- SrcBlend = One;
- DestBlend = Zero;
- VertexShader = compile vs_2_0 ambient_VS( );
- PixelShader = compile ps_2_0 ambient_PS( );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement