Marc Hampson
Marc Hampson

Reputation: 267

Scenekit - adding UIImage material to custom geometry

I'm running this in ARKit and if you try it out, the triangle appears (very tall) and 2 the right of the camera, pan round a little. The problem I have is that I can't apply an image material to the object, I can change it's colour, but not apply image:

So this works:

triangle.firstMaterial?.diffuse.contents = UIColor.red

But this doesn't:

triangle.firstMaterial?.diffuse.contents = UIImage(named: "wallpaper.jpg")

Here is the complete ViewController code from a fresh ARKit project in Xcode9:

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()

        sceneView.delegate = self

        let scene = SCNScene()

        let triangle = SCNGeometry.triangleFrom(vector1: SCNVector3(x: 0.059511181, y: -0.181795284, z: -0.0939711183), vector2: SCNVector3(x: 0.113824688, y: -0.181780666, z: -0.0674673915), vector3: SCNVector3(x: 0.113824688, y: 1.8182193, z: -0.0674673915))

        triangle.firstMaterial?.diffuse.contents = UIImage(named: "wallpaper.jpg")

        let triangleNode = SCNNode(geometry: triangle)

        triangleNode.name = "Triangle"

        scene.rootNode.addChildNode(triangleNode)

        sceneView.scene = scene

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Create a session configuration
        let configuration = ARWorldTrackingSessionConfiguration()

        // Run the view's session
        sceneView.session.run(configuration)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        sceneView.session.pause()
    }

}

extension SCNGeometry {
    class func triangleFrom(vector1: SCNVector3, vector2: SCNVector3, vector3: SCNVector3) -> SCNGeometry {

        let indices: [Int32] = [0, 1, 2]

        let source = SCNGeometrySource(vertices: [vector1, vector2, vector3])

        let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)

        return SCNGeometry(sources: [source], elements: [element])
    }
}

The only thing I've not included is the texture image but any image downloaded will do.

Thanks

Upvotes: 4

Views: 1381

Answers (1)

mnuages
mnuages

Reputation: 13462

In order to be able to map a texture onto your geometry, SceneKit needs to have a geometry source of semantic .texcoord in addition to just the positions of your vertices.

Upvotes: 1

Related Questions