-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader_editor.js
111 lines (97 loc) · 3.99 KB
/
shader_editor.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
export class ShaderEditor {
constructor(editorId, shaderManager) {
this.editor = ace.edit(editorId);
this.shaderManager = shaderManager;
this.setupEditor();
this.bindEditorEvents();
// Initialize shader code properties
this.vertexShaderCode = window.vertexShaderCode; // Initial vertex shader code
this.fragmentShaderCode = window.fragmentShaderCode; // Initial fragment shader code
this.currentShaderType = 'fragment'; // Default to fragment shader
}
setupEditor() {
this.editor.setTheme('ace/theme/mono_industrial');
this.editor.session.setMode('ace/mode/glsl');
this.editor.setFontSize(14);
const langTools = ace.require('ace/ext/language_tools');
this.editor.setOptions({
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true,
});
this.editor.completers = [
langTools.snippetCompleter,
langTools.keyWordCompleter,
langTools.textCompleter,
];
}
getFontSize() {
return this.editor.getFontSize();
}
setFontSize(size) {
this.editor.setFontSize(size);
}
getValue() {
return this.editor.getValue();
}
setValue(value) {
if (this.currentShaderType === 'vertex') {
this.vertexShaderCode = value;
} else {
this.fragmentShaderCode = value;
}
this.editor.setValue(value, -1);
}
getVertexShaderContent() {
return this.vertexShaderCode;
}
getFragmentShaderContent() {
return this.fragmentShaderCode;
}
downloadShaderCode() {
const shaderCode = this.getValue(); // Method to get the current shader code from the editor
const shaderType = this.currentShaderType; // 'vertex' or 'fragment'
const filename = shaderType === 'vertex' ? 'vertex_shader.glsl' : 'fragment_shader.glsl';
const blob = new Blob([shaderCode], { type: 'text/plain' });
const href = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = href;
link.download = filename; // Use the dynamic filename based on shader type
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(href);
}
updateEditorContent(isVertexShader) {
this.currentShaderType = isVertexShader ? 'vertex' : 'fragment';
const shaderCode = isVertexShader ? this.vertexShaderCode : this.fragmentShaderCode;
this.editor.setValue(shaderCode, -1);
}
bindEditorEvents() {
this.editor.session.on('change', () => {
const shaderType = document.getElementById('shaderType').value;
this.currentShaderType = shaderType; // Update the current shader type
const shaderCode = this.getValue();
// Save the current shader code based on the shader type
if (shaderType === 'vertex') {
this.vertexShaderCode = shaderCode;
} else {
this.fragmentShaderCode = shaderCode;
}
this.shaderManager.isVertexShader = (shaderType === 'vertex');
this.shaderManager.compileShader(shaderCode); // Compile the current shader code
});
// Handle shader type changes
document.getElementById('shaderType').addEventListener('change', (e) => {
const isVertexShader = e.target.value === 'vertex';
this.updateEditorContent(isVertexShader); // Update the editor with the corresponding shader code
// Trigger shader compilation for the newly selected shader type
const shaderCode = isVertexShader ? this.vertexShaderCode : this.fragmentShaderCode;
this.shaderManager.compileShader(shaderCode);
});
}
onSuccessfulShaderCompilation() {
clearEditorAnnotations(editor);
document.getElementById('shaderError').innerHTML = '';
}
}