Make a world in threejs
- User
Make a world in threejs
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Make a world in threejs
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
// 1. Scene Setup
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB); // Sky Blue background
// 2. Camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 5, 10); // Initial camera position
camera.lookAt(0, 0, 0); // Look at the center of the scene
// 3. Renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 4. Objects - Let's create a ground, a cube, and a sphere
// Ground
const groundGeometry = new THREE.PlaneGeometry(10, 10);
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x90EE90 }); // Light Green
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2; // Rotate to be flat
scene.add(ground);
// Cube
const cubeGeometry = new THREE.BoxGeometry(2, 2, 2);
const cubeMaterial = new THREE.MeshStandardMaterial({ color: 0xFF0000 }); // Red
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.set(2, 1, 0);
scene.add(cube);
// Sphere
const sphereGeometry = new THREE.SphereGeometry(1.5, 32, 32);
const sphereMaterial = new THREE.MeshStandardMaterial({ color: 0x0000FF }); // Blue
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.position.set(-2, 1, 0);
scene.add(sphere);
// 5. Lighting
// Ambient Light
const ambientLight = new THREE.AmbientLight(0x404040); // Dim ambient light
scene.add(ambientLight);
// Directional Light
const directionalLight = new THREE.DirectionalLight(0xFFFFFF, 0.8); // White directional light
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
// 6. Controls (OrbitControls)
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Add damping for smoother movement
controls.dampingFactor = 0.05;
controls.screenSpacePanning = false;
controls.minPolarAngle = Math.PI / 4; // Limit vertical camera angle
controls.maxPolarAngle = Math.PI * 3 / 4;
// 7. Animation Loop
function animate() {
requestAnimationFrame(animate);
// Rotate the cube and sphere
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
sphere.rotation.z += 0.01;
controls.update(); // Update OrbitControls
renderer.render(scene, camera);
}
animate();
// 8. Resize Handler (Important for responsiveness)
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', onWindowResize);
Key improvements and explanations:
import * as THREE from 'three'; and three/addons/controls/OrbitControls.js which is the recommended way to import in modern THREE.js. This avoids global namespace pollution.OrbitControls for easy camera manipulation. The code now correctly imports and uses OrbitControls. I've added damping and other settings for a better user experience.MeshStandardMaterial which is physically based and produces more realistic results than MeshBasicMaterial.animate function rotates the cube and sphere, creating a simple animation. It also updates the OrbitControls.onWindowResize function ensures that the scene adapts to changes in the browser window size. This is essential for responsiveness.requestAnimationFrame is the correct way to create an animation loop in the browser.How to Run This Code:
Install THREE.js:
npm install three
Create an HTML file (e.g., index.html):
<!DOCTYPE html>
<html>
<head>
<title>Three.js World</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script type="module" src="script.js"></script>
</body>
</html>
Save the JavaScript code as script.js in the same directory as index.html.
Serve the files: You can use a simple HTTP server. One easy way is to use npx serve:
npx serve
This will usually start a server on http://localhost:3000. Open that URL in your browser.
Explanation of Key Concepts:
requestAnimationFrame: A browser API that tells the browser you want to perform an animation and requests that the browser call a specified function to update an animation before the next repaint.This example provides a solid foundation for building more complex 3D worlds in Three.js. You can experiment with different geometries, materials, lighting, and animations to create your own unique scenes. Remember to consult the Three.js documentation for more information: https://threejs.org/docs/