Three.js - AmbientLight 环境光
它是最基本的光,可以均匀照亮整个场景。光线均匀地分布在所有方向和距离上,因此不会投射阴影。环境光会均匀地影响场景中的所有发光物体,并为物体的材质添加颜色。
const light = THREE.AmbientLight(color, intense)
示例
使用以下示例中的代码,使用不同的颜色和强度进行尝试。
ambient.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Three.js - AmbientLight</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: -applesystem, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; } html, body { height: 100vh; width: 100vw; } #threejs-container { position: block; width: 100%; height: 100%; } </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.js"></script> </head> <body> <div id="container"></div> <script type="module"> // 将环境光添加到场景中 // 如果没有此光,您将无法看到立方体的颜色 // GUI const gui = new dat.GUI() // 尺寸 let width = window.innerWidth let height = window.innerHeight // 场景 const scene = new THREE.Scene() scene.background = new THREE.Color(0x262626) // 相机 const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100) camera.position.set(0, 0, 10) // lights const light = new THREE.AmbientLight(0xffffff, 1) scene.add(light) // 灯光控制 const lightColor = { color: light.color.getHex() } const lightFolder = gui.addFolder('Ambient Light') lightFolder.addColor(lightColor, 'color').onChange(() => { light.color.set(lightColor.color) }) lightFolder.add(light, 'intensity', 0, 1, 0.01) lightFolder.open() // cube const geometry = new THREE.BoxGeometry(2, 2, 2) const material = new THREE.MeshStandardMaterial({ color: 0xffffff, wireframe: true }) const materialFolder = gui.addFolder('Material') materialFolder.add(material, 'wireframe') materialFolder.open() const cube = new THREE.Mesh(geometry, material) scene.add(cube) // 响应性 window.addEventListener('resize', () => { width = window.innerWidth height = window.innerHeight camera.aspect = width / height camera.updateProjectionMatrix() renderer.setSize(window.innerWidth, window.innerHeight) renderer.render(scene, camera) }) // 渲染器 const renderer = new THREE.WebGL1Renderer() renderer.setSize(width, height) renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) // 动画 function animate() { requestAnimationFrame(animate) cube.rotation.x += 0.005 cube.rotation.y += 0.01 renderer.render(scene, camera) } // 渲染场景 const container = document.querySelector('#container') container.append(renderer.domElement) renderer.render(scene, camera) animate() </script> </body> </html>
输出
threejs_lights_and_shadows.html