Skip to content

Instantly share code, notes, and snippets.

@banjun
Created April 16, 2024 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banjun/2a351e16e63d38dcd9d28653e66ffb75 to your computer and use it in GitHub Desktop.
Save banjun/2a351e16e63d38dcd9d28653e66ffb75 to your computer and use it in GitHub Desktop.
for visionOS gestures, generate a box with its surfaces facing inward
import RealityKit
extension ShapeResource {
/// generate a box with its surfaces facing inward.
/// NOTE: size 30 works with interactions, 50 does not work with interactions
static func generateInwardBox(size: Float) async throws -> ShapeResource {
// An array of vertex positions containing discrete points on the mesh.
let meshPositions: [SIMD3<Float>] = [
.init(-1, -1, -1), // 0 LBF
.init(+1, -1, -1), // 1 RBF
.init(+1, +1, -1), // 2 RTF
.init(-1, +1, -1), // 3 LTF
.init(-1, -1, +1), // 4 LBB
.init(+1, -1, +1), // 5 RBB
.init(+1, +1, +1), // 6 RTB
.init(-1, +1, +1), // 7 LTB
].map { $0 * size / 2 }
// An array of integers used to index into the position array. 3 indices per polygon are used to define a polygon in the mesh.
// left turn only
let meshFaceIndices: [UInt16] = [
0, 1, 2, // front
0, 2, 3, // front
0, 3, 7, // left
0, 7, 4, // left
1, 6, 2, // right
1, 5, 6, // right
4, 7, 6, // back
4, 6, 5, // back
3, 6, 7, // top
3, 2, 6, // top
0, 4, 5, // bottom
0, 5, 1 // bottom
]
return try await ShapeResource.generateStaticMesh(positions: meshPositions, faceIndices: meshFaceIndices)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment