2
0

LineMaterial.js 7.1 KB

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