Planet Generator
How do I make an planet generator?
Procedural planet generator. press space to get a new planet. . What is a planet generator? How do you make a planet generator? This script and codes were developed by Khalkeus on 11 November 2022, Friday.
Planet Generator - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Planet Generator</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <!-- hit space to generate a new planet! --> <script src='https://cdnjs.cloudflare.com/ajax/libs/three.js/84/three.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Planet Generator - Script Codes CSS Codes
body, html{ width: 100%; height: 100%; margin: 0px; padding: 0px; overflow: hidden;
}
canvas{ width: 100%; height: 100%;
}
Planet Generator - Script Codes JS Codes
var scene, camera, renderer, canvas, ctx, p, directionalLight;
window.onload = function(){ init();
}
window.onresize = function(){ camera.aspect = window.innerWidth/window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight);
}
document.onkeydown = function(e){ var x = e.which || e.keyCode; if(x === 32){ scene.remove(p.obj) p = new planet(); }
}
function initThree(){ renderer = new THREE.WebGLRenderer({antialias: true}); scene = new THREE.Scene(); camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000); var light = new THREE.AmbientLight(0x992266); scene.add(light); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); camera.position.z = 20; scene.background = new THREE.Color(0x111111); canvas = document.querySelector('#b canvas');
}
var render = function () { requestAnimationFrame(render); p.obj.rotateZ(.01); renderer.render(scene, camera);
}
function init() { initThree(); p = new planet(); render();
}
function randomIntInRange(a, b){ return Math.floor(Math.random()*(b - a) + a);
}
function randomIntArr(a){ return Math.floor(Math.random()*(a[1] - a[0]) + a[0]);
}
function randomArr(a) { return (Math.random()*(a[1] - a[0]) + a[0]);
}
var planetRanges = { 'radius' : [1, 5], 'detail' : [0, 2], 'shells' : [1, 5], 'rings' : [3, 10], 'chanceShells' : .5, 'chanceRings' : .5,
}
function pertubSphere(sphereGeo){
}
function makeBase(material, radius, detail){ var planet = new THREE.Mesh(new THREE.IcosahedronGeometry(radius, detail), material); planet.geometry.computeFlatVertexNormals(); return planet;
}
function updateLight(baseRadius){ scene.remove(directionalLight); directionalLight = new THREE.DirectionalLight(0xffffff, 1); directionalLight.position.set(randomArr([-baseRadius*2, baseRadius*2]), randomArr([-baseRadius*2, baseRadius*2]), baseRadius); scene.add(directionalLight);
}
function makeShells(baseRadius, detail, material, obj){ material = material.clone(); var numShells = randomIntArr(planetRanges['shells']) + 1; var inc = baseRadius/randomArr([5, 10]); for(var i = 0; i < numShells; i++){ baseRadius += inc*i; var shell = new THREE.IcosahedronGeometry(baseRadius, detail); var numFacesToRemove = randomIntArr([shell.faces.length/2, shell.faces.length]); shell.computeFlatVertexNormals(); material.opacity = i/numShells/2; shell = new THREE.Mesh(shell, material); obj.add(shell) } return baseRadius;
}
function makeRings(radius, detail, material, obj){ radius += Math.random() * radius; var numRings = randomIntArr(planetRanges['rings']); var inc = radius/randomArr([1, 5]); material = material.clone(); material.opacity = 1; for(var i = 0; i < numRings; i++){ radius += inc; var ring = new THREE.TorusGeometry(radius, radius/30, 5, (detail + 1) * 30); ring.computeFlatVertexNormals(); ring = new THREE.Mesh(ring, material); obj.add(ring); material = material.clone(); }
}
function planet() { this.baseRadius = randomArr(planetRanges['radius']); this.currRadius = this.baseRadius; this.detail = randomIntArr(planetRanges['detail']); this.obj = new THREE.Group(); updateLight(this.baseRadius); this.baseColor = '#' + randomIntArr([0, 16777215]).toString(16); this.material = new THREE.MeshLambertMaterial({color: this.baseColor}); this.material.transparent = true; this.init = function(){ this.obj.add(makeBase(this.material, this.baseRadius, this.detail)); if(Math.random() < planetRanges['chanceShells']){ this.currRadius = makeShells(this.baseRadius, this.detail, this.material, this.obj); } if(Math.random() < planetRanges['chanceRings']){ makeRings(this.currRadius, this.detail, new THREE.MeshLambertMaterial({color: this.baseColor}), this.obj); } this.obj.rotation.x = Math.PI/2 + randomArr([-1, 1]); this.obj.rotation.y = randomArr([-1, 1]); this.obj.rotation.z = randomArr([-1, 1]); scene.add(this.obj); } this.init(); this.update = function(){ }
}
Developer | Khalkeus |
Username | khalkeus |
Uploaded | November 11, 2022 |
Rating | 3 |
Size | 2,984 Kb |
Views | 12,144 |
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!
Name | Size |
Boids i | 3,564 Kb |
Css loaders ii | 2,294 Kb |
Color Name Game | 3,245 Kb |
Live pixel sorting | 2,314 Kb |
City generator | 2,750 Kb |
Square loading icons | 2,249 Kb |
Boids ii | 3,358 Kb |
Flowers | 2,588 Kb |
Capybaras | 2,775 Kb |
Binary clock | 2,039 Kb |
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!
Name | Username | Size |
CSS Link Zoom On Click | Bryce | 1,960 Kb |
Image Hover | Johnheiner | 3,409 Kb |
A Pen by Mike Otis | Mikeotis | 3,185 Kb |
Css or Czz.. | Judag | 4,111 Kb |
Slider for stackoverflow | KKOKU | 3,222 Kb |
Caputre Cam with JS | KimmoCommit | 2,795 Kb |
SnappySnippet Test | Elmsoftware | 8,385 Kb |
Rainbow Drops | Csbarnes | 2,365 Kb |
Large canvas mousemove experiment | Jibbon | 2,885 Kb |
Barber Shop | Bhlaird | 6,270 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!