Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// 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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.