-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuniforms.js
213 lines (175 loc) · 6.06 KB
/
uniforms.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
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 m1;
uniform float b1;
void main() {
float x = vUvs.x - 0.5;
float y = vUvs.y - 0.5;
vec4 color = vec4(1,1,1,0);
// y = x
float f = y - ( x );
//distance point to line:
// line: ax + by + c = 0, point: x0, y0
// y = -a/b*x - c/b
// let b = 1, c = 0
// distance = abs(ax0 + y0) / sqrt(a*a + 1)
float a = m1;
float distance = abs( y + (a * x) + b1 * 0.05) / sqrt(a*a + 1.0) - 0.001;
float r = 0.2 + 0.5 * sin( x * y * 0.1 + time * 0.02);
float g = 0.2 + 0.5 * sin( 0.1*sin(y * 0.1) + time * 0.015);
float b = 0.2 + 0.5 * sin( sin(x * 0.2) + time * 0.01);
float alpha = 1.0 - distance * 30.0;
if (abs(x) < 0.0005 || abs(y) < 0.0005)
{
color = vec4(0,0,0,1);
}
else if ((abs(x) < 0.01 || abs(y) < 0.01) && (mod(y,0.05) < 0.001 || mod(x,0.05) < 0.001))
{
color = vec4(0,0,0,1);
}
else if ((abs(x) < 0.005 || abs(y) < 0.005) && (mod(y,0.025) < 0.001 || mod(x,0.025) < 0.001))
{
color = vec4(0,0,0,1);
}
else if (distance < 10.0)
{
color = vec4(r,g,b,alpha);
}
gl_FragColor = color;
}`;
let leaves = PIXI.Texture.from('leaves.jpg');
const uniforms = {
uSampler2: leaves,
time: 0,
m1: 0,
b1: 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.1;
});
var slider = document.getElementById("sliderInput");
function handleSlider (value)
{
quad.shader.uniforms.m1 = value;
var text = " y<sub>1</sub> = " + (value) + "x";
text += ((quad.shader.uniforms.b1 >= 0) ? " + " : " - ") + Math.abs(quad.shader.uniforms.b1);
document.getElementById("equation").innerHTML = text;
}
function handleB1 (value)
{
quad.shader.uniforms.b1 = value;
var text = " y<sub>1</sub> = " + (quad.shader.uniforms.m1) + "x";
text += ((quad.shader.uniforms.b1 >= 0) ? " + " : " - ") + Math.abs(quad.shader.uniforms.b1);
document.getElementById("equation").innerHTML = text;
}
// 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];
});