-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
92 lines (74 loc) · 3.52 KB
/
main.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
import { SceneSetup } from './scene.js';
import { ShaderManager } from './shader_manager.js';
import { ShaderEditor } from './shader_editor.js';
import { UIController } from './ui_controller.js';
document.addEventListener('DOMContentLoaded', () => {
const modal = document.getElementById('helpModal');
modal.style.display = 'block';
document.querySelector('.close-button').onclick = function() {
modal.style.display = 'none';
};
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = 'none';
}
};
const originalConsoleError = console.error;
console.error = function (message, ...optionalParams) {
if (message.includes('THREE.WebGLProgram: Shader Error')) {
let formattedMessage = `<strong>Error:</strong> ${message}<br>`;
optionalParams.forEach(param => {
formattedMessage += `<code>${param}</code><br>`;
});
document.getElementById('shaderErrorContent').innerHTML = formattedMessage;
const toggleButton = document.getElementById('toggleShaderError');
if (toggleButton) {
toggleButton.classList.add('error', 'blink');
toggleButton.textContent = 'view compile errors!';
}
} else {
originalConsoleError.apply(console, [message, ...optionalParams]);
}
};
async function setupApplication() {
try {
const vshResponse = await fetch('./snippets/default_vertex_shader.glsl');
const fshResponse = await fetch('./snippets/default_fragment_shader.glsl');
if (!vshResponse.ok || !fshResponse.ok) {
throw new Error('Shader file fetch failed');
}
window.vertexShaderCode = await vshResponse.text();
window.fragmentShaderCode = await fshResponse.text();
await loadShadersFromLocalStorage();
const sceneSetup = new SceneSetup();
const shaderManager = new ShaderManager(sceneSetup.scene, window.material);
const editor = new ShaderEditor('editor', shaderManager);
const uiController = new UIController(editor, shaderManager);
editor.setValue(window.fragmentShaderCode);
sceneSetup.animate();
attachEditorEventListeners(editor);
} catch (error) {
console.error('An error occurred during application setup:', error);
}
}
function saveShadersToLocalStorage(vertexShaderContent, fragmentShaderContent) {
localStorage.setItem('vertexShaderContent', vertexShaderContent);
localStorage.setItem('fragmentShaderContent', fragmentShaderContent);
}
async function loadShadersFromLocalStorage() {
const savedVertexShader = localStorage.getItem('vertexShaderContent');
const savedFragmentShader = localStorage.getItem('fragmentShaderContent');
window.vertexShaderCode = savedVertexShader || window.vertexShaderCode;
window.fragmentShaderCode = savedFragmentShader || window.fragmentShaderCode;
}
function attachEditorEventListeners(editor) {
editor.editor.session.on('change', () => {
const vertexShaderContent = editor.getVertexShaderContent();
const fragmentShaderContent = editor.getFragmentShaderContent();
saveShadersToLocalStorage(vertexShaderContent, fragmentShaderContent);
});
}
setupApplication().catch(error => {
console.error('An error occurred:', error);
});
});