LineMaterial.js 8.7 KB

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