shaderexp: add initial shader explorer tool (#245)

* shaderexp: first commit
* shaderexp: further improve error handling
* shaderexp: attribute ray_marching example

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
Co-authored-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
PiergiorgioZagaria 2022-04-21 13:44:02 +02:00 committed by GitHub
parent 8d574e772c
commit 8df8b043ad
Failed to generate hash of commit
9 changed files with 646 additions and 0 deletions

View file

@ -0,0 +1,38 @@
struct UniformBufferObject {
resolution: vec2<f32>,
time: f32,
}
@group(0) @binding(0) var<uniform> ubo : UniformBufferObject;
@stage(fragment) fn main(
@location(0) uv : vec2<f32>
) -> @location(0) vec4<f32> {
let aspect = ubo.resolution / min(ubo.resolution.x,ubo.resolution.y);
let translated_uv = (uv - vec2(0.5,0.5)) * aspect * 2.0;
let col = f32(mandel(translated_uv)) / 100.0;
return vec4(vec3<f32>(col), 1.0);
}
fn mandel(uv: vec2<f32>) -> i32{
let zoom = 1.0;
let center_position = vec2<f32>(0.5,0.0);
let mapped_point = uv * zoom - center_position;
var z = mapped_point;
var tmp:f32;
var i:i32 = 0;
var found = false;
var res = 0;
loop {
if (i >= 100){
break;
}
tmp = z.x;
z.x = z.x * z.x - z.y * z.y + mapped_point.x;
z.y = 2. * tmp * z.y + mapped_point.y;
found = found || (z.x * z.x + z.y * z.y > 16.);
res = res + 1 * i32(!found);
i = i + 1;
}
return res;
}

View file

@ -0,0 +1,98 @@
// A slight modification / translation of https://www.shadertoy.com/view/XlGBW3
// to WGSL.
// License: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
struct UniformBufferObject {
resolution: vec2<f32>,
time: f32,
}
@group(0) @binding(0) var<uniform> ubo : UniformBufferObject;
fn getDist(p:vec3<f32>) -> f32{
let dist_from_center:f32 = 2.*sin(ubo.time * 3.);
let rotation_speed:f32 = 6.;
let sphere1 = vec4<f32>(dist_from_center*cos(rotation_speed*ubo.time + 3.14159 * 0. * 2. / 3.),1.1, dist_from_center*sin(rotation_speed*ubo.time + 3.14159 + 3.14159 * 0. * 2. / 3.),1.);
let sphere2 = vec4<f32>(dist_from_center*cos(rotation_speed*ubo.time + 3.14159 * 1. * 2. / 3.),1.1, dist_from_center*sin(rotation_speed*ubo.time + 3.14159 + 3.14159 * 1. * 2. / 3.),1.);
let sphere3 = vec4<f32>(dist_from_center*cos(rotation_speed*ubo.time + 3.14159 * 2. * 2. / 3.),1.1, dist_from_center*sin(rotation_speed*ubo.time + 3.14159 + 3.14159 * 2. * 2. / 3.),1.);
let sphere1_dist:f32 = length(p - sphere1.xyz) - sphere1.w;
let sphere2_dist:f32 = length(p - sphere2.xyz) - sphere2.w;
let sphere3_dist:f32 = length(p - sphere3.xyz) - sphere3.w;
let plane_dist = p.y;
return min(min(min(sphere1_dist,sphere2_dist),sphere3_dist),plane_dist);
}
fn rayMarch(ro:vec3<f32>, rd:vec3<f32>) -> f32{
let MAX_STEPS:i32 = 100;
let MAX_DIST:f32 = 100.0;
let SURF_DIST:f32 = 0.01;
var d:f32 = 0.0;
var i: i32 = 0;
loop {
if(i >= MAX_STEPS){
break;
}
let p = ro + rd * d;
let ds = getDist(p);
d = d + ds;
if(d > MAX_DIST || ds <= SURF_DIST){
break;
}
i = i + 1;
}
return d;
}
fn getNormal(p:vec3<f32>) -> vec3<f32>{
let d = getDist(p);
let e = vec2<f32>(0.1,0.0);
// We can find the normal using the points around the hit point
let n = d - vec3<f32>(
getDist(p-e.xyy),
getDist(p-e.yxy),
getDist(p-e.yyx)
);
return normalize(n);
}
fn getLight(p:vec3<f32>) -> f32{
let SURF_DIST:f32 = .01;
let light_pos = vec3<f32>(0.,5.,0.);
let l = normalize(light_pos - p) * 1.;
let n = getNormal(p);
var dif = clamp(dot(n,l),.0,1.);
let d = rayMarch(p + n * SURF_DIST * 2.,l);
if(d<length(light_pos - p)){
dif = dif * .1;
}
return dif;
}
@stage(fragment) fn main(
@location(0) uv : vec2<f32>
) -> @location(0) vec4<f32> {
let aspect = ubo.resolution / min(ubo.resolution.x,ubo.resolution.y);
let tmp_uv = (uv - vec2(0.5,0.5)) * aspect * 2.0;
var col = vec3<f32>(0.0);
let r_origin = vec3<f32>(4.0,3.,.0);
let r_dir = normalize(vec3<f32>(-1.0,tmp_uv.y,tmp_uv.x));
let d = rayMarch(r_origin,r_dir);
col = vec3<f32>(d / 8.);
let p = r_origin + r_dir * d;
let diff = getLight(p);
col = vec3<f32>(diff , 0.,0.);
return vec4<f32>(col,0.0);
}

View file

@ -0,0 +1,40 @@
struct UniformBufferObject {
resolution: vec2<f32>,
time: f32,
}
@group(0) @binding(0) var<uniform> ubo : UniformBufferObject;
@stage(fragment) fn main(
@location(0) uv : vec2<f32>
) -> @location(0) vec4<f32> {
let aspect = ubo.resolution.xy / ubo.resolution.y;
let translated_uv = (uv - vec2<f32>(0.5,0.5)) * 2.0 * aspect;
let freq:f32 = 5.0;
let speed: f32 = 5.0;
let h = (sin(freq * length(translated_uv) + speed * ubo.time) + 1.0) / 2.0;
let h_off = 20.0;
return vec4<f32>(hsl_to_rgb(h * (360.0 - h_off * 2.0) + h_off ,0.7,0.5),1.0);
}
// 0 H < 360, 0 S 1 and 0 L 1
fn hsl_to_rgb(h:f32,s:f32,l:f32) -> vec3<f32> {
let tmp_h = h % 360.0;
let c = (1.0 - abs(2.0 * l - 1.0)) * s;
let x = c * (1.0 - abs((tmp_h / 60.0) % 2.0 - 1.0));
let m = l - c / 2.0;
let case_1 = vec3<f32>(c ,x ,0.0);
let case_2 = vec3<f32>(x ,c ,0.0);
let case_3 = vec3<f32>(0.0,c ,x);
let case_4 = vec3<f32>(0.0,x ,c);
let case_5 = vec3<f32>(x ,0.0,c);
let case_6 = vec3<f32>(c ,0.0,x);
return case_1 * f32(tmp_h < 60.0 && tmp_h >= 0.0) +
case_2 * f32(tmp_h < 120.0 && tmp_h >= 60.0) +
case_3 * f32(tmp_h < 180.0 && tmp_h >= 120.0) +
case_4 * f32(tmp_h < 240.0 && tmp_h >= 180.0) +
case_5 * f32(tmp_h < 300.0 && tmp_h >= 240.0) +
case_6 * f32(tmp_h < 360.0 && tmp_h >= 300.0) + vec3<f32>(m);
}