imposter.hlsl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "torque.hlsl"
  23. static float sCornerRight[4] = { -1, 1, 1, -1 };
  24. static float sCornerUp[4] = { -1, -1, 1, 1 };
  25. static float2 sUVCornerExtent[4] =
  26. {
  27. float2( 0, 1 ),
  28. float2( 1, 1 ),
  29. float2( 1, 0 ),
  30. float2( 0, 0 )
  31. };
  32. #define IMPOSTER_MAX_UVS 64
  33. void imposter_v(
  34. // These parameters usually come from the vertex.
  35. float3 center,
  36. int corner,
  37. float halfSize,
  38. float3 imposterUp,
  39. float3 imposterRight,
  40. // These are from the imposter shader constant.
  41. int numEquatorSteps,
  42. int numPolarSteps,
  43. float polarAngle,
  44. bool includePoles,
  45. // Other shader constants.
  46. float3 camPos,
  47. float4 uvs[IMPOSTER_MAX_UVS],
  48. // The outputs of this function.
  49. out float3 outWsPosition,
  50. out float2 outTexCoord,
  51. out float3x3 outWorldToTangent
  52. )
  53. {
  54. // TODO: This could all be calculated on the CPU.
  55. float equatorStepSize = M_2PI_F / numEquatorSteps;
  56. float equatorHalfStep = ( equatorStepSize / 2.0 ) - 0.0001;
  57. float polarStepSize = M_PI_F / numPolarSteps;
  58. float polarHalfStep = ( polarStepSize / 2.0 ) - 0.0001;
  59. // The vector between the camera and the billboard.
  60. float3 lookVec = normalize( camPos - center );
  61. // Generate the camera up and right vectors from
  62. // the object transform and camera forward.
  63. float3 camUp = imposterUp;
  64. float3 camRight = normalize( cross( -lookVec, camUp ) );
  65. // The billboarding is based on the camera directions.
  66. float3 rightVec = camRight * sCornerRight[corner];
  67. float3 upVec = camUp * sCornerUp[corner];
  68. float lookPitch = acos( dot( imposterUp, lookVec ) );
  69. // First check to see if we need to render the top billboard.
  70. int index;
  71. /*
  72. if ( includePoles && ( lookPitch < polarAngle || lookPitch > sPi - polarAngle ) )
  73. {
  74. index = numEquatorSteps * 3;
  75. // When we render the top/bottom billboard we always use
  76. // a fixed vector that matches the rotation of the object.
  77. rightVec = float3( 1, 0, 0 ) * sCornerRight[corner];
  78. upVec = float3( 0, 1, 0 ) * sCornerUp[corner];
  79. if ( lookPitch > sPi - polarAngle )
  80. {
  81. upVec = -upVec;
  82. index++;
  83. }
  84. }
  85. else
  86. */
  87. {
  88. // Calculate the rotation around the z axis then add the
  89. // equator half step. This gets the images to switch a
  90. // half step before the captured angle is met.
  91. float lookAzimuth = atan2( lookVec.y, lookVec.x );
  92. float azimuth = atan2( imposterRight.y, imposterRight.x );
  93. float rotZ = ( lookAzimuth - azimuth ) + equatorHalfStep;
  94. // The y rotation is calculated from the look vector and
  95. // the object up vector.
  96. float rotY = lookPitch - polarHalfStep;
  97. // TODO: How can we do this without conditionals?
  98. // Normalize the result to 0 to 2PI.
  99. if ( rotZ < 0 )
  100. rotZ += M_2PI_F;
  101. if ( rotZ > M_2PI_F )
  102. rotZ -= M_2PI_F;
  103. if ( rotY < 0 )
  104. rotY += M_2PI_F;
  105. if ( rotY > M_PI_F ) // Not M_2PI_F?
  106. rotY -= M_2PI_F;
  107. float polarIdx = round( abs( rotY ) / polarStepSize );
  108. // Get the index to the start of the right polar
  109. // images for this viewing angle.
  110. int numPolarOffset = numEquatorSteps * polarIdx;
  111. // Calculate the final image index for lookup
  112. // of the texture coords.
  113. index = ( rotZ / equatorStepSize ) + numPolarOffset;
  114. }
  115. // Generate the final world space position.
  116. outWsPosition = center + ( upVec * halfSize ) + ( rightVec * halfSize );
  117. // Grab the uv set and setup the texture coord.
  118. float4 uvSet = uvs[index];
  119. outTexCoord.x = uvSet.x + ( uvSet.z * sUVCornerExtent[corner].x );
  120. outTexCoord.y = uvSet.y + ( uvSet.w * sUVCornerExtent[corner].y );
  121. // Needed for normal mapping and lighting.
  122. outWorldToTangent[0] = float3( 1, 0, 0 );
  123. outWorldToTangent[1] = float3( 0, 1, 0 );
  124. outWorldToTangent[2] = float3( 0, 0, -1 );
  125. }