You can run fragment shaders with Processing, a tool that even beginners can easily get started with. You can write in Java or Python.
The Java code is on the left and the Python code is on the right.
First, put the program that wrote the contents of the shader in the data folder.
data/FragmentShader.glsl
uniform vec2 resolution;
uniform float time;
void main() {
vec2 uv = -1. + 2. * gl_FragCoord.xy / resolution.xy;
gl_FragColor = vec4(
abs( sin( cos( time + 3. * uv.y ) * 2. * uv.x + time)),
abs( cos( sin( time + 2. * uv.x ) * 3. * uv.y + time)),
1.0,
1.0);
}
Write as follows in the Processing main program. Just specify FragmentShader.glsl and the file name and it will search in the data folder.
PShader sd;
void setup() {
size(600, 600, P2D);
sd = loadShader("FragmentShader.glsl");
}
void draw() {
sd.set("time", millis() / 1000.0);
sd.set("resolution", (float)width, (float)height);
shader(sd);
rect(0, 0, width, height);
}
By default, only Java can be selected as the language setting in the upper right, You can easily add Python mode.
(This link is easy to understand with an image) https://pycarnival.com/processingpy1/
In the same way, put the exact same shader file as in Java in the data folder
data/FragmentShader.glsl
uniform vec2 resolution;
uniform float time;
void main() {
vec2 uv = -1. + 2. * gl_FragCoord.xy / resolution.xy;
gl_FragColor = vec4(
abs( sin( cos( time + 3. * uv.y ) * 2. * uv.x + time)),
abs( cos( sin( time + 2. * uv.x ) * 3. * uv.y + time)),
1.0,
1.0);
}
Write as follows in the Processing main program. By declaring a variable in global sd, the same shader object can be referenced by both the setup function and the draw function.
def setup():
global sd
size(600, 600, P2D)
sd = loadShader("FragmentShader.glsl")
def draw():
global sd
sd.set("time", millis() / 1000.0)
sd.set("resolution", float(width), float(height))
shader(sd)
rect(0, 0, width, height)
Now, let's enjoy GLSL in both Java and Python with Processing!
Recommended Posts