这一篇我们来看看剩下的最后一个基础光源 DirectionalLight (方向光或叫平行光)光源。这个光源可以看做是距离很远很远的光源,就像太阳。太阳离我们是如此的遥远,以至于所有的光线到达地球时都成了相互平行的光线了。方向光跟聚光灯光源之间的主要差别是:方向光不像聚光灯那样离目标越远越暗淡,被方向光光源照亮的整个区域接收到的光的强度是一样的。
下面来看一个完整的示例,代码如下所示:
<!DOCTYPE html>
<html>
<head>
<title>示例 03.04 - 方向光光源</title>
<script src="../build/three.js"></script>
<script src="../build/js/controls/OrbitControls.js"></script>
<script src="../build/js/libs/stats.min.js"></script>
<script src="../build/js/libs/dat.gui.min.js"></script>
<script src="../jquery/jquery-3.2.1.min.js"></script>
<style>
body {
/* 设置 margin 为 0,并且 overflow 为 hidden,来完成页面样式 */
margin: 0;
overflow: hidden;
}
/* 统计对象的样式 */
#Stats-output {
position: absolute;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<!-- 用于 WebGL 输出的 Div -->
<div id="WebGL-output"></div>
<!-- 用于统计 FPS 输出的 Div -->
<div id="Stats-output"></div>
<!-- 运行 Three.js 示例的 Javascript 代码 -->
<script type="text/javascript">
var scene;
var camera;
var render;
var controls;
var stats;
var guiControls;
var cube;
var sphere;
var directionalLight;
var shadowCameraHelper;
$(function() {
stats = initStats();
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000); // 2147483647
camera.position.set(-30, 40, 30);
render = new THREE.WebGLRenderer( {antialias: true} ); // antialias 抗锯齿
render.setSize(window.innerWidth, window.innerHeight);
render.setClearColor(0xEEEEEE);
render.shadowMap.enabled = true; // 允许阴影投射
$('#WebGL-output')[0].appendChild(render.domElement);
window.addEventListener('resize', onWindowResize, false);
var target = new THREE.Vector3(scene.position.x, scene.position.y ,scene.position.z);
controls = new THREE.OrbitControls(camera, render.domElement);
controls.target = target;
camera.lookAt(target);
scene.add(new THREE.AxesHelper(20));// 加入坐标轴
// 加入一个平面
var planeGeometry = new THREE.PlaneGeometry(600, 200, 20, 20);
var planeMaterial = new THREE.MeshLambertMaterial( {color: 0xFFFFFF} );
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = -0.5 * Math.PI; // 沿着 X轴旋转-90°
plane.position.x = 15; // 沿着 x轴右移 15个单位
plane.position.y = 0; // y轴为 0
plane.position.z = 0; // z轴为 0
plane.receiveShadow = true; // 非线框几何平面接收阴影
scene.add(plane);
// 加入一个立方体
var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);
var cubeMaterial = new THREE.MeshLambertMaterial( {color: 0xFF7777} );
cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.x = -4;
cube.position.y = 3;
cube.position.z = 0;
cube.castShadow = true; // 立方体投射阴影
scene.add(cube);
// 加入一个球体
var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
var sphereMaterial = new THREE.MeshLambertMaterial( {color: 0x7777FF} );
sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.position.x = 20;
sphere.position.y = 4;
sphere.position.z = 2;
sphere.castShadow = true; // 球体投射阴影
scene.add(sphere);
// 加入一个环境光源
var ambientLight = new THREE.AmbientLight(0x0c0c0c);
scene.add(ambientLight);
// 加入一个小球体来表示聚光灯位置
sphereGeometry = new THREE.SphereGeometry(0.2, 20, 20);
sphereMaterial = new THREE.MeshBasicMaterial( {color: 0xac6c25} );
var lightMesh = new THREE.Mesh(sphereGeometry, sphereMaterial);
// 加入一个方向光:color 颜色, intensity 强度
directionalLight = new THREE.DirectionalLight( 0xff5808, 0.5);
directionalLight.position.set(-40, 60, -10);
directionalLight.castShadow = true; // 光源产生阴影
directionalLight.distance = 0;
directionalLight.shadow.mapSize.set(2048, 2048); // 必须是 2的幂,默认值为 512
directionalLight.shadow.camera.near = 2;
directionalLight.shadow.camera.far = 100;
directionalLight.shadow.camera.left = -50;
directionalLight.shadow.camera.right = 50;
directionalLight.shadow.camera.top = 50;
directionalLight.shadow.camera.bottom = -50;
directionalLight.target = plane;
directionalLight.add(lightMesh);
scene.add(directionalLight);
/** 用来保存那些需要修改的变量 */
guiControls = new function() {
this.rotationSpeed = 0.02;
this.bouncingSpeed = 0.04;
this.ambientColor = '#1c1c1c';
this.pointColor = '#ff5808';
this.intensity = 1;
this.distance = 0;
this.debug = true;
this.castShadow = true;
this.disableLight = false;
this.target = 'Plane';
this.stopMoveLight = false;
}
/** 定义 dat.GUI 对象,并绑定 guiControls 的两个属性 */
var gui = new dat.GUI();
gui.addColor(guiControls, 'ambientColor').onChange( function(e) {
ambientLight.color = new THREE.Color(e);
});
gui.addColor(guiControls, 'pointColor').onChange( function(e) {
directionalLight.color = new THREE.Color(e);
});
gui.add(guiControls, 'intensity', 0, 3).onChange( function(e) {
directionalLight.intensity = e;
});
gui.add(guiControls, 'distance', 0, 200).onChange( function(e) {
directionalLight.distance = e;
});
gui.add(guiControls, 'debug').onChange(function(e) {
updateShadowCamera();
});
gui.add(guiControls, 'castShadow').onChange(function(e) {
directionalLight.castShadow = e;
});
gui.add(guiControls, 'disableLight').onChange(function(e) {
directionalLight.visible = !e;
});
gui.add(guiControls, 'target', ['Plane', 'Cube', 'Sphere']).onChange(function(e) {
switch (e) {
case 'Plane':
directionalLight.target = plane;
break;
case 'Cube':
directionalLight.target = cube;
break;
case 'Sphere':
directionalLight.target = sphere;
break;
}
});
gui.add(guiControls, 'stopMoveLight').onChange(function(e){
this.stopMoveLight = e;
});
function updateShadowCamera() {
scene.remove(scene.getObjectByName('shadowCameraHelper'));
if (guiControls.debug) {
shadowCameraHelper = new THREE.CameraHelper(directionalLight.shadow.camera);
shadowCameraHelper.name = "shadowCameraHelper";
scene.add(shadowCameraHelper);
}
}
updateShadowCamera();
renderScene();
});
/** 渲染场景 */
function renderScene() {
stats.update();
rotateCube(); // 旋转立方体
bounceSphere(); // 弹跳球体
moveLight(); // 移动点光源及其小球
requestAnimationFrame(renderScene);
render.render(scene, camera);
}
/** 初始化 stats 统计对象 */
function initStats() {
stats = new Stats();
stats.setMode(0); // 0 为监测 FPS;1 为监测渲染时间
$('#Stats-output').append(stats.domElement);
return stats;
}
/** 当浏览器窗口大小变化时触发 */
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
render.setSize(window.innerWidth, window.innerHeight);
}
/** 转动立方体 */
function rotateCube() {
cube.rotation.x += guiControls.rotationSpeed;
cube.rotation.y += guiControls.rotationSpeed;
cube.rotation.z += guiControls.rotationSpeed;
}
/** 弹跳球体 */
var step = 0;
function bounceSphere() {
step += guiControls.bouncingSpeed;
sphere.position.x = 20 + (10 * Math.cos(step));
sphere.position.y = 2 + (10 * Math.abs(Math.sin(step)));
}
/** 移动点光源及其小球 */
var phase = 0;
function moveLight() {
if (guiControls.stopMoveLight || !directionalLight)
return;
phase += guiControls.rotationSpeed;
directionalLight.position.x = 15 + 30 * (Math.cos(phase));
directionalLight.position.y = 30 * (Math.sin(phase));
}
</script>
</body>
</html>
正如你所看到的,整个场景里没有那种锥形的光线。这种光源只用 direction 方向、color 颜色和 intensity 强度属性来计算颜色和阴影。方向光的属性大都与聚光灯光源相同,例如 position、target、intensity、distance、castShadow、shadow.camera.near、shadow.camera.far、shadow.camera.left、shadow.camera.right、shadow.camera.top、shadow.camera.bottom、shadow.mapSize.width、shadow.mapSize.height 和 shadow.bias 等。
细心的读者可能会发现,前面那个 SpotLight 聚光灯的例子中,我们必须定义生成阴影的光锥。但对于 DirectionalLight 方向光光源,由于所有光线都是平行的,所有不会有光锥的概念,取而代之的是一个方块,正如此示例中显示出来的 THREE.CameraHelper() 对象那样。这个方块包围对象的空间定义得越紧密,投影的效果越好。这主要是通过一些这些属性来定义的:
directionalLight.shadow.camera.near = 2;
directionalLight.shadow.camera.far = 100;
directionalLight.shadow.camera.left = -50;
directionalLight.shadow.camera.right = 50;
directionalLight.shadow.camera.top = 50;
directionalLight.shadow.camera.bottom = -50;
我们可以把这些设置跟之前“three.js 02-05 之相机”中的 OrthographicCamera 正投影相机的相关用法对比参照一下,含义几乎是相同的。
未完待续···