Three.js 自带几何体顶点教程
自带几何体顶点 ·Vertices· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么内置几何体底层都是BufferGeometrygeometry.attributes.position的结构Float32Array itemSize细分参数widthSegments 等如何影响顶点数量效果说明与 几何体 类似展示多种形状区别在于通过console.log输出各几何体的顶点属性数据理解「形状 顶点集合」。核心概念Three.js r125 全面使用BufferGeometry顶点存在 GPU 友好的 TypedArray 中geometry.attributes.position├── array: Float32Array [x0,y0,z0, x1,y1,z1, ...] ├── itemSize: 3 每个顶点 3 个分量 (x,y,z) └── count: N 顶点个数 array.length / itemSize细分Segments将曲面拆成更多三角面// PlaneGeometry(宽, 高, 宽分段, 高分段)const geometry4 new THREE.PlaneGeometry(10, 50, 10, 10); // 10×10 细分 → 更多顶点 → 可做位移/波浪等顶点动画| 几何体 | 细分参数 | 顶点数趋势 | |--------|---------|-----------| | BoxGeometry | 无固定 24 顶点 | 恒定 | | SphereGeometry | widthSegments, heightSegments | 分段↑ → 顶点↑ | | PlaneGeometry | widthSegments, heightSegments | 分段↑ → 顶点↑ |实现步骤创建多种 Geometry注意 Plane 带细分参数分别console.log(geometry.attributes.position)对比创建 Mesh 加入场景OrbitControls 交互代码要点import * as THREE from three; import { OrbitControls } from three/examples/jsm/controls/OrbitControls.js// 场景 const scene new THREE.Scene();// 创建场景 //BoxGeometry长方体 const geometry1 new THREE.BoxGeometry(10, 10, 10); // SphereGeometry球体 const geometry2 new THREE.SphereGeometry(10); // CylinderGeometry圆柱 const geometry3 new THREE.CylinderGeometry(10, 10, 100);// PlaneGeometry矩形平面 // 几何体细分数 const geometry4 new THREE.PlaneGeometry(10, 50, 10, 10); // CircleGeometry圆形平面 const geometry5 new THREE.CircleGeometry(10);const material new THREE.MeshBasicMaterial({ color: 0xff0000 }); //材质const mesh1 new THREE.Mesh(geometry1, material); mesh1.position.set(0, 0, 0); const mesh2 new THREE.Mesh(geometry2, material); mesh2.position.set(0, 0, 30); const mesh3 new THREE.Mesh(geometry3, material); mesh3.position.set(0, 0, 60); const mesh4 new THREE.Mesh(geometry4, material); mesh4.position.set(0, 0, 90); const mesh5 new THREE.Mesh(geometry5, material); mesh5.position.set(0, 0, 120);scene.add(mesh1); scene.add(mesh2); scene.add(mesh3); scene.add(mesh4); scene.add(mesh5);console.log(顶点位置数据, geometry1); console.log(顶点位置数据, geometry1.attributes.position); console.log(顶点位置数据, geometry2.attributes.position); console.log(顶点位置数据, geometry3.attributes.position); console.log(顶点位置数据, geometry4.attributes.position); console.log(顶点位置数据, geometry5.attributes.position);// AxesHelper const axesHelper new THREE.AxesHelper(150); scene.add(axesHelper);// 相机 const camera new THREE.PerspectiveCamera(); //相机 camera.position.set(400, 300, 500); //相机位置 camera.lookAt(0, 50, 40); //相机观察位置// 渲染器 const renderer new THREE.WebGLRenderer(); // 创建渲染器 const box document.getElementById(box); renderer.setSize(box.clientWidth, box.clientHeight); //渲染区域 renderer.render(scene, camera); //执行渲染 box.appendChild(renderer.domElement);;// 设置相机控件轨道控制器OrbitControls const controls new OrbitControls(camera, renderer.domElement); // 如果OrbitControls改变了相机参数重新调用渲染器渲染三维场景 controls.addEventListener(change, function () { renderer.render(scene, camera); //执行渲染操作 console.log(camera.position) });//监听鼠标、键盘事件完整源码GitHub小结本文提供自带几何体顶点完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库