-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
SDF-fake-box.py
58 lines (49 loc) · 1.51 KB
/
SDF-fake-box.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
# -----------------------------------------------------------------------------
# Python & OpenGL for Scientific Visualization
# www.labri.fr/perso/nrougier/python+opengl
# Copyright (c) 2017, Nicolas P. Rougier
# Distributed under the 2-Clause BSD License.
# -----------------------------------------------------------------------------
import numpy as np
from glumpy import app, gloo, gl
vertex = """
attribute vec2 position;
varying vec2 v_position;
void main(){
v_position = position;
gl_Position = vec4(position, 0.0, 1.0);
} """
fragment = """
float SDF_fake_box(vec2 p, vec2 size)
{
return max(abs(p.x)-size.x, abs(p.y)-size.y);
}
vec4 color(float d)
{
vec3 white = vec3(1.0, 1.0, 1.0);
vec3 blue = vec3(0.1, 0.4, 0.7);
vec3 color = white - sign(d)*blue;
color *= (1.0 - exp(-4.0*abs(d))) * (0.8 + 0.2*cos(140.0*d));
color = mix(color, white, 1.0-smoothstep(0.0,0.02,abs(d)) );
return vec4(color, 1.0);
}
varying vec2 v_position;
uniform vec2 size;
void main()
{
float d = SDF_fake_box(v_position, size);
gl_FragColor = color(d);
} """
window = app.Window(512, 512)
quad = gloo.Program(vertex, fragment, count=4)
quad['position'] = (-1,+1), (+1,+1), (-1,-1), (+1,-1)
phi = 0
@window.event
def on_draw(dt):
global phi
window.clear()
phi0 = np.pi*phi/180.0
quad["size"] = 0.5+0.25*np.cos(phi0), 0.5+0.25*np.sin(2*phi0)
quad.draw(gl.GL_TRIANGLE_STRIP)
phi += 1.0
app.run(framerate=60, framecount=360)