笔记.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. datasets中的orientation为模型整体绕z轴的旋转角度,初始为0
  2. filter中的:
  3. 数据集校准后不变的值有:
  4. dataset_orientation(永远存储最初始的点位的quaternion,在旋转时也不变,因此它等于没有旋转时的orientation)---- image.datasetOrientation
  5. dataset_floor_orientation(一般和dataset_orientation值一样)
  6. dataset_location
  7. dataset_floor_location
  8. 数据集校准后改变的值有:
  9. orientation----image.orientation(在旋转时实时变,且是根据模型旋转度数和dataset_orientation来算的,所以如果dataset_orientation不对,就会算错。)
  10. location----image.location
  11. floor_location
  12. ------------------------------------------------
  13. MeasurementLineMaterial : 测量线材质, 有蓝色标准实线和灰色透明虚线两种状态
  14. this.underlayScene.children[3] 包含32个子mesh, 是全景图sphere 其材质fragment在下方
  15. 加载深度图loadDepthImage 获取深度值getDepth(用于更新reticule位置)。深度图用于修改全景图sphere的gl_FragDepthEXT
  16. =======shader=======
  17. 全景图 fragment
  18. uniform sampler2D map;
  19. uniform float opacity;
  20. varying vec2 vUv;
  21. #ifdef USE_ALPHAMAP
  22. uniform sampler2D alphaMap;
  23. #endif
  24. #ifdef GL_EXT_frag_depth
  25. uniform sampler2D depthMap;
  26. uniform mat4 inverseProjectionMatrix;
  27. uniform mat4 projectionMatrix;
  28. uniform vec4 viewport;
  29. #endif
  30. void main()
  31. {
  32. vec4 color = texture2D(map, vUv);
  33. float alpha = opacity;
  34. #ifdef USE_ALPHAMAP
  35. alpha *= texture2D(alphaMap, vUv).g;
  36. #endif
  37. gl_FragColor = vec4(color.r, color.g, color.b, alpha);
  38. #ifdef GL_EXT_frag_depth
  39. /*
  40. * Useful resources:
  41. *
  42. * https://www.khronos.org/opengl/wiki/Vertex_Post-Processing#Viewport_transform
  43. * Clipping, perspective divide viewport transform
  44. *
  45. * https://www.khronos.org/opengl/wiki/Compute_eye_space_from_window_space
  46. * From window (viewport) space back to eye space in GLSL
  47. *
  48. * https://www.khronos.org/opengl/wiki/Vertex_Transformation
  49. * Summary of transformations object -> world -> eye (camera, view) -> clip -> NDC -> window
  50. *
  51. * http://slideplayer.com/slide/6837153/#
  52. * Overview presentation
  53. *
  54. * http://www.shaderific.com/glsl-variables/
  55. * GLSL built-in variables
  56. */
  57. vec4 depth = texture2D(depthMap, vUv);
  58. //float distance = depth.r + 256. * (depth.g + 256. * depth.b);
  59. //distance *= 255. * .001; // distance is now in meters
  60. //更改
  61. float distance = (depth.g + depth.r / 256.) * 255.; //为什么要乘以255
  62. // return r[1] + r[0] / 256
  63. vec4 ndcPos;
  64. ndcPos.xy = ((2.0 * gl_FragCoord.xy) - (2.0 * viewport.xy)) / (viewport.zw) - 1.;
  65. ndcPos.z = (2.0 * gl_FragCoord.z - gl_DepthRange.near - gl_DepthRange.far) /
  66. (gl_DepthRange.far - gl_DepthRange.near);
  67. ndcPos.w = 1.0;
  68. vec4 clipPos = ndcPos / gl_FragCoord.w;
  69. vec4 eyePos = inverseProjectionMatrix * clipPos;
  70. distance += .1; // add a safety margin
  71. vec4 eyePos2 = vec4(normalize(eyePos.xyz) * distance, 1.);
  72. vec4 clipPos2 = projectionMatrix * eyePos2;
  73. vec4 ndcPos2 = clipPos2 * 1. / clipPos2.w;
  74. gl_FragDepthEXT = 0.5 * ((gl_DepthRange.far - gl_DepthRange.near) * ndcPos2.z
  75. + gl_DepthRange.near + gl_DepthRange.far);
  76. #endif
  77. }
  78. --------
  79. MeasurementLineMaterial vertex
  80. "attribute vec3 previous;
  81. attribute vec3 next;
  82. attribute float side;
  83. attribute float width;
  84. attribute float counters;
  85. uniform vec2 resolution;
  86. uniform float lineWidth;
  87. uniform vec3 color;
  88. uniform float opacity;
  89. uniform float near;
  90. uniform float far;
  91. uniform float sizeAttenuation;
  92. uniform vec3 dashColor;
  93. uniform float dashOpacity;
  94. varying vec2 vUV;
  95. varying vec4 vColor;
  96. varying vec4 vDashColor;
  97. varying float vCounters;
  98. vec2 fix(vec4 i, float aspect)
  99. {
  100. vec2 res = i.xy / i.w;
  101. res.x *= aspect;
  102. vCounters = counters;
  103. return res;
  104. }
  105. // This vertex shader is a copy of the one supplied by MeshLineMaterial.
  106. // It supports drawing dashed lines.
  107. void main()
  108. {
  109. float aspect = resolution.x / resolution.y;
  110. float pixelWidthRatio = 1.0 / (resolution.x * projectionMatrix[0][0]);
  111. vColor = vec4(color, opacity);
  112. vDashColor = vec4(dashColor, dashOpacity);
  113. vUV = uv;
  114. mat4 m = projectionMatrix * modelViewMatrix;
  115. vec4 finalPosition = m * vec4(position, 1.0);
  116. vec4 prevPos = m * vec4(previous, 1.0);
  117. vec4 nextPos = m * vec4(next, 1.0);
  118. vec2 currentP = fix(finalPosition, aspect);
  119. vec2 prevP = fix(prevPos, aspect);
  120. vec2 nextP = fix(nextPos, aspect);
  121. float pixelWidth = finalPosition.w * pixelWidthRatio;
  122. float w = 1.8 * pixelWidth * lineWidth * width;
  123. if (sizeAttenuation == 1.0)
  124. {
  125. w = 1.8 * lineWidth * width;
  126. }
  127. vec2 dir;
  128. if (nextP == currentP)
  129. {
  130. dir = normalize(currentP - prevP);
  131. }
  132. else if (prevP == currentP)
  133. {
  134. dir = normalize(nextP - currentP);
  135. }
  136. else
  137. {
  138. vec2 dir1 = normalize(currentP - prevP);
  139. vec2 dir2 = normalize(nextP - currentP);
  140. dir = normalize(dir1 + dir2);
  141. vec2 perp = vec2(-dir1.y, dir1.x);
  142. vec2 miter = vec2(-dir.y, dir.x);
  143. }
  144. vec2 normal = vec2(-dir.y, dir.x);
  145. normal.x /= aspect;
  146. normal *= .5 * w;
  147. vec4 offset = vec4(normal * side, 0.0, 1.0);
  148. finalPosition.xy += offset.xy;
  149. gl_Position = finalPosition;
  150. }
  151. --------
  152. MeasurementLineMaterial fragment
  153. uniform sampler2D map;
  154. uniform sampler2D alphaMap;
  155. uniform float useMap;
  156. uniform float useAlphaMap;
  157. uniform float useDash;
  158. uniform float dashArray;
  159. uniform float dashOffset;
  160. uniform float dashRatio;
  161. uniform float visibility;
  162. uniform float alphaTest;
  163. uniform vec2 repeat;
  164. uniform sampler2D depthTexture;
  165. uniform sampler2D rgbaTexture;
  166. uniform float nearPlane;
  167. uniform float farPlane;
  168. uniform float occlusionDistance;
  169. uniform float clipDistance;
  170. uniform vec2 viewportSize;
  171. uniform vec2 viewportOffset;
  172. varying vec2 vUV;
  173. varying vec4 vColor;
  174. varying vec4 vDashColor;
  175. varying float vCounters;
  176. // Converts the exponential depth value from the depth buffer to a linear value
  177. // See https://learnopengl.com/Advanced-OpenGL/Depth-testing for more information about this formula
  178. float convertToLinear(float zValue)
  179. {
  180. float z = zValue * 2.0 - 1.0;
  181. return (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - z * (farPlane - nearPlane));
  182. }
  183. void main()
  184. {
  185. vec4 c = vDashColor;
  186. // <-- The following section of the shader is copied from MeshLineMaterial
  187. // Sample the fragment from a texture if such is supplied
  188. if (useMap == 1.0)
  189. {
  190. c *= texture2D(map, vUV * repeat);
  191. }
  192. // Sample the fragment's alpha value from an alpha texture if such is supplied
  193. if (useAlphaMap == 1.0)
  194. {
  195. c.a *= texture2D(alphaMap, vUV * repeat).a;
  196. }
  197. // Discard the fragment if below the alpha threshold
  198. if (c.a < alphaTest)
  199. {
  200. discard;
  201. }
  202. // If the line is dashed, set the alpha value of the fragment according to the line segment it belongs to
  203. if (useDash == 1.0)
  204. {
  205. c.a *= ceil(mod(vCounters + dashOffset, dashArray) - (dashArray * dashRatio));
  206. }
  207. // <-- end of copied code
  208. #ifdef GL_EXT_frag_depth
  209. // mixFactor and clipFactor define the color mixing proportion between the states of
  210. // full visibility and occluded visibility
  211. // and
  212. // full visibility and total invisibility
  213. float mixFactor = 0.0;
  214. float clipFactor = 0.0;
  215. // The linear depth value of the current fragment
  216. float fragDepth = convertToLinear(gl_FragCoord.z);
  217. // The coordinates of the current fragment in the depth texture
  218. vec2 depthTxtCoords = vec2(gl_FragCoord.x - viewportOffset.x, gl_FragCoord.y) / viewportSize;
  219. // The linear depth value of the pixel occupied by this fragment in the depth buffer
  220. float textureDepth = convertToLinear(texture2D(depthTexture, depthTxtCoords).r);
  221. // The difference between the two depths
  222. float delta = textureDepth - fragDepth;
  223. if (delta < 0.0)
  224. {
  225. // occlusionDistance and clipDistance define the width of the respective zones and
  226. // mixFactor and clipFactor express the interpolation between the two colors depending on the position
  227. // of the current fragment withing those zones.
  228. mixFactor = clamp(delta / occlusionDistance, 0.0, 1.0);
  229. clipFactor = clamp(delta / clipDistance, 0.0, 1.0);
  230. }
  231. // If the fragment is totally transparent, don't bother drawing it
  232. if (clipFactor == 1.0)
  233. {
  234. discard;
  235. }
  236. #else
  237. float mixFactor = 0.0;
  238. float clipFactor = 0.0;
  239. #endif
  240. // Calculate the color of the dashed version of the line
  241. vec4 backColor = vec4(c.rgb, c.a * step(vCounters, visibility));
  242. // Mix between the solid and the dahsed versions of the line according to the mixFactor
  243. gl_FragColor = mix(vColor, backColor, mixFactor);
  244. // Set the alpha value of the fragment according to the clipFactor
  245. // Note that clipFactor was previously clamped [0.0;1.0]
  246. gl_FragColor.a *= (1.0 - clipFactor);
  247. }