Skip to content

Three.js Шпаргалка

JavaScript-библиотека 3D для создания и отображения трёхмерной компьютерной графики.

01

Начало работы

Настройка сцены

Базовая настройка: сцена, камера, рендерер.

threejs
import * as THREE from "three";

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, w/h, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(w, h);
document.body.appendChild(renderer.domElement);

Mesh

Геометрия + материал = Mesh.

threejs
const geo = new THREE.BoxGeometry(1, 1, 1);
const mat = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const cube = new THREE.Mesh(geo, mat);
scene.add(cube);

Цикл рендеринга

requestAnimationFrame для цикла рендеринга.

threejs
function animate() {
  requestAnimationFrame(animate);
  cube.rotation.x += 0.01;
  renderer.render(scene, camera);
}
animate();
02

Объекты и свет

Источники света

DirectionalLight + AmbientLight для базового освещения.

threejs
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 5, 5);
scene.add(light);

const ambient = new THREE.AmbientLight(0x404040);
scene.add(ambient);

Group

Group для совместного преобразования нескольких объектов.

threejs
const group = new THREE.Group();
group.add(mesh1, mesh2);
scene.add(group);
group.rotation.y = Math.PI / 4;

Текстура

TextureLoader для загрузки текстур изображений.

threejs
const loader = new THREE.TextureLoader();
const texture = loader.load("texture.jpg");
const mat = new THREE.MeshStandardMaterial({ map: texture });

Was this helpful?