LineMaterial.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. float r = pow(1.0 - clipFactor, 3.0); //更透明些
  370. diffuseColor.a *= r;
  371. #endif
  372. #endif
  373. #include <logdepthbuf_fragment>
  374. #include <color_fragment>
  375. //gl_FragColor = vec4( diffuseColor.rgb, alpha );
  376. gl_FragColor = diffuseColor;
  377. #include <tonemapping_fragment>
  378. #include <encodings_fragment>
  379. #include <fog_fragment>
  380. #include <premultiplied_alpha_fragment>
  381. }
  382. `
  383. };
  384. class LineMaterial extends ShaderMaterial {
  385. constructor( parameters ) {
  386. let {vs,fs} = Potree.Common.changeShaderToWebgl2(ShaderLib[ 'line' ].vertexShader, ShaderLib[ 'line' ].fragmentShader, 'ShaderMaterial')
  387. super( {
  388. type: 'LineMaterial',
  389. uniforms: UniformsUtils.clone( ShaderLib[ 'line' ].uniforms ),
  390. vertexShader: vs,
  391. fragmentShader: fs,
  392. clipping: true // required for clipping support
  393. } );
  394. this.isLineMaterial = true;
  395. this.lineWidth_ = 0
  396. this.supportExtDepth = parameters.supportExtDepth
  397. this.depthTestWhenPick = false //pick时是否识别点云等
  398. if(parameters.color){
  399. this.color = new Color(parameters.color)
  400. }
  401. if(parameters.backColor){
  402. this.uniforms.backColor.value = new Color(parameters.backColor)
  403. }
  404. if(parameters.clipDistance){
  405. this.uniforms.clipDistance.value = parameters.clipDistance
  406. }
  407. if(parameters.occlusionDistance){
  408. this.uniforms.occlusionDistance.value = parameters.occlusionDistance
  409. }
  410. if(parameters.maxClipFactor){
  411. this.uniforms.maxClipFactor.value = parameters.maxClipFactor
  412. }
  413. Object.defineProperties( this, {
  414. color: {
  415. enumerable: true,
  416. get: function () {
  417. return this.uniforms.diffuse.value;
  418. },
  419. set: function ( value ) {
  420. this.uniforms.diffuse.value = value;
  421. }
  422. },
  423. worldUnits: {
  424. enumerable: true,
  425. get: function () {
  426. return 'WORLD_UNITS' in this.defines;
  427. },
  428. set: function ( value ) {
  429. if ( value === true ) {
  430. this.defines.WORLD_UNITS = '';
  431. } else {
  432. delete this.defines.WORLD_UNITS;
  433. }
  434. }
  435. },
  436. lineWidth: {
  437. enumerable: true,
  438. get: function () {
  439. return this.lineWidth_;//this.uniforms.lineWidth.value;
  440. },
  441. set: function ( value ) {
  442. this.uniforms.lineWidth.value = value * window.devicePixelRatio;
  443. this.lineWidth_ = value
  444. }
  445. },
  446. dashed: {
  447. enumerable: true,
  448. get: function () {
  449. return Boolean( 'USE_DASH' in this.defines );
  450. },
  451. set( value ) {
  452. if ( Boolean( value ) !== Boolean( 'USE_DASH' in this.defines ) ) {
  453. this.needsUpdate = true;
  454. }
  455. if ( value === true ) {
  456. this.defines.USE_DASH = '';
  457. } else {
  458. delete this.defines.USE_DASH;
  459. }
  460. }
  461. },
  462. dashScale: {
  463. enumerable: true,
  464. get: function () {
  465. return this.uniforms.dashScale.value;
  466. },
  467. set: function ( value ) {
  468. this.uniforms.dashScale.value = value;
  469. }
  470. },
  471. dashSize: {
  472. enumerable: true,
  473. get: function () {
  474. return this.uniforms.dashSize.value;
  475. },
  476. set: function ( value ) {
  477. this.uniforms.dashSize.value = value;
  478. }
  479. },
  480. dashOffset: {
  481. enumerable: true,
  482. get: function () {
  483. return this.uniforms.dashOffset.value;
  484. },
  485. set: function ( value ) {
  486. this.uniforms.dashOffset.value = value;
  487. }
  488. },
  489. gapSize: {
  490. enumerable: true,
  491. get: function () {
  492. return this.uniforms.gapSize.value;
  493. },
  494. set: function ( value ) {
  495. this.uniforms.gapSize.value = value;
  496. }
  497. },
  498. opacity: {
  499. enumerable: true,
  500. get: function () {
  501. return this.uniforms.opacity.value;
  502. },
  503. set: function ( value ) {
  504. this.uniforms.opacity.value = value;
  505. }
  506. },
  507. resolution: {
  508. enumerable: true,
  509. get: function () {
  510. return this.uniforms.resolution.value;
  511. },
  512. set: function ( value ) {
  513. this.uniforms.resolution.value.copy( value );
  514. }
  515. },
  516. alphaToCoverage: {
  517. enumerable: true,
  518. get: function () {
  519. return Boolean( 'USE_ALPHA_TO_COVERAGE' in this.defines );
  520. },
  521. set: function ( value ) {
  522. if ( Boolean( value ) !== Boolean( 'USE_ALPHA_TO_COVERAGE' in this.defines ) ) {
  523. this.needsUpdate = true;
  524. }
  525. if ( value === true ) {
  526. this.defines.USE_ALPHA_TO_COVERAGE = '';
  527. this.extensions.derivatives = true;
  528. } else {
  529. delete this.defines.USE_ALPHA_TO_COVERAGE;
  530. this.extensions.derivatives = false;
  531. }
  532. }
  533. },
  534. dashWithDepth:{//add
  535. enumerable: true,
  536. get: function () {
  537. return 'DASH_with_depth' in this.defines
  538. },
  539. set: function ( value ) {
  540. value = value && !!this.supportExtDepth
  541. if(value != this.dashWithDepth){
  542. if(value){
  543. this.defines.DASH_with_depth = ''
  544. }else{
  545. delete this.defines.DASH_with_depth
  546. }
  547. this.needsUpdate = true
  548. }
  549. }
  550. },
  551. } );
  552. this.events = {
  553. setSize:(e)=>{//如果出现横条状的异常,往往是viewportOffset出错 //地图不需要
  554. let viewport = e.viewport
  555. //console.log(viewport.name, viewport.resolution2)
  556. this.uniforms.resolution.value.copy(viewport.resolution2)
  557. this.uniforms.devicePixelRatio.value = window.devicePixelRatio
  558. this.lineWidth = this.lineWidth_ //update
  559. if(!this.realUseDepth || !e.viewport)return
  560. let viewportOffset = viewport.offset || new THREE.Vector2()
  561. this.uniforms.viewportOffset.value.copy(viewportOffset)
  562. },
  563. render:(e)=>{//before render 如果有大于两个viewport的话,不同viewport用不同的depthTex
  564. this.useDepth && this.updateDepthParams(e)
  565. var viewport = e.viewport || viewer.mainViewport;
  566. if(viewport != this.lastViewport){ //当mapViewer要渲染测量线后,就需要变viewport
  567. this.events.setSize({viewport})
  568. }
  569. this.lastViewport = viewport
  570. }
  571. }
  572. this.setValues( parameters );
  573. let viewport = viewer.mainViewport;
  574. this.events.setSize({viewport})
  575. viewer.addEventListener('resize', this.events.setSize)
  576. viewer.addEventListener("render.begin", this.events.render)
  577. }
  578. get useDepth(){
  579. return this.useDepth_
  580. }
  581. set useDepth(value){
  582. value = value && this.supportExtDepth //如果不支持 EXT_DEPTH 的话会失效
  583. if(this.useDepth_ != value){
  584. this.setRealDepth(value)
  585. this.useDepth_ = value
  586. }
  587. }
  588. setRealDepth(useDepth, viewport){//确实使用到depthTex
  589. if(this.realUseDepth != useDepth){
  590. if(useDepth ){
  591. this.defines.useDepth = ''
  592. }else{
  593. delete this.defines.useDepth
  594. }
  595. this.realUseDepth = useDepth
  596. if(this.autoDepthTest)this.depthWrite = this.depthTest = !useDepth //如果useDepth = false,使用原始的depthTest
  597. this.needsUpdate = true
  598. if(!viewport)viewport = viewer.mainViewport //暂时这么设置
  599. useDepth && this.events.setSize({viewport})
  600. }
  601. }
  602. set fadeFar(far){
  603. let needsUpdate = (!this.fadeFar ) != (!far) //null为全部范围可见
  604. //console.log('fadeFar needsUpdate', needsUpdate)
  605. if(far){
  606. this.defines.FadeFar = true
  607. this.uniforms.fadeFar.value = far
  608. }else{
  609. delete this.defines.FadeFar
  610. }
  611. needsUpdate && (this.needsUpdate = true)
  612. }
  613. get fadeFar(){
  614. return 'FadeFar' in this.defines && this.uniforms.fadeFar.value
  615. }
  616. updateDepthParams(e={}){
  617. var viewport = e.viewport || viewer.mainViewport;
  618. var camera = viewport.camera;
  619. let hasDepth = this.useDepth && camera.isPerspectiveCamera &&
  620. (Potree.settings.pointEnableRT || Potree.settings.displayMode == 'showPanos' || viewer.useEDL)
  621. this.setRealDepth(hasDepth, viewport)
  622. if(hasDepth){
  623. this.uniforms.depthTexture.value = viewer.getPRenderer().getRtEDL(viewport).depthTexture //其实只赋值一次就行
  624. }
  625. //hasDepth or FadeFar:
  626. this.uniforms.nearPlane.value = camera.near;
  627. this.uniforms.farPlane.value = camera.far;
  628. //this.uniforms.uUseOrthographicCamera.value = !camera.isPerspectiveCamera
  629. }
  630. }
  631. export { LineMaterial };