Skip to content

Instantly share code, notes, and snippets.

@ohbargain
Created September 21, 2016 11:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohbargain/8c71703c76623d75ee8c64be74a5f572 to your computer and use it in GitHub Desktop.
Save ohbargain/8c71703c76623d75ee8c64be74a5f572 to your computer and use it in GitHub Desktop.
CALayer as SCNMaterial content leaks...
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