Planet Generator

Developer
Size
2,984 Kb
Views
12,144

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 Previews

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(){	}
}
Planet Generator - Script Codes
Planet Generator - Script Codes
Home Page Home
Developer Khalkeus
Username khalkeus
Uploaded November 11, 2022
Rating 3
Size 2,984 Kb
Views 12,144
Do you need developer help for Planet Generator?

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!

Khalkeus (khalkeus) 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!