SamplerState gSampleState : SampleState; RasterizerState gRasterizerState : RastState; float4x4 gWorldViewProj : WorldViewProjection; float4x4 gWorldMatrix : WorldMatrix; texture2D gDiffuseMap : DiffuseMap; texture2D gNormalMap : Normal; texture2D gSpecularMap : Specular; texture2D gGlossMap : Gloss; float3 gLightDirection : LightDirection; float3 gLightColor : LightColor; float3 gCameraPosition : CameraPosition; bool gUseNormal : UseNormal; static const float3 gAmbient = float3(.03f, .03f, .03f); static const float gLightIntensity = 7.f; static const float PI = 3.14159f; static const float gSpecularReflectance = 1.f; static const float gShininess = 25.f; //Input output struct VS_INPUT { float3 Position : POSITION; float2 TexCoord : TEXCOORD; float3 Normal : NORMAL; float3 Tangent : TANGENT; }; struct VS_OUTPUT { float4 Position : SV_POSITION; float4 WorldPosition : WORLDPOSITION; float2 TexCoord : TEXCOORD; float3 Normal : NORMAL; float3 Tangent : TANGENT; }; //---------------------- // Rasterizer state //---------------------- // RasterizerState gRasterizerState // { // CullMode = none; // FrontCounterClockwise = false; //default // }; //Vertex shader VS_OUTPUT VS(VS_INPUT input){ VS_OUTPUT output = (VS_OUTPUT)0; output.Position = mul(float4(input.Position, 1.f), gWorldViewProj); output.WorldPosition = mul(float4(input.Position, 1.f), gWorldMatrix); output.TexCoord = input.TexCoord; output.Normal = mul(input.Normal, (float3x3) gWorldMatrix); output.Tangent = mul(input.Tangent, (float3x3) gWorldMatrix); return output; } float3 Phong(float ks, float exp, float3 l, float3 v, float3 n) { float3 reflected = reflect(l, n); float cosAngle = dot(reflected, v); return ks * pow(max(0.f, cosAngle), exp) * gLightColor; } float3 Lambert(float kd, float3 cd) { return (kd * cd) / PI; } float3 Shade(VS_OUTPUT input) { // Sample diffuse, specular, and gloss maps float3 diffuseSample = gDiffuseMap.Sample(gSampleState, input.TexCoord).rgb; float3 specularSample = gSpecularMap.Sample(gSampleState, input.TexCoord).rgb; float glossSample = gGlossMap.Sample(gSampleState, input.TexCoord).x; float3 normalSample = gNormalMap.Sample(gSampleState, input.TexCoord).rgb; // Compute inversed view and light directions float3 invViewDirection = normalize(gCameraPosition - input.WorldPosition.xyz); float3 invLightDirection = -gLightDirection; // Compute tangent space axes if normal mapping is used float3 normal = input.Normal; if (gUseNormal) { float3 binormal = cross(input.Normal, input.Tangent); float3x3 tangentSpaceAxis = float3x3(input.Tangent, binormal, input.Normal); // Sample and transform normal map normal = float3(2.f * normalSample.x - 1.f, 2.f * normalSample.y - 1.f, 2.f * normalSample.z - 1.f); normal = mul(normal, tangentSpaceAxis); } // Compute Lambert diffuse lighting float3 diffuse = Lambert(gLightIntensity, diffuseSample); // Compute Phong specular lighting float ks = (specularSample.x + specularSample.y + specularSample.z) / 3.f; float3 specular = Phong(ks, glossSample * gShininess, invLightDirection, invViewDirection, normal); // Compute observed area based on the cosine of the light angle float cosAngle = dot(invLightDirection, normal); float3 observedArea = float3(cosAngle, cosAngle, cosAngle); // Combine lighting components float3 color = saturate(diffuse * observedArea + specular + gAmbient * cosAngle); return color; } float4 PS(VS_OUTPUT input) : SV_TARGET{ //Check if gDiffuseMap sample has a valid value, otherwise render red if (gDiffuseMap.Sample(gSampleState, input.TexCoord).a == 0.f) return float4(1.f, 0.f, 0.f, 1.f); return gDiffuseMap.Sample(gSampleState, input.TexCoord); } DepthStencilState gDepthStencilState { //enable DepthEnable = true; DepthWriteMask = ALL; DepthFunc = LESS; //stencil StencilEnable = true; }; technique11 DefaultTechnique{ pass P0 { SetDepthStencilState(gDepthStencilState, 0); SetRasterizerState(gRasterizerState); SetVertexShader( CompileShader( vs_5_0, VS() ) ); SetGeometryShader( NULL ); SetPixelShader( CompileShader( ps_5_0, PS() ) ); } }