Perlin metaball

Developer
Size
7,396 Kb
Views
12,144

How do I make an perlin metaball?

Play with code and uncomment some of the commented lines to change the noise result.. What is a perlin metaball? How do you make a perlin metaball? This script and codes were developed by Endre Simo on 28 November 2022, Monday.

Perlin metaball Previews

Perlin metaball - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Perlin metaball</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <header> <span id="wrap"></span> <span id="arrow"></span> <h1>HTML5 Perlin Noise simmulation</h1> <span class='header-open'> click on <label id="label">+</label> for more info </span> <!-- Description about the experiment--> <div class="description"> <div class="horizontal-line"> <span style="width: 100%"></span> </div> <section id="about"> <p id accesskey="a" class="about"> HTML5 adapted perlin noise simulation. <br/>The perlin noise algorithm was ported to JS from the original Java class <a target="_blank" href="https://mrl.nyu.edu/~perlin/noise/">https://mrl.nyu.edu/~perlin/noise/</a>. This is <a href="http://staffwww.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf" target='blank'>the paper</a> written by Stefan Gustavson which explains thoroughly the Perlin noise method. I relied to it a lot during implementation.<br/> </p> </section> </div> <div class="vertical-line"></div>
</header>
<div class="wrapper"> <div class="context"></div> <h1>Hold the mouse and drag for panning</h1>
</div> <script src="js/index.js"></script>
</body>
</html>

Perlin metaball - Script Codes CSS Codes

@import "https://fonts.googleapis.com/css?family=Dosis:600|Roboto+Slab:100,300,400,700";
@import url(https://fonts.googleapis.com/css?family=Roboto+Condensed:300);
html, body { width: 100%; height: 100%; overflow: hidden; background: #111; margin: 0; padding: 0; -webkit-user-select: none; -moz-user-select: none; -khtml-user-select: none; user-select: none;
}
/*******************************
* HEADER *
********************************/
header { position: absolute; background: rgba(14, 14, 14, 0.4); opacity: 0.7; z-index: 10; height: 40px; width: 100%; padding: 0 8px 8px 8px; overflow: hidden; font-family: "Roboto Slab",​sans-serif; font-size: 16px; color: #fff; text-shadow: 1px 1px 1px #333; text-decoration: none; display: inline-block; box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.80); -moz-box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.80); -webkit-box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.80); -webkit-transition: height .20s ease-in-out; -moz-transitoon: height .20s ease-in-out; -o-transition: height .20s ease-in-out; transition: height .20s ease-in-out;
}
header #arrow { color: #adff2f; float: left; margin-right: 15px; margin-top: 8px; font-family: "Times New Roman", sans-serif; font-size: 26px; z-index: 2; cursor: pointer; -webkit-transition: all 0.9s cubic-bezier(0.165, 0.84, 0.44, 1) 0s; -moz-transition: all 0.9s cubic-bezier(0.165, 0.84, 0.44, 1) 0s; -o-transition: all 0.9s cubic-bezier(0.165, 0.84, 0.44, 1) 0s; -ms-transition: all 0.9s cubic-bezier(0.165, 0.84, 0.44, 1) 0s; transition: all 0.9s cubic-bezier(0.165, 0.84, 0.44, 1) 0s; -webkit-transform:rotate(0deg); -moz-transform:rotate(0deg); -o-transform:rotate(0deg); -ms-transform:rotate(0deg); transform: rotate(0deg);
}
#arrow.animate { -webkit-transition: all 0.9s cubic-bezier(0.165, 0.84, 0.44, 1) 0s; -moz-transition: all 0.9s cubic-bezier(0.165, 0.84, 0.44, 1) 0s; -o-transition: all 0.9s cubic-bezier(0.165, 0.84, 0.44, 1) 0s; -ms-transition: all 0.9s cubic-bezier(0.165, 0.84, 0.44, 1) 0s; transition: all 0.9s cubic-bezier(0.165, 0.84, 0.44, 1) 0s; -webkit-transform:rotate(135deg); -moz-transform:rotate(135deg); -o-transform:rotate(135deg); -ms-transform:rotate(135deg); transform: rotate(135deg);
}
header #arrow:before { content: "\2b";
}
.header-open #label { color: #fff;
}
header #wrap { position: absolute; width: 100%; height: 100%; opacity: 0.4; z-index: -10;
}
header h1 { cursor: default; font-size: 90%; font-weight: 200; float: left; position: relative; display: inline-block; text-transform: uppercase; vertical-align: middle; z-index: 3; opacity: 0.9;
}
header .header-open { clear: both; float: left; margin-top: -10px; padding: 0 0 5px; padding-left: 128px; display: inline-block; text-decoration: none; text-transform: none; font-family: "Molengo", Times, "Times New Roman", cursive; font-weight: normal; font-size: 10pt; color: GrayText; -webkit-transition: opacity .4s cubic-bezier(0.215, 0.610, 0.355, 1.000); -moz-transition: opacity .4s cubic-bezier(0.215, 0.610, 0.355, 1.000); -ms-transition: opacity .4s cubic-bezier(0.215, 0.610, 0.355, 1.000); -o-transition: opacity .4s cubic-bezier(0.215, 0.610, 0.355, 1.000); transition: opacity .4s cubic-bezier(0.215, 0.610, 0.355, 1.000);
}
/*******************************
* Header description
********************************/
header .description { clear: both; position: absolute; padding: 5px 10px; font-family: "Roboto Condensed", Helvetica, Arial, sans-serif; font-weight: normal; text-align: justify; color: #dcdcdc; font-size: 11pt; width: 400px; margin: 55px 20px; background-color:rgba(0,0,0,.2); -webkit-border-radius:4px; -moz-border-radius:4px; -ms-border-radius:4px; border-radius:4px; -webkit-box-shadow:0 1px 0 rgba(255,255,255,.05),inset 0 1px 0 rgba(0,0,0,.2); -moz-box-shadow:0 1px 0 rgba(255,255,255,.05),inset 0 1px 0 rgba(0,0,0,.2); -ms-box-shadow:0 1px 0 rgba(255,255,255,.05),inset 0 1px 0 rgba(0,0,0,.2); box-shadow:0 1px 0 rgba(255,255,255,.05),inset 0 1px 0 rgba(0,0,0,.2); -webkit-transition: opacity .4s cubic-bezier(0.215, 0.610, 0.355, 1.000); -moz-transition: opacity .4s cubic-bezier(0.215, 0.610, 0.355, 1.000); -ms-transition: opacity .4s cubic-bezier(0.215, 0.610, 0.355, 1.000); -o-transition: opacity .4s cubic-bezier(0.215, 0.610, 0.355, 1.000); transition: opacity .4s cubic-bezier(0.215, 0.610, 0.355, 1.000);
}
.horizontal-line { height: 1px; margin-top: -20px; padding-top: 5px; position: relative; border-top-style: solid; border-top-width: 1px; border-top-color: rgba(247,247,247, 0.6); border-bottom-style: hidden; border-left-style: hidden; border-right-style: hidden; opacity: 0; -webkit-transition: opacity .4s ease-out; -moz-transition: opacity .4s ease-out; -ms-transition: opacity .4s ease-out; -o-transition: opacity .4s ease-out; transition: opacity .4s ease-out;
}
span[article] { font-style: italic;
}
header h2 { text-transform: uppercase; font-size: 10pt; margin-top: 14px; margin-left: 20px; color: #fff;
}
/*******************************
* Header dropdown animation
********************************/
header.animate { height: 200px;
}
header.animate .horizontal-line { margin-top: -40px; opacity: 1;
}
header.animate .header-open { opacity: 0;
}
header.animate .description { padding-top: 25px;
}
a { text-decoration: none; color: #adff2f;
}
/*******************************
* Main display
********************************/
div#main { position:relative!important; display: block!important; width: 900px!important; height: 600px!important; width: 100%; margin: 0 auto; border: 1px solid #222; -webkit-border-radius: 2px; -moz-border-radius: 2px; -ms-border-radius: 2px; -o-border-radius: 2px; border-radius: 2px; -webkit-box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2); -moz-box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2); box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2);
}
div.context { position:realtive; width: 820px; height: 620px; margin: 0 auto; border: 1px solid #222; -webkit-border-radius: 2px; -moz-border-radius: 2px; -ms-border-radius: 2px; -o-border-radius: 2px; border-radius: 2px; -webkit-box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2); -moz-box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2); box-shadow: 0px 2px 17px rgba(0,0,0,.80), inset 0 0 15px 15px rgba(5,5,5,.2);
}
div.wrapper { position: absolute; width: 100%; margin: 0; padding: 0; top: 160px;
}
.context #canvas { position: relative; width: 800px; height: 600px; left: 10px; top: 10px;
}
.no-canvas { color: #999999; font-size: 24px; text-align: center; margin-top: 150px;
}
h1 { font-size: 26px; font-family: "Roboto Slab",​sans-serif; font-weight: 200; margin: 0 auto; width:24%; color: #fff; opacity: 0.6; padding: 10px 0;
}

Perlin metaball - Script Codes JS Codes

/*******************************
* HEADER *
********************************/
window.onload = function() { var toggleMouseDown = true; var toggleTimer = -1; var headerArrow = document.querySelector('#arrow'); var header = document.getElementsByTagName('header')[0]; document.addEventListener('click', onMouseClick, false); document.addEventListener('mousedown', onMouseDown, false); function onMouseClick(e) { e.stopPropagation(); toggleMouseDown = true; if (e.target === headerArrow && toggleMouseDown) { if (header.className === '') { clearTimeout(toggleTimer); toggleMouseDown = false; toggleTimer = setTimeout(function() { header.setAttribute('class', 'animate'); headerArrow.setAttribute('class', 'animate'); }, 1000/60); } else if (header.className === 'animate') { clearTimeout(toggleTimer); toggleTimer = setTimeout(function() { header.setAttribute('class', ''); headerArrow.setAttribute('class', ''); }, 1000/60); } } } function onMouseDown(e) { e.stopPropagation(); removeEventListener('click', onMouseClick, false); if (e.target !== headerArrow) { clearTimeout(toggleTimer); toggleTimer = setTimeout(function() { header.setAttribute('class', ''); headerArrow.setAttribute('class', ''); }, 1000/60); } else if (!toggleMouseDown) { toggleMouseDown = true; clearTimeout(toggleTimer); header.setAttribute('class', 'animate'); headerArrow.setAttribute('class', 'animate'); } } function getArrow(elt) { var supportCSS3 = document.body.style['MozPerspective'] !== undefined || document.body.style['webkitPerspective'] !== undefined; if (supportCSS3) { var selector = document.querySelectorAll(elt); var nodeLength = selector.length; for (var i=0; i< nodeLength; i++) { var node = selector[i]; if (node.className && !node.class.match('/animate/g')) { node.className += 'animate'; node.innerHTML = '<span data-name="' + node.text + '">' + node.innerHTML + '</span>'; } } } }
};
var NOISE = NOISE || { };
NOISE.Perlin = (function() { var iOctaves = 1, fPersistence = 0.2, fResult, fFreq, fPers, aOctFreq, // frequency per octave aOctPers, // persistance per octave fPersMax; // 1 / max persistence var octaveFreq = function() { var fFreq, fPers; aOctFreq = new Array(); aOctPers = new Array(); fPersMax = 0; for (var i=0; i < iOctaves; i++) { fFreq = Math.pow(2,i); fPers = Math.pow(fPersistence, i); fPersMax += fPers; aOctFreq.push(fFreq); aOctPers.push(fPers); } fPersMax = 2 / fPersMax; } var perm = new Uint8Array(512); var p = new Uint8Array(256); var grad3 = [[1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0], [1,0,1],[-1,0,1],[1,0,-1],[-1,0,-1], [0,1,1],[0,-1,1],[0,1,-1],[0,-1,-1]]; // Return the dot product for 2d perlin noise function dot2(g, x, y) { return g[0]*x + g[1]*y; } // Return the dot product for 3d perlin noise function dot3(g, x, y, z) { return g[0]*x + g[1]*y + g[2]*z; } // Seeded random number generator function seed(x) { x = (x<<13) ^ x; return ( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0); } function init() { for (var i = 0; i < 256; i++) { p[i] = Math.abs(~~(seed(i) * 256)); } // To remove the need for index wrapping, double the permutation table length for (var i=0; i < 512; i++) { perm[i] = p[i & 255]; } } /* ** 2D Simplex Noise */ function noise2D (x, y, z) { // Find unit grid cell containing point var X = Math.floor(x) & 255; var Y = Math.floor(y) & 255; // Get relative xyz coordinates of point within that cell x -= Math.floor(x); y -= Math.floor(y); var fade = function(t) { return t * t * t * (t * (t * 6.0 - 15.0) + 10.0); }; var lerp = function(a, b, t) { return (1.0-t)*a + t*b; } var u = fade(x), v = fade(y); // Calculate a set of four hashed gradient indices var n00 = perm[X + perm[Y ]] % 12; var n01 = perm[X + perm[Y+1]] % 12; var n10 = perm[X+1 + perm[Y+1]] % 12; var n11 = perm[X+1 + perm[Y+1]] % 12; // Calculate noise contributions from each of the four corners var gi00 = dot2(grad3[n00], x, y ); var gi01 = dot2(grad3[n01], x, y-1); var gi10 = dot2(grad3[n10], x-1, y ); var gi11 = dot2(grad3[n11], x-1, y-1); // Interpolate the results along axises return lerp( lerp(gi00, gi10, u), lerp(gi01, gi11, u), v); } /* ** 3D Simplex Noise */ function noise3D (x, y, z) { // Find unit grid cell containing point var X = Math.floor(x) & 255; var Y = Math.floor(y) & 255; var Z = Math.floor(z) & 255; // Get relative xyz coordinates of point within that cell x -= Math.floor(x); y -= Math.floor(y); z -= Math.floor(z); var fade = function(t) { return t * t * t * (t * (t * 6.0 - 15.0) + 10.0); }; var lerp = function(a, b, t) { return (1.0-t)*a + t*b; } var u = fade(x), v = fade(y), w = fade(z); // Calculate a set of eight hashed gradient indices var n000 = perm[X + perm[Y + perm[Z ]]] % 12; var n001 = perm[X + perm[Y + perm[Z+1]]] % 12; var n010 = perm[X + perm[Y+1+perm[Z ]]] % 12; var n011 = perm[X + perm[Y+1+perm[Z+1]]] % 12; var n100 = perm[X+1+perm[Y + perm[Z ]]] % 12; var n101 = perm[X+1+perm[Y + perm[Z+1]]] % 12; var n110 = perm[X+1+perm[Y+1+perm[Z ]]] % 12; var n111 = perm[X+1+perm[Y+1+perm[Z+1]]] % 12; // Calculate noise contributions from each of the eight corners var gi000 = dot3(grad3[n000], x, y, z ); var gi001 = dot3(grad3[n001], x, y, z-1); var gi010 = dot3(grad3[n010], x, y-1, z ); var gi011 = dot3(grad3[n011], x, y-1, z-1); var gi100 = dot3(grad3[n100], x-1, y, z ); var gi101 = dot3(grad3[n101], x-1, y, z-1); var gi110 = dot3(grad3[n110], x-1, y-1, z ); var gi111 = dot3(grad3[n111], x-1, y-1, z-1); // Interpolate the results along axises return lerp( lerp( lerp(gi000, gi100, u), lerp(gi001, gi101, u), w), lerp( lerp(gi010, gi110, u), lerp(gi011, gi111, u), w), v); } function PerlinNoise(){} PerlinNoise.prototype = { init : init, noise : function(x, y, z) { fResult = 0; for (var i=0; i < iOctaves; i++) { fFreq = aOctFreq[i]; fPers = aOctPers[i]; switch(arguments.length) { case 3 : fResult += fPers * noise3D(fFreq*x, fFreq*y, fFreq*z); break; case 2 : fResult += fPers * noise2D(fFreq*x, fFreq*y); break; default : fResult += fPers * noise3D(fFreq*x, fFreq*y, fFreq*z); break; } } return (fResult * fPersMax + 0.8) * 0.5 }, noiseDetail : function(octaves, persistance) { iOctaves = octaves || iOctaves; fPersistence = persistance || fPersistence; octaveFreq(); } } return PerlinNoise;
}).call(this);
/*******************************
* MAIN IMPLEMENTATION *
********************************/
// Request animation frame
(function() { var lastTime = 0; var vendors = ['ms', 'moz', 'webkit', 'o']; for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; } if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id) { clearTimeout(id); };
}());
var perlin = new NOISE.Perlin();
perlin.init();
perlin.noiseDetail(1, 2.8);
var canvas = document.createElement('canvas');
canvas.setAttribute('width', '384');
canvas.setAttribute('height', '320');
canvas.setAttribute('id', 'canvas');
var mainContext = document.getElementsByClassName('context')[0];
mainContext.appendChild(canvas);
var ctx = canvas.getContext('2d');
var cx = 0, cy = 0, lastX = 0, lastY = 0, dx = 0, dy = 0;
var valx = 1, valy = 1;
var mouseDown = false;
var image = ctx.createImageData(canvas.width, canvas.height);
var data = image.data;
var fTime = 0, fDeltaT = 0, fLastTime = 0;
if (!this.millis) { var millis = function() { return new Date().getTime(); };
}
function getScrollX() { return window.pageXOffset || window.document.documentElement.scrollLeft;
}
function getScrollY() { return window.pageYOffset || window.document.documentElement.scrollTop;
}
function getMousePos(event) { event.preventDefault(); dx = event.pageX - (getScrollX() + canvas.getBoundingClientRect().left); dy = event.pageY - (getScrollY() + canvas.getBoundingClientRect().top) ; lastX = event.pageX - (getScrollX() + canvas.getBoundingClientRect().left) - cx; lastY = event.pageY - (getScrollY() + canvas.getBoundingClientRect().top) - cy; return { x: lastX, y: lastY }
}
function getTouchPos(event) { event.preventDefault(); dx = event.changedTouches[0].pageX - (getScrollX() + canvas.getBoundingClientRect().left) - lastX; dy = event.changedTouches[0].pageY - (getScrollY() + canvas.getBoundingClientRect().top) - lastY; lastX = event.changedTouches[0].pageX - (getScrollX() + canvas.getBoundingClientRect().left); lastY = event.changedTouches[0].pageY - (getScrollY() + canvas.getBoundingClientRect().top); lastX = lastX > 0 ? (lastX < canvas.width ? lastX : canvas.width) : 0; lastY = lastY > 0 ? (lastY < canvas.height ? lastY : canvas.height) : 0; return { x: lastX, y: lastY }
}
window.addEventListener('resize', function() { var canvasWidth = window.getComputedStyle(canvas, null).getPropertyValue('width'); var canvasHeight = window.getComputedStyle(canvas, null).getPropertyValue('height'); canvasWidth = parseInt(canvasWidth.substring(-2)); canvasHeight = parseInt(canvasHeight.substring(-2)); var container = document.getElementsByClassName('wrapper')[0]; container.style.top = ( window.innerHeight - canvasHeight ) / 2 + 'px';
});
canvas.addEventListener('mousemove', onMouseMove);
canvas.addEventListener('mousedown', function(event) { event.preventDefault(); cx = event.pageX - (getScrollX() + canvas.getBoundingClientRect().left) - lastX; cy = event.pageY - (getScrollY() + canvas.getBoundingClientRect().top) - lastY; mouseDown = true;
});
canvas.addEventListener('mouseup', function(event) { event.preventDefault(); mouseDown = false;
});
// Mobile devices touch events
canvas.addEventListener('touchstart', function(event) { event.preventDefault(); lastX = event.changedTouches[0].pageX - (getScrollX() + canvas.getBoundingClientRect().left); lastY = event.changedTouches[0].pageY - (getScrollY() + canvas.getBoundingClientRect().top); mouseDown = true;
});
canvas.addEventListener('touchmove', onTouchMove);
canvas.addEventListener('touchend', function(event) { event.preventDefault(); mouseDown = false;
});
function onMouseMove(e) { if (getMousePos(e).y <= 40) return; if (mouseDown) { valx = dx * 0.02; valy = dy * 0.02; } else { if (dx == undefined) return; }
}
function onTouchMove(e) { if (getTouchPos(e).y <= 40) return; if (mouseDown) { valx = dx * 0.02; valy = dy * 0.02; } else { if (dx == undefined) return; }
}
function draw() { fDeltaT = millis()-fLastTime; fLastTime = millis(); var start = Date.now(); fTime += fDeltaT * 0.0005; for (var x = 0; x < canvas.width; x++) { for (var y = 0; y < canvas.height; y++) { var fXDff = x-.5*canvas.width; var fYDff = y-.5*canvas.height; var fDst = Math.sqrt(fXDff*fXDff+fYDff*fYDff) / canvas.width; //var value = Math.floor(fDst*fDst*(perlin.noise(x / 50, y / 50, fTime) )*1096); //var value = Math.abs(perlin.noise(x / 50, y / 50, fTime) * .08) * 1096; //var value = 256 - Math.floor(Math.pow(perlin.noise(x / 100, y / 100, fTime) * 4, y)); //var value = Math.abs(perlin.noise(10*x / 100, y*0.5 / 100, fTime)); var value = Math.abs(perlin.noise(x / 60 * (valx / valy), y / 60 * (valx / valy), fTime)); value *= 256; var cell = (x + y * canvas.width) * 4; data[cell] = data[cell + 1] = data[cell + 2] = value; data[cell] += Math.max(0, (25 - value) * 8); //data[cell] *= Math.random() * Math.max((valx / valy) * 0.001, 1) - 1; data[cell + 3] = 255; // alpha. } } var end = Date.now(); ctx.fillColor = 'black'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.putImageData(image, 0, 0); ctx.font = '12px sans-serif' ctx.textAlign = 'center';
}
render();
function render() { var id = requestAnimationFrame(render); draw();
};
Perlin metaball - Script Codes
Perlin metaball - Script Codes
Home Page Home
Developer Endre Simo
Username esimov
Uploaded November 28, 2022
Rating 3.5
Size 7,396 Kb
Views 12,144
Do you need developer help for Perlin metaball?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Endre Simo (esimov) Script Codes
Create amazing web content with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!