Skip to content

Instantly share code, notes, and snippets.

Created July 14, 2015 17:13
Show Gist options
  • Save anonymous/8e84cc9084ab8d631f79 to your computer and use it in GitHub Desktop.
Save anonymous/8e84cc9084ab8d631f79 to your computer and use it in GitHub Desktop.
import SceneKit
// Set up a SceneKit view
let view = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
view.autoenablesDefaultLighting = true
view.scene = SCNScene()
let root = view.scene!.rootNode
// Display it live
import XCPlayground
XCPShowView("Scene Kit", view: view)
// Define a 2D path for the parabola
let path = UIBezierPath()
path.moveToPoint(CGPointZero)
path.addQuadCurveToPoint(CGPoint(x: 100, y: 0), controlPoint: CGPoint(x: 50, y: 200))
path.addLineToPoint(CGPoint(x: 99, y: 0))
path.addQuadCurveToPoint(CGPoint(x: 1, y: 0), controlPoint: CGPoint(x: 50, y: 198))
// Tweak for a smoother shape (lower is smoother)
path.flatness = 0.25
// Make a 3D extruded shape from the path
let shape = SCNShape(path: path, extrusionDepth: 10)
shape.firstMaterial?.diffuse.contents = UIColor.blueColor()
// And place it in the scene
let shapeNode = SCNNode(geometry: shape)
shapeNode.pivot = SCNMatrix4MakeTranslation(50, 0, 0)
shapeNode.eulerAngles.y = Float(-M_PI_4)
root.addChildNode(shapeNode)
// Use a shader modifier to fade opacity across the shape
let modifier = "uniform float progress;\n #pragma transparent\n vec4 mPos = u_inverseModelViewTransform * vec4(_surface.position, 1.0);\n _surface.transparent.a = clamp(1.0 - ((mPos.x + 50.0) - progress * 200.0) / 50.0, 0.0, 1.0);"
shape.shaderModifiers = [ SCNShaderModifierEntryPointSurface: modifier ]
shape.setValue(0.0, forKey: "progress")
// Animate the shader modifier variable
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(10)
shape.setValue(1.0, forKey: "progress")
SCNTransaction.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment