Kusal Shrestha
Kusal Shrestha

Reputation: 1673

Scene not rendering properly in SceneKit

I created a floor in a scene and loaded a collada file(.dae) into my scene and tried to create a plane below that model. But I have problems like below.

enter image description here

This problem generates only when custom camera is used. Scene renders properly when system camera is used i.e if no custom camera is added My code is as below:

import UIKit import SceneKit

class TestVC: UIViewController, SCNSceneRendererDelegate {

var cameraNode: SCNNode!
var modelNode: SCNNode!
var scnView: SCNView!
var camNode: SCNNode!

var cameraPosition: SCNVector3!

override func viewDidLoad() {
    super.viewDidLoad()

    scnView = SCNView(frame: self.view.frame)
    scnView.delegate = self
    view.addSubview(scnView)
    scnView.delegate = self

    let scene = SCNScene()
    scnView.scene = scene

    //camera
    let camera = SCNCamera()
    cameraNode = SCNNode()
    cameraNode.camera = camera
    camera.zFar = 1000
    cameraPosition = SCNVector3(0, 100, 150)
    cameraNode.position = cameraPosition
    scene.rootNode.addChildNode(cameraNode)

    //floor
    let floor = SCNFloor()
    floor.reflectivity = 0
    let floorNode = SCNNode(geometry: floor)
    let firstmaterial = SCNMaterial()
    firstmaterial.diffuse.contents = UIImage(named: "art.scnassets/grass.jpg")
    firstmaterial.diffuse.wrapS = SCNWrapMode.Repeat
    firstmaterial.diffuse.wrapT = SCNWrapMode.Repeat
    floor.materials = [firstmaterial]
    scene.rootNode.addChildNode(floorNode)

    let light = SCNLight()
    let lightNode = SCNNode()
    lightNode.light = light
    light.type = SCNLightTypeAmbient
    light.color = UIColor.darkGrayColor()
    scene.rootNode.addChildNode(lightNode)

    let light1 = SCNLight()
    let light1Node = SCNNode()
    light1Node.light = light1
    light1Node.position = SCNVector3(0, 200, -100)
    light1.type = SCNLightTypeOmni
    scene.rootNode.addChildNode(light1Node)

    let modelScene = SCNScene(named: "Barcelona Chair.dae")
    let solNode = modelScene?.rootNode
    solNode?.eulerAngles = SCNVector3(GLKMathDegreesToRadians(-90), 0, 0)
    scene.rootNode.addChildNode(solNode!)
    scnView.allowsCameraControl = true

    let plane = SCNPlane(width: 100, height: 100)
    let planeNode = SCNNode(geometry: plane)
    planeNode.pivot = SCNMatrix4MakeRotation(Float(M_PI_2), 1, 0, 0)//(CGFloat(M_PI_2), 1, 0, 0)
    planeNode.position = SCNVector3(0, 1, 0)
    plane.firstMaterial?.diffuse.contents = UIColor.redColor()
    scene.rootNode.addChildNode(planeNode)

}

Upvotes: 0

Views: 1488

Answers (2)

cc.
cc.

Reputation: 3051

What you're probably seeing is known as "z-fighting". Basically, your floor and your plane are coexisting - they are coplanar - so the rendering of them is ambiguous. Try lifting the plane up a little by setting its position higher than the floor plane, and you should see this problem go away.

Upvotes: 1

Kusal Shrestha
Kusal Shrestha

Reputation: 1673

Don't know why the above problem exists but I got a different way of doing the thing. You can use SCNShape to draw an overlay over a floor. I drew a rectangular path using UIBezierPath and set the path property of the SCNShape class. :)

Upvotes: 0

Related Questions