LineMaterial.js 14 KB

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