BillboardCollectionVS.glsl 7.2 KB

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