Published
Edited
Jan 16, 2021
5 stars
Insert cell
Insert cell
p = getRandomPalette()
Insert cell
palette = shuffle(p)
Insert cell
Insert cell
seed = (Math.random() * 100000 | 0) / 100
Insert cell
Insert cell
sh = async () => raymarch({
antiAliasing: 3,
eye: `vec3(-1, 1, 1) * 5.2`,
background: `vec3(${hexToRgb(palette[0]).join(',')})/255.`,
sceneSDF: `
#define PI ${Math.PI}
#define TAU ${Math.PI * 2}

#define NUM_COLORS ${palette.length}.

uniform sampler2D u_texture;

${iqSDF.sdSphere()}
${iqSDF.opSubtraction()}
${iqSDF.opUnion()}
${iqSDF.opIntersection()}
${glslSnippets.rotate3D()}
${await glslSnippets.snoise4()}

float displacement(vec4 p) {
return snoise(p * .5 + float(${seed}));
}

float glow = 0.;

vec2 sceneSDF(vec3 p) {
p *= rotate3D(u_time * TAU, vec3(1, 1, 0));
float d = sdSphere(p, 3.);
float scale = .2;
float disp = displacement(vec4(p, sin(u_time * TAU) * scale) + vec4(cos(u_time * TAU) * scale, -sin(u_time * TAU) * scale, sin(u_time * TAU) * scale * cos(u_time * TAU) * scale, -cos(u_time * TAU) * scale));
d += disp;
d *= .3;
float dd = 10e5;
for(float r = 2.8; r <= 4.; r += .1) {
dd = opUnion(dd, opSubtraction(sdSphere(p, r - 10e-3), opIntersection(d, sdSphere(p, r))));
}

//dd = opSubtraction(d + 10e-10, dd);

//glow += 0.02 / (0.01 + dd * dd * 5.) / (float(AA * AA) * 100.);
return opUnion(vec2(dd, 1.), vec2((sdSphere(p, 2.5) - disp + .1) * .3, 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) + .5) / NUM_COLORS, .5)).rgb;
vec3 col = m == 2. ? vec3(.5, .6, 1.) : 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.), 1.0 - exp(-0.003 * d * d * d));
// gamma
// col = pow(col, vec3(0.4545));

return col;
}`,
effect: `
vec3 effect(vec3 c) {
// c += glow * texture2D(u_texture, vec2(0.5 / NUM_COLORS, .5)).rgb;
c += glow * vec3(1);
return c;
}
`,
logShader: true
})
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