在前四篇文中,我们介绍了 three.js 中常用的四种基础光源 AmbientLight(环境光光源)、PointLight(点光光源)、SpotLight(聚光灯光源)以及 DirectionalLight(方向光或叫平行光光源)。本篇我们将来介绍一下 three.js 中第一种相对特殊点的光源 HemisphereLight(半球光光源)。这种光源可以为室外场景创建更加贴近自然的光照效果。
如果不想使用这种光源,那么,要模拟室外光照,则要使用一个方向光光源来模拟太阳,或额外再添加一个环境光光源,以便为场景提供基础颜色。但是,这样搭配出来的关照效果看上去不怎么贴近自然。因为当你在室外的时候,并不是所有的光照都来自上方,很多都是同时来自空气的散射、地面的反射,以及其他物体的反射。为此,three.js 为我们提供了 HemisphereLight 半球光光源。我们先来看一下完整的示例代码,如下所示:
<!DOCTYPE html>
<html>
<head>
<title>示例 03.05 - 半球光光源</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 plane;
var cube;
var sphere;
var hemisphereLight;
var dirLight;
$(function() {
stats = initStats();
scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xaaaaaa, 0.01, 800); // 雾化效果
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2147483647); // 2147483647
camera.position.set(-20, 15, 45);
render = new THREE.WebGLRenderer( {antialias: true} ); // antialias 抗锯齿
render.setSize(window.innerWidth, window.innerHeight);
render.setClearColor(0xaaaaff, 1.0);
render.shadowMap.enabled = true; // 允许阴影投射
$('#WebGL-output')[0].appendChild(render.domElement);
window.addEventListener('resize', onWindowResize, false);
var target = new THREE.Vector3(scene.position.x + 10, 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(800, 800, 20, 20);
var textureLoader = new THREE.TextureLoader();
var textureGrass = textureLoader.load("./assets/textures/ground/grasslight-big.jpg", function(texture) {
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(4, 4);
planeMaterial.needsUpdate = true;
});
var planeMaterial = new THREE.MeshLambertMaterial( {map: textureGrass} );
plane = new THREE.Mesh(planeGeometry, planeMaterial);
plane.rotation.x = -0.5 * Math.PI; // 沿着 X轴旋转-90°
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);
// 加入一个方向光
dirLight = new THREE.DirectionalLight(0xffffff, 1.0);
dirLight.position.set(30, 10, -50);
dirLight.castShadow = true;
dirLight.shadow.mapSize.set(2048, 2048);
dirLight.target = plane;
dirLight.shadow.camera.near = 0.1;
dirLight.shadow.camera.far = 200;
dirLight.shadow.camera.left = -50;
dirLight.shadow.camera.right = 50;
dirLight.shadow.camera.top = 50;
dirLight.shadow.camera.bottom = -50;
scene.add(dirLight);
//var dirLightCamera = new THREE.CameraHelper(dirLight.shadow.camera);
//scene.add(dirLightCamera);
// 加入一个半球光(skyColor 天空色, groundColor 地面色, intensity 光照强度)
hemisphereLight = new THREE.HemisphereLight(0x0000ff, 0x00ff00, 0.3);
hemisphereLight.position.set(0, 500, 0);
scene.add(hemisphereLight);
// 加入一个聚光灯
var spotLight = new THREE.SpotLight(0xcccccc, 0.6);
spotLight.position.set(-40, 60, -10);
spotLight.target = plane;
scene.add(spotLight);
//var spotLightCamera = new THREE.CameraHelper(spotLight.shadow.camera);
//scene.add(spotLightCamera);
/** 用来保存那些需要修改的变量 */
guiControls = new function() {
this.rotationSpeed = 0.02;
this.bouncingSpeed = 0.04;
this.hemisphere = true;
this.skyColor = '#0000ff';
this.groundColor = '#00ff00';
this.intensity = 0.3;
}
/** 定义 dat.GUI 对象,并绑定 guiControls 的几个属性 */
var gui = new dat.GUI();
gui.add(guiControls, 'hemisphere').onChange( function(e) {
hemisphereLight.intensity = e ? guiControls.intensity : 0;
});
gui.addColor(guiControls, 'skyColor').onChange( function(e) {
hemisphereLight.color = new THREE.Color(e);
});
gui.addColor(guiControls, 'groundColor').onChange( function(e) {
hemisphereLight.groundColor = new THREE.Color(e);
});
gui.add(guiControls, 'intensity', 0, 3).onChange( function(e) {
hemisphereLight.intensity = e;
});
renderScene();
});
/** 渲染场景 */
function renderScene() {
stats.update();
rotateCube(); // 旋转立方体
bounceSphere(); // 弹跳球体
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)));
}
</script>
</body>
</html>
在这个例子中,我们可以通过右上角的 hemisphere 开关打开或者关闭半球光光源,还可以通过 skyColor(天空颜色) 和 groundColor(地面颜色) 来试验不同的效果。
需要特别提醒的是,本示例中地面的创建跟之前给出的所有例子都有些不同。本例中的地面,使用了一个草地纹理作为材质,这主要是通过 THREE.TextureLoader() 对象来实现的,基本用法如下:
var textureLoader = new THREE.TextureLoader();
var textureGrass = textureLoader.load("./assets/textures/ground/grasslight-big.jpg", function(texture) {
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(4, 4);
planeMaterial.needsUpdate = true;
});
示例中用到的草地纹理
其中,第一个参数给出草地纹理的图片资源路径,第二个参数接受一个回调函数,用来进一步对纹理的相关特性进行设置。关于纹理,后面我们会有专门的章节进行介绍,此处先做个初步了解即可。需要注意的是,在这个回调函数里的最后一句代码 planeMaterial.needsUpdate = true; 其含义是通知引用此纹理的 Material 对象“纹理已经就绪,请及时更新”。
创建半球光光源也比较简单,只要简单指定参数即可实现:天空颜色、地面颜色、光照强度等。如下代码片段所示:
hemisphereLight = new THREE.HemisphereLight(0x0000ff, 0x00ff00, 0.3);
hemisphereLight.position.set(0, 500, 0);
scene.add(hemisphereLight);
下表列出了半球光比较常用的几个属性:
属性 | 描述 |
---|---|
skyColor (天空色) | 表示从天空发出的光线的颜色 |
groundColor (地面色) | 表示从地面发出的光线的颜色 |
intensity (光强) | 表示光线照射的强度 |
未完待续···