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

Different shaders for geometry, lines and text?

$
0
0
Hi!

I'm trying to add some basic fog effect through the new PShader object. Found this fairly simple glsl shader and adapted to my purposes. I would like to replace only the fragment shader and keep using the "default" vertex shader, but I couldn't find how to do it, so I made a basic vertex shader using the minimum attributes, which fall in the FLAT shader type.

Here is my test code:
Copy code

  1. PShader fog;

    void setup() {
      size(640, 360, P3D);
      fog = loadShader("fogF.glsl", "fogV.glsl");
  2.   fog.set("fogNear", 0.0);
      fog.set("fogFar", 500.0);
      hint(DISABLE_DEPTH_TEST);
    }

    void draw() {
        shader(fog);
      background(0);
      noStroke();
      translate(mouseX, mouseY, -100);
      box(200);
      fill(255,0,0);
      box(100);
      stroke(255);
      strokeWeight(10);
      line(0,0,0, width/2, height/2, 100);
      line(0,0,0, -width/2, height/2, 100);
      text("shader", 100,0,100);
    }
The vertex shader:
Copy code
  1. uniform mat4 projmodelviewMatrix;

    attribute vec4 inVertex;
    attribute vec4 inColor;

    varying vec4 vColor;

    void main() {
      // Vertex in clip coordinates
      gl_Position = projmodelviewMatrix * inVertex;
      vColor = inColor;
    }

The fragment shader:
Copy code
  1. #ifdef GL_ES
    precision highp float;
    #endif

    uniform float fogNear;
    uniform float fogFar;
    varying vec4 vColor;

    void main(){
        vec3 fogColor = vec3(1.0,1.0,1.0);
        gl_FragColor = vColor;
        float depth = gl_FragCoord.z / gl_FragCoord.w;
        float fogFactor = smoothstep(fogNear, fogFar, depth);
        gl_FragColor = mix(gl_FragColor, vec4(fogColor, gl_FragColor.w), fogFactor);

    }

The FLAT type shader only works for filled untextured geometry, so, do I need to write another one using the LINES type attributes  to apply the fog effect to lines and another one for text? It feels very convoluted for a simple effect, is there a way to simplify it?

Cheers!

Viewing all articles
Browse latest Browse all 1768

Trending Articles