Python Forum
[PyGame] How do I make an advanced shader
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] How do I make an advanced shader
#1
I have been using pygame a couple months now and have been using it to recreate the terminal interface from the fallout universe (with a few tweaks to make it fully functional) I am focusing on the visual aspects at the moment and I would like to develop a shader to apply various CRT effects such as scanlines, phosphor bloom, etc but I have only ever done a basic shader to make a simple rotating cube. I found a possible starting point through THIS POST but I wouldn't know where to start. I guess i'm looking for some guidance on how I'd go about implementing a shader like this into my Pygame project as well as how to learn to make other shader effects like visual glitching.

For anyone who is interested in my project I'll toss a video in a spoiler
Reply
#2
NOTE: I havent done this either so i dont know. My guess would be a simple approach of mimicking it assuming you had a greenish background instead of black. By that i mean creating an image with scanlines with alpha in GIMP/photoshop and displaying it overtop, moving back and forth at random times. Same thing with the side shader around the edges except its stagnant and shaped differently.

Im not sure if that would look cheesy or not though.
Recommended Tutorials:
Reply
#3
1. Are you using any opengl/directx in your code ?

using pygame surfaces
import pygame

class Ticker:
    def __init__(self, length):
        self.next_tick = length
        self.length = length

    def elapse(self, ticks):
        if ticks > self.next_tick:
            self.next_tick += self.length
            return True
        return False

class Screen:
    WIDTH = 800
    HEIGHT = 600

    def __init__(self):
        pygame.init()
        pygame.display.set_caption("Scanlines")
        self.screen = pygame.display.set_mode(self.get_size())
        self.clock = pygame.time.Clock()
        self.running = False
        font = pygame.font.Font(None, 24)
        color = pygame.Color('forestgreen')
        self.text = font.render("testing scanlines", 1, color)
        self.text2 = font.render("line two", 1, color)

        self.create_scanline()
        self.lines_position = list(range(0, Screen.HEIGHT + 1, 200))
        self.scanline_ticker = Ticker(2)
        self.scanline_speed = 10

    def create_scanline(self):
        self.scanline = pygame.Surface((1, 200))
        self.scanline = self.scanline.convert_alpha()
        color = pygame.Color(0, 180, 0)
        color.a = 15
        for a in range(100):
            color.g -= 1
            self.scanline.set_at((0, a), color)
            self.scanline.set_at((0, 199 - a), color)

        self.scanline = pygame.transform.scale(self.scanline, (Screen.WIDTH, 200))

    def draw(self):
        self.screen.fill((0,0,0))
        self.screen.blit(self.text, (80, 100))
        self.screen.blit(self.text2, (80, 150))

        ticks = pygame.time.get_ticks()
        if self.scanline_ticker.elapse(ticks):
            for i in range(len(self.lines_position)):
                self.lines_position[i] -= self.scanline_speed
                if self.lines_position[i] < -199:
                    self.lines_position[i] = Screen.HEIGHT

        for line in self.lines_position:
            self.screen.blit(self.scanline, (0, line), None, pygame.BLEND_RGBA_MULT)
            self.screen.blit(self.scanline, (0, line), None)

    def get_size(self):
        return Screen.WIDTH, Screen.HEIGHT

    def get_rect(self):
        return pygame.Rect(0,0,Screen.WIDTH,Screen.HEIGHT)

    def loop(self):
        self.running = True
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False

            self.draw()
            pygame.display.flip()
            self.clock.tick(60)

        pygame.quit()

if __name__ == '__main__':
    screen = Screen()
    screen.loop()
99 percent of computer problems exists between chair and keyboard.
Reply
#4
(Jan-15-2018, 10:24 PM)Windspar Wrote: 1. Are you using any opengl/directx in your code ?

I'm not currently using either opengl or directx though i'm not opposed to the implementation. I'm currently picking apart your code, and I'm trying to digest it enough to kind of see how it would work in my current setup.

(Jan-15-2018, 10:19 PM)metulburr Wrote: NOTE: I havent done this either so i dont know. My guess would be a simple approach of mimicking it assuming you had a greenish background instead of black. By that i mean creating an image with scanlines with alpha in GIMP/photoshop and displaying it overtop, moving back and forth at random times. Same thing with the side shader around the edges except its stagnant and shaped differently.

Im not sure if that would look cheesy or not though.

I feel like mimicking with an image would defeat the purpose of trying to use a shader to handle it.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020