您好,登錄后才能下訂單哦!
小編給大家分享一下利用Three.js實現陰影效果的方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
先上效果圖:
實現這個效果其實很簡單,只需要設置幾個屬性就可以實現當前的效果。而上面的材質問題我將放到下一節:
(1)首先需要告訴渲染器我需要陰影,你給我生成陰影:
renderer.shadowMap.enabled = true;
(2)然后告訴燈光,我需要陰影:
light.castShadow = true;
(3)告訴模型哪些需要投射陰影:
//告訴球需要投射陰影 sphere.castShadow = true; //告訴立方體需要投射陰影 cube.castShadow = true;
(4)最后告訴最底下的平面長方形你要接受陰影:
plane.receiveShadow = true;
上面四步只要設置好了,就可以實現陰影的效果了。
注意事項:你的模型的材質一定要選擇對燈光有反應的材質,要不然不會出現效果,就是因為這個問題導致好長時間沒有整出來陰影。
案例全部代碼:
<!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; // 默認的是,沒有設置的這個清晰 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); } //用戶交互插件 鼠標左鍵按住旋轉,右鍵按住平移,滾輪縮放 var controls; function initControls() { controls = new THREE.TrackballControls( camera ); //旋轉速度 controls.rotateSpeed = 5; //變焦速度 controls.zoomSpeed = 3; //平移速度 controls.panSpeed = 0.8; //是否不變焦 controls.noZoom = false; //是否不平移 controls.noPan = false; //是否開啟移動慣性 controls.staticMoving = false; //動態阻尼系數 就是靈敏度 controls.dynamicDampingFactor = 0.3; //未知,占時先保留 //controls.keys = [ 65, 83, 68 ]; controls.addEventListener( 'change', render ); } function render() { renderer.render( scene, camera ); } //窗口變動觸發的函數 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實現陰影效果的方法”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。