Skip to content

Instantly share code, notes, and snippets.

@TachibanaKaoru
Created July 24, 2021 14:34
Show Gist options
  • Save TachibanaKaoru/9f3c2824f0ae250f730ae27f8b8243dd to your computer and use it in GitHub Desktop.
Save TachibanaKaoru/9f3c2824f0ae250f730ae27f8b8243dd to your computer and use it in GitHub Desktop.
import SceneKit
import PlaygroundSupport
// 表示用のSCNViewを作る。
var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
var scene = SCNScene()
sceneView.scene = scene
sceneView.autoenablesDefaultLighting = true
// PlaygroundPageに作ったSCNViewを設定。
PlaygroundPage.current.liveView = sceneView
PlaygroundPage.current.needsIndefiniteExecution = true
// camera nodeの作成
var cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(x: 0, y: 0, z: 3)
scene.rootNode.addChildNode(cameraNode)
// cone nodeの作成
let cone = SCNCone(topRadius: 0.0, bottomRadius: 0.3, height: 1.0)
let coneNode = SCNNode(geometry: cone)
cone.firstMaterial?.diffuse.contents = UIColor.systemTeal
cone.firstMaterial?.specular.contents = UIColor.white
coneNode.rotation = SCNVector4(x: 0.0, y: 0.0, z: 1.0, w: 0.0)
var conePivot = SCNMatrix4MakeTranslation(0.0, 1.0, 0.5)
coneNode.pivot = conePivot
scene.rootNode.addChildNode(coneNode)
// capsule nodeの作成
let capsule = SCNCapsule(capRadius: 0.3, height: 0.8)
let capsuleNode = SCNNode(geometry: capsule)
capsule.firstMaterial?.diffuse.contents = UIColor.systemPink
capsule.firstMaterial?.specular.contents = UIColor.white
capsuleNode.rotation = SCNVector4(x:1.0, y:0.8, z:0.0, w:0.0)
scene.rootNode.addChildNode(capsuleNode)
// cone node のアニメーションの設定
var spinAnimation = CABasicAnimation(keyPath: "rotation.w")
spinAnimation.repeatCount = HUGE
spinAnimation.toValue = Double.pi * 2.0
spinAnimation.duration = 4.0
spinAnimation.autoreverses = true
coneNode.addAnimation(spinAnimation, forKey: "spin animation")
// capsule node のアニメーションの設定
var moveAnimation = CABasicAnimation(keyPath: "position.y")
moveAnimation.repeatCount = HUGE
moveAnimation.toValue = 1.2
moveAnimation.duration = 3.0
moveAnimation.autoreverses = true
capsuleNode.addAnimation(moveAnimation, forKey: "move animation")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment