Skip to content

Instantly share code, notes, and snippets.

@heckj
Created April 21, 2019 15:22
Show Gist options
  • Save heckj/1fdb2a6bde3a915b80f0f63cc71dd9da to your computer and use it in GitHub Desktop.
Save heckj/1fdb2a6bde3a915b80f0f63cc71dd9da to your computer and use it in GitHub Desktop.
A Reusable SCNNode SubClass With An SCNCapsule Geometry
//
// Capsule.swift
//
//
// Created by Josh Robbins on 24/02/2018.
// Copyright © 2018 BlackMirror. All rights reserved.
//
import Foundation
import SceneKit
class Capsule: SCNNode{
/// Creates An SCNCapsule With Either A Single Colour Or Image For It's Material
/// (Either A Colour Or UIImage Must Be Input)
/// - Parameters:
/// - capRadius: Optional CGFloat (Defaults To 20cm)
/// - height: Optional CGFloat (Defaults To 80cm)
/// - content: Any (UIColor Or UIImage)
init(capRadius: CGFloat = 0.2, height: CGFloat = 0.8, content: Any) {
super.init()
//1. Create The Capsule's Geometry With Our Radius Parameter
self.geometry = SCNCapsule(capRadius: capRadius, height: height)
//2. Create A New Material
let material = SCNMaterial()
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
}
//3. Set The Material Of The Capsule
self.geometry?.firstMaterial = material
}
required init?(coder aDecoder: NSCoder) { fatalError("Capsule Node Coder Not Implemented") }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment