////////////////////////////////////////////////////////////////////////////// // Parameters and Structs all Techniques ////////////////////////////////////////////////////////////////////////////// float4x4 World; float4x4 View; float4x4 Projection; struct Light { float4 color; float4 position; float falloff; float range; }; Light light; shared float3 cameraPosition; float4 AmbientColor = float4(.1f, .1f, .1f, 1); float specularPower = 2.0f; float4 specularColor = float4(1,1,1,1); float specularIntensity =.35f; float textureUReps = 1.0; float textureVReps = 1.0; texture Texture; sampler TextureSampler = sampler_state { Texture = (Texture); MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = Wrap; AddressV = Wrap; }; ///////////////////////////////////////////////////////////////////// // Full Lighting Shader //////////////////////////////////////////////////////////////////// // Vertex Lighting structs and functions below are applicapible for // full and simple lighting struct fullAndSimpleVertexLighting_input { float4 Position : POSITION0; float3 Normal : NORMAL0; float2 TexCoord : TEXCOORD0; }; struct fullAndSimpleVertexLighting_out { float4 Position : POSITION0; float3 Normal : TEXCOORD0; float2 TexCoord : TEXCOORD1; float4 WorldPos : TEXCOORD2; }; // Vertex Shader fullAndSimpleVertexLighting_out fullAndSimpleVertexLighting(fullAndSimpleVertexLighting_input input) { fullAndSimpleVertexLighting_out output; float4x4 WorldViewProj = mul(mul(World, View), Projection); // Transform the models verticies and normal output.Position = mul(input.Position, WorldViewProj); output.Normal = normalize(mul(input.Normal, World)); output.TexCoord = input.TexCoord; // Save the vertices postion in world space output.WorldPos = mul(input.Position, World); return output; } // Calculates lighting for one light, this method is used so that it is possible to use // multiple lights float4 calculateLight(Light light, float3 worldPosition, float3 worldNormal, float4 diffuseColor, float4 specularColor ) { //Ir = ArKaDr + AttLr [KdDr(N dot L) + KsSr(R dot V)^n] //Ig = AgKaDg + AttLg [KdDg(N dot L) + KsSg(R dot V)^n] //Ib = AbKaDb + AttLb [KdDb(N dot L) + KsSb(R dot V)^n] float3 lightVector = light.position - worldPosition; float lightDist = length(lightVector); float3 directionToLight = normalize(lightVector); //calculate the intensity of the light with exponential falloff float baseIntensity = pow(saturate((light.range - lightDist) / light.range), light.falloff); float diffuseIntensity = saturate( dot(directionToLight, worldNormal)); float4 diffuse = diffuseIntensity * light.color * diffuseColor; //calculate Phong components per-pixel float3 reflectionVector = normalize(reflect(-directionToLight, worldNormal)); float3 directionToCamera = normalize(cameraPosition - worldPosition); //calculate specular component float4 specular = saturate(light.color * specularColor * specularIntensity * pow(saturate(dot(reflectionVector, directionToCamera)), specularPower)); return baseIntensity * (diffuse + specular); } float4 fullPerPixelLighting(fullAndSimpleVertexLighting_out input) : COLOR { // Color of the model float4 diffuseColor = tex2D(TextureSampler, input.TexCoord); // Intensity based on the direction of the light float diffuseIntensity = saturate(dot(light.position, input.Normal)); // Final diffuse color with ambient color added float4 diffuse = 1 * diffuseColor + AmbientColor; diffuse += calculateLight(light, input.WorldPos, input.Normal, diffuseColor, specularColor); return diffuse; } // Draw model with all light info technique PerPixelLighting { pass Pass1 { //set sampler states MagFilter[0] = LINEAR; MinFilter[0] = LINEAR; MipFilter[0] = LINEAR; AddressU[0] = CLAMP; AddressV[0] = CLAMP; MagFilter[1] = LINEAR; MinFilter[1] = LINEAR; MipFilter[1] = LINEAR; AddressU[1] = CLAMP; AddressV[1] = CLAMP; //set render states AlphaBlendEnable = FALSE; VertexShader = compile vs_2_0 fullAndSimpleVertexLighting(); PixelShader = compile ps_2_0 fullPerPixelLighting(); } } ///////////////////////////////////////////////////////////////////////// // Simple Shading //////////////////////////////////////////////////////////////////////// // Ambient and Diffuse Only float4 simplePerPixelLighting(fullAndSimpleVertexLighting_out input) : COLOR { // Color of the model float4 diffuseColor = tex2D(TextureSampler, input.TexCoord); // Intensity based on the direction of the light float diffuseIntensity = saturate(dot(light.position, input.Normal)); // Final diffuse color with ambient color added float4 diffuse = 1 * diffuseColor + AmbientColor; return diffuse; } // Technique for drawing the model shaded with only ambient Lighting technique SimpleLighting { pass Pass1 { //set sampler states MagFilter[0] = LINEAR; MinFilter[0] = LINEAR; MipFilter[0] = LINEAR; AddressU[0] = CLAMP; AddressV[0] = CLAMP; MagFilter[1] = LINEAR; MinFilter[1] = LINEAR; MipFilter[1] = LINEAR; AddressU[1] = CLAMP; AddressV[1] = CLAMP; //set render states AlphaBlendEnable = FALSE; VertexShader = compile vs_2_0 fullAndSimpleVertexLighting(); PixelShader = compile ps_2_0 simplePerPixelLighting(); } } //////////////////////////////////////////////////////////////////// // Bump &&^^^^^ //////////////////////////////////////////////////////////////////// texture BumpMap; sampler BumpMapSampler = sampler_state { Texture = ; }; struct bumpMapAndLighing_input { float4 Position : POSITION; float2 TexCoord : TEXCOORD0; float3 Normal : NORMAL; float3 Tangent : TANGENT; }; struct bumpMapAndLighing_output { float4 Position : POSITION; float2 TexCoord : TEXCOORD0; float4 WorldPos : TEXCOORD1; }; bumpMapAndLighing_output vsBump(bumpMapAndLighing_input input) { bumpMapAndLighing_output output; output.Position = mul(input.Position,mul(mul(World, View), Projection)); float3x3 worldToTangentSpace; worldToTangentSpace[0] = mul(input.Tangent,World); worldToTangentSpace[1] = mul(cross(input.Tangent,input.Normal),World); worldToTangentSpace[2] = mul(input.Normal,World); output.TexCoord = input.TexCoord; // Save the vertices postion in world space output.WorldPos = mul(input.Position, World); return output; } float4 psBump(bumpMapAndLighing_output input) : COLOR { float3 Normal = (2 * (tex2D(BumpMapSampler,input.TexCoord))) - 1.0; // Color of the model float4 diffuseColor = tex2D(TextureSampler, input.TexCoord); // Intensity based on the direction of the light float diffuseIntensity = saturate(dot(light.position, normalize(Normal))); // Final diffuse color with ambient color added float4 diffuse = 1 * diffuseColor + AmbientColor; diffuse += calculateLight(light, input.WorldPos, normalize(Normal), diffuseColor, specularColor); return diffuse; } technique Bump { pass Pass1 { //set sampler states MagFilter[0] = LINEAR; MinFilter[0] = LINEAR; MipFilter[0] = LINEAR; AddressU[0] = CLAMP; AddressV[0] = CLAMP; MagFilter[1] = LINEAR; MinFilter[1] = LINEAR; MipFilter[1] = LINEAR; AddressU[1] = CLAMP; AddressV[1] = CLAMP; //set render states AlphaBlendEnable = TRUE; VertexShader = compile vs_2_0 vsBump(); PixelShader = compile ps_2_0 psBump(); } } //////////////////////////////////////////////////////////////////// // SemiTransparent //////////////////////////////////////////////////////////////////// texture atmosphereTexture; sampler atmosphereTextureSampler = sampler_state { Texture = ; }; fullAndSimpleVertexLighting_out vsTransparency(fullAndSimpleVertexLighting_input input, uniform float size) { fullAndSimpleVertexLighting_out output; float4x4 WorldViewProj = mul(mul(World, View), Projection); // Transform the models verticies and normal output.Position = mul(input.Position, WorldViewProj) + (mul(size, mul(input.Normal, WorldViewProj))); output.Normal = normalize(mul(input.Normal, World)); output.TexCoord = input.TexCoord; // Save the vertices postion in world space output.WorldPos = mul(input.Position, World); return output; } float4 psTransparency(fullAndSimpleVertexLighting_out input, uniform float transparency) : COLOR { // Color of the model float4 atmosphereColor = tex2D(atmosphereTextureSampler,input.TexCoord); // Intensity based on the direction of the light float diffuseIntensity = saturate(dot(light.position, normalize(input.Normal))); // Final diffuse color with ambient color added float4 diffuse = 1 * atmosphereColor + AmbientColor; diffuse *= calculateLight(light,input.WorldPos, normalize(input.Normal),atmosphereColor, specularColor); diffuse.a = transparency; return diffuse; } //////////////////////////////////////////////////////////////////// // Cell Shading //////////////////////////////////////////////////////////////////// float4 psCell(fullAndSimpleVertexLighting_out input) : COLOR { // Calculate as normal // Color of the model float4 diffuseColor = tex2D(TextureSampler, input.TexCoord); // Intensity based on the direction of the light float diffuseIntensity = saturate(dot(light.position, input.Normal)); // Final diffuse color with ambient color added float4 diffuse = 1 * diffuseColor + AmbientColor; diffuse += calculateLight(light, input.WorldPos, input.Normal, diffuseColor, specularColor); //But now just round it off the nearest .2 for each color if (diffuse.x >= .8f) diffuse.x = 1.0f; else if (diffuse.x >=.7f) diffuse.x = .8f; else if (diffuse.x >=.4f) diffuse.x = .5f; else if (diffuse.x >=.2f) diffuse.x = .3f; else diffuse.x = 0.0f; if (diffuse.y >= .8f) diffuse.y = 1.0f; else if (diffuse.y >=.7f) diffuse.y = .8f; else if (diffuse.y >=.4f) diffuse.y = .5f; else if (diffuse.y >=.2f) diffuse.y = .3f; else diffuse.y = 0.0f; if (diffuse.z >= .8f) diffuse.z = 1.0f; else if (diffuse.z >=.7f) diffuse.z = .8f; else if (diffuse.z >=.4f) diffuse.z = .5f; else if (diffuse.z >=.2f) diffuse.z = .3f; else diffuse.z = 0.0f; return diffuse; } // Draw model with all light info technique CellShaded { pass DefaultLighting { //set sampler states MagFilter[0] = LINEAR; MinFilter[0] = LINEAR; MipFilter[0] = LINEAR; AddressU[0] = CLAMP; AddressV[0] = CLAMP; MagFilter[1] = LINEAR; MinFilter[1] = LINEAR; MipFilter[1] = LINEAR; AddressU[1] = CLAMP; AddressV[1] = CLAMP; //set render states AlphaBlendEnable = False; CullMode = CCW; VertexShader = compile vs_2_0 fullAndSimpleVertexLighting(); PixelShader = compile ps_2_0 fullPerPixelLighting(); } pass CellShading { AlphaBlendEnable = True; SrcBlend = SrcAlpha; DestBlend = One; PixelShader = compile ps_2_0 psCell(); } pass BumpMapping { VertexShader = compile vs_2_0 vsBump(); PixelShader = compile ps_2_0 psBump(); } pass Clouds1 { VertexShader = compile vs_2_0 vsTransparency(2.0f); PixelShader = compile ps_2_0 psTransparency(.4f); } pass Clouds2 { VertexShader = compile vs_2_0 vsTransparency(6.0f); PixelShader = compile ps_2_0 psTransparency(.6f); } } //////////////////////////////////////////////////////////////////// // Reflection :-D! //////////////////////////////////////////////////////////////////// texture cubeMap; samplerCUBE environmentMap : TEXUNIT3 = sampler_state { texture = ; magFilter = LINEAR; minFilter = LINEAR; addressU = mirror; addressV = mirror; }; struct vertexReflection_input { float4 Position : POSITION; float3 Normal : NORMAL; }; struct vertexReflection_output { float4 Position : POSITION; float3 Reflect: TEXCOORD1; }; float4 CubeMapLookup(float3 CubeTexcoord) { return texCUBE(environmentMap, CubeTexcoord); } vertexReflection_output ReflectionVS(vertexReflection_input input) { vertexReflection_output output; output.Position = mul(input.Position,mul(mul(World, View), Projection)); float3 Normal = mul(normalize(input.Normal), World); float3 PosWorldr = (mul(input.Position, World)); float3 ViewDir = normalize(PosWorldr - cameraPosition); output.Reflect = reflect(ViewDir, Normal); return output; } float4 perPixelReflection(vertexReflection_output input) : COLOR { float4 color = CubeMapLookup(input.Reflect)*float4(1.0f,1.0f,1.0f,.7f); return color; } // Technique for drawing a model with the Chromatic Dispersion Effect technique Reflection { pass Pass1 { //set sampler states MagFilter[0] = LINEAR; MinFilter[0] = LINEAR; MipFilter[0] = LINEAR; AddressU[0] = CLAMP; AddressV[0] = CLAMP; MagFilter[1] = LINEAR; MinFilter[1] = LINEAR; MipFilter[1] = LINEAR; AddressU[1] = CLAMP; AddressV[1] = CLAMP; Sampler[0]= (environmentMap); //set render states AlphaBlendEnable = FALSE; VertexShader = compile vs_2_0 ReflectionVS(); PixelShader = compile ps_2_0 perPixelReflection(); } }