Skip to content

Commit 2d139fa

Browse files
committed
Add triangles member for mesh and polygon
1 parent 7bbf5ea commit 2d139fa

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

ShapeScript/Interpreter.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ private extension RuntimeError {
484484
"translation": ["position"],
485485
"position": ["translate", "bounds", "center"],
486486
"faces": ["polygons"],
487-
"triangles": ["polygons"],
488487
"vertices": ["points"],
489488
"scale": ["size"],
490489
"size": ["scale", "bounds"],

ShapeScript/Members.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ extension ValueType {
114114
"count": .number,
115115
"points": .list(.point),
116116
"polygons": .list(.polygon),
117+
"triangles": .list(.polygon),
117118
"lines": .list(.string),
118119
"words": .list(.string),
119120
"characters": .list(.string),
@@ -177,13 +178,13 @@ extension Value {
177178
case let .mesh(geometry):
178179
var members = ["name", "bounds"]
179180
if geometry.hasMesh {
180-
members += ["polygons", "material"]
181+
members += ["polygons", "triangles", "material"]
181182
}
182183
return members
183184
case .path:
184185
return ["bounds", "points"]
185186
case .polygon:
186-
return ["bounds", "center", "points"]
187+
return ["bounds", "center", "points", "triangles"]
187188
case .point:
188189
return ["x", "y", "z", "position", "color", "isCurved"]
189190
case .bounds:
@@ -319,6 +320,11 @@ extension Value {
319320
let polygons = (geometry.mesh?.polygons ?? [])
320321
.transformed(by: geometry.transform)
321322
return .tuple(polygons.map { .polygon($0) })
323+
case "triangles" where geometry.hasMesh:
324+
_ = geometry.build { !isCancelled() }
325+
let triangles = (geometry.mesh?.triangulate().polygons ?? [])
326+
.transformed(by: geometry.transform)
327+
return .tuple(triangles.map { .polygon($0) })
322328
case "material" where geometry.hasMesh:
323329
return .material(geometry.material)
324330
default:
@@ -338,6 +344,8 @@ extension Value {
338344
return .vector(polygon.center)
339345
case "points":
340346
return .tuple(polygon.vertices.map { .point(PathPoint($0)) })
347+
case "triangles":
348+
return .tuple(polygon.triangulate().map { .polygon($0) })
341349
default:
342350
return nil
343351
}

docs/src/expressions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,15 @@ for point in circle.points {
312312
}
313313
```
314314

315-
For [meshes](meshes.md) you can access the `name`, `bounds`, `polygons` and `material` members:
315+
For [meshes](meshes.md) you can access the `name`, `bounds`, `polygons`, `triangles` and `material` members:
316316

317317
```swift
318318
print cube.bounds.size // prints 1 1 1
319319
print cube.polygons.count // prints 6
320+
print cube.triangles.count // prints 12
320321
```
321322

322-
For [polygons](meshes.md#polygons-and-points) you can get the `bounds` or `center`, or use `points` to access the individual vertices. For points you can access the `position` and `color`:
323+
For [polygons](meshes.md#polygons-and-points) you can get the `bounds` or `center`, use `triangles` to get the triangles that make up the polygon, or use `points` to access the individual vertices. For points you can access the `position` and `color`:
323324

324325
```swift
325326
define triangle polygon {

0 commit comments

Comments
 (0)