LineMaterial.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. import {
  2. ShaderLib,
  3. ShaderMaterial,
  4. UniformsLib,
  5. UniformsUtils,
  6. Vector2
  7. } from '../../../build/three.module.js';
  8. /**
  9. * parameters = {
  10. * color: <hex>,
  11. * linewidth: <float>,
  12. * dashed: <boolean>,
  13. * dashScale: <float>,
  14. * dashSize: <float>,
  15. * dashOffset: <float>,
  16. * gapSize: <float>,
  17. * resolution: <Vector2>, // to be set by renderer
  18. * }
  19. */
  20. UniformsLib.line = {
  21. linewidth: { value: 1 },
  22. resolution: { value: new Vector2( 1, 1 ) },
  23. dashScale: { value: 1 },
  24. dashSize: { value: 1 },
  25. dashOffset: { value: 0 },
  26. gapSize: { value: 1 }, // todo FIX - maybe change to totalSize
  27. opacity: { value: 1 }
  28. };
  29. ShaderLib[ 'line' ] = {
  30. uniforms: UniformsUtils.merge( [
  31. UniformsLib.common,
  32. UniformsLib.fog,
  33. UniformsLib.line
  34. ] ),
  35. vertexShader:
  36. `
  37. #include <common>
  38. #include <color_pars_vertex>
  39. #include <fog_pars_vertex>
  40. #include <logdepthbuf_pars_vertex>
  41. #include <clipping_planes_pars_vertex>
  42. uniform float linewidth;
  43. uniform vec2 resolution;
  44. attribute vec3 instanceStart;
  45. attribute vec3 instanceEnd;
  46. attribute vec3 instanceColorStart;
  47. attribute vec3 instanceColorEnd;
  48. varying vec2 vUv;
  49. #ifdef USE_DASH
  50. uniform float dashScale;
  51. attribute float instanceDistanceStart;
  52. attribute float instanceDistanceEnd;
  53. varying float vLineDistance;
  54. #endif
  55. void trimSegment( const in vec4 start, inout vec4 end ) {
  56. // trim end segment so it terminates between the camera plane and the near plane
  57. // conservative estimate of the near plane
  58. float a = projectionMatrix[ 2 ][ 2 ]; // 3nd entry in 3th column
  59. float b = projectionMatrix[ 3 ][ 2 ]; // 3nd entry in 4th column
  60. float nearEstimate = - 0.5 * b / a;
  61. float alpha = ( nearEstimate - start.z ) / ( end.z - start.z );
  62. end.xyz = mix( start.xyz, end.xyz, alpha );
  63. }
  64. void main() {
  65. #ifdef USE_COLOR
  66. vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
  67. #endif
  68. #ifdef USE_DASH
  69. vLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd;
  70. #endif
  71. float aspect = resolution.x / resolution.y;
  72. vUv = uv;
  73. // camera space
  74. vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
  75. vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );
  76. // special case for perspective projection, and segments that terminate either in, or behind, the camera plane
  77. // clearly the gpu firmware has a way of addressing this issue when projecting into ndc space
  78. // but we need to perform ndc-space calculations in the shader, so we must address this issue directly
  79. // perhaps there is a more elegant solution -- WestLangley
  80. bool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 ); // 4th entry in the 3rd column
  81. if ( perspective ) {
  82. if ( start.z < 0.0 && end.z >= 0.0 ) {
  83. trimSegment( start, end );
  84. } else if ( end.z < 0.0 && start.z >= 0.0 ) {
  85. trimSegment( end, start );
  86. }
  87. }
  88. // clip space
  89. vec4 clipStart = projectionMatrix * start;
  90. vec4 clipEnd = projectionMatrix * end;
  91. // ndc space
  92. vec2 ndcStart = clipStart.xy / clipStart.w;
  93. vec2 ndcEnd = clipEnd.xy / clipEnd.w;
  94. // direction
  95. vec2 dir = ndcEnd - ndcStart;
  96. // account for clip-space aspect ratio
  97. dir.x *= aspect;
  98. dir = normalize( dir );
  99. // perpendicular to dir
  100. vec2 offset = vec2( dir.y, - dir.x );
  101. // undo aspect ratio adjustment
  102. dir.x /= aspect;
  103. offset.x /= aspect;
  104. // sign flip
  105. if ( position.x < 0.0 ) offset *= - 1.0;
  106. // endcaps
  107. if ( position.y < 0.0 ) {
  108. offset += - dir;
  109. } else if ( position.y > 1.0 ) {
  110. offset += dir;
  111. }
  112. // adjust for linewidth
  113. offset *= linewidth;
  114. // adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ...
  115. offset /= resolution.y;
  116. // select end
  117. vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;
  118. // back to clip space
  119. offset *= clip.w;
  120. clip.xy += offset;
  121. gl_Position = clip;
  122. vec4 mvPosition = ( position.y < 0.5 ) ? start : end; // this is an approximation
  123. #include <logdepthbuf_vertex>
  124. #include <clipping_planes_vertex>
  125. #include <fog_vertex>
  126. }
  127. `,
  128. fragmentShader:
  129. `
  130. uniform vec3 diffuse;
  131. uniform float opacity;
  132. #ifdef USE_DASH
  133. uniform float dashSize;
  134. uniform float dashOffset;
  135. uniform float gapSize;
  136. #endif
  137. varying float vLineDistance;
  138. #include <common>
  139. #include <color_pars_fragment>
  140. #include <fog_pars_fragment>
  141. #include <logdepthbuf_pars_fragment>
  142. #include <clipping_planes_pars_fragment>
  143. varying vec2 vUv;
  144. void main() {
  145. #include <clipping_planes_fragment>
  146. #ifdef USE_DASH
  147. if ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; // discard endcaps
  148. if ( mod( vLineDistance + dashOffset, dashSize + gapSize ) > dashSize ) discard; // todo - FIX
  149. #endif
  150. if ( abs( vUv.y ) > 1.0 ) {
  151. float a = vUv.x;
  152. float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
  153. float len2 = a * a + b * b;
  154. if ( len2 > 1.0 ) discard;
  155. }
  156. vec4 diffuseColor = vec4( diffuse, opacity );
  157. #include <logdepthbuf_fragment>
  158. #include <color_fragment>
  159. gl_FragColor = vec4( diffuseColor.rgb, diffuseColor.a );
  160. #include <tonemapping_fragment>
  161. #include <encodings_fragment>
  162. #include <fog_fragment>
  163. #include <premultiplied_alpha_fragment>
  164. }
  165. `
  166. };
  167. var LineMaterial = function ( parameters ) {
  168. ShaderMaterial.call( this, {
  169. type: 'LineMaterial',
  170. uniforms: UniformsUtils.clone( ShaderLib[ 'line' ].uniforms ),
  171. vertexShader: ShaderLib[ 'line' ].vertexShader,
  172. fragmentShader: ShaderLib[ 'line' ].fragmentShader,
  173. clipping: true // required for clipping support
  174. } );
  175. this.dashed = false;
  176. Object.defineProperties( this, {
  177. color: {
  178. enumerable: true,
  179. get: function () {
  180. return this.uniforms.diffuse.value;
  181. },
  182. set: function ( value ) {
  183. this.uniforms.diffuse.value = value;
  184. }
  185. },
  186. linewidth: {
  187. enumerable: true,
  188. get: function () {
  189. return this.uniforms.linewidth.value;
  190. },
  191. set: function ( value ) {
  192. this.uniforms.linewidth.value = value;
  193. }
  194. },
  195. dashScale: {
  196. enumerable: true,
  197. get: function () {
  198. return this.uniforms.dashScale.value;
  199. },
  200. set: function ( value ) {
  201. this.uniforms.dashScale.value = value;
  202. }
  203. },
  204. dashSize: {
  205. enumerable: true,
  206. get: function () {
  207. return this.uniforms.dashSize.value;
  208. },
  209. set: function ( value ) {
  210. this.uniforms.dashSize.value = value;
  211. }
  212. },
  213. dashOffset: {
  214. enumerable: true,
  215. get: function () {
  216. return this.uniforms.dashOffset.value;
  217. },
  218. set: function ( value ) {
  219. this.uniforms.dashOffset.value = value;
  220. }
  221. },
  222. gapSize: {
  223. enumerable: true,
  224. get: function () {
  225. return this.uniforms.gapSize.value;
  226. },
  227. set: function ( value ) {
  228. this.uniforms.gapSize.value = value;
  229. }
  230. },
  231. opacity: {
  232. enumerable: true,
  233. get: function () {
  234. return this.uniforms.opacity.value;
  235. },
  236. set: function ( value ) {
  237. this.uniforms.opacity.value = value;
  238. }
  239. },
  240. resolution: {
  241. enumerable: true,
  242. get: function () {
  243. return this.uniforms.resolution.value;
  244. },
  245. set: function ( value ) {
  246. this.uniforms.resolution.value.copy( value );
  247. }
  248. }
  249. } );
  250. this.setValues( parameters );
  251. };
  252. LineMaterial.prototype = Object.create( ShaderMaterial.prototype );
  253. LineMaterial.prototype.constructor = LineMaterial;
  254. LineMaterial.prototype.isLineMaterial = true;
  255. export { LineMaterial };