Hi all,
I was wondering if some of you could help me make this 2D radiosity shader work with Processing.
By “work”, I mean being able:
- to apply the shader to basic shape primitives (rect, ellipse, triangle, arc) drawn in Processing
- to adjust the radiosity level of each shape directly from the Processing script
The goal is to have something similar looking to the beautiful (and unfortunately not available) global illumination render showed in Thomas Diewald latest demos.
So far I’ve made minor changes to the original shader so it can be compiled by Processing
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
uniform float iTime;
uniform vec2 resolution;
float sphereSDF(vec2 p, float size) {
return length(p) - size;
}
float boxSDF(vec2 p, vec2 size) {
vec2 r = abs(p) - size;
return min(max(r.x, r.y),0.) + length(max(r,vec2(0,0)));
}
vec3 colormap(float x) {
float s = sin(x*6.28);
if (x > 0.) {
return vec3(1,1,1.+s)/2.;
} else {
return vec3(1,1.+s,1)/2.;
}
}
void AddObj(inout float dist, inout vec3 color, float d, vec3 c) {
if (dist > d) {
dist = d;
color = c;
}
}
void scene(in vec2 pos, out vec3 color, out float dist) {
dist = 1e9; color = vec3(0,0,0);
AddObj(dist, color, boxSDF(pos - vec2(-4,1), vec2(1,1)), vec3(.6,.8,1.));
AddObj(dist, color, sphereSDF(pos - vec2(4,1), 1.), vec3(1,.9,.8));
AddObj(dist, color, boxSDF(pos - vec2(0,3.0*sin(iTime)), vec2(1.5 ,0.5 + cos(iTime)*.3)), vec3(.4,.1,.1));
}
void trace(vec2 p, vec2 dir, out vec3 c) {
for (;;) {
float d;
scene(p, c, d);
if (d < 1e-3) return;
if (d > 1e1) break;
p -= dir * d;
}
c = vec3(0,0,0);
}
float random (in vec2 _st) {
return fract(sin(dot(_st.xy,
vec2(12.9898,78.233)))*
43758.5453123);
}
#define SAMPLES 128
void main()
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = (gl_FragCoord.xy-(resolution.xy/2.f))/resolution.y*10.f;
vec3 col = vec3(0,0,0);
for (int i = 0; i < SAMPLES; i++) {
float t = (float(i) + random(uv+float(i)+iTime)) / float(SAMPLES) * 2. * 3.1415;
vec3 c;
trace(uv, vec2(cos(t), sin(t)), c);
col += c;
}
col /= float(SAMPLES);
// Output to screen
gl_FragColor = vec4(col*2.0,3.0);
}
Here all 3 shapes are coded in the shader. To draw the same shapes from Processing (and have the radiosity effect working) I guess I would need a vertex shader:
- Am I right ?
- What should that vertex shader look like ?
Thank you