LineMaterial.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. Object.defineProperties( this, {
  233. dashed:{//add
  234. enumerable: true,
  235. get: function () {
  236. return 'USE_DASH' in this.defines
  237. },
  238. set: function ( value ) {
  239. if(value){
  240. this.defines.USE_DASH = ''
  241. }else{
  242. delete this.defines.USE_DASH
  243. }
  244. this.needsUpdate = true
  245. }
  246. },
  247. useDepth:{//add
  248. enumerable: true,
  249. get: function () {
  250. return 'useDepth' in this.defines
  251. },
  252. set: function ( value ) {
  253. if(value != this.useDepth){
  254. if(value){
  255. this.defines.useDepth = ''
  256. this.updateDepthParams()
  257. }else{
  258. delete this.defines.useDepth
  259. }
  260. this.needsUpdate = true
  261. }
  262. }
  263. },
  264. color: {
  265. enumerable: true,
  266. get: function () {
  267. return this.uniforms.diffuse.value;
  268. },
  269. set: function ( value ) {
  270. this.uniforms.diffuse.value = value;
  271. }
  272. },
  273. linewidth: {
  274. enumerable: true,
  275. get: function () {
  276. return this.uniforms.linewidth.value;
  277. },
  278. set: function ( value ) {
  279. this.uniforms.linewidth.value = value;
  280. }
  281. },
  282. dashScale: {
  283. enumerable: true,
  284. get: function () {
  285. return this.uniforms.dashScale.value;
  286. },
  287. set: function ( value ) {
  288. this.uniforms.dashScale.value = value;
  289. }
  290. },
  291. dashSize: {
  292. enumerable: true,
  293. get: function () {
  294. return this.uniforms.dashSize.value;
  295. },
  296. set: function ( value ) {
  297. this.uniforms.dashSize.value = value;
  298. }
  299. },
  300. dashOffset: {
  301. enumerable: true,
  302. get: function () {
  303. return this.uniforms.dashOffset.value;
  304. },
  305. set: function ( value ) {
  306. this.uniforms.dashOffset.value = value;
  307. }
  308. },
  309. gapSize: {
  310. enumerable: true,
  311. get: function () {
  312. return this.uniforms.gapSize.value;
  313. },
  314. set: function ( value ) {
  315. this.uniforms.gapSize.value = value;
  316. }
  317. },
  318. opacity: {
  319. enumerable: true,
  320. get: function () {
  321. return this.uniforms.opacity.value;
  322. },
  323. set: function ( value ) {
  324. this.uniforms.opacity.value = value;
  325. }
  326. },
  327. resolution: {
  328. enumerable: true,
  329. get: function () {
  330. return this.uniforms.resolution.value;
  331. },
  332. set: function ( value ) {
  333. this.uniforms.resolution.value.copy( value );
  334. }
  335. }
  336. } );
  337. this.setValues( parameters );
  338. //add
  339. this.updateDepthParams()
  340. viewer.addEventListener('camera_changed', (e)=>{
  341. if(e.viewport.name != 'mapViewport') this.updateDepthParams(e)
  342. })
  343. let setSize = (e)=>{
  344. let viewport = e.viewport
  345. let viewportOffset = viewport.offset || new Vector2()
  346. this.uniforms.resolution.value.copy(viewport.resolution2)
  347. this.uniforms.viewportOffset.value.copy(viewportOffset)
  348. }
  349. let viewport = viewer.mainViewport;
  350. setSize({viewport})
  351. viewer.addEventListener('resize',(e)=>{
  352. if(!e.viewport || e.viewport.name != 'mapViewport'){
  353. setSize(e)
  354. //console.log(this.name + viewportOffset.toArray())
  355. }
  356. })
  357. /* viewer.addEventListener("render.begin", (e)=>{//before render 如果有大于两个viewport的话可能要
  358. if(e.viewport.name != 'mapViewport') this.updateDepthParams({camera:e.viewport.camera})
  359. }) */
  360. };
  361. LineMaterial.prototype = Object.create( ShaderMaterial.prototype );
  362. LineMaterial.prototype.constructor = LineMaterial;
  363. LineMaterial.prototype.isLineMaterial = true;
  364. LineMaterial.prototype.updateDepthParams = function(e={}){
  365. if(this.useDepth){
  366. var camera = e.camera || viewer.scene.getActiveCamera()
  367. this.uniforms.depthTexture.value = viewer.getPRenderer().rtEDL.depthTexture
  368. this.uniforms.nearPlane.value = camera.near;
  369. this.uniforms.farPlane.value = camera.far;
  370. }
  371. }
  372. export { LineMaterial };