Skip to content
Navigation Menu
{{ message }}
forked from anvaka/VivaGraphJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebglImageNodeProgram.js
More file actions
420 lines (355 loc) · 14.8 KB
/
Copy pathwebglImageNodeProgram.js
File metadata and controls
420 lines (355 loc) · 14.8 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**
* @fileOverview Defines an image nodes for webglGraphics class.
* Shape of nodes is sqare.
*
* @author Andrei Kashcha (aka anvaka) / http://anvaka.blogspot.com
*/
/*global Viva, Float32Array, window*/
/*jslint sloppy: true, vars: true, plusplus: true, bitwise: true, nomen: true */
/**
* Single texture in the webglAtlas.
*/
Viva.Graph.View.Texture = function (size) {
this.canvas = window.document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
this.isDirty = false;
this.canvas.width = this.canvas.height = size;
};
/**
* My naive implementation of textures atlas. It allows clients to load
* multimple images into atlas and get canvas representing all of them.
*
* @param tilesPerTexture - indicates how many images can be loaded to one
* texture of the atlas. If number of loaded images exceeds this
* parameter a new canvas will be created.
*/
Viva.Graph.View.webglAtlas = function (tilesPerTexture) {
var tilesPerRow = Math.sqrt(tilesPerTexture || 1024) << 0,
tileSize = tilesPerRow,
lastLoadedIdx = 1,
loadedImages = {},
dirtyTimeoutId,
skipedDirty = 0,
textures = [],
trackedUrls = [],
that,
findNearestPowerOf2 = function (n) {
// TODO: probably don't need this anymore
// http://en.wikipedia.org/wiki/Power_of_two#Algorithm_to_round_up_to_power_of_two
n = n << 0; // make it integer
n = n - 1;
n = n | (n >> 1);
n = n | (n >> 2);
n = n | (n >> 4);
n = n | (n >> 8);
n = n | (n >> 16);
return n + 1;
},
isPowerOf2 = function (n) {
return (n & (n - 1)) === 0;
},
createTexture = function () {
var texture = new Viva.Graph.View.Texture(tilesPerRow * tileSize);
textures.push(texture);
},
getTileCoordinates = function (absolutePosition) {
var textureNumber = (absolutePosition / tilesPerTexture) << 0,
localTileNumber = (absolutePosition % tilesPerTexture),
row = (localTileNumber / tilesPerRow) << 0,
col = (localTileNumber % tilesPerRow);
return {textureNumber : textureNumber, row : row, col: col};
},
markDirtyNow = function () {
that.isDirty = true;
skipedDirty = 0;
dirtyTimeoutId = null;
},
markDirty = function () {
// delay this call, since it results in texture reload
if (dirtyTimeoutId) {
window.clearTimeout(dirtyTimeoutId);
skipedDirty += 1;
dirtyTimeoutId = null;
}
if (skipedDirty > 10) {
markDirtyNow();
} else {
dirtyTimeoutId = window.setTimeout(markDirtyNow, 400);
}
},
copy = function (from, to) {
var fromCanvas = textures[from.textureNumber].canvas,
toCtx = textures[to.textureNumber].ctx,
x = to.col * tileSize,
y = to.row * tileSize;
toCtx.drawImage(fromCanvas, from.col * tileSize, from.row * tileSize, tileSize, tileSize, x, y, tileSize, tileSize);
textures[from.textureNumber].isDirty = true;
textures[to.textureNumber].isDirty = true;
},
drawAt = function (tileNumber, img, callback) {
var tilePosition = getTileCoordinates(tileNumber),
coordinates = { offset : tileNumber };
if (tilePosition.textureNumber >= textures.length) {
createTexture();
}
var currentTexture = textures[tilePosition.textureNumber];
currentTexture.ctx.drawImage(img, tilePosition.col * tileSize, tilePosition.row * tileSize, tileSize, tileSize);
trackedUrls[tileNumber] = img.src;
loadedImages[img.src] = coordinates;
currentTexture.isDirty = true;
callback(coordinates);
};
if (!isPowerOf2(tilesPerTexture)) {
throw 'Tiles per texture should be power of two.';
}
// this is the return object
that = {
/**
* indicates whether atlas has changed texture in it. If true then
* some of the textures has isDirty flag set as well.
*/
isDirty : false,
/**
* Clears any signs of atlas changes.
*/
clearDirty : function () {
var i;
this.isDirty = false;
for (i = 0; i < textures.length; ++i) {
textures[i].isDirty = false;
}
},
/**
* Removes given url from colleciton of tiles in the atlas.
*/
remove : function (imgUrl) {
var coordinates = loadedImages[imgUrl];
if (!coordinates) { return false; }
delete loadedImages[imgUrl];
lastLoadedIdx -= 1;
if (lastLoadedIdx === coordinates.offset) {
return true; // Ignore if it's last image in the whole set.
}
var tileToRemove = getTileCoordinates(coordinates.offset),
lastTileInSet = getTileCoordinates(lastLoadedIdx);
copy(lastTileInSet, tileToRemove);
var replacedOffset = loadedImages[trackedUrls[lastLoadedIdx]];
replacedOffset.offset = coordinates.offset;
trackedUrls[coordinates.offset] = trackedUrls[lastLoadedIdx];
markDirty();
return true;
},
/**
* Gets all textures in the atlas.
*/
getTextures : function () {
return textures; // I trust you...
},
/**
* Gets coordinates of the given image in the atlas. Coordinates is an object:
* {offset : int } - where offset is an absolute position of the image in the
* atlas.
*
* Absolute means it can be larger than tilesPerTexture parameter, and in that
* case clients should get next texture in getTextures() collection.
*/
getCoordinates : function (imgUrl) {
return loadedImages[imgUrl];
},
/**
* Asynchronously Loads the image to the atlas. Cross-domain security
* limitation applies.
*/
load : function (imgUrl, callback) {
if (loadedImages.hasOwnProperty(imgUrl)) {
callback(loadedImages[imgUrl]);
} else {
var img = new window.Image(),
imgId = lastLoadedIdx,
that = this;
lastLoadedIdx += 1;
img.crossOrigin = "anonymous";
img.onload = function () {
markDirty();
drawAt(imgId, img, callback);
};
img.src = imgUrl;
}
}
};
return that;
};
/**
* Defines simple UI for nodes in webgl renderer. Each node is rendered as an image.
*/
Viva.Graph.View.webglImageNodeProgram = function () {
var ATTRIBUTES_PER_PRIMITIVE = 18,
nodesFS = [
'precision mediump float;',
'varying vec4 color;',
'varying vec3 vTextureCoord;',
'uniform sampler2D u_sampler0;',
'uniform sampler2D u_sampler1;',
'uniform sampler2D u_sampler2;',
'uniform sampler2D u_sampler3;',
'void main(void) {',
' if (vTextureCoord.z == 0.) {',
' gl_FragColor = texture2D(u_sampler0, vTextureCoord.xy);',
' } else if (vTextureCoord.z == 1.) {',
' gl_FragColor = texture2D(u_sampler1, vTextureCoord.xy);',
' } else if (vTextureCoord.z == 2.) {',
' gl_FragColor = texture2D(u_sampler2, vTextureCoord.xy);',
' } else if (vTextureCoord.z == 3.) {',
' gl_FragColor = texture2D(u_sampler3, vTextureCoord.xy);',
' } else { gl_FragColor = vec4(0, 1, 0, 1); }',
'}'].join('\n'),
nodesVS = [
'attribute vec2 a_vertexPos;',
'attribute float a_customAttributes;',
'uniform vec2 u_screenSize;',
'uniform mat4 u_transform;',
'uniform float u_tilesPerTexture;',
'varying vec3 vTextureCoord;',
'void main(void) {',
' gl_Position = u_transform * vec4(a_vertexPos/u_screenSize, 0, 1);',
'float corner = mod(a_customAttributes, 4.);',
'float tileIndex = mod(floor(a_customAttributes / 4.), u_tilesPerTexture);',
'float tilesPerRow = sqrt(u_tilesPerTexture);',
'float tileSize = 1./tilesPerRow;',
'float tileColumn = mod(tileIndex, tilesPerRow);',
'float tileRow = floor(tileIndex/tilesPerRow);',
'if(corner == 0.0) {',
' vTextureCoord.xy = vec2(0, 1);',
'} else if(corner == 1.0) {',
' vTextureCoord.xy = vec2(1, 1);',
'} else if(corner == 2.0) {',
' vTextureCoord.xy = vec2(0, 0);',
'} else {',
' vTextureCoord.xy = vec2(1, 0);',
'}',
'vTextureCoord *= tileSize;',
'vTextureCoord.x += tileColumn * tileSize;',
'vTextureCoord.y += tileRow * tileSize;',
'vTextureCoord.z = floor(floor(a_customAttributes / 4.)/u_tilesPerTexture);',
'}'].join('\n'),
tilesPerTexture = 1024, // TODO: Get based on max texture size
atlas,
customPrimitiveType;
var program,
gl,
buffer,
utils,
locations,
nodesCount = 0,
texture,
nodes = new Float32Array(64),
width,
height,
transform,
sizeDirty,
refreshTexture = function (texture, idx) {
if (texture.nativeObject) {
gl.deleteTexture(texture.nativeObject);
}
var nativeObject = gl.createTexture();
gl.activeTexture(gl['TEXTURE' + idx]);
gl.bindTexture(gl.TEXTURE_2D, nativeObject);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texture.canvas);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
gl.generateMipmap(gl.TEXTURE_2D);
gl.uniform1i(locations['sampler' + idx], idx);
texture.nativeObject = nativeObject;
},
ensureAtlasTextureUpdated = function () {
if (atlas.isDirty) {
var textures = atlas.getTextures(),
i;
for (i = 0; i < textures.length; ++i) {
if (textures[i].isDirty || !textures[i].nativeObject) {
refreshTexture(textures[i], i);
}
}
atlas.clearDirty();
}
};
return {
load : function (glContext) {
gl = glContext;
utils = Viva.Graph.webgl(glContext);
atlas = new Viva.Graph.View.webglAtlas(tilesPerTexture);
program = utils.createProgram(nodesVS, nodesFS);
gl.useProgram(program);
locations = utils.getLocations(program, ['a_vertexPos', 'a_customAttributes', 'u_screenSize', 'u_transform', 'u_sampler0', 'u_sampler1', 'u_sampler2', 'u_sampler3', 'u_tilesPerTexture']);
gl.uniform1f(locations.tilesPerTexture, tilesPerTexture);
gl.enableVertexAttribArray(locations.vertexPos);
gl.enableVertexAttribArray(locations.customAttributes);
buffer = gl.createBuffer();
},
/**
* Updates position of current node in the buffer of nodes.
*
* @param idx - index of current node.
* @param pos - new position of the node.
*/
position : function (nodeUI, pos) {
var idx = nodeUI.id * ATTRIBUTES_PER_PRIMITIVE;
/*jslint white:true*/
nodes[idx] = pos.x - nodeUI.size; nodes[idx + 1] = pos.y - nodeUI.size; nodes[idx + 2] = nodeUI._offset * 4;
nodes[idx + 3] = pos.x + nodeUI.size; nodes[idx + 4] = pos.y - nodeUI.size; nodes[idx + 5] = nodeUI._offset * 4 + 1;
nodes[idx + 6] = pos.x - nodeUI.size; nodes[idx + 7] = pos.y + nodeUI.size; nodes[idx + 8] = nodeUI._offset * 4 + 2;
nodes[idx + 9] = pos.x - nodeUI.size; nodes[idx + 10] = pos.y + nodeUI.size; nodes[idx + 11] = nodeUI._offset * 4 + 2;
nodes[idx + 12] = pos.x + nodeUI.size; nodes[idx + 13] = pos.y - nodeUI.size; nodes[idx + 14] = nodeUI._offset * 4 + 1;
nodes[idx + 15] = pos.x + nodeUI.size; nodes[idx + 16] = pos.y + nodeUI.size; nodes[idx + 17] = nodeUI._offset * 4 + 3;
},
createNode : function (ui) {
nodes = utils.extendArray(nodes, nodesCount, ATTRIBUTES_PER_PRIMITIVE);
nodesCount += 1;
var coordinates = atlas.getCoordinates(ui.src);
if (coordinates) {
ui._offset = coordinates.offset;
} else {
ui._offset = 0;
// Image is not yet loaded into the atlas. Reload it:
atlas.load(ui.src, function (coordinates) {
ui._offset = coordinates.offset;
});
}
},
removeNode : function (nodeUI) {
if (nodesCount > 0) { nodesCount -= 1; }
if (nodeUI.id < nodesCount && nodesCount > 0) {
if (nodeUI.src) {
atlas.remove(nodeUI.src);
}
utils.copyArrayPart(nodes, nodeUI.id * ATTRIBUTES_PER_PRIMITIVE, nodesCount * ATTRIBUTES_PER_PRIMITIVE, ATTRIBUTES_PER_PRIMITIVE);
}
},
replaceProperties : function (replacedNode, newNode) {
newNode._offset = replacedNode._offset;
},
updateTransform : function (newTransform) {
sizeDirty = true;
transform = newTransform;
},
updateSize : function (w, h) {
width = w;
height = h;
sizeDirty = true;
},
render : function () {
gl.useProgram(program);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, nodes, gl.DYNAMIC_DRAW);
if (sizeDirty) {
sizeDirty = false;
gl.uniformMatrix4fv(locations.transform, false, transform);
gl.uniform2f(locations.screenSize, width, height);
}
gl.vertexAttribPointer(locations.vertexPos, 2, gl.FLOAT, false, 3 * Float32Array.BYTES_PER_ELEMENT, 0);
gl.vertexAttribPointer(locations.customAttributes, 1, gl.FLOAT, false, 3 * Float32Array.BYTES_PER_ELEMENT, 2 * 4);
ensureAtlasTextureUpdated();
gl.drawArrays(gl.TRIANGLES, 0, nodesCount * 6);
}
};
};
You can’t perform that action at this time.
