Skip to main content

Library to abstract numpy vectorized array operations for pygame surfaces to mimic shader code

Project description

Pixeluxe 1.0

Library to abstract numpy vectorized array operations for pygame surfaces to mimic shader code

NOTE: This won't work like shaders. There is no functions that will run for every pixel, instead there is an interface to use numpy vectorized operations specifically for surfaces with the help of x & y UV arrays.
Also, the speed is limited to numpy speed which while it's a lot, it's not as fast as GPUs so don't run too many shaders at once

Installation

Install the module from PyPI or downloading the source code here

pip install pixeluxe

Usage

NOTE: Get information about all functions, classes and properties with the help of your IDE or checking the source (it's not long)

You can find the same code on tests/readme_test.py

# You'll most likely need this
import pygame, sys
###import pixeluxe
from src.pixeluxe import pixeluxe
import numpy as np
import math as m

# pygame simple setup
pygame.init()
screen = pygame.display.set_mode((1200,800)) 
clock = pygame.Clock()

# First, define your shader functions

# This is meant to run once or few times, to initialize the pixels values. Custom setup uniforms are allowed
def shader_hello_world_setup(pixels: pixeluxe.PixelsType):
    # Use the pixels property to make a common shader 'Hello World'. Use the PixelsType's docstring to know more about those properties
    # This will be overridden by shader_hello_world_loop, this is just an example
    pixels.r_g_b = [pixels.x, pixels.y, 255]


# Here are 2 functions that should be called every frame, one of them needs a 'time' uniform. 
def shader_noise_loop(pixels: pixeluxe.PixelsType):
    pixels.rgb = pixeluxe.correct_type(np.random.rand(pixels.w,pixels.h)*255)

def shader_hello_world_loop(pixels: pixeluxe.PixelsType, time):
    sin = m.sin(time/500)
    if sin > 0:
        pixels.r_g_b = [pixels.x, pixels.y, (sin+1)/2*255]
    else:
        pixels.r_g_b = [pixels.x, (sin+1)/2*255, pixels.y/2]


# Use those functions to make 2 shaders:
hello_world_shader = pixeluxe.PShader(shader_hello_world_setup, shader_hello_world_loop)
noise_shader = pixeluxe.PShader(None, shader_noise_loop)

# Now let's create 2 surfaces that will use those shaders. Only one dimention is needed as the shaders only work for quads. Use the scaling functions for rectangles
hello_world_surf = pixeluxe.PSurface(400, hello_world_shader)
noise_surf = pixeluxe.PSurface(400, noise_shader)

# Setup should be called after the surface is created
hello_world_surf.shader_setup()

while True:
    [(pygame.quit(), sys.exit()) for e in pygame.event.get() if e.type == pygame.QUIT]
    
    screen.fill("black")
    
    # Call the shader loops on this surface to apply them, passing the correct uniforms
    hello_world_surf.shader_loop(pygame.time.get_ticks())
    noise_surf.shader_loop()
    
    # Tell the PSurface to blit the array onto the output surface. use apply_sized and apply_scaled to change the output surface size
    # We are also making rects for blitting
    hello_world_surf.apply_to_surface()
    rect = hello_world_surf.output_surface.get_rect(midright=(600,400))
    noise_surf.apply_to_surface()
    rect2 = noise_surf.output_surface.get_rect(midleft=(600,400))
    
    # Blit the surface output_surface onto the screen and watch the result!
    screen.blit(hello_world_surf.output_surface, rect)
    screen.blit(noise_surf.output_surface, rect2)
    
    clock.tick(0)
    pygame.display.flip()
    # Let's check our performance
    pygame.display.set_caption(f"{clock.get_fps():.0f}")

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pixeluxe-1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pixeluxe-1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file pixeluxe-1.0.tar.gz.

File metadata

  • Download URL: pixeluxe-1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for pixeluxe-1.0.tar.gz
Algorithm Hash digest
SHA256 3c6e5c7f46634506a61ecf9e16e1a0723c6c73c0b1b07b108a0892f660f76c16
MD5 4e469389e28c76f84b93a7609979a15a
BLAKE2b-256 e1a84a81bbeccb9973877a575999dc216fe95f8d766e5879fb284992da7f7d97

See more details on using hashes here.

File details

Details for the file pixeluxe-1.0-py3-none-any.whl.

File metadata

  • Download URL: pixeluxe-1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for pixeluxe-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c442ec58690553b9e0d348b0c58d7c321b6ab9793c0b68ef11028b911176e1c
MD5 fde16d090b9c1138b0df7d5c2fff971a
BLAKE2b-256 4bbfe328df7d659cea94b1f51581d314773ceeb39c54ba4b4c7b229bf0d9894c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page