MarkelZ / pygame-light2d

Fast 2D dynamic lighting engine for Pygame.
MIT License
20 stars 2 forks source link

render to pygame texture [enhancement proposal] #8

Open tigercoding56 opened 4 months ago

tigercoding56 commented 4 months ago

currently this library just renders to the main pygame screen (which it initializes), this is a issue if you want to not override the screen (for instance if you want to only apply lighting to part of the screen) or modify texture afterward easily (like add noise or post processing) or in my case move the rendered texture to correctly scroll on the background) my suggestion is to allow the function to return the buffer without applying it to the screen

(also good job it runs very fast , now i want a library that makes everything in pygame hardware accelerated (drop-in replacement) (like drawing shapes , blitting textures , etc (potentially with support to write shaders in python ) but that is out of the scope of this issue)

MarkelZ commented 4 months ago

Hi @tigercoding56, thank you for your feedback!! You're right, the engine is not very flexible currently. I have a shader package for pygame as you noticed in https://github.com/MarkelZ/pygame-light2d/issues/6, and my plan is to reimplement this lighting engine using the shader package. I will do it in the next days because summer has finally arrived! Once this is done, it should be quite easy to implement the features you're requesting :)

For now, for the background scroll effect, is it possible to achieve the same effect by simply moving everything in the opposite direction of the camera? This would include updating the position of every light and hull.

tigercoding56 commented 4 months ago

Hi @tigercoding56, thank you for your feedback!! You're right, the engine is not very flexible currently. I have a shader package for pygame as you noticed in #6, and my plan is to reimplement this lighting engine using the shader package. I will do it in the next days because summer has finally arrived! Once this is done, it should be quite easy to implement the features you're requesting :)

For now, for the background scroll effect, is it possible to achieve the same effect by simply moving everything in the opposite direction of the camera? This would include updating the position of every light and hull.

thank you,

MarkelZ commented 4 months ago

Hi again @tigercoding56! I reimplemented the lighting engine with my shader package, you can access the graphics with:

lighting_engine.graphics

You can use this package for postprocessing using shaders and many other features. The wiki has useful resources, especially the feature walktrhough.

I created a branch for testing rendering to a layer: https://github.com/MarkelZ/pygame-light2d/tree/render_to_layer. Please check out examples/example4.py, the lines of code that you should look at are marked with # THIS IS NEW!. For moving the texture to a new position, new_position, you may just need to do this:

    lights_engine.graphics.render(
        layer.texture, lights_engine.graphics.screen, position=new_position)  # THIS IS NEW!

Let me know if this is good for your use case. Also, don't hesitate to ask questions if you aren't sure about how the shader package is used.