Skip to content

Instantly share code, notes, and snippets.

@heckj
Created April 20, 2019 22:28
Show Gist options
  • Save heckj/b68be6945d75bad06d6e5acde06b463a to your computer and use it in GitHub Desktop.
Save heckj/b68be6945d75bad06d6e5acde06b463a to your computer and use it in GitHub Desktop.
A Reusable SCNNode SubClass With An SCNPyramid Geometry
//
// Pyramid.swift
//
// Created by Josh Robbins on 24/02/2018.
// Copyright © 2018 BlackMirror. All rights reserved.
//
import Foundation
import SceneKit
class Pyramid: SCNNode{
private var faceArray = [SCNMaterial]()
/// Creates An SCNPyramid With Either A Single Colour Or Image For It's Material
/// (Either A Colour Or UIImage Must Be Input)
/// - Parameters:
/// - width: Optional CGFloat (Defaults To 30cm)
/// - height: Optional CGFloat (Defaults To 60cm)
/// - length: Optional CGFloat (Defaults To 30cm)
/// - content: Any (UIColor Or UIImage)
init(width: CGFloat = 0.3, height: CGFloat = 0.6, length: CGFloat = 0.3, content: Any) {
super.init()
//1. Create The Pyramid Geometry With Our Width, Height & Length Parameters
self.geometry = SCNPyramid(width: width, height: height, length: length)
//2. Create A New Material
let material = SCNMaterial()
//3. Set The Material Contents
if let colour = content as? UIColor{
//The Material Will Be A UIColor
material.diffuse.contents = colour
}else if let image = content as? UIImage{
//The Material Will Be A UIImage
material.diffuse.contents = image
}else{
//Set Our Material Colour To Cyan
material.diffuse.contents = UIColor.cyan
}
//4. Set The Material Of The Pyramid
self.geometry?.firstMaterial = material
}
/// Creates An SCNPyramid With A Different Material On Each Of Its 5 Faces
/// (Either An Array [Colour] Or [UIImage] Must Be Input)
/// - Parameters:
/// - width: Optional CGFloat (Defaults To 30cm)
/// - height: Optional CGFloat (Defaults To 60cm)
/// - length: Optional CGFloat (Defaults To 30cm)
/// - contents: [Any] - [Front, Right , Left, Back, Bottom]
init(width: CGFloat = 0.3, height: CGFloat = 0.6, length: CGFloat = 0.3, contents: [Any]) {
super.init()
//1. Create The Pyramid Geometry With Our Width, Height & Length Parameters
self.geometry = SCNPyramid(width: width, height: height, length: length)
//2. Create A Temporary Array To Store Either Our UIColors Or UIImages
var sideArray = [Any]()
//3. Assign The Colours Or UIImages
if let colours = contents as? [UIColor], colours.count == 5{
//The Materials Will Be A UIColor
sideArray = colours
}else if let images = contents as? [UIImage], images.count == 5{
//The Materials Will Be A UIImage
sideArray = images
}else{
//Set Our Material Colours To Red, Orange, Green, Blue, Purple
sideArray = [UIColor.red, UIColor.orange, UIColor.green, UIColor.blue, UIColor.purple]
}
//5. Loop Through The Number Of Faces & Create A New Material For Each
for index in 0 ..< 5{
let face = SCNMaterial()
face.diffuse.contents = sideArray[index]
//Add The Material To Our Face Array
faceArray.append(face)
}
//6. Set The Pyramids Materials From Our Face Array
self.geometry?.materials = faceArray
}
required init?(coder aDecoder: NSCoder) { fatalError("Pyramid Node Coder Has Not Been Implemented") }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment