1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | 'use strict';
|
18 |
|
19 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
20 |
|
21 | (function () {
|
22 | 'use strict';
|
23 |
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | var loadShader = function loadShader(gl, type, source) {
|
38 |
|
39 | var shader = gl.createShader(type);
|
40 | if (shader == null) throw new Error('Unable to create shader!');
|
41 |
|
42 | gl.shaderSource(shader, source);
|
43 |
|
44 | gl.compileShader(shader);
|
45 |
|
46 | if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) throw new Error('Failed to compile shader:' + gl.getShaderInfoLog(shader));
|
47 | return shader;
|
48 | };
|
49 |
|
50 |
|
51 | var useShader = function useShader(gl, vshaderSource, fshaderSource) {
|
52 |
|
53 | var vertexShader = loadShader(gl, gl.VERTEX_SHADER, vshaderSource),
|
54 | fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fshaderSource);
|
55 |
|
56 | var glProgram = gl.createProgram();
|
57 |
|
58 | gl.attachShader(glProgram, vertexShader);
|
59 | gl.attachShader(glProgram, fragmentShader);
|
60 |
|
61 | gl.linkProgram(glProgram);
|
62 |
|
63 | if (!gl.getProgramParameter(glProgram, gl.LINK_STATUS)) throw new Error('Failed to link program: ' + gl.getProgramInfoLog(glProgram));
|
64 |
|
65 | gl.useProgram(glProgram);
|
66 | return glProgram;
|
67 | };
|
68 |
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | var newBuffer = function newBuffer(gl, isElement) {
|
81 | var buffer = gl.createBuffer(),
|
82 | TYPE = isElement ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
|
83 |
|
84 | gl.bindBuffer(TYPE, buffer);
|
85 | return buffer;
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | var writeBuffer = function writeBuffer(gl, data, usage, isElement) {
|
92 | var TYPE = isElement ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
|
93 | gl.bufferData(TYPE, data, usage);
|
94 | };
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | var useBuffer = function useBuffer(gl, location, size, type, stride, offset, normalized) {
|
110 |
|
111 | gl.vertexAttribPointer(location, size, type, normalized || false, stride || 0, offset || 0);
|
112 |
|
113 | gl.enableVertexAttribArray(location);
|
114 | };
|
115 |
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | var initTexture = function initTexture(gl, type, unit, _type_) {
|
126 |
|
127 | var texture = gl.createTexture();
|
128 |
|
129 | if (_type_ == '2d') {
|
130 | unit = unit || 0;
|
131 |
|
132 | gl.activeTexture(gl['TEXTURE' + unit]);
|
133 | }
|
134 |
|
135 |
|
136 | gl.bindTexture(type, texture);
|
137 | return texture;
|
138 | };
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | var linkImage = function linkImage(gl, type, level, format, textureType, image) {
|
154 | format = {
|
155 | "rgb": gl.RGB,
|
156 | "rgba": gl.RGBA,
|
157 | "alpha": gl.ALPHA
|
158 | }[format] || gl.RGBA;
|
159 |
|
160 | gl.texImage2D(type, level || 0, format, format, {
|
161 |
|
162 |
|
163 |
|
164 | }[textureType] || gl.UNSIGNED_BYTE, image);
|
165 | };
|
166 |
|
167 | var linkCube = function linkCube(gl, type, level, format, textureType, images, width, height, texture) {
|
168 | format = {
|
169 | "rgb": gl.RGB,
|
170 | "rgba": gl.RGBA,
|
171 | "alpha": gl.ALPHA
|
172 | }[format] || gl.RGBA;
|
173 |
|
174 | level = level || 0;
|
175 |
|
176 | textureType = {
|
177 |
|
178 |
|
179 |
|
180 | }[textureType] || gl.UNSIGNED_BYTE;
|
181 |
|
182 | var types = [gl.TEXTURE_CUBE_MAP_POSITIVE_X,
|
183 | gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
|
184 | gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
|
185 | gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
|
186 | gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
|
187 | gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
|
188 | ],
|
189 | i = void 0,
|
190 | target = void 0;
|
191 |
|
192 | for (i = 0; i < types.length; i++) {
|
193 | target = types[i];
|
194 | gl.texImage2D(target, level, format, width, height, 0, format, textureType, null);
|
195 | gl.bindTexture(type, texture);
|
196 | gl.texImage2D(target, level, format, format, textureType, images[i]);
|
197 | }
|
198 |
|
199 | gl.generateMipmap(type);
|
200 | };
|
201 |
|
202 | function value(gl) {
|
203 | return {
|
204 |
|
205 | |
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | setAttribute1f: function setAttribute1f(name, v0) {
|
212 |
|
213 | var location = gl.getAttribLocation(gl.program, name);
|
214 |
|
215 | gl.vertexAttrib1f(location, v0);
|
216 | },
|
217 | setAttribute2f: function setAttribute2f(name, v0, v1) {
|
218 | var location = gl.getAttribLocation(gl.program, name);
|
219 | gl.vertexAttrib2f(location, v0, v1);
|
220 | },
|
221 | setAttribute3f: function setAttribute3f(name, v0, v1, v2) {
|
222 | var location = gl.getAttribLocation(gl.program, name);
|
223 | gl.vertexAttrib3f(location, v0, v1, v2);
|
224 | },
|
225 | setAttribute4f: function setAttribute4f(name, v0, v1, v2, v3) {
|
226 | var location = gl.getAttribLocation(gl.program, name);
|
227 | gl.vertexAttrib4f(location, v0, v1, v2, v3);
|
228 | },
|
229 |
|
230 |
|
231 |
|
232 | setAttribute1i: function setAttribute1i(name, v0) {
|
233 |
|
234 | var location = gl.getAttribLocation(gl.program, name);
|
235 |
|
236 | gl.vertexAttrib1i(location, v0);
|
237 | },
|
238 | setAttribute2i: function setAttribute2i(name, v0, v1) {
|
239 | var location = gl.getAttribLocation(gl.program, name);
|
240 | gl.vertexAttrib2i(location, v0, v1);
|
241 | },
|
242 | setAttribute3i: function setAttribute3i(name, v0, v1, v2) {
|
243 | var location = gl.getAttribLocation(gl.program, name);
|
244 | gl.vertexAttrib3i(location, v0, v1, v2);
|
245 | },
|
246 | setAttribute4i: function setAttribute4i(name, v0, v1, v2, v3) {
|
247 | var location = gl.getAttribLocation(gl.program, name);
|
248 | gl.vertexAttrib4i(location, v0, v1, v2, v3);
|
249 | },
|
250 |
|
251 |
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | setUniform1f: function setUniform1f(name, v0) {
|
259 | var location = gl.getUniformLocation(gl.program, name);
|
260 | gl.uniform1f(location, v0);
|
261 | },
|
262 | setUniform2f: function setUniform2f(name, v0, v1) {
|
263 | var location = gl.getUniformLocation(gl.program, name);
|
264 | gl.uniform2f(location, v0, v1);
|
265 | },
|
266 | setUniform3f: function setUniform3f(name, v0, v1, v2) {
|
267 | var location = gl.getUniformLocation(gl.program, name);
|
268 | gl.uniform3f(location, v0, v1, v2);
|
269 | },
|
270 | setUniform4f: function setUniform4f(name, v0, v1, v2, v3) {
|
271 | var location = gl.getUniformLocation(gl.program, name);
|
272 | gl.uniform4f(location, v0, v1, v2, v3);
|
273 | },
|
274 |
|
275 |
|
276 |
|
277 | setUniform1i: function setUniform1i(name, v0) {
|
278 | var location = gl.getUniformLocation(gl.program, name);
|
279 | gl.uniform1i(location, v0);
|
280 | },
|
281 | setUniform2i: function setUniform2i(name, v0, v1) {
|
282 | var location = gl.getUniformLocation(gl.program, name);
|
283 | gl.uniform2i(location, v0, v1);
|
284 | },
|
285 | setUniform3i: function setUniform3i(name, v0, v1, v2) {
|
286 | var location = gl.getUniformLocation(gl.program, name);
|
287 | gl.uniform3i(location, v0, v1, v2);
|
288 | },
|
289 | setUniform4i: function setUniform4i(name, v0, v1, v2, v3) {
|
290 | var location = gl.getUniformLocation(gl.program, name);
|
291 | gl.uniform4i(location, v0, v1, v2, v3);
|
292 | },
|
293 |
|
294 |
|
295 |
|
296 | setUniformMatrix2fv: function setUniformMatrix2fv(name, value) {
|
297 | var location = gl.getUniformLocation(gl.program, name);
|
298 | gl.uniformMatrix2fv(location, false, value);
|
299 | },
|
300 | setUniformMatrix3fv: function setUniformMatrix3fv(name, value) {
|
301 | var location = gl.getUniformLocation(gl.program, name);
|
302 | gl.uniformMatrix3fv(location, false, value);
|
303 | },
|
304 | setUniformMatrix4fv: function setUniformMatrix4fv(name, value) {
|
305 | var location = gl.getUniformLocation(gl.program, name);
|
306 | gl.uniformMatrix4fv(location, false, value);
|
307 | }
|
308 | };
|
309 | }
|
310 |
|
311 | function _painter(gl) {
|
312 |
|
313 | var typeMap = {
|
314 | "byte": gl.UNSIGNED_BYTE,
|
315 | "short": gl.UNSIGNED_SHORT
|
316 | };
|
317 |
|
318 | return {
|
319 |
|
320 |
|
321 | openDeep: function openDeep() {
|
322 | gl.enable(gl.DEPTH_TEST);
|
323 | return this;
|
324 | },
|
325 |
|
326 |
|
327 |
|
328 | points: function points(first, count, type) {
|
329 | if (type) {
|
330 | gl.drawElements(gl.POINTS, count, typeMap[type], first);
|
331 | } else {
|
332 | gl.drawArrays(gl.POINTS, first, count);
|
333 | }
|
334 | return this;
|
335 | },
|
336 |
|
337 |
|
338 |
|
339 | lines: function lines(first, count, type) {
|
340 | if (type) {
|
341 | gl.drawElements(gl.LINES, count, typeMap[type], first);
|
342 | } else {
|
343 | gl.drawArrays(gl.LINES, first, count);
|
344 | }
|
345 | return this;
|
346 | },
|
347 |
|
348 |
|
349 |
|
350 | stripLines: function stripLines(first, count, type) {
|
351 | if (type) {
|
352 | gl.drawElements(gl.LINE_STRIP, count, typeMap[type], first);
|
353 | } else {
|
354 | gl.drawArrays(gl.LINE_STRIP, first, count);
|
355 | }
|
356 | return this;
|
357 | },
|
358 |
|
359 |
|
360 |
|
361 | loopLines: function loopLines(first, count, type) {
|
362 | if (type) {
|
363 | gl.drawElements(gl.LINE_LOOP, count, typeMap[type], first);
|
364 | } else {
|
365 | gl.drawArrays(gl.LINE_LOOP, first, count);
|
366 | }
|
367 | return this;
|
368 | },
|
369 |
|
370 |
|
371 |
|
372 | triangles: function triangles(first, count, type) {
|
373 | if (type) {
|
374 | gl.drawElements(gl.TRIANGLES, count, typeMap[type], first);
|
375 | } else {
|
376 | gl.drawArrays(gl.TRIANGLES, first, count);
|
377 | }
|
378 | return this;
|
379 | },
|
380 |
|
381 |
|
382 |
|
383 | stripTriangles: function stripTriangles(first, count, type) {
|
384 | if (type) {
|
385 | gl.drawElements(gl.TRIANGLE_STRIP, count, typeMap[type], first);
|
386 | } else {
|
387 | gl.drawArrays(gl.TRIANGLE_STRIP, first, count);
|
388 | }
|
389 | return this;
|
390 | },
|
391 |
|
392 |
|
393 |
|
394 | fanTriangles: function fanTriangles(first, count, type) {
|
395 | if (type) {
|
396 | gl.drawElements(gl.TRIANGLE_FAN, count, typeMap[type], first);
|
397 | } else {
|
398 | gl.drawArrays(gl.TRIANGLE_FAN, first, count);
|
399 | }
|
400 | return this;
|
401 | }
|
402 | };
|
403 | }
|
404 |
|
405 |
|
406 | var getCanvasWebgl = function getCanvasWebgl(node, opts) {
|
407 | var names = ["webgl", "experimental-webgl", "webkit-3d", "moz-webgl"],
|
408 | context = null,
|
409 | i = void 0;
|
410 | for (i = 0; i < names.length; i++) {
|
411 | try {
|
412 | context = node.getContext(names[i], opts);
|
413 | } catch (e) {}
|
414 | if (context) break;
|
415 | }
|
416 | if (!context) throw new Error('Non canvas or browser does not support webgl.');
|
417 | return context;
|
418 | };
|
419 |
|
420 |
|
421 | function image3DCore(node, opts) {
|
422 | var gl = getCanvasWebgl(node, opts),
|
423 | glObj = {
|
424 |
|
425 | "_gl_": gl,
|
426 |
|
427 |
|
428 | "painter": function painter() {
|
429 | return _painter(gl);
|
430 | },
|
431 |
|
432 |
|
433 | "shader": function shader(vshaderSource, fshaderSource) {
|
434 | gl.program = useShader(gl, vshaderSource, fshaderSource);
|
435 | return glObj;
|
436 | },
|
437 |
|
438 |
|
439 | "buffer": function buffer(isElement) {
|
440 |
|
441 | newBuffer(gl, isElement);
|
442 | var bufferData = void 0,
|
443 | bufferObj = {
|
444 |
|
445 | "write": function write(data, usage) {
|
446 | usage = usage || gl.STATIC_DRAW;
|
447 | writeBuffer(gl, data, usage, isElement);
|
448 | bufferData = data;
|
449 | return bufferObj;
|
450 | },
|
451 |
|
452 | "use": function use(location, size, stride, offset, type, normalized) {
|
453 | var fsize = bufferData.BYTES_PER_ELEMENT;
|
454 | if (typeof location == 'string') location = gl.getAttribLocation(gl.program, location);
|
455 | stride = stride || 0;
|
456 | offset = offset || 0;
|
457 | type = type || gl.FLOAT;
|
458 | useBuffer(gl, location, size, type, stride * fsize, offset * fsize, normalized);
|
459 | return bufferObj;
|
460 | }
|
461 | };
|
462 | return bufferObj;
|
463 | },
|
464 |
|
465 |
|
466 | "texture": function texture(_type_, unit) {
|
467 | var type = {
|
468 | "2d": gl.TEXTURE_2D,
|
469 | "cube": gl.TEXTURE_CUBE_MAP
|
470 | }[_type_];
|
471 |
|
472 |
|
473 | var texture = initTexture(gl, type, unit, _type_);
|
474 |
|
475 |
|
476 | gl.texParameteri(type, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
477 | gl.texParameteri(type, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
478 | gl.texParameteri(type, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
479 |
|
480 | var textureObj = {
|
481 |
|
482 | "useImage": function useImage(image, level, format, textureType) {
|
483 | linkImage(gl, type, level, format, textureType, image);
|
484 | return textureObj;
|
485 | },
|
486 |
|
487 | "useCube": function useCube(images, width, height, level, format, textureType) {
|
488 | linkCube(gl, type, level, format, textureType, images, width, height, texture);
|
489 | }
|
490 | };
|
491 | return textureObj;
|
492 | }
|
493 |
|
494 | };
|
495 |
|
496 |
|
497 | var valueMethods = value(gl);
|
498 | for (var key in valueMethods) {
|
499 | glObj[key] = valueMethods[key];
|
500 | }
|
501 |
|
502 | |
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 | gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
|
509 |
|
510 | return glObj;
|
511 | }
|
512 |
|
513 | if ((typeof module === 'undefined' ? 'undefined' : _typeof(module)) === "object" && _typeof(module.exports) === "object") {
|
514 | module.exports = image3DCore;
|
515 | } else {
|
516 | var
|
517 |
|
518 | _image3DCore = window.image3DCore;
|
519 |
|
520 | image3D.noConflict = function () {
|
521 |
|
522 |
|
523 |
|
524 | if (window.image3DCore === image3DCore) {
|
525 | window.image3DCore = _image3DCore;
|
526 | }
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 | return image3DCore;
|
533 | };
|
534 |
|
535 |
|
536 | window.image3DCore = image3DCore;
|
537 | }
|
538 | })(); |
\ | No newline at end of file |