您好,登錄后才能下訂單哦!
小編給大家分享一下利用Three.js實(shí)現(xiàn)陰影效果的方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
先上效果圖:
實(shí)現(xiàn)這個(gè)效果其實(shí)很簡單,只需要設(shè)置幾個(gè)屬性就可以實(shí)現(xiàn)當(dāng)前的效果。而上面的材質(zhì)問題我將放到下一節(jié):
(1)首先需要告訴渲染器我需要陰影,你給我生成陰影:
renderer.shadowMap.enabled = true;
(2)然后告訴燈光,我需要陰影:
light.castShadow = true;
(3)告訴模型哪些需要投射陰影:
//告訴球需要投射陰影 sphere.castShadow = true; //告訴立方體需要投射陰影 cube.castShadow = true;
(4)最后告訴最底下的平面長方形你要接受陰影:
plane.receiveShadow = true;
上面四步只要設(shè)置好了,就可以實(shí)現(xiàn)陰影的效果了。
注意事項(xiàng):你的模型的材質(zhì)一定要選擇對燈光有反應(yīng)的材質(zhì),要不然不會(huì)出現(xiàn)效果,就是因?yàn)檫@個(gè)問題導(dǎo)致好長時(shí)間沒有整出來陰影。
案例全部代碼:
<!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="build/three.js"></script> <script src="examples/js/controls/TrackballControls.js"></script> <script src="examples/js/libs/stats.min.js"></script> <script> var renderer; function initRender() { renderer = new THREE.WebGLRenderer({antialias:true}); renderer.setSize(window.innerWidth, window.innerHeight); //告訴渲染器需要陰影效果 renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默認(rèn)的是,沒有設(shè)置的這個(gè)清晰 THREE.PCFShadowMap document.body.appendChild(renderer.domElement); } var camera; function initCamera() { camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000); camera.position.set(0, 40, 100); camera.lookAt(new THREE.Vector3(0,0,0)); } var scene; function initScene() { scene = new THREE.Scene(); } var light; function initLight() { scene.add(new THREE.AmbientLight(0x444444)); light = new THREE.SpotLight(0xffffff); light.position.set(60,30,0); //告訴平行光需要開啟陰影投射 light.castShadow = true; scene.add(light); } function initModel() { //上面的球 var sphereGeometry = new THREE.SphereGeometry(5,20,20); var sphereMaterial = new THREE.MeshStandardMaterial({color:0x7777ff}); var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial); sphere.position.y = 5; //告訴球需要投射陰影 sphere.castShadow = true; scene.add(sphere); //輔助工具 var helper = new THREE.AxisHelper(10); scene.add(helper); //立方體 var cubeGeometry = new THREE.CubeGeometry(10,10,8); var cubeMaterial = new THREE.MeshLambertMaterial({color:0x00ffff}); var cube = new THREE.Mesh(cubeGeometry, cubeMaterial); cube.position.x = 25; cube.position.y = 5; cube.position.z = -5; //告訴立方體需要投射陰影 cube.castShadow = true; scene.add(cube); //底部平面 var planeGeometry = new THREE.PlaneGeometry(100,100); var planeMaterial = new THREE.MeshStandardMaterial({color:0xaaaaaa}); var plane = new THREE.Mesh(planeGeometry, planeMaterial); plane.rotation.x = - 0.5 * Math.PI; plane.position.y = -0; //告訴底部平面需要接收陰影 plane.receiveShadow = true; scene.add(plane); } //初始化性能插件 var stats; function initStats() { stats = new Stats(); document.body.appendChild(stats.dom); } //用戶交互插件 鼠標(biāo)左鍵按住旋轉(zhuǎn),右鍵按住平移,滾輪縮放 var controls; function initControls() { controls = new THREE.TrackballControls( camera ); //旋轉(zhuǎn)速度 controls.rotateSpeed = 5; //變焦速度 controls.zoomSpeed = 3; //平移速度 controls.panSpeed = 0.8; //是否不變焦 controls.noZoom = false; //是否不平移 controls.noPan = false; //是否開啟移動(dòng)慣性 controls.staticMoving = false; //動(dòng)態(tài)阻尼系數(shù) 就是靈敏度 controls.dynamicDampingFactor = 0.3; //未知,占時(shí)先保留 //controls.keys = [ 65, 83, 68 ]; controls.addEventListener( 'change', render ); } function render() { renderer.render( scene, camera ); } //窗口變動(dòng)觸發(fā)的函數(shù) function onWindowResize() { camera.aspect = window.innerWidth / window.innerHeight; camera.updateProjectionMatrix(); controls.handleResize(); render(); renderer.setSize( window.innerWidth, window.innerHeight ); } function animate() { //更新控制器 render(); //更新性能插件 stats.update(); controls.update(); requestAnimationFrame(animate); } function draw() { initRender(); initScene(); initCamera(); initLight(); initModel(); initControls(); initStats(); animate(); window.onresize = onWindowResize; } </script> </html>
看完了這篇文章,相信你對“利用Three.js實(shí)現(xiàn)陰影效果的方法”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。