Post

Replies

Boosts

Views

Activity

Adding floor to Entity created from RoomPlan USDZ file
I would like to add a floor to an Entity I created from a RoomPlan USDZ file. Here's my approach: Recursively traverse the Entity's children to get all of its vertices. Find the minimum and maximum X, Y and Z values and use those to create a plane. Add the plane as a child of the room's Entity. The resulting plane has the correct size, but not the correct orientation. Here's what it looks like: The coordinate axes you see show the world origin. I rendered them with this option: arView.debugOptions = [.showWorldOrigin] That world origin matches the place and orientation where I started scanning my room. I have tried many things to align the floor with the room, but nothing has worked. I'm not sure what I'm doing wrong. Here's my recursive function that gets the vertices (I'm pretty sure this function is correct since the floor has the correct size):   func getVerticesOfRoom(entity: Entity, _ transformChain: simd_float4x4) {   let modelEntity = entity as? ModelEntity   guard let modelEntity = modelEntity else {    // If the Entity isn't a ModelEntity, skip it and check if we can get the vertices of its children    let updatedTransformChain = entity.transform.matrix * transformChain    for currEntity in entity.children {     getVerticesOfRoom(entity: currEntity, updatedTransformChain)    }    return   }   // Below we get the vertices of the ModelEntity   let updatedTransformChain = modelEntity.transform.matrix * transformChain   // Iterate over all instances   var instancesIterator = modelEntity.model?.mesh.contents.instances.makeIterator()   while let currInstance = instancesIterator?.next() {    // Get the model of the current instance    let currModel = modelEntity.model?.mesh.contents.models[currInstance.model]    // Iterate over the parts of the model    var partsIterator = currModel?.parts.makeIterator()    while let currPart = partsIterator?.next() {     // Iterate over the positions of the part     var positionsIterator = currPart.positions.makeIterator()     while let currPosition = positionsIterator.next() {      // Transform the position and store it      let transformedPosition = updatedTransformChain * SIMD4<Float>(currPosition.x, currPosition.y, currPosition.z, 1.0)      modelVertices.append(SIMD3<Float>(transformedPosition.x, transformedPosition.y, transformedPosition.z))     }    }   }   // Check if we can get the vertices of the children of the ModelEntity   for currEntity in modelEntity.children {    getVerticesOfRoom(entity: currEntity, updatedTransformChain)   }  } And here's how I call it and create the floor:      // Get the vertices of the room     getVerticesOfRoom(entity: roomEntity, roomEntity.transform.matrix)     // Get the min and max X, Y and Z positions of the room     var minVertex = SIMD3<Float>(Float.greatestFiniteMagnitude, Float.greatestFiniteMagnitude, Float.greatestFiniteMagnitude)     var maxVertex = SIMD3<Float>(-Float.greatestFiniteMagnitude, -Float.greatestFiniteMagnitude, -Float.greatestFiniteMagnitude)     for vertex in modelVertices {      if vertex.x < minVertex.x { minVertex.x = vertex.x }      if vertex.y < minVertex.y { minVertex.y = vertex.y }      if vertex.z < minVertex.z { minVertex.z = vertex.z }      if vertex.x > maxVertex.x { maxVertex.x = vertex.x }      if vertex.y > maxVertex.y { maxVertex.y = vertex.y }      if vertex.z > maxVertex.z { maxVertex.z = vertex.z }     }     // Compose the corners of the floor     let upperLeftCorner: SIMD3<Float> = SIMD3<Float>(minVertex.x, minVertex.y, minVertex.z)     let lowerLeftCorner: SIMD3<Float> = SIMD3<Float>(minVertex.x, minVertex.y, maxVertex.z)     let lowerRightCorner: SIMD3<Float> = SIMD3<Float>(maxVertex.x, minVertex.y, maxVertex.z)     let upperRightCorner: SIMD3<Float> = SIMD3<Float>(maxVertex.x, minVertex.y, minVertex.z)     // Create the floor's ModelEntity     let floorPositions: [SIMD3<Float>] = [upperLeftCorner, lowerLeftCorner, lowerRightCorner, upperRightCorner]     var floorMeshDescriptor = MeshDescriptor(name: "floor")     floorMeshDescriptor.positions = MeshBuffers.Positions(floorPositions)     // Positions should be specified in CCWISE order     floorMeshDescriptor.primitives = .triangles([0, 1, 2, 2, 3, 0])     let simpleMaterial = SimpleMaterial(color: .gray, isMetallic: false)     let floorModelEntity = ModelEntity(mesh: try! .generate(from: [floorMeshDescriptor]), materials: [simpleMaterial])     guard let floorModelEntity = floorModelEntity else {      return     }     // Add the floor as a child of the room     roomEntity.addChild(floorModelEntity) Can you think of a transformation that I could apply to the vertices or the plane to align them? Thanks for any help.
5
0
1.9k
Jun ’22