123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- /**
- * @author WestLangley / http://github.com/WestLangley
- *
- * parameters = {
- * color: <hex>,
- * linewidth: <float>,
- * resolution: <Vector2>, // to be set by renderer
- * }
- */
- THREE.UniformsLib.line = {
- linewidth: { value: 1 },
- resolution: { value: new THREE.Vector2( 1, 1 ) }
- };
- THREE.ShaderLib[ 'line' ] = {
- uniforms: THREE.UniformsUtils.merge( [
- THREE.UniformsLib.common,
- THREE.UniformsLib.fog,
- THREE.UniformsLib.line
- ] ),
- vertexShader:
- `
- #include <common>
- #include <color_pars_vertex>
- #include <fog_pars_vertex>
- #include <logdepthbuf_pars_vertex>
- #include <clipping_planes_pars_vertex>
- uniform float linewidth;
- uniform vec2 resolution;
- attribute vec3 instanceStart;
- attribute vec3 instanceEnd;
- attribute vec3 instanceColorStart;
- attribute vec3 instanceColorEnd;
- varying vec2 vUv;
- void main() {
- #ifdef USE_COLOR
- vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
- #endif
- float aspect = resolution.x / resolution.y;
- vUv = uv;
- // camera space
- vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
- vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );
- // clip space
- vec4 clipStart = projectionMatrix * start;
- vec4 clipEnd = projectionMatrix * end;
- // ndc space
- vec2 ndcStart = clipStart.xy / clipStart.w;
- vec2 ndcEnd = clipEnd.xy / clipEnd.w;
- // direction
- vec2 dir = ndcEnd - ndcStart;
- // account for clip-space aspect ratio
- dir.x *= aspect;
- dir = normalize( dir );
- // perpendicular to dir
- vec2 offset = vec2( dir.y, - dir.x );
- // undo aspect ratio adjustment
- dir.x /= aspect;
- offset.x /= aspect;
- // sign flip
- if ( position.x < 0.0 ) offset *= - 1.0;
- // endcaps
- if ( position.y < 0.0 ) {
- offset += - dir;
- } else if ( position.y > 1.0 ) {
- offset += dir;
- }
- // adjust for linewidth
- offset *= linewidth;
- // adjust for clip-space to screen-space conversion // maybe it should be based on viewport ...
- offset /= resolution.y;
- // select end
- vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd;
- // back to clip space
- offset *= clip.w;
- clip.xy += offset;
- gl_Position = clip;
- #include <logdepthbuf_vertex>
- #include <worldpos_vertex>
- #include <clipping_planes_vertex>
- #include <fog_vertex>
- }
- `,
- fragmentShader:
- `
- uniform vec3 diffuse;
- uniform float opacity;
- #include <common>
- #include <color_pars_fragment>
- #include <fog_pars_fragment>
- #include <logdepthbuf_pars_fragment>
- #include <clipping_planes_pars_fragment>
- varying vec2 vUv;
- void main() {
- #include <clipping_planes_fragment>
- if ( vUv.y < 0.5 || vUv.y > 0.5 ) {
- float a = vUv.x - 0.5;
- float b = vUv.y - 0.5;
- float len2 = a * a + b * b;
- if ( len2 > 0.25 ) discard;
- }
- vec4 diffuseColor = vec4( diffuse, opacity );
- #include <logdepthbuf_fragment>
- #include <color_fragment>
- gl_FragColor = vec4( diffuseColor.rgb, diffuseColor.a );
- #include <premultiplied_alpha_fragment>
- #include <tonemapping_fragment>
- #include <encodings_fragment>
- #include <fog_fragment>
- }
- `
- };
- THREE.LineMaterial = function ( parameters ) {
- THREE.ShaderMaterial.call( this, {
- type: 'LineMaterial',
- side: THREE.DoubleSide, // for now. there is an issue with segments that terminate behind the camera
- uniforms: THREE.UniformsUtils.clone( THREE.ShaderLib[ 'line' ].uniforms ),
- vertexShader: THREE.ShaderLib[ 'line' ].vertexShader,
- fragmentShader: THREE.ShaderLib[ 'line' ].fragmentShader
- } );
- Object.defineProperties( this, {
- color: {
- enumerable: true,
- get: function () {
- return this.uniforms.diffuse.value;
- },
- set: function ( value ) {
- this.uniforms.diffuse.value = value;
- }
- },
- linewidth: {
- enumerable: true,
- get: function () {
- return this.uniforms.linewidth.value;
- },
- set: function ( value ) {
- this.uniforms.linewidth.value = value;
- }
- },
- resolution: {
- enumerable: true,
- get: function () {
- return this.uniforms.resolution.value;
- },
- set: function ( value ) {
- this.uniforms.resolution.value.copy( value );
- }
- }
- } );
- this.setValues( parameters );
- };
- THREE.LineMaterial.prototype = Object.create( THREE.ShaderMaterial.prototype );
- THREE.LineMaterial.prototype.constructor = THREE.LineMaterial;
- THREE.LineMaterial.prototype.isLineMaterial = true;
- THREE.LineMaterial.prototype.copy = function ( source ) {
- THREE.ShaderMaterial.prototype.copy.call( this, source );
- this.color.copy( source.color );
- this.linewidth = source.linewidth;
- this.resolution = source.resolution;
- // todo
- return this;
- };
|