Published
Edited
Dec 17, 2020
1 fork
1 star
Insert cell
Insert cell
Insert cell
sh = () => raymarch({
eye: `vec3(3.8, sin(u_time * TAU), -3.8)`,
antiAliasing: 3,
sceneSDF: `
${iqSDF.opUnion()}
${iqSDF.opSmoothUnion()}
${iqSDF.opIntersection()}
${iqSDF.sdBox()}
${iqSDF.sdTorus()}
${iqSDF.sdCappedCylinder()}
${iqSDF.opSymXZ()}
${iqSDF.opSymXYZ()}
${iqSDF.opRep()}
${motionToolkit.easeInOutQuad()}
${motionToolkit.linearstep()}
${motionToolkit.linearstepUpDown()}
${glslSnippets.rotate3D()}
${glslSnippets.hg_sdf()}

float glow = 0.;
vec2 sceneSDF(vec3 p) {
p *= rotate3D(easeInOutQuad(u_time) * TAU, vec3(0, 1, 0));
vec3 pp = p;
vec2 d = vec2(1e10, 0.);
const float N = 20.;
for(float i = 0.; i < N; i ++) {
float angle = (TAU / 4.) / (N - 1.) * i;
vec3 q = vec3(cos(angle), 0., sin(angle)) * 1.5;
float h = linearstepUpDown(.1, .15, .3, .35, mod(u_time + i * .01, .5)) * (cos(angle * 4.) * .2 + .3);
d = opUnion(d, vec2(sdBox(opSymXZ(p) - q, vec3(vec2(.0001, h).xyx)), 1.));
}
glow += 0.01 / (0.01 + d.x * d.x * 50.) / (float(AA) * 4.);
for(float i = 0.; i < 8.; i ++) {
p = opSymXYZ(p);
p *= .95;
p -= .1;
p *= rotate3D(easeInOutQuad(mod(u_time + .2, 1.)) * TAU, vec3(0, 1, 1));
d = opUnion(d, vec2(sdTorus(p, vec2(.3, .04)), mod(i, 2.) + 1.));
d = opUnion(d, vec2(sdTorus(p.xzy, vec2(.3, .04)), mod(i, 2.) + 1.));
d = opUnion(d, vec2(sdTorus(p.yxz, vec2(.3, .04)), mod(i, 2.) + 1.));
}

return d;
}`,
computeColor: `
vec3 computeColor(vec3 ro, vec3 rd, vec3 pos, float d, float m) {
vec3 nor = calcNormal(pos);
vec3 ref = reflect(rd, nor); // reflected ray

// material
vec3 col = 0.45 + 0.35 * sin(vec3(0.05, 0.08, 0.10) * (m - 1.0));
if(m == 1.) col = vec3(0);
else if(m == 2.) col = vec3(1);

// lighting
float occ = calcAO(pos, nor); // ambient occlusion
vec3 lig = normalize(vec3(-0.4, 0.7, -0.6)); // sunlight
float amb = clamp(0.5 + 0.5 * nor.y, 0.0, 1.0); // ambient light
float dif = clamp(dot(nor, lig), 0.0, 1.0); // diffuse reflection from sunlight
// backlight
float bac = clamp(dot(nor, normalize(vec3(-lig.x, 0.0, -lig.z))), 0.0, 1.0) * clamp(1.0 - pos.y, 0.0, 1.0);
float dom = smoothstep(-0.1, 0.1, ref.y); // dome light
float fre = pow(clamp(1.0 + dot(nor, rd), 0.0, 1.0), 2.0); // fresnel
float spe = pow(clamp(dot(ref, lig), 0.0, 1.0), 16.0); // specular reflection

dif *= softshadow(pos, lig, 0.02, 2.5);
dom *= softshadow(pos, ref, 0.02, 2.5);

vec3 lin = vec3(0.0);
lin += 1.30 * dif * vec3(1.00, 0.80, 0.55);
lin += 2.00 * spe * vec3(1.00, 0.90, 0.70) * dif;
lin += 0.40 * amb * vec3(0.40, 0.60, 1.00) * occ;
lin += 0.50 * dom * vec3(0.40, 0.60, 1.00) * occ;
lin += 0.50 * bac * vec3(0.25, 0.25, 0.25) * occ;
lin += 0.25 * fre * vec3(1.00, 1.00, 1.00) * occ;
col = col * lin;

// mix in fog?
// col = mix(col, vec3(0.8, 0.9, 1.0), 1.0 - exp(-0.0002 * d * d * d));
//col = mix( col, vec3(0.0), 1.0 - exp(-0.02 * d * d * d));
// gamma
// col = pow(col, vec3(0.4545));

return col;
}`,
effect: `
vec3 effect(vec3 c) {
c += glow * vec3(1., .3, .3);
// c += glow2 * vec3(1., .1, .1);
return c;
}`,
logShader: true
})
Insert cell
iqSDF.sdCappedCylinder()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more