//==================================================== // Adaptation of Wang Jing (http://rtshaders.deviantart.com) shader by Patapom //==================================================== // // #o3d VertexShaderEntryPoint VS // #o3d PixelShaderEntryPoint PS // #o3d MatrixLoadOrder RowMajor float4x4 WorldIT : WORLDINVERSETRANSPOSE; float4x4 Local2Proj : WORLDVIEWPROJECTION; float4x4 Local2World : WORLD; float4x4 Camera2World : VIEWINVERSE; #include "../Data/Shaders/ShaderInterfaces/DirectionalLight.shader" #include "../Data/Shaders/ShaderInterfaces/AmbientLight.shader" float LightPower; float LightStrength; float3 DiffuseColor; float SpecularPower; float SpecularHighlightsStrength; sampler2D TextureColorSampler; sampler2D TextureAOSampler; float AOStrength; sampler2D TextureNormalSampler; float BumpX; float BumpY; sampler2D TextureSpecularSampler; sampler2D TextureSpecularLevelSampler; sampler2D TextureGlossSampler; samplerCUBE TextureDiffuseEnvironmentSampler; samplerCUBE TextureEnvironmentSampler; //-------------- // vertex shader //-------------- struct VSInput { float4 Position : POSITION; float4 Normal : NORMAL; float4 Tangent : TANGENT; float4 Binormal : BINORMAL; float2 UV0 : TEXCOORD0; // float2 UV1 : TEXCOORD1; // No second UV set supported for now... (that was the cause of failure!) }; struct PSInput { float4 __Position : POSITION; float2 UV0 : TEXCOORD0; float2 UV1 : TEXCOORD1; float3 ToLight : TEXCOORD2; float3 Normal : TEXCOORD3; float3 ToCamera : TEXCOORD4; float3 Tangent : TEXCOORD5; float3 Binormal : TEXCOORD6; }; 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 = LightWorldDirection; Output.UV0 = _Input.UV0; Output.UV1 = _Input.UV0; // No second UV set supported for now... Simply use the first channel... 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 = tex2D( TextureColorSampler, _Input.UV0 ).xyz; float3 SpecularColor = tex2D( TextureSpecularSampler, _Input.UV0 ).xyz; float SpecularLevel = tex2D( TextureSpecularLevelSampler, _Input.UV0 ).x; float3 GlossColor = tex2D( TextureGlossSampler, _Input.UV0 ).xyz; float AO = tex2D( TextureAOSampler, _Input.UV1 ).y; // Uses 2nd UV set float3 Normal = 2 * tex2D( TextureNormalSampler, _Input.UV0 ) - 1; Normal *= float3( BumpX, BumpY, 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 normal direction float3 SampleDir = float3( -Normal.x, -Normal.z, Normal.y ); float3 ImageLightColor = texCUBE( TextureDiffuseEnvironmentSampler, SampleDir ); // Fetch lighting in reflected direction SampleDir = float3( -Reflected.x, -Reflected.z, Reflected.y ); float3 ImageReflectedLightColor = texCUBE( TextureDiffuseEnvironmentSampler, SampleDir ); // Compute diffuse factor float t = saturate( saturate( dot( ToLight, Normal ) + LightStrength ) / (10 - LightStrength) ); float b = 3 - 2 * t; float t2 = t * t; float fDiffuseFactor = b * t2; // Compute specular factor float fSpecularDot = 0.3 + 0.7 * saturate( dot( -Reflected, ToLight ) ); float fSpecularFactor = SpecularHighlightsStrength * SpecularLevel * pow( fSpecularDot, SpecularPower ); // Compute gloss factor fSpecularDot *= fSpecularDot; fSpecularDot *= fSpecularDot; float fGlossFactor = SpecularHighlightsStrength * fSpecularDot; // Compute AO factor float fAOFactor = saturate( pow( AO, 1 / AOStrength ) ); ////// COMBINE ////// float3 Color = 0; // Color += fDiffuseFactor * LightPower * LightColor * DiffuseColor * ImageLightColor * ColorColor; Color += fDiffuseFactor * LightPower * AmbientColor * DiffuseColor * ColorColor; Color += fDiffuseFactor * LightPower * LightColor * ColorColor; Color += LightPower * LightColor * fSpecularFactor * SpecularColor; Color += ImageReflectedLightColor * fGlossFactor * SpecularColor; Color *= fAOFactor; return float4( Color, 1 ); } // technique Pipo // { // // pass ModelPass // { // ZEnable = true; // ZWriteEnable = true; // CullMode = CW; // // AlphaTestEnable = TRUE; // // AlphaFunc = 7; // // AlphaRef = 350; // // AlphaBlendEnable= TRUE; // // DestBlend = INVSRCALPHA; // // SrcBlend = SRCALPHA; // // VertexShader = compile vs_1_1 VS(); // PixelShader = compile ps_2_0 PS(); // } // }