ShadowVolumeAppearanceVS.glsl 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. attribute vec3 position3DHigh;
  2. attribute vec3 position3DLow;
  3. attribute float batchId;
  4. #ifdef EXTRUDED_GEOMETRY
  5. attribute vec3 extrudeDirection;
  6. uniform float u_globeMinimumAltitude;
  7. #endif // EXTRUDED_GEOMETRY
  8. #ifdef PER_INSTANCE_COLOR
  9. varying vec4 v_color;
  10. #endif // PER_INSTANCE_COLOR
  11. #ifdef TEXTURE_COORDINATES
  12. #ifdef SPHERICAL
  13. varying vec4 v_sphericalExtents;
  14. #else // SPHERICAL
  15. varying vec2 v_inversePlaneExtents;
  16. varying vec4 v_westPlane;
  17. varying vec4 v_southPlane;
  18. #endif // SPHERICAL
  19. varying vec3 v_uvMinAndSphericalLongitudeRotation;
  20. varying vec3 v_uMaxAndInverseDistance;
  21. varying vec3 v_vMaxAndInverseDistance;
  22. #endif // TEXTURE_COORDINATES
  23. #if defined(TEXTURE_COORDINATES) && !defined(SPHERICAL) && defined(UINT8_PACKING)
  24. vec4 clampAndMagnitude(vec4 sd)
  25. {
  26. vec4 d = sd;
  27. d.x = czm_branchFreeTernary(sd.x < 128.0, d.x, (255.0 - sd.x));
  28. d.x = floor(0.5 + d.x);
  29. d.y = floor(0.5 + d.y);
  30. d.z = floor(0.5 + d.z);
  31. d.w = floor(0.5 + d.w);
  32. return d;
  33. }
  34. float unpackLowLessThan100k(vec4 sd)
  35. {
  36. vec4 d = clampAndMagnitude(sd);
  37. return (1000.0 * d.x + 10.0 * d.y + 0.1 * d.z + 0.001 * d.w) * czm_branchFreeTernary(sd.x < 128.0, 1.0, -1.0);
  38. }
  39. vec3 southwest_LOW(vec4 x, vec4 y, vec4 z)
  40. {
  41. vec3 value;
  42. value.x = unpackLowLessThan100k(x);
  43. value.y = unpackLowLessThan100k(y);
  44. value.z = unpackLowLessThan100k(z);
  45. return value;
  46. }
  47. float unpackHighMagLessThan100Million(vec4 sd)
  48. {
  49. vec4 d = clampAndMagnitude(sd);
  50. return (1000000.0 * d.x + 10000.0 * d.y + 100.0 * d.z + d.w) * czm_branchFreeTernary(sd.x < 128.0, 1.0, -1.0);
  51. }
  52. vec3 southwest_HIGH(vec4 x, vec4 y, vec4 z)
  53. {
  54. vec3 value;
  55. value.x = unpackHighMagLessThan100Million(x);
  56. value.y = unpackHighMagLessThan100Million(y);
  57. value.z = unpackHighMagLessThan100Million(z);
  58. return value;
  59. }
  60. #ifdef COLUMBUS_VIEW_2D
  61. vec4 unpackPlanes2D_HIGH(vec4 x, vec4 y, vec4 z, vec4 w)
  62. {
  63. vec4 value;
  64. value.x = unpackHighMagLessThan100Million(x);
  65. value.y = unpackHighMagLessThan100Million(y);
  66. value.z = unpackHighMagLessThan100Million(z);
  67. value.w = unpackHighMagLessThan100Million(w);
  68. return value;
  69. }
  70. vec4 unpackPlanes2D_LOW(vec4 x, vec4 y, vec4 z, vec4 w)
  71. {
  72. vec4 value;
  73. value.x = unpackLowLessThan100k(x);
  74. value.y = unpackLowLessThan100k(y);
  75. value.z = unpackLowLessThan100k(z);
  76. value.w = unpackLowLessThan100k(w);
  77. return value;
  78. }
  79. #else
  80. float unpackLowLessThan1000k(vec4 sd)
  81. {
  82. vec4 d = clampAndMagnitude(sd);
  83. return (10000.0 * d.x + 100.0 * d.y + d.z + 0.01 * d.w) * czm_branchFreeTernary(sd.x < 128.0, 1.0, -1.0);
  84. }
  85. vec3 unpackExtent(vec4 x, vec4 y, vec4 z)
  86. {
  87. vec3 value;
  88. value.x = unpackLowLessThan1000k(x);
  89. value.y = unpackLowLessThan1000k(y);
  90. value.z = unpackLowLessThan1000k(z);
  91. return value;
  92. }
  93. #endif
  94. #endif
  95. void main()
  96. {
  97. vec4 position = czm_computePosition();
  98. #ifdef EXTRUDED_GEOMETRY
  99. float delta = min(u_globeMinimumAltitude, czm_geometricToleranceOverMeter * length(position.xyz));
  100. delta *= czm_sceneMode == czm_sceneMode3D ? 1.0 : 0.0;
  101. //extrudeDirection is zero for the top layer
  102. position = position + vec4(extrudeDirection * delta, 0.0);
  103. #endif
  104. #ifdef TEXTURE_COORDINATES
  105. #ifdef SPHERICAL
  106. v_sphericalExtents = czm_batchTable_sphericalExtents(batchId);
  107. v_uvMinAndSphericalLongitudeRotation.z = czm_batchTable_longitudeRotation(batchId);
  108. #else // SPHERICAL
  109. #ifdef COLUMBUS_VIEW_2D
  110. #ifdef UINT8_PACKING
  111. vec4 planes2D_high = unpackPlanes2D_HIGH(czm_batchTable_planes2D_HIGH_x(batchId),
  112. czm_batchTable_planes2D_HIGH_y(batchId),
  113. czm_batchTable_planes2D_HIGH_z(batchId),
  114. czm_batchTable_planes2D_HIGH_w(batchId));
  115. vec4 planes2D_low = unpackPlanes2D_LOW(czm_batchTable_planes2D_LOW_x(batchId),
  116. czm_batchTable_planes2D_LOW_y(batchId),
  117. czm_batchTable_planes2D_LOW_z(batchId),
  118. czm_batchTable_planes2D_LOW_w(batchId));
  119. #else // UINT8_PACKING
  120. vec4 planes2D_high = czm_batchTable_planes2D_HIGH(batchId);
  121. vec4 planes2D_low = czm_batchTable_planes2D_LOW(batchId);
  122. #endif // UINT8_PACKING
  123. // If the primitive is split across the IDL (planes2D_high.x > planes2D_high.w):
  124. // - If this vertex is on the east side of the IDL (position3DLow.y > 0.0, comparison with position3DHigh may produce artifacts)
  125. // - existing "east" is on the wrong side of the world, far away (planes2D_high/low.w)
  126. // - so set "east" as beyond the eastmost extent of the projection (idlSplitNewPlaneHiLow)
  127. vec2 idlSplitNewPlaneHiLow = vec2(EAST_MOST_X_HIGH - (WEST_MOST_X_HIGH - planes2D_high.w), EAST_MOST_X_LOW - (WEST_MOST_X_LOW - planes2D_low.w));
  128. bool idlSplit = planes2D_high.x > planes2D_high.w && position3DLow.y > 0.0;
  129. planes2D_high.w = czm_branchFreeTernary(idlSplit, idlSplitNewPlaneHiLow.x, planes2D_high.w);
  130. planes2D_low.w = czm_branchFreeTernary(idlSplit, idlSplitNewPlaneHiLow.y, planes2D_low.w);
  131. // - else, if this vertex is on the west side of the IDL (position3DLow.y < 0.0)
  132. // - existing "west" is on the wrong side of the world, far away (planes2D_high/low.x)
  133. // - so set "west" as beyond the westmost extent of the projection (idlSplitNewPlaneHiLow)
  134. idlSplit = planes2D_high.x > planes2D_high.w && position3DLow.y < 0.0;
  135. idlSplitNewPlaneHiLow = vec2(WEST_MOST_X_HIGH - (EAST_MOST_X_HIGH - planes2D_high.x), WEST_MOST_X_LOW - (EAST_MOST_X_LOW - planes2D_low.x));
  136. planes2D_high.x = czm_branchFreeTernary(idlSplit, idlSplitNewPlaneHiLow.x, planes2D_high.x);
  137. planes2D_low.x = czm_branchFreeTernary(idlSplit, idlSplitNewPlaneHiLow.y, planes2D_low.x);
  138. vec3 southWestCorner = (czm_modelViewRelativeToEye * czm_translateRelativeToEye(vec3(0.0, planes2D_high.xy), vec3(0.0, planes2D_low.xy))).xyz;
  139. vec3 northWestCorner = (czm_modelViewRelativeToEye * czm_translateRelativeToEye(vec3(0.0, planes2D_high.x, planes2D_high.z), vec3(0.0, planes2D_low.x, planes2D_low.z))).xyz;
  140. vec3 southEastCorner = (czm_modelViewRelativeToEye * czm_translateRelativeToEye(vec3(0.0, planes2D_high.w, planes2D_high.y), vec3(0.0, planes2D_low.w, planes2D_low.y))).xyz;
  141. #else // COLUMBUS_VIEW_2D
  142. // 3D case has smaller "plane extents," so planes encoded as a 64 bit position and 2 vec3s for distances/direction
  143. #ifdef UINT8_PACKING
  144. vec3 low = southwest_LOW(czm_batchTable_southWest_LOW_x(batchId), czm_batchTable_southWest_LOW_y(batchId), czm_batchTable_southWest_LOW_z(batchId));
  145. vec3 high = southwest_HIGH(czm_batchTable_southWest_HIGH_x(batchId), czm_batchTable_southWest_HIGH_y(batchId), czm_batchTable_southWest_HIGH_z(batchId));
  146. vec3 southWestCorner = (czm_modelViewRelativeToEye * czm_translateRelativeToEye(high, low)).xyz;
  147. vec3 northWestCorner = czm_normal * unpackExtent(
  148. czm_batchTable_northward_x(batchId),
  149. czm_batchTable_northward_y(batchId),
  150. czm_batchTable_northward_z(batchId)) + southWestCorner;
  151. vec3 southEastCorner = czm_normal * unpackExtent(
  152. czm_batchTable_eastward_x(batchId),
  153. czm_batchTable_eastward_y(batchId),
  154. czm_batchTable_eastward_z(batchId)) + southWestCorner;
  155. #else // UINT8_PACKING
  156. vec3 southWestCorner = (czm_modelViewRelativeToEye * czm_translateRelativeToEye(czm_batchTable_southWest_HIGH(batchId), czm_batchTable_southWest_LOW(batchId))).xyz;
  157. vec3 northWestCorner = czm_normal * czm_batchTable_northward(batchId) + southWestCorner;
  158. vec3 southEastCorner = czm_normal * czm_batchTable_eastward(batchId) + southWestCorner;
  159. #endif // UINT8_PACKING
  160. #endif // COLUMBUS_VIEW_2D
  161. vec3 eastWard = southEastCorner - southWestCorner;
  162. float eastExtent = length(eastWard);
  163. eastWard /= eastExtent;
  164. vec3 northWard = northWestCorner - southWestCorner;
  165. float northExtent = length(northWard);
  166. northWard /= northExtent;
  167. v_westPlane = vec4(eastWard, -dot(eastWard, southWestCorner));
  168. v_southPlane = vec4(northWard, -dot(northWard, southWestCorner));
  169. v_inversePlaneExtents = vec2(1.0 / eastExtent, 1.0 / northExtent);
  170. #endif // SPHERICAL
  171. vec4 uvMinAndExtents = czm_batchTable_uvMinAndExtents(batchId);
  172. vec4 uMaxVmax = czm_batchTable_uMaxVmax(batchId);
  173. v_uMaxAndInverseDistance = vec3(uMaxVmax.xy, uvMinAndExtents.z);
  174. v_vMaxAndInverseDistance = vec3(uMaxVmax.zw, uvMinAndExtents.w);
  175. v_uvMinAndSphericalLongitudeRotation.xy = uvMinAndExtents.xy;
  176. #endif // TEXTURE_COORDINATES
  177. #ifdef PER_INSTANCE_COLOR
  178. v_color = czm_batchTable_color(batchId);
  179. #endif
  180. gl_Position = czm_depthClampFarPlane(czm_modelViewProjectionRelativeToEye * position);
  181. }