threeJS-Helper12-RectAreaLightHelper(矩形區域光助手)
阿新 • • 發佈:2018-12-16
需要電子檔書籍或者原始碼可以Q群:828202939 希望可以和大家一起學習、一起進步!!
如有錯別字或有理解不到位的地方,可以留言或者加微信15250969798,博主會及時修改!!!!!
博主的案例並不難,只是為了更好的給想入門threeJS的同學一點點經驗!!!!!
本章節學習的內容可以從的官方文件中找到
涉及的知識點博主已經從three原始碼庫裡面摘要出來放在對應的註釋裡面
今天學習Helper裡面的RectAreaLightHelper助手!
順便加入了滑鼠控制相機的元件OrbitControls,不懂可以先忽略!
效果圖:
程式碼:
<html> <head> <title>threeJS-Helper12-RectAreaLightHelper(矩形區域光助手)</title> <style> body { background-color: #000; margin: 0px; overflow: hidden; } canvas { width: 100%; height: 100% } </style> </head> <body> <script src="../../../build/three.js"></script> <script src="../../js/lights/RectAreaLightUniformsLib.js"></script> <script src="../../js/controls/OrbitControls.js"></script> <script> var camera, scene, renderer, geometry, material, animate, controls; //常用變數 var cube, light, helper, rectLight; //自定義物件變數 var target = new THREE.Vector3(0, 0, 0); init(); animate(); function init() { scene = new THREE.Scene(); scene.background = new THREE.Color(0xcfcfcf); camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.set(0, 50, 150); camera.lookAt(target); camera.updateProjectionMatrix(); //相機更新 var ambient = new THREE.AmbientLight(0xffffff, 0.1); scene.add(ambient); // RectAreaLight( color:顏色, intensity:強度, width:寬, height:高 ) rectLight = new THREE.RectAreaLight(0xcfcfcf, 1, 100, 100); rectLight.position.set(30, 50, -30); scene.add(rectLight); //這裡設定2個平面,來顯示區域光 var rectLightMesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(), new THREE.MeshBasicMaterial({ //這裡可以設定區域光片面材質的屬性 } )); rectLightMesh.scale.x = rectLight.width; rectLightMesh.scale.y = rectLight.height; rectLight.add(rectLightMesh); var rectLightMeshBack = new THREE.Mesh(new THREE.PlaneBufferGeometry(), new THREE.MeshBasicMaterial({ color: 0xcfcfff })); rectLightMeshBack.rotation.y = Math.PI; rectLightMesh.add(rectLightMeshBack); // PolarGridHelper( radius:標網格的半徑, radials:徑向線的數量, circles:圓圈數, // divisions:每個圓圈使用的線段數, color1:用於網格元素的第一種顏色, color2:用於網格元素的第一種顏色 ) var radius = 100; var radials = 16; var circles = 8; var divisions = 64; var PolarGridHelper = new THREE.PolarGridHelper(radius, radials, circles, divisions); PolarGridHelper.position.set(0, -20, 0) scene.add(PolarGridHelper) renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(window.innerWidth, window.innerHeight); renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap; renderer.gammaInput = true; renderer.gammaOutput = true; document.body.appendChild(renderer.domElement); //OrbitControls控制元件操作模組 controls = new THREE.OrbitControls(camera, renderer.domElement); controls.addEventListener('change', function () { renderer.render(scene, camera); }); // BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) geometry = new THREE.BoxGeometry(30, 30, 30, 3, 3, 3); material = new THREE.MeshStandardMaterial({ color: 0xcfcfcf, wireframe: false, castShadow: true, receiveShadow: true, roughness: 0, //表面放射粗糙度 metalness: 0 //金屬感 }); //wireframe預設為false cube = new THREE.Mesh(geometry, material); cube.position.x=20; scene.add(cube); var matStdObjects = new THREE.MeshStandardMaterial( { color: 0xA0cf00, roughness: 0, metalness: 0.6 } ); var mshStdBox = new THREE.Mesh( geometry, matStdObjects ); mshStdBox.position.set(-30, 0, 20 ); mshStdBox.rotation.set( 0, Math.PI / 2.0, 0 ); mshStdBox.castShadow = true; mshStdBox.receiveShadow = true; scene.add( mshStdBox ); } function animate() { requestAnimationFrame(animate); controls.update(); var t = (Date.now() / 2000); var r = 25.0; var lx = r * Math.cos(t); rectLight.position.set(lx, rectLight.position.y, rectLight.position.z); rectLight.lookAt(target); cube.rotation.y += 0.01; renderer.render(scene, camera); }; </script> </body> </html>