def __init__(self): shader = pi3d.Shader("uv_reflect") bumpsh = pi3d.Shader("uv_bump") flatsh = pi3d.Shader("uv_flat") #shade2d = pi3d.Shader('2d_flat') ectex = pi3d.loadECfiles("../textures/ecubes/RedPlanet", "redplanet_256", "png", True) self.myecube = pi3d.EnvironmentCube(size=1800.0, maptype="FACES") self.myecube.set_draw_details(flatsh,ectex) # Create elevation map mapwidth=2000.0 mapdepth=2000.0 mapheight=100.0 redplanet = pi3d.Texture("../textures/mars_colour.png") bumpimg = pi3d.Texture("../textures/mudnormal.jpg") self.mymap = pi3d.ElevationMap(mapfile='../textures/mars_height.png', width=mapwidth, depth=mapdepth, height=mapheight, divx=64, divy=64) self.mymap.set_draw_details(bumpsh,[redplanet, bumpimg],128.0, 0.0) self.mymap.set_fog((0.3, 0.15, 0.1, 0.0), 1000.0) #Load Corridors sections sttnbmp = pi3d.Texture("../textures/floor_nm.jpg") sttnshn = pi3d.Texture("../textures/stars.jpg") x,z = 0,0 y = self.mymap.calcHeight(x, z) #corridor with windows self.cor_win = pi3d.Model(file_string="../models/MarsStation/corridor_win_lowpoly.egg", x=x, y=y, z=z, sx=0.1, sy=0.1, sz=0.1) self.cor_win.set_shader(shader) #normal corridor self.corridor = pi3d.Model(file_string="../models/MarsStation/corridor_lowpoly.egg", x=x, y=y, z=z, sx=0.1, sy=0.1, sz=0.1) self.corridor.set_shader(shader) #corridor crossing self.cor_cross = pi3d.Model(file_string="../models/MarsStation/cross_room.egg", x=x, y=y, z=z, sx=0.1, sy=0.1, sz=0.1) self.cor_cross.set_shader(shader) self.cor_cross.set_normal_shine(sttnbmp, 32.0, sttnshn, 0.1) #corridor crossing with doors self.cor_cross_doors = pi3d.Model(file_string="../models/MarsStation/cross_room_doors.egg", x=x, y=y, z=z, sx=0.1, sy=0.1, sz=0.1) self.cor_cross_doors.set_shader(shader) self.cor_cross_doors.set_normal_shine(sttnbmp, 32.0, sttnshn, 0.1) #corridor with 90 degree bend self.cor_bend = pi3d.Model(file_string="../models/MarsStation/bend_lowpoly.egg", x=x, y=y, z=z, sx=0.1, sy=0.1, sz=0.1) self.cor_bend.set_shader(shader) self.cor_bend.set_normal_shine(sttnbmp, 32.0) self.cor_bend.rotateToY(180)
def __init__(self): #Setup shaders flatsh = pi3d.Shader("uv_flat") #shade2d = pi3d.Shader('2d_flat') # create splash screen and draw it #splash = pi3d.ImageSprite("../textures/pi3d_splash.jpg", shade2d, w=10, h=10, z=0.2) #splash.draw() #DISPLAY.swap_buffers() #Setup environment cube ectex = pi3d.loadECfiles("../textures/ecubes/Miramar", "miramar_256", "png", nobottom = True) self.myecube = pi3d.EnvironmentCube(size=1800.0, maptype="FACES", nobottom=True) self.myecube.set_draw_details(flatsh, ectex) #Load Hall model self.mymap = pi3d.Model(file_string="../models/ConferenceHall/conferencehall.egg", name="Hall", sx=0.1, sy=0.1, sz=0.1) self.mymap.set_shader(flatsh) self.mymap.position(0.0,10.0, 0.0)
def main(): #Model textures and shaders shader = pi3d.Shader("uv_reflect") bumptex = pi3d.Texture("textures/floor_nm.jpg") shinetex = pi3d.Texture("textures/stars.jpg") # load model mymodel = pi3d.Model(file_string='models/teapot.obj', z=10) mymodel.set_shader(shader) mymodel.set_normal_shine(bumptex, 4.0, shinetex, 0.5) #Create environment box flatsh = pi3d.Shader("uv_flat") ectex=pi3d.loadECfiles("textures/ecubes","sbox") myecube = pi3d.EnvironmentCube(size=900.0, maptype="FACES", name="cube") myecube.set_draw_details(flatsh, ectex) CAMERA = pi3d.Camera.instance() rot = 0.0 # rotation of camera tilt = 0.0 # tilt of camera while DISPLAY.loop_running() and not \ inputs.key_state("KEY_ESC"): #Rotate camera inputs.do_input_events() # camera steered by mouse #Note:Some mice devices will be located on #get_mouse_movement(1) instead of get_mouse_movement() mx,my,mv,mh,md=inputs.get_mouse_movement() #mx,my,mv,mh,md=inputs.get_mouse_movement(1) rot -= (mx)*0.2 tilt -= (my)*0.2 CAMERA.reset() CAMERA.rotate(tilt, rot, 0) #Rotate object mymodel.rotateIncY(2.0) mymodel.rotateIncZ(0.1) mymodel.rotateIncX(0.3) #Draw objects mymodel.draw() myecube.draw()
#======================================== # create splash screen and draw it splash = pi3d.ImageSprite("textures/tiger_splash.jpg", shade2d, w=10, h=10, z=0.2) splash.draw() DISPLAY.swap_buffers() # create environment cube ectex = pi3d.loadECfiles('textures/ecubes/Miramar', 'miramar_256', suffix='png') myecube = pi3d.EnvironmentCube(size=1800.0, maptype='FACES') myecube.set_draw_details(flatsh, ectex) # Create elevation map mapwidth = 1800.0 mapdepth = 1800.0 mapheight = 120.0 mountimg1 = pi3d.Texture('textures/mountains3_512.jpg') bumpimg = pi3d.Texture('textures/grasstile_n.jpg') tigerbmp = pi3d.Texture('models/Tiger/tiger_bump.jpg') topbmp = pi3d.Texture('models/Tiger/top_bump.jpg') #roadway = pi3d.Texture('textures/road5.png') mymap = pi3d.ElevationMap(mapfile='textures/mountainsHgt2.png', width=mapwidth, depth=mapdepth, height=mapheight,
try: refid = (open("/sys/class/net/wlan0/address").read()).strip() except: refid = "00:00:00:00:00:00" #create the instances of Aeroplane a = Aeroplane("models/biplane.obj", 0.02, refid) a.z, a.direction = 900, 180 #create instance of instruments inst = Instruments() others = {"start": 0.0} #contains a dictionary of other players keyed by refid thr = threading.Thread(target=json_load, args=(a, others)) thr.daemon = True #allows the program to exit even if a Thread is still running thr.start() # Load textures for the environment cube ectex = pi3d.loadECfiles("textures/ecubes", "sbox") myecube = pi3d.EnvironmentCube(size=7000.0, maptype="FACES", camera=CAMERA) myecube.set_draw_details(FLATSH, ectex) myecube.set_fog((0.5, 0.5, 0.5, 1.0), 4000) # Create elevation map mapwidth = 10000.0 mapdepth = 10000.0 mapheight = 1000.0 mountimg1 = pi3d.Texture("textures/mountains3_512.jpg") bumpimg = pi3d.Texture("textures/grasstile_n.jpg") reflimg = pi3d.Texture("textures/stars.jpg") mymap = pi3d.ElevationMap("textures/mountainsHgt.jpg", name="map", width=mapwidth, depth=mapdepth, height=mapheight, divx=64,
light = pi3d.Light((10, -10, 20)) # load shader shader = pi3d.Shader("uv_reflect") flatsh = pi3d.Shader("uv_flat") #defocus = pi3d.Defocus() #<<<<<<<<<<<<<<< #======================================== # 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
drawing will use the previous image which is top """ import demo import pi3d # Setup display and initialise pi3d DISPLAY = pi3d.Display.create(x=50, y=50) shader = pi3d.Shader('shaders/uv_flat') #======================================== #select the environment cube with 'box'... box = 3 if box == 0: ectex = [pi3d.Texture('textures/ecubes/skybox_interstellar.jpg')] myecube = pi3d.EnvironmentCube(size=900.0, maptype='CROSS') elif box == 1: ectex = [pi3d.Texture('textures/ecubes/SkyBox.jpg')] myecube = pi3d.EnvironmentCube(size=900.0, maptype='HALFCROSS') elif box == 2: ectex = pi3d.loadECfiles('textures/ecubes', 'sbox_interstellar', nobottom=True) myecube = pi3d.EnvironmentCube(size=900.0, maptype='FACES', nobottom=True) else: ectex = pi3d.loadECfiles('textures/ecubes', 'skybox_hall') myecube = pi3d.EnvironmentCube(size=900.0, maptype='FACES') myecube.set_draw_details(shader, ectex) rot = 0.0
BLOCK_SIZE = 4.0 # Setup display and initialise pi3d DISPLAY = pi3d.Display.create(x=100, y=100, frames_per_second=10) DISPLAY.set_background(0.4, 0.8, 0.8, 1) # r,g,b,alpha #======================================== CAMERA = pi3d.Camera() LIGHT = pi3d.Light(lightamb=(0.6, 0.6, 0.8)) # load shader shader = pi3d.Shader("uv_light") flatsh = pi3d.Shader("uv_flat") foodTex = pi3d.Texture("textures/Raspi256x256.png") partTex = pi3d.Texture("textures/world_map256x256.jpg") ectex = pi3d.loadECfiles("textures/ecubes", "skybox_hall") myecube = pi3d.EnvironmentCube(size=2.0 * SIZE, maptype="FACES", name="cube") myecube.set_draw_details(shader, ectex) class Food: def __init__(self, snake): self.snake = snake plane = pi3d.Plane(w=BLOCK_SIZE * 2.0, h=BLOCK_SIZE * 2.0) self.shape = pi3d.MergeShape() self.shape.add(plane, 0, 0, 0) self.shape.add(plane, 0, 0, 0, 0, 90, 0) self.shape.set_draw_details(shader, [foodTex]) self.spawn() def spawn(self): collision = True
class Main(object): # Setup display and initialise pi3d DISPLAY = pi3d.Display.create() 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") tree2img = pi3d.Texture("textures/tree2.png") tree1img = pi3d.Texture("textures/tree1.png") hb2img = pi3d.Texture("textures/hornbeam2.png") 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) TFOG = ((0.2, 0.24, 0.22, 1.0), 150.0) #myecube = pi3d.EnvironmentCube(900.0,"HALFCROSS") 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 tree models treeplane = pi3d.Plane(w=4.0, h=5.0) treemodel1 = pi3d.MergeShape(name="baretree") treemodel1.add(treeplane.buf[0], 0, 0, 0) treemodel1.add(treeplane.buf[0], 0, 0, 0, 0, 90, 0) treemodel2 = pi3d.MergeShape(name="bushytree") treemodel2.add(treeplane.buf[0], 0, 0, 0) treemodel2.add(treeplane.buf[0], 0, 0, 0, 0, 60, 0) treemodel2.add(treeplane.buf[0], 0, 0, 0, 0, 120, 0) #Scatter them on map using Merge shape's cluster function mytrees1 = pi3d.MergeShape(name="trees1") mytrees1.cluster(treemodel1.buf[0], mymap, 0.0, 0.0, 200.0, 200.0, 20, "", 8.0, 3.0) mytrees1.set_draw_details(flatsh, [tree2img], 0.0, 0.0) mytrees1.set_fog(*TFOG) 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 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) #avatar camera rot = 0.0 tilt = 0.0 avhgt = 3.5 xm = 0.0 zm = 0.0 ym = mymap.calcHeight(xm, zm) + avhgt go_flag = False go_speed = 0.2 CAMERA = pi3d.Camera.instance() def pi3dloop(self, dt): self.DISPLAY.loop_running() self.CAMERA.reset() self.CAMERA.rotate(self.tilt, self.rot, 0) self.CAMERA.position((self.xm, self.ym, self.zm)) self.myecube.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.myecube.draw() self.mymap.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() self.mytrees1.draw() self.mytrees2.draw() self.mytrees3.draw() if pi3d.PLATFORM == 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.tapped: self.go_speed *= 1.5 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 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: if k == ord('w'): #key W self.go_flag = not self.go_flag elif k == 27: #Escape key return False if self.go_flag: self.xm -= math.sin(math.radians(self.rot)) * self.go_speed self.zm += math.cos(math.radians(self.rot)) * self.go_speed self.ym = self.mymap.calcHeight(self.xm, self.zm) + self.avhgt 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.go_speed = 0.2 return True def run(self): if pi3d.PLATFORM == PLATFORM_ANDROID: #***************************** self.DISPLAY.android.set_loop(self.pi3dloop) self.DISPLAY.android.run() 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()
def __init__(self, camera): super(Background, self).__init__(camera, pi3d.EnvironmentCube(size=40.0, maptype='FACES'), fground=False)
#!/usr/bin/python3 import pi3d from vrzero import engine # This VR Zero demo is modified version of the original Pi3D demo, found here: # https://github.com/pi3d/pi3d_demos/blob/master/LoadModelObj.py engine.init() shader = pi3d.Shader("uv_reflect") flat_shader = pi3d.Shader("uv_flat") ectex = pi3d.loadECfiles("textures/ecubes","sbox") myecube = pi3d.EnvironmentCube(size=900.0, maptype="FACES", name="bfa", y=50.0) myecube.set_draw_details(flat_shader, ectex) # ======================================== # 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. pi3d.opengles.glDisable(pi3d.GL_CULL_FACE) # ======================================== # load bump and reflection textures bumptex = pi3d.Texture("textures/floor_nm.jpg") shinetex = pi3d.Texture("textures/stars.jpg") # ======================================== # load model
def pi_3d(inHeightmap, inWidth, inDepth, inHeight, inTextureMap, inBumpMap): rads = 0.017453292512 # degrees to radians # helpful messages # Esc to quit, W to go forward, Mouse to steer # At the edge you will turn into a ghost and be able to fly and pass through rocks! # Setup display and initialise pi3d DISPLAY = pi3d.Display.create(x=100, y=100, background=(0.4, 0.8, 0.8, 1), use_pygame=True) shader = pi3d.Shader("uv_bump") flatsh = pi3d.Shader("uv_flat") light = pi3d.Light((1, 1, 1), (400.0, 400.0, 350.0), (0.03, 0.03, 0.05), True) # sun = pi3d.Light((0.0, 1000.0, 0.0), (0.5, 1.0, 0.7), (0.3, 0.1, 0.1), is_point=True) # load Textures rockimg1 = pi3d.Texture("textures/techy1.jpg") rockimg2 = pi3d.Texture("textures/rocktile2.jpg") shineimg = pi3d.Texture("textures/stars.jpg") # environment cube ectex = pi3d.Texture("textures/ecubes/skybox_stormydays.jpg") myecube = pi3d.EnvironmentCube(size=900.0, maptype="CROSS") myecube.set_draw_details(flatsh, ectex) # Create elevation map # inWidth = Width of Map (i.e. 1024.0) # inDepth = Depth of Map (i.e. 1024.0) # inHeight = Height of Map (i.e 0.5) mapwidth = inWidth mapdepth = inDepth mapheight = inHeight bumpsh = pi3d.Shader('uv_bump') # inHeightmap = Maps/seed_XXXXX_grayscale.png # inTextureMap = Maps/seed_XXXXX_elevation.png # inBumpMap = Maps/seed_XXXXX_normal.png mymap = pi3d.ElevationMap(inHeightmap, width=mapwidth, depth=mapdepth, height=mapheight, divx=199, divy=199, name="sub") mymap.set_draw_details(shader, [rockimg1, rockimg2, shineimg], 128.0, 0.05) redplanet = pi3d.Texture(inTextureMap) bumpimg = pi3d.Texture(inBumpMap) mymap.set_draw_details(bumpsh, [redplanet, bumpimg], 128.0, 0.0) mymap.set_fog((0.3, 0.15, 0.1, 0.0), 1000.0) rot = 0.0 tilt = 0.0 avhgt = 3.0 xm, oxm = 0.0, -1.0 zm, ozm = 0.0, -1.0 ym = mymap.calcHeight(xm, zm) + avhgt # Fetch key presses mykeys = pi3d.Keyboard() mymouse = pi3d.Mouse(restrict=False) mymouse.start() omx, omy = mymouse.position() fly = False walk = True CAMERA = pi3d.Camera.instance() print ('at line 71 - Code3D') while DISPLAY.loop_running(): # movement of camera mx, my = mymouse.position() rot -= (mx - omx) * 0.2 tilt += (my - omy) * 0.1 dx = -math.sin(rot * rads) dz = math.cos(rot * rads) dy = math.sin(tilt * rads) if walk: if fly: xm += dx * 3 zm += dz * 3 ym += dy * 3 else: dy = mymap.calcHeight(xm + dx * 1.5, zm + dz * 1.5) + avhgt - ym if dy < 1.2: # limit steepness so can't climb up walls xm += dx * 0.5 zm += dz * 0.5 ym += dy * 0.5 if xm < -490 or xm > 490 or zm < -490 or zm > 490: fly = True # reached the edge if not (mx == omx and my == omy and oxm == xm and ozm == zm): CAMERA.reset() CAMERA.rotate(tilt, 0, 0) CAMERA.rotate(0, rot, 0) CAMERA.position((xm, ym, zm)) omx = mx omy = my oxm = xm ozm = zm light.position((xm, ym + 15.0, zm)) light.is_point = True mymap.set_light(light) myecube.position(xm, ym, zm) myecube.draw() mymap.draw() # key presses (ESCAPE to terminate) k = mykeys.read() if k > -1: if k == 119: # W Key toggle walk = not walk elif k == 115: # S Key walk = False dy = -(mymap.calcHeight(xm - dx, zm - dz) + avhgt) - ym if dy > -1.0: xm -= dx zm -= dz ym += dy elif k == 27: # ESCAPE key DISPLAY.destroy() mykeys.close() mymouse.stop() break # this will save a little time each loop if the camera is not moved CAMERA.was_moved = False quit()
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()
def __init__(self): # load shader shader = pi3d.Shader("uv_bump") #shinesh = pi3d.Shader("uv_reflect") flatsh = pi3d.Shader("uv_flat") tree1img = pi3d.Texture("../textures/tree1.png") tree2img = pi3d.Texture("../textures/tree2.png") hb2img = pi3d.Texture("../textures/hornbeam2.png") bumpimg = pi3d.Texture("../textures/grasstile_n.jpg") reflimg = pi3d.Texture("../textures/stars.jpg") floorimg = pi3d.Texture("../textures/floor_nm.jpg") FOG = ((0.3, 0.3, 0.4, 0.8), 650.0) TFOG = ((0.2, 0.24, 0.22, 1.0), 150.0) ectex=pi3d.loadECfiles("../textures/ecubes","sbox") self.myecube = pi3d.EnvironmentCube(size=900.0, maptype="FACES", name="cube") self.myecube.set_draw_details(flatsh, ectex) self.myecube.position(0,0,0) # Create elevation map mapsize = 1000.0 mapheight = 60.0 mountimg1 = pi3d.Texture("../textures/mountains3_512.jpg") self.mymap = pi3d.ElevationMap("../textures/mountainsHgt.png", name="map", width=mapsize, depth=mapsize, height=mapheight, divx=32, divy=32) self.mymap.set_draw_details(shader, [mountimg1, bumpimg, reflimg], 128.0, 0.0) self.mymap.set_fog(*FOG) self.building = pi3d.Model(file_string="../models/ConferenceHall/conferencehall.egg", name="Hall", sx=0.1, sy=0.1, sz=0.1) self.building.set_shader(flatsh) #Create tree models treeplane = pi3d.Plane(w=4.0, h=5.0) treemodel1 = pi3d.MergeShape(name="baretree") treemodel1.add(treeplane.buf[0], 0,0,0) treemodel1.add(treeplane.buf[0], 0,0,0, 0,90,0) treemodel2 = pi3d.MergeShape(name="bushytree") treemodel2.add(treeplane.buf[0], 0,0,0) treemodel2.add(treeplane.buf[0], 0,0,0, 0,60,0) treemodel2.add(treeplane.buf[0], 0,0,0, 0,120,0) #Scatter them on map using Merge shape's cluster function self.mytrees1 = pi3d.MergeShape(name="trees1") self.mytrees1.cluster(treemodel1.buf[0], self.mymap,0.0,0.0,200.0,200.0,20,"",8.0,3.0) self.mytrees1.set_draw_details(flatsh, [tree2img], 0.0, 0.0) self.mytrees1.set_fog(*TFOG) self.mytrees2 = pi3d.MergeShape(name="trees2") self.mytrees2.cluster(treemodel2.buf[0], self.mymap,0.0,0.0,200.0,200.0,20,"",6.0,3.0) self.mytrees2.set_draw_details(flatsh, [tree1img], 0.0, 0.0) self.mytrees2.set_fog(*TFOG) self.mytrees3 = pi3d.MergeShape(name="trees3") self.mytrees3.cluster(treemodel2, self.mymap,0.0,0.0,300.0,300.0,20,"",4.0,2.0) self.mytrees3.set_draw_details(flatsh, [hb2img], 0.0, 0.0) self.mytrees3.set_fog(*TFOG) #screenshot number scshots = 1
w=winw, h=winh, far=2200.0, fov = 60, background=(0.4, 0.8, 0.8, 1), frames_per_second=20) win = DISPLAY.tkwin #Setup shaders flatsh = pi3d.Shader("shaders/uv_flat") shade2d = pi3d.Shader('shaders/2d_flat') # create splash screen and draw it splash = pi3d.ImageSprite("textures/pi3d_splash.jpg", shade2d, w=10, h=10, z=0.2) splash.draw() DISPLAY.swap_buffers() #Setup environment cube ectex = pi3d.loadECfiles("textures/ecubes/Miramar", "miramar_256", "png", nobottom = True) myecube = pi3d.EnvironmentCube(size=1800.0, maptype="FACES", nobottom=True) myecube.set_draw_details(flatsh, ectex) #Load Hall model hall = pi3d.Model(file_string="models/ConferenceHall/conferencehall.egg", name="Hall", sx=0.1, sy=0.1, sz=0.1) hall.set_shader(flatsh) #key presses mymouse = pi3d.Mouse(restrict = False) mymouse.start() omx, omy = mymouse.position() #position vars mouserot = -70.0 tilt = 10.0 avhgt = 4.0
DISPLAY.set_background(0, 0, 0, 1) # r,g,b,alpha # Camera --------------------------------- CAMERA = pi3d.Camera() # Shaders -------------------------------- shader = pi3d.Shader("uv_light") flatsh = pi3d.Shader("uv_flat") tracksh = pi3d.Shader("mat_flat") # Textures ------------------------------- cloudimg = pi3d.Texture("textures/earth_clouds.png", True) sunimg = pi3d.Texture("textures/sun.jpg") sunshellimg = pi3d.Texture("textures/sun_shell.png", True) earthimg = pi3d.Texture("textures/world_map.jpg") moonimg = pi3d.Texture("textures/moon.jpg") # EnvironmentCube ------------------------ ectex = [pi3d.Texture('textures/ecubes/skybox_grimmnight.jpg')] myecube = pi3d.EnvironmentCube(size=900.0, maptype='CROSS') myecube.set_draw_details(flatsh, ectex) # Planets -------------------------------- sun = Planet([sunimg, sunshellimg], shader, 1.0, 8000000, pos=[0.0, 0.0, 0.0], vel=[-0.01, 0.0, 0.0]) #to keep total momentum of system zero! earth = Planet([earthimg, cloudimg], shader, 0.125, 80000000, pos=[0.0, -1.0, -9.0], vel=[0.5, 0.1, 0.0], track_shader=tracksh)
shinesh = pi3d.Shader("uv_reflect") flatsh = pi3d.Shader("uv_flat") tree2img = pi3d.Texture("textures/tree2.png", mipmap=False) tree1img = pi3d.Texture("textures/tree1.png", mipmap=False) hb2img = pi3d.Texture("textures/hornbeam2.png", mipmap=False) 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) TFOG = ((0.2, 0.24, 0.22, 1.0), 350.0) #myecube = pi3d.EnvironmentCube(900.0,"HALFCROSS") 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 mapwidth = 1000.0 mapdepth = 1000.0 mapheight = 45.0 # can't cope with much elevation mountimg1 = pi3d.Texture("textures/mountains3_512.jpg") mymap = pi3d.ElevationMap("textures/mountainsHgt.jpg", name="map", width=mapwidth, depth=mapdepth, height=mapheight, divx=32, divy=32) #testislands.jpg mymap.set_draw_details(shader, [mountimg1, bumpimg, bumpimg], 128.0, 0.0) mymap.set_fog(*FOG) #Create tree models treeplane = pi3d.Plane(w=4.0, h=5.0)
piCamera = PiCamera() piCamera.start_preview() # Shaders shader = pi3d.Shader("uv_light") shinesh = pi3d.Shader("uv_reflect") flatsh = pi3d.Shader("uv_flat") matsh = pi3d.Shader("mat_reflect") ################################# # Textures patimg = pi3d.Texture("textures/PATRN.PNG") shapebump = pi3d.Texture("textures/floor_nm.jpg") shapshine = 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 shape cylinder = pi3d.EnvironmentCube(light=light, size=1, name="Cylinder", x=0, y=0, z=5) cylinder.set_line_width(2) # Text arialFont = pi3d.Font("fonts/FreeMonoBoldOblique.ttf", (221,0,170,255)) delta = 1 # Fetch key presses mykeys = pi3d.Keyboard() scale = 1 lastKeyRead = -1 while DISPLAY.loop_running(): displayString = "Delta: {}, X: {}, Y:{}, Z: {}".format(delta, cylinder.x(), cylinder.y(), cylinder.z()) mystring = pi3d.FixedString("fonts/FreeMonoBoldOblique.ttf", displayString, camera=TEXT_CAMERA, color=(255,255,255,255), font_size=24, margin=0.0, justify='R', background_color=(0,0,0,255), shader=flatsh, f_type='SMOOTH') CAMERA.reset() cylinder.draw(shinesh, [patimg, shapebump, shapshine], 4.0, 0.1)