1. 程式人生 > >69 - three.js 筆記 - 使用光照貼圖 lightMap 建立假陰影

69 - three.js 筆記 - 使用光照貼圖 lightMap 建立假陰影

MateriallightMap屬性,是光照貼圖,使用光照貼圖可以模擬出真實的陰影效果,但是陰影的位置不能隨著隨著物體的移動而移動。

1、示例

示例
http://ithanmang.com/threeJs/home/201809/20180905/06-lightMap-texture.html
效果
這裡寫圖片描述
下面的陰影是通過光照貼圖載入進來的,如下面的關照貼圖
這裡寫圖片描述

2、使用步驟

2.1、建立例項
 let lm = new THREE.TextureLoader().load('../../textures/lightmap/lm-1.png');
 planeGround.material.lightMap = lm;

將平面的材質的lightMap設定

2.3、指定UV對映
planeGround.geometry.faceVertexUvs[1] = planeGround.geometry.faceVertexUvs[0];

為光照貼圖指定uv對映,將紋理的那一部分對映到表面,只有這樣才能將關照貼圖與其它貼圖分別開來。

3、示例程式碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="../../../three.png"
>
<title>使用光照貼圖建立假陰影</title> <style> body { margin: 0; overflow: hidden; /* 溢位隱藏 */ } #loading { position: fixed; top: 50%; left: 50%; color: #FFFFFF; font-size: 20px; margin-top
: -30px
; margin-left: -40px; }
</style> <script src="../../libs/build/three-r93.js"></script> <script src="../../libs/examples/js/Detector.js"></script> <script src="../../libs/examples/js/libs/dat.gui.min.js"></script> <script src="../../libs/examples/js/libs/stats.min.js"></script> <script src="../../libs/examples/js/controls/OrbitControls.js"></script> </head> <body> <p id="loading">loading......</p> <script> let scene, camera, renderer, controls, guiControls; let stats = initStats(); /* 場景 */ function initScene() { scene = new THREE.Scene(); scene.background = new THREE.Color(0x050505); } /* 相機 */ function initCamera() { camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000); camera.position.set(0, 50, -90); camera.lookAt(new THREE.Vector3(0, 0, 0)); } /* 渲染器 */ function initRender() { renderer = new THREE.WebGLRenderer({antialias: true}); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); } /* 燈光 */ function initLight() { scene.add(new THREE.AmbientLight(0x0c0c0c)); let spotLight1 = new THREE.SpotLight(0xffffff); spotLight1.position.set(-400, -400, -400); let spotLight2 = new THREE.SpotLight(0xffffff); spotLight2.position.set(400, 400, 400); scene.add(spotLight1); scene.add(spotLight2); } /* 控制器 */ function initControls() { /* 地圖控制元件 */ controls = new THREE.OrbitControls(camera, renderer.domElement); /* 屬性引數 */ } /* 除錯外掛 */ function initGui() { guiControls = new function () { this.normalScale = 1; }; let gui = new dat.GUI(); gui.add(guiControls, 'normalScale', -2, 2).onChange(function (e) { box1.material.normalScale.set(e, e); }); } /* 場景中的內容 */ let box1; let box2; function initContent() { let planeGround = createMesh(new THREE.PlaneGeometry(95, 95, 1, 1), 'floor-wood.jpg'); box1 = createMesh(new THREE.BoxGeometry(10, 10, 10), 'plaster.jpg', 'plaster-normal.jpg'); box2 = createMesh(new THREE.BoxGeometry(10, 10, 10), 'plaster.jpg'); let lm = new THREE.TextureLoader().load('../../textures/lightmap/lm-1.png'); box1.translateX(10); box2.translateX(-10); box1.translateY(10); box2.translateY(10); planeGround.rotateX(Math.PI * -0.5); planeGround.material.lightMap = lm; planeGround.geometry.faceVertexUvs[1] = planeGround.geometry.faceVertexUvs[0]; scene.add(planeGround); scene.add(box1); scene.add(box2); } /* 移除載入元素 */ function removeLoading() { document.getElementById('loading').style.display = 'none'; } /* 建立帶有紋理的網格 */ function createMesh(geometry, imageUrl, normalMap) { let loader = new THREE.TextureLoader(); let texture = loader.load('../../textures/general/' + imageUrl); let material = new THREE.MeshPhongMaterial(); material.map = texture; if (normalMap !== undefined) { let normal = loader.load('../../textures/general/' + normalMap, function () { removeLoading(); }); material.normalMap = normal; } let mesh = new THREE.Mesh(geometry, material); return mesh; } /* 效能外掛 */ function initStats() { let stats = new Stats(); document.body.appendChild(stats.domElement); return stats; } /* 視窗變動觸發 */ function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); renderer.setSize(window.innerWidth, window.innerHeight); } /* 資料更新 */ function update() { stats.update(); controls.update(); if (box1 && box2) { box1.rotateY(0.01); box2.rotateY(0.01); } } /* 初始化 */ function init() { if (!Detector.webgl) Detector.addGetWebGLMessage(); initScene(); initCamera(); initRender(); initLight(); initControls(); initContent(); initGui(); /* 監聽事件 */ window.addEventListener('resize', onWindowResize, false); } /* 迴圈渲染 */ function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); update(); } /* 初始載入 */ (function () { console.log("three init start..."); init(); animate(); console.log("three init end..."); })(); </script> </body> </html>