Created
September 12, 2023 16:04
-
-
Save laszlokorte/ae1918660849d2c672ab520694ee5e4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ShaderChunk } from 'three'; | |
export const vertexShader = ` | |
${ShaderChunk.logdepthbuf_pars_vertex} | |
${ShaderChunk.fog_pars_vertex} | |
attribute vec3 previous; | |
attribute vec3 next; | |
attribute float side; | |
attribute float width; | |
attribute float counters; | |
uniform vec2 resolution; | |
uniform float lineWidth; | |
uniform vec3 color; | |
uniform float opacity; | |
uniform float sizeAttenuation; | |
uniform float scaleDown; | |
varying vec2 vUV; | |
varying vec4 vColor; | |
varying float vCounters; | |
vec2 intoScreen( vec4 i, float aspect ) { | |
vCounters = counters; | |
return resolution * (0.5 * i.xy / i.w + 0.5); | |
} | |
void main() { | |
float aspect = resolution.x / resolution.y; | |
vColor = vec4( color, opacity ); | |
vUV = uv; | |
mat4 m = projectionMatrix * modelViewMatrix; | |
vec4 currentClip = m * vec4( position, 1.0 ); | |
vec4 prevClip = m * vec4( previous, 1.0 ); | |
vec4 nextClip = m * vec4( next, 1.0 ); | |
vec2 currentScreen = intoScreen( currentClip, aspect ); | |
vec2 prevScreen = intoScreen( prevClip, aspect ); | |
vec2 nextScreen = intoScreen( nextClip, aspect ); | |
float w = lineWidth * width; | |
vec2 dir; | |
if( nextScreen == currentScreen ) dir = normalize( currentScreen - prevScreen ); | |
else if( prevScreen == currentScreen ) dir = normalize( nextScreen - currentScreen ); | |
else { | |
vec2 dir1 = normalize( currentScreen - prevScreen ); | |
vec2 dir2 = normalize( nextScreen - currentScreen ); | |
dir = normalize( dir1 + dir2 ); | |
vec2 perp = vec2( -dir1.y, dir1.x ); | |
vec2 miter = vec2( -dir.y, dir.x ); | |
} | |
vec2 normal = vec2( -dir.y, dir.x ); | |
normal.x *= aspect; | |
if(sizeAttenuation != 0.0) { | |
normal /= currentClip.w; | |
normal *= min(resolution.x, resolution.y)*0.05; | |
} | |
if( scaleDown > 0. ) { | |
float dist = length(currentScreen - prevScreen); | |
normal *= smoothstep(0.0, scaleDown, dist); | |
} | |
vec2 offsettedScreen = currentScreen + lineWidth * normal * side; | |
gl_Position = vec4(currentClip.w * (2.0 * offsettedScreen/resolution - 1.0), currentClip.z, currentClip.w); | |
${ShaderChunk.logdepthbuf_vertex} | |
${ShaderChunk.fog_vertex} | |
} | |
`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment