LineMaterial.js 8.3 KB

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