Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save haikusw/a1779ec12bb845f100c7e047f8b790cf to your computer and use it in GitHub Desktop.
Save haikusw/a1779ec12bb845f100c7e047f8b790cf to your computer and use it in GitHub Desktop.
A RealityKit ModelEntity extension that replaces its model with a new model with reversed triangle indices
//
// MeshEntity+GenerateSolidInteriorModel.swift
//
// Created by Drew Olbrich on 12/16/23.
//
import RealityKit
extension ModelEntity {
/// Replaces the model entity's `model` property with a new model suitable for
/// drawing a solid interior on visionOS.
///
/// In a better world, we'd simply enable front face culling for the solid interior
/// geometry, but on visionOS, RealityKit doesn't provide a `faceCulling` option for
/// `ShaderGraphMaterial`, like it does for `CustomMaterial` on iOS.
///
/// To work around that limitation, this method reverses the order of the triangle
/// indices in the model entity's meshes. RealityKit's back face culling will then
/// effectively perform front face culling.
func generateSolidInteriorModel() async {
guard let model else {
assertionFailure("model is undefined")
return
}
var updatedContents = MeshResource.Contents()
let contents = model.mesh.contents
updatedContents.instances = contents.instances
updatedContents.skeletons = contents.skeletons
var updatedModels = MeshModelCollection()
for model in contents.models {
var updatedParts: [MeshResource.Part] = []
for part in model.parts {
guard let triangleIndices = part.triangleIndices else {
assertionFailure("triangleIndices is undefined")
updatedParts.append(part)
return
}
let reversedTriangleIndices = MeshBuffers.TriangleIndices(triangleIndices.elements.reversed())
var updatedPart = MeshResource.Part(id: part.id, materialIndex: part.materialIndex)
updatedPart.triangleIndices = reversedTriangleIndices
updatedPart.positions = part.positions
// We don't need the texture coordinates or normals for the solid interior
// geometry, but if we remove the following lines, our regular lit exterior
// geometry mysteriously disappears.
updatedPart.textureCoordinates = part.textureCoordinates
updatedPart.normals = part.normals
updatedPart.tangents = part.tangents
updatedPart.bitangents = part.bitangents
updatedPart.skeletonID = part.skeletonID
updatedPart.jointInfluences = part.jointInfluences
updatedParts.append(updatedPart)
}
let updatedModel = MeshResource.Model(id: model.id, parts: updatedParts)
updatedModels.insert(updatedModel)
}
updatedContents.models = updatedModels
guard let updatedMesh = try? await MeshResource(from: updatedContents) else {
assertionFailure("updatedMesh is undefined")
return
}
let updatedModel = ModelComponent(mesh: updatedMesh, materials: model.materials)
self.model = updatedModel
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment