
- Three.js 教程
- Three.js - 首頁
- Three.js - 簡介
- Three.js - 安裝
- Three.js - Hello Cube 應用
- Three.js - 渲染器和響應性
- Three.js - 響應式設計
- Three.js - 除錯和統計
- Three.js - 相機
- Three.js - 控制元件
- Three.js - 光照與陰影
- Three.js - 幾何體
- Three.js - 材質
- Three.js - 紋理
- Three.js - 繪製線條
- Three.js - 動畫
- Three.js - 建立文字
- Three.js - 載入 3D 模型
- Three.js - 庫和外掛
- Three.js 有用資源
- Three.js - 快速指南
- Three.js - 有用資源
- Three.js - 討論
Three.js - 繪製線條
您已經瞭解了 Three.js 中相當多的材質。現在讓我們看看一些用於繪製線條的獨特材質。我們可以使用線條繪製各種形狀和圖案。
使用 BufferGeometry
THREE.BufferGeometry 是 Three.js 中所有內建幾何體的基類。您可以透過傳遞幾何體頂點陣列來建立自己的幾何體。
瞭解更多關於 BufferGeometry 的資訊 這裡。
const points = [] points.push(new THREE.Vector3(-10, 0, 0)) points.push(new THREE.Vector3(0, -10, 0)) points.push(new THREE.Vector3(10, 0, 0))
這些是 Three.js 提供給我們建立幾何體的一些額外元素。THREE.Vector3(x, y, z) - 它在 3D 空間中建立一個點。在上面的程式碼中,我們將 3 個點新增到 points 陣列中。
const geometry = new THREE.BufferGeometry().setFromPoints(points)
THREE.BufferGeometry(),如前所述,它建立了我們的幾何體。我們使用 setFromPoints 方法使用點陣列設定幾何體。
注意 - 線在每個連續的頂點對之間繪製,但在第一個和最後一個頂點之間不繪製(線條未閉合)。
const material = new THREE.LineBasicMaterial({ // for normal lines color: 0xffffff, linewidth: 1, linecap: 'round', //ignored by WebGLRenderer linejoin: 'round', //ignored by WebGLRenderer }) // or const material = new THREE.LineDashedMaterial({ // for dashed lines color: 0xffffff, linewidth: 1,scale: 1, dashSize: 3, gapSize: 1, })
這些是線條的獨特材質。您可以使用 THREE.LineBasicMaterial 或 THREE.LineDashedMaterial 中的任何一個。
const line = new THREE.Line(geometry, material)
示例
現在,我們使用 THREE.Line 來繪製線條,而不是使用 THREE.Mesh。現在,您會看到螢幕上使用線條繪製的“V”形。
linebasic.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 - Line basic</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"> // Creating a line using LineBasicMaterial // GUI const gui = new dat.GUI() // sizes let width = window.innerWidth let height = window.innerHeight // scene const scene = new THREE.Scene() scene.background = new THREE.Color(0x262626) // camera const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100) camera.position.set(0, 0, 50) camera.lookAt(0, 0, 0) const camFolder = gui.addFolder('Camera') camFolder.add(camera.position, 'z', 10, 100) camFolder.open() // Line const points = [] points.push(new THREE.Vector3(-10, 0, 0)) points.push(new THREE.Vector3(0, -20, 0)) points.push(new THREE.Vector3(10, 0, 0)) const folders = [gui.addFolder('Poin 1'), gui.addFolder('Poin 2'), gui.addFolder('Poin 3')] folders.forEach((folder, i) => { folder.add(points[i], 'x', -30, 30, 1).onChange(redraw) folder.add(points[i], 'y', -30, 30, 1).onChange(redraw) folder.add(points[i], 'z', -30, 30, 1).onChange(redraw) folder.open() }) const geometry = new THREE.BufferGeometry().setFromPoints(points) const material = new THREE.LineBasicMaterial({ color: 0xffffff, linewidth: 2 }) const line = new THREE.Line(geometry, material) line.position.set(0, 10, 0) scene.add(line) function redraw() { let newGeometry = new THREE.BufferGeometry().setFromPoints(points) line.geometry.dispose() line.geometry = newGeometry } // responsiveness 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) }) // renderer const renderer = new THREE.WebGL1Renderer() renderer.setSize(width, height) renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) // animation function animate() { requestAnimationFrame(animate) renderer.render(scene, camera) } // rendering the scene const container = document.querySelector('#threejs-container') container.append(renderer.domElement) renderer.render(scene, camera) animate() </script> </body> </html>
輸出
示例
您可以透過指定頂點來建立任何型別的幾何體線框。檢視以下示例,我們正在繪製虛線。
dashedline.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 - Dashed line</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"> // Creating dashed line using LineDashedMaterial // GUI const gui = new dat.GUI() // sizes let width = window.innerWidth let height = window.innerHeight // scene const scene = new THREE.Scene() scene.background = new THREE.Color(0x262626) // camera const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100) camera.position.set(0, 0, 50) camera.lookAt(0, 0, 0) const camFolder = gui.addFolder('Camera') camFolder.add(camera.position, 'z', 10, 100) camFolder.open() // Line const points = [] points.push(new THREE.Vector3(-10, 0, 0)) points.push(new THREE.Vector3(0, -20, 0)) points.push(new THREE.Vector3(10, 0, 0)) const folders = [gui.addFolder('Poin 1'), gui.addFolder('Poin 2'), gui.addFolder('Poin 3')] folders.forEach((folder, i) => { folder.add(points[i], 'x', -30, 30, 1).onChange(redraw) folder.add(points[i], 'y', -30, 30, 1).onChange(redraw) folder.add(points[i], 'z', -30, 30, 1).onChange(redraw) folder.open() }) const geometry = new THREE.BufferGeometry().setFromPoints(points) const material = new THREE.LineDashedMaterial({ color: 0xffffff, linewidth: 2, scale: 1, dashSize: 3, gapSize: 2 }) const line = new THREE.Line(geometry, material) line.computeLineDistances() line.position.set(0, 10, 0) scene.add(line) console.log(line) function redraw() { let newGeometry = new THREE.BufferGeometry().setFromPoints(points) line.geometry.dispose() line.geometry = newGeometry } // responsiveness 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) }) // renderer const renderer = new THREE.WebGL1Renderer() renderer.setSize(width, height) renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) // animation function animate() { requestAnimationFrame(animate) renderer.render(scene, camera) } // rendering the scene const container = document.querySelector('#threejs-container') container.append(renderer.domElement) renderer.render(scene, camera) animate() </script> </body> </html>
輸出
廣告