Group Group Group Group Group Group Group Group Group

Apply gif to SCNPlane

Right now my swift code creates a scnplane with a image called we.jpg. What I would like to is replace we.jpg with a gif call ball.gif. materail.diffuse.conents is where the uiimage is attached to the scnplane.

let planeGeometry = SCNPlane(width: 0.2, height: 0.35)
    let material = SCNMaterial()
    material.diffuse.contents = UIImage(named: "we.jpg")
    planeGeometry.materials = [material]

You can easily replace SCNPlane’s .jpg texture with .gif texture. But remember that in SceneKit there’s no support for animated GIF textures.

Here’s a code:

import ARKit
import SceneKit

class ViewController: UIViewController {

@IBOutlet var sceneView: ARSCNView!   // in case you're using AR app
//@IBOutlet var sceneView: SCNView!   // in case you're using VR app 
let planeNode = SCNNode()

override func viewDidLoad() {
    super.viewDidLoad()

    let scene = SCNScene()
    sceneView.scene = scene
    sceneView.allowsCameraControl = true

    planeNode.geometry = SCNPlane(width: 0.2, height: 0.35)
    planeNode.position = SCNVector3(0, 0,-0.5)
    sceneView.scene?.rootNode.addChildNode(planeNode)

    DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {  // 3 sec delay
        let image = UIImage(named: "texture.jpg")
        self.planeNode.geometry?.firstMaterial?.diffuse.contents = image
    }

    // Other stuff...
}

//...and you can replace a texture with a button's click:

@IBAction func replaceTexture(_ sender: UIButton) {
    let image = UIImage(named: "texture.gif")
    planeNode.geometry?.firstMaterial?.diffuse.contents = image
}

}