Skip to content

Instantly share code, notes, and snippets.

@banjun
Last active August 2, 2024 09:55
Show Gist options
  • Save banjun/e53c77f05ccd1dab90c6efed0a3a8c32 to your computer and use it in GitHub Desktop.
Save banjun/e53c77f05ccd1dab90c6efed0a3a8c32 to your computer and use it in GitHub Desktop.
Create Plane USDA
import simd
func generatePlaneMesh(lengthInMeter: Float = 1, divisions: Int = 100, name: String = "GeneratedPlane") {
let n = divisions
let xys: [(Int, Int)] = (0..<n).flatMap { y in (0..<n).map { x in (x, y) }}
let points: [SIMD3<Float>] = xys.map { x, y in
SIMD3(Float(x) / Float(n - 1) - 0.5, Float(y) / Float(n - 1) - 0.5, 0) * lengthInMeter
}
let faceVertexIndices: [Int] = xys.flatMap { x, y in
guard x < n - 1, y < n - 1 else { return [Int]() }
let o = x + y * n
return [o, o + 1, o + 1 + n, o + n]
}
let faceVertexCounts: [Int] = .init(repeating: 4, count: faceVertexIndices.count / 4)
// USDA
print("")
print("#usda 1.0")
print("")
print("def Mesh \"\(name)\" {")
print(" float3[] points = \(points.map {($0.x, $0.y, $0.z)})")
print(" int[] faceVertexIndices = \(faceVertexIndices)")
print(" int[] faceVertexCounts = \(faceVertexCounts)")
print("}")
print("")
}
generatePlaneMesh()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment