RealityKit - 动画模型实体的不透明度? [英] RealityKit - Animate opacity of a ModelEntity?

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

问题描述

通过在 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 - 动画模型实体的不透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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