-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webgl.html
408 lines (364 loc) · 21.4 KB
/
webgl.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
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
408
<!DOCTYPE html>
<!--
// "Three.js ShaderToy Harness"
jakedowns 1/1/24 7:07 PM ET
source: https://github.com/jakedowns/starpages/blob/main/webgl.html
live: https://jakedowns.github.io/starpages/webgl.html
-->
<html>
<head>
<meta charset="utf-8">
<title>WebGL.html</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://jakedowns.github.io/starpages/res/CCapture.all.min.js"></script>
<script src="./res/import/dat.gui.min.js"></script>
<!-- TODO:
https://threejs.org/examples/#webgl_video_panorama_equirectangular
https://threejs.org/examples/#webgl_materials_video
https://threejs.org/examples/#webgl_effects_anaglyph
add fog from: https://threejs.org/examples/#webgl_geometry_dynamic
-->
<style>
body { margin: 0; background-color: #000; overflow: hidden; }
canvas { width: 100%; height: 100%; background-color: #0FF; }
#pasteFromClipboardPrompt {
position: absolute;
top: 0;
left: 0;
width: calc(100% - 100px);
height: 50px;
opacity: 0;
transition: opacity 0.5s ease-in-out;
pointer-events: none;
/* transition: opacity 0.5s ease-in-out; */
}
#pasteFromClipboardPrompt.show {
opacity: 1;
pointer-events: all;
}
#controls {
position: absolute;
right: 0;
top: 0;
width: 100px;
height: 100%;
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: stretch;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="controls">
<input type="file" accept="image/*" id="fileupload" />
<progress id="progress" value="0" max="100"></progress>
<button onclick="window.startCapture()">record video</button>
</div>
<div id="pasteFromClipboardPrompt">
<div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center;">
<h1>Paste from clipboard?</h1>
<p>Press <kbd>Ctrl</kbd>+<kbd>V</kbd> to paste the image from your clipboard.</p>
<p>Press <kbd>Esc</kbd> to dismiss this message.</p>
<button onclick="window.doPasteFromClipboard()">Tap Here</button>
</div>
</div>
<script>
window.frame_blend_mode = "additive";
window.do_clear_frame = false;
window.maxFrames = 120 * 10;
window.image_height = window.innerHeight;
window.progress = document.getElementById("progress");
function setupCapturing(){
try{
// Create a capturer that exports a WebM video
var isWebMSupported = document.createElement('video').canPlayType('video/webm; codecs="vp8, vorbis"');
var motionBlurFrames = 0;//2;
capturer = isWebMSupported
? new CCapture( {
format: 'webm',
motionBlurFrames,
verbose: false,
fps: 120
} )
: new CCapture( {
format: 'png',
motionBlurFrames,
verbose: false,
framerate: 120
} );
}catch(e){
console.error('failed to create capturer',e);
}
window.startCapture = function(){
if(!window.capturer){
console.warn("capturer not initialized");
return;
}
//window.maxFrames = 120 * image_height
console.warn('image_height',{
image_height: window.image_height,
maxFrames: window.maxFrames,
height: window.innerHeight
})
window.capturing = true;
window.capturedFrames = 0;
window.capturer.start();
animate();
}
window.stopCapture = function(){
if(!window.capturer){
console.warn("capturer not initialized");
return;
}
window.capturing = false;
window.capturer.stop();
window.capturer.save();
}
}
document.addEventListener("DOMContentLoaded",async()=>{
setupCapturing();
// use "await" to fetch "./web-fragment.glsl";
// assign to window.fragmentShader
window.fragmentShader = await (await fetch("./webgl-fragment.glsl")).text();
const pasteFromClipboardPrompt = document.getElementById("pasteFromClipboardPrompt");
function showPasteFromClipboardPrompt(){
pasteFromClipboardPrompt.classList.add("show");
setTimeout(()=>{
pasteFromClipboardPrompt.classList.remove("show");
},5000);
}
function hidePasteFromClipboardPrompt(){
pasteFromClipboardPrompt.classList.remove("show");
}
let didShowClipboardPrompt = false;
let clipboardCheckInterval = setInterval(async()=>{
if(didShowClipboardPrompt){
return;
}
didShowClipboardPrompt = true;
try{
const text = await navigator.clipboard.readText();
if(text){
showPasteFromClipboardPrompt();
}else{
hidePasteFromClipboardPrompt();
console.warn('nothing in clipboard')
}
}catch(e){
console.error(e);
}
},1000);
/* adds paste support for Clipboard ImageBlob -> Three.js Texture */
window.doPasteFromClipboard = async function(){
try{
const items = await navigator.clipboard.read();
console.warn('items',items)
for (const item of items) {
if (item.types.includes('image/png')) {
const blob = await item.getType('image/png');
const url = URL.createObjectURL(blob);
texture.image.onload = function() {
URL.revokeObjectURL(url);
texture.needsUpdate = true;
};
texture.image.src = url;
break;
}
// or if ti's a base64 image in a text string, pass _that_ to image.src
if (item.types.includes('text/plain')) {
const text = await item.getType('text/plain');
texture.image.onload = function() {
URL.revokeObjectURL(url);
texture.needsUpdate = true;
};
texture.image.src = text;
break;
}
}
}catch(e){
console.error(e);
}
}
document.addEventListener("keydown",(e)=>{
if(e.key == "v" && (e.ctrlKey || e.metaKey)){
try{
window.doPasteFromClipboard();
}catch(e){
console.error(e);
}
}
},false);
document.getElementById("fileupload").addEventListener("change", (e) => {
const file = e.target.files[0];
if (file.type.indexOf('image') === 0) {
const reader = new FileReader();
reader.onload = (e) => {
texture.image.src = e.target.result;
texture.needsUpdate = true;
};
reader.readAsDataURL(file);
}
});
const canvas = document.getElementById("canvas");
const renderer = new THREE.WebGLRenderer({canvas});
// Orthographic camera setup
const frustumSize = 1;
const aspect = canvas.clientWidth / canvas.clientHeight;
const camera = new THREE.OrthographicCamera(
frustumSize * aspect / -2,
frustumSize * aspect / 2,
frustumSize / 2,
frustumSize / -2,
1,
1000
);
camera.position.z = 2;
const scene = new THREE.Scene();
// Full-screen plane geometry
const planeGeometry = new THREE.PlaneGeometry(2 * aspect, 2);
const textureLoader = new THREE.TextureLoader();
let rand_int_1_thru_17 = Math.floor(Math.random() * 17) + 1;
//let url = `./res/bg_${rand_int_1_thru_17}.png`;
//let url = "./res/download (3).png";
let url = "./res/download (10).png";
const texture = textureLoader.load(url);
// Assuming you have a texture loaded into a variable named 'texture'
texture.wrapS = THREE.MirroredRepeatWrapping; // For horizontal mirroring
texture.wrapT = THREE.MirroredRepeatWrapping; // For vertical mirroring
window.image_height = texture.height;
// Alternatively, use RepeatWrapping for wrap-around effect
// texture.wrapS = THREE.RepeatWrapping;
// texture.wrapT = THREE.RepeatWrapping;
// Set how many times the texture should repeat
// texture.repeat.set(1, 1); // Adjust as needed
// Custom Shader
const shaderMaterial = new THREE.ShaderMaterial({
uniforms: {
u_texture: { value: texture },
u_time: { value: 1.0 },
u_resolution: { value: new THREE.Vector2(window.innerWidth, window.innerHeight) },
u_mouse: { value: new THREE.Vector2(0.0, 0.0) },
// Add other uniforms here
},
vertexShader: `
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`,
fragmentShader: fragmentShader,
transparent: true, // Enable transparency
});
// Creating and adding the plane to the scene
const plane = new THREE.Mesh(planeGeometry, shaderMaterial);
scene.add(plane);
function resizeRendererToDisplaySize(renderer) {
const canvas = renderer.domElement;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let width = canvas.clientWidth;
let height = canvas.clientHeight;
// force to even dimensions for ffmpeg compatibility
width = 2 * Math.floor(width / 2);
height = 2 * Math.floor(height / 2);
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
window.mouseX = window.mouseY = 0;
document.addEventListener('mousemove', (e)=>{
window.mouseX = e.clientX;
window.mouseY = e.clientY;
});
function updateMouseUniform() {
// Normalize coordinates (0 to 1)
let normalizedX = window.mouseX / window.innerWidth;
let normalizedY = window.mouseY / window.innerHeight;
// Adjust for aspect ratio
const aspect = window.innerWidth / window.innerHeight;
if (aspect > 1) {
// Wider than tall
normalizedX = (normalizedX - 0.5) * aspect + 0.5;
} else {
// Taller than wide
normalizedY = (normalizedY - 0.5) / aspect + 0.5;
}
// offset to center of "quad"
normalizedX = normalizedX * 2 - 1;
normalizedY = normalizedY * 2 - 1;
// Set the shader uniform
shaderMaterial.uniforms.u_mouse.value.set(normalizedX, normalizedY);
}
window.animate = function(time) {
if (resizeRendererToDisplaySize(renderer)) {
const canvas = renderer.domElement;
// enforce dimensions of 2 for ffmpeg compatibility
canvas.width = 2 * Math.floor(canvas.clientWidth / 2);
canvas.height = 2 * Math.floor(canvas.clientHeight / 2);
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
shaderMaterial.uniforms.u_time.value = time * 0.001; // Convert time to seconds
shaderMaterial.uniforms.u_resolution.value.set(window.innerWidth, window.innerHeight);
updateMouseUniform();
if(window.do_clear_frame){
}else{
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
if(window.capturing){
window.capturer.capture(canvas);
window.capturedFrames++;
progress.value = (window.capturedFrames / maxFrames) * 100;
if(window.capturedFrames >= maxFrames){
window.stopCapture();
}
}
}
requestAnimationFrame(animate);
// when a user drops an image on the canvas, load it to our texture
canvas.addEventListener('drop', (e) => {
e.preventDefault();
e.stopPropagation();
if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {
const file = e.dataTransfer.files[0];
if (file.type.indexOf('image') === 0) {
const reader = new FileReader();
reader.onload = (e) => {
texture.image.src = e.target.result;
texture.needsUpdate = true;
};
reader.readAsDataURL(file);
}
}
});
canvas.addEventListener('dragover', (e) => {
e.preventDefault();
e.stopPropagation();
});
document.addEventListener("resize",()=>{
const canvas = document.getElementById("canvas");
const renderer = new THREE.WebGLRenderer({canvas,alpha:true});
const width = canvas.clientWidth;
const height = canvas.clientHeight;
renderer.setSize(width, height, false);
})
});
</script>
<style>
/* here comes dat gui */
.dg ul{list-style:none;margin:0;padding:0;width:100%;clear:both}.dg.ac{position:fixed;top:0;left:0;right:0;height:0;z-index:0}.dg:not(.ac) .main{overflow:hidden}.dg.main{-webkit-transition:opacity .1s linear;-o-transition:opacity .1s linear;-moz-transition:opacity .1s linear;transition:opacity .1s linear}.dg.main.taller-than-window{overflow-y:auto}.dg.main.taller-than-window .close-button{opacity:1;margin-top:-1px;border-top:1px solid #2c2c2c}.dg.main ul.closed .close-button{opacity:1 !important}.dg.main:hover .close-button,.dg.main .close-button.drag{opacity:1}.dg.main .close-button{-webkit-transition:opacity .1s linear;-o-transition:opacity .1s linear;-moz-transition:opacity .1s linear;transition:opacity .1s linear;border:0;line-height:19px;height:20px;cursor:pointer;text-align:center;background-color:#000}.dg.main .close-button.close-top{position:relative}.dg.main .close-button.close-bottom{position:absolute}.dg.main .close-button:hover{background-color:#111}.dg.a{float:right;margin-right:15px;overflow-y:visible}.dg.a.has-save>ul.close-top{margin-top:0}.dg.a.has-save>ul.close-bottom{margin-top:27px}.dg.a.has-save>ul.closed{margin-top:0}.dg.a .save-row{top:0;z-index:1002}.dg.a .save-row.close-top{position:relative}.dg.a .save-row.close-bottom{position:fixed}.dg li{-webkit-transition:height .1s ease-out;-o-transition:height .1s ease-out;-moz-transition:height .1s ease-out;transition:height .1s ease-out;-webkit-transition:overflow .1s linear;-o-transition:overflow .1s linear;-moz-transition:overflow .1s linear;transition:overflow .1s linear}.dg li:not(.folder){cursor:auto;height:27px;line-height:27px;padding:0 4px 0 5px}.dg li.folder{padding:0;border-left:4px solid rgba(0,0,0,0)}.dg li.title{cursor:pointer;margin-left:-4px}.dg .closed li:not(.title),.dg .closed ul li,.dg .closed ul li>*{height:0;overflow:hidden;border:0}.dg .cr{clear:both;padding-left:3px;height:27px;overflow:hidden}.dg .property-name{cursor:default;float:left;clear:left;width:40%;overflow:hidden;text-overflow:ellipsis}.dg .cr.function .property-name{width:100%}.dg .c{float:left;width:60%;position:relative}.dg .c input[type=text]{border:0;margin-top:4px;padding:3px;width:100%;float:right}.dg .has-slider input[type=text]{width:30%;margin-left:0}.dg .slider{float:left;width:66%;margin-left:-5px;margin-right:0;height:19px;margin-top:4px}.dg .slider-fg{height:100%}.dg .c input[type=checkbox]{margin-top:7px}.dg .c select{margin-top:5px}.dg .cr.function,.dg .cr.function .property-name,.dg .cr.function *,.dg .cr.boolean,.dg .cr.boolean *{cursor:pointer}.dg .cr.color{overflow:visible}.dg .selector{display:none;position:absolute;margin-left:-9px;margin-top:23px;z-index:10}.dg .c:hover .selector,.dg .selector.drag{display:block}.dg li.save-row{padding:0}.dg li.save-row .button{display:inline-block;padding:0px 6px}.dg.dialogue{background-color:#222;width:460px;padding:15px;font-size:13px;line-height:15px}#dg-new-constructor{padding:10px;color:#222;font-family:Monaco, monospace;font-size:10px;border:0;resize:none;box-shadow:inset 1px 1px 1px #888;word-wrap:break-word;margin:12px 0;display:block;width:440px;overflow-y:scroll;height:100px;position:relative}#dg-local-explain{display:none;font-size:11px;line-height:17px;border-radius:3px;background-color:#333;padding:8px;margin-top:10px}#dg-local-explain code{font-size:10px}#dat-gui-save-locally{display:none}.dg{color:#eee;font:11px 'Lucida Grande', sans-serif;text-shadow:0 -1px 0 #111}.dg.main::-webkit-scrollbar{width:5px;background:#1a1a1a}.dg.main::-webkit-scrollbar-corner{height:0;display:none}.dg.main::-webkit-scrollbar-thumb{border-radius:5px;background:#676767}.dg li:not(.folder){background:#1a1a1a;border-bottom:1px solid #2c2c2c}.dg li.save-row{line-height:25px;background:#dad5cb;border:0}.dg li.save-row select{margin-left:5px;width:108px}.dg li.save-row .button{margin-left:5px;margin-top:1px;border-radius:2px;font-size:9px;line-height:7px;padding:4px 4px 5px 4px;background:#c5bdad;color:#fff;text-shadow:0 1px 0 #b0a58f;box-shadow:0 -1px 0 #b0a58f;cursor:pointer}.dg li.save-row .button.gears{background:#c5bdad url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAANCAYAAAB/9ZQ7AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQJJREFUeNpiYKAU/P//PwGIC/ApCABiBSAW+I8AClAcgKxQ4T9hoMAEUrxx2QSGN6+egDX+/vWT4e7N82AMYoPAx/evwWoYoSYbACX2s7KxCxzcsezDh3evFoDEBYTEEqycggWAzA9AuUSQQgeYPa9fPv6/YWm/Acx5IPb7ty/fw+QZblw67vDs8R0YHyQhgObx+yAJkBqmG5dPPDh1aPOGR/eugW0G4vlIoTIfyFcA+QekhhHJhPdQxbiAIguMBTQZrPD7108M6roWYDFQiIAAv6Aow/1bFwXgis+f2LUAynwoIaNcz8XNx3Dl7MEJUDGQpx9gtQ8YCueB+D26OECAAQDadt7e46D42QAAAABJRU5ErkJggg==) 2px 1px no-repeat;height:7px;width:8px}.dg li.save-row .button:hover{background-color:#bab19e;box-shadow:0 -1px 0 #b0a58f}.dg li.folder{border-bottom:0}.dg li.title{padding-left:16px;background:#000 url(data:image/gif;base64,R0lGODlhBQAFAJEAAP////Pz8////////yH5BAEAAAIALAAAAAAFAAUAAAIIlI+hKgFxoCgAOw==) 6px 10px no-repeat;cursor:pointer;border-bottom:1px solid rgba(255,255,255,0.2)}.dg .closed li.title{background-image:url(data:image/gif;base64,R0lGODlhBQAFAJEAAP////Pz8////////yH5BAEAAAIALAAAAAAFAAUAAAIIlGIWqMCbWAEAOw==)}.dg .cr.boolean{border-left:3px solid #806787}.dg .cr.color{border-left:3px solid}.dg .cr.function{border-left:3px solid #e61d5f}.dg .cr.number{border-left:3px solid #2FA1D6}.dg .cr.number input[type=text]{color:#2FA1D6}.dg .cr.string{border-left:3px solid #1ed36f}.dg .cr.string input[type=text]{color:#1ed36f}.dg .cr.function:hover,.dg .cr.boolean:hover{background:#111}.dg .c input[type=text]{background:#303030;outline:none}.dg .c input[type=text]:hover{background:#3c3c3c}.dg .c input[type=text]:focus{background:#494949;color:#fff}.dg .c .slider{background:#303030;cursor:ew-resize}.dg .c .slider-fg{background:#2FA1D6;max-width:100%}.dg .c .slider:hover{background:#3c3c3c}.dg .c .slider:hover .slider-fg{background:#44abda}
</style>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-GYK2ZMX0M9"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-GYK2ZMX0M9');
</script>
</body>
</html>