/* * Sprite.fx * Authors: August Zinsser * * Copyright August Zinsser 2007 * This program is distributed under the terms of the GNU General Public License * * Renders sprites. Useful for particle systems. */ // --------------------------------------------------------------------------- // Parameters // --------------------------------------------------------------------------- texture xTexture; float4x4 xViewMat; float4x4 xProjMat; sampler TextureSampler = sampler_state { texture = ; magfilter = LINEAR; minfilter = LINEAR; mipfilter = LINEAR; AddressU = CLAMP; AddressV = CLAMP; }; // --------------------------------------------------------------------------- // Structs // --------------------------------------------------------------------------- struct a2v { float4 position : POSITION0; float rotation : NORMAL; float2 size : PSIZE; float2 uv : TEXCOORD0; float4 color : COLOR0; }; struct v2p { float4 position : POSITION0; float2 uv : TEXCOORD0; float4 color : COLOR0; }; // --------------------------------------------------------------------------- // Vertex Shaders // --------------------------------------------------------------------------- v2p SpriteVS(a2v In) { v2p Out; // Get the basis from the camera to this point //float3 viewForward = xViewMat._m02_m12_m22; float3 viewUp = xViewMat._m01_m11_m21; float3 viewRight = xViewMat._m00_m10_m20; // Rotate the offset vectors float3 newRight = viewRight * cos(In.rotation) - viewUp * sin(In.rotation); float3 newUp = viewRight * sin(In.rotation) + viewUp * cos(In.rotation); // Adjust the position of this point to make 2 camera-facing tris float3 position = In.position; position += newRight * In.size.x; position += newUp * In.size.y; // Transform position from object space into clip space float4 viewPos = mul(float4(position,1), xViewMat); Out.position = mul(viewPos, xProjMat); // Pass through the uvs Out.uv = In.uv; // Pass through the color Out.color = In.color; return Out; } // --------------------------------------------------------------------------- // Pixel Shaders // --------------------------------------------------------------------------- float4 SpritePS(v2p In) : COLOR0 { float4 retCol = tex2D(TextureSampler, In.uv) * In.color; return retCol; } // --------------------------------------------------------------------------- // Techniques // --------------------------------------------------------------------------- technique Billboard { pass P0 { VertexShader = compile vs_1_1 SpriteVS(); PixelShader = compile ps_1_1 SpritePS(); } }