Here's a complete implementation of a procedurally generated planet using Three.js. This example creates a textured sphere with randomized terrain features.
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 with Three.js</title>
<style>
body { margin: 0; overflow: hidden; }
canvas { display: block; }
</style>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/examples/js/controls/OrbitControls.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.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);
// Add ambient light
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
// Add directional light (sun)
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 3, 5);
scene.add(directionalLight);
// Create planet
function createPlanet() {
const radius = 5;
const widthSegments = 64;
const heightSegments = 64;
// Create geometry
const geometry = new THREE.SphereGeometry(radius, widthSegments, heightSegments);
// Generate procedural texture
const texture = generatePlanetTexture(1024, 1024);
// Create material
const material = new THREE.MeshPhongMaterial({
map: texture,
bumpMap: texture,
bumpScale: 0.2,
specular: new THREE.Color(0x333333),
shininess: 5
});
// Create mesh
const planet = new THREE.Mesh(geometry, material);
scene.add(planet);
return planet;
}
// Generate procedural planet texture
function generatePlanetTexture(width, height) {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const context = canvas.getContext('2d');
// Base color (random blue/green/brown)
const baseHue = Math.random() * 60 + 180; // Range between 180-240 (blue-green)
const baseColor = `hsl(${baseHue}, ${Math.random() * 30 + 50}%, ${Math.random() * 20 + 30}%)`;
// Draw base color
context.fillStyle = baseColor;
context.fillRect(0, 0, width, height);
// Add land masses (using Perlin noise would be better, but we'll use circles for simplicity)
const landColor = `hsl(${baseHue - 30}, ${Math.random() * 30 + 40}%, ${Math.random() * 15 + 20}%)`;
context.fillStyle = landColor;
const landCount = 5 + Math.floor(Math.random() * 10);
for (let i = 0; i < landCount; i++) {
const x = Math.random() * width;
const y = Math.random() * height;
const radius = Math.random() * (width / 4) + (width / 8);
context.beginPath();
context.arc(x, y, radius, 0, Math.PI * 2);
context.fill();
// Add some variation within the land mass
context.fillStyle = `hsl(${baseHue - 30 + Math.random() * 20}, ${Math.random() * 20 + 40}%, ${Math.random() * 10 + 15}%)`;
}
// Add cloud cover (semi-transparent)
context.fillStyle = 'rgba(255, 255, 255, 0.2)';
const cloudCount = 3 + Math.floor(Math.random() * 5);
for (let i = 0; i < cloudCount; i++) {
const x = Math.random() * width;
const y = Math.random() * height;
const radius = Math.random() * (width / 5) + (width / 10);
context.beginPath();
context.arc(x, y, radius, 0, Math.PI * 2);
context.fill();
}
// Convert canvas to texture
const texture = new THREE.CanvasTexture(canvas);
texture.needsUpdate = true;
return texture;
}
// Create the planet
const planet = createPlanet();
// Position camera
camera.position.z = 15;
// Add orbit controls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Rotate planet slowly
planet.rotation.y += 0.001;
controls.update();
renderer.render(scene, camera);
}
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
</script>
</body>
</html>
How This Works:
Scene Setup: Creates a Three.js scene, camera, and renderer with orbit controls for navigation.
Lighting: Adds ambient and directional lighting to illuminate the planet.
Planet Creation:
Creates a sphere geometry
Generates a procedural texture with random colors and patterns
Applies the texture as both color map and bump map for basic terrain
Texture Generation:
Uses canvas 2D API to create a random planet texture
Generates a base color (blue/green/brown)
Adds random land masses as circles
Adds semi-transparent cloud cover
Animation: Slowly rotates the planet and updates the scene
Enhancements You Could Add:
Improved Terrain Generation:
Use Perlin noise or Simplex noise for more realistic land masses
Add elevation data to create actual 3D terrain (displacement map)
Atmosphere Effect:
Add a semi-transparent outer sphere with scattering shader
Implement glow effect for the terminator line
Biome Variation:
Different colors for deserts, forests, ice caps
Add city lights on the dark side
Cloud Animation:
Animate cloud movement over time
Use particle systems for more dynamic clouds
Procedural Generation Parameters:
Allow user to input seed values
Add controls for planet size, color, etc.
To run this code, simply copy it into an HTML file and open it in a modern browser that supports WebGL.