Three.js - BoxGeometry 盒子几何体
THREE.BoxGeometry 创建一个具有指定尺寸的简单 3D 盒子。这是 PlaneGeometry 在 z 轴上的扩展版本,作为深度。
const box = new THREE.CubeGeometry( width, height, depth, widthSegments, heightSegments, depthSegments )
示例
查看以下示例。
cube.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 - Cube</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="threejs-container"></div> <script type="module"> // 立方体几何 // Three.js 中的简单 3D 立方体 // 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 ambientLight = new THREE.AmbientLight(0xffffff, 1) scene.add(ambientLight) const pointLight = new THREE.PointLight(0xffffff, 0.5) pointLight.position.x = 2 pointLight.position.y = 3 pointLight.position.z = 4 scene.add(pointLight) // 相机 const camera = new THREE.PerspectiveCamera(30, width / height, 0.1, 100) camera.position.set(0, 0, 10) const camFolder = gui.addFolder('Camera') camFolder.add(camera.position, 'z').min(10).max(60).step(10) camFolder.open() // cube const geometry = new THREE.BoxGeometry(1, 1, 1) const material = new THREE.MeshStandardMaterial({ color: 0x87ceeb, wireframe: true }) const materialFolder = gui.addFolder('Material') materialFolder.add(material, 'wireframe') materialFolder.open() const cube = new THREE.Mesh(geometry, material) scene.add(cube) console.log(cube) // cube properties const cubeProps = { width: 1, height: 1, depth: 1, widthSegments: 1, heightSegments: 1, depthSegments: 1, wireframe: true } // GUI for exporimenting cube properties const props = gui.addFolder('Properties') props .add(cubeProps, 'width', 1, 30) .step(1) .onChange(redraw) .onFinishChange(() => console.dir(cube.geometry)) props.add(cubeProps, 'height', 1, 30).step(1).onChange(redraw) props.add(cubeProps, 'depth', 1, 30).step(1).onChange(redraw) props.add(cubeProps, 'widthSegments', 1, 30).step(1).onChange(redraw) props.add(cubeProps, 'heightSegments', 1, 30).step(1).onChange(redraw) props.add(cubeProps, 'depthSegments', 1, 30).step(1).onChange(redraw) props.open() function redraw() { let newGeometry = new THREE.BoxGeometry( cubeProps.width, cubeProps.height, cubeProps.depth, cubeProps.widthSegments, cubeProps.heightSegments, cubeProps.depthSegments ) cube.geometry.dispose() cube.geometry = newGeometry } // 响应性 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('#threejs-container') container.append(renderer.domElement) renderer.render(scene, camera) animate() </script> </body> </html>