LineMaterial.js 7.0 KB

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