def add_orientation_dataset(h5_fname, **kwargs): with h5py.File(h5_fname, 'r+') as f: f.copy('/raw', '/preprocessed') for name, obj in nested_h5py.walk_h5py_path(f['/preprocessed/']): if not isinstance(obj, h5py.Dataset) or not 'Rotation' in name: continue rot_df = pd.DataFrame.from_records(obj.value).set_index( ['Frame', 'Time']) rot_df.columns = rot_df.columns.str.lower() oris, ori0 = [], rc.Camera().orientation0 for _, row in rot_df.iterrows(): oris.append(rc.RotationQuaternion(**row).rotate(ori0)) odf = pd.DataFrame(oris, columns=['X', 'Y', 'Z'], index=rot_df.index) f.create_dataset(name=obj.parent.name + '/Orientation', data=odf.to_records(), compression='gzip', compression_opts=7) return None
def __init__(self): super(World, self).__init__(); self.scene = rc.Scene(meshes=[],camera=rc.Camera(orientation0=(0, 0, -1),rotation=(0, 0, 0))) self.scene.bgColor = 33, 33, 33 self.name = 'World'; self.objs = []; self.objs_num = len(self.objs); self.rules = []; self.rules_num = len(self.rules); self.size = 32;
def view_mesh(body, obj_filename): # """Displays mesh in .obj file. Useful for checking that files are rendering properly.""" reader = rc.WavefrontReader(obj_filename) mesh = reader.get_mesh(body, position=(0, 0, -1)) print(mesh.vertices.shape) mesh.scale.x = .2 / np.ptp(mesh.vertices, axis=0).max() camera = rc.Camera(projection=rc.PerspectiveProjection(fov_y=20)) light = rc.Light(position=(camera.position.xyz)) scene = rc.Scene(meshes=[mesh], camera=camera, light=light, bgColor=(.2, .4, .2)) scene.gl_states = scene.gl_states[:-1] display = pyglet.window.get_platform().get_default_display() screen = display.get_screens()[0] window = pyglet.window.Window(fullscreen=True, screen=screen) fbo = rc.FBO(rc.Texture(width=4096, height=4096)) quad = rc.gen_fullscreen_quad() quad.texture = fbo.texture label = pyglet.text.Label() @window.event def on_draw(): with rc.resources.genShader, fbo: scene.draw() with rc.resources.deferredShader: quad.draw() verts_mean = np.ptp(mesh.vertices, axis=0) label.text = 'Name: {}\nRotation: {}\nSize: {} x {} x {}'.format( mesh.name, mesh.rotation, verts_mean[0], verts_mean[1], verts_mean[2]) label.draw() @window.event def on_resize(width, height): camera.projection.aspect = float(width) / height @window.event def on_mouse_motion(x, y, dx, dy): x, y = x / float(window.width) - .5, y / float(window.height) - .5 mesh.rotation.x = -360 * y mesh.rotation.y = 360 * x pyglet.app.run()
def main(): FULLSCREEN = True reader = rc.WavefrontReader('resources/maze.obj') arena = reader.get_mesh('Cube') arena.textures.append(rc.Texture.from_image(rc.resources.img_uvgrid)) sphere = reader.get_mesh('Sphere') # , position=(0, 0, -1)) cylinder = reader.get_mesh('Cylinder') # , position=(0, 0, -1)) player = rc.Camera(projection=rc.PerspectiveProjection(z_near=.001, z_far=4.5), position=(0, .3, 0)) player.rotation.axes = 'sxyz' scene = rc.Scene(meshes=[arena, sphere, cylinder], camera=player, bgColor=(1., 0., 0.)) #scene.gl_states = scene.gl_states[:-1] window = FPSGame(player=player, scene=scene, fullscreen=FULLSCREEN) pyglet.app.run()
raise KeyError("Rigid Body {} not detected. Add it in Motive, so we can track it!".format(RIGID_BODY_NAME)) print(tracker.rigid_bodies) # Load up Projector with open('projector_data.pickle') as f: projdict = pickle.load(f) modelmat = np.identity(4) modelmat[:-1, :-1] = -projdict['rotation'] rot = np.degrees(trans.euler_from_matrix(modelmat, 'rxyz')) print(modelmat) print(rot) projector = rc.Camera(fov_y=projdict['fov_y'], position=projdict['position']) # projector.rot_x = -90 projector.rot_x = rot[0] - 1. projector.rot_y = rot[1] projector.rot_z = 180 - rot[2] # projector._rot_matrix = projdict['rotation'] display = pyglet.window.get_platform().get_default_display() screen = display.get_screens()[1] window = pyglet.window.Window(vsync=True, fullscreen=True, screen=screen) reader = rc.WavefrontReader(rc.resources.obj_primitives) sphere = reader.get_mesh('Sphere', scale=.01) sphere.position = rbody.position print(sphere.uniforms) sphere.uniforms['flat_shading'] = 1
obj_reader = rc.WavefrontReader(obj_filename2) # Create Mesh thor = obj_reader.get_mesh("thor") # Seta a posição inicial, escala, ? thor.position.xyz = 0, 0, -2 scale = .1 thor.scale = scale thor.uniforms['diffuse'] = [.5, .0, .8] # Cria a cena scene = rc.Scene(meshes=[thor]) scene.bgColor = 0.4, 0.2, 0.4 scene.light.position = 0, 3, -1 # Cria a camera camera = rc.Camera(projection=rc.PerspectiveProjection(fov_y=90, aspect=1.)) scene.camera = camera #Faz a projeção da cena projected_scene = rc.Scene(meshes=[thor], bgColor=(1., 1., 1.)) projected_scene.light.position = scene.light.position projected_scene.camera = rc.Camera(position=(0, 0, 5), rotation=(0, 0, 0)) projected_scene.camera.projection.z_far = 50 # Atualiza a movimentação da camera e do objeto def move_camera(dt): global scale global projected_scene camera_speed = 1 if keys[key.W]: thor.scale.x += 0.1
} """ shader = rc.Shader(vert=vert_shader, frag=frag_shader) plane = rc.WavefrontReader(rc.resources.obj_primitives).get_mesh( 'Plane') #gen_fullscreen_quad() plane.scale.xyz = .2 plane.position.xyz = 0, 0, -1 plane.uniforms['theta'] = 0. plane.uniforms['width'] = .01 window = pyglet.window.Window(fullscreen=True) scene = rc.Scene(meshes=[plane], bgColor=(.5, .5, .5), camera=rc.Camera(projection=rc.OrthoProjection())) # Draw Function @window.event def on_draw(): with shader: scene.draw() def update(dt): plane.uniforms['theta'] += dt * 7. pyglet.clock.schedule(update)
cylinder.uniforms['flat_shading'] = True cylinder.point_size = .02 cylinder.position.x = -.3 cylinder.scale.xyz = .5 cyl2 = cylinder.copy() cyl2.position.x = 0 # cyl2.rotation.x = 20 cyl3 = cylinder.copy() cyl3.position.x = .3 cyl3.rotation.y = 30 win = pyglet.window.Window(fullscreen=True) scene = rc.Scene(meshes=[cylinder, cyl2, cyl3], bgColor=(0, 0, 0), camera=rc.Camera(projection=rc.OrthoProjection(coords='relative'))) fps_label = pyglet.window.FPSDisplay(window=win) @win.event def on_draw(): with rc.default_shader: scene.draw() fps_label.draw() # def update(dt): cylinder.rotation.x += 100 * dt cyl2.rotation.y += 100 * dt cyl3.rotation.z += 100 * dt pyglet.clock.schedule(update)
def main(): #gettign positions of rigib bodies in real time client = NatClient() arena_rb = client.rigid_bodies['Arena'] rat_rb = client.rigid_bodies['Rat'] window = pyglet.window.Window(resizable=True, fullscreen=True, screen=get_screen(1)) # Opening the basic pyglet window # Load Arena remove_image_lines_from_mtl('assets/3D/grass_scene.mtl') arena_filename = 'assets/3D/grass_scene.obj'# we are taking an arena which has been opened in blender and rendered to 3D after scanning it does not have flipped normals arena_reader = rc.WavefrontReader(arena_filename) # loading the mesh of the arena thought a wavefrontreader arena = arena_reader.get_mesh("Arena", position=arena_rb.position) # making the wafrotn into mesh so we can extrude texture ont top of it. arena.uniforms['diffuse'] = 1., 1., 1. # addign a white diffuse material to the arena arena.rotation = arena.rotation.to_quaternion() # we also need to get arena's rotation not just xyz so it can be tracked and moved if it gets bumped # Load the projector as a Ratcave camera, set light to its position projector = rc.Camera.from_pickle('assets/3D/projector.pkl') # settign the pickle filled of the projector, which gives us the coordinates of where the projector is projector.position.x += .004 projector.projection = rc.PerspectiveProjection(fov_y =40.5, aspect=1.777777778) light = rc.Light(position=projector.position) ## Make Virtual Scene ## fields = [] for x, z in itertools.product([-.8, 0, .8], [-1.6, 0, 1.6]): field = load_textured_mesh(arena_reader, 'grass', 'grass.png') field.position.x += x field.position.z += z fields.append(field) ground = load_textured_mesh(arena_reader, 'Ground', 'dirt.png') sky = load_textured_mesh(arena_reader, 'Sky', 'sky.png') snake = load_textured_mesh(arena_reader, 'Snake', 'snake.png') rat_camera = rc.Camera(projection=rc.PerspectiveProjection(aspect=1, fov_y=90, z_near=.001, z_far=10), position=rat_rb.position) # settign the camera to be on top of the rats head meshes = [ground, sky, snake] + fields for mesh in meshes: mesh.uniforms['diffuse'] = 1., 1., 1. mesh.uniforms['flat_shading'] = False mesh.parent = arena virtual_scene = rc.Scene(meshes=meshes, light=light, camera=rat_camera, bgColor=(0, 0, 255)) # seetign aset virtual scene to be projected as the mesh of the arena virtual_scene.gl_states.states = virtual_scene.gl_states.states[:-1] ## Make Cubemapping work on arena cube_texture = rc.TextureCube(width=4096, height=4096) # usign cube mapping to import eh image on the texture of the arena framebuffer = rc.FBO(texture=cube_texture) ## creating a fr`amebuffer as the texture - in tut 4 it was the blue screen arena.textures.append(cube_texture) # Stereo vr_camgroup = rc.StereoCameraGroup(distance=.05) vr_camgroup.rotation = vr_camgroup.rotation.to_quaternion() # updating the posiotn of the arena in xyz and also in rotational perspective def update(dt): """main update function: put any movement or tracking steps in here, because it will be run constantly!""" vr_camgroup.position, vr_camgroup.rotation.xyzw = rat_rb.position, rat_rb.quaternion # setting the actual osiont of the rat camera to vbe of the rat position arena.uniforms['playerPos'] = rat_rb.position arena.position, arena.rotation.xyzw = arena_rb.position, arena_rb.quaternion arena.position.y -= .02 pyglet.clock.schedule(update) # making it so that the app updates in real time @window.event def on_draw(): ## Render virtual scene onto cube texture with framebuffer: with cube_shader: for mask, camside in zip([(True, False, False, True), (False, True, True, True)], [vr_camgroup.left, vr_camgroup.right]): gl.glColorMask(*mask) virtual_scene.camera.position.xyz = camside.position_global virtual_scene.draw360_to_texture(cube_texture) ## Render real scene onto screen gl.glColorMask(True, True, True, True) window.clear() with cube_shader: # usign cube shader to create the actuall 6 sided virtual cube which gets upated with position and angle of the camera/viewer rc.clear_color(255, 0, 0) # why is it here 39? e with projector, light: arena.draw() # actually run everything. pyglet.app.run()
import pyglet import ratcave as rc import numpy as np from numpy.random import random n_points = 2000 width, height = 0.2, 0.5 theta = random(n_points) * np.pi * 2 verts = np.vstack((np.sin(theta) * width, (random(n_points) - .5) * height, np.cos(theta) * width)).T cylinder = rc.Mesh.from_incomplete_data(verts, drawmode=rc.GL_POINTS, position=(0, 0, -2), point_size=1, mean_center=False) cylinder.uniforms['diffuse'] = 1., 1., 1. cylinder.uniforms['flat_shading'] = True scene = rc.Scene(meshes=[cylinder], bgColor=(0., 0, 0), camera=rc.Camera(projection=rc.OrthoProjection())) win = pyglet.window.Window() @win.event def on_draw(): with rc.default_shader: scene.draw() def update(dt): theta = random(n_points) * np.pi * 2 verts = np.vstack((np.sin(theta) * width, (random(n_points) - .5) * height, np.cos(theta) * width)).T cylinder.vertices = verts pyglet.clock.schedule_interval(update, 1/60.)
) # we also need to get arena's rotation not just xyz so it can be tracked and moved if it gets bumped # Load the projector as a Ratcave camera, set light to its position projector = rc.Camera.from_pickle( 'calibration_assets/projector.pkl' ) # settign the pickle filled of the projector, which gives us the coordinates of where the projector is projector.projection = rc.PerspectiveProjection(fov_y=41.5, aspect=1.777777778) light = rc.Light(position=projector.position) ## Make Virtual Scene ## virtual_arena = arena_reader.get_mesh('Arena') wall = arena_reader.get_mesh("Plane") wall.parent = virtual_arena rat_camera = rc.Camera(projection=rc.PerspectiveProjection(aspect=1, fov_y=90, z_near=.001), position=rat_rb.position ) # settign the camera to be on top of the rats head virtual_scene = rc.Scene( meshes=[wall, virtual_arena], light=light, camera=rat_camera, bgColor=(0, 0, 255) ) # seetign aset virtual scene to be projected as the mesh of the arena virtual_scene.gl_states.states = virtual_scene.gl_states.states[:-1] checkerboard_texture = rc.Texture.from_image( rc.resources.img_uvgrid) # or 'assets/wood.jpg' wall.textures.append(checkerboard_texture) virtual_arena.textures.append(checkerboard_texture) virtual_arena.uniforms['flat_shading'] = True
print(ID, model, mesh, pos) pos = pos.split(',') pos = [float(i) for i in pos] return Obj_Attr( rc.WavefrontReader(model).get_mesh(mesh, position=(pos[0], pos[1], -1.5), scale=.03, rotation=(0, 0, 0)), pos, 0, 0, 0) make_world_from_fw('sample_1.fw') meshes = [i.mesh for i in objs] print len(objs) print 'ok' scene = rc.Scene(meshes=meshes, camera=rc.Camera(orientation0=(0, 0, -1), rotation=(0, 0, 0))) scene.bgColor = 33, 33, 33 def move_camera(dt): camera_speed = 3 if keys[key.LEFT]: scene.camera.position.x -= camera_speed * dt if keys[key.RIGHT]: scene.camera.position.x += camera_speed * dt if keys[key.UP]: scene.camera.position.z += camera_speed * dt if keys[key.DOWN]: scene.camera.position.z -= camera_speed * dt if keys[key.K]: scene.camera.position.y += camera_speed * dt
cylinder.dynamic = True cyl2 = cylinder.copy() cyl2.position.x = 0 # cyl2.rotation.x = 20 cyl3 = cylinder.copy() cyl3.position.x = .3 cyl3.rotation.y = 30 win = pyglet.window.Window(fullscreen=True) scene = rc.Scene( meshes=[cylinder, cyl2, cyl3], bgColor=(0, 0, 0), camera=rc.Camera(projection=rc.OrthoProjection(coords='relative'))) fps_label = pyglet.window.FPSDisplay(window=win) @win.event def on_draw(): with rc.default_shader: scene.draw() fps_label.draw() # def update(dt): cylinder.rotation.x += 100 * dt cyl2.rotation.y += 100 * dt
import numpy as np import csv import config # Make Psychopy visual stimuli window = visual.Window(fullscr=config.FULLSCREEN, allowStencil=True, color=config.BGCOLOR) fixcross = visual.TextStim(window, text='+', alignVert='center', alignHoriz='center') # Make Ratcave stimuli reader = rc.WavefrontReader('stimuli/rotstim.obj') stim_orig = reader.get_mesh('OrigStim', position=(-.25, 0., -1), rotation=(0, config.YROT, 0), scale=config.SCALE) stim_orig2 = reader.get_mesh('OrigStim', position = (.25, 0., -1), rotation=(0, config.YROT, 0), scale=config.SCALE) stim_flipped = reader.get_mesh('FlippedStim', position = (.25, 0., -1), rotation=(0, config.YROT, 0), scale=config.SCALE) scene = rc.Scene(meshes=[], camera=rc.Camera(projection=rc.OrthoProjection(origin='center', coords='relative'))) scene.light.position.z = 20 # Make Groups of Stimuli to refer to in experiment stims = [stim_orig, stim_orig2, stim_flipped] condA = [stim_orig, stim_orig2] condB = [stim_orig, stim_flipped] def show_instructions(): msg = """ 3D Mental Rotation Study Press the left arrow if the two objects can be rotated into the same configuration, and the right if they are different.
def get_cubecamera(z_near=.004, z_far=1.5): """Returns a ratcave.Camera instance with fov_y=90 and aspect=1. Useful for dynamic cubemapping.""" camera = rc.Camera(projection=rc.PerspectiveProjection( fov_y=90., aspect=1., z_near=z_near, z_far=z_far)) camera.rotation = camera.rotation.to_quaternion() return camera
# Add the ball # initialize the movement of the ball x = np.random.random() * 6 - 3 y = np.random.randint(-3, 3) angle = np.random.randint(150, 210) ball = Ball(x=x, y=y, color=(0.6, 0, 0), angle=angle) print(ball.angle, sind(ball.angle)) #ball_sound = pyglet.media.load('PPB.wav', streaming=False) # Create Scene scene = rc.Scene(meshes=[ball.mesh, bat.mesh, bat2.mesh]) scene.bgColor = 0, 0, 0.2 # set the background of thee scene # scene.light.position.xyz = 0, 0, -9 scene.camera = rc.Camera(position=(0, 0, 2), rotation=(0, 0, 0)) scene.camera.projection.z_far = 10 ''' there is a need for a function that gives the dimension of the object and its position withing the scene in terms of pixels, because then it's much easier to relate it to the window size ''' ''' can I change the width and height of the cube here? ''' def checkBounce(ballPos, ballRadius, batPos, batDim): result = [1, 1, 1, 1] # left, right, up, down # left side (x is -ve)
def main(): display = pyglet.window.get_platform().get_default_display() screens = display.get_screens() window2 = pyglet.window.Window(vsync=True, resizable=True, screen=screens[0]) window = pyglet.window.Window(vsync=True, resizable=True, fullscreen=True, screen=screens[1]) shader = rc.Shader.from_file(*rc.resources.genShader) keys = key.KeyStateHandler() window.push_handlers(keys) def update(dt): pos_speed = .2 rotation_speed = 15. if keys[key.LEFT]: #projector.position.x -= pos_speed * dt dx, dy, dz = projector.rotation.rotate([-1., 0., 0.]) # projector.position.y += pos_speed * dt projector.position.x += dx * pos_speed * dt projector.position.y += dy * pos_speed * dt projector.position.z += dz * pos_speed * dt if keys[key.RIGHT]: #projector.position.x += pos_speed* dt dx, dy, dz = projector.rotation.rotate([1., 0., 0.]) # projector.position.y += pos_speed * dt projector.position.x += dx * pos_speed * dt projector.position.y += dy * pos_speed * dt projector.position.z += dz * pos_speed * dt if keys[key.DOWN]: #projector.position.z -= pos_speed * dt dx, dy, dz = projector.rotation.rotate([0., 0., -1.]) # projector.position.y += pos_speed * dt projector.position.x += dx * pos_speed * dt projector.position.y += dy * pos_speed * dt projector.position.z += dz * pos_speed * dt if keys[key.UP]: # projector.position.z += pos_speed * dt dx, dy, dz = projector.rotation.rotate([0., 0., 1.]) # projector.position.y += pos_speed * dt projector.position.x += dx * pos_speed * dt projector.position.y += dy * pos_speed * dt projector.position.z += dz * pos_speed * dt if keys[key.PAGEDOWN]: # projector.position.y -= pos_speed * dt dx, dy, dz = projector.rotation.rotate([0., -1., 0.]) # projector.position.y += pos_speed * dt projector.position.x += dx * pos_speed * dt projector.position.y += dy * pos_speed * dt projector.position.z += dz * pos_speed * dt if keys[key.PAGEUP]: dx, dy, dz = projector.rotation.rotate([0., 1., 0.]) # projector.position.y += pos_speed * dt projector.position.x += dx * pos_speed * dt projector.position.y += dy * pos_speed * dt projector.position.z += dz * pos_speed * dt if keys[key.W]: projector.rotation.x += rotation_speed * dt if keys[key.S]: projector.rotation.x -= rotation_speed * dt if keys[key.A]: projector.rotation.y += rotation_speed * dt if keys[key.D]: projector.rotation.y -= rotation_speed * dt pyglet.clock.schedule(update) grating_file = rc.resources.obj_primitives grating_reader = rc.WavefrontReader(grating_file) grating = grating_reader.get_mesh("Circle", position=(0, 0, .4), scale=.1) grating.rotation.xyz = (180, 0, 0) text = pyglet.text.Label(text='Hello', y=window2.height / 2., x=window2.width / 4., multiline=True, width=800) format_str = """ Calibration Coordinates ----------------------- Projector Position [Arrow + PageUp, PageDown]: (x={:.2f}, y={:.2f}, z={:.2f}) Projector Rotation [W/A/S/D]: (x={:.2f}, y={:.2f}, z={:.2f}) """ projector = rc.Camera(rotation=(180, 0, 0)) projector.projection.fov_y = 28.3 # field of view setting scene = rc.Scene(meshes=[grating], camera=projector) # variable name goes inside meshes #scene.bgColor = .6, .5, .4 @window.event def on_draw(): with shader: scene.draw() @window2.event def on_draw(): window2.clear() text.draw() @window.event def on_resize(width, height): projector.projection.aspect = float(width) / height @window.event def on_mouse_drag(x, y, dx, dy, buttons, modifiers): rot_speed = .2 projector.rotation.x += dx * rot_speed projector.rotation.y += dy * rot_speed def update_text(dt): pos = projector.position rot = projector.rotation text.text = format_str.format(pos.x, pos.y, pos.z, rot.x, rot.y, rot.z) pyglet.clock.schedule(update_text) pyglet.app.run()
import ratcave as rc import pyglet window = pyglet.window.Window(resizable=True, width=400, height=800) pyglet.clock.schedule(lambda dt: dt) cube = rc.WavefrontReader(rc.resources.obj_primitives).get_mesh('Cube', scale=.02) cube.position.xyz = .5, .5, -2 cam = rc.Camera() cam.projection = rc.OrthoProjection(coords='relative', origin='corner') @window.event def on_draw(): with rc.default_shader, rc.default_states, cam: cube.draw() @window.event def on_resize(width, height): # cam.projection.match_aspect_to_viewport() cam.projection.aspect = width / height # cam.reset_uniforms() # cam.projection.projection_matrix[0, 0] += .01 print(cam.projection.viewport) print(cam.projection_matrix) print(cam.uniforms['projection_matrix']) pyglet.app.run()
SHRINKING = 2 PLASMA = 3 # pyglet inits window = pyglet.window.Window(width=1280, height=960, screen=0) keys = key.KeyStateHandler() window.push_handlers(keys) # ratcave inits default = rc.WavefrontReader(rc.resources.obj_primitives) scene = rc.Scene( meshes=[], bgColor=(45 / 255, 45 / 255, 45 / 255), light=rc.Light(position=(0, 10_000, 0)), camera=rc.Camera(position=(0, 7, 7), rotation=(-45, 0, 0))) class Planet: def __init__(self, diameter, pos=(0, 0, 0), day=24, period=None, name="", children=None, schedule=False): """ :param diameter: radius of planet (0-1, not to scale) :param pos: distance from the sun in 3 axis (AU) :param period: orbital period (days to encircle the parent-celestial-object), 365.2 in earth's case :param day: hours in this object's day """