For this week’s assignment, create an animated character, creature, or other organic looking thing, building on nov25/example4.html.
As a stretch goal, see if you can vary some visual property over different parts of your creation, the way I varied color. The property can be shininess, opacity, or stripes, spots or some other procedural texture.
If you are feeling really ambitious, you might also try to find ways to make Marching Cubes run faster. For example, you might try pre-sampling the volume at a coarser resolution, and then using those results to figure out where to sample at a finer resolution. Or if you are rendering ellipsoidal blobs, you might try using the locations of those blobs to figure out where you need to sample and where you don’t. There are lots of possible approaches.
Meanwhile, happy Thanksgiving everybody!
fixed my library.
added ken’s noise to the library by stefan gustavson.
//original:
let noise = (x, y, z) => {
let normalize = (v) => {
let s = Math.sqrt(dot(v, v));
return v.map((a) => a / s);
};
let c = Math.cos,
floor = Math.floor,
dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
let r = (x, y, z) => c(5 * (x + 5 * c(5 * (y + 5 * c(5 * (z + 5 * c(5 * x)))))));
let s = (x, y, z) => normalize([r(x, y, z), r(y, z, x), r(z, x, y)]);
let i = floor(x),
j = floor(y),
k = floor(z),
u = x - i,
v = y - j,
w = z - k;
let t = (x, y, z) => dot(s(i + x, j + y, k + z), [u - x, v - y, w - z]);
let e = (f, a, b) => a + (b - a) * (f < 0.5 ? 2 * f * f : 2 * f * (2 - f) - 1);
return e(
w,
e(v, e(u, t(0, 0, 0), t(1, 0, 0)), e(u, t(0, 1, 0), t(1, 1, 0))),
e(v, e(u, t(0, 0, 1), t(1, 0, 1)), e(u, t(0, 1, 1), t(1, 1, 1))),
);
};
simplest volumetric mesh:
let f = (x, y, z) => x * x + y * y + z * z - 0.175 + noise.ken3(8 * x, 8 * y, 8 * z) / 10;
...
m.draw(volume_mesh(f, 100, cache));
first understood implicit vs explicit functions. thought the idea was very cool.
broken implicit function:

let blobs = (x, y, z) => {
let p = [x, y, z];
//flip the check. > 0 is inside.
let S = (p) => p[0] * p[0] - p[1] * p[1] - p[2] * p[2];
let r = 0.5;
return S([x / r, y / r, z / r]) - 1.;
};
for a mesh, we specify the subdivisions with N.
bad mesh.

did many, many experiments.
<!-- diamond wobble: -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>sketch</title>
</head>
<body bgcolor="white">
<!--<canvas id="canvas" width="800" height="800"></canvas>-->
<canvas id="canvas"></canvas>
<!-- for debug: -->
<style>
canvas {
/*border: 1px solid white;*/
margin: 0;
padding: 0;
width: 99vw;
height: 99vh;
overflow: hidden;
}
</style>
<!-- scripts: -->
<!-- math & helpers: -->
<script src="../assets/library-1.5.js"></script>
<!-- js -> webgl: -->
<script src="../assets/webgl-5.js"></script>
<!-- shape-definitions (as well as volumetric meshes) -->
<script src="../assets/shapes-3.js"></script>
<script src="../assets/materials.js"></script>
<!-- called with noise.{type}([p]) -->
<script src="../assets/noise.js"></script>
<!-- webgl: -->
<script>
let checker = true;
function Scene() {
//to make a scene now, we simply define a vertex map (where each thing is, and its size):
this.vertex_size = 8;
this.vertex_attributes = {
a_pos: { size: 3, position: 0 },
a_nor: { size: 3, position: 3 },
a_uv: { size: 2, position: 6 },
};
this.vertex_shader = `#version 300 es
uniform mat4 u_mf, u_mi;
in vec3 a_pos, a_nor;
in vec2 a_uv;
out vec2 v_uv;
out vec3 v_pos, v_nor;
const int max = 32;
uniform int u_n_blobs;
uniform mat4 u_insides[max], u_outsides[max], u_bm[max], u_bn[max];
float blob (vec4 p) {
return 1. - p.x * p.x - p.y * p.y - p.z * p.z;
}
void main(){
vec4 pos = vec4(a_pos, 1.);
vec4 nor = vec4(a_nor, 0.);
if (u_n_blobs > 0){
vec4 p = vec4(0.);
vec4 n = vec4(0.);
for (int i = 0; i < max; i++){
if (i >= u_n_blobs) break;
float outside = blob(u_outsides[i] * 5. * pos);
if (outside > 0.){
float inside = blob(u_insides[i] * .5 * pos);
float t = outside / (outside - inside);
p+= t * t * (u_bm[i] * pos);
n+= t * t * (nor * u_bn[i]);
}
}
pos = p;
nor = n;
}
pos = u_mf * pos;
nor = nor * u_mi;
v_pos = pos.xyz / pos.w;
v_pos.z *= -1.;
v_nor = nor.xyz;
v_uv = a_uv;
gl_Position = vec4(v_pos, 1.);
}`;
this.fragment_shader = `#version 300 es
precision highp float;
in vec3 v_pos, v_nor;
in vec2 v_uv;
out vec4 frag_col;
uniform vec3 u_amb, u_diff;
uniform vec4 u_spec;
uniform float u_time;
uniform sampler2D u_sampler[16];
uniform int u_texture;
void main() {
vec3 l_dir = normalize(vec3(-1., -1., 1.));
vec3 n = normalize(v_nor);
vec3 v = vec3(0., 0., 1.);
float sim = dot (n, l_dir);
vec3 ref = 2. * dot(n, l_dir) * n - l_dir;
vec3 light = u_amb + u_diff * max (0., sim) + u_spec.rgb * pow(max(0., dot(ref, v)), u_spec.w);
frag_col = vec4(sqrt(light), 1.);
}`;
//define variables:
let start_time = Date.now() / 1000;
let m = new Matrix();
//define shapes to be used:
this.init = () => {
set_uniform("1iv", "u_sampler", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
//load_texture(0, 'brick.png');
//loadTexture(1, 'brick.png');
};
let outsides = [];
let insides = [];
let bm = [];
let bn = [];
let grow_blob = (_i, r = 0.5) => {
let outside = _i.slice();
for (let i = 0; i < 12; i +=4){
let x = _i.slice(i, i+3);
[outside[i], outside[i+1], outside[i+2]] = scale(x, 1+r / v_length(x));
}
return outside;
}
let blobs_animate = (_m, r = .5) => {
for (let n in _m){
insides[n] = invert_matrix(_m[n]);
outsides[n] = invert_matrix(grow_blob(_m[n], r));
bm[n] = mxm(_m[n], insides[n]);
bn[n] = invert_matrix(_m[n]);
}
}
let blobs = (x,y,z) => {
let p = [x,y,z];
//unit sphere: 1 - x*x - y*y - z*z.
let S = p => 1 - p[0]*p[0] - p[1]*p[1] - p[2]*p[2];
//assume not on it:
let t = -1;
for (let n = 0; n < outsides.length; n++){
let p_outside = S(mxp(outsides[n], p));
if (p_outside > 0){
let p_inside = S(mxp(insides[n], p));
//now calculate distance:
let d = p_outside / (p_outside - p_inside);
t+= d * d;
}
}
return t;
}
const cols = 20;
const rows = cols;
const rad = 0.01;
const spacing = rad * 3;
const outer_rad= spacing;
const move = 0.01;
const dampen = 0.07;
let b = Array.from({length:cols * rows}, () => []);
this.update = () => {
//resize to active canvas:
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
gl.viewport(0, 0, canvas.width, canvas.height);
let t = Date.now() / 1000 - start_time;
set_uniform("1f", "u_time", t);
for (let row = 0; row < rows; row++){
for (let col = 0; col < cols; col ++){
let x = (col - (cols - 1) / 2) * spacing;
let y = (row - (rows - 1) / 2) * spacing;
x+=noise.perlin2(x, y * t % 60) * dampen;
y+=noise.perlin2(x * t % 60,y) * dampen;
m.identity();
m.move(x, y, 0);
m.scale(rad + (noise.perlin2(x * t,y) * 0.02));
m.copy_to(b[row * cols + col]);
}
}
blobs_animate(b, outer_rad);
m.identity();
m.push();
m.scale(canvas.height / canvas.width, 1, 1);
m.draw(volume_mesh(blobs, cols), Material.blue_plastic);
m.pop();
};
}
gl_start(canvas, new Scene());
//since i have frag in a different file:
//let scene = new Scene();
//fetch('./frag.frag').then(r => r.text()).then(src => {
// scene.fragment_shader = src;
// gl_start(canvas, scene);
//});
</script>
</body>
</html>