-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.js
More file actions
283 lines (237 loc) · 8.47 KB
/
Copy pathmain.js
File metadata and controls
283 lines (237 loc) · 8.47 KB
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
'use strict';
import * as dat from 'datgui';
import Points, { RenderPass } from 'points';
import { shaderProjects } from './index_files/shader_projects.js';
import { isMobile, setDisabled } from 'utils';
import CanvasRecorder from './../src/CanvasRecorder.js';
const canvas = document.getElementById('canvas');
const gui = new dat.GUI({ name: 'Points GUI' });
const recorder = new CanvasRecorder(canvas);
const infoEl = document.getElementById('info');
const isM = isMobile();
const navLeft = document.getElementsByClassName('nav column left')[0];
if (isM) {
const menuBtn = document.getElementById('menu_btn')
menuBtn.classList.toggle('hide');
menuBtn.addEventListener('click', e => navLeft.classList.toggle('hide'));
gui.close();
infoEl.classList.toggle('mobile');
}
if (!isM) {
navLeft.classList.toggle('hide');
}
/**
* Gets all the uri parts in an array.
*/
const uriParts = () => window.location.hash.split('#').filter(s => s.length >= 0);
/**
* Changes uri.
* @param {string} uri
*/
function changeUri(uri) { window.history.pushState('', '', `index.html#${uri}`) };
/***************/
const stats = new Stats();
stats.showPanel(0); // 0: fps, 1: ms, 2: mb, 3+: custom
stats.dom.style.position = 'relative';
stats.dom.style.display = 'inline-block';
document.getElementsByClassName('right')[0].appendChild(stats.dom);
const capturer = new CCapture({
format: 'jpg',
//timeLimit: 10,
verbose: true
});
const FOLDER_NAME = 'Options'
let optionsFolder = gui.addFolder(FOLDER_NAME);
let statsVisible = (localStorage.getItem('stats-visible') === 'true') || false;
function setStatsVisibility(value) {
value && (stats.dom.style.display = 'inline-block');
!value && (stats.dom.style.display = 'none');
localStorage.setItem('stats-visible', value);
}
setStatsVisibility(statsVisible);
let stats2 = { 'visible': statsVisible };
gui.add(stats2, 'visible').name('Show Stats').onChange(value => setStatsVisibility(value));
let isFullscreenData = { 'isFullscreen': false };
let fullscreenCheck = gui.add(isFullscreenData, 'isFullscreen').name('Fullscreen').onChange(value => points.fullscreen = value);
document.addEventListener('fullscreenchange', e => {
let isFullscreen = window.innerWidth == screen.width && window.innerHeight == screen.height;
isFullscreenData.isFullscreen = isFullscreen;
fullscreenCheck.updateDisplay();
});
let isFitWindowData = { 'isFitWindow': false };
gui.add(isFitWindowData, 'isFitWindow').name('Fit Window').listen().onChange(value => {
points.fitWindow = value;
});
const shaderNames = {};
const nav = document.getElementById('nav');
const menuSectionNames = ['showcase', 'reference', 'animation', 'audio', 'color', 'debug', 'effects', 'image', 'math',
'noise2d', 'classicnoise2d', 'classicnoise3d', 'random', 'renderpasses', 'sdf', 'compute', 'raymarching', 'threed', 'mesh', 'glb'];
const menuSections = [];
menuSectionNames.forEach(name => {
menuSections.push({
name,
el: nav.querySelector(`.${name}`)
})
})
let lastSelected = null;
const onClickNavItem = e => {
if (isM) {
navLeft.classList.toggle('hide');
}
lastSelected?.classList.remove('selected');
e.target.classList.add('selected');
lastSelected = e.target;
loadShaderByIndex(e.target.index)
}
const createListItem = (item, index) => {
const li = document.createElement('li');
const a = document.createElement('a');
a.href = `#${item.uri}`;
a.innerHTML = item.name;
a.index = index;
a.addEventListener('click', onClickNavItem);
li.appendChild(a);
return li;
}
shaderProjects
// .filter(item => item.enabled)
// .filter(item => item.tax == 'showcase')
.forEach((item, index) => {
if (!item.enabled) {
return;
}
shaderNames[item.name] = index;
menuSections.forEach(section => {
const { name, el } = section;
if (item.tax.split(' ').find(t => t === name)) {
el.appendChild(createListItem(item, index));
}
})
});
let selectedShader = { index: Number(localStorage.getItem('selected-shader')) || 0 }
const sourceBtn = document.getElementById('source_btn');
const titleInfoEl = infoEl.querySelector('#info-title');
const descInfoEl = infoEl.querySelector('#info-desc');
const authorInfoEl = infoEl.querySelector('#info-author');
const authorLinkEl = infoEl.querySelector('#author-link');
let requestToCancel = false;
async function loadShaderByIndex(index) {
// disable ui
setDisabled(nav, true);
console.clear();
requestToCancel = true;
if (index > shaderProjects.length) {
index = 0;
}
localStorage.setItem('selected-shader', index);
const shaderProject = shaderProjects[index];
sourceBtn.href = `https://github.com/Absulit/points/tree/master/examples/${shaderProject.uri}`;
titleInfoEl.innerHTML = shaderProject.name;
descInfoEl.innerHTML = shaderProject.desc;
authorInfoEl.innerHTML = shaderProject.author;
authorLinkEl.href = shaderProject.authlink;
isFitWindowData.isFitWindow = shaderProject.fitWindow;
changeUri(shaderProject.uri);
shaders?.remove?.();
points?.reset();
shaders = (await import(shaderProject.path)).default;
await init();
}
async function loadShaderByURI() {
const parts = uriParts();
let index = shaderProjects.findIndex(s => s.uri == parts[1]);
if (index == -1) {
index = selectedShader.index;
} else {
selectedShader.index = index;
}
lastSelected = Array.from(document.querySelectorAll('#nav a')).filter(a => a.index == index)[0];
lastSelected?.classList.add('selected');
}
//---
const recordingFolder = gui.addFolder('Recording');
recordingFolder.open();
const recordingOptions = [
{
nameStopped: 'CCapture (slow/HQ)',
nameStarted: 'RECORDING (STOP)',
fn: function (e) {
this.started = !this.started;
if (this.started) {
this.controller.name(this.nameStarted);
capturer.start();
} else {
this.controller.name(this.nameStopped);
// stop and download
capturer.stop();
// default save, will download automatically a file called {name}.extension (webm/gif/tar)
capturer.save();
}
},
started: false,
controller: null
},
{
nameStopped: 'Live Capture (fast/LQ)',
nameStarted: 'RECORDING (STOP)',
fn: function (e) {
this.started = !this.started;
if (this.started) {
this.controller.name(this.nameStarted);
recorder.start();
} else {
this.controller.name(this.nameStopped);
recorder.stop();
}
},
started: false,
controller: null
},
{
nameStopped: 'Download PNG Image',
fn: _ => recorder.getPNG(),
},
];
recordingOptions.forEach(recordingOption => {
recordingOption.controller = recordingFolder.add(recordingOption, 'fn').name(recordingOption.nameStopped)
});
/***************/
/** @type {Points} */
let points;
let shaders;
await loadShaderByURI();
async function init() {
requestToCancel = true;
if (!points) {
points = new Points('canvas');
}
gui.removeFolder(optionsFolder);
optionsFolder = gui.addFolder(FOLDER_NAME);
await shaders.init(points, optionsFolder);
const renderPasses = shaders.renderPasses || [new RenderPass(shaders.vert, shaders.frag, shaders.compute)];
// await points.addPostRenderPass(RenderPasses.GRAYSCALE);
if (await points.init(renderPasses)) {
requestToCancel = false;
points.fitWindow = isFitWindowData.isFitWindow;
points.update(update);
// enable ui
setDisabled(nav, false);
} else {
const el = document.getElementById('nowebgpu');
el.classList.toggle('show');
}
}
async function update(t, dt) {
if (requestToCancel) {
points.update(null);
return
}
stats.begin();
// code here
await shaders.update(points, t, dt);
await shaders.read?.(points);
//
stats.end();
capturer.capture(points.canvas);
}
loadShaderByIndex(selectedShader.index);