LineMaterial.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. ( function () {
  2. /**
  3. * parameters = {
  4. * color: <hex>,
  5. * linewidth: <float>,
  6. * dashed: <boolean>,
  7. * dashScale: <float>,
  8. * dashSize: <float>,
  9. * gapSize: <float>,
  10. * resolution: <Vector2>, // to be set by renderer
  11. * }
  12. */
  13. THREE.UniformsLib.line = {
  14. worldUnits: {
  15. value: 1
  16. },
  17. linewidth: {
  18. value: 1
  19. },
  20. resolution: {
  21. value: new THREE.Vector2( 1, 1 )
  22. },
  23. dashScale: {
  24. value: 1
  25. },
  26. dashSize: {
  27. value: 1
  28. },
  29. gapSize: {
  30. value: 1
  31. } // todo FIX - maybe change to totalSize
  32. };
  33. THREE.ShaderLib[ 'line' ] = {
  34. uniforms: THREE.UniformsUtils.merge( [ THREE.UniformsLib.common, THREE.UniformsLib.fog, THREE.UniformsLib.line ] ),
  35. vertexShader:
  36. /* glsl */
  37. `
  38. #include <common>
  39. #include <color_pars_vertex>
  40. #include <fog_pars_vertex>
  41. #include <logdepthbuf_pars_vertex>
  42. #include <clipping_planes_pars_vertex>
  43. uniform float linewidth;
  44. uniform vec2 resolution;
  45. attribute vec3 instanceStart;
  46. attribute vec3 instanceEnd;
  47. attribute vec3 instanceColorStart;
  48. attribute vec3 instanceColorEnd;
  49. #ifdef WORLD_UNITS
  50. varying vec4 worldPos;
  51. varying vec3 worldStart;
  52. varying vec3 worldEnd;
  53. #ifdef USE_DASH
  54. varying vec2 vUv;
  55. #endif
  56. #else
  57. varying vec2 vUv;
  58. #endif
  59. #ifdef USE_DASH
  60. uniform float dashScale;
  61. attribute float instanceDistanceStart;
  62. attribute float instanceDistanceEnd;
  63. varying float vLineDistance;
  64. #endif
  65. void trimSegment( const in vec4 start, inout vec4 end ) {
  66. // trim end segment so it terminates between the camera plane and the near plane
  67. // conservative estimate of the near plane
  68. float a = projectionMatrix[ 2 ][ 2 ]; // 3nd entry in 3th column
  69. float b = projectionMatrix[ 3 ][ 2 ]; // 3nd entry in 4th column
  70. float nearEstimate = - 0.5 * b / a;
  71. float alpha = ( nearEstimate - start.z ) / ( end.z - start.z );
  72. end.xyz = mix( start.xyz, end.xyz, alpha );
  73. }
  74. void main() {
  75. #ifdef USE_COLOR
  76. vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
  77. #endif
  78. #ifdef USE_DASH
  79. vLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd;
  80. vUv = uv;
  81. #endif
  82. float aspect = resolution.x / resolution.y;
  83. // camera space
  84. vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
  85. vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );
  86. #ifdef WORLD_UNITS
  87. worldStart = start.xyz;
  88. worldEnd = end.xyz;
  89. #else
  90. vUv = uv;
  91. #endif
  92. // special case for perspective projection, and segments that terminate either in, or behind, the camera plane
  93. // clearly the gpu firmware has a way of addressing this issue when projecting into ndc space
  94. // but we need to perform ndc-space calculations in the shader, so we must address this issue directly
  95. // perhaps there is a more elegant solution -- WestLangley
  96. bool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 ); // 4th entry in the 3rd column
  97. if ( perspective ) {
  98. if ( start.z < 0.0 && end.z >= 0.0 ) {
  99. trimSegment( start, end );
  100. } else if ( end.z < 0.0 && start.z >= 0.0 ) {
  101. trimSegment( end, start );
  102. }
  103. }
  104. // clip space
  105. vec4 clipStart = projectionMatrix * start;
  106. vec4 clipEnd = projectionMatrix * end;
  107. // ndc space
  108. vec3 ndcStart = clipStart.xyz / clipStart.w;
  109. vec3 ndcEnd = clipEnd.xyz / clipEnd.w;
  110. // direction
  111. vec2 dir = ndcEnd.xy - ndcStart.xy;
  112. // account for clip-space aspect ratio
  113. dir.x *= aspect;
  114. dir = normalize( dir );
  115. #ifdef WORLD_UNITS
  116. // get the offset direction as perpendicular to the view vector
  117. vec3 worldDir = normalize( end.xyz - start.xyz );
  118. vec3 offset;
  119. if ( position.y < 0.5 ) {
  120. offset = normalize( cross( start.xyz, worldDir ) );
  121. } else {
  122. offset = normalize( cross( end.xyz, worldDir ) );
  123. }
  124. // sign flip
  125. if ( position.x < 0.0 ) offset *= - 1.0;
  126. float forwardOffset = dot( worldDir, vec3( 0.0, 0.0, 1.0 ) );
  127. // don't extend the line if we're rendering dashes because we
  128. // won't be rendering the endcaps
  129. #ifndef USE_DASH
  130. // extend the line bounds to encompass endcaps
  131. start.xyz += - worldDir * linewidth * 0.5;
  132. end.xyz += worldDir * linewidth * 0.5;
  133. // shift the position of the quad so it hugs the forward edge of the line
  134. offset.xy -= dir * forwardOffset;
  135. offset.z += 0.5;
  136. #endif
  137. // endcaps
  138. if ( position.y > 1.0 || position.y < 0.0 ) {
  139. offset.xy += dir * 2.0 * forwardOffset;
  140. }
  141. // adjust for linewidth
  142. offset *= linewidth * 0.5;
  143. // set the world position
  144. worldPos = ( position.y < 0.5 ) ? start : end;
  145. worldPos.xyz += offset;
  146. // project the worldpos
  147. vec4 clip = projectionMatrix * worldPos;
  148. // shift the depth of the projected points so the line
  149. // segements overlap neatly
  150. vec3 clipPose = ( position.y < 0.5 ) ? ndcStart : ndcEnd;
  151. clip.z = clipPose.z * clip.w;
  152. #else
  153. vec2 offset = vec2( dir.y, - dir.x );
  154. // undo aspect ratio adjustment
  155. dir.x /= aspect;
  156. offset.x /= aspect;
  157. // sign flip
  158. if ( position.x < 0.0 ) offset *= - 1.0;
  159. // endcaps
  160. if ( position.y < 0.0 ) {
  161. offset += - dir;
  162. } else if ( position.y > 1.0 ) {
  163. offset += dir;
  164. }
  165. // adjust for linewidth
  166. offset *= linewidth;
  167. // adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ...
  168. offset /= resolution.y;
  169. // select end
  170. vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;
  171. // back to clip space
  172. offset *= clip.w;
  173. clip.xy += offset;
  174. #endif
  175. gl_Position = clip;
  176. vec4 mvPosition = ( position.y < 0.5 ) ? start : end; // this is an approximation
  177. #include <logdepthbuf_vertex>
  178. #include <clipping_planes_vertex>
  179. #include <fog_vertex>
  180. }
  181. `,
  182. fragmentShader:
  183. /* glsl */
  184. `
  185. uniform vec3 diffuse;
  186. uniform float opacity;
  187. uniform float linewidth;
  188. #ifdef USE_DASH
  189. uniform float dashSize;
  190. uniform float gapSize;
  191. #endif
  192. varying float vLineDistance;
  193. #ifdef WORLD_UNITS
  194. varying vec4 worldPos;
  195. varying vec3 worldStart;
  196. varying vec3 worldEnd;
  197. #ifdef USE_DASH
  198. varying vec2 vUv;
  199. #endif
  200. #else
  201. varying vec2 vUv;
  202. #endif
  203. #include <common>
  204. #include <color_pars_fragment>
  205. #include <fog_pars_fragment>
  206. #include <logdepthbuf_pars_fragment>
  207. #include <clipping_planes_pars_fragment>
  208. vec2 closestLineToLine(vec3 p1, vec3 p2, vec3 p3, vec3 p4) {
  209. float mua;
  210. float mub;
  211. vec3 p13 = p1 - p3;
  212. vec3 p43 = p4 - p3;
  213. vec3 p21 = p2 - p1;
  214. float d1343 = dot( p13, p43 );
  215. float d4321 = dot( p43, p21 );
  216. float d1321 = dot( p13, p21 );
  217. float d4343 = dot( p43, p43 );
  218. float d2121 = dot( p21, p21 );
  219. float denom = d2121 * d4343 - d4321 * d4321;
  220. float numer = d1343 * d4321 - d1321 * d4343;
  221. mua = numer / denom;
  222. mua = clamp( mua, 0.0, 1.0 );
  223. mub = ( d1343 + d4321 * ( mua ) ) / d4343;
  224. mub = clamp( mub, 0.0, 1.0 );
  225. return vec2( mua, mub );
  226. }
  227. void main() {
  228. #include <clipping_planes_fragment>
  229. #ifdef USE_DASH
  230. if ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; // discard endcaps
  231. if ( mod( vLineDistance, dashSize + gapSize ) > dashSize ) discard; // todo - FIX
  232. #endif
  233. float alpha = opacity;
  234. #ifdef WORLD_UNITS
  235. // Find the closest points on the view ray and the line segment
  236. vec3 rayEnd = normalize( worldPos.xyz ) * 1e5;
  237. vec3 lineDir = worldEnd - worldStart;
  238. vec2 params = closestLineToLine( worldStart, worldEnd, vec3( 0.0, 0.0, 0.0 ), rayEnd );
  239. vec3 p1 = worldStart + lineDir * params.x;
  240. vec3 p2 = rayEnd * params.y;
  241. vec3 delta = p1 - p2;
  242. float len = length( delta );
  243. float norm = len / linewidth;
  244. #ifndef USE_DASH
  245. #ifdef USE_ALPHA_TO_COVERAGE
  246. float dnorm = fwidth( norm );
  247. alpha = 1.0 - smoothstep( 0.5 - dnorm, 0.5 + dnorm, norm );
  248. #else
  249. if ( norm > 0.5 ) {
  250. discard;
  251. }
  252. #endif
  253. #endif
  254. #else
  255. #ifdef USE_ALPHA_TO_COVERAGE
  256. // artifacts appear on some hardware if a derivative is taken within a conditional
  257. float a = vUv.x;
  258. float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
  259. float len2 = a * a + b * b;
  260. float dlen = fwidth( len2 );
  261. if ( abs( vUv.y ) > 1.0 ) {
  262. alpha = 1.0 - smoothstep( 1.0 - dlen, 1.0 + dlen, len2 );
  263. }
  264. #else
  265. if ( abs( vUv.y ) > 1.0 ) {
  266. float a = vUv.x;
  267. float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
  268. float len2 = a * a + b * b;
  269. if ( len2 > 1.0 ) discard;
  270. }
  271. #endif
  272. #endif
  273. vec4 diffuseColor = vec4( diffuse, alpha );
  274. #include <logdepthbuf_fragment>
  275. #include <color_fragment>
  276. gl_FragColor = vec4( diffuseColor.rgb, alpha );
  277. #include <tonemapping_fragment>
  278. #include <encodings_fragment>
  279. #include <fog_fragment>
  280. #include <premultiplied_alpha_fragment>
  281. }
  282. `
  283. };
  284. class LineMaterial extends THREE.ShaderMaterial {
  285. constructor( parameters ) {
  286. super( {
  287. type: 'LineMaterial',
  288. uniforms: THREE.UniformsUtils.clone( THREE.ShaderLib[ 'line' ].uniforms ),
  289. vertexShader: THREE.ShaderLib[ 'line' ].vertexShader,
  290. fragmentShader: THREE.ShaderLib[ 'line' ].fragmentShader,
  291. clipping: true // required for clipping support
  292. } );
  293. Object.defineProperties( this, {
  294. color: {
  295. enumerable: true,
  296. get: function () {
  297. return this.uniforms.diffuse.value;
  298. },
  299. set: function ( value ) {
  300. this.uniforms.diffuse.value = value;
  301. }
  302. },
  303. worldUnits: {
  304. enumerable: true,
  305. get: function () {
  306. return 'WORLD_UNITS' in this.defines;
  307. },
  308. set: function ( value ) {
  309. if ( value === true ) {
  310. this.defines.WORLD_UNITS = '';
  311. } else {
  312. delete this.defines.WORLD_UNITS;
  313. }
  314. }
  315. },
  316. linewidth: {
  317. enumerable: true,
  318. get: function () {
  319. return this.uniforms.linewidth.value;
  320. },
  321. set: function ( value ) {
  322. this.uniforms.linewidth.value = value;
  323. }
  324. },
  325. dashed: {
  326. enumerable: true,
  327. get: function () {
  328. return Boolean( 'USE_DASH' in this.defines );
  329. },
  330. set( value ) {
  331. if ( Boolean( value ) !== Boolean( 'USE_DASH' in this.defines ) ) {
  332. this.needsUpdate = true;
  333. }
  334. if ( value === true ) {
  335. this.defines.USE_DASH = '';
  336. } else {
  337. delete this.defines.USE_DASH;
  338. }
  339. }
  340. },
  341. dashScale: {
  342. enumerable: true,
  343. get: function () {
  344. return this.uniforms.dashScale.value;
  345. },
  346. set: function ( value ) {
  347. this.uniforms.dashScale.value = value;
  348. }
  349. },
  350. dashSize: {
  351. enumerable: true,
  352. get: function () {
  353. return this.uniforms.dashSize.value;
  354. },
  355. set: function ( value ) {
  356. this.uniforms.dashSize.value = value;
  357. }
  358. },
  359. dashOffset: {
  360. enumerable: true,
  361. get: function () {
  362. return this.uniforms.dashOffset.value;
  363. },
  364. set: function ( value ) {
  365. this.uniforms.dashOffset.value = value;
  366. }
  367. },
  368. gapSize: {
  369. enumerable: true,
  370. get: function () {
  371. return this.uniforms.gapSize.value;
  372. },
  373. set: function ( value ) {
  374. this.uniforms.gapSize.value = value;
  375. }
  376. },
  377. opacity: {
  378. enumerable: true,
  379. get: function () {
  380. return this.uniforms.opacity.value;
  381. },
  382. set: function ( value ) {
  383. this.uniforms.opacity.value = value;
  384. }
  385. },
  386. resolution: {
  387. enumerable: true,
  388. get: function () {
  389. return this.uniforms.resolution.value;
  390. },
  391. set: function ( value ) {
  392. this.uniforms.resolution.value.copy( value );
  393. }
  394. },
  395. alphaToCoverage: {
  396. enumerable: true,
  397. get: function () {
  398. return Boolean( 'USE_ALPHA_TO_COVERAGE' in this.defines );
  399. },
  400. set: function ( value ) {
  401. if ( Boolean( value ) !== Boolean( 'USE_ALPHA_TO_COVERAGE' in this.defines ) ) {
  402. this.needsUpdate = true;
  403. }
  404. if ( value === true ) {
  405. this.defines.USE_ALPHA_TO_COVERAGE = '';
  406. this.extensions.derivatives = true;
  407. } else {
  408. delete this.defines.USE_ALPHA_TO_COVERAGE;
  409. this.extensions.derivatives = false;
  410. }
  411. }
  412. }
  413. } );
  414. this.setValues( parameters );
  415. }
  416. }
  417. LineMaterial.prototype.isLineMaterial = true;
  418. THREE.LineMaterial = LineMaterial;
  419. } )();