ShatterEffect.fx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //-----------------------------------------------------------------------------
  2. // ShatterEffect.fx
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. #if OPENGL
  8. #define SV_POSITION POSITION
  9. #define VS_SHADERMODEL vs_3_0
  10. #define PS_SHADERMODEL ps_3_0
  11. #else
  12. #define VS_SHADERMODEL vs_4_0_level_9_1
  13. #define PS_SHADERMODEL ps_4_0_level_9_1
  14. #endif
  15. float4x4 WorldViewProjection;
  16. float4x4 World : World;
  17. float RotationAmount;
  18. float TranslationAmount;
  19. texture modelTexture;
  20. float time;
  21. // Lighting Data
  22. float3 eyePosition;
  23. float3 lightPosition;
  24. float4 ambientColor;
  25. float4 diffuseColor;
  26. float4 specularColor;
  27. float specularPower;
  28. sampler TextureSampler = sampler_state
  29. {
  30. Texture = <modelTexture>;
  31. MinFilter = Anisotropic;
  32. MagFilter = LINEAR;
  33. MipFilter = LINEAR;
  34. MaxAnisotropy = 8;
  35. AddressU = WRAP;
  36. AddressV = WRAP;
  37. };
  38. struct VertexShaderOutput
  39. {
  40. float4 Position : SV_POSITION;
  41. float3 WorldNormal : TEXCOORD0;
  42. float3 WorldPosition : TEXCOORD1;
  43. float2 texCoords : TEXCOORD2;
  44. float4 Color : COLOR0;
  45. };
  46. struct PixelShaderInput
  47. {
  48. float3 WorldNormal : TEXCOORD0;
  49. float3 WorldPosition : TEXCOORD1;
  50. float2 TexCoords : TEXCOORD2;
  51. float4 Color: COLOR0;
  52. };
  53. struct InputVS
  54. {
  55. float3 Position : SV_POSITION;
  56. float3 Normal : NORMAL;
  57. float2 TexCoords : TEXCOORD0;
  58. float3 TriangleCenter : TEXCOORD1;
  59. float3 RotationalVelocity : TEXCOORD2;
  60. };
  61. // Helper function to create a YawPitchRoll Matrix.
  62. // Used to rotate the verticies by the random rotational values generated in the
  63. // processor.
  64. float4x4 CreateYawPitchRollMatrix(float x, float y, float z)
  65. {
  66. float4x4 result;
  67. result[0][0] = cos(z)*cos(y) + sin(z)*sin(x)*sin(y);
  68. result[0][1] = -sin(z)*cos(y) + cos(z)*sin(x)*sin(y);
  69. result[0][2] = cos(x)*sin(y);
  70. result[0][3] = 0;
  71. result[1][0] = sin(z)*cos(x);
  72. result[1][1] = cos(z)*cos(x);
  73. result[1][2] = -sin(x);
  74. result[1][3] = 0;
  75. result[2][0] = cos(z)*-sin(y) + sin(z)*sin(x)*cos(y);
  76. result[2][1] = sin(z)*sin(y) + cos(z)*sin(x)*cos(y);
  77. result[2][2] = cos(x)*cos(y);
  78. result[2][3] = 0;
  79. result[3][0] = 0;
  80. result[3][1] = 0;
  81. result[3][2] = 0;
  82. result[3][3] = 1;
  83. return result;
  84. }
  85. VertexShaderOutput ShatterVS(InputVS input)
  86. {
  87. VertexShaderOutput output = (VertexShaderOutput)0;
  88. // Shattering Calculations
  89. //
  90. // The shatter effect is pretty simple. First we create a YawPitchRoll Matrix based
  91. // on the random values we generated in the processor.
  92. // Second, we transform the vertex position by rotating it around it's center using
  93. // the rotation Matrix. Third, we translate the vertex along it's normal to have it
  94. // move outwards. Last, we drop the vertex along it's Y axis as a function of
  95. // time^2 to give a falling effect.
  96. // Create a rotation matrix
  97. input.RotationalVelocity *= RotationAmount;
  98. float4x4 rotMatrix = CreateYawPitchRollMatrix(input.RotationalVelocity.x,
  99. input.RotationalVelocity.y,
  100. input.RotationalVelocity.z);
  101. // Build 3x3 rotation matrix explicitly (avoid cast issues on some translators)
  102. float3x3 rot3 = float3x3(
  103. rotMatrix[0][0], rotMatrix[0][1], rotMatrix[0][2],
  104. rotMatrix[1][0], rotMatrix[1][1], rotMatrix[1][2],
  105. rotMatrix[2][0], rotMatrix[2][1], rotMatrix[2][2]);
  106. // Rotate the vertex around its triangle's center using 3x3 matrix math
  107. float3 position = input.TriangleCenter + mul((input.Position - input.TriangleCenter), rot3);
  108. // Displace the vertex along its normal
  109. position += input.Normal * TranslationAmount;
  110. // Move the vertex downward as a function of time^2 to give a nice curvy falling
  111. // effect.
  112. position.y -= time*time * 200;
  113. // Proceed as usual
  114. output.Position = mul(float4(position,1.0),WorldViewProjection);
  115. // We must rotate the Normal as well for accurate lighting calculations.
  116. // Transform normal using 3x3 rotation then world 3x3; normalize result
  117. float3x3 world3 = float3x3(
  118. World[0][0], World[0][1], World[0][2],
  119. World[1][0], World[1][1], World[1][2],
  120. World[2][0], World[2][1], World[2][2]);
  121. output.WorldNormal = normalize(mul(mul(input.Normal, rot3), world3));
  122. float4 worldPosition = mul(float4(position,1.0),World);
  123. // Divide perspective-correct and keep as float3
  124. output.WorldPosition = worldPosition.xyz / worldPosition.w;
  125. output.texCoords = input.TexCoords;
  126. //calculate diffuse component
  127. float3 directionToLight = normalize(lightPosition - output.WorldPosition);
  128. float diffuseIntensity = saturate( dot(directionToLight, output.WorldNormal));
  129. float4 diffuse = diffuseColor * diffuseIntensity;
  130. output.Color = diffuse + ambientColor;
  131. return output;
  132. }
  133. float4 PhongPS(PixelShaderInput input) : COLOR
  134. {
  135. float3 directionToLight = normalize(lightPosition - input.WorldPosition);
  136. float3 reflectionVector = normalize(reflect(-directionToLight, input.WorldNormal));
  137. float3 directionToCamera = normalize(eyePosition - input.WorldPosition);
  138. //calculate specular component
  139. float4 specular = specularColor *
  140. pow( saturate(dot(reflectionVector, directionToCamera)),
  141. specularPower);
  142. float4 TextureColor = tex2D(TextureSampler, input.TexCoords);
  143. float4 color = TextureColor * input.Color + specular;
  144. color.a = 1.0;
  145. return color;
  146. }
  147. technique ShatterEffect
  148. {
  149. pass Pass0
  150. {
  151. VertexShader = compile VS_SHADERMODEL ShatterVS();
  152. PixelShader = compile PS_SHADERMODEL PhongPS();
  153. }
  154. }