-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathred_root.py
More file actions
204 lines (180 loc) · 6.38 KB
/
red_root.py
File metadata and controls
204 lines (180 loc) · 6.38 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# red_root_shader_fixed_toggle.py
import glfw
from OpenGL.GL import *
import numpy as np
import time
# ---------- SHADERS ----------
VERT_SRC = """
#version 330 core
layout (location = 0) in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}
"""
FRAG_SRC = """
#version 330 core
out vec4 FragColor;
uniform vec2 u_res;
uniform float u_time;
uniform vec2 u_mouse;
uniform float u_intensity;
uniform float u_speed;
uint hash_u(uint x){
x += (x << 10u); x ^= (x >> 6u);
x += (x << 3u); x ^= (x >> 11u);
x += (x << 15u);
return x;
}
float hashf(vec2 p){
uint x = floatBitsToUint(p.x) ^ (floatBitsToUint(p.y) << 1u);
return float(hash_u(x)) * (1.0 / 4294967295.0);
}
float noise(vec2 p){
vec2 i = floor(p);
vec2 f = fract(p);
float a = hashf(i + vec2(0.0,0.0));
float b = hashf(i + vec2(1.0,0.0));
float c = hashf(i + vec2(0.0,1.0));
float d = hashf(i + vec2(1.0,1.0));
vec2 u = f*f*(3.0-2.0*f);
return mix(a,b,u.x) + (c-a)*u.y*(1.0-u.x) + (d-b)*u.x*u.y;
}
float fbm(vec2 p){
float v = 0.0;
float a = 0.5;
for(int i=0;i<6;i++){
v += a * noise(p);
p *= 2.0;
a *= 0.5;
}
return v;
}
vec2 domainWarp(vec2 p, float t){
float q = fbm(p * 0.8 + t * 0.2);
vec2 r = vec2(fbm(p + vec2(1.7,9.2) + t*0.3),
fbm(p + vec2(8.3,2.8) - t*0.25));
return p + (r - 0.5) * (0.8 + q*1.6);
}
void main() {
vec2 uv = (gl_FragCoord.xy / u_res) * 2.0 - 1.0;
uv.x *= u_res.x / u_res.y;
vec2 p = uv * 1.6;
float t = u_time * u_speed;
p.y += t * 0.25;
vec2 w = domainWarp(p * 1.2, t);
w = domainWarp(w * 1.5 + vec2(3.0, -2.0), t*0.9);
float n = fbm(w * 2.5);
float ridge = abs(0.5 - n) * 2.0;
float strands = smoothstep(0.18, 0.55, pow(ridge, 1.8) * u_intensity);
float veins = smoothstep(0.5, 0.8, fbm(w * 6.0 + vec2(0.0, t*0.4)));
float m = length(uv - u_mouse);
float attract = exp(-m*6.0) * 1.8;
float mask = strands * (1.0 + attract) + veins * 0.2;
float vign = smoothstep(0.9, 0.25, length(uv));
vec3 coreColor = vec3(1.0, 0.2, 0.15);
vec3 midColor = vec3(0.6, 0.05, 0.03);
vec3 darkColor = vec3(0.12, 0.02, 0.02);
vec3 color = mix(darkColor, midColor, mask);
color = mix(color, coreColor, pow(mask, 2.0) * (0.6 + veins*0.4));
color *= 0.9 + 0.15 * sin(u_time*3.0 + uv.x*10.0);
color *= vign;
FragColor = vec4(color, 1.0);
}
"""
# ---------- FUNCTIONS ----------
def compile_shader(src, shader_type):
shader = glCreateShader(shader_type)
glShaderSource(shader, src)
glCompileShader(shader)
if not glGetShaderiv(shader, GL_COMPILE_STATUS):
raise RuntimeError(glGetShaderInfoLog(shader))
return shader
def create_shader_program():
prog = glCreateProgram()
vs = compile_shader(VERT_SRC, GL_VERTEX_SHADER)
fs = compile_shader(FRAG_SRC, GL_FRAGMENT_SHADER)
glAttachShader(prog, vs)
glAttachShader(prog, fs)
glLinkProgram(prog)
if not glGetProgramiv(prog, GL_LINK_STATUS):
raise RuntimeError(glGetProgramInfoLog(prog))
glUseProgram(prog)
return prog
def create_geometry():
verts = np.array([-1,-1, 1,-1, -1,1, -1,1, 1,-1, 1,1], dtype=np.float32)
vao = glGenVertexArrays(1)
vbo = glGenBuffers(1)
glBindVertexArray(vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, verts.nbytes, verts, GL_STATIC_DRAW)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, None)
return vao
# ---------- INIT ----------
if not glfw.init():
raise Exception("GLFW init failed")
monitor = glfw.get_primary_monitor()
mode = glfw.get_video_mode(monitor)
WINDOW_W, WINDOW_H = 1920, 1080
current_win = glfw.create_window(WINDOW_W, WINDOW_H, "Red Root Shader", None, None)
glfw.make_context_current(current_win)
is_fullscreen = False
prog = create_shader_program()
vao = create_geometry()
u_res = glGetUniformLocation(prog, "u_res")
u_time = glGetUniformLocation(prog, "u_time")
u_mouse = glGetUniformLocation(prog, "u_mouse")
u_intensity = glGetUniformLocation(prog, "u_intensity")
u_speed = glGetUniformLocation(prog, "u_speed")
start = time.time()
mouse = (0.0, 0.0)
def cursor_pos(win, x, y):
w, h = glfw.get_framebuffer_size(win)
nx = (x / w) * 2 - 1
ny = ((h - y) / h) * 2 - 1
aspect = w / h
global mouse
mouse = (nx*aspect, ny)
glfw.set_cursor_pos_callback(current_win, cursor_pos)
def key_callback(win, key, scancode, action, mods):
global is_fullscreen, current_win, prog, vao
if key == glfw.KEY_F11 and action == glfw.PRESS:
is_fullscreen = not is_fullscreen
glfw.destroy_window(current_win)
if is_fullscreen:
current_win = glfw.create_window(mode.size.width, mode.size.height, "Red Root Shader", monitor, None)
else:
current_win = glfw.create_window(WINDOW_W, WINDOW_H, "Red Root Shader", None, None)
glfw.make_context_current(current_win)
glfw.set_cursor_pos_callback(current_win, cursor_pos)
glfw.set_key_callback(current_win, key_callback)
# Recreate program + geometry after context rebuild
prog = create_shader_program()
vao = create_geometry()
# Re-get uniforms
global u_res, u_time, u_mouse, u_intensity, u_speed
u_res = glGetUniformLocation(prog, "u_res")
u_time = glGetUniformLocation(prog, "u_time")
u_mouse = glGetUniformLocation(prog, "u_mouse")
u_intensity = glGetUniformLocation(prog, "u_intensity")
u_speed = glGetUniformLocation(prog, "u_speed")
if key == glfw.KEY_ESCAPE and action == glfw.PRESS:
glfw.set_window_should_close(win, True)
glfw.set_key_callback(current_win, key_callback)
# ---------- MAIN LOOP ----------
while not glfw.window_should_close(current_win):
glfw.poll_events()
w, h = glfw.get_framebuffer_size(current_win)
glViewport(0, 0, w, h)
glClear(GL_COLOR_BUFFER_BIT)
t = time.time() - start
glUseProgram(prog)
glUniform2f(u_res, w, h)
glUniform1f(u_time, t)
glUniform2f(u_mouse, *mouse)
glUniform1f(u_intensity, 1.4)
glUniform1f(u_speed, 0.8)
glBindVertexArray(vao)
glDrawArrays(GL_TRIANGLES, 0, 6)
glfw.swap_buffers(current_win)
glfw.terminate()