Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
Last active August 11, 2024 03:41
Show Gist options
  • Save drewolbrich/095502702487e10d9a7231c9b3a2d32c to your computer and use it in GitHub Desktop.
Save drewolbrich/095502702487e10d9a7231c9b3a2d32c to your computer and use it in GitHub Desktop.
A ModelEntity extension that reverses triangle indices
//
// ModelEntity+ReverseTriangleIndices.swift
//
// Created by Drew Olbrich on 12/10/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.
//
extension ModelEntity {
func reverseTriangleIndices() 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
updatedPart.normals = part.normals
updatedPart.tangents = part.tangents
updatedPart.bitangents = part.bitangents
updatedPart.skeletonID = part.skeletonID
updatedPart.jointInfluences = part.jointInfluences
updatedPart.textureCoordinates = part.textureCoordinates
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