def initialize(self, core_manager): logger.info("initialize " + GetClassName(self)) self.core_manager = core_manager self.resource_manager = core_manager.resource_manager self.scene_loader = self.resource_manager.scene_loader self.renderer = core_manager.renderer self.effect_manager = core_manager.effect_manager
def set_window_info(self, width, height, full_screen): changed = False if full_screen: width = self.screen_width height = self.screen_height if 0 < width != self.width: self.width = width changed = True if 0 < height != self.height: self.height = height changed = True if 0 < width and 0 < height: self.aspect = float(width) / float(height) if full_screen != self.full_screen: self.full_screen = full_screen changed = True logger.info("Set Window Info : %d x %d Full Screen (%s)" % (width, height, full_screen)) return changed
def generate_texture(self): logger.info("Generate VectorFieldTexture3D.") core_manager = CoreManager.getInstance() resource_manager = core_manager.resource_manager renderer = core_manager.renderer texture = CreateTexture( name=self.texture_name, texture_type=Texture3D, width=self.texture_width, height=self.texture_height, depth=self.texture_depth, internal_format=GL_RGBA16F, texture_format=GL_RGBA, min_filter=GL_LINEAR, mag_filter=GL_LINEAR, data_type=GL_FLOAT, wrap=GL_CLAMP, ) resource = resource_manager.texture_loader.get_resource( self.texture_name) if resource is None: resource = resource_manager.texture_loader.create_resource( self.texture_name, texture) else: old_texture = resource.get_data() if old_texture is not None: old_texture.delete() resource.set_data(texture) glPolygonMode(GL_FRONT_AND_BACK, renderer.view_mode) glDepthFunc(GL_LEQUAL) glEnable(GL_CULL_FACE) glFrontFace(GL_CCW) glEnable(GL_DEPTH_TEST) glDepthMask(True) glClearColor(0.0, 0.0, 0.0, 1.0) glClearDepth(1.0) renderer.set_blend_state(False) renderer.framebuffer_manager.bind_framebuffer(texture) glClear(GL_COLOR_BUFFER_BIT) material_instance = resource_manager.get_material_instance( 'procedural.vector_field_3d') material_instance.use_program() for i in range(texture.depth): material_instance.bind_uniform_data('depth', i / texture.depth) renderer.framebuffer_manager.bind_framebuffer(texture, target_layer=i) renderer.postprocess.draw_elements() renderer.restore_blend_state_prev() # save resource_manager.texture_loader.save_resource(resource.name)
def __init__(self, material_instance_name, **data): self.valid = False self.isNeedToSave = False logger.info("Load Material Instance : " + material_instance_name) self.name = material_instance_name self.shader_name = data.get('shader_name', 'default') self.material = None self.material_name = data.get('material_name', 'default') self.macros = copy.copy(data.get('macros', OrderedDict())) self.linked_uniform_map = dict() self.linked_material_component_map = dict() self.show_message = {} self.Attributes = Attributes() material = data.get('material') # link uniform_buffers and uniform_data self.set_material(material) if self.material: # and set the loaded uniform data. uniform_datas = data.get('uniform_datas', {}) for data_name, data_value in uniform_datas.items(): self.set_uniform_data_from_string(data_name, data_value) else: logger.error("%s material instance has no material." % self.name) return self.valid = True
def add_atmosphere(self, **atmosphere_data): atmosphere_data['name'] = self.generate_object_name(atmosphere_data.get('name', 'atmosphere')) logger.info("add Atmosphere : %s" % atmosphere_data['name']) atmosphere = Atmosphere(**atmosphere_data) atmosphere.initialize() self.regist_object(atmosphere) return atmosphere
def initialize(self, core_manager): logger.info("ScriptManager::initialize") self.core_manager = core_manager self.renderer = core_manager.renderer self.debug_line_manager = core_manager.debug_line_manager self.game_backend = core_manager.game_backend self.resource_manager = core_manager.resource_manager self.scene_manager = core_manager.scene_manager self.viewport_manager = core_manager.viewport_manager self.main_viewport = core_manager.viewport_manager.main_viewport self.scene_manager.clear_actors() camera_transform = self.scene_manager.main_camera.transform camera_transform.set_pos([4.5, 4.5, 6.8]) camera_transform.set_pitch(5.92) camera_transform.set_yaw(0.67) self.sphere = self.resource_manager.get_model("sphere") self.scene_manager.add_object(model=self.sphere, pos=[2.0, 1.0, 0.5]) self.suzan = self.resource_manager.get_model("suzan") self.scene_manager.add_object(model=self.suzan, pos=[-1.0, 0.5, 2.0]) if not self.core_manager.is_basic_mode: self.skeletal = self.resource_manager.get_model("skeletal") self.scene_manager.add_object(model=self.skeletal, pos=[2.0, 1.0, 3.0], scale=[0.01, 0.01, 0.01])
def delete(self): logger.info("Delete %s" % GetClassName(self)) self.set_color_textures() self.set_depth_texture(None) glDeleteFramebuffers(1, [ self.buffer, ])
def add_main_light(self, **light_data): name = self.generate_object_name(light_data.get('name', 'main_light')) light_data['name'] = name light_data['model'] = self.resource_manager.get_model('Cube') logger.info("add MainLight : %s" % name) light = MainLight(**light_data) self.regist_object(light) return light
def add_light_probe(self, **light_probe_data): name = self.generate_object_name(light_probe_data.get('name', 'light_probe')) light_probe_data['name'] = name light_probe_data['model'] = self.resource_manager.get_model('Cube') logger.info("add Light Probe : %s" % name) light_probe = LightProbe(**light_probe_data) self.regist_object(light_probe) return light_probe
def add_effect(self, **effect_data): name = self.generate_object_name(effect_data.get('name', 'effect')) logger.info("add Particle : %s" % name) effect_data['name'] = name effect_data['effect_info'] = self.resource_manager.get_effect(effect_data.get('effect_info', '')) effect = Effect(**effect_data) self.regist_object(effect) return effect
def add_terrain(self, **object_data): object_data['name'] = self.generate_object_name( object_data.get('name', 'terrain')) logger.info("add Terrain : %s" % object_data['name']) terrain = Terrain(**object_data) if not self.core_manager.is_basic_mode: terrain.initialize() self.regist_object(terrain) return terrain
def add_ocean(self, **object_data): object_data['name'] = self.generate_object_name( object_data.get('name', 'ocean')) logger.info("add Ocean : %s" % object_data['name']) ocean = Ocean(**object_data) if not self.core_manager.is_basic_mode: ocean.initialize() self.regist_object(ocean) return ocean
def open_scene(self, scene_name, scene_data): self.begin_open_scene() self.set_current_scene_name(scene_name) logger.info("Open scene : %s" % scene_name) camera_datas = scene_data.get('cameras', []) for camera_data in camera_datas: self.add_camera(**camera_data) self.main_camera = self.cameras[0] if 0 < len(self.cameras) else None main_light_data = scene_data.get('main_light', None) if main_light_data is not None: self.main_light = self.add_main_light(**main_light_data) else: self.main_light = self.add_main_light() light_datas = scene_data.get('lights', []) for light_data in light_datas: self.add_light(**light_data) light_probe_datas = scene_data.get('light_probes', []) if light_probe_datas: for light_probe_data in light_probe_datas: self.add_light_probe(**light_probe_data) else: self.add_light_probe() self.main_light_probe = self.light_probes[0] if 0 < len( self.light_probes) else None spline_datas = scene_data.get('splines', []) for spline_data in spline_datas: self.add_spline(**spline_data) atmosphere_data = scene_data.get('atmosphere', {}) self.atmosphere = self.add_atmosphere(**atmosphere_data) ocean_data = scene_data.get('ocean', {}) self.ocean = self.add_ocean(**ocean_data) terrain_data = scene_data.get('terrain', {}) self.terrain = self.add_terrain(**terrain_data) for collision_data in scene_data.get('collision_actors', []): self.add_collision(**collision_data) for object_data in scene_data.get('static_actors', []): self.add_object(**object_data) for object_data in scene_data.get('skeleton_actors', []): self.add_object(**object_data) for effect_data in scene_data.get('effects', []): self.add_effect(**effect_data) self.end_open_scene()
def add_spline(self, **spline_data): name = self.generate_object_name(spline_data.get('name', 'spline')) spline_data['name'] = name spline_data['spline_data'] = self.resource_manager.get_spline( spline_data.get('spline_data', '')) logger.info("add Spline : %s" % name) spline = Spline3D(**spline_data) self.regist_object(spline) return spline
def add_camera(self, **camera_data): name = self.generate_object_name(camera_data.get('name', 'camera')) camera_data['name'] = name camera_data['model'] = self.resource_manager.get_model('Cube') logger.info("add Camera : %s" % name) camera = Camera(scene_manager=self, **camera_data) camera.initialize() self.regist_object(camera) return camera
def get_image_data(self, level=0): if self.target not in (GL_TEXTURE_2D, GL_TEXTURE_2D_ARRAY, GL_TEXTURE_3D): return None level = min(level, self.get_mipmap_count()) dtype = get_numpy_dtype(self.data_type) try: glBindTexture(self.target, self.buffer) data = OpenGLContext.glGetTexImage(self.target, level, self.texture_format, self.data_type) # convert to numpy array if type(data) is bytes: data = np.fromstring(data, dtype=dtype) else: data = np.array(data, dtype=dtype) glBindTexture(self.target, 0) return data except: logger.error(traceback.format_exc()) logger.error('%s failed to get image data.' % self.name) logger.info('Try to glReadPixels.') glBindTexture(self.target, self.buffer) fb = glGenFramebuffers(1) glBindFramebuffer(GL_FRAMEBUFFER, fb) data = [] for layer in range(self.depth): if GL_TEXTURE_2D == self.target: glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.buffer, level) elif GL_TEXTURE_3D == self.target: glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, self.buffer, level, layer) elif GL_TEXTURE_2D_ARRAY == self.target: glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, self.buffer, level, layer) glReadBuffer(GL_COLOR_ATTACHMENT0) width, height = self.get_mipmap_size(level) pixels = glReadPixels(0, 0, width, height, self.texture_format, self.data_type) # convert to numpy array if type(pixels) is bytes: pixels = np.fromstring(pixels, dtype=dtype) data.append(pixels) data = np.array(data, dtype=dtype) glBindTexture(self.target, 0) glBindFramebuffer(GL_FRAMEBUFFER, 0) glDeleteFramebuffers(1, [ fb, ]) return data
def initialize(self, core_manager): logger.info("initialize " + GetClassName(self)) self.core_manager = core_manager self.resource_manager = core_manager.resource_manager self.scene_loader = self.resource_manager.scene_loader self.renderer = core_manager.renderer self.effect_manager = core_manager.effect_manager self.axis_gizmo = AxisGizmo( name='axis_gizmo', model=self.resource_manager.get_model('axis_gizmo'))
def add_collision(self, **collision_data): name = self.generate_object_name( collision_data.get('name', 'collision')) mesh = self.resource_manager.get_model(collision_data.get('model')) if mesh is not None: collision_data['name'] = name collision_data['model'] = mesh logger.info("add Collision : %s" % name) collision = CollisionActor(**collision_data) self.regist_object(collision) return collision return None
def __init__(self): logger.info("ScriptManager::__init__") self.core_manager = None self.renderer = None self.debug_line_manager = None self.game_backend = None self.resource_manager = None self.scene_manager = None self.viewport_manager = None self.main_viewport = None self.sphere = None self.suzan = None self.skeletal = None
def exit(self): logger.info("ScriptManager::exit") if self.sphere is not None: self.scene_manager.delete_object(self.sphere.name) self.sphere = None if self.suzan is not None: self.scene_manager.delete_object(self.suzan.name) self.suzan = None if self.skeletal is not None: self.scene_manager.delete_object(self.skeletal.name) self.skeletal = None
def __init__(self, material_name, material_datas={}): self.valid = False logger.info("Load %s material." % material_name) # for save self.material_datas = material_datas shader_codes = material_datas.get('shader_codes') binary_format = material_datas.get('binary_format') binary_data = material_datas.get('binary_data') uniforms = material_datas.get('uniforms', []) uniform_datas = material_datas.get('uniform_datas', {}) self.material_component_names = [ x[1] for x in material_datas.get('material_components', []) ] self.macros = material_datas.get('macros', OrderedDict()) self.is_translucent = True if 0 < self.macros.get( 'TRANSPARENT_MATERIAL', 0) else False self.name = material_name self.shader_name = material_datas.get('shader_name', '') self.program = -1 self.uniform_buffers = dict( ) # OrderedDict() # Declaration order is important. self.Attributes = Attributes() if CoreManager.instance().is_basic_mode: self.valid = True else: if binary_format is not None and binary_data is not None: self.compile_from_binary(binary_format, binary_data) self.valid = self.check_validate() and self.check_linked() if not self.valid: logger.error( "%s material has been failed to compile from binary" % self.name) self.compile_message = "" if not self.valid: self.compile_from_source(shader_codes) self.valid = self.check_validate() and self.check_linked() if not self.valid: logger.error( "%s material has been failed to compile from source" % self.name) if self.valid: self.create_uniform_buffers(uniforms, uniform_datas)
def exit(self): logger.info("GameClient::exit") self.sound_manager.clear() self.resource_manager.sound_loader.clear() self.height_map_infos.clear() self.clear_ui() self.game_ui_manager.destroy() self.actor_manager.destroy() self.bullet_manager.destroy() self.game_backend.set_mouse_grab(False) RenderOption.RENDER_GIZMO = True RenderOption.RENDER_OBJECT_ID = True self.restore_edit_mode()
def get_geometry_data(self): geometry_datas = [] for mesh in self.meshes: positions = [] normals = [] texcoords = [] indices = [] indexMap = {} boundMin = Float3(FLOAT32_MAX, FLOAT32_MAX, FLOAT32_MAX) boundMax = Float3(FLOAT32_MIN, FLOAT32_MIN, FLOAT32_MIN) for n, mesh_indices in enumerate(mesh.indices): # exclude material postionIndicies, normalIndicies, texcoordIndicies = mesh_indices for i in range(len(postionIndicies)): index_key = (postionIndicies[i], normalIndicies[i], texcoordIndicies[i]) if index_key in indexMap: indices.append(indexMap[index_key]) else: indices.append(len(indexMap)) indexMap[index_key] = len(indexMap) positions.append(self.positions[postionIndicies[i]]) normals.append(self.normals[normalIndicies[i]]) texcoords.append(self.texcoords[texcoordIndicies[i]]) # bounding box position = positions[-1] for j in range(3): if boundMin[j] > position[j]: boundMin[j] = position[j] if boundMax[j] < position[j]: boundMax[j] = position[j] if len(positions) == 0: logger.info('%s has a empty mesh. %s' % (self.filename, mesh.name)) continue geometry_data = dict(name=mesh.name, positions=copy.deepcopy(positions), normals=copy.deepcopy(normals), texcoords=copy.deepcopy(texcoords), indices=copy.deepcopy(indices), bound_min=copy.deepcopy(boundMin), bound_max=copy.deepcopy(boundMax), radius=length( np.maximum(abs(boundMax), abs(boundMin)))) geometry_datas.append(geometry_data) return geometry_datas
def add_object(self, **object_data): model = object_data.get('model') if model: object_data['name'] = self.generate_object_name(object_data.get('name', model.name)) objType = GetClassName(model) logger.info("add %s : %s" % (objType, object_data['name'])) if model.mesh and model.mesh.has_bone(): obj_instance = SkeletonActor(**object_data) else: obj_instance = StaticActor(**object_data) # regist self.regist_object(obj_instance) return obj_instance return None
def initialize(self, core_manager): logger.info("Initialize DebugLineManager") self.core_manager = core_manager self.renderer = core_manager.renderer if not core_manager.is_basic_mode: self.debug_line_material = core_manager.resource_manager.get_material_instance( "debug_line") self.debug_line_vertex_buffer = ScreenQuad.get_vertex_array_buffer( ) self.debug_line_instance_buffer = InstanceBuffer( name="debug_line_instance_buffer", location_offset=1, element_datas=self.debug_line_instance_element_data) self.resize_debug_line_instance_data(10)
def exit(self): logger.info("ScriptManager::exit") pybullet.disconnect() for actor_sphere in self.actor_spheres: self.scene_manager.delete_object(actor_sphere.name) self.actor_spheres = [] for actor_suzan in self.actor_suzans: self.scene_manager.delete_object(actor_suzan.name) self.actor_suzans = [] if self.actor_plane is not None: self.scene_manager.delete_object(self.actor_plane.name) self.actor_plane = None
def initialize(self, core_manager): logger.info("ScriptManager::initialize") self.core_manager = core_manager self.renderer = core_manager.renderer self.debug_line_manager = core_manager.debug_line_manager self.game_backend = core_manager.game_backend self.resource_manager = core_manager.resource_manager self.scene_manager = core_manager.scene_manager self.viewport_manager = core_manager.viewport_manager self.main_viewport = core_manager.viewport_manager.main_viewport self.resource_manager.open_scene('physics_scene') camera_transform = self.scene_manager.main_camera.transform camera_transform.set_pos([12.5, 12.5, 13.8]) camera_transform.set_pitch(5.62) camera_transform.set_yaw(0.67) model_plane = self.resource_manager.get_model("Cube") model_sphere = self.resource_manager.get_model("sphere") model_suzan = self.resource_manager.get_model("suzan") mesh_count = 10 self.actor_plane = self.scene_manager.add_object( model=model_plane, pos=[10.0, 0.0, 0.0], scale=[15.0, 0.01, 15.0]) def get_random_pos(): pos = np.random.rand(3) pos[0] = pos[0] * 2.0 - 1.0 pos[1] += np.random.rand() * 6.0 pos[2] = pos[2] * 2.0 - 1.0 return pos * 2.0 for i in range(mesh_count): actor_sphere = self.scene_manager.add_object(model=model_sphere, pos=get_random_pos(), scale=Float3( 0.5, 0.5, 0.5)) actor_suzan = self.scene_manager.add_object(model=model_suzan, pos=get_random_pos(), scale=Float3( 0.5, 0.5, 0.5)) self.actor_spheres.append(actor_sphere) self.actor_suzans.append(actor_suzan) self.initialize_physics()
def __init__(self, name=''): logger.info("Create %s framebuffer" % name) self.name = name self.buffer = None self.max_draw_buffers = glGetInteger(GL_MAX_DRAW_BUFFERS) self.color_textures = [None, ] * self.max_draw_buffers self.attach_count = 0 self.attachments = [0, ] * self.max_draw_buffers self.depth_texture = None self.width = 0 self.height = 0 self.viewport_width = 0 self.viewport_height = 0 self.viewport_scale = 1.0 self.target_face = GL_TEXTURE_CUBE_MAP_POSITIVE_X # cubemap face self.target_layer = 0 # 3d texture layer self.target_level = 0 # mipmap level
def __init__(self): logger.info("ScriptManager::__init__") self.core_manager = None self.renderer = None self.debug_line_manager = None self.game_backend = None self.resource_manager = None self.scene_manager = None self.viewport_manager = None self.main_viewport = None self.actor_plane = None self.actor_spheres = [] self.actor_suzans = [] self.physics_client = None self.physics_plane = None self.physics_suzans = [] self.physics_spheres = []
def new_scene(self): self.begin_open_scene() # add scene objects self.main_camera = self.add_camera() self.main_light = self.add_main_light() self.main_light_probe = self.add_light_probe() self.atmosphere = self.add_atmosphere() self.ocean = self.add_ocean() self.terrain = self.add_terrain() self.set_current_scene_name(self.resource_manager.scene_loader.get_new_resource_name("new_scene")) logger.info("New scene : %s" % self.__current_scene_name) scene_data = self.get_save_data() self.resource_manager.scene_loader.create_resource(self.__current_scene_name, scene_data) self.end_open_scene()