Skip to content

Scene

场景能够让你在什么地方、摆放什么东西来交给three.js来渲染,这是你放置物体、灯光和摄像机的地方。

Scene继承自Object3D

1.构造函数

js
const scene = new THREE.Scene();

2.属性

background

若不为空,在渲染场景的时候将设置背景,且背景总是首先被渲染的。 可以设置一个用于的“clear”的Color(颜色)

js
// 设置画布背景色
const scene = new THREE.Scene();
scene.background=new THREE.Color( 0xff0000 )

fog

Fog可以给三维场景添加雾化效果,默认值为null。

js
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xffffff,1,50)

overrideMaterial

强制场景中的每个物体使用这里的材质来渲染。默认值为null。

js
const scene = new THREE.Scene();
scene.overrideMaterial=new THREE.MeshLamberMaterial({color:0xff0000})

children

返回场景中所有对象的列表

3.方法

add()

向场景中添加对象

js
const planeGeometry = new THREE.PlaneGeometry(20, 30);
const planeMaterial = new THREE.MeshLambertMaterial({
    color: 0x999999,
});
const plane = new THREE.Mesh(planeGeometry, planeMaterial);

scene.add(plane);

remove()

从场景中移除一个对象

js
scene.remove(plane);

getObjectByName()

创建对象时可以复制一个唯一name属性,通过此方法,传入name可获取该对象

js
const sphereGeometry = new THREE.SphereGeometry(1, 10, 10);
const sphereMaterial = new THREE.MeshBasicMaterial({
    color: 0x00ff00,
    wireframe: false,
});
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);

sphere.name='sphere'
scene.add(sphere);

console.log(scene.getObjectByName('sphere'))

MIT Licensed