LineMaterial.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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: /* glsl */`
  36. #include <common>
  37. #include <color_pars_vertex>
  38. #include <fog_pars_vertex>
  39. #include <logdepthbuf_pars_vertex>
  40. #include <clipping_planes_pars_vertex>
  41. uniform float linewidth;
  42. uniform vec2 resolution;
  43. attribute vec3 instanceStart;
  44. attribute vec3 instanceEnd;
  45. attribute vec3 instanceColorStart;
  46. attribute vec3 instanceColorEnd;
  47. varying vec2 vUv;
  48. #ifdef USE_DASH
  49. uniform float dashScale;
  50. attribute float instanceDistanceStart;
  51. attribute float instanceDistanceEnd;
  52. varying float vLineDistance;
  53. #endif
  54. void trimSegment( const in vec4 start, inout vec4 end ) {
  55. // trim end segment so it terminates between the camera plane and the near plane
  56. // conservative estimate of the near plane
  57. float a = projectionMatrix[ 2 ][ 2 ]; // 3nd entry in 3th column
  58. float b = projectionMatrix[ 3 ][ 2 ]; // 3nd entry in 4th column
  59. float nearEstimate = - 0.5 * b / a;
  60. float alpha = ( nearEstimate - start.z ) / ( end.z - start.z );
  61. end.xyz = mix( start.xyz, end.xyz, alpha );
  62. }
  63. void main() {
  64. #ifdef USE_COLOR
  65. vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
  66. #endif
  67. #ifdef USE_DASH
  68. vLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd;
  69. #endif
  70. float aspect = resolution.x / resolution.y;
  71. vUv = uv;
  72. // camera space
  73. vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
  74. vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );
  75. // special case for perspective projection, and segments that terminate either in, or behind, the camera plane
  76. // clearly the gpu firmware has a way of addressing this issue when projecting into ndc space
  77. // but we need to perform ndc-space calculations in the shader, so we must address this issue directly
  78. // perhaps there is a more elegant solution -- WestLangley
  79. bool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 ); // 4th entry in the 3rd column
  80. if ( perspective ) {
  81. if ( start.z < 0.0 && end.z >= 0.0 ) {
  82. trimSegment( start, end );
  83. } else if ( end.z < 0.0 && start.z >= 0.0 ) {
  84. trimSegment( end, start );
  85. }
  86. }
  87. // clip space
  88. vec4 clipStart = projectionMatrix * start;
  89. vec4 clipEnd = projectionMatrix * end;
  90. // ndc space
  91. vec2 ndcStart = clipStart.xy / clipStart.w;
  92. vec2 ndcEnd = clipEnd.xy / clipEnd.w;
  93. // direction
  94. vec2 dir = ndcEnd - ndcStart;
  95. // account for clip-space aspect ratio
  96. dir.x *= aspect;
  97. dir = normalize( dir );
  98. // perpendicular to dir
  99. vec2 offset = vec2( dir.y, - dir.x );
  100. // undo aspect ratio adjustment
  101. dir.x /= aspect;
  102. offset.x /= aspect;
  103. // sign flip
  104. if ( position.x < 0.0 ) offset *= - 1.0;
  105. // endcaps
  106. if ( position.y < 0.0 ) {
  107. offset += - dir;
  108. } else if ( position.y > 1.0 ) {
  109. offset += dir;
  110. }
  111. // adjust for linewidth
  112. offset *= linewidth;
  113. // adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ...
  114. offset /= resolution.y;
  115. // select end
  116. vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;
  117. // back to clip space
  118. offset *= clip.w;
  119. clip.xy += offset;
  120. gl_Position = clip;
  121. vec4 mvPosition = ( position.y < 0.5 ) ? start : end; // this is an approximation
  122. #include <logdepthbuf_vertex>
  123. #include <clipping_planes_vertex>
  124. #include <fog_vertex>
  125. }`,
  126. fragmentShader: /* glsl */`
  127. uniform vec3 diffuse;
  128. uniform float opacity;
  129. #ifdef USE_DASH
  130. uniform float dashSize;
  131. uniform float dashOffset;
  132. uniform float gapSize;
  133. #endif
  134. varying float vLineDistance;
  135. #include <common>
  136. #include <color_pars_fragment>
  137. #include <fog_pars_fragment>
  138. #include <logdepthbuf_pars_fragment>
  139. #include <clipping_planes_pars_fragment>
  140. varying vec2 vUv;
  141. void main() {
  142. #include <clipping_planes_fragment>
  143. #ifdef USE_DASH
  144. if ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; // discard endcaps
  145. if ( mod( vLineDistance + dashOffset, dashSize + gapSize ) > dashSize ) discard; // todo - FIX
  146. #endif
  147. float alpha = 1.0;
  148. #ifdef ALPHA_TO_COVERAGE
  149. // artifacts appear on some hardware if a derivative is taken within a conditional
  150. float a = vUv.x;
  151. float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
  152. float len2 = a * a + b * b;
  153. float dlen = fwidth( len2 );
  154. if ( abs( vUv.y ) > 1.0 ) {
  155. alpha = 1.0 - smoothstep( 1.0 - dlen, 1.0 + dlen, len2 );
  156. }
  157. #else
  158. if ( abs( vUv.y ) > 1.0 ) {
  159. float a = vUv.x;
  160. float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0;
  161. float len2 = a * a + b * b;
  162. if ( len2 > 1.0 ) discard;
  163. }
  164. #endif
  165. vec4 diffuseColor = vec4( diffuse, opacity * alpha );
  166. #include <logdepthbuf_fragment>
  167. #include <color_fragment>
  168. gl_FragColor = diffuseColor;
  169. #include <tonemapping_fragment>
  170. #include <encodings_fragment>
  171. #include <fog_fragment>
  172. #include <premultiplied_alpha_fragment>
  173. }`
  174. };
  175. class LineMaterial extends ShaderMaterial {
  176. constructor( parameters ) {
  177. super( {
  178. type: 'LineMaterial',
  179. uniforms: UniformsUtils.clone( ShaderLib[ 'line' ].uniforms ),
  180. vertexShader: ShaderLib[ 'line' ].vertexShader,
  181. fragmentShader: ShaderLib[ 'line' ].fragmentShader,
  182. clipping: true // required for clipping support
  183. } );
  184. Object.defineProperties( this, {
  185. color: {
  186. enumerable: true,
  187. get: function () {
  188. return this.uniforms.diffuse.value;
  189. },
  190. set: function ( value ) {
  191. this.uniforms.diffuse.value = value;
  192. }
  193. },
  194. linewidth: {
  195. enumerable: true,
  196. get: function () {
  197. return this.uniforms.linewidth.value;
  198. },
  199. set: function ( value ) {
  200. this.uniforms.linewidth.value = value;
  201. }
  202. },
  203. dashed: {
  204. enumerable: true,
  205. get: function () {
  206. return Boolean( 'USE_DASH' in this.defines );
  207. },
  208. set( value ) {
  209. if ( Boolean( value ) !== Boolean( 'USE_DASH' in this.defines ) ) {
  210. this.needsUpdate = true;
  211. }
  212. if ( value === true ) {
  213. this.defines.USE_DASH = '';
  214. } else {
  215. delete this.defines.USE_DASH;
  216. }
  217. }
  218. },
  219. dashScale: {
  220. enumerable: true,
  221. get: function () {
  222. return this.uniforms.dashScale.value;
  223. },
  224. set: function ( value ) {
  225. this.uniforms.dashScale.value = value;
  226. }
  227. },
  228. dashSize: {
  229. enumerable: true,
  230. get: function () {
  231. return this.uniforms.dashSize.value;
  232. },
  233. set: function ( value ) {
  234. this.uniforms.dashSize.value = value;
  235. }
  236. },
  237. dashOffset: {
  238. enumerable: true,
  239. get: function () {
  240. return this.uniforms.dashOffset.value;
  241. },
  242. set: function ( value ) {
  243. this.uniforms.dashOffset.value = value;
  244. }
  245. },
  246. gapSize: {
  247. enumerable: true,
  248. get: function () {
  249. return this.uniforms.gapSize.value;
  250. },
  251. set: function ( value ) {
  252. this.uniforms.gapSize.value = value;
  253. }
  254. },
  255. opacity: {
  256. enumerable: true,
  257. get: function () {
  258. return this.uniforms.opacity.value;
  259. },
  260. set: function ( value ) {
  261. this.uniforms.opacity.value = value;
  262. }
  263. },
  264. resolution: {
  265. enumerable: true,
  266. get: function () {
  267. return this.uniforms.resolution.value;
  268. },
  269. set: function ( value ) {
  270. this.uniforms.resolution.value.copy( value );
  271. }
  272. },
  273. alphaToCoverage: {
  274. enumerable: true,
  275. get: function () {
  276. return Boolean( 'ALPHA_TO_COVERAGE' in this.defines );
  277. },
  278. set: function ( value ) {
  279. if ( Boolean( value ) !== Boolean( 'ALPHA_TO_COVERAGE' in this.defines ) ) {
  280. this.needsUpdate = true;
  281. }
  282. if ( value === true ) {
  283. this.defines.ALPHA_TO_COVERAGE = '';
  284. this.extensions.derivatives = true;
  285. } else {
  286. delete this.defines.ALPHA_TO_COVERAGE;
  287. this.extensions.derivatives = false;
  288. }
  289. }
  290. }
  291. } );
  292. this.setValues( parameters );
  293. }
  294. }
  295. LineMaterial.prototype.isLineMaterial = true;
  296. export { LineMaterial };