-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniforms_3.js
407 lines (316 loc) · 10.7 KB
/
uniforms_3.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
var w = 600;
var h = 600;
const app = new PIXI.Application( {width: w,
height: h,
backgroundColor: '0x86D0F2',
transparent: true,
antialias: true,
autoResize: true,
resolution: devicePixelRatio,
resizeTo: window
});
document.body.appendChild(app.view);
// create the root of the scene graph
var stage = new PIXI.Container();
const geometry = new PIXI.Geometry()
.addAttribute('aVertexPosition', // the attribute name
[-100, -100, // x, y
100, -100, // x, y
100, 100,
-100, 100], // x, y
2) // the size of the attribute
.addAttribute('aUvs', // the attribute name
[0, 0, // u, v
1, 0, // u, v
1, 1,
0, 1], // u, v
2) // the size of the attribute
.addIndex([0, 1, 2, 0, 2, 3]);
const vertexSrc = `
precision mediump float;
attribute vec2 aVertexPosition;
attribute vec2 aUvs;
uniform mat3 translationMatrix;
uniform mat3 projectionMatrix;
varying vec2 vUvs;
varying vec4 position;
void main() {
vUvs = aUvs;
position = vec4((projectionMatrix * translationMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);
gl_Position = position;
}`;
const fragmentSrc = `
precision mediump float;
varying vec2 vUvs;
varying vec4 position;
uniform sampler2D uSampler2;
uniform float time;
uniform float circle_size;
void main() {
float dx = vUvs.x - 0.5;
float dy = vUvs.y - 0.5;
float x = mod(vUvs.x * 1.0,1.0);
float y = mod(vUvs.y * 1.0,1.0);
float r = 0.2 + 0.5 * sin(tan(x+y)*0.0 + circle_size * 0.1);
float g = 0.2 + 0.5 * sin( 2.0*sin(y * 3.0) + time * 0.1);
float b = 0.2 + 0.5 * sin( sin(x * 6.0) + time * 0.1);
float distance = circle_size / 300.0 - sqrt(dx*dx + dy*dy);
float alpha = distance * 10.0;
gl_FragColor = vec4(r,g,b,alpha);
//gl_FragColor = texture2D(uSampler2, vec2(x + 0.1*sin(time), y));
//float distance2edge = abs(dx - 0.5) - 0.5;
//gl_FragColor = vec4(0,1,0,pow(distance2edge * 2.0, 0.2));
}`;
const lineFragmentSrc = `
precision mediump float;
varying vec2 vUvs;
varying vec4 position;
uniform sampler2D uSampler2;
uniform float time;
uniform float theta;
uniform float m1;
uniform float b1;
uniform int phase;
uniform int show_axes;
uniform float rad;
uniform float angle;
uniform float a1;
uniform float n1;
uniform float c1;
void main() {
float x = vUvs.x - 0.5;
float y = vUvs.y - 0.5;
vec4 color = vec4(1,1,1,0);
float theta2 = 360. - theta;
float PI = 3.14159;
float th = 2.*PI*theta2/360.;
float qx = 10.*(x);
float qy = 10.*y;
float r = 1.;
float g = 1.;
float b = 1.;
float ax = 1.5*sqrt(qx*qx+qy*qy);
float ay = atan(qy,-qx)+PI;
if (phase==1) {
if ((-cos(8.*ay+PI)>.98 || cos(2.*PI*ax/1.)>.98) && ax<4.) {
r=0.;
g=0.;
}
//float rad = 2.;
float angle2 = angle / 180. * PI;
float px = qx-rad/1.5*cos(-angle2);
float py = qy-rad/1.5*sin(-angle2);
float dist = sqrt(px*px+py*py);
if (show_axes==1) {
if (ax<1. && ay<angle2) {
r=1.;
g=1.;
b=0.5;
}
if (abs(angle2-ay)<.03 && ax<rad) {
r=0.;
g=.7;
b=0.2;
}
}
if (dist<.1) {
r=1.;
g=0.;
b=0.;
}
}
if (phase>=2) {
float ay = qx;
float ax = -qy;
float slide = exp(th);
float h = 1.;
qx = sqrt(ay*ay+(ax+slide-h)*(ax+slide-h))+h-slide;
qy = atan(ay,ax+slide-h)*slide;
for (float i=0.; i<4.; i+=1.) {
float by = qy-2.*PI*i+6.*PI;
float f = c1/4.-a1/4.*sin(by*n1);
float f2 = m1/4. * by + b1/4.;
if (abs(qy)<PI && abs(qx-1.)<1.) {
if (show_axes==1 && i==0.)
{
if (-cos(16.*qy+PI)>.98 || cos(4.*2.*PI*qx/1.)>.98) {
r=0.;
g=0.;
}
}
if (phase == 4) // sine
{
if (abs(qx-f)<.03) {
b=i/6.;
g=i/4.;
}
}
if (phase == 2) // line
{
if (abs(qx - f2) < .03){
b=i/6.;
g=i/4.;
}
}
}
}
}
color = vec4(r,g,b,1.);
gl_FragColor = color;
}`;
let leaves = PIXI.Texture.from('leaves.jpg');
const uniforms = {
phase: 1,
uSampler2: leaves,
time: 0,
theta: 0,
a1: 0,
c1: 0,
n1: 0,
m1: 0,
b1: 0,
show_axes: 1,
rad: 0,
angle: 0
};
const circleShader = PIXI.Shader.from(vertexSrc, fragmentSrc, uniforms);
const lineShader = PIXI.Shader.from(vertexSrc, lineFragmentSrc, uniforms);
const quad = new PIXI.Mesh(geometry, lineShader);
quad.position.set(w/2, h/2);
quad.scale.set(4);
app.stage.addChild(quad);
// start the animation..
// requestAnimationFrame(animate);
app.ticker.add((delta) => {
quad.shader.uniforms.time += 0.0001;
});
var slider = document.getElementById("sliderInput");
function rgb(r, g, b){
return "rgb("+r+","+g+","+b+")";
}
function handleSlider (value)
{
quad.shader.uniforms.theta = value;
color = rgb(255 * 0.3, 255 * (0.3 + 0.4 * value / 360.), 255 * 0.3);
color2 = rgb(255 * 0.3, 255 * (0.3 + 0.4 * (360 - value) / 360.), 255 * 0.3);
document.getElementById("sliderCart").style.color = color2;
document.getElementById("sliderPol").style.color = color;
}
function handleLineMode(value)
{
quad.shader.uniforms.phase = 2;
document.getElementById("slidersPhase2Line").style.display = "block";
document.getElementById("slidersPhase2Sine").style.display = "none";
}
function handleSineMode(value)
{
quad.shader.uniforms.phase = 4;
document.getElementById("slidersPhase2Line").style.display = "none";
document.getElementById("slidersPhase2Sine").style.display = "block";
var text = " y = " + (quad.shader.uniforms.a1) + " * sin(" + (quad.shader.uniforms.n1) + " * x) + " + (quad.shader.uniforms.c1);
document.getElementById("equation").innerHTML = text;
}
function handleASlider(value)
{
quad.shader.uniforms.a1 = value;
var text = " y = " + (quad.shader.uniforms.a1) + " * sin(" + (quad.shader.uniforms.n1) + " * x) + " + (quad.shader.uniforms.c1);
document.getElementById("equation").innerHTML = text;
}
function handleNSlider(value)
{
quad.shader.uniforms.n1 = value;
var text = " y = " + (quad.shader.uniforms.a1) + " * sin(" + (quad.shader.uniforms.n1) + " * x) + " + (quad.shader.uniforms.c1);
document.getElementById("equation").innerHTML = text;
}
function handleCSlider(value)
{
quad.shader.uniforms.c1 = value;
var text = " y = " + (quad.shader.uniforms.a1) + " * sin(" + (quad.shader.uniforms.n1) + " * x) + " + (quad.shader.uniforms.c1);
document.getElementById("equation").innerHTML = text;
}
function handleMSlider(value)
{
quad.shader.uniforms.m1 = value;
var text = " y = " + (quad.shader.uniforms.m1) + "x";
text += ((quad.shader.uniforms.b1 >= 0) ? " + " : " - ") + Math.abs(quad.shader.uniforms.b1);
document.getElementById("equation").innerHTML = text;
}
function handleBSlider(value)
{
quad.shader.uniforms.b1 = value;
var text = " y = " + (quad.shader.uniforms.m1) + "x";
text += ((quad.shader.uniforms.b1 >= 0) ? " + " : " - ") + Math.abs(quad.shader.uniforms.b1);
document.getElementById("equation").innerHTML = text;
}
function handleRSlider(value)
{
quad.shader.uniforms.rad = value;
}
function handleTSlider(value)
{
quad.shader.uniforms.angle = value;
}
function handleTrianglePhase (value)
{
quad.shader.uniforms.phase = 1;
document.getElementById("panel1").style.display = "block";
document.getElementById("panel2").style.display = "none";
document.getElementById("slidersPhase1").style.display = "block";
document.getElementById("slidersPhase2").style.display = "none";
document.getElementById("equation").style.display = "none";
}
function handleCirclePhase (value)
{
quad.shader.uniforms.phase = 2;
document.getElementById("panel1").style.display = "none";
document.getElementById("panel2").style.display = "block";
document.getElementById("slidersPhase1").style.display = "none";
document.getElementById("slidersPhase2").style.display = "block";
document.getElementById("equation").style.display = "block";
}
function handleAxes( value )
{
var checkbox = document.getElementById("axes");
if (checkbox.checked)
{
quad.shader.uniforms.show_axes = 1;
}else{
quad.shader.uniforms.show_axes = 0;
}
}
// Listen for window resize events
window.addEventListener('resize', resize);
// Resize function window
function resize() {
// Resize the renderer
app.renderer.resize(window.innerWidth, window.innerHeight);
// You can use the 'screen' property as the renderer visible
// area, this is more useful than view.width/height because
// it handles resolution
quad.position.set(app.screen.width/2, app.screen.height/2);
var ratio = app.screen.width / app.screen.height;
var tall_side = app.screen.height;
if (ratio > 1){
tall_side = app.screen.width;
}
quad.scale.set(tall_side/200);
}
resize();
screen.orientation.addEventListener("change", function(e) {
resize();
}, false);
let keysPressed = {};
document.addEventListener('keydown', (event) => {
keysPressed[event.key] = true;
if (keysPressed['Alt'] && event.key == '1') {
alert("Mode 1");
quad.shader = circleShader;
}
if (keysPressed['Alt'] && event.key == '2') {
alert("Mode 2");
quad.shader = lineShader;
}
});
document.addEventListener('keyup', (event) => {
delete keysPressed[event.key];
});