I am trying to crop and edit a 3d model which I created using photogrammetry ,I tried multiple solutions but seems like none of that worked , posting in urge of guidance , let me address what I did , I tried to select the vertices where user tap and removed that from my geometry but that did not work , also I tried to make a new geometry with a texture added to specific part of the geometry, but this all didn't worked
func cropModelIs(node: SCNNode, geometry: SCNGeometry, selectedVertices: [SCNVector3]) {
var newVertices: [SCNVector3] = []
var newIndices: [UInt32] = []
var vertexIndexMap = [Int: Int]()
var newVertexIndex = 0
// Iterate through the existing vertices and exclude selected vertices
for (vertexIndex, vertex) in getVerticesFromGeometry(geometry: geometry).enumerated() {
if !selectedVertices.contains(vertex) {
newVertices.append(vertex)
vertexIndexMap[vertexIndex] = newVertexIndex
newVertexIndex += 1
}
}
// Iterate through the existing triangles and update their indices
for element in geometry.elements {
guard element.primitiveType == .triangles else {
continue
}
let indices: [UInt32] = element.data.withUnsafeBytes { dataPtr in
Array(dataPtr.bindMemory(to: UInt32.self))
}
// Create new triangle indices by mapping old indices to new indices
for i in stride(from: 0, to: indices.count, by: 3) {
let oldIndices = [indices[i], indices[i + 1], indices[i + 2]]
// Check if all vertices in the triangle are valid (not removed)
let shouldKeepTriangle = oldIndices.allSatisfy { vertexIndexMap.keys.contains(Int($0)) }
if shouldKeepTriangle {
// All vertices are valid, add the triangle with updated indices
let newTriangleIndices = oldIndices.map { UInt32(vertexIndexMap[Int($0)]!) }
// Make sure the new indices form a valid triangle
if newTriangleIndices[0] != newTriangleIndices[1] && newTriangleIndices[1] != newTriangleIndices[2] && newTriangleIndices[2] != newTriangleIndices[0] {
newIndices.append(contentsOf: newTriangleIndices)
}
}
}
}
// Create a new geometry with the updated vertices and indices
let newGeometry = SCNGeometry(sources: [SCNGeometrySource(vertices: newVertices)], elements: [SCNGeometryElement(indices: newIndices, primitiveType: .triangles)])
// var meshGeometry = SCNGeometry()
// Apply materials to the new geometry (not shown in your code)
// Update the geometry of the original node (assuming it's the right hierarchy)
if let imageURL = getImageFromMeshTexture(node: originalNode, materialIndex: 0){
let newGeometryIs = applyTextureToGeometry(geometry: newGeometry, imageURL: imageURL)
// Now you can create a new node using the new geometry
// let newNode = SCNNode(geometry: newGeometryIs)
originalNode.childNode(withName: "Geometry", recursively: true)?.childNodes[0].geometry = newGeometryIs
// Replace the original node with the new node
// if let parentNode = originalNode.parent {
// originalNode.removeFromParentNode()
// parentNode.addChildNode(newNode)
// }
}
}