|
1 | 1 | import * as THREE from "three"; |
2 | 2 | import * as OBC from "@thatopen/components"; |
| 3 | +// eslint-disable-next-line import/no-extraneous-dependencies |
| 4 | +import earcut from "earcut"; |
3 | 5 | import { Area } from "./area"; |
4 | 6 | import { MeasureMark } from "./measure-mark"; |
5 | 7 |
|
@@ -116,23 +118,34 @@ export class MeasureFill { |
116 | 118 | private updateMesh() { |
117 | 119 | if (this.area.points.size < 3) return; |
118 | 120 |
|
119 | | - const points = [...this.area.points]; |
| 121 | + const points2D = this.area.points2D; |
| 122 | + if (!points2D || points2D.length < 3) return; |
120 | 123 |
|
121 | | - const vertices = points.flatMap((point) => [point.x, point.y, point.z]); |
| 124 | + // Flatten 2D points for earcut triangulation |
| 125 | + const flatPoints = points2D.flatMap((point) => [point.x, point.y]); |
122 | 126 |
|
123 | | - // Generate indices for the triangles (assuming the points are coplanar and ordered) |
124 | | - const indices: number[] = []; |
125 | | - for (let i = 1; i < points.length - 1; i++) { |
126 | | - indices.push(0, i, i + 1); // Create triangles from the first point and subsequent pairs |
| 127 | + // Triangulate using earcut |
| 128 | + const indices = earcut(flatPoints); |
| 129 | + |
| 130 | + // Convert 2D points back to 3D for the geometry |
| 131 | + const vertices: number[] = []; |
| 132 | + for (const point2D of points2D) { |
| 133 | + const point3D = this.area.convertPointTo3D(point2D); |
| 134 | + if (point3D) { |
| 135 | + vertices.push(point3D.x, point3D.y, point3D.z); |
| 136 | + } |
127 | 137 | } |
128 | 138 |
|
129 | | - this.three.geometry.dispose(); |
| 139 | + // Dispose old geometry if it exists |
| 140 | + if (this.three.geometry) { |
| 141 | + this.three.geometry.dispose(); |
| 142 | + } |
130 | 143 |
|
| 144 | + // Create new geometry with triangulated indices |
131 | 145 | const geo = new THREE.BufferGeometry(); |
132 | 146 | geo.setAttribute("position", new THREE.Float32BufferAttribute(vertices, 3)); |
133 | 147 | geo.setIndex(indices); |
134 | 148 | this.three.geometry = geo; |
135 | | - |
136 | 149 | this.three.material = this.material; |
137 | 150 | } |
138 | 151 |
|
|
0 commit comments