Created
September 21, 2016 11:22
-
-
Save ohbargain/8c71703c76623d75ee8c64be74a5f572 to your computer and use it in GitHub Desktop.
CALayer as SCNMaterial content leaks...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import SceneKit | |
class ViewController: NSViewController { | |
@IBOutlet weak var sceneView: SCNView! | |
@IBOutlet weak var loadButton: NSButton! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
loadButton.target = self | |
loadButton.action = #selector(self.loadAction) | |
} | |
var cameraNode : SCNNode = { | |
let camera = SCNCamera() | |
camera.usesOrthographicProjection = true | |
let node = SCNNode() | |
node.camera = camera | |
node.position.z = 50 | |
return node | |
}() | |
func loadAction(sender: NSButton) { | |
let enable = sender.state == NSOnState | |
if enable { | |
let parentNode = SCNNode() | |
let box = SCNBox(width: 50, height: 50, length: 50, chamferRadius: 1) | |
box.firstMaterial?.diffuse.contents = NSColor.red | |
let boxNode = SCNNode(geometry: box) | |
parentNode.addChildNode(boxNode) | |
let plane = SCNPlane(width: 300, height: 300) | |
plane.firstMaterial?.diffuse.contents = TestLayer() | |
let planeNode = SCNNode(geometry: plane) | |
parentNode.addChildNode(planeNode) | |
let newScene = SCNScene() | |
newScene.rootNode.addChildNode(parentNode) | |
newScene.rootNode.addChildNode(self.cameraNode) | |
self.sceneView.scene = newScene | |
self.sceneView.autoenablesDefaultLighting = true | |
} else { | |
self.sceneView.scene = nil | |
} | |
} | |
} | |
class TestLayer : CALayer { | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
override init() { | |
super.init() | |
self.frame = NSMakeRect(0, 0 , 300, 300) | |
self.backgroundColor = NSColor.clear.cgColor | |
} | |
deinit { | |
print("I'm deinited") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment