-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.go
176 lines (131 loc) · 4.04 KB
/
shader.go
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
package main
import "github.com/go-gl/mathgl/mgl32"
var iTime float32
var iMouse mgl32.Vec4
var iLightPos mgl32.Vec2
// NOTE: I have added #version and all "uniforms". I also renamed the main() function
// and moved it's imports to "out" and "in" package variables
//@TODO I think utexture needs to be brought in to the output render (see second shader for example)
var fragmentShaderLighting = `
#version 330 core
out vec4 fragColor;
// Steps is pretty much shadow smoothness
#define STEPS 16
//#define LIGHT_SPEED 1.5
#define LIGHT_SPEED 0.5
#define HARDNESS 2.0
uniform sampler2D uTexture;
uniform vec4 uTexBounds;
// custom uniforms
uniform float iTime;
uniform vec4 iMouse;
uniform vec2 iLightPos;
struct ray {
vec2 t;
vec2 p;
vec2 d;
};
float scene (in vec2 p) {
// p is position of rotating square
p -= vec2 (70,20);
// probably something to do with rotation
// used in conjunction sin/cos (iTime / x) changing x changes square rotation speed
// changing one x makes the square change size on a sin/cos curve
float sn = sin (iTime / 2.0);
float cs = cos (iTime / 2.0);
float f1 = dot (vec2 (sn,-cs), p);
float f2 = dot (vec2 (cs, sn), p);
float f = max (f1*f1, f2*f2);
// Make an extra circle, needs to be placed variably @TODO many broken pls fix
//p += vec2 (70, 20);
//f = min (f, dot (p, p) * 2400.0);
// this p sets circle position, equation makes it follow a circular arc
p += vec2 (sn + 0.5, cs) * 200.0;
// increase multiplier to decrease circle size
f = min (f, dot (p, p) * 2.0);
// something to do with square / circle sizes / (f + x)
return 1000.0 / (f + 1000.0);
}
ray newRay (in vec2 origin, in vec2 target) {
ray r;
r.t = target;
r.p = origin;
r.d = (target - origin) / float (STEPS);
return r;
}
void rayMarch (inout ray r) {
r.p += r.d * clamp (HARDNESS - scene (r.p) * HARDNESS * 2.0, 0.0, LIGHT_SPEED);
}
vec3 light (in ray r, in vec3 color) {
return color / (dot (r.p, r.p) + color);
}
void main() {
// window position / size from origin
vec2 uv = (gl_FragCoord.xy-225) / uTexBounds.zw * vec2 (1, 1);
// light positions
ray r0 = newRay (uv, vec2 (200, 250));
ray r1 = newRay (uv, vec2 (0, -250));
ray r2 = newRay (uv, iLightPos);
for (int i = 0; i < STEPS; i++) {
rayMarch (r0);
rayMarch (r1);
rayMarch (r2);
}
r0.p -= r0.t;
r1.p -= r1.t;
r2.p -= r2.t;
// (r0, vec3(r, g, b) * intensity)
vec3 light1 = light (r0, vec3 (0.34, 0.06, 0.55) * 2000.0);
vec3 light2 = light (r1, vec3 (0.1, 0.2, 0.3) * 1500.0);
vec3 light3 = light (r2, vec3 (0.1, 0.1, 0.1) * 0.0001);
// affects light intensity on multiple objects
// final number is most useful, affects square/circle roughness
float f = clamp (scene (uv) * 200.0 - 100.0, 0.0, 4.0);
vec3 col = texture(uTexture, uv).rgb;
fragColor = vec4 ((col + light1 + light2 + light3) * (1.0 + f), 1.0);
//fragColor = vec4 (scene (uv));
}
`
var fragmentShaderDrunkered = `
#version 330 core
out vec4 fragColor;
uniform sampler2D uTexture;
uniform vec4 uTexBounds;
// custom uniforms
uniform float uSpeed;
uniform float uTime;
void main() {
vec2 t = gl_FragCoord.xy / uTexBounds.zw;
vec3 influence = texture(uTexture, t).rgb;
if (influence.r + influence.g + influence.b > 0.3) {
t.y += cos(t.x * 40.0 + (uTime * uSpeed))*0.005;
t.x += cos(t.y * 40.0 + (uTime * uSpeed))*0.01;
}
vec3 col = texture(uTexture, t).rgb;
fragColor = vec4(col * vec3(0.6, 0.6, 1.2),1.0);
}
`
/* Drunkered requires :
var uTime, uSpeed float32
win.Canvas().SetUniform("uTime", &uTime)
win.Canvas().SetUniform("uSpeed", &uSpeed)
uSpeed = 5.0
*/
var fragmentShaderGreyScale = `
#version 330 core
in vec2 vTexCoords;
out vec4 fragColor;
uniform vec4 uTexBounds;
uniform sampler2D uTexture;
void main() {
// Get our current screen coordinate
vec2 t = (vTexCoords - uTexBounds.xy) / uTexBounds.zw;
// Sum our 3 color channels
float sum = texture(uTexture, t).r;
sum += texture(uTexture, t).g;
sum += texture(uTexture, t).b;
// Divide by 3, and set the output to the result
vec4 color = vec4( sum/3, sum/3, sum/3, 1.0);
fragColor = color;
}
`