BillboardCollectionVS.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //This file is automatically rebuilt by the Cesium build process.
  2. /*global define*/
  3. define(function() {
  4. "use strict";
  5. return "attribute vec4 positionHighAndScale;\n\
  6. attribute vec4 positionLowAndRotation; \n\
  7. attribute vec4 compressedAttribute0; // pixel offset, translate, horizontal origin, vertical origin, show, texture coordinates, direction\n\
  8. attribute vec4 compressedAttribute1; // aligned axis, translucency by distance, image width\n\
  9. attribute vec4 compressedAttribute2; // image height, color, pick color, 2 bytes free\n\
  10. attribute vec3 eyeOffset; // eye offset in meters\n\
  11. attribute vec4 scaleByDistance; // near, nearScale, far, farScale\n\
  12. attribute vec4 pixelOffsetScaleByDistance; // near, nearScale, far, farScale\n\
  13. \n\
  14. varying vec2 v_textureCoordinates;\n\
  15. \n\
  16. #ifdef RENDER_FOR_PICK\n\
  17. varying vec4 v_pickColor;\n\
  18. #else\n\
  19. varying vec4 v_color;\n\
  20. #endif\n\
  21. \n\
  22. float getNearFarScalar(vec4 nearFarScalar, float cameraDistSq)\n\
  23. {\n\
  24. float valueAtMin = nearFarScalar.y;\n\
  25. float valueAtMax = nearFarScalar.w;\n\
  26. float nearDistanceSq = nearFarScalar.x * nearFarScalar.x;\n\
  27. float farDistanceSq = nearFarScalar.z * nearFarScalar.z;\n\
  28. \n\
  29. float t = (cameraDistSq - nearDistanceSq) / (farDistanceSq - nearDistanceSq);\n\
  30. \n\
  31. t = pow(clamp(t, 0.0, 1.0), 0.2);\n\
  32. \n\
  33. return mix(valueAtMin, valueAtMax, t);\n\
  34. }\n\
  35. \n\
  36. const float UPPER_BOUND = 32768.0;\n\
  37. \n\
  38. const float SHIFT_LEFT16 = 65536.0;\n\
  39. const float SHIFT_LEFT8 = 256.0;\n\
  40. const float SHIFT_LEFT7 = 128.0;\n\
  41. const float SHIFT_LEFT5 = 32.0;\n\
  42. const float SHIFT_LEFT3 = 8.0;\n\
  43. const float SHIFT_LEFT2 = 4.0;\n\
  44. const float SHIFT_LEFT1 = 2.0;\n\
  45. \n\
  46. const float SHIFT_RIGHT8 = 1.0 / 256.0;\n\
  47. const float SHIFT_RIGHT7 = 1.0 / 128.0;\n\
  48. const float SHIFT_RIGHT5 = 1.0 / 32.0;\n\
  49. const float SHIFT_RIGHT3 = 1.0 / 8.0;\n\
  50. const float SHIFT_RIGHT2 = 1.0 / 4.0;\n\
  51. const float SHIFT_RIGHT1 = 1.0 / 2.0;\n\
  52. \n\
  53. void main() \n\
  54. {\n\
  55. // Modifying this shader may also require modifications to Billboard._computeScreenSpacePosition\n\
  56. \n\
  57. // unpack attributes\n\
  58. vec3 positionHigh = positionHighAndScale.xyz;\n\
  59. vec3 positionLow = positionLowAndRotation.xyz;\n\
  60. float scale = positionHighAndScale.w;\n\
  61. \n\
  62. #if defined(ROTATION) || defined(ALIGNED_AXIS)\n\
  63. float rotation = positionLowAndRotation.w;\n\
  64. #endif\n\
  65. \n\
  66. float compressed = compressedAttribute0.x;\n\
  67. \n\
  68. vec2 pixelOffset;\n\
  69. pixelOffset.x = floor(compressed * SHIFT_RIGHT7);\n\
  70. compressed -= pixelOffset.x * SHIFT_LEFT7;\n\
  71. pixelOffset.x -= UPPER_BOUND;\n\
  72. \n\
  73. vec2 origin;\n\
  74. origin.x = floor(compressed * SHIFT_RIGHT5);\n\
  75. compressed -= origin.x * SHIFT_LEFT5;\n\
  76. \n\
  77. origin.y = floor(compressed * SHIFT_RIGHT3);\n\
  78. compressed -= origin.y * SHIFT_LEFT3;\n\
  79. \n\
  80. origin -= vec2(1.0);\n\
  81. \n\
  82. float show = floor(compressed * SHIFT_RIGHT2);\n\
  83. compressed -= show * SHIFT_LEFT2;\n\
  84. \n\
  85. vec2 direction;\n\
  86. direction.x = floor(compressed * SHIFT_RIGHT1);\n\
  87. direction.y = compressed - direction.x * SHIFT_LEFT1;\n\
  88. \n\
  89. float temp = compressedAttribute0.y * SHIFT_RIGHT8;\n\
  90. pixelOffset.y = -(floor(temp) - UPPER_BOUND);\n\
  91. \n\
  92. vec2 translate;\n\
  93. translate.y = (temp - floor(temp)) * SHIFT_LEFT16;\n\
  94. \n\
  95. temp = compressedAttribute0.z * SHIFT_RIGHT8;\n\
  96. translate.x = floor(temp) - UPPER_BOUND;\n\
  97. \n\
  98. translate.y += (temp - floor(temp)) * SHIFT_LEFT8;\n\
  99. translate.y -= UPPER_BOUND;\n\
  100. \n\
  101. vec2 textureCoordinates = czm_decompressTextureCoordinates(compressedAttribute0.w);\n\
  102. \n\
  103. temp = compressedAttribute1.x * SHIFT_RIGHT8;\n\
  104. \n\
  105. vec2 imageSize = vec2(floor(temp), compressedAttribute2.w);\n\
  106. \n\
  107. #ifdef EYE_DISTANCE_TRANSLUCENCY\n\
  108. vec4 translucencyByDistance;\n\
  109. translucencyByDistance.x = compressedAttribute1.z;\n\
  110. translucencyByDistance.z = compressedAttribute1.w;\n\
  111. \n\
  112. translucencyByDistance.y = ((temp - floor(temp)) * SHIFT_LEFT8) / 255.0;\n\
  113. \n\
  114. temp = compressedAttribute1.y * SHIFT_RIGHT8;\n\
  115. translucencyByDistance.w = ((temp - floor(temp)) * SHIFT_LEFT8) / 255.0;\n\
  116. #endif\n\
  117. \n\
  118. #ifdef ALIGNED_AXIS\n\
  119. vec3 alignedAxis = czm_octDecode(floor(compressedAttribute1.y * SHIFT_RIGHT8));\n\
  120. #else\n\
  121. vec3 alignedAxis = vec3(0.0);\n\
  122. #endif\n\
  123. \n\
  124. #ifdef RENDER_FOR_PICK\n\
  125. temp = compressedAttribute2.y;\n\
  126. #else\n\
  127. temp = compressedAttribute2.x;\n\
  128. #endif\n\
  129. \n\
  130. vec4 color;\n\
  131. temp = temp * SHIFT_RIGHT8;\n\
  132. color.b = (temp - floor(temp)) * SHIFT_LEFT8;\n\
  133. temp = floor(temp) * SHIFT_RIGHT8;\n\
  134. color.g = (temp - floor(temp)) * SHIFT_LEFT8;\n\
  135. color.r = floor(temp);\n\
  136. \n\
  137. temp = compressedAttribute2.z * SHIFT_RIGHT8;\n\
  138. \n\
  139. #ifdef RENDER_FOR_PICK\n\
  140. color.a = (temp - floor(temp)) * SHIFT_LEFT8;\n\
  141. vec4 pickColor = color / 255.0;\n\
  142. #else\n\
  143. color.a = floor(temp);\n\
  144. color /= 255.0;\n\
  145. #endif\n\
  146. \n\
  147. ///////////////////////////////////////////////////////////////////////////\n\
  148. \n\
  149. vec4 p = czm_translateRelativeToEye(positionHigh, positionLow);\n\
  150. vec4 positionEC = czm_modelViewRelativeToEye * p;\n\
  151. positionEC = czm_eyeOffset(positionEC, eyeOffset);\n\
  152. positionEC.xyz *= show;\n\
  153. \n\
  154. /////////////////////////////////////////////////////////////////////////// \n\
  155. \n\
  156. #if defined(EYE_DISTANCE_SCALING) || defined(EYE_DISTANCE_TRANSLUCENCY) || defined(EYE_DISTANCE_PIXEL_OFFSET)\n\
  157. float lengthSq;\n\
  158. if (czm_sceneMode == czm_sceneMode2D)\n\
  159. {\n\
  160. // 2D camera distance is a special case\n\
  161. // treat all billboards as flattened to the z=0.0 plane\n\
  162. lengthSq = czm_eyeHeight2D.y;\n\
  163. }\n\
  164. else\n\
  165. {\n\
  166. lengthSq = dot(positionEC.xyz, positionEC.xyz);\n\
  167. }\n\
  168. #endif\n\
  169. \n\
  170. #ifdef EYE_DISTANCE_SCALING\n\
  171. scale *= getNearFarScalar(scaleByDistance, lengthSq);\n\
  172. // push vertex behind near plane for clipping\n\
  173. if (scale == 0.0)\n\
  174. {\n\
  175. positionEC.xyz = vec3(0.0);\n\
  176. }\n\
  177. #endif\n\
  178. \n\
  179. float translucency = 1.0;\n\
  180. #ifdef EYE_DISTANCE_TRANSLUCENCY\n\
  181. translucency = getNearFarScalar(translucencyByDistance, lengthSq);\n\
  182. // push vertex behind near plane for clipping\n\
  183. if (translucency == 0.0)\n\
  184. {\n\
  185. positionEC.xyz = vec3(0.0);\n\
  186. }\n\
  187. #endif\n\
  188. \n\
  189. #ifdef EYE_DISTANCE_PIXEL_OFFSET\n\
  190. float pixelOffsetScale = getNearFarScalar(pixelOffsetScaleByDistance, lengthSq);\n\
  191. pixelOffset *= pixelOffsetScale;\n\
  192. #endif\n\
  193. \n\
  194. vec4 positionWC = czm_eyeToWindowCoordinates(positionEC);\n\
  195. \n\
  196. vec2 halfSize = imageSize * scale * czm_resolutionScale;\n\
  197. halfSize *= ((direction * 2.0) - 1.0);\n\
  198. \n\
  199. positionWC.xy += (origin * abs(halfSize));\n\
  200. \n\
  201. #if defined(ROTATION) || defined(ALIGNED_AXIS)\n\
  202. if (!all(equal(alignedAxis, vec3(0.0))) || rotation != 0.0)\n\
  203. {\n\
  204. float angle = rotation;\n\
  205. if (!all(equal(alignedAxis, vec3(0.0))))\n\
  206. {\n\
  207. vec3 pos = positionEC.xyz + czm_encodedCameraPositionMCHigh + czm_encodedCameraPositionMCLow;\n\
  208. vec3 normal = normalize(cross(alignedAxis, pos));\n\
  209. vec4 tangent = vec4(normalize(cross(pos, normal)), 0.0);\n\
  210. tangent = czm_modelViewProjection * tangent;\n\
  211. angle += sign(-tangent.x) * acos(tangent.y / length(tangent.xy));\n\
  212. }\n\
  213. \n\
  214. float cosTheta = cos(angle);\n\
  215. float sinTheta = sin(angle);\n\
  216. mat2 rotationMatrix = mat2(cosTheta, sinTheta, -sinTheta, cosTheta);\n\
  217. halfSize = rotationMatrix * halfSize;\n\
  218. }\n\
  219. #endif\n\
  220. \n\
  221. positionWC.xy += halfSize;\n\
  222. positionWC.xy += translate;\n\
  223. positionWC.xy += (pixelOffset * czm_resolutionScale);\n\
  224. \n\
  225. gl_Position = czm_viewportOrthographic * vec4(positionWC.xy, -positionWC.z, 1.0);\n\
  226. v_textureCoordinates = textureCoordinates;\n\
  227. \n\
  228. #ifdef RENDER_FOR_PICK\n\
  229. v_pickColor = pickColor;\n\
  230. #else\n\
  231. v_color = color;\n\
  232. v_color.a *= translucency;\n\
  233. #endif\n\
  234. }\n\
  235. ";
  236. });