Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : Different shaders for geometry, lines and text?

$
0
0
You have to specify the LINE type when calling shader(), it might sound superflous but the reason was to being able to do something like:

  1. shader(pointSh, POINT);
  2. shader(lineSh, LINE);
  3. shader(texSh); // shader(texSh, TRIANGLE) is also valid
  4. ...
  5. draw your geometry, including lines and points...
  6. ...
  7. resetShader(LINE); // only resets the LINE shader
Although, now I feel that the LINE/POINT argument might be redundant after all...

In any case, as things stand right now the fog sketch should look like:
  1. PShader fogColor; 
  2. PShader fogLines; 
  3. PShader fogTex; 

  4. void setup() { 
  5.   size(640, 360, P3D); 
  6.   
  7.   fogColor = loadShader("fogColor.glsl");
  8.   fogColor.set("fogNear", 0.0); 
  9.   fogColor.set("fogFar", 500.0);

  10.   fogLines = loadShader("fogLines.glsl");
  11.   fogLines.set("fogNear", 0.0); 
  12.   fogLines.set("fogFar", 500.0);

  13.   fogTex = loadShader("fogTex.glsl");
  14.   fogTex.set("fogNear", 0.0); 
  15.   fogTex.set("fogFar", 500.0);
  16.   
  17.   hint(DISABLE_DEPTH_TEST); 

  18. void draw() { 
  19.   background(0);
  20.   fill(255,0,0);
  21.   
  22.   shader(fogColor);  
  23.   noStroke(); 
  24.   translate(mouseX, mouseY, -100); 
  25.   box(200);  
  26.   box(100); 

  27.   shader(fogLines, LINES);
  28.   stroke(255, 0, 0); 
  29.   strokeWeight(10); 
  30.   line(0,0,0, width/2, height/2, 100); 
  31.   line(0,0,0, -width/2, height/2, 100);
  32.    
  33.   shader(fogTex);
  34.   text("shader", 100, 0, 100); 
  35. }

with the following shaders:

fogColor.glsl
  1. #define PROCESSING_COLOR_SHADER

  2. varying vec4 vertColor;

  3. uniform float fogNear;
  4. uniform float fogFar;

  5. void main(){
  6.     gl_FragColor = vertColor;
  7.     
  8.     vec3 fogColor = vec3(1.0,1.0,1.0);
  9.     float depth = gl_FragCoord.z / gl_FragCoord.w;
  10.     float fogFactor = smoothstep(fogNear, fogFar, depth);
  11.     gl_FragColor = mix(gl_FragColor, vec4(fogColor, gl_FragColor.w), fogFactor);
  12. }

fogLines.glsl:
  1. #define PROCESSING_LINE_SHADER

  2. varying vec4 vertColor;

  3. uniform float fogNear;
  4. uniform float fogFar;

  5. void main(){
  6.     gl_FragColor = vertColor;
  7.     
  8.     vec3 fogColor = vec3(1.0,1.0,1.0);
  9.     float depth = gl_FragCoord.z / gl_FragCoord.w;
  10.     float fogFactor = smoothstep(fogNear, fogFar, depth);
  11.     gl_FragColor = mix(gl_FragColor, vec4(fogColor, gl_FragColor.w), fogFactor);
  12. }

fogTex.glsl
  1. #define PROCESSING_TEXTURE_SHADER

  2. uniform sampler2D texture;
  3. varying vec4 vertColor;
  4. varying vec4 vertTexCoord;

  5. uniform float fogNear;
  6. uniform float fogFar;

  7. void main() {
  8.   gl_FragColor = texture2D(texture, vertTexCoord.st) * vertColor;
  9.   
  10.   vec3 fogColor = vec3(1.0,1.0,1.0);
  11.   float depth = gl_FragCoord.z / gl_FragCoord.w;
  12.   float fogFactor = smoothstep(fogNear, fogFar, depth);
  13.   gl_FragColor = mix(gl_FragColor, vec4(fogColor, gl_FragColor.w), fogFactor);
  14. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles