Someone please help me to get a single shader working in Processing. I am not totally new to Processing, and I have created many shaders in Shadertoy. Now I would like to use shaders to make my simulations in Processing run faster…
But all the basic examples, for example from the book of shaders, or from Andres Colubri’s tutorial (tutorial ) fail to run. Usually I get a message like
"The file "ToonFrag.glsl" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
NullPointerException
NullPointerException
And I have created a data file where the GLSL codes are living. Here’s an example from Colubri’s tutorial which I fail to make work(and yes the glsl parts are in a separate “data” file):
Code listing 2.1: Sketch that uses a toon effect shader to render a sphere.
PShader toon;
void setup() {
size(640, 360, P3D);
noStroke();
fill(204);
toon = loadShader("ToonFrag.glsl", "ToonVert.glsl");
toon.set("fraction", 1.0);
}
void draw() {
shader(toon);
background(0);
float dirY = (mouseY / float(height) - 0.5) * 2;
float dirX = (mouseX / float(width) - 0.5) * 2;
directionalLight(204, 204, 204, -dirX, -dirY, -1);
translate(width/2, height/2);
sphere(120);
}
ToonVert.glsl: (this I have placed in the data folder)
uniform mat4 transform;
uniform mat3 normalMatrix;
uniform vec3 lightNormal;
attribute vec4 position;
attribute vec4 color;
attribute vec3 normal;
varying vec4 vertColor;
varying vec3 vertNormal;
varying vec3 vertLightDir;
void main() {
gl_Position = transform * position;
vertColor = color;
vertNormal = normalize(normalMatrix * normal);
vertLightDir = -lightNormal;
}
ToonFrag.glsl: (this I have placed in the data folder)
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
uniform float fraction;
varying vec4 vertColor;
varying vec3 vertNormal;
varying vec3 vertLightDir;
void main() {
float intensity;
vec4 color;
intensity = max(0.0, dot(vertLightDir, vertNormal));
if (intensity > pow(0.95, fraction)) {
color = vec4(vec3(1.0), 1.0);
} else if (intensity > pow(0.5, fraction)) {
color = vec4(vec3(0.6), 1.0);
} else if (intensity > pow(0.25, fraction)) {
color = vec4(vec3(0.4), 1.0);
} else {
color = vec4(vec3(0.2), 1.0);
}
gl_FragColor = color * vertColor;
}
Any help? Or can someone suggest other simple codes for me to try?