def update(self, task=None): """Updates the shader inputs that need to be updated every frame. Normally, you shouldn't call this, it's being called in a task.""" if "VolumetricLighting" in self.configuration: caster = self.configuration["VolumetricLighting"].caster casterpos = LPoint2() self.manager.camera.node().getLens().project( caster.getPos(self.manager.camera), casterpos) self.finalQuad.setShaderInput( "casterpos", LVecBase4(casterpos.getX() * 0.5 + 0.5, (casterpos.getY() * 0.5 + 0.5), 0, 0)) if task != None: return task.cont
def get_offsets(spritesheet: Texture, sprite_sizes: tuple) -> types.SpritesheetData: """Fetch all available offsets from provided spritesheet.""" # For now, this has 2 limitations, both of which are addressed as exceptions: # 1. Spritesheet HAS TO DIVIDE TO PROVIDED SPRITE SIZE WITHOUT REMAINDER. If # it doesnt cut to perfect sprites, you will get strange results during using # some of these sprites. # 2. Amount of sprite rows and columns MUST BE POWER OF 2. Otherwise - see # above. This is because of limitation of set_tex_offset() and set_tex_scale() # functions, both of which operate with floats between 0 and 1 to determine # texture's size and position. # I assume, its possible to fix both of these. But right now I have no idea how # As for first - I can probably add bool to enable optional cut with PNMimage # of all the garbage that dont fit #TODO log.debug(f"Fetching {sprite_sizes} offsets from {spritesheet.get_name()}") # Checking if our spritesheet match first limitation, mentioned above if _has_remainder(spritesheet, sprite_sizes): raise exceptions.InvalidSpriteSize(spritesheet.get_name(), sprite_sizes) # Determining amount of sprites in each row sprite_columns, sprite_rows = _get_columns_and_rows( spritesheet, sprite_sizes) # Checking if we pass second limitation from above if not _is_power_of_two(sprite_columns) or not _is_power_of_two( sprite_rows): raise exceptions.InvalidSpriteSize(spritesheet.get_name(), sprite_sizes) log.debug(f"Our sheet has {sprite_columns}x{sprite_rows} sprites") # idk if these should be flipped - its 3 am # this may backfire on values bigger than one... but it should never happen horizontal_offset_step = 1 / sprite_columns vertical_offset_step = 1 / sprite_rows offset_steps = LPoint2(horizontal_offset_step, vertical_offset_step) log.debug(f"Offset steps are {offset_steps}") spritesheet_offsets = [] # We process rows backwards to make it match "from top left to bottom right" # style of image processing, used by most tools (and thus probs expected) for row in range(sprite_rows - 1, -1, -1): log.debug(f"Processing row {row}") for column in range(0, sprite_columns): log.debug(f"Processing column {column}") horizontal_offset = column * horizontal_offset_step vertical_offset = row * vertical_offset_step offsets = LPoint2(horizontal_offset, vertical_offset) log.debug(f"Got offsets: {offsets}") spritesheet_offsets.append(offsets) log.debug(f"Spritesheet contain following offsets: {spritesheet_offsets}") data = types.SpritesheetData(spritesheet, spritesheet_offsets, offset_steps) log.debug(f"Got following data: {data}, returning") return data