2017-10-10 82 views
2

我正在處理一個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只顯示)。我想下面的步驟來糾正這一點,但還是徒勞

  1. 改變平面的寬度/高度
  2. 改變的CALayer的界限
  3. 不同大小的GIF圖片

請人幫我試瞭如何使用CALayer在SCNPlane中呈現完整的gif圖像。

+2

一半的圖像或圖像的四分之一?因爲圖像的四分之一聽起來像是定位點/原點問題 – Knight0fDragon

+0

是的只有圖像的四分之一可見。這裏可能是什麼問題? – yaali

回答

2

作爲一種變通方法我也跟着以下程序正常加載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)