-
Notifications
You must be signed in to change notification settings - Fork 5
/
editor.js
240 lines (212 loc) · 7.25 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
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
const errorParser = require('gl-shader-errors')
const triangle = require('a-big-triangle')
const tokenize = require('glsl-tokenizer')
const CodeMirror = require('codemirror')
const insert = require('defaultcss')
const unescape = require('unescape')
const Shader = require('gl-shader')
const Fit = require('canvas-fit')
const fs = require('fs')
require('./editor-mode-glsl')(CodeMirror)
insert('cmtheme', fs.readFileSync(require.resolve('codemirror/theme/xq-light.css'), 'utf8'))
insert('cm-main', fs.readFileSync(require.resolve('codemirror/lib/codemirror.css'), 'utf8'))
module.exports = createEditor
function createEditor () {
var container = document.querySelector('.editor')
var data = JSON.parse(unescape(document.getElementById('chapter-data').innerHTML))
var vert = `
precision mediump float;
attribute vec2 position;
varying vec2 uv;
void main() {
uv = position * 0.5 + 0.5;
gl_Position = vec4(position, 1, 1);
}
`
var lessonKey = 'lesson:' + data.name
var cOpts = { preserveDrawingBuffer: true }
var displayQuestion = document.querySelector('#canvas-us')
var displaySolution = document.querySelector('#canvas-them')
var canvasQuestion = displayQuestion.appendChild(document.createElement('canvas'))
var canvasSolution = displaySolution.appendChild(document.createElement('canvas'))
var glq = canvasQuestion.getContext('webgl', cOpts) || canvasQuestion.getContext('experimental-webgl', cOpts)
var gls = canvasSolution.getContext('webgl', cOpts) || canvasSolution.getContext('experimental-webgl', cOpts)
glq.getExtension('OES_standard_derivatives')
gls.getExtension('OES_standard_derivatives')
var shaderQuestion = Shader(glq, vert, getFrag(data.question))
var shaderSolution = Shader(gls, vert, getFrag(data.solution))
var currTime = 0
var start = Date.now()
var shape = []
var fitQuestion = Fit(canvasQuestion)
var fitSolution = Fit(canvasSolution)
var rem = 16
function resize (e) {
var height = window.innerHeight - 5 * rem - 2
displayQuestion.parentNode.style.minWidth = Math.ceil(height / 2) + 'px'
fitQuestion(e)
fitSolution(e)
canvasSolution.width = canvasQuestion.width
canvasSolution.height = canvasQuestion.height
matchOffset = 0
}
function loop () {
currTime = (Date.now() - start) / 100
window.requestAnimationFrame(loop)
draw(glq, shaderQuestion)
draw(gls, shaderSolution)
matchPoll()
}
// omg loads of work to compare buffers
var matchLabel = document.querySelector('.mainui .checker span')
var pixelBuffer1 = new Uint8Array(4 * 512 * 512)
var pixelBuffer2 = new Uint8Array(4 * 512 * 512)
var failedMatch = false
var passedMatch = false
var matchOffset = 0
var matchStepSize = 16
var incr = 0
var prefixLineCount = (data.prefix || '').split('\n').length + 2
if (checkBanned(data.question)) failedMatch = true
function matchPoll () {
var width = canvasSolution.width
var height = canvasSolution.height
if (passedMatch) return
if (failedMatch) return
if ((incr++ % 5)) return
if (matchOffset + matchStepSize >= height) {
passedMatch = true
matchLabel.innerHTML = 'Got it! Nice work :D'
matchLabel.parentNode.style.background = '#69e61b'
var doneMark = document.querySelector('.done-mark[data-name="' + data.name + '"]')
if (doneMark) {
doneMark.classList.add('is-done')
doneMark.parentNode.classList.add('faded')
}
if (window.localStorage) {
window.localStorage.setItem(lessonKey, String(true))
}
return
}
var threshold = 3 * 0.01 * 255
var total = 3 * width * matchStepSize
var missed = 0
glq.readPixels(0, matchOffset, width, matchStepSize, glq.RGBA, glq.UNSIGNED_BYTE, pixelBuffer1)
gls.readPixels(0, matchOffset, width, matchStepSize, gls.RGBA, gls.UNSIGNED_BYTE, pixelBuffer2)
for (var y = 0, i = 0; y < matchStepSize; y++) {
for (var x = 0; x < width; x++, i++) {
var r1 = pixelBuffer1[i], r2 = pixelBuffer2[i++]
var g1 = pixelBuffer1[i], g2 = pixelBuffer2[i++]
var b1 = pixelBuffer1[i], b2 = pixelBuffer2[i++]
var diff = Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2)
if (diff > threshold) missed++
}
}
var error = missed / total
if (error > 0.01) {
failedMatch = true
matchLabel.innerHTML = ''
return
}
var completion = 100 * matchOffset / height
matchLabel.innerHTML = 'Checking Answer: ' + completion.toFixed(2) + '%...'
matchOffset += matchStepSize
}
function draw (gl, shader) {
var width = gl.canvas.width
var height = gl.canvas.height
gl.viewport(0, 0, width, height)
shape[0] = width
shape[1] = height
shader.bind()
shader.uniforms.iResolution = shape
shader.uniforms.iGlobalTime = currTime
gl.clearColor(0, 0, 0, 1)
gl.clear(gl.COLOR_BUFFER_BIT)
triangle(gl)
}
var editor = new CodeMirror(container, {
value: data.question,
theme: 'xq-light',
viewportMargin: Infinity,
lineNumbers: true,
gutters: [
'shaderError',
'CodeMirror-linenumbers'
]
})
var noop = function(){}
editor.on('change', function () {
var frag = getFrag(editor.getValue())
var warn = console.warn
editor.clearGutter('shaderError')
try {
console.warn = noop
shaderQuestion.update(vert, frag)
console.warn = warn
} catch (e) {
var errors = errorParser(e.rawError)
for (var i = 0; i < errors.length; i++) {
var err = errors[i]
var line = err.line - prefixLineCount
var el = document.createElement('div')
el.style.width = '8px'
el.style.height = '8px'
el.style.borderRadius = '8px'
el.style.background = '#f00'
el.style.marginTop = '6px'
el.title = errors[i].message
editor.setGutterMarker(line - 1, 'shaderError', el)
}
return
}
failedMatch = checkBanned(editor.getValue())
passedMatch = false
matchOffset = 0
})
function getFrag (src) {
return '#extension GL_OES_standard_derivatives : enable\nprecision highp float;\n' + data.prefix + src + data.suffix
}
function checkBanned (frag) {
if (!data.bannedTokens) return
var tokens = tokenize(frag)
for (var i = 0; i < tokens.length; i++) {
if (
tokens[i].type === 'keyword' &&
tokens[i + 1] &&
tokens[i + 1].type === 'whitespace'
) continue
if (data.bannedTokens.indexOf(tokens[i].data) !== -1) {
failedMatch = true
passedMatch = false
matchOffset = 0
return true
}
}
}
resize()
window.addEventListener('resize', resize)
window.requestAnimationFrame(loop)
}
// #extension GL_OES_standard_derivatives : enable
//
// vec3 red = vec3(1, 0, 0);
// vec3 green = vec3(0, 1, 0);
// vec3 blue = vec3(0, 0, 1);
// vec3 cyan = vec3(0, 1, 1);
// vec3 magenta = vec3(1, 0, 1);
// vec3 yellow = vec3(1, 1, 0);
// vec3 white = vec3(1, 1, 1);
//
// float aastep (float threshold, float value) {
// float afwidth = length(vec2(dFdx(value), dFdy(value))) * 0.70710678118654757;
// return smoothstep(threshold-afwidth, threshold+afwidth, value);
// }
//
// void main() {
// vec3 color = vec3(0);
//
// color += aastep(0.0, length(p) - 1.0) * white;
//
// gl_FragColor = vec4(color, 1);
// }