Contoured Mandelbulb

Contour lines on raymarched Mandelbulb.

Log in to post a comment.

// LL 2021

const turtle = new Turtle();

//Canvas.setpenopacity(-1);

const detail_z = 201;  // min=1, max=301, step=2
const detail_xy = 1.;  // min=0.1, max=3, step=0.1
const passes = 3;      /// min=1, max=3, step=1 (X, XY, XYZ)
const iTime = 2.05;    // min=0, max=12, step=0.01

const MAX_STEPS = 256;
const MAX_DIST = 1000;
const SURF_DIST = .001;

const range = [ -2, 2 ];

function length2(v2) { return Math.sqrt(v2[0]*v2[0] + v2[1]*v2[1]); }
function length3(v3) { return Math.sqrt(v3[0]*v3[0] + v3[1]*v3[1] + v3[2]*v3[2]); }
function normalize3(v3) { var l = length3(v3); if (l<0.00001) l=1; return [v3[0]/l, v3[1]/l, v3[2]/l]; }
function mul2(a2, f) { return [a2[0]*f, a2[1]*f]; }
function mul3(a3, f) { return [a3[0]*f, a3[1]*f, a3[2]*f]; }
function add3(a3, b3) { return [a3[0]+b3[0], a3[1]+b3[1], a3[2]+b3[2]]; }
function sub3(a3, b3) { return [a3[0]-b3[0], a3[1]-b3[1], a3[2]-b3[2]]; }
function cross3(a3, b3) { return [ a3[1] * b3[2] - a3[2] * b3[1], a3[2] * b3[0] - a3[0] * b3[2], a3[0] * b3[1] - a3[1] * b3[0] ]; }
function fract3(v3) { return [ Math.trunc(v3[0]), Math.trunc(v3[1]), Math.trunc(v3[2]) ]; }
function abs3(v3) { return [ Math.abs(v3[0]), Math.abs(v3[1]), Math.abs(v3[2]) ]; }
function dot3(a3, b3) { return a3[0] * b3[0] + a3[1] * b3[1] + a3[2] * b3[2]; }
function mul_mat2(v2, m22) { return [ v2[0] * m22[0] + v2[1] * m22[1], v2[0] * m22[2] + v2[1] * m22[3] ]; }
function clamp(x, min, max) { return Math.min(max, Math.max(min, x)); }
function smoothstep(edge0, edge1, x) { x = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0); return x * x * (3 - 2 * x); }
function tri3(x3) { return abs3(sub3(fract3(x3), .5)); }
function mix(x, y, a) { return x * (1-a) + y * a; }
function mix3(a3, b3, f) { return [ mix(a3[0], b3[0], f), mix(a3[1], b3[1], f), mix(a3[2], b3[2], f) ]; }
function smin(a, b , s) { var h = clamp( 0.5 + 0.5*(b-a)/s, 0. , 1.); return mix(b, a, h) - h*(1.0-h)*s; }

function rotation_mat22(a)
{
    var s = Math.sin(a);
    var c = Math.cos(a);
    return [c, -s, s, c];
}

function GetRayDir(uv2, p3, l3, z)
{
    var f3 = normalize3(sub3(l3, p3));
    var r3 = normalize3(cross3([0,1,0], f3));
    var u3 = cross3(f3, r3);
    var c3 = mul3(f3, z);
    var i3 = add3(add3(c3, mul3(r3, uv2[0])), mul3(u3, uv2[1]));
    var d3 = normalize3(i3);
    return d3;
}

function sdBox(p3, s3)
{
    p3 = sub3(abs3(p3), s3);
    var r = Math.min(Math.max(p3[0], Math.max(p3[1], p3[2])), 0);
    var q3 = [Math.max(p3[0], 0), Math.max(p3[1], 0), Math.max(p3[2], 0)];
    q3 = add3(q3, [r,r,r]);
	return length3(q3);
}

function sdMandelbulb(time, p3, radius)
{
	p3 = [ -p3[0], -p3[2], -p3[1] ];
	var q3 = [...p3];

	const ITERATIONS = 64;
	if (time < 0) time = Math.abs(time) * 0.3;
	var power = 2 + 10 * (0.5 + 0.5 * Math.cos(2.7 + time * 0.5));
	const minimumDistanceToSurface = 0.0003;
	
	var externalBoundingRadius = 1.1;
	var r = length3(p3) - externalBoundingRadius;
	if (r > 1.0) { return r; }

	var internalBoundingRadius = 0.4;

	var derivative = 1.0;
	
	for (var i = 0; i < ITERATIONS; i++)
	{
		const r = length3(q3);
		
		if (r > 2.0)
		{
			return Math.min(length3(p3) - internalBoundingRadius, 0.5 * Math.log(r) * r / derivative);
		}
		else
		{		
			const theta = Math.acos(q3[2] / r) * power;
			const phi   = Math.atan(q3[1] / q3[0]) * power;			

			derivative = Math.pow(r, power - 1.0) * power * derivative + 1.0;
			
			const sinTheta = Math.sin(theta);
			
			q3 = [ sinTheta * Math.cos(phi), sinTheta * Math.sin(phi), Math.cos(theta) ];
			q3 = add3(mul3(q3, Math.pow(r, power)), p3);
		}			
	}

	return minimumDistanceToSurface;
}

function map(p3)
{
    var d = MAX_DIST;
    
    d = Math.min(d, sdMandelbulb(iTimeT, add3(p3, [0, 1, 0]), 1));
    d = Math.min(d, sdBox(add3(p3, [0,-2,0]), [0.5, 2, 0.5]));
    d = Math.min(d, -0.1 + sdBox(add3(p3, [0,-0.01,0]), [0.7, 0.01, 0.7]));
    
    return d;
}

function RayMarch(ro3, rd3)
{
	var dO = 0;
    
    for (var i = 0; i < MAX_STEPS; i++)
    {
    	var p3 = add3(ro3, mul3(rd3, dO));
        var dS = map(p3);
        dO += dS;
        if (dO > MAX_DIST) return MAX_DIST;
        if (Math.abs(dS)<SURF_DIST) break;
    }
    
    return dO;
}

var pass = -1;

function key(p2) { return p2[0] * 10000 + p2[1] * 10 + pass; }

var cache = {};

// p: 2D point in -100 to 100 range
function zFunc(p2)
{
    var key_p2 = key(p2);
    
    if (key_p2 in cache)
    {
        var dist = cache[key_p2];
        return dist;
    }

    // Convert to -1 to 1
    var uv2 = [ p2[0] / 100, p2[1] / 100 ];

    // Ray origin
    var ro3 = [ -1, -1, -1.7 ];

    var l3 = [ 0, -0.8, 0 ];

    // Ray direction
    var rd3 = GetRayDir(uv2, ro3, l3, 1.);

    // Get distance to intersection
    var dist = RayMarch(ro3, rd3);

    if (dist < MAX_DIST)
    {
        // Get intersection point
        var p3 = add3(ro3, mul3(rd3, dist));
        dist = (p3[pass]-range[0]) / (range[1]-range[0]) * detail_z;
    }

    cache[key_p2] = dist;
    
    return dist;
}

function floor(x) { return Math.round(x*10); }

function line_key(line)
{
    return floor(line[0][0]) * 1000000000 +  floor(line[0][1]) * 1000000 +  floor(line[1][0]) * 1000 + floor(line[1][1]);
}

var line_cache = {};
function is_good(line)
{
    var key = line_key(line);
    if (key in line_cache) return false;
    line_cache[key]=1;
    return true;
}

var iTimeT = iTime;

function walk(i, t)
{
    //iTimeT = iTime + t * 6.283185; // Doesn't work
    
    pass = Math.floor(i / (detail_z+1));
    if (pass >= passes) return false;

    const lines = ContourLines(i%detail_z, 1/detail_xy, zFunc);
    lines.forEach(line => {
        if (is_good(line))
        {
            turtle.jump(line[0]);
            turtle.goto(line[1]);
        }
    });
    
    return true;
}

// Metaball Contour Lines. Created by Reinder Nijhoff 2020 - @reindernijhoff
// The MIT License
// https://turtletoy.net/turtle/104c4775c5
function ContourLines(z, step, zFunc) {
    const intersectSegmentZ = (z, v1, v2) => {
    	if (v1[2] === v2[2]) return false;
    	const t = (z - v1[2]) / (v2[2] - v1[2]);
    	if (t <= 0 || t > 1) return false;
    	return [v1[0]+(v2[0]-v1[0])*t, v1[1]+(v2[1]-v1[1])*t];
    }
    const intersectTriangleZ = (z, p1, p2, p3) => {
        const p = [];
    	const v1 = intersectSegmentZ(z, p1, p2);
    	const v2 = intersectSegmentZ(z, p2, p3);
    	const v3 = intersectSegmentZ(z, p3, p1);
    	if (v1 && v2) p.push([v1, v2]);
        if (v1 && v3) p.push([v1, v3]);
    	if (v2 && v3) p.push([v2, v3]);
		return p;
    }
	const result = [];
	for (let x = -100; x <= 100; x += step) {
    	for (let y = -100; y <= 100; y += step) {
			const corners = [[x, y], [x+step, y], [x+step, y+step], [x, y+step]];
			corners.forEach( c => c[2] = zFunc(c) );
			const c3 = [x+step/2, y+step/2, zFunc([x+step/2, y+step/2])];
			for (let i=0; i<4; i++) {
			    result.push(...intersectTriangleZ(z, corners[i], corners[(i+1) & 3], c3));
			}
		}
	}
	return result;
}