-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7_10_01_24.py
103 lines (78 loc) · 3.17 KB
/
7_10_01_24.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Author: Ethan.R
Date of Creation: 7th January 2025
Date of Release: NA
Name of Creation: SDF
"""
from _lib import Main
import moderngl as mgl
import pygame
class ShaderProgram(Main):
program_frag: str = """
# version 460 core
uniform vec2 uResolution;
uniform vec2 uMousePos;
out vec4 fColour;
// SDF Functions
float sdfCircle(vec2 pos, float radius){
return length(pos) - radius;
}
float sdfBox(vec2 pos, vec2 radius){
vec2 distance = abs(pos) - radius;
return length(max(distance, 0.0)) + min(max(distance.x, distance.y), 0.0);
}
float sdfSegment(vec2 p, vec2 point1, vec2 point2){
vec2 pa = p - point1;
vec2 ba = point2 - point1;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
return length(pa - ba * h);
}
void main(){
vec2 uvs = gl_FragCoord.xy/uResolution.xy;
vec2 mousePos = uMousePos/uResolution;
// Create a Signed Distance Field Shape
float distanceToShape = sdfCircle(uvs - mousePos, 0.1);
//float distanceToShape = sdfBox(uvs - mousePos, vec2(0.2, 0.1));
//float distanceToShape = sdfSegment(uvs - mousePos, vec2(0.1, 0.1), vec2(0.5, 0.1));
vec3 colour = vec3(distanceToShape / 0.05);
fColour = vec4(colour.rgb, 1.0);
}
"""
def __init__(self, caption:str, swizzle:str, scale:int, flip:bool=True, components:int=3, method:str="nearest", path:str="None", url:str="None", headless:bool=False):
super().__init__(path=path, url=url, scale=scale, caption=caption, flip=flip, swizzle=swizzle, components=components, method=method, headless=headless)
self.load_program()
# Load render target texture
self.new_texture: mgl.Texture = self.ctx.texture(size=self.start_texture.size, components=4)
self.new_texture.filter: tuple = (mgl.NEAREST, mgl.NEAREST)
self.framebuffer: mgl.Framebuffer = self.ctx.framebuffer(color_attachments=[self.new_texture])
# Create shader program
self.new_program: mgl.Program = self.ctx.program(vertex_shader=Main.main_vertex, fragment_shader=ShaderProgram.program_frag)
self.new_vao: mgl.VertexArray = self.ctx.vertex_array(self.new_program, [(self.quad_buffer, "2f 2f", "aPosition", "aTexCoord")])
@Main.d_update
def update(self):
self.new_program["uResolution"] = self.screen.get_size()
self.new_program["uMousePos"] = pygame.mouse.get_pos()
def garbage_cleanup(self):
super().garbage_cleanup() # A better way then using decorators in this context
self.new_program.release()
self.new_program.release()
self.framebuffer.release()
self.new_vao.release()
self.new_texture.release()
@Main.d_draw
def draw(self):
self.framebuffer.use()
self.new_vao.render(mgl.TRIANGLE_STRIP)
self.ctx.screen.clear(red=0.0, green=0.0, blue=0.0, alpha=1.0)
self.ctx.screen.use()
self.framebuffer.color_attachments[0].use(location=0)
self.main_program["myTexture"] = 0
if __name__ == "__main__":
shader_program: ShaderProgram = ShaderProgram(
caption="NA",
swizzle="RGBA",
scale=1.0,
flip=True,
components=4,
path=r"_images\0NormalWall.png"
).run()