based on Clay John's tutorial : https://steemit.com/utopian-io/@clayjohn/learning-3d-graphics-with-three-js-or-dynamic-geometry
Created
October 19, 2021 08:14
-
-
Save nirmalrepo/07348ad1577b0c7d4a513bf28aab5b68 to your computer and use it in GitHub Desktop.
Three.JS dynamic blob with Perlin noise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<canvas id="canvas"></canvas> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var renderer = new THREE.WebGLRenderer({ canvas : document.getElementById('canvas'), antialias:true}); | |
// default bg canvas color // | |
renderer.setClearColor(0x7b7b7b); | |
// use device aspect ratio // | |
renderer.setPixelRatio(window.devicePixelRatio); | |
// set size of canvas within window // | |
renderer.setSize(window.innerWidth, window.innerHeight); | |
var scene = new THREE.Scene(); | |
var camera = new THREE.PerspectiveCamera( 45, window.innerWidth/window.innerHeight, 0.1, 1000 ); | |
camera.position.z = 5; | |
var sphere_geometry = new THREE.SphereGeometry(1, 128, 128); | |
var material = new THREE.MeshNormalMaterial(); | |
var sphere = new THREE.Mesh(sphere_geometry, material); | |
scene.add(sphere); | |
var update = function() { | |
// change '0.003' for more aggressive animation | |
var time = performance.now() * 0.003; | |
//console.log(time) | |
//go through vertices here and reposition them | |
// change 'k' value for more spikes | |
var k = 3; | |
for (var i = 0; i < sphere.geometry.vertices.length; i++) { | |
var p = sphere.geometry.vertices[i]; | |
p.normalize().multiplyScalar(1 + 0.3 * noise.perlin3(p.x * k + time, p.y * k, p.z * k)); | |
} | |
sphere.geometry.computeVertexNormals(); | |
sphere.geometry.normalsNeedUpdate = true; | |
sphere.geometry.verticesNeedUpdate = true; | |
} | |
function animate() { | |
//sphere.rotation.x += 0.01; | |
//sphere.rotation.y += 0.01; | |
update(); | |
/* render scene and camera */ | |
renderer.render(scene,camera); | |
requestAnimationFrame(animate); | |
} | |
requestAnimationFrame(animate); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r83/three.js"></script> | |
<script src="https://fariskassim.com/stage/rebel9/teaf/blob/v4/js/perlin.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html, body { | |
margin:0; | |
overflow:hidden | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment