Exemple #1
0
 def __init__(self, w=1., h=1.8, l=2., name='block', position=(0., 0.), rotation=0.):
   self.block = scn.Box(w, h, l, 0.1)
   self.block.firstMaterial = RoadBlock.block_material
   self.block_node = scn.Node.nodeWithGeometry(self.block)
   self.block_node.name = name
   self.block_node.position = position if len(position) == 3 else (position[0], h/2-0.2, position[1])
   self.block_node.rotation = (0, 1, 0, rotation)
   self.block_node.physicsBody = scn.PhysicsBody.staticBody()
   self.block_node.physicsBody.contactTestBitMask = scn.PhysicsCollisionCategory.Default.value
Exemple #2
0
def demo():
    main_view = ui.View()
    w, h = ui.get_screen_size()
    main_view.frame = (0, 0, w, h)
    main_view.name = 'textDemo'

    scene_view = scn.View(main_view.frame, superView=main_view)
    scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin
    scene_view.antialiasingMode = scn.AntialiasingMode.Multisampling16X

    scene_view.allowsCameraControl = True

    scene_view.backgroundColor = 'white'

    scene_view.scene = scn.Scene()

    root_node = scene_view.scene.rootNode
    text_mesh = scn.Text.textWithString('Pythonista', 6.0)
    text_mesh.flatness = 0.2
    text_mesh.chamferRadius = 0.4
    text_mesh.font = ('HelveticaNeue-Bold', 18)
    bbox_min, bbox_max = text_mesh.boundingBox
    text_width = bbox_max.x - bbox_min.x
    text_node = scn.Node.nodeWithGeometry(text_mesh)
    text_node.castsShadow = True
    text_container = scn.Node.node()
    text_container.addChildNode(text_node)
    text_container.position = (0, 40, 0)
    text_node.position = (-text_width / 2, 0, 0)
    box = scn.Box(width=150, height=4, length=150, chamferRadius=1)
    box_node = scn.Node.nodeWithGeometry(box)
    root_node.addChildNode(box_node)
    rotate_action = scn.Action.repeatActionForever(
        scn.Action.rotateBy(0, math.pi * 2, math.pi * 2, 10))
    text_container.runAction(rotate_action)
    root_node.addChildNode(text_container)
    light_node = scn.Node.node()
    light_node.position = (0, 105, 5)
    light_node.rotation = (1, 0, 0, -math.pi / 2)
    light = scn.Light.light()
    light.type = 'spot'
    light.spotOuterAngle = 65
    light.castsShadow = True
    light.shadowSampleCount = 16
    light.color = 'cyan'
    light_node.light = light
    root_node.addChildNode(light_node)

    main_view.present(style='fullscreen', hide_title_bar=False)
Exemple #3
0
 def addTrainToSceneBlock(child):
   if child.geometry is not None and child.geometry != scn.nil:
     node = child.clone()
     node.position = scn.Vector3(node.position.x + pos.x, node.position.y + pos.y, node.position.z + pos.z)
     min, max = node.getBoundingBox()
             
     body = scn.PhysicsBody.dynamicBody()
     boxShape = scn.Box(width=(max.x - min.x), height=(max.y - min.y), length=(max.z - min.z), chamferRadius=0.0)
             
     body.physicsShape = scn.PhysicsShape(geometry=boxShape, options=None)
     
     trans = list(scn.Matrix4Identity)
     trans[13] = -min.y
     node.pivot = trans
     node.physicsBody = body
     scene.rootNode.addChildNode(node)
     return False
Exemple #4
0
 def addWoodenBlockToScene(self, scene, imageName=None, position=None):
   #create a new node
   block = scn.Node()
       
   #place it
   block.position = position
       
   #attach a box of 5x5x5
   block.geometry = scn.Box(width=5, height=5, length=5, chamferRadius=0)
       
   #use the specified images named as the texture
   block.geometry.firstMaterial.diffuse.contents = imageName
       
   #turn on mipmapping
   block.geometry.firstMaterial.diffuse.mipFilter = scn.FilterMode.Linear
       
   #make it physically based
   block.physicsBody = scn.PhysicsBody.dynamicBody()
       
   #add to the scene
   scene.rootNode.addChildNode(block)
def setup_center_view(piece_set):
    center_view = scn.View(None, superView=data.main_view)
    center_view.preferredFramesPerSecond = 30
    center_view.rendersContinuously = False
    center_view.autoenablesDefaultLighting = True
    center_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleWidth
    center_view.allowsCameraControl = True
    center_view.jitteringEnabled = True

    center_view.backgroundColor = 'beige'

    box_size = 3.0
    box = scn.Box(box_size, box_size, box_size, 0.0)
    box.firstMaterial.diffuse.contents = (.86, .86, .86)
    box.firstMaterial.transparency = 0.15
    box.firstMaterial.cullMode = scn.CullMode.Front
    box_node = scn.Node.nodeWithGeometry(box)
    box_node.name = 'box'
    box_node.categoryBitMask = 2

    tray_handle = setup_tray(piece_set)
    axis_handle = setup_axes()
    tray_handle.addChildNode(axis_handle)
    box_node.addChildNode(tray_handle)

    scene = scn.Scene()
    scene.rootNode.addChildNode(box_node)

    camera_node = scn.Node()
    camera_node.name = 'center_camera'
    camera_node.position = (8., 10., 12)
    camera_node.lookAt((1., 3., -1.5))
    camera_node.camera = scn.Camera()
    camera_node.camera.fieldOfView = 50
    scene.rootNode.addChildNode(camera_node)

    center_view.scene = scene
    return center_view
def main():
    main_view = ui.View()
    w, h = ui.get_screen_size()
    main_view.frame = (0, 0, w, h)
    main_view.name = 'Photos cube'

    scene_view = scn.View(main_view.frame, superView=main_view)
    scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin
    scene_view.antialiasingMode = scn.AntialiasingMode.Multisampling16X

    scene_view.allowsCameraControl = True

    scene_view.backgroundColor = 'white'

    scene_view.delegate = RendererDelegateForMyScene()

    scene = scn.Scene()
    scene_view.scene = scene

    scn.Transaction.disableActions = True

    scene.background.contents = tile_image
    scene.background.contentsTransform = tile_factor
    scene.background.wrapS, scene.background.wrapT = scn.WrapMode.Repeat, scn.WrapMode.Repeat

    root_node = scene.rootNode

    cube_geometry = scn.Box(width=1, height=1, length=1, chamferRadius=0.05)
    cube_geometry_inside = scn.Box(width=1 - 0.001,
                                   height=1 - 0.001,
                                   length=1 - 0.001,
                                   chamferRadius=0.04)

    material_inside = scn.Material()
    material_inside.diffuse.contents = inside_image

    cube_geometry_inside.materials = [material_inside for i in range(6)]

    cube_geometry_materials = [scn.Material() for i in range(6)]
    for i in range(6):
        cube_geometry_materials[i].diffuse.contents = cube_images[
            random.randrange(len(cube_image_names))]
    cube_geometry.materials = cube_geometry_materials

    cube_node = scn.Node.nodeWithGeometry(cube_geometry)
    cube_node.name = 'cube node'
    cube_node_inside = scn.Node.nodeWithGeometry(cube_geometry_inside)
    cube_node.addChildNode(cube_node_inside)

    cube_node.position = (0., 0., -5.)

    cube_action = scn.Action.scaleTo(1.5, 3.0)
    cube_reversed_action = scn.Action.scaleTo(1.0, 1.0)
    cube_wait_action = scn.Action.waitForDuration(0.8)
    cube_y_rot_action1 = scn.Action.rotateBy(0, 0.2, 0, 0.3)
    cube_y_rot_action2 = scn.Action.rotateBy(0, -0.4, 0, 0.3)
    cube_y_rot_action3 = scn.Action.rotateBy(0, 0.2, 0, 0.3)

    cube_sequence = scn.Action.sequence([
        cube_action, cube_reversed_action, cube_wait_action,
        cube_y_rot_action1, cube_y_rot_action2, cube_y_rot_action3,
        cube_wait_action
    ])
    cube_forever = scn.Action.repeatActionForever(cube_sequence)

    cube_node.runAction(cube_forever)

    constraint = scn.LookAtConstraint.lookAtConstraintWithTarget(cube_node)
    constraint.gimbalLockEnabled = True

    camera = scn.Camera()
    camera_node = scn.Node()
    camera_node.name = 'camera!'
    camera_node.camera = camera
    camera_node.position = (-3.0, 3.0, 3.0)
    camera_node.constraints = [constraint]

    lights_node = scn.Node()

    ambient_light = scn.Light()
    ambient_light.type = scn.LightTypeAmbient
    ambient_light.name = 'ambient light'
    ambient_light.color = (.99, 1.0, .86)
    ambient_node = scn.Node()
    ambient_node.light = ambient_light
    lights_node.addChildNode(ambient_node)

    directional_light = scn.Light()
    directional_light.type = scn.LightTypeDirectional
    directional_light.name = 'directional light'
    directional_light.color = 'white'
    directional_node = scn.Node()
    directional_node.light = directional_light
    directional_node.position = camera_node.position
    directional_node.constraints = [constraint]

    lights_node.addChildNode(directional_node)

    root_node.addChildNode(camera_node)
    root_node.addChildNode(lights_node)
    root_node.addChildNode(cube_node)
    scene_view.pointOfView = camera_node

    scn.Transaction.begin()
    scn.Transaction.disableActions = False
    scn.Transaction.animationDuration = 10.0
    directional_light.color = 'yellow'
    scn.Transaction.commit()

    scene_view.playing = True

    main_view.present(style='fullscreen', hide_title_bar=False)
Exemple #7
0
  def buildCar(self, body_color=None, sound_file=None, sound_volume=1.0):
    self.chassis_node = scn.Node()
    self.chassis_node.categoryBitMask = 1 << 1
    
    self.camera_controller_node = scn.Node()

    self.camera_node = scn.Node()
    self.camera_node.position = (0, 1.6, 2.05)
    self.camera_node.lookAt((0, 0.9, 10))
    self.camera = scn.Camera()    
    self.camera.zNear = 0.25
    self.camera.zFar = 10
    self.camera.fieldOfView = 35
    self.camera_node.camera = self.camera
    
    self.camera_controller_node.addChildNode(self.camera_node)
    self.chassis_node.addChildNode(self.camera_controller_node)

    self.radar_p1L = scn.Vector3(1.2, 1.3, 2.05)
    self.radar_p2L = scn.Vector3(4.5, 0.8, 20)
    self.radar_pSL = scn.Vector3(10., 0.8, 2.4)
    self.radar_p1R = scn.Vector3(-1.2, 1.3, 2.05)
    self.radar_p2R = scn.Vector3(-4.5, 0.8, 20)
    self.radar_pSR = scn.Vector3(-10., 0.8, 2.4)
    
    self.body_material = scn.Material()
    self.body_material.diffuse.contents = body_color
    self.body_material.specular.contents = (.88, .88, .88)
    
    self.body = scn.Box(2, 1, 4, 0.2)
    self.body.firstMaterial = self.body_material

    self.body_node = scn.Node.nodeWithGeometry(self.body)
    self.body_node.position = (0, 0.75, 0)
    self.chassis_node.addChildNode(self.body_node)
    
    self.physicsBody = scn.PhysicsBody.dynamicBody()
    self.physicsBody.allowsResting = False
    self.physicsBody.mass = 1200
    self.physicsBody.restitution = 0.1
    self.physicsBody.damping = 0.3
    self.chassis_node.physicsBody = self.physicsBody
    
    self.top = scn.Box(1.6, 0.6, 1.8, 0.1)
    self.top.firstMaterial = self.body_material
    self.top_node = scn.Node.nodeWithGeometry(self.top)
    self.top_node.position = (0, 0.5+0.2, 0)
    self.body_node.addChildNode(self.top_node)
    
    self.door1 = scn.Box(2.02, 1-0.2, 1.8/2.2, 0.08)
    self.door1.firstMaterial = self.body_material
    self.door1_node = scn.Node.nodeWithGeometry(self.door1)
    self.door1_node.position = (0, 0.1, 1.8/4)
    self.body_node.addChildNode(self.door1_node)
    
    self.door2_node = scn.Node.nodeWithGeometry(self.door1)
    self.door2_node.position = (0, 0.1, -1.8/4+0.1)
    self.body_node.addChildNode(self.door2_node)
    
    self.window_material = scn.Material()
    self.window_material.diffuse.contents = (.64, .71, .75, 0.6)
    self.window_material.specular.contents = (.88, .88, .88, 0.8)
    
    self.sideW1 = scn.Box(1.61, 0.6-0.1, 1.8/2.2, 0.08)
    self.sideW1.firstMaterial = self.window_material
    self.sideW1_node = scn.Node.nodeWithGeometry(self.sideW1)
    self.sideW1_node.position = (0, 0.5+0.2, 1.8/4)
    self.body_node.addChildNode(self.sideW1_node)
    
    self.sideW2_node = scn.Node.nodeWithGeometry(self.sideW1)
    self.sideW2_node.position = (0, 0.5+0.2, -1.8/4+0.1)
    self.body_node.addChildNode(self.sideW2_node)
    
    self.window_materials = [scn.Material() for i in range(6)]
    self.window_materials[0] = self.window_material
    self.window_materials[2] = self.window_material
    for i in [1, 3, 4, 5]:
      self.window_materials[i] = self.body_material
    
    alpha = math.pi/5
    self.frontW = scn.Box(1.4, 0.6/math.cos(alpha), 0.1, 0.06)
    self.frontW.materials = self.window_materials
    self.frontW_node = scn.Node.nodeWithGeometry(self.frontW)
    self.frontW_node.position = (0, 0.5+0.2-0.05, 1.8/2+math.tan(alpha)*0.6/2-0.1)
    self.frontW_node.rotation = (1, 0, 0, -alpha)
    self.body_node.addChildNode(self.frontW_node)
    
    alpha = math.pi/5
    self.frontW2 = scn.Box(1.3, 0.6/math.cos(alpha), 0.3, 0.0)
    self.frontW2.firstMaterial = self.window_material
    self.frontW2_node = scn.Node.nodeWithGeometry(self.frontW2)
    self.frontW2_node.position = (0, 0.5+0.2-0.05-0.2, 1.8/2+math.tan(alpha)*0.6/2-0.08)
    self.frontW2_node.rotation = (1, 0, 0, -alpha)
    self.body_node.addChildNode(self.frontW2_node)
    
    alpha = math.pi/3.2
    self.rearW = scn.Box(1.4, 0.6/math.cos(alpha), 0.2, 0.2)
    self.rearW.materials = self.window_materials
    self.rearW_node = scn.Node.nodeWithGeometry(self.rearW)
    self.rearW_node.position = (0, 0.5+0.2-0.0417, -1.8/2-math.tan(alpha)*0.6/2+0.15)
    self.rearW_node.rotation = (1, 0, 0, alpha)
    self.body_node.addChildNode(self.rearW_node)
    
    alpha = math.pi/3.2
    self.rearW2 = scn.Box(1.3, 0.6/math.cos(alpha), 0.3, 0.05)
    self.rearW2.firstMaterial = self.window_material
    self.rearW2_node = scn.Node.nodeWithGeometry(self.rearW2)
    self.rearW2_node.position = (0, 0.5+0.2-0.05-0.2, -1.8/2-math.tan(alpha)*0.6/2+0.1)
    self.rearW2_node.rotation = (1, 0, 0, alpha)
    self.body_node.addChildNode(self.rearW2_node)
    
    self.nose = scn.Pyramid(2-0.4, 0.15, 1-0.2)
    self.nose.firstMaterial = self.body_material
    self.nose_node = scn.Node.nodeWithGeometry(self.nose)
    self.nose_node.position = (0, 0.75, 2-0.03)
    self.nose_node.rotation = (1, 0, 0, math.pi/2)
    self.chassis_node.addChildNode(self.nose_node)
    
    self.lampBack_colors = [(.6, .0, .0), (1.0, .0, .0)]
    
    self.front_spot = scn.Light()
    self.front_spot.type = scn.LightTypeSpot
    self.front_spot.castsShadow = False
    self.front_spot.color = (1.0, 1.0, .95)
    self.front_spot.spotInnerAngle = 20
    self.front_spot.spotOuterAngle = 25
    self.front_spot.attenuationEndDistance = 15
    
    self.exhaust = scn.Tube(0.05, 0.07, 0.08)
    self.exhaust.firstMaterial.metalness.contents = (.5, .5, .5)
    self.exhaust_node = scn.Node.nodeWithGeometry(self.exhaust)
    self.exhaust_node.position = (0.5, -0.42, -2.04)
    self.exhaust_node.rotation = (1, 0, 0, math.pi/2)
    self.body_node.addChildNode(self.exhaust_node)
    
    self.smoke = scn.ParticleSystem()
    self.smoke.emitterShape = scn.Sphere(0.01)
    self.smoke.birthLocation = scn.ParticleBirthLocation.SCNParticleBirthLocationSurface
    self.smoke.birthRate =6000
    self.smoke.loops = True
    self.smoke.emissionDuration = 0.08
    self.smoke.idleDuration = 0.4
    self.smoke.idleDurationVariation = 0.2
    self.smoke.particleLifeSpan = 0.3
    self.smoke.particleLifeSpanVariation = 1.2
    self.smoke.particleColor = (1., 1., 1., 1.)
    self.smoke.particleColorVariation = (.6, .0, .6, 0.)
    self.smoke.blendMode = scn.ParticleBlendMode.Multiply
    self.smoke.birthDirection = scn.ParticleBirthDirection.Random
    self.smoke.particleVelocity = 2.
    self.smoke.particleVelocityVariation = 3.5
    self.smoke.acceleration = (0., 15, 0.)
    self.sizeAnim = scn.CoreBasicAnimation()
    self.sizeAnim.fromValue = 0.1
    self.sizeAnim.toValue = 0.0
    self.size_con = scn.ParticlePropertyController.controllerWithAnimation(self.sizeAnim)    
    self.smoke.propertyControllers = {scn.SCNParticlePropertySize:self.size_con}

    self.smoker_node = scn.Node()
    self.smoker_node.position = (0., -0.15, 0.)
    self.smoker_node.addParticleSystem(self.smoke)
    self.exhaust_node.addChildNode(self.smoker_node)
    
    self.lamp = scn.Tube(0.12, 0.15, 4.07)
    self.lamp.firstMaterial.metalness.contents = (.93, .93, .93)
    self.lampGlasFront = scn.Sphere(0.13)
    self.lampGlasFront.firstMaterial.emission.contents = (.92, .93, .66)
    self.lampGlasBack = scn.Sphere(0.13)
    self.lampGlasBack.firstMaterial.diffuse.contents = 'black'
    self.lampGlasBack.firstMaterial.emission.contents = self.lampBack_colors[0]
    
    self.lamp_nodeR = scn.Node.nodeWithGeometry(self.lamp)
    self.lamp_nodeR.position = (-0.6, 0.75, 0.015)
    self.lamp_nodeR.rotation = (1, 0, 0, math.pi/2)
    self.chassis_node.addChildNode(self.lamp_nodeR)
    self.lamp_nodeL = scn.Node.nodeWithGeometry(self.lamp)
    self.lamp_nodeL.position = (0.6, 0.75, 0.015)
    self.lamp_nodeL.rotation = (1, 0, 0, math.pi/2)
    self.chassis_node.addChildNode(self.lamp_nodeL)
    
    self.lampGlasFront_nodeR = scn.Node.nodeWithGeometry(self.lampGlasFront)
    self.lampGlasFront_nodeR.position = (0, 1.95, 0)
    self.lampGlasFront_nodeR.lookAt((0, 45, 10))
    self.lampGlasFront_nodeR.light = self.front_spot
    self.lamp_nodeR.addChildNode(self.lampGlasFront_nodeR)    
    self.lampGlasBack_nodeR = scn.Node.nodeWithGeometry(self.lampGlasBack)
    self.lampGlasBack_nodeR.position = (0, -1.95, 0)
    self.lamp_nodeR.addChildNode(self.lampGlasBack_nodeR)
    
    self.lampGlasFront_nodeL = scn.Node.nodeWithGeometry(self.lampGlasFront)
    self.lampGlasFront_nodeL.position = (0, 1.95, 0)
    self.lampGlasFront_nodeL.lookAt((0, 45, 10))
    self.lampGlasFront_nodeL.light = self.front_spot
    self.lamp_nodeL.addChildNode(self.lampGlasFront_nodeL)    
    self.lampGlasBack_nodeL = scn.Node.nodeWithGeometry(self.lampGlasBack)
    self.lampGlasBack_nodeL.position = (0, -1.95, 0)
    self.lamp_nodeL.addChildNode(self.lampGlasBack_nodeL)
    
    self.wheel_nodes = [scn.Node()]
    self.tire = scn.Tube(0.12, 0.35, 0.25)
    self.tire.firstMaterial.diffuse.contents = 'black'
    self.wheel_nodes[0].position = (0.94, 0.4, 2-0.6)
    self.tire_node = scn.Node.nodeWithGeometry(self.tire)
    self.tire_node.rotation = (0, 0, 1, math.pi/2)
    self.wheel_nodes[0].addChildNode(self.tire_node)
    
    self.trace = scn.ParticleSystem()
    self.trace.birthRate = 750
    self.trace.loops = True
    self.trace.emissionDuration = 0.1
    self.trace.particleLifeSpan = 4.6
    self.trace.particleLifeSpanVariation = 5
    self.trace.particleSize = 0.02
    self.trace.particleColor = (.1, .1, .1, 1.)
    self.trace.particleColorVariation = (0.1, 0.1, 0.1, 0.1)
    self.trace.blendMode = scn.ParticleBlendMode.Replace
    self.trace.emitterShape = scn.Cylinder(0.02, 0.26)
    self.trace.birthLocation = scn.ParticleBirthLocation.SCNParticleBirthLocationVolume
    self.trace.handle(scn.ParticleEvent.Birth, [scn.ParticlePropertyPosition], self.traceParticleEventBlock)

    self.tire_node.addParticleSystem(self.trace)

    self.rim = scn.Cylinder(0.14, 0.1)
    self.rim.firstMaterial.diffuse.contents = 'gray' 
    self.rim.firstMaterial.specular.contents = (.88, .88, .88)
    self.rim_node = scn.Node.nodeWithGeometry(self.rim)
    self.rim_node.name = 'rim'
    self.rim_node.position = (0, 0.06, 0)
    self.tire_node.addChildNode(self.rim_node)
    self.rim_deco = scn.Text('Y', 0.05)
    self.rim_deco.font = ('Arial Rounded MT Bold', 0.3)
    self.rim_deco.firstMaterial.diffuse.contents = 'black' 
    self.rim_deco.firstMaterial.specular.contents = (.88, .88, .88)
    self.rim_deco_node = scn.Node.nodeWithGeometry(self.rim_deco)
    self.rim_deco_node.name = 'deco'
    self.rim_deco_node.position = (-0.1, 0.03, -1.12)
    self.rim_deco_node.rotation = (1, 0, 0, math.pi/2)
    self.rim_node.addChildNode(self.rim_deco_node)
    
    self.wheel_nodes.append(self.wheel_nodes[0].clone())
    self.wheel_nodes[1].position = (-0.94, 0.4, 2-0.6)
    self.wheel_nodes[1].childNodeWithName('rim', True).position = (0, -0.06, 0)
    self.wheel_nodes[1].childNodeWithName('deco', True).position = (-0.1, -0.03, -1.12)
    self.wheel_nodes[1].childNodeWithName('rim', True).rotation = (0, 1, 0, -math.pi/7)
    
    self.wheel_nodes.append(self.wheel_nodes[0].clone())
    self.wheel_nodes[2].position = (0.94, 0.4, -2+0.7)
    self.wheel_nodes[2].childNodeWithName('rim', True).rotation = (0, 1, 0, math.pi/7)
    
    self.wheel_nodes.append(self.wheel_nodes[0].clone())
    self.wheel_nodes[3].position = (-0.94, 0.4, -2+0.7)
    self.wheel_nodes[3].childNodeWithName('rim', True).position = (0, -0.06, 0)
    self.wheel_nodes[3].childNodeWithName('deco', True).position = (-0.1, -0.03, -1.12)
    self.wheel_nodes[3].childNodeWithName('rim', True).rotation = (0, 1, 0, math.pi/3)
    
    for aNode in self.wheel_nodes: self.chassis_node.addChildNode(aNode)
    
    self.wheels = [scn.PhysicsVehicleWheel(node=aNode) for aNode in self.wheel_nodes]    
    for i in [0, 1]: self.wheels[i].suspensionRestLength = 1.3     
    for i in [2, 3]: self.wheels[i].suspensionRestLength = 1.4
    for aWheel in self.wheels: aWheel.maximumSuspensionTravel = 150
    
    self.chassis_node.physicsBody.contactTestBitMask = scn.PhysicsCollisionCategory.Default.value
    self.chassis_node.physicsBody.continuousCollisionDetectionThreshold = 2.
    self.vehicle = scn.PhysicsVehicle(chassisBody=self.chassis_node.physicsBody, wheels=self.wheels)
    self.physics_world.addBehavior(self.vehicle)
    self.world.root_node.addChildNode(self.chassis_node)

    if ENGINESOUND:    
      self.sound = scn.AudioSource(sound_file)
      self.sound.load()
      self.sound.loops = True
      self.sound.volume = sound_volume
      self.sound_player = scn.AudioPlayer.audioPlayerWithSource(self.sound)
      self.chassis_node.addAudioPlayer(self.sound_player)
def setup_elementary_cube_geometry():
    geometry = scn.Box(1., 1., 1., 0.)
    material = scn.Material()
    material.diffuse.contents = 'resources/wood.png'
    geometry.firstMaterial = material
    return geometry
Exemple #9
0
def demo():
  main_view = ui.View()
  main_view.frame = (0,0,w,h)
  main_view.name = 'shapeDemo'
  main_view.background_color = 'white'
  
  image_view = ui.ImageView()
  image_view.frame = image_frame
  image_view.background_color = 'white'
  image_view.content_mode = ui.CONTENT_CENTER
  image_view.image = img
  
  main_view.add_subview(image_view)
  
  scene_view = scn.View(scene_frame, superView=main_view)
  scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin  
  scene_view.antialiasingMode = scn.AntialiasingMode.Multisampling16X
  scene_view.allowsCameraControl = True 
  scene_view.backgroundColor = 'white' 
  scene_view.scene = scn.Scene()

  root_node = scene_view.scene.rootNode

  shape = scn.Shape.shapeWithPath(path, 40)
  shape.chamferRadius = 8
  shape.firstMaterial.contents = 'yellow'
  
  shape_node = scn.Node.nodeWithGeometry(shape)
  shape_node.castsShadow = True
  bbox_min, bbox_max = shape.boundingBox
  shape_width = bbox_max.x - bbox_min.x
  shape_height = bbox_max.y - bbox_min.y
  shape_tr = list(scn.Matrix4Identity)
  shape_tr[13] = bbox_min.y+0.5*shape_height
  shape_node.pivot = shape_tr
  shape_node.position = (-bbox_min.x-0.5*shape_width, 0, 0)

  shape_container = scn.Node()
  shape_container.addChildNode(shape_node)

  scale = image_size.width/shape_width
  shape_container.scale = (scale, scale, 1)
  shape_width *= scale
  shape_height *= scale
  
  shape_container.position = (0, 0.2*shape_height, 0.5*shape_height)
  shape_container.rotation = (0, 1, 0, pi) # compensates for the differen coordinate systems of drawing vs. scene
  
  rotate_action = scn.Action.rotateBy(0, 0, 2*pi, 10)
  rotate_action.timingMode = scn.ActionTimingMode.EaseInEaseOut
  invers_action = rotate_action.reversedAction()  
  combined_action = scn.Action.sequence([rotate_action, invers_action])  
  shape_container.runAction(scn.Action.repeatActionForever(combined_action))
  
  box = scn.Box(width=2*shape_width, height=4, length=1.6*shape_width, chamferRadius=1)
  box.firstMaterial.contents = (.76, .91, 1.0)
  
  boxBoxMin, boxBoxMax = box.boundingBox
  box.boundingBox = ((box_bubble*boxBoxMin.x, boxBoxMin.y, boxBoxMin.z), (box_bubble*boxBoxMax.x, boxBoxMax.y, boxBoxMax.z))
  box_node = scn.Node.nodeWithGeometry(box)
  box_node.rotation = (1, 0, 0, pi/4)

  root_node.addChildNode(box_node)
  root_node.addChildNode(shape_container)
  
  constraint = scn.LookAtConstraint.lookAtConstraintWithTarget(shape_container)
  constraint.gimbalLockEnabled = True
  
  light_node = scn.Node()
  light_z = boxBoxMax.z + 0.0*boxBoxMax.z
  light_y = (shape_container.position.y+0.5*shape_height/2)/shape_container.position.z*light_z
  light_node.position = (20, light_y, light_z)
  light_node.constraints = [constraint]
  light = scn.Light()
  light.type = 'spot'
  light.spotOuterAngle = 90
  light.castsShadow = True
  light.zFar = 1500
  light.shadowSampleCount = 16
  light.color = 'white'
  light_node.light = light
  root_node.addChildNode(light_node)
  
  ambient_light = scn.Light() 
  ambient_light.type = scn.LightTypeAmbient
  ambient_light.color = (.41, .32, .0)
  ambient_node = scn.Node()
  ambient_node.light = ambient_light
  root_node.addChildNode(ambient_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 = 'physics experiment demo - 2'
  
    scene_view = scn.View(main_view.frame, superView=main_view)
    scene_view.autoresizingMask = scn.ViewAutoresizing.FlexibleHeight | scn.ViewAutoresizing.FlexibleRightMargin
    scene_view.allowsCameraControl = True
    
    self.counter_scene = Counter()
    counter_view = SceneView()
    counter_view.frame = (0., 0., 100., 50.)
    counter_view.scene = self.counter_scene    
    main_view.add_subview(counter_view)
    
#    scene_view.debugOptions = scn.DebugOption.ShowBoundingBoxes | scn.DebugOption.ShowCameras | scn.DebugOption.ShowLightInfluences
    
#    scene_view.debugOptions = scn.DebugOption.ShowPhysicsShapes
    
    scene_view.backgroundColor = 'white'
    scene_view.scene = scn.Scene()
    
    physics_world = scene_view.scene.physicsWorld
    physics_world.contactDelegate = self
    
    root_node = scene_view.scene.rootNode
    
    box_size = 10
    box = scn.Box(box_size, box_size, box_size, 0.0)
    box.firstMaterial.diffuse.contents = (.86, .86, .86)
    box.firstMaterial.transparency = 0.15
    box.firstMaterial.cullMode = scn.CullMode.Front
    box_node = scn.Node.nodeWithGeometry(box)
    root_node.addChildNode(box_node)
    
    self.particle_number = 25
    particle_max_r = box_size/20
    particle_colors = ['black', 'blue', 'green', 'pink', 'yellow', 'red', 'cyan', 'gray', 'magenta', 'brown', 'crimson', 'gold', 'indigo', 'olive']
    particles_node = scn.Node()
    particles_node.physicsField = scn.PhysicsField.electricField()
    particles_node.physicsField.strength = 5.0

    root_node.addChildNode(particles_node)
    for i in range(self.particle_number):
      r = random.uniform(0.35*particle_max_r, particle_max_r)
      particle = scn.Sphere(r)
      particle.firstMaterial.diffuse.contents = random.choice(particle_colors)
      particle.firstMaterial.specular.contents = (.86, .94, 1.0, 0.8)
      particle_node = scn.Node.nodeWithGeometry(particle)
      particle_node.position = (random.uniform(-(box_size/2-particle_max_r)*0.95, (box_size/2-particle_max_r)*0.95), random.uniform(-(box_size/2-particle_max_r)*0.95, (box_size/2-particle_max_r)*0.95), random.uniform(-(box_size/2-particle_max_r)*0.95, (box_size/2-particle_max_r)*0.95))
      particle_node.physicsBody = scn.PhysicsBody.dynamicBody()
      particle_node.physicsBody.mass = 1.2*r
      particle_node.physicsBody.charge = random.uniform(-10*r, +0*r)
      particle_node.physicsBody.restitution = 1.0
      particle_node.physicsBody.damping = 0.0
      particle_node.physicsBody.angularDamping = 0.0
      particle_node.physicsBody.continuousCollisionDetectionThreshold = 1*r
      particle_node.physicsBody.affectedByGravity = False
      particle_node.physicsBody.contactTestBitMask = scn.PhysicsCollisionCategory.Default.value
      particle_node.physicsBody.velocity = (random.uniform(-box_size, box_size), random.uniform(-box_size, box_size), random.uniform(-box_size, box_size))
      particles_node.addChildNode(particle_node)
    
    box_nodes = scn.Node()
    d = box_size/2
    for pos in [(d,0,0), (-d,0,0), (0,d,0), (0,-d,0), (0,0,d), (0,0,-d)]:
      aNode = scn.Node()
      aNode.position = pos
      aNode.lookAt((0,0,0))
      aNode.physicsBody = scn.PhysicsBody.staticBody()
      aNode.physicsBody.physicsShape = scn.PhysicsShape.shapeWithGeometry(scn.Plane(box_size, box_size))
      box_nodes.addChildNode(aNode)
    root_node.addChildNode(box_nodes)
    
    constraint = scn.LookAtConstraint.lookAtConstraintWithTarget(root_node)
    constraint.gimbalLockEnabled = True
    
    camera_node = scn.Node()
    camera_node.camera = scn.Camera()
    camera_node.position = (-2.5*box_size, 1.5*box_size, 2*box_size)
    camera_node.constraints = constraint
    root_node.addChildNode(camera_node)
    
    light_node = scn.Node()
    light_node.position = (box_size, box_size, box_size)
    light = scn.Light()
    light.type = scn.LightTypeDirectional
    light.castsShadow = True
    light.shadowSampleCount = 32
    light.color = (.95, 1.0, .98)
    light_node.light = light
    light_node.constraints = constraint
    root_node.addChildNode(light_node)
    
    main_view.present(style='fullscreen', hide_title_bar=False)
Exemple #11
0
  def setupSceneElements(self, scene):
    #add walls
    wall = scn.Node(geometry=scn.Box(width=400, height=100, length=4, chamferRadius=0))
    wall.geometry.firstMaterial.diffuse.contents = "resources/wall.jpg"
    scale = list(scn.Matrix4Identity)
    scale[0], scale[5], scale[13], scale[10] = 24., 2., 1., 1.
    
    wall.geometry.firstMaterial.diffuse.contentsTransform = scale
    wall.geometry.firstMaterial.diffuse.wrapS = scn.WrapMode.Repeat
    wall.geometry.firstMaterial.diffuse.wrapT = scn.WrapMode.Mirror
    wall.geometry.firstMaterial.doubleSided = False
    wall.castsShadow = False
    wall.geometry.firstMaterial.locksAmbientWithDiffuse = False
        
    wall.position = scn.Vector3(0, 50, -92)
    wall.physicsBody = scn.PhysicsBody.staticBody()
    scene.rootNode.addChildNode(wall)

    wall = wall.clone()
    wall.position = scn.Vector3(-202, 50, 0)
    wall.rotation = scn.Vector4(0, 1, 0, M_PI_2)
    scene.rootNode.addChildNode(wall)
        
    wall = wall.clone()
    wall.position = scn.Vector3(202, 50, 0)
    wall.rotation = scn.Vector4(0, 1, 0, -M_PI_2)
    scene.rootNode.addChildNode(wall)
        
    backWall = scn.Node(geometry=scn.Plane(width=400, height=100))
    backWall.geometry.firstMaterial = wall.geometry.firstMaterial
    backWall.position = scn.Vector3(0, 50, 200)
    backWall.rotation = scn.Vector4(0, 1, 0, M_PI)
    backWall.castsShadow = False
    backWall.physicsBody = scn.PhysicsBody.staticBody()
    scene.rootNode.addChildNode(backWall)
        
    #add ceil
    ceilNode = scn.Node(geometry=scn.Plane(width=400, height=400))
    ceilNode.position = scn.Vector3(0, 100, 0)
    ceilNode.rotation = scn.Vector4(1, 0, 0, M_PI_2)
    ceilNode.geometry.firstMaterial.doubleSided = False
    ceilNode.castsShadow = False
    ceilNode.geometry.firstMaterial.locksAmbientWithDiffuse = False
    scene.rootNode.addChildNode(ceilNode)
   
    #add a train
    self.addTrainToScene(scene, pos=scn.Vector3(-5, 20, -40))
        
    #add wooden blocks
    self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeA.jpg", position=scn.Vector3(-10, 15, 10))
    self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeB.jpg", position=scn.Vector3(-9, 10, 10))
    self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeC.jpg", position=scn.Vector3(20, 15, -11))
    self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeA.jpg", position=scn.Vector3(25, 5, -20))

    #add more block
    for _ in range(4):
      self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeA.jpg", position=scn.Vector3(random.randint(-30, 30), 20, random.randint(-20, 20)))
      self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeB.jpg", position=scn.Vector3(random.randint(-30, 30), 20, random.randint(-20, 20)))
      self.addWoodenBlockToScene(scene, imageName="resources/WoodCubeC.jpg", position=scn.Vector3(random.randint(-30, 30), 20, random.randint(-20, 20)))
        
    #add cartoon book
    block = scn.Node()
    block.position = scn.Vector3(20, 10, -16)
    block.rotation = scn.Vector4(0, 1, 0, -M_PI_4)
    block.geometry = scn.Box(width=22, height=2., length=34, chamferRadius=0)
    frontMat = scn.Material()
    frontMat.locksAmbientWithDiffuse = True
    frontMat.diffuse.contents = "resources/book_front.jpg"
    frontMat.diffuse.mipFilter = scn.FilterMode.Linear
    backMat = scn.Material()
    backMat.locksAmbientWithDiffuse = True
    backMat.diffuse.contents = "resources/book_back.jpg"
    backMat.diffuse.mipFilter = scn.FilterMode.Linear
    edgeMat = scn.Material()
    edgeMat.locksAmbientWithDiffuse = True
    edgeMat.diffuse.contents = "resources/book_side_title.jpg"
    edgeMat.diffuse.mipFilter = scn.FilterMode.Linear
    edgeMatSide = scn.Material()
    edgeMatSide.locksAmbientWithDiffuse = True
    edgeMatSide.diffuse.contents = "resources/book_side.jpg"
    edgeMatSide.diffuse.mipFilter = scn.FilterMode.Linear
  
    block.geometry.materials = [edgeMatSide, edgeMatSide, edgeMatSide, edgeMat, frontMat, backMat]
    block.physicsBody = scn.PhysicsBody.dynamicBody()
    scene.rootNode.addChildNode(block)
        
    #add carpet
    path = ui.Path.rounded_rect(-50, -30, 100, 50, 2.5)
    rug_geometry = scn.Shape.shapeWithPath(path, extrusionDepth=0.05)
    rug = scn.Node.nodeWithGeometry(rug_geometry)
    rug.geometry.firstMaterial.locksAmbientWithDiffuse = True
    rug.geometry.firstMaterial.diffuse.contents = "resources/carpet.jpg"
    rug.position = scn.Vector3(0, 0.01, 0)
    rug.rotation = scn.Vector4(1, 0, 0, M_PI_2)
    scene.rootNode.addChildNode(rug)
        
    #add ball
    ball = scn.Node()
    ball.position = scn.Vector3(-5, 5, -18)
    ball.geometry = scn.Sphere(radius=5)
    ball.geometry.firstMaterial.locksAmbientWithDiffuse = True
    ball.geometry.firstMaterial.diffuse.contents = "resources/ball.jpg"
    scale = list(scn.Matrix4Identity)
    scale[0], scale[5], scale[10] = 2., 1., 1.
    ball.geometry.firstMaterial.diffuse.contentsTransform = scale
    ball.geometry.firstMaterial.diffuse.wrapS = scn.WrapMode.Mirror
    ball.physicsBody = scn.PhysicsBody.dynamicBody()
    ball.physicsBody.restitution = 0.9
    scene.rootNode.addChildNode(ball)
Exemple #12
0
def main():
    main_view = ui.View()
    w, h = ui.get_screen_size()
    main_view.frame = (0, 0, w, h)
    main_view.name = 'Photos cube'

    scene_view = scn.View(main_view.frame)
    scene_view.autoresizingMask = (scn.ViewAutoresizing.FlexibleHeight,
                                   scn.ViewAutoresizing.FlexibleRightMargin)

    scene_view.allowsCameraControl = True

    scene_view.backgroundColor = 'white'

    scene_view.addToSuperview(main_view)

    scene = scn.Scene()
    scene_view.scene = scene

    root_node = scene.rootNode

    cube_geometry = scn.Box(width=1, height=1, length=1, chamferRadius=0.05)
    cube_geometry_inside = scn.Box(width=1 - 0.001,
                                   height=1 - 0.001,
                                   length=1 - 0.001,
                                   chamferRadius=0.04)

    Material_inside = scn.Material()
    Material_inside.diffuse.contents = inside_image

    cube_geometry_inside.materials = [Material_inside for i in range(6)]

    cube_geometry_materials = [scn.Material() for i in range(6)]
    for i in range(6):
        cube_geometry_materials[i].diffuse.contents = cube_images[i]

    cube_geometry.materials = cube_geometry_materials

    cube_node = scn.Node.nodeWithGeometry(cube_geometry)
    cube_node_inside = scn.Node.nodeWithGeometry(cube_geometry_inside)
    cube_node.addChildNode(cube_node_inside)

    cube_action = scn.Action.scaleTo(1.5, 3.0)
    cube_reversed_action = scn.Action.scaleTo(1.0, 1.0)
    cube_wait_action = scn.Action.waitForDuration(0.8)
    cube_y_rot_action1 = scn.Action.rotateBy(0, 0.2, 0, 0.3)
    cube_y_rot_action2 = scn.Action.rotateBy(0, -0.4, 0, 0.3)
    cube_y_rot_action3 = scn.Action.rotateBy(0, 0.2, 0, 0.3)
    cube_sequence = scn.Action.sequence([
        cube_action, cube_reversed_action, cube_wait_action,
        cube_y_rot_action1, cube_y_rot_action2, cube_y_rot_action3,
        cube_wait_action
    ])
    cube_forever = scn.Action.repeatActionForever(cube_sequence)

    cube_node.runAction(cube_forever)

    constraint = scn.LookAtConstraint.lookAtConstraintWithTarget(cube_node)
    constraint.gimbalLockEnabled = True

    camera = scn.Camera()
    camera_node = scn.Node()
    camera_node.name = 'camera!'
    camera_node.camera = camera
    camera_node.position = (-3.0, 3.0, 3.0)
    camera_node.constraints = [constraint]

    lights_node = scn.Node()

    ambient_light = scn.Light()
    ambient_light.type = scn.LightTypeAmbient
    ambient_light.name = 'ambient light'
    ambient_light.color = (.99, 1.0, .86)
    ambient_node = scn.Node()
    ambient_node.light = ambient_light
    lights_node.addChildNode(ambient_node)

    directional_light = scn.Light()
    directional_light.type = scn.LightTypeDirectional
    directional_light.name = 'directional light'
    directional_light.color = 'white'
    directional_node = scn.Node()
    directional_node.light = directional_light
    directional_node.position = camera_node.position
    directional_node.constraints = [constraint]

    lights_node.addChildNode(directional_node)

    root_node.addChildNode(camera_node)
    root_node.addChildNode(lights_node)
    root_node.addChildNode(cube_node)
    scene_view.pointOfView = camera_node

    scn.Transaction.setAnimationDuration(10.0)
    directional_light.color = 'yellow'

    main_view.present(hide_title_bar=False)
Exemple #13
0
    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)