edit strokeWeight inside a vertex shader

edited June 2015 in GLSL / Shaders

Do you know how to edit the stroke weight of a line inside a vertex shader using a variable called uniform float strokeWeight ?

The final idea is to attribute a different weight to each stroke.

#define PROCESSING_LINE_SHADER

uniform mat4 transform;
uniform vec4 viewport;

uniform float strokeWeight;

attribute vec4 vertex;
attribute vec4 color;
attribute vec4 direction;

varying vec4 vertColor;

vec3 clipToWindow(vec4 clip, vec4 viewport) {
  vec3 dclip = clip.xyz / clip.w;
  vec2 xypos = (dclip.xy + vec2(1.0, 1.0)) * 0.5 * viewport.zw;
  return vec3(xypos, dclip.z * 0.5 + 0.5);
}

void main() {
  vec4 clip0 = transform * vertex;
  vec4 clip1 = clip0 + transform * vec4(direction.xyz, 0);
  float thickness = direction.w;

  vec3 win0 = clipToWindow(clip0, viewport); 
  vec3 win1 = clipToWindow(clip1, viewport); 
  vec2 tangent = win1.xy - win0.xy;

  vec2 normal = normalize(vec2(-tangent.y, tangent.x));
  vec2 offset = normal * thickness;
  gl_Position.xy = clip0.xy + offset.xy;
  gl_Position.zw = clip0.zw;
  vertColor = color;  
}

Answers

  • You could either replace thickness with strokeWeight or multiply thickness by strokeWeight.

    But why would you want to do that? If I remember correctly, thickness (direction.w) is already defined by Processing's strokeWeight() function.

Sign In or Register to comment.