// when DEBUG_VIEW is defined, texture using the RGB portion of the // shadow pass, to verify that projection is correct //#define DEBUG_VIEW #include float Script : STANDARDSGLOBAL < string UIWidget = "none"; string ScriptClass = "sceneorobject"; string ScriptOrder = "standard"; string ScriptOutput = "color"; string Script = "Technique=Technique?Shadowed:Unshadowed;"; > = 0.8; // version # float4 ClearColor < string UIWidget = "color"; string UIName = "background"; > = {0,0,0,0.0}; float ClearDepth = 1.0; float4 ShadowClearColor < string UIWidget = "none"; > = {1,1,1,0.0}; /************* "UN-TWEAKABLES," TRACKED BY CPU APPLICATION **************/ float4x4 WorldITXf : WorldInverseTranspose ; float4x4 WorldViewProjXf : WorldViewProjection ; float4x4 WorldXf : World ; float4x4 ViewIXf : ViewInverse ; float4x4 ViewITXf : ViewInverseTranspose ; DECLARE_SHADOW_XFORMS("light0",LampViewXf,LampProjXf,ShadowViewProjXf) DECLARE_SHADOW_BIAS DECLARE_SHADOW_MAPS(ColorShadMap,ColorShadSampler,ShadDepthTarget,ShadDepthSampler) /////////////////////////////////////////////////////////////// /// TWEAKABLES //////////////////////////////////////////////// /////////////////////////////////////////////////////////////// ////////////////////////////////////////////// spot light float3 SpotLightPos : POSITION < string UIName = "Light Posistion"; string Object = "SpotLight"; string Space = "World"; > = {-1.0f, 1.0f, 0.0f}; float3 SpotLightColor : Diffuse < string UIName = "Lamp"; string Object = "SpotLight"; string UIWidget = "Color"; > = {0.8f, 1.0f, 0.4f}; float SpotLightIntensity < string UIName = "Light Intensity"; string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 12; float UIStep = 0.1; > = 1; ////////////////////////////////////////////// ambient light float3 AmbiLightColor : Ambient < string UIName = "Ambient"; > = {0.07f, 0.07f, 0.07f}; ////////////////////////////////////////////// surface attributes float3 SurfColor : Diffuse < string UIName = "Surface"; string UIWidget = "Color"; > = {1.0f, 0.7f, 0.3f}; float Kd < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 1.5; float UIStep = 0.01; string UIName = "Diffuse"; > = 1.0; float Ks < string UIWidget = "slider"; float UIMin = 0.0; float UIMax = 1.5; float UIStep = 0.01; string UIName = "Specular"; > = 1.0; float SpecExpon : SpecularPower < string UIWidget = "slider"; float UIMin = 1.0; float UIMax = 128.0; float UIStep = 1.0; string UIName = "Specular power"; > = 12.0; //////////////////////////////////////////////////////////////////////////// /// SHADER CODE BEGINS ///////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// /*********************************************************/ /*********** pixel shader ********************************/ /*********************************************************/ // // core of the surface shading, shared by both shadowed and unshadowed versions // void lightingCalc(ShadowingVertexOutput IN, out float3 litContrib, out float3 ambiContrib) { float3 Nn = normalize(IN.WNormal); float3 Vn = normalize(IN.WView); Nn = faceforward(Nn,-Vn,Nn); float falloff = 1.0 / dot(IN.LightVec,IN.LightVec); float3 Ln = normalize(IN.LightVec); float3 Hn = normalize(Vn + Ln); float hdn = dot(Hn,Nn); float ldn = dot(Ln,Nn); float4 litVec = lit(ldn,hdn,SpecExpon); ldn = litVec.y * SpotLightIntensity; ambiContrib = SurfColor * AmbiLightColor; float3 diffContrib = SurfColor*(Kd*ldn * SpotLightColor); float3 specContrib = ((ldn * litVec.z * Ks) * SpotLightColor); float3 result = diffContrib + specContrib; float cone = tex2Dproj(SpotSamp,IN.LProj); litContrib = ((cone*falloff) * result); } float4 useShadowPS(ShadowingVertexOutput IN) : COLOR { #ifdef DEBUG_VIEW return tex2Dproj(ColorShadSampler,IN.LProj); // show the RGB render instead #else /*!DEBUG_VIEW */ float3 litPart, ambiPart; lightingCalc(IN,litPart,ambiPart); float4 shadowed = tex2Dproj(ShadDepthSampler,IN.LProj); return float4((shadowed.x*litPart)+ambiPart,1); #endif /*!DEBUG_VIEW */ } float4 unshadowedPS(ShadowingVertexOutput IN) : COLOR { #ifdef DEBUG_VIEW return tex2Dproj(ColorShadSampler,IN.LProj); // show the RGB render instead #else /*!DEBUG_VIEW */ float3 litPart, ambiPart; lightingCalc(IN,litPart,ambiPart); // float4 shadowed = tex2Dproj(ShadDepthSampler,IN.LProj); return float4(litPart+ambiPart,1); #endif /*!DEBUG_VIEW */ } //////////////////////////////////////////////////////////////////// /// TECHNIQUES ///////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////// technique Shadowed < string Script = "Pass=MakeShadow;" "Pass=UseShadow;"; > { pass MakeShadow < string Script = "RenderColorTarget0=ColorShadMap;" "RenderDepthStencilTarget=ShadDepthTarget;" "RenderPort=light0;" "ClearSetColor=ShadowClearColor;" "ClearSetDepth=ClearDepth;" "Clear=Color;" "Clear=Depth;" "Draw=geometry;"; > { VertexShader = compile vs_2_0 shadowGenVS(WorldXf,WorldITXf,ShadowViewProjXf); ZEnable = true; ZWriteEnable = true; ZFunc = LessEqual; CullMode = None; // no pixel shader! } pass UseShadow < string Script = "RenderColorTarget0=;" "RenderDepthStencilTarget=;" "RenderPort=;" "ClearSetColor=ClearColor;" "ClearSetDepth=ClearDepth;" "Clear=Color;" "Clear=Depth;" "Draw=geometry;"; > { VertexShader = compile vs_2_0 shadowUseVS(WorldXf,WorldITXf, WorldViewProjXf, ShadowViewProjXf,ViewIXf,ShadBiasXf, SpotLightPos); ZEnable = true; ZWriteEnable = true; ZFunc = LessEqual; CullMode = None; PixelShader = compile ps_2_a useShadowPS(); } } technique Unshadowed < string Script = "Pass=NoShadow;"; > { pass NoShadow < string Script = "RenderColorTarget0=;" "RenderDepthStencilTarget=;" "RenderPort=;" "ClearSetColor=ClearColor;" "ClearSetDepth=ClearDepth;" "Clear=Color;" "Clear=Depth;" "Draw=geometry;"; > { VertexShader = compile vs_2_0 shadowUseVS(WorldXf,WorldITXf, WorldViewProjXf, ShadowViewProjXf,ViewIXf,ShadBiasXf, SpotLightPos); ZEnable = true; ZWriteEnable = true; ZFunc = LessEqual; CullMode = None; PixelShader = compile ps_2_a unshadowedPS(); } } /***************************** eof ***/ ]]>; \ sampler SampName = sampler_state { \ texture = ; \ AddressU = AddrMode; \ AddressV = AddrMode; \ MipFilter = LINEAR; \ MinFilter = LINEAR; \ MagFilter = LINEAR; \ }; // // Simple 2D File Textures // // example usage: FILE_TEXTURE_2D(GlowMap,GlowSampler,"myfile.dds") // #define FILE_TEXTURE_2D(TextureName,SamplerName,Diskfile) FILE_TEXTURE_2D_MODAL(TextureName,SamplerName,(Diskfile),WRAP) // // Use this variation of D