Search code examples
swiftaugmented-realityarkitrealitykit

How to recolor all model's parts when using raycasting?


I have a fanfare.reality model in my arView from Reality Composer. I do raycast by entity(at:location) and enable the ModelDebugOptionsComponent(visualizationMode: .lightingDiffuse) of the objects hit, which make the appearance of objects turns grey. However, I found only the fanfare itself turns grey and the flag above the fanfare does not change at all.

enter image description here

I load the fanfare.reality by LoadAsync() and print the returned value as follows. The reason is that the flag, star and fanfare itself are divded into 3 ModelEntity. In RealityKit, raycast searches the entities with CollisionComponent.only can be added to entities that have ModelComponent.

enter image description here

Therefore, my question how can I turn the entire reality model grey (fanfare+flag+star) when I tap the model on screen(by raycast).


Solution

  • Separate-parts-model approach

    You can easily retrieve all 3 models. But you have to specify this whole long hierarchical path:

    let scene = try! Experience.loadFanfare()
    
    // Fanfare – .children[0].children[0]
    let fanfare = scene.children[0] ..... children[0].children[0] as! ModelEntity
    fanfare.model?.materials[0] = UnlitMaterial(color: .darkGray)
        
    // Flag – .children[1].children[0]
    let flag = scene.children[0] ..... children[1].children[0] as! ModelEntity
    flag.model?.materials[0] = UnlitMaterial(color: .darkGray)
    
    // Star – .children[2].children[0]
    let star = scene.children[0] ..... children[2].children[0] as! ModelEntity
    star.model?.materials[0] = UnlitMaterial(color: .darkGray)
    

    I don't see much difference when retrieving model entities from .rcproject, .reality or .usdz files. According to the printed diagram, all three model-entities are located at the same level of hierarchy, they are offsprings of the same entity. The condition in the if statement can be set to its simplest form – if a ray hits a collision shape of fanfare or (||) flag or (||) star, then all three models must be recolored.

    Mono-model approach

    The best solution for interacting with 3D models through raycasting is the mono-model approach. A mono-model is a solid 3D object that does not have separate parts – all parts are combined into a whole model. Textures for mono-models are always mapped in UV editors. The mono-model can be made in 3D authoring apps like Maya or Blender.


    P.S.

    All seasoned AR developers know that Wow! AR experience isn't about code but rather about 3D content. You understand that there is no "miracle pill" for an easy solution if your 3D model consists of many parts. Competently made AR model is 75% of success when working with code.