I'll help you create a procedurally generated planet with Three.js. This example will include terrain generation using noise functions, dynamic coloring based on elevation, and atmospheric effects.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Procedural Planet Generator</title>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
overflow: hidden;
background: #000;
}
#info {
position: absolute;
top: 10px;
left: 10px;
color: white;
background: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 5px;
}
#controls {
position: absolute;
top: 10px;
right: 10px;
color: white;
background: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 5px;
}
button {
margin: 5px;
padding: 5px 10px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="info">
<h3>Procedural Planet</h3>
<p>Left click + drag to rotate</p>
<p>Scroll to zoom</p>
</div>
<div id="controls">
<button onclick="regeneratePlanet()">New Planet</button>
<button onclick="toggleRotation()">Toggle Rotation</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/simplex-noise@3.0.0/simplex-noise.js"></script>
<script>
// Scene setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// Lighting
const ambientLight = new THREE.AmbientLight(0x404040, 0.5);
scene.add(ambientLight);
const sunLight = new THREE.DirectionalLight(0xffffff, 1);
sunLight.position.set(50, 50, 50);
sunLight.castShadow = true;
sunLight.shadow.camera.near = 0.1;
sunLight.shadow.camera.far = 200;
sunLight.shadow.camera.left = -50;
sunLight.shadow.camera.right = 50;
sunLight.shadow.camera.top = 50;
sunLight.shadow.camera.bottom = -50;
scene.add(sunLight);
// Camera position
camera.position.z = 3;
// Controls
let mouseX = 0, mouseY = 0;
let targetRotationX = 0, targetRotationY = 0;
let mouseDown = false;
let autoRotate = true;
document.addEventListener('mousedown', () => mouseDown = true);
document.addEventListener('mouseup', () => mouseDown = false);
document.addEventListener('mousemove', (event) => {
if (mouseDown) {
mouseX = (event.clientX / window.innerWidth) * 2 - 1;
mouseY = (event.clientY / window.innerHeight) * 2 - 1;
targetRotationY = mouseX * Math.PI;
targetRotationX = mouseY * Math.PI;
}
});
document.addEventListener('wheel', (event) => {
camera.position.z += event.deltaY * 0.002;
camera.position.z = Math.max(1.5, Math.min(10, camera.position.z));
});
// Planet generation
let planet, atmosphere;
const simplex = new SimplexNoise();
function generatePlanet() {
// Remove old planet
if (planet) {
scene.remove(planet);
planet.geometry.dispose();
planet.material.dispose();
}
if (atmosphere) {
scene.remove(atmosphere);
atmosphere.geometry.dispose();
atmosphere.material.dispose();
}
// Planet parameters
const radius = 1;
const detail = 128;
const noiseScale = Math.random() * 2 + 1;
const noiseStrength = Math.random() * 0.2 + 0.1;
const seed = Math.random() * 1000;
// Create sphere geometry
const geometry = new THREE.SphereGeometry(radius, detail, detail);
const positions = geometry.attributes.position.array;
const colors = [];
// Generate terrain
for (let i = 0; i < positions.length; i += 3) {
const x = positions[i];
const y = positions[i + 1];
const z = positions[i + 2];
// Get vertex position on unit sphere
const length = Math.sqrt(x * x + y * y + z * z);
const nx = x / length;
const ny = y / length;
const nz = z / length;
// Generate noise-based elevation
let elevation = 0;
let frequency = noiseScale;
let amplitude = noiseStrength;
// Octaves for more detail
for (let j = 0; j < 4; j++) {
elevation += simplex.noise3D(
nx * frequency + seed,
ny * frequency + seed,
nz * frequency + seed
) * amplitude;
frequency *= 2;
amplitude *= 0.5;
}
// Apply elevation
const finalRadius = radius + elevation;
positions[i] = nx * finalRadius;
positions[i + 1] = ny * finalRadius;
positions[i + 2] = nz * finalRadius;
// Color based on elevation
let color;
if (elevation < -0.05) {
// Deep ocean
color = new THREE.Color(0.1, 0.2, 0.5);
} else if (elevation < 0) {
// Shallow water
color = new THREE.Color(0.2, 0.4, 0.8);
} else if (elevation < 0.03) {
// Beach/sand
color = new THREE.Color(0.9, 0.8, 0.6);
} else if (elevation < 0.08) {
// Grass/forest
color = new THREE.Color(0.2, 0.6, 0.2);
} else if (elevation < 0.12) {
// Mountain
color = new THREE.Color(0.5, 0.4, 0.3);
} else {
// Snow
color = new THREE.Color(0.9, 0.9, 0.95);
}
colors.push(color.r, color.g, color.b);
}
// Update geometry
geometry.attributes.position.needsUpdate = true;
geometry.computeVertexNormals();
geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
// Create material
const material = new THREE.MeshPhongMaterial({
vertexColors: true,
shininess: 20,
flatShading: false
});
// Create planet mesh
planet = new THREE.Mesh(geometry, material);
planet.castShadow = true;
planet.receiveShadow = true;
scene.add(planet);
// Create atmosphere
const atmosphereGeometry = new THREE.SphereGeometry(radius * 1.1, 32, 32);
const atmosphereMaterial = new THREE.MeshBasicMaterial({
color: 0x4488ff,
transparent: true,
opacity: 0.1,
side: THREE.BackSide
});
atmosphere = new THREE.Mesh(atmosphereGeometry, atmosphereMaterial);
scene.add(atmosphere);
// Add clouds (optional)
const cloudGeometry = new THREE.SphereGeometry(radius * 1.02, 32, 32);
const cloudTexture = generateCloudTexture();
const cloudMaterial = new THREE.MeshBasicMaterial({
map: cloudTexture,
transparent: true,
opacity: 0.4,
side: THREE.DoubleSide
});
const clouds = new THREE.Mesh(cloudGeometry, cloudMaterial);
planet.add(clouds);
}
function generateCloudTexture() {
const canvas = document.createElement('canvas');
canvas.width = 512;
canvas.height = 256;
const ctx = canvas.getContext('2d');
// Generate cloud pattern
const imageData = ctx.createImageData(canvas.width, canvas.height);
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const x = (i / 4) % canvas.width;
const y = Math.floor((i / 4) / canvas.width);
const noise = simplex.noise2D(x * 0.01, y * 0.01) * 0.5 + 0.5;
const cloud = noise > 0.5 ? 255 : 0;
data[i] = 255; // R
data[i + 1] = 255; // G
data[i + 2] = 255; // B
data[i + 3] = cloud * noise; // A
}
ctx.putImageData(imageData, 0, 0);
return new THREE.CanvasTexture(canvas);
}
// Animation loop
function animate() {
requestAnimationFrame(animate);
if (planet) {
// Smooth rotation
planet.rotation.x += (targetRotationX - planet.rotation.x) * 0.05;
planet.rotation.y += (targetRotationY - planet.rotation.y) * 0.05;
// Auto rotation
if (autoRotate && !mouseDown) {
planet.rotation.y += 0.002;
targetRotationY = planet.rotation.y;
}
}
renderer.render(scene, camera);
}
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Control functions
function regeneratePlanet() {
generatePlanet();
}
function toggleRotation() {
autoRotate = !autoRotate;
}
// Add stars background
function createStars() {
const starsGeometry = new THREE.BufferGeometry();
const starsMaterial = new THREE.PointsMaterial({
color: 0xffffff,
size: 0.02,
sizeAttenuation: true
});
const starsVertices = [];
for (let i = 0; i < 10000; i++) {
const x = (Math.random() - 0.5) * 200;
const y = (Math.random() - 0.5) * 200;
const z = (Math.random() - 0.5) * 200;
starsVertices.push(x, y, z);
}
starsGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starsVertices, 3));
const stars = new THREE.Points(starsGeometry, starsMaterial);
scene.add(stars);
}
// Initialize
createStars();
generatePlanet();
animate();
</script>
</body>
</html>
This procedurally generated planet includes:
Features:
Terrain generation using Simplex noise for realistic elevation
Dynamic coloring based on elevation (ocean, beach, grass, mountains, snow)
Atmospheric glow effect
Procedural cloud layer
Interactive controls (mouse rotation and zoom)
Starfield background
Button to generate new random planets
Technical Details:
Uses multiple octaves of noise for detailed terrain
Vertex colors for efficient rendering
Proper lighting with shadows
Smooth camera controls
Responsive design
Customization Options:
You can modify various parameters to create different planet types:
Adjust noiseScale for continent size
Change noiseStrength for mountain height
Modify color thresholds for different biomes
Add more noise octaves for finer detail
Implement different noise patterns for varied terrain
The planet generation is completely procedural, so each time you click "New Planet", you'll get a unique world with different continents, oceans, and mountain ranges.