-
Notifications
You must be signed in to change notification settings - Fork 7
/
Rendering.js
332 lines (289 loc) · 8.05 KB
/
Rendering.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
const textureCache = {}
function TextureCache_get(name)
{
var tex = textureCache[name]
if (tex)
return tex
const info = nodeResources.sampler2D[name]
const data = info.genFunc()
const gl = window.GLCtx
tex = gl.createTexture()
gl.bindTexture(gl.TEXTURE_2D, tex)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
if (typeof data === "string")
{
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([100, 100, 100, 255]))
const image = new Image()
image.onload = function()
{
gl.bindTexture(gl.TEXTURE_2D, tex)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image)
gl.generateMipmap(gl.TEXTURE_2D)
}
image.src = data
}
else
{
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, data.width, data.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(data.data.buffer))
gl.generateMipmap(gl.TEXTURE_2D)
}
textureCache[name] = tex
return tex
}
function TextureCache_getCubemapInternal(url)
{
var tex = textureCache[url]
if (tex)
return tex
tex = loadCubemap(url)
textureCache[url] = tex
return tex
}
function loadShader(type, source)
{
const gl = window.GLCtx
const shader = gl.createShader(type)
gl.shaderSource(shader, source)
gl.compileShader(shader)
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
{
console.log("[GL] shader compilation failed: " + gl.getShaderInfoLog(shader))
gl.deleteShader(shader)
return null
}
return shader
}
function initShaderProgram(vsSource, fsSource)
{
const gl = window.GLCtx
const vertexShader = loadShader(gl.VERTEX_SHADER, vsSource)
const fragmentShader = loadShader(gl.FRAGMENT_SHADER, fsSource)
const shaderProgram = gl.createProgram()
gl.attachShader(shaderProgram, vertexShader)
gl.attachShader(shaderProgram, fragmentShader)
gl.linkProgram(shaderProgram)
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS))
{
console.log("[GL] shader program linkage failed: " + gl.getProgramInfoLog(shaderProgram))
return null
}
return shaderProgram
}
const shaderCache = {}
function ShaderCache_getProgram(vsSource, fsSource)
{
const key = vsSource + fsSource
const pinfo = shaderCache[key]
if (pinfo)
{
pinfo.time = Date.now()
return pinfo.program
}
const p = initShaderProgram(vsSource, fsSource)
shaderCache[key] = { time: Date.now(), program: p }
return p
}
function ShaderCache_GC(expirationTimeSec)
{
const toClean = {}
const now = Date.now()
for (var key in shaderCache)
{
var pinfo = shaderCache[key]
if (now - pinfo.time > expirationTimeSec * 1000)
toClean[key] = true
}
var num = 0
for (var key in toClean)
{
num++
delete shaderCache[key]
}
if (num)
console.log(`[ShaderCache GC] dropped ${num} items`)
}
function initGL()
{
window.PreviewStartTime = Date.now()
const gl = window.GLCtx
gl.getExtension("OES_standard_derivatives")
gl.getExtension("EXT_shader_texture_lod")
gl.enable(gl.CULL_FACE);
gl.cullFace(gl.BACK);
gl.blendFuncSeparate(gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE_MINUS_DST_ALPHA, gl.ONE)
window.GLQuadMesh = createQuadMesh()
window.GLSphereMesh = createSphereMesh()
}
window.GLCameraDist = 3
window.GLCameraYaw = 160
window.GLCameraPitch = 75
window.GLCameraPos = [-2.5, 1.5, 0]
window.GLCameraTarget = [0, 0, 0]
function shaderProgramLinkInputs(p, info, asp)
{
const gl = window.GLCtx
gl.useProgram(p)
if (info.uniform.time)
{
const time = (Date.now() - window.PreviewStartTime) / 1000
gl.uniform1f(gl.getUniformLocation(p, "time"), time)
}
if (info.uniform.uCameraPos)
{
gl.uniform3fv(gl.getUniformLocation(p, "uCameraPos"), GLCameraPos)
}
if (info.uniform.uViewProjMatrix)
{
const vm = lookAtMatrix(GLCameraPos, GLCameraTarget, [0, 0, 1])
const pm = perspectiveMatrix(60, asp, 0.001, 1000)
const vpm = multiplyMatrices(pm, vm)
gl.uniformMatrix4fv(gl.getUniformLocation(p, "uViewProjMatrix"), false, vpm)
}
if (info.uniform.uInvViewMatrix)
{
const vm = lookAtMatrix(GLCameraPos, GLCameraTarget, [0, 0, 1])
const ivm = invertMatrix(vm)
gl.uniformMatrix4fv(gl.getUniformLocation(p, "uInvViewMatrix"), false, ivm)
}
if (info.uniform.uProjMatrix)
{
const pm = perspectiveMatrix(60, asp, 0.001, 1000)
gl.uniformMatrix4fv(gl.getUniformLocation(p, "uProjMatrix"), false, pm)
}
var sid = 0
for (var key in info.sampler2D)
{
gl.uniform1i(gl.getUniformLocation(p, key), sid)
var tex = TextureCache_get(key)
gl.activeTexture(gl.TEXTURE0 + sid)
gl.bindTexture(gl.TEXTURE_2D, tex)
sid++
}
for (var key in info.samplerCube)
{
var tex
if (key == "sCubemap")
tex = TextureCache_getCubemapInternal("cubemaps/cubemap")
gl.uniform1i(gl.getUniformLocation(p, key), sid)
gl.activeTexture(gl.TEXTURE0 + sid)
gl.bindTexture(gl.TEXTURE_CUBE_MAP, tex)
sid++
}
}
function drawOnePreview(nodeID, aspect, drawBgr)
{
const gl = window.GLCtx
if (drawBgr)
{
var shBgr =
{
vshader: shader_cubeBgrVS,
fshader: shader_cubeBgrFS,
uniform: { uInvViewMatrix: true, uProjMatrix: true },
samplerCube: { sCubemap: true }
}
const progBgr = ShaderCache_getProgram(shBgr.vshader, shBgr.fshader)
shaderProgramLinkInputs(progBgr, shBgr, aspect)
drawTriangleMesh(GLQuadMesh, progBgr)
}
gl.enable(gl.BLEND)
const genSh = nodesGenerateShader(nodeMap[nodeID])
const prog = ShaderCache_getProgram(genSh.vshader, genSh.fshader)
shaderProgramLinkInputs(prog, genSh, aspect)
drawTriangleMesh(GLSphereMesh, prog)
gl.disable(gl.BLEND)
}
function redrawPreviews()
{
const nodePreviewCanvas = document.getElementsByClassName("nodePreviewCanvas")
var minw = 0, minh = 0
for (var i = 0; i < nodePreviewCanvas.length; ++i)
{
// all preview canvas are assumed to be equal in size
const pnc = nodePreviewCanvas[i]
minw = pnc.offsetWidth
minh = pnc.offsetHeight
break
}
const canvas = document.getElementById("mainPreview")
const parent = canvas.parentElement
var tw = parent.offsetWidth
var th = parent.offsetHeight
if (tw < minw)
tw = minw
if (th < minh)
th = minh
if (canvas.width != tw)
canvas.width = tw
if (canvas.height != th)
canvas.height = th
var gl = window.GLCtx
if (typeof gl === "undefined")
{
gl = canvas.getContext("webgl")
if (!gl)
gl = false
window.GLCtx = gl
if (gl)
initGL()
}
requestAnimationFrame(redrawPreviews)
if (!gl)
{
var ctx = canvas.getContext("2d")
ctx.fillStyle = "#EEE"
ctx.font = "32px sans-serif"
ctx.fillText("WebGL is unavailable", 32, 64)
ctx.fillText("Please restart your browser or try another one", 32, 96+16)
return
}
var yaw = GLCameraYaw * Math.PI / 180
var pitch = GLCameraPitch * Math.PI / 180
var dist = GLCameraDist
window.GLCameraPos = [Math.cos(yaw) * Math.sin(pitch) * dist, Math.sin(yaw) * Math.sin(pitch) * dist, Math.cos(pitch) * dist]
gl.clearColor(0, 0, 0, 0)
if (minw && minh)
{
// calculate batch size
const numX = Math.floor(tw / minw)
const numY = Math.floor(th / minh)
const numBatch = numX * numY
for (var batchOff = 0; batchOff < nodePreviewCanvas.length; batchOff += numBatch)
{
gl.clear(gl.COLOR_BUFFER_BIT)
for (var i = batchOff; i < Math.min(nodePreviewCanvas.length, batchOff + numBatch); ++i)
{
const pnc = nodePreviewCanvas[i]
const w = pnc.offsetWidth
const h = pnc.offsetHeight
const inBatch = i - batchOff
const bx = inBatch % numX
const by = Math.floor(inBatch / numX)
var id
for (var el = pnc; el; el = el.parentElement)
{
id = el.dataset.id
if (id)
break
}
gl.viewport(bx * w, canvas.height - h * (1 + by), w, h)
drawOnePreview(id, w / h, false)
}
for (var i = batchOff; i < Math.min(nodePreviewCanvas.length, batchOff + numBatch); ++i)
{
const pnc = nodePreviewCanvas[i]
const w = pnc.offsetWidth
const h = pnc.offsetHeight
const inBatch = i - batchOff
const bx = inBatch % numX
const by = Math.floor(inBatch / numX)
const tctx = pnc.getContext("2d")
tctx.clearRect(0, 0, w, h)
tctx.drawImage(canvas, bx * w, by * h, w, h, 0, 0, w, h)
}
}
}
gl.clear(gl.COLOR_BUFFER_BIT)
gl.viewport(0, 0, canvas.width, canvas.height)
drawOnePreview(funcGetCurData().outputNode, canvas.width / canvas.height, true)
}