LineMaterial.js 21 KB

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