案例查看地址:http://www.wjceo.com/blog/threejs/2018-05-03/156.html
左边为设置normalMap
后的效果,右边为正常效果。我们会发现设置了normalMap
后的立体感非常的强烈。
简介
法线贴图保存的不是高度信息,二十法向量的方向。简单来讲,使用法向贴图只需要使用很少的顶点和面就可以创建出细节很丰富的模型。
缺点:使用法向贴图最大的问题就是它们很难创建,需要使用比如Blender
和Photoshop
这样的特殊工具。这些工具可以将高分辨率的效果图或者纹理作为出入来创建法线贴图。
案例实现
法线贴图的使用方法和凹凸贴图的使用方法是一样的。只是这次我们将normalMap
属性设置为法向纹理。
var bump = new THREE.TextureLoader().load("/lib/assets/textures/general/plaster-normal.jpg");
var normal = new THREE.TextureLoader().load("/lib/assets/textures/general/plaster.jpg");
var material1 = new THREE.MeshPhongMaterial({
map:normal
});
//添加法向量贴图,使用默认的法向量影响值
material1.normalMap = bump;
我们还可以通过设置normalScale
属性来指定凹凸程度。
material.normalScale.set(1, 1);
通过这两个参数,你可以沿着x
轴和y
轴进行缩放,但是最好的方式是将它们设置成一样,以达到最好的效果。需要注意的是,如果设置的值为负数,那么高度就会反转。
实现通过图片创建法向贴图
Three.js
同样提供了再运行期间创建法向贴图的方法。要实现这个方法,我们需要额外引入插件:
<script src="/lib/js/utils/ImageUtils.js"></script>
然后,通过使用THREE.ImageUtils.getNormalMap
方法,来创建法向贴图,该方法接受JavaScript/Dom
图像作为输入并将其转换为法向贴图。
//切换纹理,使用three.js自动生成法向贴图
var normal = new THREE.Texture(THREE.ImageUtils.getNormalMap(image));
normal.needsUpdate = true;
cube1.material.normalMap = normal;
这样就实现了通过three.js
创建法向贴图的方法。
案例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
html, body {
margin: 0;
height: 100%;
}
canvas {
display: block;
}
</style>
</head>
<body onload="draw();">
</body>
<script src="https://cdn.bootcss.com/three.js/91/three.min.js"></script>
<script src="/lib/js/utils/ImageUtils.js"></script>
<script src="/lib/js/controls/OrbitControls.js"></script>
<script src="https://cdn.bootcss.com/stats.js/r17/Stats.min.js"></script>
<script src="https://cdn.bootcss.com/dat-gui/0.7.1/dat.gui.min.js"></script>
<script src="/lib/js/Detector.js"></script>
<script>
var renderer, camera, scene, gui, light, stats, controls, cube1, cube2, pointLight;
var angle = 0, radian, r = 5;
function initRender() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xeeeeee);
renderer.shadowMap.enabled = true;
//告诉渲染器需要阴影效果
document.body.appendChild(renderer.domElement);
}
function initCamera() {
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 200);
camera.position.set(0, 12, 15 );
}
function initScene() {
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xa0a0a0 );
scene.fog = new THREE.Fog( 0xa0a0a0, 5, 50 );
}
//初始化dat.GUI简化试验流程
function initGui() {
//声明一个保存需求修改的相关数据的对象
gui = {
normalScale:1,
animation:false,
changeTexture:function () {
var image = new Image();
image.src = "/lib/textures/crate.gif";
image.onload = function (ev) {
var texture = new THREE.Texture(image);
texture.needsUpdate = true;
cube1.material.map = texture;
//切换纹理,使用three.js自动生成法向贴图
var normal = new THREE.Texture(THREE.ImageUtils.getNormalMap(image));
normal.needsUpdate = true;
cube1.material.normalMap = normal;
//更换第二个cube的texture
cube2.material.map = texture;
};
}
};
var datGui = new dat.GUI();
//将设置属性添加到gui当中,gui.add(对象,属性,最小值,最大值)
datGui.add(gui, "normalScale", -2, 30).onChange(function (e) {
cube1.material.normalScale.set(e, e);
cube1.material.needsUpdate = true;
});
datGui.add(gui, "animation");
datGui.add(gui, "changeTexture");
}
function initLight() {
scene.add(new THREE.AmbientLight(0x444444));
light = new THREE.DirectionalLight(0xffffff);
light.position.set(0, 20, 10 );
light.castShadow = true;
light.shadow.camera.top = 10;
light.shadow.camera.bottom = -10;
light.shadow.camera.left = -10;
light.shadow.camera.right = 10;
//告诉平行光需要开启阴影投射
light.castShadow = true;
scene.add(light);
//添加可以移动的点光源
pointLight = new THREE.PointLight(0x00ffff);
pointLight.position.set(0, 5, 0);
scene.add(pointLight);
//给光源添加一个模型显示位置
pointLight.add(new THREE.Mesh(new THREE.SphereGeometry(0.05, 20, 20), new THREE.MeshBasicMaterial({color:0x00ffff})));
}
function initModel() {
//辅助工具
var helper = new THREE.AxesHelper(50);
scene.add(helper);
// 地板
var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 200, 200 ), new THREE.MeshPhongMaterial( { color: 0xffffff, depthWrite: false } ) );
mesh.rotation.x = - Math.PI / 2;
mesh.receiveShadow = true;
scene.add( mesh );
//添加地板割线
var grid = new THREE.GridHelper( 200, 50, 0x000000, 0x000000 );
grid.material.opacity = 0.2;
grid.material.transparent = true;
scene.add( grid );
//添加两个对比模型,一个添加凹凸贴图纹理
var bump = new THREE.TextureLoader().load("/lib/assets/textures/general/plaster-normal.jpg");
var normal = new THREE.TextureLoader().load("/lib/assets/textures/general/plaster.jpg");
var material1 = new THREE.MeshPhongMaterial({
map:normal
});
//添加法向量贴图,使用默认的法向量影响值
material1.normalMap = bump;
var geometry = new THREE.CubeGeometry(6, 6, 6);
cube1 = new THREE.Mesh(geometry, material1);
cube1.position.set(-5, 5, 0);
cube1.rotation.y += Math.PI/6;
scene.add(cube1);
//创建不带凹凸纹理的图形
var material2 = new THREE.MeshPhongMaterial({
map:normal
});
cube2 = new THREE.Mesh(geometry, material2);
cube2.position.set(5, 5, 0);
cube2.rotation.y -= Math.PI/6;
scene.add(cube2);
}
//初始化性能插件
function initStats() {
stats = new Stats();
document.body.appendChild(stats.dom);
}
function initControls() {
controls = new THREE.OrbitControls(camera, renderer.domElement);
//设置控制器的中心点
controls.target.set( 0, 5, 0 );
// 如果使用animate方法时,将此函数删除
//controls.addEventListener( 'change', render );
// 使动画循环使用时阻尼或自转 意思是否有惯性
controls.enableDamping = true;
//动态阻尼系数 就是鼠标拖拽旋转灵敏度
//controls.dampingFactor = 0.25;
//是否可以缩放
controls.enableZoom = true;
//是否自动旋转
controls.autoRotate = false;
controls.autoRotateSpeed = 0.5;
//设置相机距离原点的最远距离
controls.minDistance = 1;
//设置相机距离原点的最远距离
controls.maxDistance = 2000;
//是否开启右键拖拽
controls.enablePan = true;
}
function render() {
//设置模型的旋转
if(gui.animation){
cube1.rotation.y += 0.01;
cube2.rotation.y -= 0.01;
}
//设置点光源的移动
angle += 1;
radian = angle/180 * Math.PI;
var x = Math.sin(radian);
var y = Math.cos(radian);
if(angle%720 > 360){
y = -y+2;
}
pointLight.position.z = x*r;
pointLight.position.x = y*r-r;
}
//窗口变动触发的函数
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
//更新控制器
render();
//更新性能插件
stats.update();
controls.update();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
function draw() {
//兼容性判断
if (!Detector.webgl) Detector.addGetWebGLMessage();
initGui();
initRender();
initScene();
initCamera();
initLight();
initModel();
initControls();
initStats();
animate();
window.onresize = onWindowResize;
}
</script>
</html>