RealityKit-对ModelEntity的不透明度进行动画处理? [英] RealityKit - Animate opacity of a ModelEntity?

查看:131
本文介绍了RealityKit-对ModelEntity的不透明度进行动画处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过在ModelEntitymodel属性上设置材料的颜色,我可以更改对象的不透明度/alpha.但是,如何为它设置动画?我的目标是为具有完全不透明度的对象设置动画,然后使它们淡化为设置的不透明度,例如50%.

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? My goal is to animate objects with full opacity, then have them fade to a set opacity, such as 50%.

SceneKit中的SCNNode上使用SCNAction.fadeOpacity时,这特别容易.

With SCNAction.fadeOpacity on a SCNNode in SceneKit, this was particularly easy.

let fade = SCNAction.fadeOpacity(by: 0.5, duration: 0.5)
node.runAction(fade)

Entity符合HasTransform,但这仅允许您设置比例,位置和方向的动画.与将材质淡入或淡出一样,与材质的动画无关.如果您创建用于隐藏或显示动画的行为,则效果在RealityComposer中,但是似乎没有与HasTransform类似的东西来提供用于动画不透明度的功能.

An Entity conforms to HasTransform, but that will only allow you to animate scale, position, and orientation. Nothing to do with animation of the material for something like fading it in or out. The effect is in RealityComposer if you create a behavior for animating hide or showing, but there doesn't seem to be something similar to HasTransform to provide functionality for animating opacity.

我到处都是文档,一直在寻找东西,我的下一个想法实质上是创建一个自定义动画来替代此行为,但似乎应该可以使用,而我只是没有找到它.

I've been all around the documentation looking for something, my next idea is essentially creating a custom animation to replace this behavior, but it seems like it should be available and I am just not finding it.

推荐答案

正如@AndyFedo所说,目前无法为Entity的透明度和Alpha设置动画.

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

即使在运行时更改SimpleMaterial,当前也会导致闪烁.

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

我已经说过,我能够为SimpleMaterials Color的Alpha设置动画,但是基于测试,它绝不是最佳选择,也不推荐用于此问题.

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.

但是,以防万一,您想尝试进一步尝试使用此方法,请参阅随附的示例,该示例假定您只有一个SimpleMaterial:

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()

我还要礼貌地问,这个答案不会被人们否定,因为我只是在反复这样一个事实,即(a)任何现有的内置方法都不可能,并且(b)尽管可以部分实现在非常有限的范围内(因此目前是这样;以一种人们认为适合生产的方式).

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).

这篇关于RealityKit-对ModelEntity的不透明度进行动画处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆