Published
Edited
Dec 17, 2020
6 stars
Insert cell
Insert cell
p = getRandomPalette()
Insert cell
palette = shuffle(p)
Insert cell
Insert cell
Insert cell
sh = () => raymarch({
antiAliasing: 3,
eye: `vec3(3, 1, 3) * .8`,
// background: `vec3(${hexToRgb(palette[0]).join(',')})/255.`,
sceneSDF: `
#define PI ${Math.PI}
#define TAU ${Math.PI * 2}
#define PHI (sqrt(5.)*0.5 + 0.5)

#define NUM_COLORS ${palette.length}.

uniform sampler2D u_texture;

${iqSDF.sdBox()}
${iqSDF.sdSphere()}
${iqSDF.opUnion()}
${iqSDF.opSmoothUnion()}
${iqSDF.opIntersection()}
${iqSDF.opSubtraction()}
${iqSDF.opRep()}
${iqSDF.rotateY()}
${iqSDF.rotateX()}
${glslSnippets.rotate3D()}
${motionToolkit.linearstep()}
${motionToolkit.linearstepUpDown()}
${motionToolkit.easeInOutCubic()}

float glow = 0.;
float glow2 = 0.;

float fBlob(vec3 p) {
p = abs(p);
if (p.x < max(p.y, p.z)) p = p.yzx;
if (p.x < max(p.y, p.z)) p = p.yzx;
float b = max(max(max(
dot(p, normalize(vec3(1))),
dot(p.xz, normalize(vec2(PHI+1., 1.)))),
dot(p.yx, normalize(vec2(1., PHI)))),
dot(p.xz, normalize(vec2(1., PHI))));
float l = length(p);
return l - 1.5 - 0.2 * (1.5 / 2.)* cos(min(sqrt(1.01 - b / l)*(PI / 0.25), PI));
}

float pModPolar(inout vec2 p, float repetitions) {
float angle = 2.*PI/repetitions;
float a = atan(p.y, p.x) + angle/2.;
float r = length(p);
float c = floor(a/angle);
a = mod(a,angle) - angle/2.;
p = vec2(cos(a), sin(a))*r;
// For an odd number of repetitions, fix cell index of the cell in -x direction
// (cell index would be e.g. -5 and 5 in the two halves of the cell):
if (abs(c) >= (repetitions/2.)) c = abs(c);
return c;
}

float sdWiredBox(vec3 p, vec3 dim, float weight) {
float b = sdBox(p, dim);
b = max(-sdBox(p, vec3(dim.x + 1., dim.y - weight, dim.z - weight)), b);
b = max(-sdBox(p, vec3(dim.x - weight, dim.y + 1., dim.z - weight)), b);
b = max(-sdBox(p, vec3(dim.x - weight, dim.y - weight, dim.z + 1.)), b);
return b;
}

vec2 sceneSDF(vec3 p) {
/*
float t = u_time * TAU;
float sinT = sin(t);
float cosT = cos(t);

// p *= rotate3D(t, vec3(0, 1, 0));

float d = sdWiredBox(p * rotate3D(t, vec3(0, 1, 0)), vec3(1.3), .0001);
//glow += 0.01 / (0.01 + d * d * 5.) / (float(AA * AA) * 10.);
float blob = fBlob(p);
blob = opSubtraction(blob - .1, blob);
float slices = sdBox(opRep(p - vec3(0., u_time * 2., 0.), vec3(20., .4, 20.)), vec3(1.5, .0001, 1.5));
//blob = opIntersection(blob, slices);
//glow2 += 0.01 / (0.01 + blob * blob * 5.) / (float(AA * AA) * 10.);

d = opUnion(d, blob);
*/

vec3 pp = p;
pModPolar(pp.xz, 100.);
float d = opIntersection(sdBox(p - vec3(0., 2.2 - easeInOutCubic(linearstep(.25, .75, fract(u_time * 2.))) * 4.4, 0.), vec3(2.5, .5, 2.5)), sdBox(pp, vec3(2.5, 2.5, .001)));
d = opIntersection(d, opSubtraction(fBlob(p) + 0.001, fBlob(p)));
glow += 0.01 / (0.01 + d * d * 20.) / (float(AA * AA) * 10.);

p *= 2.;
p *= rotateY(u_time * TAU);
// p *= rotateX(u_time * TAU);

float cosT = cos(u_time * TAU);
float sinT = sin(u_time * TAU);
float cosT2 = cos(u_time * TAU * 2.);
float sinT2 = sin(u_time * TAU * 2.);
float cosT3 = cos(u_time * TAU * 3.);
float sinT3 = sin(u_time * TAU * 3.);

vec3 p1 = p + vec3(sinT2, -cosT, sinT3 * cosT2) * 1.7;
vec3 p2 = p + vec3(-cosT * sinT2, -sinT3, cosT3 * cosT2) * 1.9;
vec3 p3 = p + vec3(-sinT3 * sinT2, sinT3 * cosT, -cosT * sinT) * 1.6;
vec3 p4 = p + vec3(sinT3, -sinT * cosT, -cosT * sinT) * 1.8;
vec3 p5 = p + vec3(sinT * sinT3, -cosT2 * cosT2, cosT3 * sinT) * 1.6;

float dd = opSmoothUnion(
opSmoothUnion(
opSmoothUnion(
opSmoothUnion(
sdSphere(p1, .2 * .1) * .5,
sdSphere(p2, .3 * .1) * .5,
2.
),
sdSphere(p3, .5 * .1) * .5,
2.
),
sdSphere(p4, .6 * .1) * .5,
2.
),
sdSphere(p5, .6 * .1) * .5,
2.
);

d = opUnion(d, dd);


return vec2(d, 2.);
}`,
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 = texture2D(u_texture, vec2(floor(m) / NUM_COLORS, .5)).rgb;

// 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.), 1.0 - exp(-0.003 * d * d * d));
// gamma
// col = pow(col, vec3(0.4545));

return col;
}`,
effect: `
vec3 effect(vec3 c) {
c += glow * vec3(.8, .6, .01);
c += glow2 * texture2D(u_texture, vec2(3.5 / NUM_COLORS, .5)).rgb;
return c;
}
`,
logShader: true
})
Insert cell
iqSDF.opRep()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
iqSDF
Insert cell
Insert cell
motionToolkit
Insert cell
Insert cell
glslSnippets
Insert cell
import {shuffle} from '@makio135/utilities'
Insert cell
import {getRandomPalette, displayPalettes, hexToRgb} from '@makio135/give-me-colors'
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