Three.js Colored Cube - Threejs Typing CST Test
Loading…
Three.js Colored Cube — Threejs Code
A cube with different face colors rendered in Three.js.
# threejs/demo/colored_cube.js
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('canvas') });
renderer.setSize(window.innerWidth, window.innerHeight);
const geometry = new THREE.BoxGeometry();
const materials = [
new THREE.MeshBasicMaterial({color: 0xff0000}),
new THREE.MeshBasicMaterial({color: 0x00ff00}),
new THREE.MeshBasicMaterial({color: 0x0000ff}),
new THREE.MeshBasicMaterial({color: 0xffff00}),
new THREE.MeshBasicMaterial({color: 0xff00ff}),
new THREE.MeshBasicMaterial({color: 0x00ffff})
];
const cube = new THREE.Mesh(geometry, materials);
scene.add(cube);
camera.position.z = 5;
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();Threejs Language Guide
Three.js is a popular open-source 3D JavaScript library that simplifies creating, displaying, and animating 3D graphics in web browsers using WebGL. It provides abstractions for cameras, lights, materials, geometries, and scene management, making 3D web development accessible and efficient.
Primary Use Cases
- ▸Interactive 3D websites and apps
- ▸Product visualization and configurators
- ▸Web-based games using WebGL
- ▸AR/VR experiences via WebXR
- ▸Scientific and architectural visualizations
Notable Features
- ▸Cross-browser WebGL abstraction
- ▸Extensive geometry and material library
- ▸Animation and keyframe system
- ▸Post-processing effects
- ▸Integration with physics and audio libraries
Origin & Creator
Three.js was created by Ricardo Cabello (mr.doob) in 2010 and has since evolved with contributions from a global community of developers.
Industrial Note
Three.js is widely used in web-based games, AR/VR, data visualization, product configurators, architectural walkthroughs, and interactive marketing websites, providing powerful 3D experiences without requiring native apps.
Quick Explain
- ▸Three.js abstracts WebGL’s low-level API into an easy-to-use framework for 3D graphics.
- ▸It supports cameras, lights, meshes, materials, textures, and animations.
- ▸Offers utilities for loading 3D models (GLTF, OBJ, FBX) and textures.
- ▸Provides helpers for physics, particles, post-processing, and shaders.
- ▸Runs natively in browsers without plugins via WebGL.
Core Features
- ▸Scene graph management
- ▸Cameras, lights, and shadows
- ▸Meshes, geometries, and materials
- ▸Loaders for 3D models and textures
- ▸ShaderMaterial for custom GLSL shaders
Learning Path
- ▸Learn basics of WebGL concepts
- ▸Understand Three.js core classes (Scene, Camera, Renderer)
- ▸Create simple objects and materials
- ▸Animate scenes and load 3D models
- ▸Build complex interactive 3D applications
Practical Examples
- ▸Simple rotating cube
- ▸GLTF model loading and rendering
- ▸Particle systems
- ▸Physics-enabled 3D simulations
- ▸AR/VR interactive scene via WebXR
Comparisons
- ▸Three.js vs raw WebGL: easier to use, less control
- ▸Three.js vs Babylon.js: simpler API, smaller bundle
- ▸Three.js vs WebGPU: less low-level control, slower for heavy compute
- ▸Three.js vs Unity WebGL: more lightweight, JS-based
- ▸Three.js vs PlayCanvas: open-source vs SaaS engine
Strengths
- ▸Simplifies WebGL 3D rendering
- ▸Large ecosystem with plugins and examples
- ▸Flexible and extensible
- ▸Active community support
- ▸Good performance for real-time 3D web apps
Limitations
- ▸Not as low-level as raw WebGL or WebGPU
- ▸Performance bottlenecks with very large scenes
- ▸Limited compute capabilities (no native GPU compute)
- ▸Some advanced effects require custom shaders
- ▸Debugging can be challenging for complex scenes
When NOT to Use
- ▸Heavy GPU compute tasks -> use WebGPU
- ▸Ultra-realistic AAA game -> use Unity/Unreal
- ▸Simple 2D canvas -> use Canvas2D
- ▸Non-browser environments
- ▸If team prefers visual editors over code
Cheat Sheet
- ▸const scene = new THREE.Scene();
- ▸const camera = new THREE.PerspectiveCamera();
- ▸const renderer = new THREE.WebGLRenderer();
- ▸const mesh = new THREE.Mesh(geometry, material);
- ▸renderer.render(scene, camera);
FAQ
- ▸Is Three.js free? -> Yes, MIT licensed.
- ▸Does it work on mobile? -> Yes, with WebGL support.
- ▸Can Three.js use WebGPU? -> Experimental via renderer.
- ▸Do I need to know WebGL? -> Helpful but not mandatory.
- ▸Is Three.js good for games? -> Suitable for browser-based 3D games.
30-Day Skill Plan
- ▸Week 1: Geometry and materials
- ▸Week 2: Lights, shadows, and cameras
- ▸Week 3: Model loading and textures
- ▸Week 4: Animation and post-processing
- ▸Week 5: WebXR and physics integration
Final Summary
- ▸Three.js simplifies web-based 3D graphics development.
- ▸Abstraction over WebGL makes it beginner-friendly.
- ▸Supports models, textures, animations, and effects.
- ▸Widely used in games, AR/VR, visualization, and interactive websites.
- ▸Foundation for web 3D experiences.
Project Structure
- ▸index.html - HTML container with canvas
- ▸main.js - initializes Three.js scene and renderer
- ▸models/ - GLTF/OBJ 3D assets
- ▸textures/ - image assets for materials
- ▸utils/ - helper functions for scene management
Monetization
- ▸3D product configurators for e-commerce
- ▸Interactive marketing campaigns
- ▸Web-based 3D games and experiences
- ▸AR/VR training platforms
- ▸Premium 3D visualization SaaS
Productivity Tips
- ▸Reuse meshes and materials
- ▸Use helper libraries for loaders and controls
- ▸Keep animations modular
- ▸Profile performance regularly
- ▸Use the Three.js editor for prototyping
Basic Concepts
- ▸Scene -> container for 3D objects
- ▸Camera -> defines perspective or orthographic view
- ▸Renderer -> draws the scene using WebGL
- ▸Meshes -> combination of geometry and material
- ▸Lights -> illuminate objects in the scene