2

I need an advice. I am using RealityKit for showing models of fences. I have models which has several meshes that shouldn't be shown at once. For example filling of the fence. There are different options and I have it all in my model:

3D usdz model showing in Xcode

I would like to show always one at the time. How can I do that? I can't edit meshes at runtime, can I? I was thinking about changing material for each mesh but when I tried to set clear color with SimpleMaterial it shows mesh in black and when I tried OcclusionMateril I get this result:

Apply OcclusionMaterial in real app

Example how I tried hide material with SimpleMaterial:

var material = SimpleMaterial()
material.baseColor = MaterialColorParameter.color(UIColor.clear)
material.tintColor = .clear
clonedEntity.model?.materials[index] = material

Or with OcclusionMaterial (in screenshot from app above):

let occlusion = OcclusionMaterial()
clonedEntity.model?.materials[index] = occlusion

Is it possible to somehow hide parts of 3D model? Or do I have to several models and when I want to change filling for example I must change whole model? Or should I somehow composite one object from multiple models (for example filling and rest of fence)? Thanks for any advice.

Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182

1 Answers1

3

The most robust way is to create a component USDZ model (containing separate parts) and then use .isEnabled instance property in RealityKit 2.0 to turn any part On or Off.

enter image description here

import UIKit
import RealityKit

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!
    let shelf = try! ModelEntity.load(named: "Shelf")
    var counter: Int = 0

    override func touchesBegan(_ touches: Set<UITouch>, 
                              with event: UIEvent?) {
        self.counter += 1

        let metal = shelf.children[0].children[0].children[0]
                         .children[0].children[0].children[0] as! ModelEntity
        let wood = shelf.children[0].children[0].children[0]
                        .children[0].children[0].children[1] as! ModelEntity

        if counter % 3 == 1 {
            metal.isEnabled = false
            wood.isEnabled = true
        } else if counter % 3 == 2 {
            metal.isEnabled = true
            wood.isEnabled = false
        } else if counter % 3 == 0 {
            metal.isEnabled = true
            wood.isEnabled = true
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        let anchor = AnchorEntity()
        anchor.addChild(shelf)
        arView.scene.anchors.append(anchor)
    }
}

If you do not like using .children[i] chaining, look at this answer,

enter image description here

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • This is exactly what I was looking for. Just how did you created that component USDZ file? Is it from design tool and exported in .usdz? Because when I load my usdz file it should have different meshes (I see it in Xcode in asset preview) but when running my entity has no children. – Libor Zapletal Jul 16 '21 at 17:02
  • Ideally, you must be the author of USDZ models. But "separate-parts-approach" is rarely possible in case you use USDZ models from libraries or from web resources. Very often, creators of USDZ content combine meshes into one group (composite group), so you'll be not able to use separate parts in such a model. – Andy Jazz Jul 17 '21 at 02:43
  • 1
    However, if you already got a composite USDZ model, you can separate its combined mesh in Maya 2022 with USD plugin installed. https://knowledge.autodesk.com/support/maya/learn-explore/caas/CloudHelp/cloudhelp/2022/ENU/Maya-USD/files/USD-for-Maya/GUID-36CFE2C3-766F-4B00-8464-E94F95E7AF4B-html.html – Andy Jazz Jul 17 '21 at 02:58
  • I am not author of USDZ but it's custom created from designer. He gave me .obj file with materials and I try to export it with Blender (https://github.com/robmcrosby/BlenderUSDZ). Do you know how can I set separated meshes in Blender? – Libor Zapletal Jul 20 '21 at 10:49
  • I know how to separate surfaces in Maya. – Andy Jazz Jul 20 '21 at 10:50