Game Development Reference
In-Depth Information
Let's optimize the scene by merging the geometries of all the buildings. To do this,
we'll tweak the code that spawns our many buildings:
var cityGeometry = new THREE.Geometry();
for (var i = 0; i < 300; i++) {
var building = new THREE.Mesh(geometry.clone());
building.position.x = Math.floor(Math.random() * 200 - 100) * 4;
building.position.z = Math.floor(Math.random() * 200 - 100) * 4;
building.scale.x = Math.random() * 50 + 10;
building.scale.y = Math.random() * building.scale.x * 8 + 8;
building.scale.z = building.scale.x;
THREE.GeometryUtils.merge(cityGeometry, building);
}
var city = new THREE.Mesh(cityGeometry, material);
scene.add(city);
The key here is that we're now merging all of the building meshes into a single
cityGeometry
using
GeometryUtils.merge()
. This is an important optimization
for scenes with a lot of geometry that does not move independently because the
renderer can more intelligently batch drawing calls if it knows about all the vertices
and faces at once instead of drawing them one mesh at a time.
Lighting
Lights are instances of
THREE.Light
that affect how the
MeshLambertMaterial
and
MeshPhongMaterial
surfaces are illuminated. Most lights have color (specified
in hexadecimal notation like CSS colors) and intensity (a decimal value, usually
between zero and one, indicating how bright the light should be). There are different
kinds of lights as shown in the following table:
Type
Constructor
Description
THREE.AmbientLight(color)
Ambient
It affects all lit objects in the scene
equally.
Directional
THREE.
DirectionalLight(color,
intensity = 1)
For this type, all light is parallel and
comes from a given direction, as if
the source was very far away.
THREE.
HemisphereLight(skyColor,
groundColor, intensity =
1)
Hemisphere
It simulates refractive lighting from
the sun, sort of like two opposing
directional lights.
Point
THREE.PointLight(color,
intensity = 1, radius = 0)
It emanates from a specific point in
space, like a lightbulb. It illuminates
only objects within
radius
.