//==================================================== // Adaptation of Wang Jing shader by Patapom (http://rtshaders.deviantart.com) //==================================================== 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}; float4 LightColor < string UIName = "Light Color "; string UIWidget = "Color"; > = {1.0f, 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; > = 3.0; float SpecularPower < string UIWidget = "IntSpinner"; half UIMin = 1; half UIMax = 1024.0; string UIName = "Light Specular Power"; > = 16.0; float SpecularHighlightsStrength < string UIWidget = "IntSpinner"; half UIMin = 0.00; half UIMax = 1024.0; string UIName = "Specular Highlights Strength"; > = 1.0; float4 DiffuseColor < string UIName = "Diffuse Color "; string UIWidget = "Color"; > = {1.0f, 1.0f, 1.0f, 1.0f}; 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; }; texture TextureAO < string UIName = "Ao Map/Lightmap"; int Texcoord = 1; int MapChannel = 2; >; sampler TextureAOSampler = sampler_state { Texture = (TextureAO); MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; AddressU = Wrap; AddressV = Wrap; }; float AOStrength< string UIWidget = "slider"; string UIName = "Ao Map/Lightmap Strength"; float UIMin = 0.01; float UIMax = 1024.0; float UIStep = 1.0; > = 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 BumpX< string UIWidget = "IntSpinner"; half UIMin = -150.0; half UIMax = 150.0; string UIName = "BumpX"; > = 1.0; float BumpY< string UIWidget = "IntSpinner"; half UIMin = -150.0; half UIMax = 150.0; string UIName = "BumpY"; > = 1.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; }; texture TextureSpecularLevel < string UIName = "Specular Level"; int Texcoord = 0; int MapChannel = 1; >; sampler TextureSpecularLevelSampler = sampler_state { Texture = (TextureSpecularLevel); MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; AddressU = Wrap; AddressV = Wrap; }; texture TextureGloss < string UIName = "Gloss Texture"; int Texcoord = 0; int MapChannel = 1; >; sampler TextureGlossSampler = sampler_state { Texture = (TextureGloss); MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; AddressU = Wrap; AddressV = Wrap; }; texture TextureDiffuseEnvironment < string UIName = "Diffuse CubeMap"; string ResourceType = "CUBE"; >; samplerCUBE TextureDiffuseEnvironmentSampler = sampler_state { Texture = (TextureDiffuseEnvironment); MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = clamp; AddressV = clamp; AddressW = clamp; }; 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; float2 UV1 : TEXCOORD1; }; 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 = LightPosition - WorldPosition; Output.UV0 = _Input.UV0; Output.UV1 = _Input.UV1; 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 ); // float3 ImageReflectedEnvironmentColor = texCUBE( TextureEnvironmentSampler, SampleDir ); // Compute ambient factor float fAmbientFactor = LightPower; // Compute diffuse factor float fDiffuseFactor = LightPower * saturate( dot( ToLight, Normal ) ); // 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( AOStrength * AO ); float fAOFactor = saturate( pow( AO, 1 / AOStrength ) ); ////// COMBINE ////// // float3 Color = fAOFactor * ColorColor * ( // fAmbientFactor * DiffuseColor + // Ambient // fDiffuseFactor * ImageLightColor + // Diffuse // fSpecularFactor * ImageReflectedEnvironmentColor + // Specular // fGlossFactor * ImageReflectedLightColor // Gloss // ); // 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 * LightPower * LightColor * DiffuseColor * ImageLightColor * ColorColor; // DIFFUSE AMBIENT à moduler par LightStrength [0,10] Color += GlobalLightFactor * LightPower * LightColor * ColorColor; Color += LightPower * LightColor * fSpecularFactor * SpecularColor; Color += ImageReflectedLightColor * fGlossFactor * SpecularColor; Color *= fAOFactor; 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(); } }