LineMaterial.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. bool unvisible = mod( vLineDistance + dashOffset, dashSize + gapSize ) > dashSize;
  168. //加
  169. #ifdef DASH_with_depth
  170. #else
  171. if (unvisible) discard; // todo - FIX
  172. #endif
  173. #endif
  174. if ( abs( vUv.y ) > 1.0 ) {
  175. float a = vUv.x;
  176. float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
  177. float len2 = a * a + b * b;
  178. if ( len2 > 1.0 ) discard;
  179. }
  180. vec4 diffuseColor = vec4( diffuse, opacity );
  181. //加
  182. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  183. float mixFactor = 0.0;
  184. float clipFactor = 0.0;
  185. float fragDepth = convertToLinear(gl_FragCoord.z);
  186. vec2 depthTxtCoords = vec2(gl_FragCoord.x - viewportOffset.x, gl_FragCoord.y) / resolution;
  187. float textureDepth = convertToLinear(texture2D(depthTexture, depthTxtCoords).r);
  188. float delta = textureDepth - fragDepth;
  189. if (delta < 0.0)
  190. {
  191. float occlusionDistance = - 1.0;
  192. float clipDistance = - 4.0;
  193. mixFactor = clamp(delta / occlusionDistance, 0.0, 1.0);
  194. clipFactor = clamp(delta / clipDistance, 0.0, 1.0);
  195. }
  196. if (clipFactor == 1.0)
  197. {
  198. discard;
  199. }
  200. vec4 backColor = vec4(0.8,0.8,0.8, 0.8*opacity);
  201. #ifdef DASH_with_depth
  202. // 只在被遮住的部分显示虚线
  203. if(unvisible) backColor.a = 0.0;
  204. #endif
  205. //vec4 diffuseColor = vec4(mix(diffuse, backColor, mixFactor), opacity*(1.0 - clipFactor));
  206. diffuseColor = mix(diffuseColor, backColor , mixFactor);
  207. diffuseColor.a *= (1.0 - clipFactor);
  208. #endif
  209. #include <logdepthbuf_fragment>
  210. #include <color_fragment>
  211. gl_FragColor = vec4( diffuseColor.rgb, diffuseColor.a );
  212. #include <tonemapping_fragment>
  213. #include <encodings_fragment>
  214. #include <fog_fragment>
  215. #include <premultiplied_alpha_fragment>
  216. }
  217. `
  218. };
  219. var LineMaterial = function ( parameters ) {
  220. ShaderMaterial.call( this, {
  221. type: 'LineMaterial',
  222. uniforms: UniformsUtils.clone( ShaderLib[ 'line' ].uniforms ),
  223. vertexShader: ShaderLib[ 'line' ].vertexShader,
  224. fragmentShader: ShaderLib[ 'line' ].fragmentShader,
  225. clipping: true // required for clipping support
  226. } );
  227. this.dashed = false;
  228. this.lineWidth_ = 0
  229. Object.defineProperties( this, {
  230. dashed:{//add
  231. enumerable: true,
  232. get: function () {
  233. return 'USE_DASH' in this.defines
  234. },
  235. set: function ( value ) {
  236. if(value){
  237. this.defines.USE_DASH = ''
  238. }else{
  239. delete this.defines.USE_DASH
  240. }
  241. this.needsUpdate = true
  242. }
  243. },
  244. useDepth:{//add
  245. enumerable: true,
  246. get: function () {
  247. return 'useDepth' in this.defines
  248. },
  249. set: function ( value ) {
  250. if(value != this.useDepth){
  251. if(value){
  252. this.defines.useDepth = ''
  253. this.updateDepthParams()
  254. }else{
  255. delete this.defines.useDepth
  256. }
  257. this.needsUpdate = true
  258. }
  259. }
  260. },
  261. dashWithDepth:{//add
  262. enumerable: true,
  263. get: function () {
  264. return 'DASH_with_depth' in this.defines
  265. },
  266. set: function ( value ) {
  267. if(value != this.dashWithDepth){
  268. if(value){
  269. this.defines.DASH_with_depth = ''
  270. }else{
  271. delete this.defines.DASH_with_depth
  272. }
  273. this.needsUpdate = true
  274. }
  275. }
  276. },
  277. color: {
  278. enumerable: true,
  279. get: function () {
  280. return this.uniforms.diffuse.value;
  281. },
  282. set: function ( value ) {
  283. this.uniforms.diffuse.value = value;
  284. }
  285. },
  286. lineWidth: {
  287. enumerable: true,
  288. get: function () {
  289. return this.lineWidth_;//this.uniforms.lineWidth.value;
  290. },
  291. set: function ( value ) {
  292. this.uniforms.lineWidth.value = value * window.devicePixelRatio;
  293. this.lineWidth_ = value
  294. }
  295. },
  296. dashScale: {
  297. enumerable: true,
  298. get: function () {
  299. return this.uniforms.dashScale.value;
  300. },
  301. set: function ( value ) {
  302. this.uniforms.dashScale.value = value;
  303. }
  304. },
  305. dashSize: {
  306. enumerable: true,
  307. get: function () {
  308. return this.uniforms.dashSize.value;
  309. },
  310. set: function ( value ) {
  311. this.uniforms.dashSize.value = value;
  312. }
  313. },
  314. dashOffset: {
  315. enumerable: true,
  316. get: function () {
  317. return this.uniforms.dashOffset.value;
  318. },
  319. set: function ( value ) {
  320. this.uniforms.dashOffset.value = value;
  321. }
  322. },
  323. gapSize: {
  324. enumerable: true,
  325. get: function () {
  326. return this.uniforms.gapSize.value;
  327. },
  328. set: function ( value ) {
  329. this.uniforms.gapSize.value = value;
  330. }
  331. },
  332. opacity: {
  333. enumerable: true,
  334. get: function () {
  335. return this.uniforms.opacity.value;
  336. },
  337. set: function ( value ) {
  338. this.uniforms.opacity.value = value;
  339. }
  340. },
  341. resolution: {
  342. enumerable: true,
  343. get: function () {
  344. return this.uniforms.resolution.value;
  345. },
  346. set: function ( value ) {
  347. this.uniforms.resolution.value.copy( value );
  348. }
  349. }
  350. } );
  351. this.setValues( parameters );
  352. //add
  353. this.updateDepthParams()
  354. viewer.addEventListener('camera_changed', (e)=>{
  355. if(e.viewport.name != 'mapViewport') this.updateDepthParams(e)
  356. })
  357. let setSize = (e)=>{
  358. let viewport = e.viewport
  359. let viewportOffset = viewport.offset || new Vector2()
  360. this.uniforms.resolution.value.copy(viewport.resolution2)
  361. this.uniforms.viewportOffset.value.copy(viewportOffset)
  362. this.lineWidth = this.lineWidth_
  363. }
  364. let viewport = viewer.mainViewport;
  365. setSize({viewport})
  366. viewer.addEventListener('resize',(e)=>{
  367. if(!e.viewport || e.viewport.name != 'mapViewport'){
  368. setSize(e)
  369. //console.log(this.name + viewportOffset.toArray())
  370. }
  371. })
  372. /* viewer.addEventListener("render.begin", (e)=>{//before render 如果有大于两个viewport的话可能要
  373. if(e.viewport.name != 'mapViewport') this.updateDepthParams({camera:e.viewport.camera})
  374. }) */
  375. };
  376. LineMaterial.prototype = Object.create( ShaderMaterial.prototype );
  377. LineMaterial.prototype.constructor = LineMaterial;
  378. LineMaterial.prototype.isLineMaterial = true;
  379. LineMaterial.prototype.updateDepthParams = function(e={}){
  380. if(this.useDepth){
  381. var camera = e.camera || viewer.scene.getActiveCamera()
  382. this.uniforms.depthTexture.value = viewer.getPRenderer().rtEDL.depthTexture
  383. this.uniforms.nearPlane.value = camera.near;
  384. this.uniforms.farPlane.value = camera.far;
  385. }
  386. }
  387. export { LineMaterial };