Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add vertex color support #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/MeshLineMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class MeshLineMaterial extends ShaderMaterial {
useMap: IUniform<boolean>
alphaMap: IUniform<Texture | null>
useAlphaMap: IUniform<boolean>
color: IUniform<Color>
aColor: IUniform<Color>
opacity: IUniform<number>
resolution: IUniform<Vector2>
sizeAttenuation: IUniform<boolean>
Expand All @@ -33,7 +33,7 @@ export class MeshLineMaterial extends ShaderMaterial {
useMap: {value: false},
alphaMap: {value: null},
useAlphaMap: {value: false},
color: {value: new Color(0xffffff)},
aColor: {value: new Color(0xffffff)},
opacity: {value: 1},
resolution: {value: new Vector2(1, 1)},
sizeAttenuation: {value: true},
Expand Down Expand Up @@ -137,10 +137,10 @@ export class MeshLineMaterial extends ShaderMaterial {
color: {
enumerable: true,
get: () => {
return this.uniforms.color.value
return this.uniforms.aColor.value
},
set: value => {
this.uniforms.color.value = value
this.uniforms.aColor.value = value
},
},
opacity: {
Expand Down
134 changes: 70 additions & 64 deletions src/meshline.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ShaderChunk['meshline_vert'] = /*glsl*/ `

uniform vec2 resolution;
uniform float lineWidth;
uniform vec3 color;
uniform vec3 aColor;
uniform float opacity;
uniform float sizeAttenuation;

Expand All @@ -22,61 +22,67 @@ ShaderChunk['meshline_vert'] = /*glsl*/ `

vec2 fix( vec4 i, float aspect ) {

vec2 res = i.xy / i.w;
res.x *= aspect;
vCounters = counters;
return res;
vec2 res = i.xy / i.w;
res.x *= aspect;
vCounters = counters;
return res;
}

void main() {

float aspect = resolution.x / resolution.y;

vColor = vec4( color, opacity );
vUV = uv;

mat4 m = projectionMatrix * modelViewMatrix;
vec4 finalPosition = m * vec4( position, 1.0 );
vec4 prevPos = m * vec4( previous, 1.0 );
vec4 nextPos = m * vec4( next, 1.0 );

vec2 currentP = fix( finalPosition, aspect );
vec2 prevP = fix( prevPos, aspect );
vec2 nextP = fix( nextPos, aspect );

float w = lineWidth * width;

vec2 dir;
if( nextP == currentP ) dir = normalize( currentP - prevP );
else if( prevP == currentP ) dir = normalize( nextP - currentP );
else {
vec2 dir1 = normalize( currentP - prevP );
vec2 dir2 = normalize( nextP - currentP );
dir = normalize( dir1 + dir2 );

vec2 perp = vec2( -dir1.y, dir1.x );
vec2 miter = vec2( -dir.y, dir.x );
//w = clamp( w / dot( miter, perp ), 0., 4. * lineWidth * width );

}

//vec2 normal = ( cross( vec3( dir, 0. ), vec3( 0., 0., 1. ) ) ).xy;
vec4 normal = vec4( -dir.y, dir.x, 0., 1. );
normal.xy *= .5 * w;
normal *= projectionMatrix;
if( sizeAttenuation == 0. ) {
normal.xy *= finalPosition.w;
normal.xy /= ( vec4( resolution, 0., 1. ) * projectionMatrix ).xy;
}

finalPosition.xy += normal.xy * side;

gl_Position = finalPosition;

${ShaderChunk.logdepthbuf_vertex}
${ShaderChunk.fog_vertex && /*glsl*/ `vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );`}
${ShaderChunk.fog_vertex}
float aspect = resolution.x / resolution.y;

#if defined( USE_COLOR_ALPHA )
vColor = color;
#elif defined( USE_COLOR )
vColor = vec4( color, opacity );
#else
vColor = vec4( aColor, opacity );
#endif
vUV = uv;

mat4 m = projectionMatrix * modelViewMatrix;
vec4 finalPosition = m * vec4( position, 1.0 );
vec4 prevPos = m * vec4( previous, 1.0 );
vec4 nextPos = m * vec4( next, 1.0 );

vec2 currentP = fix( finalPosition, aspect );
vec2 prevP = fix( prevPos, aspect );
vec2 nextP = fix( nextPos, aspect );

float w = lineWidth * width;

vec2 dir;
if( nextP == currentP ) dir = normalize( currentP - prevP );
else if( prevP == currentP ) dir = normalize( nextP - currentP );
else {
vec2 dir1 = normalize( currentP - prevP );
vec2 dir2 = normalize( nextP - currentP );
dir = normalize( dir1 + dir2 );

vec2 perp = vec2( -dir1.y, dir1.x );
vec2 miter = vec2( -dir.y, dir.x );
//w = clamp( w / dot( miter, perp ), 0., 4. * lineWidth * width );

}

//vec2 normal = ( cross( vec3( dir, 0. ), vec3( 0., 0., 1. ) ) ).xy;
vec4 normal = vec4( -dir.y, dir.x, 0., 1. );
normal.xy *= .5 * w;
normal *= projectionMatrix;
if( sizeAttenuation == 0. ) {
normal.xy *= finalPosition.w;
normal.xy /= ( vec4( resolution, 0., 1. ) * projectionMatrix ).xy;
}

finalPosition.xy += normal.xy * side;

gl_Position = finalPosition;

${ShaderChunk.logdepthbuf_vertex}
${ShaderChunk.fog_vertex && /*glsl*/ `vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );`}
${ShaderChunk.fog_vertex}
}
`

Expand All @@ -101,18 +107,18 @@ ShaderChunk['meshline_frag'] = /*glsl*/ `
varying float vCounters;

void main() {
${ShaderChunk.logdepthbuf_fragment}
${ShaderChunk.logdepthbuf_fragment}

vec4 c = vColor;
if( useMap == 1. ) c *= texture2D( map, vUV * repeat );
if( useAlphaMap == 1. ) c.a *= texture2D( alphaMap, vUV * repeat ).a;
if( c.a < alphaTest ) discard;
if( useDash == 1. ){
c.a *= ceil(mod(vCounters + dashOffset, dashArray) - (dashArray * dashRatio));
}
gl_FragColor = c;
gl_FragColor.a *= step(vCounters, visibility);

${ShaderChunk.fog_fragment}
vec4 c = vColor;
if( useMap == 1. ) c *= texture2D( map, vUV * repeat );
if( useAlphaMap == 1. ) c.a *= texture2D( alphaMap, vUV * repeat ).a;
if( c.a < alphaTest ) discard;
if( useDash == 1. ){
c.a *= ceil(mod(vCounters + dashOffset, dashArray) - (dashArray * dashRatio));
}
gl_FragColor = c;
gl_FragColor.a *= step(vCounters, visibility);

${ShaderChunk.fog_fragment}
}
`