通过设置材质的颜色model
的财产ModelEntity
,我可以改变对象的不透明度/alpha。但如何将其动画化呢?我的目标是使对象具有完全不透明度的动画,然后让它们淡入设定的不透明度,例如 50%。
With SCNAction.fadeOpacity
on a SCNNode
in SceneKit
,这特别容易。
let fade = SCNAction.fadeOpacity(by: 0.5, duration: 0.5)
node.runAction(fade)
An Entity
符合HasTransform
,但这只允许您对比例、位置和方向进行动画处理。与淡入或淡出之类的材质动画无关。如果您创建一个用于动画隐藏或显示的行为,则效果在 RealityComposer 中,但似乎没有类似的东西HasTransform
提供动画不透明度的功能。
我一直在文档中寻找一些东西,我的下一个想法本质上是创建一个自定义动画来替换这种行为,但它似乎应该可用,但我只是没有找到它。
我使用不同的技术对其进行了测试,并得出了悲伤的结论:您无法在 RealityKit 框架中对材质的不透明度进行动画处理,因为RealityKit materials don't support animation at runtime
(现在我希望)。让我们一起等待RealityKit的重大更新吧。
这是您可以用于测试的代码
(arView.alpha
财产只是工作):
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
arView.alpha = 1.0
opacityAnimation()
}
func opacityAnimation() {
UIView.animate(withDuration: 5.0,
animations: {
self.arView.alpha = 0.0
})
}
}
并使用此代码片段以确保动画无法正常工作
(没有动画过程,只是赋值):
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
let tetheringAnchor = AnchorEntity(world: [0,0,0])
var material = SimpleMaterial()
let mesh: MeshResource = .generateSphere(radius: 0.5)
var sphereComponent: ModelComponent? = nil
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
material.metallic = .float(1.0)
material.roughness = .float(0.0)
material.baseColor = .color(.red)
sphereComponent = ModelComponent(mesh: mesh,
materials: [material])
tetheringAnchor.components.set(sphereComponent!)
arView.scene.anchors.append(tetheringAnchor)
opacityAnimation()
}
func opacityAnimation() {
UIView.animate(withDuration: 5.0,
animations: {
self.material.metallic = .float(1.0)
self.material.roughness = .float(0.0)
self.material.baseColor = .color(.green)
self.sphereComponent = ModelComponent(mesh: self.mesh,
materials: [self.material])
self.tetheringAnchor.components.set(self.sphereComponent!)
self.arView.scene.anchors.append(self.tetheringAnchor)
})
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)