Last active
April 14, 2025 19:56
-
-
Save drewolbrich/82b6036f9f11a71585fc53b7e3b3f41a 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MeshEntity+GenerateSolidInteriorModel.swift | |
// | |
// Created by Drew Olbrich on 12/16/23. | |
// Copyright © 2023 Lunar Skydiving LLC. All rights reserved. | |
// | |
// MIT License | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
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