使用CALayer无法正常呈现在SCNNode- ARKit(SceneKit)中加载gif图像
问题描述:
我正在处理一个AR应用程序,在其中渲染3D模型。使用CALayer无法正常呈现在SCNNode- ARKit(SceneKit)中加载gif图像
在攻子节点我正在使用的CALayer创建动画,并通过这个线程Set contents of CALayer to animated 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)
我能够呈现以下的gif加载它在diffuse.contents显示GIF图像SCNNode中的图像,但它只显示四分之一(右下角大部分gif只显示)。我想下面的步骤来纠正这一点,但还是徒劳
- 改变平面的宽度/高度
- 改变的CALayer的界限
- 不同大小的GIF图片
请人帮我试了如何使用CALayer在SCNPlane中呈现完整的gif图像。
答
作为一种变通方法我也跟着以下程序正常加载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)
一半的图像或图像的四分之一?因为图像的四分之一听起来像是定位点/原点问题 – Knight0fDragon
是的只有图像的四分之一可见。这里可能是什么问题? – yaali