//-------------------------------------------------------------------------------------- // Global variables //-------------------------------------------------------------------------------------- float3 g_LightDir[4]; // Light's direction in world space float4 g_LightDiffuse[4]; // Light's diffuse color float4 g_LightAmbient; // Light's ambient color float appTime; // App's time in seconds float4x4 worldMatrix; // World matrix for object float4x4 worldViewProjection; // World * View * Projection matrix //-------------------------------------------------------------------------------------- // Vertex shader output structure //-------------------------------------------------------------------------------------- struct VS_OUTPUT { float4 Position : POSITION; // vertex position float4 Diffuse : COLOR0; // vertex diffuse color (note that COLOR0 is clamped from 0..1) }; //-------------------------------------------------------------------------------------- // This shader computes standard transform and lighting //-------------------------------------------------------------------------------------- VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, float3 vNormal : NORMAL ) { VS_OUTPUT Output; float3 localNormal; float3 vNormalWorldSpace; float dotPos=dot(vPos.xyz,vPos.xyz); float4 vAnimatedPos = vPos; vAnimatedPos.xyz=((1.0f-appTime)*dotPos+appTime/sqrt(dotPos))*vPos.xyz; // when appTime is 1, we want the normal to be vAnimatedPos // when appTime is 0 we want the normal to be 2 normal-vAnimatedPos localNormal = normalize(appTime*vAnimatedPos.xyz + (1.0f-appTime)*(2.0f*vNormal-vAnimatedPos.xyz)); // Transform the position from object space to homogeneous projection space Output.Position = mul(vAnimatedPos, worldViewProjection); // Transform the normal from object space to world space vNormalWorldSpace = normalize(mul(localNormal, (float3x3)worldMatrix)); // normal (world space) // Compute simple directional lighting equation float3 vTotalLightDiffuse = float3(0,0,0); for(int i=0; i<4; i++ ) vTotalLightDiffuse += g_LightDiffuse[i] * max(0,dot(vNormalWorldSpace, g_LightDir[i])); Output.Diffuse.rgb = vTotalLightDiffuse + g_LightAmbient; Output.Diffuse.a = 1.0f; return Output; } //-------------------------------------------------------------------------------------- // Pixel shader output structure //-------------------------------------------------------------------------------------- struct PS_OUTPUT { float4 RGBColor : COLOR0; // Pixel color }; //-------------------------------------------------------------------------------------- // This shader outputs the pixel's color by modulating the texture's // color with diffuse material color //-------------------------------------------------------------------------------------- PS_OUTPUT RenderScenePS( VS_OUTPUT In ) { PS_OUTPUT Output; Output.RGBColor = In.Diffuse; return Output; } Technique Simple { pass P0 { VertexShader = compile vs_1_1 RenderSceneVS( ); PixelShader = compile ps_1_1 RenderScenePS( ); // trivial pixel shader (could use FF instead if desired) } }