def change_details(self, star_ref, planet_tex, obj_object, v): self.star_ref = star_ref star_loc = v[star_ref, 0:3] star_rgb = v[star_ref, 3:6] light = pi3d.Light(is_point=True, lightpos=star_loc, lightcol=star_rgb * 15.0, lightamb=(0.01, 0.01, 0.01)) if self.star is None: self.star = pi3d.Sphere(radius=0.4, slices=24, sides=24) self.star.set_draw_details(self.flatsh, [self.star_tex]) self.star.set_material(star_rgb) self.star.position(*star_loc) if self.planet is None: self.planet = pi3d.Sphere(radius=0.1, slices=24, sides=24, light=light) self.planet.set_draw_details(self.shader, planet_tex, 1.0, 0.0) self.planet.set_light(light) self.planet.position(star_loc[0], star_loc[1], star_loc[2] - 2.2) self.obj_object = obj_object self.obj_object.set_shader(self.shader) self.obj_object.set_light(light) self.obj_object.position(star_loc[0] + 0.2, star_loc[1], star_loc[2] - 1.75)
def __init__(self, x=0, y=0, z=0, direction=NORTH): self.direction = direction #self.shape = pi3d.Cuboid(w=BLOCK_SIZE, h=BLOCK_SIZE, d=BLOCK_SIZE) self.shape = pi3d.Sphere(radius=BLOCK_SIZE / 2.0) self.shape.set_draw_details(shader, [partTex]) self.shape.position(x, y, z) self.speed = BLOCK_SIZE
def __init__(self, x, y, z): self.x = x self.y = y self.z = z print('Spawning a food in {0}, {1}, {2}'.format( self.x, self.y, self.z)) self.model = pi3d.Sphere(camera=CAMERA, x=self.x, y=self.y, z=self.z)
def __init__(self, camera): super(SimpleSphere, self).__init__( camera, pi3d.Sphere(radius=20.0, slices=16, sides=16, x=0, y=0, z=160.0, name='1'))
def create_shape(self, x, y, z): if self.shape_type == CUBE: shape = pi3d.Cuboid(x=(x - self.width / 2), y=(y - self.height / 2), z=(z - self.height / 2)) if self.shape_type == SPHERE: shape = pi3d.Sphere(x=(x - self.width / 2), y=(y - self.height / 2), z=(z - self.height / 2), radius=0.5) self.set_material(shape, x, y, z) return shape
def __init__(self, size, dist, img): self.size = size self.dist = dist #distance from Sun self.freq = 1 / dist #frequency is based on distance self.img = pi3d.Texture(img) #declare image self.offset = randint(0, 1000) #sets it randomly along orbit self.light = pi3d.Light(is_point=True, lightpos=(0, 0, 0), lightcol=(5000, 5000, 5000), lightamb=(0.05, 0.05, 0.05)) self.obj = pi3d.Sphere(light=self.light, radius=self.size, x=self.dist) print(self, "size:", self.size, "dist:", self.dist, "freq:", self.freq)
def __init__(self, textures, shader, radius, density, pos=[0.0, 0.0, 0.0], vel=[0.0, 0.0, 0.0], acc=[0.0, 0.0, 0.0], track_shader=None): """arguments generally self explanatory; textures is a list of Textures if more than one then a shell sphere is created with a slightly faster rotational speed, if track_shader is passed then a trace_shape is generated and drawn as points every 1200 call of the position_and_draw() method. The code for the trace_shape is a little obscure. Basically it saves the position of the Planet in a list of vertex (x,y,z) tuples and adds dummy normal and texture coordinates because there is no way to create a Buffer without (at the moment this will be addressed in future) """ super(Planet, self).__init__(radius=radius, x=pos[0], y=pos[1], z=pos[2]) super(Planet, self).set_draw_details(shader, [textures[0]]) self.pos = array(pos) self.vel = array(vel) self.acc = array(acc) self.mass = pi * 4.0 / 3.0 * density * radius**3.0 self.rotation = -0.1 / radius self.shell = None if len(textures) > 1: # second Texture for 'shell' sphere self.shell = pi3d.Sphere(radius=radius * 1.05) self.shell.set_draw_details(shader, [textures[1]]) self.shell.rotation = self.rotation * 1.5 self.track_shader = track_shader self.t_v, self.t_n, self.t_t, self.t_f = [], [], [], [] self.t_len = 0 self.trace_shape = None
def __init__(self, textures, shader, radius, density, pos=[0.0, 0.0, 0.0], vel=[0.0, 0.0, 0.0], acc=[0.0, 0.0, 0.0], track_shader=None, light=None): """arguments generally self explanatory; textures is a list of Textures if more than one then a shell sphere is created with a slightly faster rotational speed, if track_shader is passed then a trace_shape is generated and drawn as points every 1200 call of the position_and_draw() method. The code for the track has been much improved in this version. It now uses a Lines object which has re_init() called every third frame. The Camera method relocate() is also used to make the code tidier. """ super(Planet, self).__init__(radius=radius, slices=24, sides=24, x=pos[0], y=pos[1], z=pos[2]) super(Planet, self).set_draw_details(shader, [textures[0]]) if light is not None: self.set_light(light) self.pos = np.array(pos) self.vel = np.array(vel) self.acc = np.array(acc) self.mass = math.pi * 4.0 / 3.0 * density * radius ** 3.0 self.rotation = -0.1 / radius self.shell = None if len(textures) > 1: # second Texture for 'shell' sphere self.shell = pi3d.Sphere(radius=radius*1.05, slices=24, sides=24) self.shell.set_draw_details(shader, [textures[1]]) if light is not None: self.shell.set_light(light) self.shell.rotation = self.rotation * 1.5 self.track_shader = track_shader if track_shader is not None: pts = np.tile(self.pos, (750, 1)) #start off all inside planet! self.track = pi3d.Lines(material=(0.0,1.0,1.0), vertices=pts) self.f = 0 # frame counter for trail self.t_v, self.t_n, self.t_t, self.t_f = [], [], [], [] self.t_len = 0
persp_cam = pi3d.Camera(scale=0.5) # default instance camera perspecive view #setup textures, light position and initial model position pi3d.Light((0, 5, 0)) #create shaders shader = pi3d.Shader("star") flatsh = pi3d.Shader("uv_flat") post = pi3d.PostProcess(camera=persp_cam, scale=0.5) #Create textures shapeimg = pi3d.Texture("textures/straw1.jpg") shapebump = pi3d.Texture("textures/floor_nm.jpg", True) #Create shape myshape = pi3d.MergeShape(camera=persp_cam) #specify perspective view asphere = pi3d.Sphere(sides=32, slices=32) myshape.radialCopy(asphere, step=72) myshape.position(0.0, 0.0, 5.0) myshape.set_draw_details(shader, [shapeimg], 8.0, 0.1) mysprite = pi3d.Sprite(w=10.0, h=10.0, camera=persp_cam) mysprite.position(0.0, 0.0, 15.0) mysprite.set_draw_details(flatsh, [shapebump]) # Fetch key presses. mykeys = pi3d.Keyboard() tm = 0.0 dt = 0.02 sc = 0.0 ds = 0.001 x = 0.0
mytrees2 = pi3d.MergeShape(name="trees2") mytrees2.cluster(treemodel2.buf[0], mymap,0.0,0.0,200.0,200.0,20,"",6.0,3.0) mytrees2.set_draw_details(flatsh, [tree1img], 0.0, 0.0) mytrees2.set_fog(*TFOG) mytrees3 = pi3d.MergeShape(name="trees3") mytrees3.cluster(treemodel2, mymap,0.0,0.0,300.0,300.0,20,"",4.0,2.0) mytrees3.set_draw_details(flatsh, [hb2img], 0.0, 0.0) mytrees3.set_fog(*TFOG) """ #Create monument tex = pi3d.Texture(image) # can pass numpy array or PIL.Image rather than path as string monument = pi3d.Sphere(sx=W/100.0, sy=H/20.0, sz=W/20.0) monument.set_draw_details(shader, [tex, bumpimg], 4.0, umult=2.0) monument.set_fog(*TFOG) monument.translate(100.0, -mymap.calcHeight(100.0, 235) + 25.0, 235.0) #screenshot number scshots = 1 #avatar camera rot = 0.0 tilt = 0.0 avhgt = 3.5 xm = 0.0 zm = 0.0 ym = mymap.calcHeight(xm, zm) + avhgt
#Display class is the core of pi3d, used to hold screen info #Shape class - all drawn objects in pi3d inherit from the Shape class. #Buffer objecft - contains arrays of values representing vertices. #Shader class - used to compile very fast programs #Camera #Texture objects - used to load images #Light #create screen display = pi3d.Display.create() display.set_background(0.0, 0.0, 0.0, 1) #create shader shader = pi3d.Shader("uv_light") earth_texture = pi3d.Texture("earthtexture.jpg") ball = pi3d.Sphere(z=5.0) #ball.set_material((1.0,0,0,0,0)) #change ball color #listen for keystrokes mykeys = pi3d.Keyboard() while display.loop_running(): k = mykeys.read() if k == 27: #esc mykeys.close() display.destroy() break ball.draw(shader, [earth_texture])
mapheight = 150.0 mymap = pi3d.ElevationMap("mars_height.png", name="map", width=mapsize, depth=mapsize, height=mapheight, divx=32, divy=32) mymap.set_draw_details(shader, [plntimg, mudnorm], 128.0, 0.0) mymap.set_fog(*FOG) # ball stuff DRAG = 0.02 # 2% reduction in speed each frame G = -0.01 # g -> change in y velocity each frame rot = 0.0 # rotation of camera about vertical axis ballrot = 180.0 # heading of ball about vert axis (y) tilt = 0.0 # rotation of camera about local x axis (left to right horizontal) RADIUS = 1.0 # radius of ball ball = pi3d.Triangle(corners=((-0.01, 0.0), (0.0, 0.01), (0.01, 0.0))) # an 'empty' game object ball_shape = pi3d.Sphere(radius=RADIUS, slices=24, sides=24) # the visible shape ball.add_child(ball_shape) # to get realistic 'rolling' ball_shape.set_draw_details(shader, [plntimg, plnnorm], 1.0) # platforms W = 8.0 H = 1.5 on_plat = False # flag if on a platform (for jumping) platforms = [ # locations of platforms pi3d.Cuboid(x=44, y=5, z=43), pi3d.Cuboid(x=32, y=3, z=28), pi3d.Cuboid(x=30, y=2, z=15), pi3d.Cuboid(x=15, y=1, z=5), pi3d.Cuboid(x=0, y=2, z=0), ]
y = json_data['coords'][0]['conformers'][0]['y'] z = json_data['coords'][0]['conformers'][0]['z'] n = len(element) '''NB if you download different molecules you might need to add to alter this. atomic number: name, radius, (r,g,b) tuple, Buffer num (NB this last value not used in the Molecule1 demo)''' atoms = [[1, 'hydrogen', 0.5, (1.0, 0.0, 0.0), 0], [6, 'carbon', 0.8, (0.1, 0.1, 0.1), 1], [7, 'nitrogen', 0.9, (0.1, 0.9, 1.0), 2], [8, 'oxygen', 0.9, (1.0, 1.0, 1.0), 3], [15, 'phosphorus', 1.5, (1.0, 0.0, 1.0), 4]] display = pi3d.Display.create(frames_per_second=30) camera = pi3d.Camera() ball = pi3d.Sphere() ball.set_shader(pi3d.Shader('mat_light')) molecule = pi3d.MergeShape(z=10) molecule.set_fog((0.3, 0.2, 0.6, 0.05), 18.9) # NB fog starts at 0.9 * 18.9, complete at 18.9 for a in atoms: ball.set_material(a[3]) buflist = [] for i in range(n): if a[0] == element[i]: # merge scale values used for atom size buflist.append([ ball, x[i], y[i], z[i], 0.0, 0.0, 0.0, a[2], a[2], a[2], a[4] ]) molecule.merge(buflist)
# Setting 2nd param to True renders 'True' Blending # (this can be changed later to 'False' with 'rockimg2.blend = False') groundimg = pi3d.Texture("textures/stripwood.jpg") monstimg = pi3d.Texture("textures/pong3.png") ballimg = pi3d.Texture("textures/pong2.jpg") # environment cube ectex = pi3d.Texture("textures/ecubes/skybox_stormydays.jpg") myecube = pi3d.EnvironmentCube(camera, light, 900.0, "CROSS") myecube.set_draw_details(flatsh, [ectex]) #ball maxdsz = 0.3 radius = 1.0 ball = pi3d.Sphere(camera, light, radius, 12, 12, 0.0, "sphere", -4, 8, -7) # Shape.set_draw_details is a wrapper for calling the method on each item in buf # as is done explicitly here for no reason than to show that it can be done! ball.set_draw_details(shader, [ballimg], 0.0, 0.0) #monster monster = pi3d.Plane(camera, light, 5.0, 5.0, "monster", 0, 0, 0, 0, 0, 0) monster.set_draw_details(flatsh, [monstimg]) # Create elevation map mapwidth = 50.0 mapdepth = 50.0 maphalf = 22.0 mapheight = 40.0 mymap = pi3d.ElevationMap("textures/pong.jpg",
mytrees2 = pi3d.MergeShape(name="trees2") mytrees2.cluster(treemodel2.buf[0], mymap, 0.0, 0.0, 100.0, 100.0, 30, "", 6.0, 3.0) mytrees2.set_draw_details(flatsh, [tree1img], 0.0, 0.0) mytrees2.set_fog(*TFOG) mytrees3 = pi3d.MergeShape(name="trees3") mytrees3.cluster(treemodel2, mymap, 0.0, 0.0, 300.0, 300.0, 30, "", 4.0, 2.0) mytrees3.set_draw_details(flatsh, [hb2img], 0.0, 0.0) mytrees3.set_fog(*TFOG) #Create monolith monolith = pi3d.Sphere(radius=8.0, slices=12, sides=48, sy=10.0, name="monolith") monolith.translate(100.0, -mymap.calcHeight(100.0, 350) + 10.0, 350.0) monolith.set_draw_details(shader, [rockimg, bumpimg, reflimg], 32.0, 0.3) monolith.set_fog(*FOG) #screenshot number scshots = 1 #avatar camera rot = 0.0 tilt = 0.0 avhgt = 3.5 xm = 0.0 zm = 0.0
########################################## # Load textures patimg = pi3d.Texture("textures/PATRN.PNG") coffimg = pi3d.Texture("textures/COFFEE.PNG") shapebump = pi3d.Texture("textures/floor_nm.jpg") shapeshine = pi3d.Texture("textures/stars.jpg") light = pi3d.Light(lightpos=(-1.0, 0.0, 10.0), lightcol=(3.0, 3.0, 2.0), lightamb=(0.02, 0.01, 0.03), is_point=True) #Create inbuilt shapes mysphere = pi3d.Sphere(radius=1, sides=24, slices=24, name="sphere", x=-4, y=2, z=10) mytcone = pi3d.TCone(radiusBot=0.8, radiusTop=0.6, height=1, sides=24, name="TCone", x=-2, y=2, z=10) myhelix = pi3d.Helix(radius=0.4, thickness=0.1, ringrots=12, sides=24,
element = json_data['atoms']['element'] x = json_data['coords'][0]['conformers'][0]['x'] y = json_data['coords'][0]['conformers'][0]['y'] z = json_data['coords'][0]['conformers'][0]['z'] n = len(element) '''NB if you download different molecules you might need to add to alter this. atomic number: name, radius, (r,g,b) tuple ''' atoms = { 1: ['hydrogen', 0.5 / scaleDownFactorAtom, (1.0, 0.0, 0.0)], 6: ['carbon', 0.8 / scaleDownFactorAtom, (0.1, 0.1, 0.1)], 7: ['nitrogen', 0.9 / scaleDownFactorAtom, (0.1, 0.9, 1.0)], 8: ['oxygen', 0.9 / scaleDownFactorAtom, (1.0, 1.0, 1.0)], 15: ['phosphorus', 1.5 / scaleDownFactorAtom, (1.0, 0.0, 1.0)] } shape1 = pi3d.Sphere(radius=atoms[6][1], y=2.5) empty = pi3d.Triangle(corners=((-0.01, 10.0), (0.0, 0.01), (0.01, 0.0)), z=10.0) elem = {} #made some changes here to make the elem variable a list so that each individual set of atoms can be manipulated later on #otherwise the elem variable will overwrite itself on each atom iteration and only the last set will be manipulable for e in atoms: if e != 6: # don't do carbon again shape1 = pi3d.Sphere(radius=atoms[e][1]) elem[e] = pi3d.MergeShape() for i in range(n): if element[i] == e:
api = get_instagram_api() display = pi3d.Display.create(x=50, y=50) shader = pi3d.Shader("uv_light") img_dir = '/home/sam/Dropbox/Photos/friends/' imgs = [img for img in os.listdir(img_dir) if img[0] != '.'] cur_img = 0 images = [] tex = pi3d.Texture(img_dir + imgs[cur_img]) box = pi3d.Cuboid(x=0, y=0, z=2.2) box.set_draw_details(shader, [tex]) ball = pi3d.Sphere(x=-1.5, y=0, z=2.6, radius=0.5) ball.set_draw_details(shader, [tex]) mykeys = pi3d.Keyboard() cur_user = 0 users = [ "meoremy", "samwell3", "omg.rb.md", "moistbuddha", "butterknuckles", "peanutbutterpear" ] cur_tag = 0 tags = ["magic egg", "fractals", "news", "beauty", "time", "reflection"] loaded = [] while display.loop_running():
mapdepth = 1000.0 mapheight = 60.0 mountimg1 = pi3d.Texture("textures/mars_colour.png") bumpimg = pi3d.Texture("textures/mudnormal.jpg") mymap = pi3d.ElevationMap(mapfile="textures/mars_height.png", width=mapwidth, depth=mapdepth, height=mapheight, divx=128, divy=128) mymap.set_draw_details(bumpsh, [mountimg1, bumpimg], 128.0, 0.0) mymap.set_fog((0.3, 0.15, 0.1, 0.7), 700.0) #create robot metalimg = pi3d.Texture("textures/metalhull.jpg") robot_head = pi3d.Sphere(radius=1.0) robot_body = pi3d.Cylinder(radius=1.0, height=2.0, sides=12) robot_leg = pi3d.Cuboid(w=0.35, h=2.0) robot = pi3d.MergeShape() robot.add(robot_head.buf[0], 0.0, 1.6) robot.add(robot_body.buf[0], 0.0, 0.5) robot.add(robot_leg.buf[0], -1.04, 0, 0) robot.add(robot_leg.buf[0], 1.05, 0, 0) robot.set_draw_details(shader, [metalimg, metalimg, reflcn], 0.0, 0.5) #create space station ssphere = pi3d.Sphere(radius=10, slices=16, sides=16) scorrid = pi3d.Cylinder(radius=4, height=22) station = pi3d.MergeShape(y=mymap.calcHeight(0, 0), rx=4, ry=4, rz=4)
DISPLAY.set_background(0, 0, 0, 1) # r,g,b,alpha shader = pi3d.Shader("uv_light") shinesh = pi3d.Shader("uv_reflect") flatsh = pi3d.Shader("uv_flat") light = pi3d.Light(lightpos=(1.0, 0.0, 0.1)) #======================================== # Setting 2nd param to True renders 'True' Blending # (this can be changed later to 'False' with 'cloudimg.blend = False') cloudimg = pi3d.Texture("textures/earth_clouds.png", True) earthimg = pi3d.Texture("textures/world_map.jpg") moonimg = pi3d.Texture("textures/moon.jpg") starsimg = pi3d.Texture("textures/stars2.jpg") watimg = pi3d.Texture("textures/water.jpg") moonbmp = pi3d.Texture("textures/moon_nm.jpg") # Load shapes mysphere = pi3d.Sphere(radius=2, slices=24, sides=24, name="earth", z=5.8) mysphere2 = pi3d.Sphere(radius=2.05, slices=24, sides=24, name="clouds", z=5.8) mymoon = pi3d.Sphere(radius=0.4, slices=16, sides=16, name="moon") mymoon2 = pi3d.Sphere(radius=0.15, slices=16, sides=16, name="moon2") myplane = pi3d.Plane(w=50, h=50, name="stars", z=30) # Fetch key presses mykeys = pi3d.Keyboard() rot = 0.0 rot1 = 90.0 rot2 = 0.0 m1Rad = 4 # radius of moon orbit m2Rad = 0.6 # radius moon's moon orbit # Display scene while DISPLAY.loop_running():
#setup textures, light position and initial model position pi3d.Light((0, 5, 0)) #create shaders shader = pi3d.Shader("uv_reflect") flatsh = pi3d.Shader("uv_flat") defocus = pi3d.Defocus() #Create textures shapeimg = pi3d.Texture("textures/straw1.jpg") shapebump = pi3d.Texture("textures/floor_nm.jpg") shapeshine = pi3d.Texture("textures/pong3.png") #Create shape myshape = pi3d.MergeShape(camera=persp_cam) #specify perspective view asphere = pi3d.Sphere(sides=16, slices=16) myshape.radialCopy(asphere, step=72) myshape.position(0.0, 0.0, 5.0) myshape.set_draw_details(shader, [shapeimg, shapebump, shapeshine], 8.0, 0.1) mysprite = pi3d.Sprite(w=10.0, h=10.0, camera=persp_cam) mysprite.position(0.0, 0.0, 15.0) mysprite.set_draw_details(flatsh, [shapebump]) tick = 0 next_time = time.time() + 2.0 #load ttf font and set the font colour to 'raspberry' arialFont = pi3d.Font("fonts/NotoSerif-Regular.ttf", (221, 0, 170, 255), add_codepoints=[256]) arialFont.blend = True #much better anitaliased look but must String.draw() after everything else
def __init__(self): shader = pi3d.Shader("mat_flat") self.bullet_prototype = pi3d.Sphere(radius=0.5) self.bullet_prototype.set_shader(shader) self.bullet_prototype.set_material((1.0, 0.0, 0.0))
#Create textures shapeimg = pi3d.Texture("textures/straw1.jpg") shapebump = pi3d.Texture("textures/straw1.jpg", normal_map=-6.0) waterbump = [] iFiles = glob.glob("textures/water/n_norm???.png") iFiles.sort() # order is vital to animation! for f in iFiles: waterbump.append(pi3d.Texture(f)) num_n = len(waterbump) shapeshine = pi3d.Texture("textures/stars.jpg") #Create shape myshape = pi3d.MergeShape() num = (2, 2) asphere = pi3d.Sphere(sides=32) for i in range(num[0]): for j in range(num[1]): myshape.add(asphere, -num[0] * 0.9 + 1.8 * i, -num[1] * 0.9 + 1.8 * j, 0.0) myshape.position(0.0, 0.0, 5) myshape.set_draw_details(shader, [shapeimg, shapebump, shapeshine], 1.0, 0.1) mywater = pi3d.LodSprite(w=250.0, h=250.0, n=6) mywater.set_draw_details(matsh, [waterbump[0], shapeshine], 14.0, 0.6) mywater.set_material((0.1, 0.25, 0.3)) mywater.set_fog((0.4, 0.6, 0.8, 0.0), 100) mywater.rotateToX(85.0) mywater.position(10.0, -2.0, 0.0)
pulse_end_time = time() sleep(0.06) return pulse_end_time - pulse_start def calculate_distance(duration): velocidade = 343 dist = velocidade * duration / 2 # return dist * 100 # transform to meters return dist DISPLAY = pi3d.Display.create( frames_per_second=30) DISPLAY.set_background(0,0,0,1) # r,g,b,alpha light = pi3d.Light(lightpos=(1.0, 0.0, 0.1)) mysphere = pi3d.Sphere(radius=0.10, slices=24, sides=24, name="earth" , z=5.8) myrastros = pi3d.Sphere(radius=0.02, slices=24, sides=24, name="rastros" , z=5.8) myrastros.position(0,0,5.8) rastros = [] for c in range(100): myclone = pi3d.Sphere(radius=0.02, slices=24, sides=24, name="rastros" , z=5.8) myclone.position(0,0,5.8) rastros.append(myclone) contrastros = 0
"textures/inside_map2.png" ] avhgt = 5.0 aveyelevel = 4.0 MAP_BLOCK = 15.0 aveyeleveladjust = aveyelevel - avhgt / 2 PLAYERHEIGHT = (mymap.calcHeight(5, 5) + avhgt / 2) #Start the player in the top-left corner startpos = [(8 * MAP_BLOCK), PLAYERHEIGHT, (8 * MAP_BLOCK)] endpos = [0, PLAYERHEIGHT, 0] #Set the end pos in the centre person = SolidObject("person", Size(1, avhgt, 1), Position(startpos[0], startpos[1], startpos[2]), 1) #Add spheres for start and end, end must also have a solid object #so we can detect when we hit it startobject = pi3d.Sphere(name="start", x=startpos[0], y=startpos[1] + avhgt, z=startpos[2]) startobject.set_draw_details(shader, [startimg, bumpimg], 32.0, 0.3) endobject = pi3d.Sphere(name="end", x=endpos[0], y=endpos[1], z=endpos[2]) endobject.set_draw_details(shader, [endimg, bumpimg], 32.0, 0.3) endSolid = SolidObject("end", Size(1, avhgt, 1), Position(endpos[0], endpos[1], endpos[2]), 1) mazeScheme = { "#models": 3, (1, None): [["C", 2]], #white cell : Ceiling (0, 1, "edge"): [["W", 1]], #white cell on edge next # black cell : Wall (1, 0, "edge"): [["W", 1]], #black cell on edge next # to white cell : Wall (0, 1): [["W", 0]]
flatsh = pi3d.Shader('uv_flat') #======================================== # this is a bit of a one off because the texture has transparent parts # comment out and google to see why it's included here. from pi3d import opengles, GL_CULL_FACE opengles.glDisable(GL_CULL_FACE) #======================================== # load bump and reflection textures bumptex = pi3d.Texture("textures/floor_nm.jpg") shinetex = pi3d.Texture("textures/photosphere.jpg") # load model_loadmodel mymodel = pi3d.Model(file_string='models/teapot.obj', name='teapot') mymodel.set_shader(shader) mymodel.set_normal_shine(bumptex, 0.0, shinetex, 0.4) # create two material shaded spheres sphere1 = pi3d.Sphere(radius=0.25, x=2.0) sphere1.set_draw_details(matsh, [bumptex, shinetex], 4.0, 0.2) sphere1.set_material((0.4, 0.3, 0.1)) sphere1.set_specular((0.6, 0.6, 0.8)) sphere2 = pi3d.Sphere(radius=0.25, z=2.0) sphere2.set_draw_details(matsh, [bumptex, shinetex], 4.0, 0.3) sphere2.set_material((0.2, 0.8, 0.6)) sphere2.set_specular((0.8, 0.8, 1.1)) # environment sphere mysphere = pi3d.Sphere(radius=400.0, rx=180, ry=180, invert=True) mysphere.set_draw_details(flatsh, [shinetex]) # Fetch key presses mykeys = pi3d.Keyboard() mymouse = pi3d.Mouse(restrict=False)
flatsh = pi3d.Shader('uv_flat') #======================================== # this is a bit of a one off because the texture has transparent parts # comment out and google to see why it's included here. from pi3d import opengles, GL_CULL_FACE opengles.glDisable(GL_CULL_FACE) #======================================== # load bump and reflection textures bumptex = pi3d.Texture("textures/floor_nm.jpg") shinetex = pi3d.Texture(npa) # load model_loadmodel mymodel = pi3d.Model(file_string='models/teapot.obj', name='teapot') mymodel.set_shader(shader) mymodel.set_normal_shine(bumptex, 0.0, shinetex, 0.7) mysphere = pi3d.Sphere(radius=400.0, rx=180, ry=180, invert=True) mysphere.set_draw_details(flatsh, [shinetex], vmult=3.0, umult=3.0) # Fetch key presses mykeys = pi3d.Keyboard() mymouse = pi3d.Mouse(restrict=False) mymouse.start() CAMERA = pi3d.Camera.instance() dist = [-4.0, -4.0, -4.0] rot = 0.0 tilt = 0.0 while DISPLAY.loop_running(): k = mykeys.read()
frames_per_second=fps, background=BACKGROUND_COLOR, display_config=pi3d.DISPLAY_CONFIG_HIDE_CURSOR | pi3d.DISPLAY_CONFIG_MAXIMIZED, use_glx=True) #======================================== # load shader shader = pi3d.Shader("uv_bump") flatsh = pi3d.Shader("uv_flat") # Create monument (a sphere with video texture on) tex = pi3d.Texture( image) # can pass numpy array or PIL.Image rather than path as string monument = pi3d.Sphere(sx=W, sy=H, sz=W) monument.set_draw_details(shader, [tex, tex], 4.0, umult=2.0) # create 2D flat sprite instead, taking up the whole screen #sprite = pi3d.Triangle(corners=((-1.0, -1.0),(-1.0, 3.0),(3.0, -1.0))) #sprite.set_shader(flatsh) #sprite.set_draw_details(flatsh, [tex, tex], 4.0, umult=2.0) CAM2D = pi3d.Camera(is_3d=False) sprite = pi3d.Triangle(camera=CAM2D, corners=((-1.0, -1.0), (-1.0, 3.0), (3.0, -1.0))) #sprite = pi3d.Triangle(corners=((-1.0, -1.0),(-1.0, 3.0),(3.0, -1.0))) sprite.set_shader(flatsh) sprite.set_draw_details(flatsh, [tex], umult=2.0, vmult=2.0) ## Matrix ##
def update(self, t): self.obj.position(self.dist * cos(self.freq * (t + self.offset)), 0, self.dist * sin(self.freq * (t + self.offset))) self.obj.draw(shader, [self.img]) #create shader shader = pi3d.Shader('uv_light') sunimg = pi3d.Texture('sun.jpg') #create lights: sunlight = pi3d.Light(lightamb=(1.0, 1.0, 1.0)) #create our objects planets = [] sun = pi3d.Sphere(light=sunlight, radius=15.0, sides=96) mercury = Planet(0.5, 20, 'mercury.jpg') venus = Planet(0.7, 25, 'venus.jpg') earth = Planet(1, 30, 'earth.jpg') mars = Planet(0.7, 40, 'mars.jpg') jupiter = Planet(3, 50, 'jupiter.jpg') uranus = Planet(2, 70, 'uranus.jpg') planets += [mercury, venus, earth, mars, jupiter, uranus]
class Main(object): # Setup display and initialise pi3d DISPLAY = pi3d.Display.create(depth=16) pi3d.Light(lightpos=(1, -1, -3), lightcol=(1.0, 1.0, 0.8), lightamb=(0.25, 0.2, 0.3)) # load shader shader = pi3d.Shader("uv_bump") shinesh = pi3d.Shader("uv_reflect") flatsh = pi3d.Shader("uv_flat") bumpimg = pi3d.Texture("textures/grasstile_n.jpg") reflimg = pi3d.Texture("textures/stars.jpg") rockimg = pi3d.Texture("textures/rock1.jpg") FOG = ((0.3, 0.3, 0.4, 0.8), 650.0) ectex = pi3d.loadECfiles("textures/ecubes", "sbox") myecube = pi3d.EnvironmentCube(size=900.0, maptype="FACES", name="cube") myecube.set_draw_details(flatsh, ectex) # Create elevation map mapsize = 1000.0 mapheight = 60.0 mountimg1 = pi3d.Texture("textures/mountains3_512.jpg") mymap = pi3d.ElevationMap("textures/mountainsHgt.png", name="map", width=mapsize, depth=mapsize, height=mapheight, divx=32, divy=32) mymap.set_draw_details(shader, [mountimg1, bumpimg, reflimg], 128.0, 0.0) mymap.set_fog(*FOG) #Create monument monument = pi3d.Model(file_string="models/pi3d.obj", name="monument") monument.set_shader(shinesh) monument.set_normal_shine(bumpimg, 16.0, reflimg, 0.4) monument.set_fog(*FOG) monument.translate(100.0, -mymap.calcHeight(100.0, 235) + 12.0, 235.0) monument.scale(20.0, 20.0, 20.0) monument.rotateToY(65) #Ball ball = pi3d.Triangle(corners=((-0.01, 0.0), (0.0, 0.01), (0.01, 0.0))) sphere = pi3d.Sphere() sphere.set_draw_details(shader, [rockimg, bumpimg], 1.0) ball.add_child(sphere) #avatar camera rot = 0.0 tilt = 0.0 avhgt = 1.0 xm = 0.0 zm = 0.0 ym = mymap.calcHeight(xm, zm) + avhgt go_flag = False vx, vz = 0.0, 0.0 gx, gz = 0.0, 0.0 CAMERA = pi3d.Camera.instance() CAMERA2D = pi3d.Camera(is_3d=False) font = pi3d.Pngfont("fonts/GillSansMT.png", (200, 30, 10, 255)) font.blend = True txt = None if pi3d.PLATFORM == pi3d.PLATFORM_ANDROID: #***************************** from jnius import autoclass Hardware = autoclass(b'org.renpy.android.Hardware') Hardware.accelerometerEnable(True) def pi3dloop(self, dt): self.DISPLAY.loop_running() self.CAMERA.relocate(self.rot, self.tilt, [self.xm, self.ym, self.zm], [-OFFSET, -OFFSET, -OFFSET]) self.myecube.position(self.xm, self.ym, self.zm) self.ball.position(self.xm, self.ym, self.zm) # for opaque objects it is more efficient to draw from near to far as the # shader will not calculate pixels already concealed by something nearer self.ball.draw() self.mymap.draw() self.myecube.draw() dx = math.copysign(self.mapsize, self.xm) dz = math.copysign(self.mapsize, self.zm) mid = 0.3 * self.mapsize if abs(self.xm) > mid: #nearing edge self.mymap.position(dx, 0.0, 0.0) self.mymap.draw() if abs(self.zm) > mid: #other edge self.mymap.position(0.0, 0.0, dz) self.mymap.draw() if abs(self.xm) > mid: #i.e. in corner, both edges self.mymap.position(dx, 0.0, dz) self.mymap.draw() self.mymap.position(0.0, 0.0, 0.0) self.monument.draw() if pi3d.PLATFORM == pi3d.PLATFORM_ANDROID: #***************************** if self.DISPLAY.android.screen.moved: self.rot -= self.DISPLAY.android.screen.touch.dx * 0.25 self.tilt += self.DISPLAY.android.screen.touch.dy * 0.25 self.DISPLAY.android.screen.moved = False self.DISPLAY.android.screen.tapped = False elif self.DISPLAY.android.screen.double_tapped: self.go_flag = not self.go_flag self.DISPLAY.android.screen.double_tapped = False from kivy.network.urlrequest import UrlRequest def url_success(req, results): self.txt = pi3d.String(camera=self.CAMERA2D, font=self.font, string=results, is_3d=False, y=self.DISPLAY.height / 2.0 + 30.0) self.txt.set_shader(self.flatsh) self.monument.rotateIncY(4) # rotate by some random amount req = UrlRequest( "http://www.eldwick.org.uk/files/rogo/test2.php?num=1", url_success) if self.txt is not None: self.txt.draw() (x, y, z) = self.Hardware.accelerometerReading() sr, _, cr = self.CAMERA.get_direction() self.gx = y * cr + (z - x) * sr self.gz = ( z - x) * cr - y * sr # i.e. hold at 45 degrees for neutral else: mx, my = self.mymouse.position() #if mx>display.left and mx<display.right and my>display.top and my<display.bottom: self.rot -= (mx - self.omx) * 0.2 self.tilt += (my - self.omy) * 0.2 self.omx = mx self.omy = my #Press ESCAPE to terminate k = self.mykeys.read() if k > -1: sr, _, cr = self.CAMERA.get_direction() if k == ord('w'): #key W self.go_flag = not self.go_flag elif k == 261 or k == 137: # rgt self.gx += 0.5 * cr self.gz -= 0.5 * sr elif k == 260 or k == 136: # lft self.gx -= 0.5 * cr self.gz += 0.5 * sr elif k == 259 or k == 134: # up self.gz += 0.5 * cr self.gx += 0.5 * sr elif k == 258 or k == 135: # dwn self.gz -= 0.5 * cr self.gx -= 0.5 * sr elif k == 27: #Escape key return False if self.go_flag: self.xm += self.vx self.zm += self.vz ht, norm = self.mymap.calcHeight(self.xm, self.zm, True) self.ym = ht + self.avhgt self.vx += norm[0] * SLOPE_F + self.gx * TILT_F self.vz += norm[2] * SLOPE_F + self.gz * TILT_F self.vx *= FRICTION self.vz *= FRICTION self.ball.rotateToY(math.degrees(math.atan2(self.vx, self.vz))) self.sphere.rotateIncX(math.degrees( (self.vx**2 + self.vz**2)**0.5)) halfmap = self.mapsize / 2.0 # save doing this four times! self.xm = (self.xm + halfmap) % self.mapsize - halfmap self.zm = (self.zm + halfmap) % self.mapsize - halfmap else: self.vx = 0.0 self.vz = 0.0 self.gx = 0.0 self.gz = 0.0 return True def run(self): if pi3d.PLATFORM == pi3d.PLATFORM_ANDROID: #***************************** self.DISPLAY.android.set_loop(self.pi3dloop) self.DISPLAY.android.run() self.Hardware.accelerometerEnable(False) else: # Fetch key presses self.mykeys = pi3d.Keyboard() self.mymouse = pi3d.Mouse(restrict=False) self.mymouse.start() self.omx, self.omy = self.mymouse.position() while self.pi3dloop(0.0): pass self.mykeys.close() self.mymouse.stop() self.DISPLAY.stop()