Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use properties from SCNFloor with Swift and Scenekit?

Tags:

swift

scenekit

I can't figure out how to access properties from the Scenekit SCNFloor class using Swift, in objective-c they do it like this:

SCNNode*floor = [SCNNode node];
floor.geometry = [SCNFloor floor];
((SCNFloor*)floor.geometry).reflectionFalloffEnd = 10;

This creates the floor, how do i acces "reflectionFalloffEnd"?

let levelFloor = SCNNode()
levelFloor.geometry = SCNFloor()

Thanks

EDIT Thanks for the quick responses it works now :)

like image 810
NC_DEV Avatar asked Jun 23 '14 19:06

NC_DEV


2 Answers

Whether you're in ObjC or Swift, for the compiler to be happy with you using SCNFloor properties or methods on a reference, that reference needs to have the type SCNFloor.

You can do that inline with a cast:

// ObjC
((SCNFloor*)floor.geometry).reflectionFalloffEnd = 10;

// Swift
(levelFloor.geometry as SCNFloor).reflectionFalloffEnd = 10

But that can get rather unwieldy, especially if you're going to set a bunch of properties on a geometry. I prefer to just use local variables (or constants, rather, since the references don't need to change):

let floor = SCNFloor()
floor.reflectionFalloffEnd = 10
floor.reflectivity = 0.5
let floorNode = SCNNode(geometry: floor)
floorNode.position = SCNVector3(x: 0, y: -10.0, z: 0)
scene.rootNode.addChildNode(floorNode)
like image 133
rickster Avatar answered Nov 15 '22 06:11

rickster


(levelFloor.geometry as SCNFloor).reflectionFalloffEnd = 10 is how you would make that cast in Swift - you were almost there!

as an alternative - if you are going to need to access this property often you may end up with a bunch of as casts in your code :) you may instead prefer to simply declare constants to hold those objects:

let levelFloor = SCNNode()
let levelFloorGeometry = SCNFloor()
levelFloor.geometry = levelFloorGeometry

levelFloorGeometry.reflectionFalloffEnd = 10
// and so on as this property of your SCNNode's geometry needs to change throughout your app
like image 44
fqdn Avatar answered Nov 15 '22 06:11

fqdn