forked from CharStiles/shaderplace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.js
209 lines (170 loc) · 4.59 KB
/
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
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
/* eslint-env browser */
// @ts-ignore
import CodeMirror from "codemirror";
import * as Y from "yjs";
import { WebsocketProvider } from "y-websocket";
import { CodemirrorBinding } from "y-codemirror";
import "codemirror/mode/clike/clike.js";
import __fragmentShader from "./fragmentShader.js";
import * as THREE from "three";
var container;
var threeCam, scene, renderer;
var uniforms;
var gl;
var editor;
// meow globals
var geometry;
var material;
var mesh;
var socket;
var isDirty = false;
function initYdoc() {
console.log("in init doc")
const ydoc = new Y.Doc();
var room = document.getElementById("room").value;
const provider = new WebsocketProvider(
"wss://demos.yjs.dev",
room,
ydoc
);
const editorContainer = document.createElement("div");
editorContainer.setAttribute("id", "editor");
document.body.insertBefore(editorContainer, null);
editorContainer.insertAdjacentHTML("beforebegin", "<mark>")
editorContainer.insertAdjacentHTML("afterend", "</mark>")
editor = CodeMirror(editorContainer, {
value: _fragmentShader,
lineNumbers: true,
mode: "x-shader/x-vertex"
});
const ytext = ydoc.getText("codemirror");
// const undoManager = new Y.UndoManager(ytext, { trackedOrigins: new Set([ydoc.clientID]) })
// ytext.delete(0)
//ytext.insert(0, _fragmentShader)
const binding = new CodemirrorBinding(ytext, editor, provider.awareness);
const setDefaultVal = () => {
if (ytext.toString() === "") {
ytext.insert(0, _fragmentShader);
}
};
if (provider.synced) {
setDefaultVal();
} else {
provider.once("synced", setDefaultVal);
}
editor.getDoc().markText(
{
line: 5,
ch: 1
},
{
line: 50,
ch: 3
},
{
css: "color : red"
}
);
// @ts-ignore
window.example = { provider, ydoc, ytext, binding, Y };
editor.on("change", onEdit);
onEdit();
init();
animate();
}
// this function will trigger a change to the editor
function onEdit() {
const fragmentCode = editor.getValue();
updateShader(fragmentCode);
}
function updateShader(fragmentCode) {
if (!checkFragmentShader(fragmentCode)) {
return;
}
console.log("did update");
_fragmentShader = fragmentCode;
isDirty = true;
}
function updateScene() {
scene = new THREE.Scene();
geometry = new THREE.PlaneBufferGeometry(2, 2);
try {
material = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: vertexShader(),
fragmentShader: fragmentShader()
});
} catch (e) {
console.log("MY ERROR", e);
return;
}
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
// shaderDirtyFlag = false;
}
window.onload = (event) => {
var goButton = document.getElementById("goButton");
goButton.onclick = initYdoc;
}
function init() {
container = document.getElementById("container");
threeCam = new THREE.Camera();
threeCam.position.z = 1;
// video = document.querySelector( 'video' );
// feed = new THREE.VideoTexture( video );
uniforms = {
u_time: { type: "f", value: 1.0 },
u_resolution: { type: "v2", value: new THREE.Vector2() },
u_mouse: { type: "v2", value: new THREE.Vector2() },
u_camRot: { type: "v3", value: new THREE.Vector3() }
// u_feed: {type: "", value: new THREE.VideoTexture(video)}
};
updateScene();
container = document.getElementById("container");
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
gl = renderer.getContext();
container.appendChild(renderer.domElement);
onWindowResize();
window.addEventListener("resize", onWindowResize, false);
}
function onWindowResize(event) {
renderer.setSize(window.innerWidth, window.innerHeight);
uniforms.u_resolution.value.x = renderer.domElement.width;
uniforms.u_resolution.value.y = renderer.domElement.height;
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
if (isDirty) {
updateScene();
}
uniforms.u_time.value += 0.05;
// uniforms.u_feed.value = feed;
renderer.render(scene, threeCam);
}
function vertexShader() {
return `
void main() {
gl_Position = vec4( position, 1.0 );
}
`;
}
function fragmentShader() {
return _fragmentShader;
}
// this returns false if the fragment shader cannot compile
// true if it can
function checkFragmentShader(shaderCode) {
if (!gl) {
return;
}
let shader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(shader, shaderCode);
gl.compileShader(shader);
let result = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!result) console.log(gl.getShaderInfoLog(shader));
return result;
}