LineMaterial.js 12 KB

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