Skip to content

Instantly share code, notes, and snippets.

@heckj
Created April 20, 2019 22:28
Show Gist options
  • Save heckj/db5847c1cb6f5a0fefaef36c07493f53 to your computer and use it in GitHub Desktop.
Save heckj/db5847c1cb6f5a0fefaef36c07493f53 to your computer and use it in GitHub Desktop.
A Reusable SCNNode SubClass With An SCNTube Geometry
//
// Tube.swift
//
// Created by Josh Robbins on 24/02/2018.
// Copyright © 2018 BlackMirror. All rights reserved.
//
import Foundation
import SceneKit
class Tube: SCNNode{
private var faceArray = [SCNMaterial]()
/// Creates An SCNTube With Either A Single Colour Or Image For It's Material
/// (Either A Colour Or UIImage Must Be Input)
/// - Parameters:
/// - innerRadius: Optional CGFloat (Defaults To 10cm)
/// - outerRadius: Optional CGFloat (Defaults To 30cm)
/// - height: Optional CGFloat (Defaults To 60cm)
/// - content: Any (UIColor Or UIImage)
init(innerRadius: CGFloat = 0.1, outerRadius: CGFloat = 0.3, height: CGFloat = 0.6, content: Any) {
super.init()
//1. Create The Tubes Geometry With Our Radius Parameter
self.geometry = SCNTube(innerRadius: innerRadius, outerRadius: outerRadius, height: height)
//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 Tube
self.geometry?.firstMaterial = material
}
/// Creates An SCNTube With Either A Colour Or UIImage For It's Base, Top And Outer & Inner Surfaces
/// (Either An Array [Colour] Or [UIImage] Must Be Input)
/// - Parameters:
/// - innerRadius: Optional CGFloat (Defaults To 10cm)
/// - outerRadius: Optional CGFloat (Defaults To 30cm)
/// - height: Optional CGFloat (Defaults To 60cm)
/// - contents: [Any] - [Outer Surface, Inner Surface, Top, Bottom]
init(innerRadius: CGFloat = 0.1, outerRadius: CGFloat = 0.3, height: CGFloat = 0.6, contents: [Any]) {
super.init()
//1. Create The Tube Geometry With Our Inner & Outer Radiuses As Well As Our Height
self.geometry = SCNTube(innerRadius: innerRadius, outerRadius: outerRadius, height: height)
//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 == 4{
//The Materials Will Be A UIColor
sideArray = colours
}else if let images = contents as? [UIImage], images.count == 4{
//The Materials Will Be A UIImage
sideArray = images
}else{
//Set Our Material Colours To Red, Orange & Green
sideArray = [UIColor.red, UIColor.orange, UIColor.green, UIColor.blue]
}
//4. Loop Through The Number Of Faces & Create A New Material For Each
for index in 0 ..< 4{
let face = SCNMaterial()
face.diffuse.contents = sideArray[index]
//Add The Material To Our Face Array
faceArray.append(face)
}
//5. Set The Tubes Materials From Our Face Array
self.geometry?.materials = faceArray
}
required init?(coder aDecoder: NSCoder) { fatalError("Tube Node Coder Not Implemented") }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment