/* % HFPS Shader for XNA % Handles Directional light... % and Point Lights, but only 4? keywords: texture dlight plight bumpmapping specular date: 06.01.08 */ /* *** *** *** ** Variables ** *** *** *** */ bool fullbright; float wAlpha=1; float4x4 view : View; float4x4 proj : Projection; float4x4 world : World; float4x4 wRot : Rotation; float4x4 viewInverse : ViewInverse; float3 pLightPos[16]; bool pLightOn[16]; float3 pLightDiffuse[16]; float3 pLightSpecular[16]; float pLightNear[16]; float pLightFar[16]; bool BumpMappingEnabled = true; bool SpecularEnabled = true; float3 decalPos[8]; float decalRadius[8]; bool decalActivated[8]; float3 dLightDir : Direction < string Object = "DirectionalLight"; string Space = "World"; > = { 1, 1, 0 }; float4 dLDiffuseColor : Diffuse = { 0.0f, 0.0f, 0.0f, 1.0f }; float4 dLSpecularColor : Specular = { 0.0f, 0.0f, 0.0f, 1.0f }; float4 ambientColor : Ambient = { 0.1f, 0.1f, 0.1f, 1.0f }; float4 diffuseColor : Diffuse = { 0.5f, 0.5f, 0.5f, 1.0f }; float4 specularColor : Specular = { 1.0f, 1.0f, 1.0f, 1.0f }; float shininess : SpecularPower = 24.0f; /* *** *** *** ** Textures ** *** *** *** */ texture diffuseTexture : Diffuse < string ResourceName = "rgbcheck.bmp"; >; sampler DiffuseTextureSampler = sampler_state { Texture = ; MinFilter = linear; MagFilter = linear; MipFilter = linear; AddressU = mirror; AddressV = mirror; }; texture bumpTexture : Specular < string ResourceName = "TestBM.bmp"; >; sampler BumpTextureSampler = sampler_state { Texture = ; MinFilter = linear; MagFilter = linear; MipFilter = linear; }; /* *** *** *** ** Structures ** *** *** *** */ struct EngineVertexInput { float3 pos : POSITION; float2 texCoord : TEXCOORD0; float3 normal : NORMAL; float3 tangent : TANGENT; }; struct EngineVertexToPixel { float4 pos : POSITION; float2 texCoord : TEXCOORD0; float3 wPos : TEXCOORD1; float3 viewVec : TEXCOORD2; float3x3 tangentMatrix : TEXCOORD3; }; /* *** *** *** ** Functions ** *** *** *** */ // gets the transformation matrix // to chg world space to tangent // space for normal mapping float3x3 ComputeTangentMatrix(float3 tangent, float3 normal) { float3x3 worldToTangentSpace; worldToTangentSpace[0] = mul(cross(normal,tangent),wRot); worldToTangentSpace[1] = mul(tangent,wRot); worldToTangentSpace[2] = mul(normal,wRot); return worldToTangentSpace; } // transforms a position by // the WVP float4 TransformPosition(float3 pos) { return mul(float4(pos,1), mul(mul(world,view),proj)); } // transforms the position by // the world matrix and returns // the result float3 GetWorldPos(float3 pos) { return mul(float4(pos,1),world).xyz; } // gets the location of the cam float3 GetCameraPos() { return viewInverse[3].xyz; } /* *** *** *** ** Shaders ** *** *** *** */ EngineVertexToPixel EngineVertexShader(EngineVertexInput input) { EngineVertexToPixel output = (EngineVertexToPixel)0; output.pos = TransformPosition(input.pos); output.texCoord = input.texCoord; float3x3 worldToTangentSpace = ComputeTangentMatrix(input.tangent, input.normal); float3 worldEyePos = GetCameraPos(); float3 worldVertPos = GetWorldPos(input.pos); output.wPos = worldVertPos;//mul(input.pos,world); output.viewVec = mul(worldToTangentSpace, worldEyePos - worldVertPos); output.tangentMatrix = worldToTangentSpace; return output; } float4 EnginePixelShader(EngineVertexToPixel input) : COLOR { float4 diffuseTex = tex2D(DiffuseTextureSampler,input.texCoord); // The following is decal code that is not yet // implemented as it causes other code to crash // please ignore for the time being /*float3 tanpos = mul(input.wPos,input.tangentMatrix); for(int i=0;i<8;i++) { if(decalActivated[i]) if(sqrt(pow(input.wPos.x-decalPos[i].x,2)+pow(input.wPos.y-decalPos[i].y,2)+pow(input.wPos.z-decalPos[i].z,2))