案例查看地址: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 );
}
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;
var normal = new THREE.Texture(THREE.ImageUtils.getNormalMap(image));
normal.needsUpdate = true;
cube1.material.normalMap = normal;
cube2.material.map = texture;
};
}
};
var datGui = new dat.GUI();
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 );
controls.enableDamping = true;
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>