//==================================================== // DNSR Shader // Supports Diffuse, Normal, Specular and Reflection //==================================================== string ParamID = "0x003"; float4x4 WorldIT : WORLDINVERSETRANSPOSE; float4x4 Local2Proj : WORLDVIEWPROJECTION; float4x4 Local2World : WORLD; float4x4 Camera2World : VIEWINVERSE; float3 LightPosition: POSITION < string UIName = "Light Position"; string Object = "PointLight"; string Space = "World"; > = {-1.0f, 1.0f, 0.0f}; float3 LightColor < string UIName = "Light Color "; string UIWidget = "Color"; > = { 1.0f, 1.0f, 1.0f }; // float LightPower< // string UIWidget = "slider"; // string UIName = "Light Power"; // float UIMin = 0.0; // float UIMax = 1024.0; // float UIStep = 0.1; // > = 1.0; float LightStrength < string UIWidget = "slider"; string UIName = "Light Strength"; float UIMin = 0.0; float UIMax = 10.0; float UIStep = 1.0; > = 10.0; float3 AmbientColor < string UIName = "Ambient Color "; string UIWidget = "Color"; > = { 0.2f, 0.2f, 0.2f }; float DiffuseFactor < string UIWidget = "IntSpinner"; half UIMin = 0; half UIMax = 150.0; string UIName = "Diffuse Factor"; > = 1.0; texture TextureColor : Diffuse < string UIName = "Diffuse Texture"; int Texcoord = 0; // int MapChannel = 1; >; sampler TextureColorSampler = sampler_state { Texture = (TextureColor); MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; AddressU = Wrap; AddressV = Wrap; }; float BumpFactor < string UIWidget = "IntSpinner"; half UIMin = -150.0; half UIMax = 150.0; string UIName = "Bump Factor"; > = 1.0; texture TextureNormal : Normal < string UIName = "Normal Texture"; int Texcoord = 0; // int MapChannel = 1; >; sampler TextureNormalSampler = sampler_state { Texture = (TextureNormal); MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = Wrap; AddressV = Wrap; }; float SpecularFactor < string UIWidget = "IntSpinner"; half UIMin = 0.0; half UIMax = 1024.0; string UIName = "Specular Factor"; > = 1.0; float SpecularPower < string UIWidget = "IntSpinner"; half UIMin = 1; half UIMax = 1024.0; string UIName = "Light Specular Power"; > = 16.0; texture TextureSpecular < string UIName = "Specular Texture"; int Texcoord = 0; // int MapChannel = 1; >; sampler TextureSpecularSampler = sampler_state { Texture = (TextureSpecular); MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; AddressU = Wrap; AddressV = Wrap; }; float ReflectionFactor < string UIWidget = "IntSpinner"; half UIMin = 0.0; half UIMax = 150.0; string UIName = "Reflection Factor"; > = 1.0; texture TextureReflection < string UIName = "Reflection Texture"; int Texcoord = 0; // int MapChannel = 1; >; sampler TextureReflectionSampler = sampler_state { Texture = (TextureReflection); MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; AddressU = Wrap; AddressV = Wrap; }; texture TextureEnvironment < string UIName = "Environment"; string ResourceType = "CUBE"; >; samplerCUBE TextureEnvironmentSampler = sampler_state { Texture = (TextureEnvironment); MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = clamp; AddressV = clamp; AddressW = clamp; }; //-------------- // vertex shader //-------------- struct VSInput { float4 Position : POSITION; float4 Normal : NORMAL; float4 Tangent : TANGENT; float4 Binormal : BINORMAL; float2 UV0 : TEXCOORD0; }; struct PSInput { float4 __Position : POSITION; float2 UV0 : TEXCOORD0; float3 ToLight : TEXCOORD1; float3 ToCamera : TEXCOORD2; float3 Normal : TEXCOORD3; float3 Tangent : TEXCOORD4; float3 Binormal : TEXCOORD5; }; PSInput VS( VSInput _Input ) { PSInput Output; Output.__Position = mul( _Input.Position, Local2Proj ); float3 WorldPosition = mul( _Input.Position, Local2World ).xyz; Output.ToCamera = Camera2World[3].xyz - WorldPosition; Output.ToLight = LightPosition - WorldPosition; Output.UV0 = _Input.UV0; Output.Normal = mul( _Input.Normal, WorldIT ).xyz; Output.Tangent = mul( _Input.Tangent, WorldIT ).xyz; Output.Binormal = mul( _Input.Binormal, WorldIT ).xyz; return Output; } //-------------- // pixel shader //-------------- float4 PS( PSInput _Input ) : COLOR { // Retrieve texture values float3 ColorColor = DiffuseFactor * tex2D( TextureColorSampler, _Input.UV0 ).xyz; float3 SpecularColor = SpecularFactor * tex2D( TextureSpecularSampler, _Input.UV0 ).xyz; float3 ReflectionColor = ReflectionFactor * tex2D( TextureReflectionSampler, _Input.UV0 ).xyz; float3 Normal = 2 * tex2D( TextureNormalSampler, _Input.UV0 ) - 1; Normal *= float3( BumpFactor, BumpFactor, 1 ); // Transform normal into WORLD space _Input.Binormal = normalize( _Input.Binormal ); _Input.Tangent = normalize( _Input.Tangent ); _Input.Normal = normalize( _Input.Normal ); float3x3 Tangent2World = { _Input.Binormal, _Input.Tangent, _Input.Normal }; Normal = mul( Normal, Tangent2World ); Normal = normalize( Normal ); // Compute camera & lights vectors float3 ToLight = normalize( _Input.ToLight ); float3 ToCamera = normalize( _Input.ToCamera ); float3 Reflected = reflect( ToCamera, Normal ); // Fetch lighting in reflected direction float3 SampleDir = float3( -Reflected.x, -Reflected.z, Reflected.y ); float3 ReflectedEnvColor = texCUBE( TextureEnvironmentSampler, SampleDir ); // Compute specular factor float fSpecularDot = 0.3 + 0.7 * saturate( dot( -Reflected, ToLight ) ); float fSpecularFactor = pow( fSpecularDot, SpecularPower ); ////// COMBINE ////// // float t = saturate( LightStrength * (1 + (saturate( dot( ToLight, Normal ) )) ) / (10 + LightStrength) ); float t = saturate( saturate( dot( ToLight, Normal ) + LightStrength ) / (10 - LightStrength) ); float b = 3 - 2 * t; float t2 = t * t; float GlobalLightFactor = b * t2; float3 Color = 0; // Color += GlobalLightFactor * ColorColor * (LightPower * LightColor + AmbientColor); // Some combination of ambient & diffuse modulated by a factor depending on dot( light, normal ) // Color += fSpecularFactor * SpecularColor * LightPower * LightColor; // Color += ReflectionColor * ReflectedEnvColor * LightPower * LightColor; Color += GlobalLightFactor * ColorColor * (LightColor + AmbientColor); // Some combination of ambient & diffuse modulated by a factor depending on dot( light, normal ) Color += fSpecularFactor * SpecularColor * LightColor; Color += ReflectionColor * ReflectedEnvColor * LightColor; return float4( Color, 1 ); } //-------------- // techniques //-------------- technique Pipo { pass ModelPass < string Script= "RenderColorTarget0=;" "Draw=Geometry;"; > { ZEnable = true; ZWriteEnable = true; CullMode = none; AlphaTestEnable = TRUE; AlphaFunc = 7; AlphaRef = 350; AlphaBlendEnable= TRUE; DestBlend = INVSRCALPHA; SrcBlend = SRCALPHA; VertexShader = compile vs_1_1 VS(); PixelShader = compile ps_2_0 PS(); } }