#atom

JavaScript library for creating and displaying animated 3D computer graphics in web browsers

Core Idea: Three.js is a cross-browser JavaScript library that abstracts WebGL complexity, providing a high-level API for creating and rendering 3D graphics in web applications with minimal code.

Key Elements

Core Components

Rendering Pipeline

Development Workflow

Implementation Examples

Basic Scene Setup

// Create a scene
const scene = new THREE.Scene();

// Create a camera
const camera = new THREE.PerspectiveCamera(
  75,                   // Field of view
  window.innerWidth / window.innerHeight, // Aspect ratio
  0.1,                  // Near clipping plane
  1000                  // Far clipping plane
);
camera.position.z = 5;  // Position camera

// Create a renderer
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// Create a cube
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);

// Add lighting
const light = new THREE.AmbientLight(0x404040); // Soft white light
scene.add(light);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);

// Animation loop
function animate() {
  requestAnimationFrame(animate);
  
  // Rotate the cube
  cube.rotation.x += 0.01;
  cube.rotation.y += 0.01;
  
  // Render the scene
  renderer.render(scene, camera);
}
animate();

Advanced Material Usage

// Standard physically-based material
const standardMaterial = new THREE.MeshStandardMaterial({
  color: 0x049ef4,
  roughness: 0.5,
  metalness: 0.7,
  envMap: environmentTexture,
  map: colorTexture,
  normalMap: normalTexture,
  aoMap: ambientOcclusionTexture,
  displacementMap: heightTexture,
  displacementScale: 0.2
});

// Creating a custom shader material
const customMaterial = new THREE.ShaderMaterial({
  uniforms: {
    time: { value: 1.0 },
    resolution: { value: new THREE.Vector2() }
  },
  vertexShader: document.getElementById('vertexShader').textContent,
  fragmentShader: document.getElementById('fragmentShader').textContent
});

Loading 3D Models

// GLTF loader example
const loader = new THREE.GLTFLoader();
loader.load(
  'models/scene.gltf',
  function(gltf) {
    // Model loaded successfully
    scene.add(gltf.scene);
    
    // Access animations
    const animations = gltf.animations;
    const mixer = new THREE.AnimationMixer(gltf.scene);
    const action = mixer.clipAction(animations[0]);
    action.play();
  },
  function(xhr) {
    // Loading progress
    console.log((xhr.loaded / xhr.total * 100) + '% loaded');
  },
  function(error) {
    // Error occurred
    console.error('An error happened', error);
  }
);

Integration Approaches

Web Applications

Mobile Integration

Game Development

Optimization Techniques

Ecosystem and Extensions

Connections

References

  1. Three.js official documentation (https://threejs.org/docs/)
  2. "3D Graphics with Three.js" by various authors
  3. "WebGL Programming Guide" for underlying concepts

#three-js #webgl #3d-graphics #javascript #web-development


Connections:


Sources: