def main(self): main_view = ui.View() w, h = ui.get_screen_size() main_view.frame = (0,0,w,h) main_view.name = 'MDL import demo' scene_view = scn.View(main_view.frame, superView=main_view) scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleWidth scene_view.allowsCameraControl = True scene_view.backgroundColor = 'white' scene_view.rendersContinuously = True scene_view.scene = scn.Scene() root_node = scene_view.scene.rootNode floor_geometry = scn.Floor() floor_node = scn.Node.nodeWithGeometry(floor_geometry) root_node.addChildNode(floor_node) asset = MDLAsset.alloc().initWithURL_(nsurl('Lucy.obj')) mesh = asset.objectAtIndex_(0) lucy_geometry = scn.Geometry.geometryWithMDLMesh(mesh) lucy_node_1 = scn.Node.nodeWithGeometry(lucy_geometry) root_node.addChildNode(lucy_node_1) lucy_node_2 = scn.Node.nodeWithMDLObject(mesh) lucy_node_2.position = (10., 0., 0.) root_node.addChildNode(lucy_node_2) camera_node = scn.Node() camera_node.camera = scn.Camera() camera_node.position = (10., 10., 10.) camera_node.lookAt(root_node.position) root_node.addChildNode(camera_node) light_node = scn.Node() light_node.position = (-20., 20., 20) light = scn.Light() light.type = scn.LightTypeDirectional light.castsShadow = True light.shadowSampleCount = 32 light.color = (.99, 1.0, .86) light_node.light = light light_node.lookAt(root_node.position) root_node.addChildNode(light_node) main_view.present(hide_title_bar=False)
def setupEnvironment(self, scene): #add an ambient light ambientLight = scn.Node() ambientLight.light = scn.Light() ambientLight.light.type = scn.LightTypeAmbient ambientLight.light.color = (0.3, 0.3, 0.3, 1.0) scene.rootNode.addChildNode(ambientLight) #add a key light to the scene #keep an ivar for later manipulation self.mySpotLightNode = scn.Node() self.mySpotLightNode.light = scn.Light() self.mySpotLightNode.light.type = scn.LightTypeSpot self.mySpotLightNode.light.castsShadow = True self.mySpotLightNode.light.color = (0.8, 0.8, 0.8, 1.0) self.mySpotLightNode.position = scn.Vector3(0, 80, 30) self.mySpotLightNode.rotation = scn.Vector4(1, 0, 0, -M_PI/2.8) self.mySpotLightNode.light.spotInnerAngle = 0 self.mySpotLightNode.light.spotOuterAngle = 50 self.mySpotLightNode.light.shadowColor = 'black' self.mySpotLightNode.light.zFar = 40 self.mySpotLightNode.light.zNear = 5 self.mySpotLightNode.shadowSampleCount = 16 scene.rootNode.addChildNode(self.mySpotLightNode) floor = scn.Node() floor.geometry = scn.Floor() floor.geometry.firstMaterial.diffuse.contents = "resources/wood.png" scale = list(scn.Matrix4Identity) scale[0], scale[5], scale[10] = 2., 2., 1. floor.geometry.firstMaterial.diffuse.contentsTransform = scale floor.geometry.firstMaterial.locksAmbientWithDiffuse = True floor.geometry.reflectionFalloffEnd = 10 staticBody = scn.PhysicsBody.staticBody() floor.physicsBody = staticBody scene.rootNode.addChildNode(floor)
def main(self): self.main_view = ui.View() w, h = ui.get_window_size() self.main_view.frame = (0, 0, w, h) self.main_view.name = 'vehicle demo' self.close_button = ui.Button() self.close_button.action = self.close self.close_button.frame = (20, 40, 40, 40) self.close_button.background_image = ui.Image.named('emj:No_Entry_2') if DEBUG: self.scene_view = scn.View((0,25,w,h-25), superView=self.main_view) self.scene_view.showsStatistics = True else: self.scene_view = scn.View((0, 0, w, h), superView=self.main_view) self.scene_view.preferredFramesPerSecond = 30 self.scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleWidth self.scene_view.allowsCameraControl = False self.scene_view.scene = scn.Scene() self.root_node = self.scene_view.scene.rootNode self.scene_view.backgroundColor = (.77, .97, 1.0) self.scene_view.delegate = self self.physics_world = self.scene_view.scene.physicsWorld self.physics_world.speed = 2. self.physics_world.contactDelegate = self if DEBUG: marker1 = scn.Sphere(0.1) marker1.firstMaterial.emission.contents = (1.0, .14, .14) marker1.firstMaterial.diffuse.contents = (.0, .0, .0) Demo.marker_node1 = scn.Node.nodeWithGeometry(marker1) Demo.marker_node1.name = 'marker1' self.root_node.addChildNode(Demo.marker_node1) marker2 = scn.Sphere(0.1) marker2.firstMaterial.emission.contents = (.17, .0, 1.0) marker2.firstMaterial.diffuse.contents = (.0, .0, .0) Demo.marker_node2 = scn.Node.nodeWithGeometry(marker2) Demo.marker_node2.name = 'marker2' self.root_node.addChildNode(Demo.marker_node2) floor_geometry = scn.Floor() floor_geometry.reflectivity = 0.05 tile_image = ui.Image.named('plf:Ground_DirtCenter') tile_number = 5 tile_factor = scn.Matrix4(tile_number, 0.0, 0.0, 0.0, 0.0, tile_number, 0.0, 0.0, 0.0, 0.0, tile_number, 0.0, 0.0, 0.0, 0.0, 1.0) floor_geometry.firstMaterial.diffuse.contents = tile_image floor_geometry.firstMaterial.diffuse.intensity = 0.8 floor_geometry.firstMaterial.diffuse.contentsTransform = tile_factor floor_geometry.firstMaterial.diffuse.wrapS, floor_geometry.firstMaterial.diffuse.wrapT = scn.WrapMode.Repeat, scn.WrapMode.Repeat floor_geometry.firstMaterial.locksAmbientWithDiffuse = True self.floor_node = scn.Node.nodeWithGeometry(floor_geometry) self.floor_node.name = 'Floor' self.floor_node.physicsBody = scn.PhysicsBody.staticBody() self.root_node.addChildNode(self.floor_node) cars = [dict(name='red', position=(5, 0, 0), volume=1.), dict(name='yellow', too_far=25, body_color=(1.0, .78, .0), position=(-5, 0, -2), sound='game:Pulley', volume=0.1), dict(name='blue', too_far=30, body_color=(.0, .61, 1.0), position=(-12, 0, -6), sound='game:Woosh_1', volume=0.5), dict(name='green', too_far=18, body_color=(.0, .82, .28), position=(10, 0, -10), sound='casino:DiceThrow3', volume=0.8), dict(name='pink', too_far=20, body_color=(.91, .52, .62), position=(5, 0, 10), sound='casino:DieThrow3', volume=0.5)] self.cars =[Car(world=self, props=cars[i]) for i in range(min(MAXCARS, len(cars)))] self.free_flags = [] for i in range(2*len(self.cars)): node = scn.Node() self.free_flags.append(node) self.root_node.addChildNode(node) self.used_flags = {} self.crash = Sparks().particleSystem self.crash_sound = scn.AudioSource('game:Crashing') self.crash_action = scn.Action.playAudioSource(self.crash_sound, False) self.road_blocks_node = scn.Node() self.road_blocks = [] self.road_blocks.append(RoadBlock(w=1.6, l=25, name='block 0', position=(28, 6))) self.road_blocks.append(RoadBlock(w=20, l=1.6, name='block 1', position=(-2, -12))) self.road_blocks.append(RoadBlock(w=8, l=1.6, name='block 2', position=(-10, 6), rotation=-math.pi/6)) self.road_blocks.append(RoadBlock(w=40, l=1.6, name='block 3', position=(-40, 0), rotation=-math.pi/3)) self.road_blocks.append(RoadBlock(w=0.8, h=3, l=0.8, name='start', position=(0, 0))) for aBlock in self.road_blocks: self.road_blocks_node.addChildNode(aBlock.block_node) self.root_node.addChildNode(self.road_blocks_node) self.camera_node = scn.Node() self.camera_node.camera = scn.Camera() self.camera_node.camera.zFar = 150 carPos = self.cars[0].node.position self.camera_node.position = scn.Vector3(carPos.x+5, 10, carPos.z+30) self.root_node.addChildNode(self.camera_node) self.light_node = scn.Node() self.light_node.position = (50, 50, 50) self.light_node.lookAt(self.root_node.position) self.light = scn.Light() self.light.type = scn.LightTypeDirectional self.light.castsShadow = True self.light.shadowSampleCount = 16 self.light.color = (.95, 1.0, .98) self.light_node.light = self.light self.root_node.addChildNode(self.light_node) self.ambient_node = scn.Node() self.ambient = scn.Light() self.ambient.type = scn.LightTypeAmbient self.ambient.color = (.38, .42, .45, .1) self.ambient_node.light = self.ambient self.root_node.addChildNode(self.ambient_node) self.scene_view.pointOfView = self.camera_node if DEBUG: self.main_view.add_subview(Demo.status_label) self.close = False self.shut_down = False self.main_view.add_subview(self.close_button) self.main_view.present(hide_title_bar=~DEBUG)
def main(self): # actually you need only to preserve those properties that are needed after the main_view.present call, # in this case the self.morpher. All the other self. prefixes are not needed for the same functionality self.q=queue.PriorityQueue() self.q1=queue.PriorityQueue() self.Eprom=Eprom1() self.sv=scene.SceneView() self.sv.scene=MyScene() self.sv.anti_alias = False self.sv.frame_interval = 1 self.sv.multi_touch_enabled = True self.sv.shows_fps = True self.sv.bg_color=(1,1,1,1) v1width=650 self.view1=ui.View(frame=(256+768-v1width,0,v1width,v1width)) self.messagetext='' self.rbtn1=ui.SegmentedControl(frame=(5.0,340.0,204.0,34.0), segments=('auto','xyz','123','maze'), action= self.rbutton_tapped) self.switch1=ui.Switch(frame=(6.0,34.0,51.0,31.0),action=self.setPin) self.switch1.targetPin=2 self.switch2=ui.Switch(frame=(197,167,51.0,31.0),action=self.setPin) self.switch2.targetPin=21 self.sv.add_subview(self.view1) self.sv.add_subview(self.rbtn1) self.sv.add_subview(self.switch1) self.sv.add_subview(self.switch2) self.keypad1=keypadNode(scale=1.15,radius=150,position=(15,35),anchor_point=(0,0), keytitles=['inc','y','X','Z','dec','z','r/g','x','Y','../_'], on_output_change=self.keypad_output_changed) self.keypad2=keypadNode(scale=1.15,radius=150,position=(15,35),anchor_point=(0,0), keytitles=['1','2','3','4','5','6','7','8','9','0'], orientation=((0,-1),(1,0),), on_output_change=self.keypad_output_changed) self.keypad3=keypadNode(scale=1.15,radius=150,position=(15,35),anchor_point=(0,0), keytitles=['Put\n{NE}','N','E','[NW]\nU','Take\n{SW}','D\n{SE}','[Ctrl]','W','S','{Alt}'], on_output_change=self.keypad_output_changed) scene.LabelNode(position=(-30,-120), anchor_point=(0,0), text='Reset: [Ctrl Alt D]',font=('Helvetica',15),parent=self.keypad3,color='black') self.mode=0 self.key='' self.scene_view = scn.View((0,0,self.view1.frame[2],self.view1.frame[3]), superView=self.view1) self.scene_view.allowsCameraControl = True self.scene_view.scene = scn.Scene() self.scene_view.delegate = self self.root_node = self.scene_view.scene.rootNode self.camera_node = scn.Node() self.camera_node.camera = scn.Camera() self.camera_node.position = (-10,1.5,2) self.camera_node.camera.focalLength=70 self.root_node.addChildNode(self.camera_node) self.origin_node = scn.Node() self.root_node.addChildNode(self.origin_node) self.floor_node=scn.Node(geometry=scn.Floor()) self.floor_node.position=(0,-1.25,0) # self.root_node.addChildNode(self.floor_node) n=4 scale=0.1/n r=3 # self.off_led = scn.Sphere(radius=r*scale) self.off_led = scn.Capsule(capRadius=r*scale,height=3*r*scale) self.off_led.firstMaterial.contents=UIColor.lightGrayColor().CGColor() # off_led.firstMaterial().emission().setColor_(UIColor.greenColor().CGColor()) self.green_led = scn.Capsule(capRadius=r*scale*1.1,height=3*r*scale*1.05) self.green_led.firstMaterial.contents=(UIColor.grayColor().CGColor()) self.green_led.firstMaterial.emission.contents=(UIColor.greenColor().CGColor()) self.red_led = scn.Capsule(capRadius=r*scale*1.1,height=3*r*scale*1.05) self.red_led.firstMaterial.contents=UIColor.grayColor().CGColor() self.red_led.firstMaterial.emission.contents=(UIColor.redColor().CGColor()) self.led_nodes = [[[scn.Node.nodeWithGeometry(self.off_led) for k in range(n)]for j in range(n)]for i in range(n)] self.off_wire = scn.Capsule(capRadius=r*0.25*scale,height=20*(n+0.5)*scale) self.off_wire.firstMaterial.contents=UIColor.lightGrayColor().CGColor() self.pos_wire = scn.Capsule(capRadius=r*0.25*scale,height=20*(n+0.5)*scale) self.pos_wire.firstMaterial.contents=UIColor.lightGrayColor().CGColor() self.pos_wire.firstMaterial.emission.contents=(0.7,0,0)#UIColor.magentaColor().CGColor()) self.neg_wire = scn.Capsule(capRadius=r*0.25*scale,height=20*(n+0.5)*scale) self.neg_wire.firstMaterial.contents=UIColor.lightGrayColor().CGColor() self.neg_wire.firstMaterial.emission.contents=(0,0,0.75)#(UIColor.blueColor().CGColor()) self.wire_nodes=[[[scn.Node.nodeWithGeometry((self.off_wire,self.neg_wire,self.pos_wire)[0]) for j in range(n)]for i in range(n)]for k in range(3)] wireoffset=r*scale for i in range(n): for j in range(n): x=(i-(n-1)/2)*20*scale y=(j-(n-1)/2)*20*scale self.root_node.addChildNode(self.wire_nodes[0][i][j]) self.wire_nodes[0][i][j].setPosition((x+wireoffset,0,y)) self.root_node.addChildNode(self.wire_nodes[1][i][j]) self.wire_nodes[1][i][j].setPosition((x,y-wireoffset,0)) self.wire_nodes[1][i][j].eulerAngles=(math.pi/2,0,0) self.root_node.addChildNode(self.wire_nodes[2][i][j]) self.wire_nodes[2][i][j].setPosition((0,x,y-wireoffset)) self.wire_nodes[2][i][j].eulerAngles=(0,0,math.pi/2) for k in range(n): z=(k-(n-1)/2)*20*scale self.root_node.addChildNode(self.led_nodes[i][j][k]) self.led_nodes[i][j][k].setPosition((x,y,z)) self.led_nodes[i][j][k].eulerAngles=(0.61547970867039,0,math.pi/4) self.index=0 self.oldindex=0 constraint = scn.LookAtConstraint(self.root_node)#(self.sphere_nodes[2][2][2]) constraint.gimbalLockEnabled = True self.camera_node.constraints = constraint self.light_node = scn.Node() self.light_node.position = (100, 0, -10) self.light = scn.Light() self.light.type = scn.LightTypeDirectional self.light.castsShadow = False self.light.color = 'white' self.light_node.light = self.light self.root_node.addChildNode(self.light_node) self.action = scn.Action.repeatActionForever(scn.Action.rotateBy(0, math.pi*2, 0, 10)) self.origin_node.runAction(self.action) self.sv.present(orientations= ['landscape'])
def main(self): main_view = ui.View() w, h = ui.get_screen_size() main_view.frame = (0, 0, w, h) main_view.name = 'physics experiment demo - 1' scene_view = scn.View(main_view.frame, superView=main_view) scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin scene_view.allowsCameraControl = True scene_view.backgroundColor = 'white' scene_view.scene = scn.Scene() physics_world = scene_view.scene.physicsWorld root_node = scene_view.scene.rootNode floor_geometry = scn.Floor() floor_geometry.reflectivity = 0.1 floor_node = scn.Node.nodeWithGeometry(floor_geometry) root_node.addChildNode(floor_node) ball_radius = 0.2 ball_geometry = scn.Sphere(radius=ball_radius) ball_geometry.firstMaterial.diffuse.contents = (.48, .48, .48) ball_geometry.firstMaterial.specular.contents = (.88, .88, .88) ball_node = scn.Node.nodeWithGeometry(ball_geometry) rope_seg_nr = 45 rope_length = 5.0 rope_radius = 0.025 rope_seg_length = rope_length / rope_seg_nr rope_element_geometry = scn.Capsule(rope_radius, rope_seg_length) rope_element_geometry.firstMaterial.diffuse.content = (.77, .67, .09) rope_nodes = [] for i in range(rope_seg_nr): rope_nodes.append(scn.Node.nodeWithGeometry(rope_element_geometry)) if i > 0: rope_nodes[-1].position = (0., -rope_seg_length, 0.) rope_nodes[-2].addChildNode(rope_nodes[-1]) aConstraint = scn.DistanceConstraint.distanceConstraintWithTarget( rope_nodes[-2]) aConstraint.maximumDistance = rope_seg_length aConstraint.minimumDistance = rope_seg_length rope_nodes[-1].constraints = aConstraint else: rope_nodes[0].position = (0, rope_length + 1.0 - rope_seg_length / 2, 0) aConstraint = scn.DistanceConstraint.distanceConstraintWithTarget( root_node) aConstraint.maximumDistance = rope_length + 1.0 - rope_seg_length / 2 aConstraint.minimumDistance = rope_length + 1.0 - rope_seg_length / 2 rope_nodes[0].constraints = aConstraint ball_node.position = (0, -rope_seg_length / 2 - rope_radius - ball_radius, 0) aConstraint = scn.DistanceConstraint.distanceConstraintWithTarget( rope_nodes[-1]) aConstraint.maximumDistance = rope_seg_length / 2 + rope_radius + ball_radius aConstraint.minimumDistance = rope_seg_length / 2 + rope_radius + ball_radius ball_node.constraints = aConstraint rope_nodes[-1].addChildNode(ball_node) root_node.addChildNode(rope_nodes[0]) ball_physicsBody = scn.PhysicsBody.dynamicBody() ball_physicsBody.physicsShape = scn.PhysicsShape.shapeWithGeometry( ball_geometry) ball_physicsBody.mass = 5.0 ball_physicsBody.damping = 0.005 ball_physicsBody.angularDamping = 0.00 ball_node.physicsBody = ball_physicsBody rope_element_physicsShape = scn.PhysicsShape.shapeWithGeometry( rope_element_geometry) for i in range(rope_seg_nr): rope_nodes[i].physicsBody = scn.PhysicsBody.dynamicBody() rope_nodes[i].physicsBody.physicsShape = rope_element_physicsShape rope_nodes[i].physicsBody.mass = 1.1 rope_nodes[i].physicsBody.damping = 0.00 rope_nodes[i].physicsBody.angularDamping = 0.00 if i > 0: physics_world.addBehavior( scn.PhysicsBallSocketJoint.joint( rope_nodes[i - 1].physicsBody, (0, -rope_seg_length / 2, 0), rope_nodes[i].physicsBody, (0, rope_seg_length / 2, 0))) physics_world.addBehavior( scn.PhysicsBallSocketJoint.joint(rope_nodes[0].physicsBody, (0, rope_seg_length / 2, 0))) physics_world.addBehavior( scn.PhysicsBallSocketJoint.joint(rope_nodes[-1].physicsBody, (0, -rope_seg_length / 2, 0), ball_node.physicsBody, (0, ball_radius, 0))) ball_node.physicsBody.applyForce((45.0, 0.0, 0.0), True) camera_node = scn.Node() camera_node.camera = scn.Camera() camera_node.position = (0, rope_length + 1.0, rope_length) camera_node.lookAt((0, rope_length / 2 + 1.0, 0)) root_node.addChildNode(camera_node) light_node = scn.Node() light_node.position = (rope_length, rope_length, rope_length) light = scn.Light() light.type = scn.LightTypeDirectional light.castsShadow = True light.shadowSampleCount = 32 light.color = (.99, 1.0, .86) light_node.light = light light_node.lookAt((0, rope_length / 2 + 1.0, 0)) root_node.addChildNode(light_node) main_view.present(style='fullscreen', hide_title_bar=False)
def main(self): main_view = ui.View() w, h = ui.get_screen_size() main_view.frame = (0, 0, w, h) main_view.name = 'avoid occluder demo' scene_view = scn.View(main_view.frame, superView=main_view) scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleWidth scene_view.allowsCameraControl = True scene_view.delegate = self scene_view.backgroundColor = 'white' scene_view.rendersContinuously = True scene_view.scene = scn.Scene() root_node = scene_view.scene.rootNode floor_geometry = scn.Floor() floor_node = scn.Node.nodeWithGeometry(floor_geometry) root_node.addChildNode(floor_node) ball_radius = 0.2 ball_geometry = scn.Sphere(radius=ball_radius) ball_geometry.firstMaterial.diffuse.contents = (.48, .48, .48) ball_geometry.firstMaterial.specular.contents = (.88, .88, .88) self.ball_node_1 = scn.Node.nodeWithGeometry(ball_geometry) self.ball_node_2 = scn.Node.nodeWithGeometry(ball_geometry) root_node.addChildNode(self.ball_node_1) root_node.addChildNode(self.ball_node_2) occluder_geometry = scn.Box(0.3, 2., 15., 0.2) occluder_geometry.firstMaterial.diffuse.contents = (.91, .91, .91) occluder_node = scn.Node.nodeWithGeometry(occluder_geometry) occluder_node.position = (0., 0.8, 0.) root_node.addChildNode(occluder_node) self.orbit_r = 10 self.omega_speed_1 = math.pi / 1500 self.omega_speed_2 = 1.5 * self.omega_speed_1 self.ball_node_1.position = (self.orbit_r, 0.5, 0.) self.ball_node_2.position = (0., 0.5, self.orbit_r) constraint = scn.AvoidOccluderConstraint.avoidOccluderConstraintWithTarget( self.ball_node_1) self.ball_node_2.constraints = [constraint] camera_node = scn.Node() camera_node.camera = scn.Camera() camera_node.position = (0.5 * self.orbit_r, 0.5 * self.orbit_r, 1.5 * self.orbit_r) camera_node.lookAt(root_node.position) root_node.addChildNode(camera_node) light_node = scn.Node() light_node.position = (self.orbit_r, self.orbit_r, self.orbit_r) light = scn.Light() light.type = scn.LightTypeDirectional light.castsShadow = True light.shadowSampleCount = 32 light.color = (.99, 1.0, .86) light_node.light = light light_node.lookAt(root_node.position) root_node.addChildNode(light_node) main_view.present(style='fullscreen', hide_title_bar=False)