d3d9.hlsl 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Part of the Irrlicht Engine Shader example.
  2. // These simple Direct3D9 pixel and vertex shaders will be loaded by the shaders
  3. // example. Please note that these example shaders don't do anything really useful.
  4. // They only demonstrate that shaders can be used in Irrlicht.
  5. //-----------------------------------------------------------------------------
  6. // Global variables
  7. //-----------------------------------------------------------------------------
  8. float4x4 mWorldViewProj; // World * View * Projection transformation
  9. float4x4 mInvWorld; // Inverted world matrix
  10. float4x4 mTransWorld; // Transposed world matrix
  11. float3 mLightPos; // Light position (actually just camera-pos in this case)
  12. float4 mLightColor; // Light color
  13. float4 mEmissive; // Emissive material color
  14. // Vertex shader output structure
  15. struct VS_OUTPUT
  16. {
  17. float4 Position : POSITION; // vertex position
  18. float4 Diffuse : COLOR0; // vertex diffuse color
  19. float2 TexCoord : TEXCOORD0; // tex coords
  20. // float3 Tangent : TEXCOORD1; // Not used in this example, but additional values can be passed on as tex coords
  21. // float3 Binormal : TEXCOORD2; // Not used in this example, but additional values can be passed on as tex coords
  22. };
  23. VS_OUTPUT vertexMain( in float4 vPosition : POSITION
  24. , in float3 vNormal : NORMAL
  25. , float2 texCoord : TEXCOORD0
  26. //,float3 Tangent : TEXCOORD1; // Used for Tangent when working with S3DVertexTangents
  27. //,float3 Binormal : TEXCOORD2; // Used for Binormal when working with S3DVertexTangents
  28. )
  29. {
  30. VS_OUTPUT Output;
  31. // transform position to clip space
  32. Output.Position = mul(vPosition, mWorldViewProj);
  33. // transform normal somehow (NOTE: for the real vertex normal you would use an inverse-transpose world matrix instead of mInvWorld)
  34. float3 normal = mul(float4(vNormal,0.0), mInvWorld);
  35. // renormalize normal
  36. normal = normalize(normal);
  37. // position in world coordinates (NOTE: not sure why transposed world is used instead of world?)
  38. float3 worldpos = mul(mTransWorld, vPosition);
  39. // calculate light vector, vtxpos - lightpos
  40. float3 lightVector = worldpos - mLightPos;
  41. // normalize light vector
  42. lightVector = normalize(lightVector);
  43. // calculate light color
  44. float3 tmp = dot(-lightVector, normal);
  45. tmp = lit(tmp.x, tmp.y, 1.0);
  46. tmp = mLightColor * tmp.y;
  47. Output.Diffuse = float4(tmp.x, tmp.y, tmp.z, 0);
  48. Output.TexCoord = texCoord;
  49. return Output;
  50. }
  51. // Pixel shader output structure
  52. struct PS_OUTPUT
  53. {
  54. float4 RGBColor : COLOR0; // Pixel color
  55. };
  56. sampler2D myTexture;
  57. PS_OUTPUT pixelMain(float2 TexCoord : TEXCOORD0,
  58. float4 Position : POSITION,
  59. float4 Diffuse : COLOR0 )
  60. {
  61. PS_OUTPUT Output;
  62. float4 col = tex2D( myTexture, TexCoord ); // sample color map
  63. // multiply with diffuse and do other senseless operations
  64. Output.RGBColor = Diffuse * col;
  65. Output.RGBColor *= 4.0;
  66. Output.RGBColor += mEmissive;
  67. return Output;
  68. }