-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfinity.html
298 lines (254 loc) · 9.34 KB
/
infinity.html
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
<!DOCTYPE html>
<!-- saved from url=(0037)https://twgljs.org/examples/tiny.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--
@license twgl.js Copyright (c) 2015, Gregg Tavares All Rights Reserved.
Available via the MIT license.
see: http://github.com/greggman/twgl.js for details
-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta property="og:title" content="TWGL.js - tiny">
<meta property="og:type" content="website">
<meta property="og:image" content="http://twgljs.org/examples/screenshots/tiny.png">
<meta property="og:description" content="TWGL.js - tiny">
<meta property="og:url" content="http://twgljs.org">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@greggman">
<meta name="twitter:creator" content="@greggman">
<meta name="twitter:domain" content="twgljs.org">
<meta name="twitter:title" content="TWGL.js - tiny">
<meta name="twitter:url" content="http://twgljs.org/examples/tiny.html">
<meta name="twitter:description" content="TWGL.js - tiny">
<meta name="twitter:image:src" content="http://twgljs.org/examples/screenshots/tiny.png">
<link href="https://twgljs.org/resources/images/twgljs-icon.png" rel="shortcut icon" type="image/png">
<title>twgl.js - tiny</title>
<style>
body {
margin: 0;
font-family: monospace;
}
canvas {
display: block;
width: 100vw;
height: 100vh;
}
#b {
position: absolute;
top: : 10px;;
width: 100%;
text-align: center;
z-index: 2;
}
</style>
</head>
<body>
<canvas id="c" width="1680" height="855"></canvas>
<div id="b"><a href="https://twgljs.org/">twgl.js</a> - tiny</div>
<script id="vs" type="notjs">
attribute vec4 position;
void main() {
gl_Position = position;
}
</script>
<script id="fs" type="notjs">
</script>
<script type="module">
//alert(1);
function delme_loadShapeFunction() {
return fetch('/function')
.then(response => response.text().then(oneSrc => {
/* console.log(oneSrc); */
return fetch('/function')
.then(response => response.text().then(twoSrc => {
/* console.log(twoSrc); */
return fetch('/infinity-main.glsl')
.then(response => response.text().then(main => {
let oneFunc = "vec4 one(vec2 uv) {\n" + oneSrc + "\n}\n"
let twoFunc = "vec4 two(vec2 uv) {\n" + twoSrc + "\n}\n"
let functions = oneFunc + "\n" + twoFunc + "\n";
let all = main.replace("FUNCTIONS_HERE", functions);
/* let all = main + "\n" + oneSrc + "\n" + twoSrc; */
/* console.log("all");
* console.log(all);
*/
var script = document.createElement('script');
script.setAttribute("id", "fs");
script.setAttribute("type","notjs");
script.text = all;
document.head.appendChild(script);
// go();
}));
}));
}));
}
// loadShapeFunction().then(() => go());
async function getThing(url) {
return fetch(url).then(response => response.text());
}
const playDuration = 5.0;
const transitionDuration = 2.0;
let shapeOne = undefined;
let shapeTwo = undefined;
let shaderMain = undefined;
async function setup() {
shapeOne = await getThing('/function');
shapeTwo = await getThing('/function');
shaderMain = await getThing('/infinity-main.glsl');
installShader(assembleShader());
}
async function nextShader() {
shapeOne = shapeTwo;
shapeTwo = await getThing('/function');
installShader(assembleShader());
}
function assembleShader() {
let oneFunc = "vec4 one(vec2 uv, float time) {\n" + shapeOne + "\n}\n"
let twoFunc = "vec4 two(vec2 uv, float time) {\n" + shapeTwo + "\n}\n"
let functions = oneFunc + "\n" + twoFunc + "\n";
let all = shaderMain.replace("FUNCTIONS_HERE", functions);
return all;
}
function installShader(glsl) {
/* var script = document.createElement('script');
* script.setAttribute("id", "fs");
* script.setAttribute("type","notjs"); */
let script = document.getElementById('fs');
script.text = glsl;
document.body.appendChild(script);
}
await setup();
go();
/* async function getShape() {
* return getThing('/function');
* // return fetch('/function').then(response => response.text());
* }
*/
function delme_nextShader() {
return fetch('/function')
.then(response => response.text().then(src => {
return fetch('/infinity-main.glsl')
.then(response => response.text().then(main => {
let oneFunc = "vec4 one(vec2 uv) {\n" + oneSrc + "\n}\n"
let twoFunc = "vec4 two(vec2 uv) {\n" + twoSrc + "\n}\n"
let functions = oneFunc + "\n" + twoFunc + "\n";
let all = main.replace("FUNCTIONS_HERE", functions);
/* let all = main + "\n" + oneSrc + "\n" + twoSrc; */
/* console.log("all");
* console.log(all);
*/
var script = document.createElement('script');
script.setAttribute("id", "fs");
script.setAttribute("type","notjs");
script.text = all;
document.head.appendChild(script);
// go();
}));
}));
}
/* async function lalalawr() {
* const yeah = (await fetch('/function')).text();
* }
* const yeah = (await fetch('/function')).text();
* console.log("yeah");
* console.log(yeah);
*/
// works
/* function loadShapeFunction(elementId) {
* fetch('/function')
* .then(response => response.text().then(text => {
* console.log(text);
* var script = document.createElement('script');
* script.setAttribute("id", elementId);
* script.setAttribute("type","notjs");
* script.text = text
* document.head.appendChild(script);
* }));
* }
* loadShapeFunction('one');
*/
// Works
/* var script = document.createElement('script');
script.setAttribute("id","func_one")
script.setAttribute("type","notjs")
// script.text = "}}}}}";
script.text = "attribute vec4 position;\nvoid main() {\ngl_Position = position;\n}\n";
document.head.appendChild(script);
/* script.onload = function () {
* //do stuff with the script
* script.text = "alert(3);"
* alert(2);
* };
* script.src = "/function";
*/
// No idea
/* var js = document.createElement('script');
* js.setAttribute("id","func_one")
* js.setAttribute("type","notjs")
* js.src = '/function';
* var first = document.getElementsByTagName('script')[0];
* first.parentNode.insertBefore(js, first);
*/
// doesn't load it
/* var first = document.getElementById('vs');
* alert(first);
* first.src = "/function";
*/
/* var first = document.getElementsByTagName('vs')[0];
* //first.parentNode.insertBefore(js, first);
* alert(first);
*/
import * as twgl from './twgl-full.module.js';
function go() {
const gl = document.querySelector("#c").getContext("webgl");
let programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);
const arrays = {
position: [-1, -1, 0, 1, -1, 0, -1, 1, 0, -1, 1, 0, 1, -1, 0, 1, 1, 0],
};
const bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays);
let n = 0;
let firstTime = undefined;
let step = 0;
let lastLoadedStep = 0;
let nextedForStep = -1;
let numFramesDrawn = 0;
function render(time_) {
if (firstTime == undefined) {
firstTime = time_;
}
const time = time_ - firstTime;
if (n%100 == 0) {
console.log("time " + time + " step " + step + " nframes " + numFramesDrawn);
}
n++;
twgl.resizeCanvasToDisplaySize(gl.canvas);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
const uniforms = {
globalTime: time * 0.001,
resolution: [gl.canvas.width, gl.canvas.height],
step: step,
playDuration: playDuration,
transitionDuration: transitionDuration,
};
gl.useProgram(programInfo.program);
twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
twgl.setUniforms(programInfo, uniforms);
twgl.drawBufferInfo(gl, bufferInfo);
const shouldNext = step != nextedForStep && ((time / 1000.0) > ((step + 1) * (playDuration + transitionDuration)));
//if (time > 9000.0 && !nexted) {
if (shouldNext) {
//console.log("nexting " + time);
nextedForStep = step;
nextShader().then(() => {
programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);
step++;
//console.log("um " + shouldNext + " " + step + " " + nextedForStep + " " + ((step + 1) * (playDuration + transitionDuration)) + " " + time);
//console.log("nexted " + time);
});
}
numFramesDrawn ++;
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
</script>
</body></html>