LineMaterial.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /**
  2. * parameters = {
  3. * color: <hex>,
  4. * lineWidth: <float>,
  5. * dashed: <boolean>,
  6. * dashScale: <float>,
  7. * dashSize: <float>,
  8. * dashOffset: <float>,
  9. * gapSize: <float>,
  10. * resolution: <Vector2>, // to be set by renderer
  11. * }
  12. */
  13. import {
  14. ShaderLib,
  15. ShaderMaterial,
  16. UniformsLib,
  17. UniformsUtils,
  18. Vector2,
  19. Color
  20. } from '../build/three.module.js';
  21. UniformsLib.line = {
  22. /*
  23. worldUnits: { value: 1 },
  24. lineWidth: { value: 1 },
  25. resolution: { value: new Vector2( 1, 1 ) },
  26. dashOffset: { value: 0 },
  27. dashScale: { value: 1 },
  28. dashSize: { value: 1 },
  29. gapSize: { value: 1 } // todo FIX - maybe change to totalSize
  30. */
  31. worldUnits: { value: 1 },
  32. lineWidth: { value: 1 },
  33. resolution: { value: new Vector2( 1, 1 ) },
  34. viewportOffset: { value: new Vector2(0, 0 ) }, //left, top
  35. devicePixelRatio:{ value:window.devicePixelRatio},
  36. dashScale: { value: 1 },
  37. dashSize: { value: 1 },
  38. dashOffset: { value: 0 },
  39. gapSize: { value: 1 },
  40. opacity: { value: 1 },
  41. backColor: {type:'v3', value: new Color("#ddd")},
  42. clipDistance : { type: 'f', value: 4}, //消失距离
  43. occlusionDistance : { type: 'f', value: 1 }, //变为backColor距离
  44. maxClipFactor : { type: 'f', value: 1 }, //0-1
  45. depthTexture:{ value: null },
  46. nearPlane:{value: 0.1},
  47. farPlane:{value: 100000},
  48. };
  49. ShaderLib[ 'line' ] = {
  50. uniforms: UniformsUtils.merge( [
  51. UniformsLib.common,
  52. UniformsLib.fog,
  53. UniformsLib.line
  54. ] ),
  55. vertexShader:
  56. /* glsl */`
  57. #include <common>
  58. #include <color_pars_vertex>
  59. #include <fog_pars_vertex>
  60. #include <logdepthbuf_pars_vertex>
  61. #include <clipping_planes_pars_vertex>
  62. uniform float lineWidth;
  63. uniform vec2 resolution;
  64. uniform float devicePixelRatio; //add
  65. attribute vec3 instanceStart;
  66. attribute vec3 instanceEnd;
  67. attribute vec3 instanceColorStart;
  68. attribute vec3 instanceColorEnd;
  69. #ifdef WORLD_UNITS
  70. varying vec4 worldPos;
  71. varying vec3 worldStart;
  72. varying vec3 worldEnd;
  73. #ifdef USE_DASH
  74. varying vec2 vUv;
  75. #endif
  76. #else
  77. varying vec2 vUv;
  78. #endif
  79. #ifdef USE_DASH
  80. uniform float dashScale;
  81. attribute float instanceDistanceStart;
  82. attribute float instanceDistanceEnd;
  83. varying float vLineDistance;
  84. #endif
  85. void trimSegment( const in vec4 start, inout vec4 end ) {
  86. // trim end segment so it terminates between the camera plane and the near plane
  87. // conservative estimate of the near plane
  88. float a = projectionMatrix[ 2 ][ 2 ]; // 3nd entry in 3th column
  89. float b = projectionMatrix[ 3 ][ 2 ]; // 3nd entry in 4th column
  90. float nearEstimate = - 0.5 * b / a;
  91. float alpha = ( nearEstimate - start.z ) / ( end.z - start.z );
  92. end.xyz = mix( start.xyz, end.xyz, alpha );
  93. }
  94. void main() {
  95. #ifdef USE_COLOR
  96. vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
  97. #endif
  98. #ifdef USE_DASH
  99. vLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd;
  100. vUv = uv;
  101. #endif
  102. float aspect = resolution.x / resolution.y;
  103. // camera space
  104. vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
  105. vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );
  106. #ifdef WORLD_UNITS
  107. worldStart = start.xyz;
  108. worldEnd = end.xyz;
  109. #else
  110. vUv = uv;
  111. #endif
  112. // special case for perspective projection, and segments that terminate either in, or behind, the camera plane
  113. // clearly the gpu firmware has a way of addressing this issue when projecting into ndc space
  114. // but we need to perform ndc-space calculations in the shader, so we must address this issue directly
  115. // perhaps there is a more elegant solution -- WestLangley
  116. bool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 ); // 4th entry in the 3rd column
  117. if ( perspective ) {
  118. if ( start.z < 0.0 && end.z >= 0.0 ) {
  119. trimSegment( start, end );
  120. } else if ( end.z < 0.0 && start.z >= 0.0 ) {
  121. trimSegment( end, start );
  122. }
  123. }
  124. // clip space
  125. vec4 clipStart = projectionMatrix * start;
  126. vec4 clipEnd = projectionMatrix * end;
  127. // ndc space
  128. vec3 ndcStart = clipStart.xyz / clipStart.w;
  129. vec3 ndcEnd = clipEnd.xyz / clipEnd.w;
  130. // direction
  131. vec2 dir = ndcEnd.xy - ndcStart.xy;
  132. // account for clip-space aspect ratio
  133. dir.x *= aspect;
  134. dir = normalize( dir );
  135. #ifdef WORLD_UNITS
  136. // get the offset direction as perpendicular to the view vector
  137. vec3 worldDir = normalize( end.xyz - start.xyz );
  138. vec3 offset;
  139. if ( position.y < 0.5 ) {
  140. offset = normalize( cross( start.xyz, worldDir ) );
  141. } else {
  142. offset = normalize( cross( end.xyz, worldDir ) );
  143. }
  144. // sign flip
  145. if ( position.x < 0.0 ) offset *= - 1.0;
  146. float forwardOffset = dot( worldDir, vec3( 0.0, 0.0, 1.0 ) );
  147. // don't extend the line if we're rendering dashes because we
  148. // won't be rendering the endcaps
  149. #ifndef USE_DASH
  150. // extend the line bounds to encompass endcaps
  151. start.xyz += - worldDir * lineWidth * 0.5;
  152. end.xyz += worldDir * lineWidth * 0.5;
  153. // shift the position of the quad so it hugs the forward edge of the line
  154. offset.xy -= dir * forwardOffset;
  155. offset.z += 0.5;
  156. #endif
  157. // endcaps
  158. if ( position.y > 1.0 || position.y < 0.0 ) {
  159. offset.xy += dir * 2.0 * forwardOffset;
  160. }
  161. // adjust for lineWidth
  162. offset *= lineWidth * 0.5;
  163. // set the world position
  164. worldPos = ( position.y < 0.5 ) ? start : end;
  165. worldPos.xyz += offset;
  166. // project the worldpos
  167. vec4 clip = projectionMatrix * worldPos;
  168. // shift the depth of the projected points so the line
  169. // segments overlap neatly
  170. vec3 clipPose = ( position.y < 0.5 ) ? ndcStart : ndcEnd;
  171. clip.z = clipPose.z * clip.w;
  172. #else
  173. vec2 offset = vec2( dir.y, - dir.x );
  174. // undo aspect ratio adjustment
  175. dir.x /= aspect;
  176. offset.x /= aspect;
  177. // sign flip
  178. if ( position.x < 0.0 ) offset *= - 1.0;
  179. // endcaps
  180. if ( position.y < 0.0 ) {
  181. offset += - dir;
  182. } else if ( position.y > 1.0 ) {
  183. offset += dir;
  184. }
  185. // adjust for lineWidth
  186. offset *= lineWidth;
  187. // adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ...
  188. offset /= resolution.y * devicePixelRatio;
  189. // select end
  190. vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;
  191. // back to clip space
  192. offset *= clip.w;
  193. clip.xy += offset;
  194. #endif
  195. gl_Position = clip;
  196. vec4 mvPosition = ( position.y < 0.5 ) ? start : end; // this is an approximation
  197. #include <logdepthbuf_vertex>
  198. #include <clipping_planes_vertex>
  199. #include <fog_vertex>
  200. }
  201. `,
  202. fragmentShader:
  203. /* glsl */`
  204. uniform vec3 diffuse;
  205. uniform float opacity;
  206. uniform float lineWidth;
  207. uniform vec3 backColor;
  208. uniform float occlusionDistance;
  209. uniform float clipDistance;
  210. uniform float maxClipFactor;
  211. #ifdef USE_DASH
  212. uniform float dashOffset;
  213. uniform float dashSize;
  214. uniform float gapSize;
  215. #endif
  216. //加
  217. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  218. uniform sampler2D depthTexture;
  219. uniform float nearPlane;
  220. uniform float farPlane;
  221. uniform vec2 resolution;
  222. uniform vec2 viewportOffset;
  223. #endif
  224. varying float vLineDistance;
  225. #ifdef WORLD_UNITS
  226. varying vec4 worldPos;
  227. varying vec3 worldStart;
  228. varying vec3 worldEnd;
  229. #ifdef USE_DASH
  230. varying vec2 vUv;
  231. #endif
  232. #else
  233. varying vec2 vUv;
  234. #endif
  235. #include <common>
  236. #include <color_pars_fragment>
  237. #include <fog_pars_fragment>
  238. #include <logdepthbuf_pars_fragment>
  239. #include <clipping_planes_pars_fragment>
  240. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  241. float convertToLinear(float zValue)
  242. {
  243. float z = zValue * 2.0 - 1.0;
  244. return (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - z * (farPlane - nearPlane));
  245. }
  246. #endif
  247. vec2 closestLineToLine(vec3 p1, vec3 p2, vec3 p3, vec3 p4) {
  248. float mua;
  249. float mub;
  250. vec3 p13 = p1 - p3;
  251. vec3 p43 = p4 - p3;
  252. vec3 p21 = p2 - p1;
  253. float d1343 = dot( p13, p43 );
  254. float d4321 = dot( p43, p21 );
  255. float d1321 = dot( p13, p21 );
  256. float d4343 = dot( p43, p43 );
  257. float d2121 = dot( p21, p21 );
  258. float denom = d2121 * d4343 - d4321 * d4321;
  259. float numer = d1343 * d4321 - d1321 * d4343;
  260. mua = numer / denom;
  261. mua = clamp( mua, 0.0, 1.0 );
  262. mub = ( d1343 + d4321 * ( mua ) ) / d4343;
  263. mub = clamp( mub, 0.0, 1.0 );
  264. return vec2( mua, mub );
  265. }
  266. void main() {
  267. #include <clipping_planes_fragment>
  268. /*#ifdef USE_DASH
  269. if ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; // discard endcaps
  270. if ( mod( vLineDistance + dashOffset, dashSize + gapSize ) > dashSize ) discard; // todo - FIX
  271. #endif*/
  272. #ifdef USE_DASH
  273. if ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; // discard endcaps
  274. bool unvisible = mod( vLineDistance + dashOffset, dashSize + gapSize ) > dashSize;
  275. //加
  276. #ifdef DASH_with_depth
  277. #else
  278. if (unvisible) discard; // todo - FIX
  279. #endif
  280. #endif
  281. float alpha = opacity;
  282. #ifdef WORLD_UNITS
  283. // Find the closest points on the view ray and the line segment
  284. vec3 rayEnd = normalize( worldPos.xyz ) * 1e5;
  285. vec3 lineDir = worldEnd - worldStart;
  286. vec2 params = closestLineToLine( worldStart, worldEnd, vec3( 0.0, 0.0, 0.0 ), rayEnd );
  287. vec3 p1 = worldStart + lineDir * params.x;
  288. vec3 p2 = rayEnd * params.y;
  289. vec3 delta = p1 - p2;
  290. float len = length( delta );
  291. float norm = len / lineWidth;
  292. #ifndef USE_DASH
  293. #ifdef USE_ALPHA_TO_COVERAGE
  294. float dnorm = fwidth( norm );
  295. alpha = 1.0 - smoothstep( 0.5 - dnorm, 0.5 + dnorm, norm );
  296. #else
  297. if ( norm > 0.5 ) {
  298. discard;
  299. }
  300. #endif
  301. #endif
  302. #else
  303. #ifdef USE_ALPHA_TO_COVERAGE
  304. // artifacts appear on some hardware if a derivative is taken within a conditional
  305. float a = vUv.x;
  306. float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
  307. float len2 = a * a + b * b;
  308. float dlen = fwidth( len2 );
  309. if ( abs( vUv.y ) > 1.0 ) {
  310. alpha = 1.0 - smoothstep( 1.0 - dlen, 1.0 + dlen, len2 );
  311. }
  312. #else
  313. if ( abs( vUv.y ) > 1.0 ) {
  314. float a = vUv.x;
  315. float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
  316. float len2 = a * a + b * b;
  317. if ( len2 > 1.0 ) discard;
  318. }
  319. #endif
  320. #endif
  321. vec4 diffuseColor = vec4( diffuse, alpha );
  322. //加
  323. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  324. float mixFactor = 0.0;
  325. float clipFactor = 0.0;
  326. float fragDepth = convertToLinear(gl_FragCoord.z);
  327. //gl_FragCoord大小为 viewport client大小
  328. vec2 depthTxtCoords = vec2(gl_FragCoord.x - viewportOffset.x, gl_FragCoord.y - viewportOffset.y) / resolution;
  329. float textureDepth = convertToLinear(texture2D(depthTexture, depthTxtCoords).r);
  330. float delta = fragDepth - textureDepth;
  331. if (delta > 0.0)
  332. {
  333. mixFactor = clamp(delta / occlusionDistance, 0.0, 1.0);
  334. clipFactor = clamp(delta / clipDistance, 0.0, maxClipFactor);
  335. }
  336. if (clipFactor == 1.0)
  337. {
  338. discard;
  339. }
  340. vec4 backColor_ = vec4(backColor, opacity); //vec4(0.8,0.8,0.8, 0.8*opacity);
  341. #ifdef DASH_with_depth
  342. // 只在被遮住的部分显示虚线, 所以若同时是虚线不可见部分和被遮住时, a为0
  343. if(unvisible) backColor_.a = 0.0;
  344. #endif
  345. //vec4 diffuseColor = vec4(mix(diffuse, backColor_, mixFactor), opacity*(1.0 - clipFactor));
  346. diffuseColor = mix(diffuseColor, backColor_ , mixFactor);
  347. diffuseColor.a *= (1.0 - clipFactor);
  348. #endif
  349. #include <logdepthbuf_fragment>
  350. #include <color_fragment>
  351. //gl_FragColor = vec4( diffuseColor.rgb, alpha );
  352. gl_FragColor = vec4( diffuseColor.rgb, diffuseColor.a );
  353. #include <tonemapping_fragment>
  354. #include <encodings_fragment>
  355. #include <fog_fragment>
  356. #include <premultiplied_alpha_fragment>
  357. }
  358. `
  359. };
  360. class LineMaterial extends ShaderMaterial {
  361. constructor( parameters ) {
  362. super( {
  363. type: 'LineMaterial',
  364. uniforms: UniformsUtils.clone( ShaderLib[ 'line' ].uniforms ),
  365. vertexShader: ShaderLib[ 'line' ].vertexShader,
  366. fragmentShader: ShaderLib[ 'line' ].fragmentShader,
  367. clipping: true // required for clipping support
  368. } );
  369. this.isLineMaterial = true;
  370. this.lineWidth_ = 0
  371. this.supportExtDepth = parameters.supportExtDepth
  372. this.depthTestWhenPick = false //pick时是否识别点云等
  373. if(parameters.color){
  374. this.color = new Color(parameters.color)
  375. }
  376. if(parameters.backColor){
  377. this.uniforms.backColor.value = new Color(parameters.backColor)
  378. }
  379. if(parameters.clipDistance){
  380. this.uniforms.clipDistance.value = parameters.clipDistance
  381. }
  382. if(parameters.occlusionDistance){
  383. this.uniforms.occlusionDistance.value = parameters.occlusionDistance
  384. }
  385. if(parameters.maxClipFactor){
  386. this.uniforms.maxClipFactor.value = parameters.maxClipFactor
  387. }
  388. Object.defineProperties( this, {
  389. color: {
  390. enumerable: true,
  391. get: function () {
  392. return this.uniforms.diffuse.value;
  393. },
  394. set: function ( value ) {
  395. this.uniforms.diffuse.value = value;
  396. }
  397. },
  398. worldUnits: {
  399. enumerable: true,
  400. get: function () {
  401. return 'WORLD_UNITS' in this.defines;
  402. },
  403. set: function ( value ) {
  404. if ( value === true ) {
  405. this.defines.WORLD_UNITS = '';
  406. } else {
  407. delete this.defines.WORLD_UNITS;
  408. }
  409. }
  410. },
  411. lineWidth: {
  412. enumerable: true,
  413. get: function () {
  414. return this.lineWidth_;//this.uniforms.lineWidth.value;
  415. },
  416. set: function ( value ) {
  417. this.uniforms.lineWidth.value = value * window.devicePixelRatio;
  418. this.lineWidth_ = value
  419. }
  420. },
  421. dashed: {
  422. enumerable: true,
  423. get: function () {
  424. return Boolean( 'USE_DASH' in this.defines );
  425. },
  426. set( value ) {
  427. if ( Boolean( value ) !== Boolean( 'USE_DASH' in this.defines ) ) {
  428. this.needsUpdate = true;
  429. }
  430. if ( value === true ) {
  431. this.defines.USE_DASH = '';
  432. } else {
  433. delete this.defines.USE_DASH;
  434. }
  435. }
  436. },
  437. dashScale: {
  438. enumerable: true,
  439. get: function () {
  440. return this.uniforms.dashScale.value;
  441. },
  442. set: function ( value ) {
  443. this.uniforms.dashScale.value = value;
  444. }
  445. },
  446. dashSize: {
  447. enumerable: true,
  448. get: function () {
  449. return this.uniforms.dashSize.value;
  450. },
  451. set: function ( value ) {
  452. this.uniforms.dashSize.value = value;
  453. }
  454. },
  455. dashOffset: {
  456. enumerable: true,
  457. get: function () {
  458. return this.uniforms.dashOffset.value;
  459. },
  460. set: function ( value ) {
  461. this.uniforms.dashOffset.value = value;
  462. }
  463. },
  464. gapSize: {
  465. enumerable: true,
  466. get: function () {
  467. return this.uniforms.gapSize.value;
  468. },
  469. set: function ( value ) {
  470. this.uniforms.gapSize.value = value;
  471. }
  472. },
  473. opacity: {
  474. enumerable: true,
  475. get: function () {
  476. return this.uniforms.opacity.value;
  477. },
  478. set: function ( value ) {
  479. this.uniforms.opacity.value = value;
  480. }
  481. },
  482. resolution: {
  483. enumerable: true,
  484. get: function () {
  485. return this.uniforms.resolution.value;
  486. },
  487. set: function ( value ) {
  488. this.uniforms.resolution.value.copy( value );
  489. }
  490. },
  491. alphaToCoverage: {
  492. enumerable: true,
  493. get: function () {
  494. return Boolean( 'USE_ALPHA_TO_COVERAGE' in this.defines );
  495. },
  496. set: function ( value ) {
  497. if ( Boolean( value ) !== Boolean( 'USE_ALPHA_TO_COVERAGE' in this.defines ) ) {
  498. this.needsUpdate = true;
  499. }
  500. if ( value === true ) {
  501. this.defines.USE_ALPHA_TO_COVERAGE = '';
  502. this.extensions.derivatives = true;
  503. } else {
  504. delete this.defines.USE_ALPHA_TO_COVERAGE;
  505. this.extensions.derivatives = false;
  506. }
  507. }
  508. }
  509. ,
  510. useDepth:{//add
  511. enumerable: true,
  512. get: function () {
  513. return 'useDepth' in this.defines
  514. },
  515. set: function ( value ) {
  516. value = value && !!this.supportExtDepth
  517. if(value != this.useDepth){
  518. if(value){
  519. this.defines.useDepth = ''
  520. this.updateDepthParams()
  521. }else{
  522. delete this.defines.useDepth
  523. }
  524. this.needsUpdate = true
  525. }
  526. }
  527. },
  528. dashWithDepth:{//add
  529. enumerable: true,
  530. get: function () {
  531. return 'DASH_with_depth' in this.defines
  532. },
  533. set: function ( value ) {
  534. value = value && !!this.supportExtDepth
  535. if(value != this.dashWithDepth){
  536. if(value){
  537. this.defines.DASH_with_depth = ''
  538. }else{
  539. delete this.defines.DASH_with_depth
  540. }
  541. this.needsUpdate = true
  542. }
  543. }
  544. },
  545. } );
  546. this.setValues( parameters );
  547. let setSize = (e)=>{
  548. let viewport = e.viewport
  549. let viewportOffset = viewport.offset || new Vector2()
  550. this.uniforms.resolution.value.copy(viewport.resolution)
  551. this.uniforms.viewportOffset.value.copy(viewportOffset)
  552. this.uniforms.devicePixelRatio.value = window.devicePixelRatio
  553. this.lineWidth = this.lineWidth_
  554. }
  555. let viewport = viewer.mainViewport;
  556. setSize({viewport})
  557. viewer.addEventListener('resize',(e)=>{
  558. setSize(e)
  559. })
  560. if(this.supportExtDepth){
  561. //add
  562. this.updateDepthParams()
  563. /* viewer.addEventListener('camera_changed', (e)=>{
  564. if(e.viewport.name != 'mapViewport') this.updateDepthParams(e)
  565. }) */
  566. viewer.addEventListener("render.begin", (e)=>{//before render 如果有大于两个viewport的话,不同viewport用不同的depthTex
  567. if(e.viewport.camera.isPerspectiveCamera) this.updateDepthParams(e)
  568. })
  569. }
  570. }
  571. updateDepthParams(e={}){
  572. if(this.useDepth){
  573. var viewport = e.viewport || viewer.mainViewport;
  574. var camera = viewport.camera;
  575. this.uniforms.depthTexture.value = viewer.getPRenderer().getRtEDL(viewport).depthTexture //viewer.getPRenderer().rtEDL.depthTexture
  576. this.uniforms.nearPlane.value = camera.near; //似乎因为这个所以对OrthographicCamera 无效
  577. this.uniforms.farPlane.value = camera.far;
  578. }
  579. }
  580. }
  581. export { LineMaterial };