LineMaterial.js 7.1 KB

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