RealityKit - Animate opacity of a ModelEntity?

后端 未结 3 1331
天命终不由人
天命终不由人 2021-01-06 12:59

By setting the color of a material on the model property of a ModelEntity, I can alter the opacity/alpha of an object. But how do you animate this?

3条回答
  •  無奈伤痛
    2021-01-06 14:02

    As @AndyFedo says there is currently no way to animate the opacity nor alpha of an Entity.

    Even changing a SimpleMaterial at run time currently results in flickering.

    Having said this I was able to animate the Alpha of a SimpleMaterials Color, however based on testing it is in no way optimal or recommended for that matter.

    But just in case you wanted to try to further experiment with this avenue please see an attached example which assumes that you only have a single SimpleMaterial:

    class CustomBox: Entity, HasModel, HasAnchoring {
    
      var timer: Timer?
      var baseColour: UIColor!
    
      //MARK:- Initialization
    
      /// Initializes The Box With The Desired Colour
      /// - Parameter color: UIColor
      required init(color: UIColor) {
        self.baseColour = color
        super.init()
        self.components[ModelComponent] = ModelComponent(mesh: .generateBox(size: [0.2, 0.2, 0.2]),
                                                         materials:  [SimpleMaterial (color:  baseColour, isMetallic: false)]
        )
      }
    
      required init() { super.init() }
    
      //MARK:- Example Fading
    
      /// Fades The Colour Of The Entities Current Material
      func fadeOut() {
    
        var alpha: CGFloat = 1.0
        timer = Timer.scheduledTimer(withTimeInterval: 0.05, repeats: true) { timer in
    
          if alpha == 0 {
            timer.invalidate()
            return
          }
    
          var material = SimpleMaterial()
          alpha -= 0.01
          material.baseColor = MaterialColorParameter.color(self.baseColour.withAlphaComponent(alpha))
          material.metallic = .float(Float(alpha))
          material.roughness = .float(Float(alpha))
          DispatchQueue.main.async {
            self.model?.materials = [material]
    
          }
        }
      }
    }
    

    As such just to test you can create and then call the function like so:

    let box = CustomBox(color: .green)
    box.position = [0,0,-0.5]
    arView.scene.anchors.append(box)
    box.fadeOut()
    

    Also I would politely ask, that this answer not get downvoted as I am simply iterating the fact that (a) it isn't possible with any current built in methods, and (b) that it can in part be achieved albeit to a very limited extent (and thus currently; in a way which one would see fit for production).

提交回复
热议问题