LineMaterial.js 7.4 KB

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