ship.fx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //-----------------------------------------------------------------------------
  2. // Ship.fx
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. //Input variables
  8. float4x4 world;
  9. float4x4 inverseWorld;
  10. float4x4 worldViewProjection;
  11. float4 viewPosition;
  12. float4 Ambient;
  13. float4 DirectionalDirection;
  14. float4 DirectionalColor;
  15. float4 PointPosition;
  16. float4 PointColor;
  17. float PointFactor;
  18. float4 Material;
  19. texture SkinTexture;
  20. sampler Skin = sampler_state
  21. {
  22. Texture = (SkinTexture);
  23. ADDRESSU = CLAMP;
  24. ADDRESSV = CLAMP;
  25. MAGFILTER = LINEAR;
  26. MINFILTER = LINEAR;
  27. MIPFILTER = LINEAR;
  28. };
  29. texture NormalMapTexture;
  30. sampler NormalMap = sampler_state
  31. {
  32. Texture = (NormalMapTexture);
  33. ADDRESSU = CLAMP;
  34. ADDRESSV = CLAMP;
  35. MAGFILTER = LINEAR;
  36. MINFILTER = LINEAR;
  37. MIPFILTER = LINEAR;
  38. };
  39. texture ReflectionTexture;
  40. sampler Reflection = sampler_state
  41. {
  42. Texture = (ReflectionTexture);
  43. ADDRESSU = CLAMP;
  44. ADDRESSV = CLAMP;
  45. MAGFILTER = LINEAR;
  46. MINFILTER = LINEAR;
  47. MIPFILTER = LINEAR;
  48. };
  49. struct VS_INPUT
  50. {
  51. float4 ObjectPos: POSITION;
  52. float2 Tex : TEXCOORD0;
  53. float3 ObjectNormal : NORMAL;
  54. };
  55. struct VS_OUTPUT
  56. {
  57. float4 ScreenPos: POSITION;
  58. float2 Tex: TEXCOORD0;
  59. float3 WorldNormal: TEXCOORD1;
  60. float4 PointDirection: COLOR;
  61. };
  62. float4 DirectionalLight(float3 WorldNormal)
  63. {
  64. return DirectionalColor * saturate(dot(WorldNormal, normalize(DirectionalDirection)));
  65. }
  66. float4 PointLight(float3 Normal, float4 PointDirection)
  67. {
  68. return PointColor * saturate(PointDirection.w * dot(Normal, normalize(PointDirection.xyz)));
  69. }
  70. VS_OUTPUT ShipVS(VS_INPUT In)
  71. {
  72. VS_OUTPUT Out;
  73. //Move to screen space
  74. Out.ScreenPos = mul(In.ObjectPos, worldViewProjection);
  75. float4 WorldPos = mul(In.ObjectPos, world);
  76. float4 WorldNormal = normalize(mul(In.ObjectNormal, world)); // First optimization, lets not normalize per pixel, eh?
  77. //Pass on texture coordinates and normal
  78. Out.Tex = In.Tex;
  79. Out.WorldNormal = WorldNormal;
  80. //Direction of point light to this vertex
  81. float4 lightDir;
  82. lightDir= PointPosition - WorldPos;
  83. float dist = length(lightDir);
  84. lightDir = lightDir/dist;
  85. //Store attenuation in w
  86. lightDir.w = saturate(1/(PointFactor * dist));
  87. float4 Directional = DirectionalLight(WorldNormal);
  88. float4 Point = PointLight(WorldNormal, lightDir);
  89. Out.PointDirection = Directional + Point + Ambient;
  90. return Out;
  91. }
  92. float4 ShipPS( float2 tex: TEXCOORD0, float3 WorldNormal : TEXCOORD1, float4 LightColor : COLOR) : COLOR
  93. {
  94. //Specular map is in alpha of diffusemap and material
  95. float MattFactor = saturate(Material.a * tex2D(Skin, tex).a);
  96. //Add it all up
  97. float4 retval;
  98. retval= saturate( Material *
  99. (((LightColor) * float4(tex2D(Skin, tex).rgb, 1)) +
  100. (1-MattFactor) * texCUBE(Reflection, WorldNormal)));
  101. return retval;
  102. }
  103. //--------------------------------------------------------------//
  104. // Technique Section for Ship Effects
  105. //--------------------------------------------------------------//
  106. technique Ship
  107. {
  108. pass Single_Pass
  109. {
  110. ZENABLE = TRUE;
  111. ZWRITEENABLE = TRUE;
  112. ALPHABLENDENABLE = FALSE;
  113. CULLMODE = CCW;
  114. VertexShader = compile vs_1_1 ShipVS();
  115. PixelShader = compile ps_2_0 ShipPS();
  116. }
  117. }