LineMaterial.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @author WestLangley / http://github.com/WestLangley
  3. *
  4. * parameters = {
  5. * color: <hex>,
  6. * linewidth: <float>,
  7. * resolution: <Vector2>, // to be set by renderer
  8. * }
  9. */
  10. THREE.UniformsLib.line = {
  11. linewidth: { value: 1 },
  12. resolution: { value: new THREE.Vector2( 1, 1 ) }
  13. };
  14. THREE.ShaderLib[ 'line' ] = {
  15. uniforms: THREE.UniformsUtils.merge( [
  16. THREE.UniformsLib.common,
  17. THREE.UniformsLib.fog,
  18. THREE.UniformsLib.line
  19. ] ),
  20. vertexShader:
  21. `
  22. #include <common>
  23. #include <color_pars_vertex>
  24. #include <fog_pars_vertex>
  25. #include <logdepthbuf_pars_vertex>
  26. #include <clipping_planes_pars_vertex>
  27. uniform float linewidth;
  28. uniform vec2 resolution;
  29. attribute vec3 instanceStart;
  30. attribute vec3 instanceEnd;
  31. attribute vec3 instanceColorStart;
  32. attribute vec3 instanceColorEnd;
  33. varying vec2 vUv;
  34. void main() {
  35. #ifdef USE_COLOR
  36. vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
  37. #endif
  38. float aspect = resolution.x / resolution.y;
  39. vUv = uv;
  40. // camera space
  41. vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
  42. vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );
  43. // clip space
  44. vec4 clipStart = projectionMatrix * start;
  45. vec4 clipEnd = projectionMatrix * end;
  46. // ndc space
  47. vec2 ndcStart = clipStart.xy / clipStart.w;
  48. vec2 ndcEnd = clipEnd.xy / clipEnd.w;
  49. // direction
  50. vec2 dir = ndcEnd - ndcStart;
  51. // account for clip-space aspect ratio
  52. dir.x *= aspect;
  53. dir = normalize( dir );
  54. // perpendicular to dir
  55. vec2 offset = vec2( dir.y, - dir.x );
  56. // undo aspect ratio adjustment
  57. dir.x /= aspect;
  58. offset.x /= aspect;
  59. // sign flip
  60. if ( position.x < 0.0 ) offset *= - 1.0;
  61. // endcaps
  62. if ( position.y < 0.0 ) {
  63. offset += - dir;
  64. } else if ( position.y > 1.0 ) {
  65. offset += dir;
  66. }
  67. // adjust for linewidth
  68. offset *= linewidth;
  69. // adjust for clip-space to screen-space conversion // maybe it should be based on viewport ...
  70. offset /= resolution.y;
  71. // select end
  72. vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;
  73. // back to clip space
  74. offset *= clip.w;
  75. clip.xy += offset;
  76. gl_Position = clip;
  77. #include <logdepthbuf_vertex>
  78. #include <worldpos_vertex>
  79. #include <clipping_planes_vertex>
  80. #include <fog_vertex>
  81. }
  82. `,
  83. fragmentShader:
  84. `
  85. uniform vec3 diffuse;
  86. uniform float opacity;
  87. #include <common>
  88. #include <color_pars_fragment>
  89. #include <fog_pars_fragment>
  90. #include <logdepthbuf_pars_fragment>
  91. #include <clipping_planes_pars_fragment>
  92. varying vec2 vUv;
  93. void main() {
  94. #include <clipping_planes_fragment>
  95. if ( vUv.y < 0.5 || vUv.y > 0.5 ) {
  96. float a = vUv.x - 0.5;
  97. float b = vUv.y - 0.5;
  98. float len2 = a * a + b * b;
  99. if ( len2 > 0.25 ) discard;
  100. }
  101. vec4 diffuseColor = vec4( diffuse, opacity );
  102. #include <logdepthbuf_fragment>
  103. #include <color_fragment>
  104. gl_FragColor = vec4( diffuseColor.rgb, diffuseColor.a );
  105. #include <premultiplied_alpha_fragment>
  106. #include <tonemapping_fragment>
  107. #include <encodings_fragment>
  108. #include <fog_fragment>
  109. }
  110. `
  111. };
  112. THREE.LineMaterial = function ( parameters ) {
  113. THREE.ShaderMaterial.call( this, {
  114. type: 'LineMaterial',
  115. side: THREE.DoubleSide, // for now. there is an issue with segments that terminate behind the camera
  116. uniforms: THREE.UniformsUtils.clone( THREE.ShaderLib[ 'line' ].uniforms ),
  117. vertexShader: THREE.ShaderLib[ 'line' ].vertexShader,
  118. fragmentShader: THREE.ShaderLib[ 'line' ].fragmentShader
  119. } );
  120. Object.defineProperties( this, {
  121. color: {
  122. enumerable: true,
  123. get: function () {
  124. return this.uniforms.diffuse.value;
  125. },
  126. set: function ( value ) {
  127. this.uniforms.diffuse.value = value;
  128. }
  129. },
  130. linewidth: {
  131. enumerable: true,
  132. get: function () {
  133. return this.uniforms.linewidth.value;
  134. },
  135. set: function ( value ) {
  136. this.uniforms.linewidth.value = value;
  137. }
  138. },
  139. resolution: {
  140. enumerable: true,
  141. get: function () {
  142. return this.uniforms.resolution.value;
  143. },
  144. set: function ( value ) {
  145. this.uniforms.resolution.value.copy( value );
  146. }
  147. }
  148. } );
  149. this.setValues( parameters );
  150. };
  151. THREE.LineMaterial.prototype = Object.create( THREE.ShaderMaterial.prototype );
  152. THREE.LineMaterial.prototype.constructor = THREE.LineMaterial;
  153. THREE.LineMaterial.prototype.isLineMaterial = true;
  154. THREE.LineMaterial.prototype.copy = function ( source ) {
  155. THREE.ShaderMaterial.prototype.copy.call( this, source );
  156. this.color.copy( source.color );
  157. this.linewidth = source.linewidth;
  158. this.resolution = source.resolution;
  159. // todo
  160. return this;
  161. };