//==================================================== // 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: Diffuse < string UIName = "Light Color "; string UIWidget = "Color"; > = { 1.0f, 1.0f, 1.0f }; 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 : Diffuse < string UIName = "Ambient Color "; string UIWidget = "Color"; > = { 0.2f, 0.2f, 0.2f }; float DiffuseFactor < string UIWidget = "IntSpinner"; half UIMin = 0; string UIName = "Diffuse Factor"; > = 1.0; texture TxtColor : Diffuse < string UIName = "Diffuse Texture"; string ResourceName = ""; string ResourceType = "2D"; int Texcoord = 0; >; sampler TextureColor = sampler_state { Texture = ; MinFilter = LINEAR; MagFilter = LINEAR; }; bool bEnableEmissive< string UIName = "Emissive Enable"; > = true; float EmissiveFactor < string UIWidget = "IntSpinner"; half UIMin = 0; string UIName = "Emissive Factor"; > = 1.0; texture TxtEmissive : Diffuse < string UIName = "Emissive Texture"; int Texcoord = 0; // int MapChannel = 1; >; sampler TextureEmissive = sampler_state { Texture = (TxtEmissive); MinFilter = LINEAR; MagFilter = LINEAR; }; bool bEnableNormal< string UIName = "Normal Enable"; > = true; float BumpFactor < string UIWidget = "IntSpinner"; half UIMin = -150.0; half UIMax = 150.0; string UIName = "Bump Factor"; > = 1.0; texture TxtNormal : Normal < string UIName = "Normal Texture"; int Texcoord = 0; // int MapChannel = 1; >; sampler TextureNormal = sampler_state { Texture = (TxtNormal); MinFilter = Linear; MagFilter = Linear; }; bool bEnableSpecular< string UIName = "Specular Enable"; > = true; float SpecularFactor < string UIWidget = "IntSpinner"; half UIMin = 0.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 TxtSpecular < string UIName = "Specular Texture"; int Texcoord = 0; // int MapChannel = 1; >; sampler TextureSpecular = sampler_state { Texture = (TxtSpecular); MinFilter = LINEAR; MagFilter = LINEAR; }; bool bEnableReflection < string UIName = "Reflection Enable"; > = true; float ReflectionFactor < string UIWidget = "IntSpinner"; half UIMin = 0.0; string UIName = "Reflection Factor"; > = 1.0; texture TxtReflection < string UIName = "Reflection Texture"; int Texcoord = 0; // int MapChannel = 1; >; sampler TextureReflection = sampler_state { Texture = (TxtReflection); MinFilter = LINEAR; MagFilter = LINEAR; }; texture TxtEnvironment < string UIName = "Environment"; string ResourceType = "CUBE"; >; samplerCUBE TextureEnvironment = sampler_state { Texture = (TxtEnvironment); MinFilter = Linear; MagFilter = Linear; }; bool bEnableDetail < string UIName = "Detail Enable"; > = false; float DetailFactor < string UIWidget = "IntSpinner"; half UIMin = 0.0; string UIName = "Detail Factor"; > = 0.5; float DetailScale < string UIWidget = "IntSpinner"; half UIMin = 0.01; string UIName = "Detail Scale"; > = 8.0; texture TxtDetail < string UIName = "Detail Texture"; int Texcoord = 0; // int MapChannel = 1; >; sampler TextureDetail = sampler_state { Texture = (TxtDetail); MinFilter = LINEAR; MagFilter = LINEAR; }; //-------------- // 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 { return float4( 1, 1, 0, 1 ); // Retrieve texture values float3 EmissiveColor = step( 1, bEnableEmissive ) * EmissiveFactor * tex2D( TextureEmissive, _Input.UV0 ).xyz; float3 ColorColor = DiffuseFactor * tex2D( TextureColor, _Input.UV0 ).xyz; float3 SpecularColor = step( 1, bEnableSpecular ) * SpecularFactor * tex2D( TextureSpecular, _Input.UV0 ).xyz; float3 ReflectionColor = step( 1, bEnableReflection ) * ReflectionFactor * tex2D( TextureReflection, _Input.UV0 ).xyz; float3 DetailColor = step( 1, bEnableDetail ) * DetailFactor * (tex2D( TextureDetail, _Input.UV0 * DetailScale ).xyz - 0.5); float3 Normal = 2 * tex2D( TextureNormal, _Input.UV0 ) - 1; Normal *= float3( BumpFactor, BumpFactor, 1 ); ColorColor += DetailColor; if ( !bEnableNormal ) Normal = float3( 0, 0, 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( TextureEnvironment, 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 = EmissiveColor * LightColor; Color += ColorColor * lerp( AmbientColor, LightColor, GlobalLightFactor ); // 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 Default { pass ModelPass < string Script= "RenderColorTarget0=;" "Draw=Geometry;"; > { ZEnable = true; ZWriteEnable = true; CullMode = none; // AlphaTestEnable = TRUE; // AlphaFunc = 7; // AlphaRef = 350; // AlphaBlendEnable= TRUE; VertexShader = compile vs_1_1 VS(); PixelShader = compile ps_2_0 PS(); } }