-
-
Save nickarthur/48af0c70a40d6bbcbc27b6983371f8b9 to your computer and use it in GitHub Desktop.
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
// | |
// Plane.swift | |
// TextAR | |
// | |
// Created by Arvind Ravi on 23/08/17. | |
// Copyright © 2017 Arvind Ravi. All rights reserved. | |
// | |
import Foundation | |
import ARKit | |
class Plane: SCNNode { | |
// MARK: - Properties | |
var anchor: ARPlaneAnchor | |
var height: CGFloat = 0.01 | |
// MARK: - Initialization | |
init(_ anchor: ARPlaneAnchor) { | |
self.anchor = anchor | |
super.init() | |
// init geometry | |
self.geometry = SCNBox(width: CGFloat(anchor.extent.x), height: height, length: CGFloat(anchor.extent.z), chamferRadius: 0.0) | |
self.geometry?.firstMaterial?.diffuse.contents = UIColor.white.withAlphaComponent(0.5) | |
// init position | |
self.position = SCNVector3( | |
anchor.center.x, | |
anchor.center.y, | |
anchor.center.z | |
) | |
// physics | |
self.physicsBody = SCNPhysicsBody(type: .kinematic, shape: SCNPhysicsShape(geometry: geometry, options: nil)) | |
// disable shadow for planes | |
castsShadow = false | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
func update(_ anchor: ARPlaneAnchor) { | |
self.anchor = anchor | |
// update geometry | |
if let geometry = self.geometry as? SCNBox { | |
geometry.width = CGFloat(anchor.extent.x) | |
geometry.length = CGFloat(anchor.extent.z) | |
geometry.height = height | |
} | |
// update position | |
self.position = SCNVector3( | |
anchor.center.x, | |
anchor.center.y, | |
anchor.center.z | |
) | |
// physics | |
self.physicsBody = SCNPhysicsBody(type: .kinematic, shape: SCNPhysicsShape(geometry: geometry!, options: nil)) | |
} | |
func hide() { | |
let transparentMaterial = SCNMaterial() | |
transparentMaterial.lightingModel = .constant | |
transparentMaterial.diffuse.contents = UIColor.white | |
transparentMaterial.colorBufferWriteMask = SCNColorMask.init(rawValue: 0) | |
transparentMaterial.transparencyMode = .aOne | |
transparentMaterial.transparency = 0 | |
self.geometry?.materials = [transparentMaterial, transparentMaterial, transparentMaterial, transparentMaterial, transparentMaterial, transparentMaterial] | |
} | |
func unhide() { | |
self.geometry?.firstMaterial?.diffuse.contents = UIColor.white.withAlphaComponent(0.5) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment