LineMaterial.js 23 KB

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