Skip to content

Instantly share code, notes, and snippets.

@balachandrana
Created March 30, 2017 03:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balachandrana/644840a223b636ecd84d14a595dad33b to your computer and use it in GitHub Desktop.
Save balachandrana/644840a223b636ecd84d14a595dad33b to your computer and use it in GitHub Desktop.
3dtut10.py
from objc_util import *
import ui
import math
load_framework('SceneKit')
SCNView, SCNScene, SCNBox ,SCNNode, SCNMaterial, SCNCamera, SCNLookAtConstraint , SCNLight, SCNPlane= map(ObjCClass, ['SCNView', 'SCNScene', 'SCNBox' ,'SCNNode', 'SCNMaterial', 'SCNCamera', 'SCNLookAtConstraint', 'SCNLight', 'SCNPlane'])
class SCNVector3 (Structure):
_fields_ = [('x', c_float), ('y', c_float), ('z', c_float)]
@on_main_thread
def demo():
main_view = ui.View()
main_view_objc = ObjCInstance(main_view)
scene_view = SCNView.alloc().initWithFrame_options_(((0,0),(400, 400)), None).autorelease()
scene_view.setAutoresizingMask_(18)
scene_view.setAllowsCameraControl_(True)
main_view_objc.addSubview_(scene_view)
main_view.name = 'SceneKit Demo'
scene = SCNScene.scene()
scene_view.setScene_(scene)
root_node = scene.rootNode()
camera = SCNCamera.camera()
camera_node = SCNNode.node()
camera_node.setCamera_(camera)
camera_node.setPosition_((-3.0, 3.0, 3.0))
ambient_light = SCNLight.light()
ambient_light.setType_('ambient')
ambient_light.setColor_(UIColor.color(red=.2, green=.2, blue=.2, alpha=1.0))
ambient_light_node = SCNNode.node()
ambient_light_node.setLight_(ambient_light)
light = SCNLight.light()
light.setType_('spot')
light.setSpotInnerAngle_(20.0)
light.setSpotOuterAngle_(70.0)
light.setCastsShadow_(True)
light_node = SCNNode.node()
light_node.setLight_(light)
light_node.setPosition((1.5, 1.5, 1.5))
cube_geometry = SCNBox.boxWithWidth_height_length_chamferRadius_(1, 1, 1, 0)
cube_node = SCNNode.nodeWithGeometry_(cube_geometry)
red_material = SCNMaterial.material()
rd = red_material.diffuse()
rd.setContents_(UIColor.redColor().CGColor())
red_material.setLocksAmbientWithDiffuse_(True)
cube_geometry.setMaterials_([red_material])
plane_geometry = SCNPlane.planeWithWidth_height_(50.0, 50.0)
plane_node = SCNNode.nodeWithGeometry_(plane_geometry)
plane_node.setEulerAngles_((math.radians(-90), 0, 0))
plane_node.setPosition_((0, -.5, 0))
green_material = SCNMaterial.material()
gd = green_material.diffuse()
gd.setContents_(UIColor.greenColor().CGColor())
green_material.setLocksAmbientWithDiffuse_(True)
plane_geometry.setMaterials_([green_material])
# Add a constraint to the camera to keep it pointing to the target cube
constraint = SCNLookAtConstraint.lookAtConstraintWithTarget_(cube_node)
constraint.setGimbalLockEnabled(True)
camera_node.setConstraints_([constraint])
light_node.setConstraints_([constraint])
root_node.addChildNode_(camera_node)
root_node.addChildNode_(light_node)
root_node.addChildNode_(ambient_light_node)
root_node.addChildNode_(cube_node)
root_node.addChildNode_(plane_node)
main_view.present()
demo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment