def update_recording(scn): global records, timer if records is None: records = {} timer = 0 record = {} record['cam'] = serialize_matrix( scn.GetCurrentCamera().GetTransform().GetWorld()) head_controller = gs.GetInputSystem().GetDevice("openvr_hmd") if head_controller is not None: record['head_controller'] = serialize_matrix( head_controller.GetMatrix(gs.InputDevice.MatrixHead)) controller0 = gs.GetInputSystem().GetDevice("openvr_controller_0") if controller0 is not None: record['controller_0'] = serialize_matrix( controller0.GetMatrix(gs.InputDevice.MatrixHead)) controller1 = gs.GetInputSystem().GetDevice("openvr_controller_1") if controller1 is not None: record['controller_1'] = serialize_matrix( controller1.GetMatrix(gs.InputDevice.MatrixHead)) if gs.GetPlus().KeyDown(gs.InputDevice.KeyN): record['clap'] = True records[str(timer)] = record timer += gs.GetPlus().GetClockDt().to_sec()
def calibrate_offset(scn): global calibration_matrix temp_timer = 1 with open("axe_x.json", 'r') as outfile: axe_json = json.load(outfile) record = axe_json[min(axe_json.keys(), key=lambda k: abs(float(k) - temp_timer))] controller_mat_vr = gs.Matrix4.TranslationMatrix( deserialize_matrix(record["controller_1"]).GetTranslation()) origin_vr = deserialize_matrix(record["controller_0"]).GetTranslation() axe_x = ( deserialize_matrix(record["head_controller"]).GetTranslation() - origin_vr).Normalized() temp_timer = 5 with open("axe_z.json", 'r') as outfile: axe_json = json.load(outfile) record = axe_json[min(axe_json.keys(), key=lambda k: abs(float(k) - temp_timer))] axe_z = ( deserialize_matrix(record["head_controller"]).GetTranslation() - origin_vr).Normalized() axe_y = axe_x.Cross(axe_z) # change origin origin_vr_rotation_mat = gs.Matrix3() origin_vr_rotation_mat.SetX(axe_x) origin_vr_rotation_mat.SetY(axe_y) origin_vr_rotation_mat.SetZ(axe_z) origin_vr_mat = gs.Matrix4.TransformationMatrix(origin_vr, origin_vr_rotation_mat) controller_mat = origin_vr_mat.InversedFast() * controller_mat_vr gs.GetPlus().AddCube(scn, controller_mat, 0.1, 0.1, 0.1) cam_mat = gs.Matrix4.TranslationMatrix((3.75, 1.23, 3.02)) #cam_mat = cam_mat.LookAt(gs.Vector3(0, 0, 0)) gs.GetPlus().AddCube(scn, cam_mat, 0.1, 0.1, 0.1, "assets/material_diffuse_color_blue.mat") #gs.GetPlus().AddCube(scn, gs.Matrix4.Identity, 0.1, 0.1, 0.1, "assets/material_diffuse_color_green.mat") #gs.GetPlus().AddCube(scn, origin_vr_mat, 0.1, 0.1, 0.1) gs.GetPlus().AddCube( scn, gs.Matrix4.TransformationMatrix(origin_vr, gs.Matrix3.LookAt(axe_x, (0, 1, 0))), 0.01, 0.01, 10, "assets/material_diffuse_color_red.mat") gs.GetPlus().AddCube( scn, gs.Matrix4.TransformationMatrix(origin_vr, gs.Matrix3.LookAt(axe_y, (1, 0, 0))), 0.01, 0.01, 10, "assets/material_diffuse_color_green.mat") gs.GetPlus().AddCube( scn, gs.Matrix4.TransformationMatrix(origin_vr, gs.Matrix3.LookAt(axe_z, (0, 1, 0))), 0.01, 0.01, 10, "assets/material_diffuse_color_blue.mat")
def update(scn, openvr_frame_renderer): if calibration_matrix is None: load_calibration() if recording: update_recording(scn) elif playing or playing_record_frame or do_calibration: update_play(scn, openvr_frame_renderer) if show_live_cam: plus = gs.GetPlus() size = plus.GetRendererAsync().GetCurrentOutputWindow().GetSize() if openvr_frame_renderer is not None: scn.GetSystem("Renderable").SetFrameRenderer(None) plus.GetRendererAsync().SetViewport( gs.fRect(0, 0, size.x * 0.5, size.y * 0.5)) plus.GetRendererAsync().SetClippingRect( gs.fRect(0, 0, size.x * 0.5, size.y * 0.5)) plus.GetRendererAsync().Clear(gs.Color.Black) scn.GetCurrentCamera().GetTransform().SetWorld(saved_cam_matrix) scn.Update(gs.time(0)) scn.WaitUpdate() scn.Commit() scn.WaitCommit() plus.GetRendererAsync().SetViewport(gs.fRect(0, 0, size.x, size.y)) plus.GetRendererAsync().SetClippingRect(gs.fRect(0, 0, size.x, size.y)) if openvr_frame_renderer is not None: scn.GetSystem("Renderable").SetFrameRenderer(openvr_frame_renderer)
def engine_init(): global al, channel, plus try: gs.LoadPlugins(gs.get_default_plugins_path()) except: pass plus = gs.GetPlus() plus.RenderInit(demo_screen_size[0], demo_screen_size[1], 1, demo_screen_size[2]) # mount the system file driver gs.MountFileDriver(gs.StdFileDriver("assets/"), "@assets/") al = None channel = None
def update(scn, gui, openvr_frame_renderer): global timer if playing: timer += gs.GetPlus().GetClockDt().to_sec() # find object and update there position for key, obj in animations.items(): if key == "camera": node = scn.GetCurrentCamera() else: node = scn.GetNode(key) if node is not None: # get anim key key_a = 0 for i in range(len(obj)): if obj[i]["time"] > timer: key_a = i - 1 break # animate only if there is key after to interpolate if 0 <= key_a < len(obj)-1: # get the 2 key node node_a = scn.GetNode(obj[key_a]["object_name"]) node_b = scn.GetNode(obj[key_a + 1]["object_name"]) if node_a is not None and node_b is not None: # interpolate between the 2 key pos_a = node_a.GetTransform().GetWorld().GetTranslation() quat_a = gs.Quaternion.FromMatrix3(node_a.GetTransform().GetWorld().GetRotationMatrix()) pos_b = node_b.GetTransform().GetWorld().GetTranslation() quat_b = gs.Quaternion.FromMatrix3(node_b.GetTransform().GetWorld().GetRotationMatrix()) time_a = obj[key_a]["time"] time_b = obj[key_a+1]["time"] t = range_adjust(timer, time_a, time_b, 0, 1) pos = pos_a + (pos_b - pos_a) * t quat = gs.Quaternion.Slerp(t, quat_a, quat_b) # if it's for the camera and in case of no vr, up the camera to 1.75 if key == "camera" and openvr_frame_renderer is None: pos.y = pos.y + 1.75 node.GetTransform().SetWorld(gs.Matrix4.TransformationMatrix(pos, quat.ToMatrix3()))
import gs plus = gs.GetPlus() controller_nodes = [] helmet_node = None create_nodes_controller = True def clear_controllers(scn): global controller_nodes, helmet_node for node in controller_nodes: scn.RemoveNode(node) if helmet_node is not None: scn.RemoveNode(helmet_node) controller_nodes = [] helmet_node = None def create_helmet(scn): return plus.AddGeometry(scn, "vr_helmet/generic_hmd_generic_hmd_mesh.geo") def create_controller(scn): return plus.AddGeometry(scn, "vr_controller/whole_model_group1.geo") def update_controller(scn): global controller_nodes, helmet_node if scn.GetCurrentCamera() is None or not create_nodes_controller:
def update_play(scn, openvr_frame_renderer): global timer, playing, counter_frame if records is not None: nearest_key = min(records.keys(), key=lambda k: abs(float(k) - timer)) records_list = list(sorted(records)) index = records_list.index(nearest_key) if float(nearest_key) > timer and index - 1 >= 0: first_key = records_list[index - 1] second_key = records_list[index] elif index + 1 < len(records_list): first_key = records_list[index] second_key = records_list[index + 1] else: first_key = records_list[0] second_key = records_list[1] def interpolate_mat(name_record): first_mat = deserialize_matrix(records[first_key][name_record]) second_mat = deserialize_matrix(records[second_key][name_record]) t = (timer - float(first_key)) / (float(second_key) - float(first_key)) pos = first_mat.GetTranslation() + (second_mat.GetTranslation() - first_mat.GetTranslation()) * t rot = gs.Quaternion.Slerp( t, gs.Quaternion.FromMatrix3(first_mat.GetRotationMatrix()), gs.Quaternion.FromMatrix3( second_mat.GetRotationMatrix())).ToMatrix3() return gs.Matrix4.TransformationMatrix(pos, rot) cam = scn.GetCurrentCamera() if cam is not None: if render_head: cam.GetTransform().SetWorld( interpolate_mat('cam') * interpolate_mat('head_controller')) else: cam.GetTransform().SetWorld( interpolate_mat('cam') * interpolate_mat('controller_1') * calibration_matrix) for id_controller, controller in enumerate( vr_controller.controller_nodes): if controller.GetTransform() is not None: if 'controller_{}'.format(id_controller) in records[first_key]: controller.GetTransform().SetWorld( interpolate_mat('cam') * interpolate_mat('controller_{}'.format(id_controller))) if vr_controller.helmet_node is not None and vr_controller.helmet_node.GetTransform( ) is not None: vr_controller.helmet_node.GetTransform().SetWorld( interpolate_mat('cam') * interpolate_mat('head_controller')) if not do_calibration: if record_frame: pic = gs.Picture() if gs.GetPlus().GetRendererAsync().CaptureFramebuffer( pic).get(): if "clap" in records[first_key]: pic.ClearRGBA(1, 0, 1) render_path = "{0}\\{1}".format( records_output_folder, os.path.splitext( os.path.basename(current_filename))[0]) if render_head: render_path = "{0}\\{1}_head".format( records_output_folder, os.path.splitext( os.path.basename(current_filename))[0]) if not os.path.exists(render_path): os.mkdir(render_path) lts.RunTask(task, [ pic, "{0}\\{1}_{2:08d}.jpg".format( render_path, os.path.splitext( os.path.basename(current_filename))[0], counter_frame) ]) counter_frame += 1 timer += 1.0 / fps_record else: timer += gs.GetPlus().GetClockDt().to_sec() if timer > max([float(i) for i in records.keys()]): stop_play(scn, openvr_frame_renderer)
def calibration(scn, openvr_frame_renderer, gui): global calibration_matrix, do_calibration, timer, calibration_fov, show_live_cam, draw_calibration_picture show_live_cam = gui.Checkbox("Show live cam", show_live_cam) temp_calibration = gui.Checkbox("Calibration", do_calibration) if temp_calibration: if not show_live_cam and temp_calibration != do_calibration: load_record(scn, openvr_frame_renderer) # draw alpha picture draw_calibration_picture = gui.Checkbox("Draw calibration picture", draw_calibration_picture) if draw_calibration_picture: gs.GetPlus().Image2D(0, 0, 1.0, "calibration.png", gs.Color(1, 1, 1, 0.5)) if gui.Button("Calibrate live from ground"): controller1 = gs.GetInputSystem().GetDevice("openvr_controller_1") if controller1 is not None: calibration_matrix = controller1.GetMatrix( gs.InputDevice.MatrixHead).InversedFast() delta = 0.01 pos_calibration = calibration_matrix.GetTranslation() if gui.Button("PosX -"): pos_calibration.x -= delta gui.SameLine() if gui.Button("PosX +"): pos_calibration.x += delta gui.SameLine() pos_calibration.x = gui.InputFloat("px", pos_calibration.x)[1] if gui.Button("PosY -"): pos_calibration.y -= delta gui.SameLine() if gui.Button("PosY +"): pos_calibration.y += delta gui.SameLine() pos_calibration.y = gui.InputFloat("py", pos_calibration.y)[1] if gui.Button("PosZ -"): pos_calibration.z -= delta gui.SameLine() if gui.Button("PosZ +"): pos_calibration.z += delta gui.SameLine() pos_calibration.z = gui.InputFloat("pz", pos_calibration.z)[1] # pos_calibration = gui.InputVector3("Pos", pos_calibration)[1] rot_calibration = calibration_matrix.GetRotation() if gui.Button("RotX -"): rot_calibration.x -= delta gui.SameLine() if gui.Button("RotX +"): rot_calibration.x += delta gui.SameLine() rot_calibration.x = gui.InputFloat( "rx", rot_calibration.x * 180.0 / 3.1415)[1] * 3.1415 / 180.0 if gui.Button("RotY -"): rot_calibration.y -= delta gui.SameLine() if gui.Button("RotY +"): rot_calibration.y += delta gui.SameLine() rot_calibration.y = gui.InputFloat( "ry", rot_calibration.y * 180.0 / 3.1415)[1] * 3.1415 / 180.0 if gui.Button("RotZ -"): rot_calibration.z -= delta gui.SameLine() if gui.Button("RotZ +"): rot_calibration.z += delta gui.SameLine() rot_calibration.z = gui.InputFloat( "rz", rot_calibration.z * 180.0 / 3.1415)[1] * 3.1415 / 180.0 calibration_matrix = gs.Matrix4.TransformationMatrix( pos_calibration, rot_calibration) changed, calibration_fov = gui.SliderFloat("FOV (deg)", calibration_fov, 1, 180) if changed: scn.GetCurrentCamera().GetCamera().SetZoomFactor( gs.FovToZoomFactor(calibration_fov * 3.1415 / 180.0)) if records is not None: timer = gui.SliderFloat("Timeline", timer, 0, max([float(i) for i in records.keys()]))[1] else: if temp_calibration != do_calibration: save_calibration() stop_play(scn, openvr_frame_renderer) do_calibration = temp_calibration
def main(): pc_screen_windowed = True pc_screen_width = 1280 pc_screen_height = 720 plus = gs.GetPlus() plus.CreateWorkers() DemoSimulation.print_ascii_intro(None) if getattr(sys, 'frozen', False): # frozen dir_ = dirname(sys.executable) else: # unfrozen dir_ = dirname(realpath(__file__)) window_mode = pymsgbox.confirm(text='Select your screen mode', title='System Zoetrope', buttons=['Windowed', 'Fullscreen']) if window_mode == 'Windowed': pc_screen_windowed = True screen_resolutions = ['640x480', '720x568', '800x600', '1280x800'] elif window_mode == 'Fullscreen': pc_screen_windowed = False screen_resolutions = [ '640x480', '800x600', '1280x720', '1280x800', '1920x1080' ] else: return False screen_res = pymsgbox.confirm(text='Select your screen resolution', title='System Zoetrope', buttons=screen_resolutions) if screen_res is not None: pc_screen_width = int(screen_res.split('x')[0]) pc_screen_height = int(screen_res.split('x')[1]) else: return False demo_screen_width = 720 // 2 demo_screen_height = 568 // 2 amiga_screen_ratio = demo_screen_height / demo_screen_width overscan_factor = gs.Vector4( 16 / demo_screen_width, 4 / demo_screen_height, (demo_screen_width - 16) / demo_screen_width, (demo_screen_height - 28) / demo_screen_height) # in pixels if pc_screen_windowed: pc_screen_width = int(pc_screen_height * (demo_screen_width / demo_screen_height)) # mount the system file driver # gs.GetFilesystem().Mount(gs.StdFileDriver("pkg.core"), "@core") gs.MountFileDriver(gs.StdFileDriver()) gs.LoadPlugins(gs.get_default_plugins_path()) # create the renderer and render system if pc_screen_windowed: w_mode = gs.Window.Windowed else: w_mode = gs.Window.Fullscreen plus.RenderInit(pc_screen_width, pc_screen_height, 1, w_mode) egl = plus.GetRendererAsync() # create the font object font = gs.RasterFont("@core/fonts/default.ttf", 48, 512) # Init demo simulation demo = DemoSimulation(demo_screen_width, demo_screen_height) # # load a simple 2d shader outputting a single color # shader = egl.LoadShader("res/shader_2d_single_texture.isl") # # # Create index buffer # data = gs.BinaryBlob() # data.WriteShorts([0, 1, 2, 0, 2, 3]) # # idx = egl.NewBuffer() # egl.CreateBuffer(idx, data, gs.GpuBuffer.Index) # # # Create vertex buffer # # Create vertex buffer # vtx_layout = gs.VertexLayout() # vtx_layout.AddAttribute(gs.VertexAttribute.Position, 3, gs.VertexFloat) # vtx_layout.AddAttribute(gs.VertexAttribute.UV0, 2, gs.VertexUByte, True) # UVs are sent as normalized 8 bit unsigned integer (range [0;255]) # # def custom_uv(u, v): # return [int(Clamp(u, 0, 1) * 255), int(Clamp(v, 0, 1) * 255)] # # data = gs.BinaryBlob() # x, y = 1, 1 # data.WriteFloats([-x, -y, 0.5]) # data.WriteUnsignedBytes(custom_uv(overscan_factor.x, overscan_factor.w)) # data.WriteFloats([-x, y, 0.5]) # data.WriteUnsignedBytes(custom_uv(overscan_factor.x, overscan_factor.y)) # data.WriteFloats([x, y, 0.5]) # data.WriteUnsignedBytes(custom_uv(overscan_factor.z, overscan_factor.y)) # data.WriteFloats([x, -y, 0.5]) # data.WriteUnsignedBytes(custom_uv(overscan_factor.z, overscan_factor.w)) # # vtx = egl.NewBuffer() # egl.CreateBuffer(vtx, data, gs.GpuBuffer.Vertex) # demo bitmaps demo.load_textures() demo_screen_tex = egl.NewTexture("demo_screen_texture") res = egl.CreateTexture(demo_screen_tex, demo.screen_pic) print("CreateTexture() returned ", res) # play music al = gs.MixerAsync(gs.ALMixer()) al.Open() channel_state = gs.MixerChannelState(0, 1, gs.MixerRepeat) al.Stream("res/music_loop.ogg", channel_state) mode_switch = "DMODE_SW_UBOB" while not plus.IsAppEnded( plus.EndOnDefaultWindowClosed) and not plus.KeyPress( gs.InputDevice.KeyEscape): dt = plus.UpdateClock() plus.Clear() # Demo simulation (re-creation) demo.update_dt(dt.to_sec()) demo.clear_screen() demo.draw_pixel_art_logo() demo.draw_checkerboard() if mode_switch == "DMODE_SW_UBOB": if not demo.draw_unlimited_bobs(): if demo.figure_mode % 2 == 0: mode_switch = "DMODE_SW_CLEAR_FROM_TOP" else: mode_switch = "DMODE_SW_CLEAR_FROM_BOTTOM" elif mode_switch == "DMODE_SW_CLEAR_FROM_TOP": if demo.clear_playfield(True): mode_switch = "DMODE_SW_NEXT_UBOB" elif mode_switch == "DMODE_SW_CLEAR_FROM_BOTTOM": if demo.clear_playfield(False): mode_switch = "DMODE_SW_NEXT_UBOB" elif mode_switch == "DMODE_SW_NEXT_UBOB": demo.set_next_unlimited_bobs() mode_switch = "DMODE_SW_UBOB" demo.render_demo_text() egl.BlitTexture(demo_screen_tex, gs.BinaryBlobFromByteArray(demo.screen_pic.GetData()), demo_screen_width, demo_screen_height) if pc_screen_windowed: # egl.SetShader(shader) # egl.SetShaderTexture("u_tex", demo_screen_tex) # egl.DrawBuffers(6, idx, vtx, vtx_layout) plus.Quad2D(0, 0, 0, pc_screen_height, pc_screen_width, pc_screen_height, pc_screen_width, 0, gs.Color.White, gs.Color.White, gs.Color.White, gs.Color.White, demo_screen_tex, overscan_factor.x, overscan_factor.y, overscan_factor.z, overscan_factor.w) else: _x_offset = (pc_screen_width - pc_screen_width * amiga_screen_ratio) * 0.5 plus.Quad2D(_x_offset, 0, _x_offset, pc_screen_height, _x_offset + (pc_screen_width * amiga_screen_ratio), pc_screen_height, _x_offset + (pc_screen_width * amiga_screen_ratio), 0, gs.Color.White, gs.Color.White, gs.Color.White, gs.Color.White, demo_screen_tex, overscan_factor.x, overscan_factor.y, overscan_factor.z, overscan_factor.w) plus.Flip()