作者:KenNaNa | 来源:互联网 | 2023-01-13 17:56
我正在开发一个3D模型的AR应用程序。
点击子节点后,我将通过使用CALayer创建动画并将其加载到diffuse.contents中(通过遵循此线程)来显示GIF图像将CALayer的内容设置为动画GIF?
let animation : CAKeyframeAnimation = createGIFAnimation(url: gifImageURL!)!
let layer = CALayer()
layer.bounds = CGRect(x: 0, y: 0, width:600, height:200)
layer.add(animation, forKey: "contents")
let newMaterial = SCNMaterial()
newMaterial.isDoubleSided = true
newMaterial.diffuse.cOntents= layer
let plane = SCNPlane(width: 5, height: 5)
plane.materials = [newMaterial]
let node = SCNNode(geometry: plane)
self.sceneView.scene.rootNode.addChildNode(node)
我能够在SCNNode中渲染gif图像,但它只显示其中的四分之一(GIF的最右下半部分仅可见)。我尝试了以下步骤来纠正此问题,但仍然徒劳。
更改平面的宽度/高度
更改CALayer的界限
尝试使用不同尺寸的gif图像
任何人都可以帮助我如何使用CALayer在SCNPlane中渲染完整的gif图像。
1> yaali..:
作为一种解决方法,我按照以下过程正确加载了GIF:
let plane = SCNPlane(width: 2, height: 2)
let bundleURL = Bundle.main.url(forResource: "engine", withExtension: "gif")
let animation : CAKeyframeAnimation = createGIFAnimation(url: bundleURL!)!
let layer = CALayer()
layer.bounds = CGRect(x: 0, y: 0, width: 900, height: 900)
layer.add(animation, forKey: "contents")
let tempView = UIView.init(frame: CGRect(x: 0, y: 0, width: 900, height: 900))
tempView.layer.bounds = CGRect(x: -450, y: -450, width: tempView.frame.size.width, height: tempView.frame.size.height)
tempView.layer.addSublayer(layer)
let newMaterial = SCNMaterial()
newMaterial.isDoubleSided = true
newMaterial.diffuse.cOntents= tempView.layer
plane.materials = [newMaterial]
let node = SCNNode(geometry: plane)
node.name = "engineGif"
let gifImagePosition = SCNVector3Make((self.virtualObjectInteraction.selectedObject?.childNodes[0].position.x)! + 2,
(self.virtualObjectInteraction.selectedObject?.childNodes[0].position.y)! + 4,
(self.virtualObjectInteraction.selectedObject?.childNodes[0].position.z)! - 2)
node.position = gifImagePosition
self.virtualObjectInteraction.selectedObject?.childNodes[0].addChildNode(node)
在createGIFAnimation创建所需动画的地方,
func createGIFAnimation(url:URL) -> CAKeyframeAnimation? {
guard let src = CGImageSourceCreateWithURL(url as CFURL, nil) else { return nil }
let frameCount = CGImageSourceGetCount(src)
// Total loop time
var time : Float = 0
// Arrays
var framesArray = [AnyObject]()
var tempTimesArray = [NSNumber]()
// Loop
for i in 0..