LineMaterial.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. import {
  2. ShaderLib,
  3. ShaderMaterial,
  4. UniformsLib,
  5. UniformsUtils,
  6. Vector2
  7. } from '../build/three.module.js';
  8. /**
  9. * parameters = {
  10. * color: <hex>,
  11. * linewidth: <float>,
  12. * dashed: <boolean>,
  13. * dashScale: <float>,
  14. * dashSize: <float>,
  15. * dashOffset: <float>,
  16. * gapSize: <float>,
  17. * resolution: <Vector2>, // to be set by renderer
  18. * }
  19. */
  20. UniformsLib.line = {
  21. linewidth: { value: 1 },
  22. resolution: { value: new Vector2( 1, 1 ) },
  23. viewportOffset: { value: new Vector2(0, 0 ) }, //left, top
  24. dashScale: { value: 1 },
  25. dashSize: { value: 1 },
  26. dashOffset: { value: 0 },
  27. gapSize: { value: 1 }, // todo FIX - maybe change to totalSize
  28. opacity: { value: 1 },
  29. depthTexture:{ value: null },
  30. nearPlane:{value: 0.1},
  31. farPlane:{value: 100000},
  32. };
  33. ShaderLib[ 'line' ] = {
  34. uniforms: UniformsUtils.merge( [
  35. UniformsLib.common,
  36. UniformsLib.fog,
  37. UniformsLib.line
  38. ] ),
  39. vertexShader:
  40. `
  41. #include <common>
  42. #include <color_pars_vertex>
  43. #include <fog_pars_vertex>
  44. #include <logdepthbuf_pars_vertex>
  45. #include <clipping_planes_pars_vertex>
  46. uniform float linewidth;
  47. uniform vec2 resolution;
  48. attribute vec3 instanceStart;
  49. attribute vec3 instanceEnd;
  50. attribute vec3 instanceColorStart;
  51. attribute vec3 instanceColorEnd;
  52. varying vec2 vUv;
  53. #ifdef USE_DASH
  54. uniform float dashScale;
  55. attribute float instanceDistanceStart;
  56. attribute float instanceDistanceEnd;
  57. varying float vLineDistance;
  58. #endif
  59. void trimSegment( const in vec4 start, inout vec4 end ) {
  60. // trim end segment so it terminates between the camera plane and the near plane
  61. // conservative estimate of the near plane
  62. float a = projectionMatrix[ 2 ][ 2 ]; // 3nd entry in 3th column
  63. float b = projectionMatrix[ 3 ][ 2 ]; // 3nd entry in 4th column
  64. float nearEstimate = - 0.5 * b / a;
  65. float alpha = ( nearEstimate - start.z ) / ( end.z - start.z );
  66. end.xyz = mix( start.xyz, end.xyz, alpha );
  67. }
  68. void main() {
  69. #ifdef USE_COLOR
  70. vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
  71. #endif
  72. #ifdef USE_DASH
  73. vLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd;
  74. #endif
  75. float aspect = resolution.x / resolution.y;
  76. vUv = uv;
  77. // camera space
  78. vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
  79. vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );
  80. // special case for perspective projection, and segments that terminate either in, or behind, the camera plane
  81. // clearly the gpu firmware has a way of addressing this issue when projecting into ndc space
  82. // but we need to perform ndc-space calculations in the shader, so we must address this issue directly
  83. // perhaps there is a more elegant solution -- WestLangley
  84. bool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 ); // 4th entry in the 3rd column
  85. if ( perspective ) {
  86. if ( start.z < 0.0 && end.z >= 0.0 ) {
  87. trimSegment( start, end );
  88. } else if ( end.z < 0.0 && start.z >= 0.0 ) {
  89. trimSegment( end, start );
  90. }
  91. }
  92. // clip space
  93. vec4 clipStart = projectionMatrix * start;
  94. vec4 clipEnd = projectionMatrix * end;
  95. // ndc space
  96. vec2 ndcStart = clipStart.xy / clipStart.w;
  97. vec2 ndcEnd = clipEnd.xy / clipEnd.w;
  98. // direction
  99. vec2 dir = ndcEnd - ndcStart;
  100. // account for clip-space aspect ratio
  101. dir.x *= aspect;
  102. dir = normalize( dir );
  103. // perpendicular to dir
  104. vec2 offset = vec2( dir.y, - dir.x );
  105. // undo aspect ratio adjustment
  106. dir.x /= aspect;
  107. offset.x /= aspect;
  108. // sign flip
  109. if ( position.x < 0.0 ) offset *= - 1.0;
  110. // endcaps
  111. if ( position.y < 0.0 ) {
  112. offset += - dir;
  113. } else if ( position.y > 1.0 ) {
  114. offset += dir;
  115. }
  116. // adjust for linewidth
  117. offset *= linewidth;
  118. // adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ...
  119. offset /= resolution.y;
  120. // select end
  121. vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;
  122. // back to clip space
  123. offset *= clip.w;
  124. clip.xy += offset;
  125. gl_Position = clip;
  126. vec4 mvPosition = ( position.y < 0.5 ) ? start : end; // this is an approximation
  127. #include <logdepthbuf_vertex>
  128. #include <clipping_planes_vertex>
  129. #include <fog_vertex>
  130. }
  131. `,
  132. fragmentShader:
  133. `
  134. uniform vec3 diffuse;
  135. uniform float opacity;
  136. #ifdef USE_DASH
  137. uniform float dashSize;
  138. uniform float dashOffset;
  139. uniform float gapSize;
  140. #endif
  141. //加
  142. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  143. uniform sampler2D depthTexture;
  144. uniform float nearPlane;
  145. uniform float farPlane;
  146. uniform vec2 resolution;
  147. uniform vec2 viewportOffset;
  148. #endif
  149. varying float vLineDistance;
  150. #include <common>
  151. #include <color_pars_fragment>
  152. #include <fog_pars_fragment>
  153. #include <logdepthbuf_pars_fragment>
  154. #include <clipping_planes_pars_fragment>
  155. varying vec2 vUv;
  156. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  157. float convertToLinear(float zValue)
  158. {
  159. float z = zValue * 2.0 - 1.0;
  160. return (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - z * (farPlane - nearPlane));
  161. }
  162. #endif
  163. void main() {
  164. #include <clipping_planes_fragment>
  165. #ifdef USE_DASH
  166. if ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; // discard endcaps
  167. if ( mod( vLineDistance + dashOffset, dashSize + gapSize ) > dashSize ) discard; // todo - FIX
  168. #endif
  169. if ( abs( vUv.y ) > 1.0 ) {
  170. float a = vUv.x;
  171. float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
  172. float len2 = a * a + b * b;
  173. if ( len2 > 1.0 ) discard;
  174. }
  175. //加
  176. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  177. // mixFactor and clipFactor define the color mixing proportion between the states of
  178. // full visibility and occluded visibility
  179. // and
  180. // full visibility and total invisibility
  181. float mixFactor = 0.0;
  182. float clipFactor = 0.0;
  183. // The linear depth value of the current fragment
  184. float fragDepth = convertToLinear(gl_FragCoord.z);
  185. // The coordinates of the current fragment in the depth texture
  186. vec2 depthTxtCoords = vec2(gl_FragCoord.x - viewportOffset.x, gl_FragCoord.y) / resolution;
  187. // The linear depth value of the pixel occupied by this fragment in the depth buffer
  188. float textureDepth = convertToLinear(texture2D(depthTexture, depthTxtCoords).r);
  189. // The difference between the two depths
  190. float delta = textureDepth - fragDepth;
  191. if (delta < 0.0)
  192. {
  193. // occlusionDistance and clipDistance define the width of the respective zones and
  194. // mixFactor and clipFactor express the interpolation between the two colors depending on the position
  195. // of the current fragment withing those zones.
  196. float occlusionDistance = - 1.0;
  197. float clipDistance = - 4.0;
  198. mixFactor = clamp(delta / occlusionDistance, 0.0, 1.0);
  199. clipFactor = clamp(delta / clipDistance, 0.0, 1.0);
  200. }
  201. // If the fragment is totally transparent, don't bother drawing it
  202. if (clipFactor == 1.0)
  203. {
  204. discard;
  205. }
  206. vec3 backColor = vec3(0.8,0.8,0.8);
  207. // Mix between the solid and the dahsed versions of the line according to the mixFactor
  208. vec4 diffuseColor = vec4(mix(diffuse, backColor, mixFactor), opacity*(1.0 - clipFactor));
  209. #else
  210. vec4 diffuseColor = vec4( diffuse, opacity );
  211. #endif
  212. //vec4 diffuseColor = vec4( diffuse, opacity );
  213. #include <logdepthbuf_fragment>
  214. #include <color_fragment>
  215. gl_FragColor = vec4( diffuseColor.rgb, diffuseColor.a );
  216. #include <tonemapping_fragment>
  217. #include <encodings_fragment>
  218. #include <fog_fragment>
  219. #include <premultiplied_alpha_fragment>
  220. }
  221. `
  222. };
  223. var LineMaterial = function ( parameters ) {
  224. ShaderMaterial.call( this, {
  225. type: 'LineMaterial',
  226. uniforms: UniformsUtils.clone( ShaderLib[ 'line' ].uniforms ),
  227. vertexShader: ShaderLib[ 'line' ].vertexShader,
  228. fragmentShader: ShaderLib[ 'line' ].fragmentShader,
  229. clipping: true // required for clipping support
  230. } );
  231. this.dashed = false;
  232. //add
  233. this.updateDepthParams()
  234. viewer.addEventListener('camera_changed', (e)=>{
  235. if(e.viewport.name != 'mapViewport') this.updateDepthParams(e)
  236. })
  237. let setSize = (e)=>{
  238. let viewportOffset = new Vector2(e.left||0, e.bottom||0)
  239. this.uniforms.resolution.value.copy(e.resolution)
  240. this.uniforms.viewportOffset.value.copy(viewportOffset)
  241. }
  242. let viewport = viewer.mainViewport;
  243. setSize(viewport)
  244. viewer.addEventListener('resize',(e)=>{
  245. //if(!e.viewport || e.viewport.name != 'mapViewport'){
  246. setSize(e)
  247. //console.log(this.name + viewportOffset.toArray())
  248. //}
  249. })
  250. /* viewer.addEventListener("render.begin", (e)=>{//before render 如果有大于两个viewport的话可能要
  251. if(e.viewport.name != 'mapViewport') this.updateDepthParams({camera:e.viewport.camera})
  252. }) */
  253. Object.defineProperties( this, {
  254. dashed:{//add
  255. enumerable: true,
  256. get: function () {
  257. return 'USE_DASH' in this.defines
  258. },
  259. set: function ( value ) {
  260. if(value){
  261. this.defines.USE_DASH = ''
  262. }else{
  263. delete this.defines.USE_DASH
  264. }
  265. this.needsUpdate = true
  266. }
  267. },
  268. useDepth:{//add
  269. enumerable: true,
  270. get: function () {
  271. return 'useDepth' in this.defines
  272. },
  273. set: function ( value ) {
  274. if(value != this.useDepth){
  275. if(value){
  276. this.defines.useDepth = ''
  277. this.updateDepthParams()
  278. }else{
  279. delete this.defines.useDepth
  280. }
  281. this.needsUpdate = true
  282. }
  283. }
  284. },
  285. color: {
  286. enumerable: true,
  287. get: function () {
  288. return this.uniforms.diffuse.value;
  289. },
  290. set: function ( value ) {
  291. this.uniforms.diffuse.value = value;
  292. }
  293. },
  294. linewidth: {
  295. enumerable: true,
  296. get: function () {
  297. return this.uniforms.linewidth.value;
  298. },
  299. set: function ( value ) {
  300. this.uniforms.linewidth.value = value;
  301. }
  302. },
  303. dashScale: {
  304. enumerable: true,
  305. get: function () {
  306. return this.uniforms.dashScale.value;
  307. },
  308. set: function ( value ) {
  309. this.uniforms.dashScale.value = value;
  310. }
  311. },
  312. dashSize: {
  313. enumerable: true,
  314. get: function () {
  315. return this.uniforms.dashSize.value;
  316. },
  317. set: function ( value ) {
  318. this.uniforms.dashSize.value = value;
  319. }
  320. },
  321. dashOffset: {
  322. enumerable: true,
  323. get: function () {
  324. return this.uniforms.dashOffset.value;
  325. },
  326. set: function ( value ) {
  327. this.uniforms.dashOffset.value = value;
  328. }
  329. },
  330. gapSize: {
  331. enumerable: true,
  332. get: function () {
  333. return this.uniforms.gapSize.value;
  334. },
  335. set: function ( value ) {
  336. this.uniforms.gapSize.value = value;
  337. }
  338. },
  339. opacity: {
  340. enumerable: true,
  341. get: function () {
  342. return this.uniforms.opacity.value;
  343. },
  344. set: function ( value ) {
  345. this.uniforms.opacity.value = value;
  346. }
  347. },
  348. resolution: {
  349. enumerable: true,
  350. get: function () {
  351. return this.uniforms.resolution.value;
  352. },
  353. set: function ( value ) {
  354. this.uniforms.resolution.value.copy( value );
  355. }
  356. }
  357. } );
  358. this.setValues( parameters );
  359. };
  360. LineMaterial.prototype = Object.create( ShaderMaterial.prototype );
  361. LineMaterial.prototype.constructor = LineMaterial;
  362. LineMaterial.prototype.isLineMaterial = true;
  363. LineMaterial.prototype.updateDepthParams = function(e={}){
  364. if(this.useDepth){
  365. var camera = e.camera || viewer.scene.getActiveCamera()
  366. this.uniforms.depthTexture.value = viewer.getPRenderer().rtEDL.depthTexture
  367. this.uniforms.nearPlane.value = camera.near;
  368. this.uniforms.farPlane.value = camera.far;
  369. }
  370. }
  371. export { LineMaterial };