newUI, python3,...
This commit is contained in:
parent
0bb0049f02
commit
e9d3009ffb
551 changed files with 22992 additions and 787437 deletions
7
www/trckr/js/libs/Stats.js
Normal file
7
www/trckr/js/libs/Stats.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
// stats.js r8 - http://github.com/mrdoob/stats.js
|
||||
var Stats=function(){var h,a,n=0,o=0,i=Date.now(),u=i,p=i,l=0,q=1E3,r=0,e,j,f,b=[[16,16,48],[0,255,255]],m=0,s=1E3,t=0,d,k,g,c=[[16,48,16],[0,255,0]];h=document.createElement("div");h.style.cursor="pointer";h.style.width="80px";h.style.opacity="0.9";h.style.zIndex="10001";h.addEventListener("mousedown",function(a){a.preventDefault();n=(n+1)%2;n==0?(e.style.display="block",d.style.display="none"):(e.style.display="none",d.style.display="block")},!1);e=document.createElement("div");e.style.textAlign=
|
||||
"left";e.style.lineHeight="1.2em";e.style.backgroundColor="rgb("+Math.floor(b[0][0]/2)+","+Math.floor(b[0][1]/2)+","+Math.floor(b[0][2]/2)+")";e.style.padding="0 0 3px 3px";h.appendChild(e);j=document.createElement("div");j.style.fontFamily="Helvetica, Arial, sans-serif";j.style.fontSize="9px";j.style.color="rgb("+b[1][0]+","+b[1][1]+","+b[1][2]+")";j.style.fontWeight="bold";j.innerHTML="FPS";e.appendChild(j);f=document.createElement("div");f.style.position="relative";f.style.width="74px";f.style.height=
|
||||
"30px";f.style.backgroundColor="rgb("+b[1][0]+","+b[1][1]+","+b[1][2]+")";for(e.appendChild(f);f.children.length<74;)a=document.createElement("span"),a.style.width="1px",a.style.height="30px",a.style.cssFloat="left",a.style.backgroundColor="rgb("+b[0][0]+","+b[0][1]+","+b[0][2]+")",f.appendChild(a);d=document.createElement("div");d.style.textAlign="left";d.style.lineHeight="1.2em";d.style.backgroundColor="rgb("+Math.floor(c[0][0]/2)+","+Math.floor(c[0][1]/2)+","+Math.floor(c[0][2]/2)+")";d.style.padding=
|
||||
"0 0 3px 3px";d.style.display="none";h.appendChild(d);k=document.createElement("div");k.style.fontFamily="Helvetica, Arial, sans-serif";k.style.fontSize="9px";k.style.color="rgb("+c[1][0]+","+c[1][1]+","+c[1][2]+")";k.style.fontWeight="bold";k.innerHTML="MS";d.appendChild(k);g=document.createElement("div");g.style.position="relative";g.style.width="74px";g.style.height="30px";g.style.backgroundColor="rgb("+c[1][0]+","+c[1][1]+","+c[1][2]+")";for(d.appendChild(g);g.children.length<74;)a=document.createElement("span"),
|
||||
a.style.width="1px",a.style.height=Math.random()*30+"px",a.style.cssFloat="left",a.style.backgroundColor="rgb("+c[0][0]+","+c[0][1]+","+c[0][2]+")",g.appendChild(a);return{domElement:h,update:function(){i=Date.now();m=i-u;s=Math.min(s,m);t=Math.max(t,m);k.textContent=m+" MS ("+s+"-"+t+")";var a=Math.min(30,30-m/200*30);g.appendChild(g.firstChild).style.height=a+"px";u=i;o++;if(i>p+1E3)l=Math.round(o*1E3/(i-p)),q=Math.min(q,l),r=Math.max(r,l),j.textContent=l+" FPS ("+q+"-"+r+")",a=Math.min(30,30-l/
|
||||
100*30),f.appendChild(f.firstChild).style.height=a+"px",p=i,o=0}}};
|
||||
5
www/trckr/js/libs/d3.min.js
vendored
Normal file
5
www/trckr/js/libs/d3.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
94
www/trckr/js/libs/dat.gui.min.js
vendored
Normal file
94
www/trckr/js/libs/dat.gui.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
www/trckr/js/libs/jquery.imgareaselect.pack.js
Normal file
1
www/trckr/js/libs/jquery.imgareaselect.pack.js
Normal file
File diff suppressed because one or more lines are too long
2
www/trckr/js/libs/jquery.min.js
vendored
Normal file
2
www/trckr/js/libs/jquery.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
185
www/trckr/js/libs/poisson_new.js
Normal file
185
www/trckr/js/libs/poisson_new.js
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
/**
|
||||
* Poisson Image Editing module
|
||||
* http://rest-term.com
|
||||
*/
|
||||
(function() {
|
||||
var Poisson; // top-level namaspace
|
||||
var _root = this; // reference to 'window' or 'global'
|
||||
|
||||
if(typeof exports !== 'undefined') {
|
||||
Poisson = exports; // for CommonJS
|
||||
} else {
|
||||
Poisson = _root.Poisson = {};
|
||||
}
|
||||
|
||||
// core operations
|
||||
var EPS = 1.0E-08;
|
||||
var ctx = null,
|
||||
images = [new Image(), new Image(), new Image()],
|
||||
files = [],
|
||||
data = [],
|
||||
loadedCount = 0;
|
||||
|
||||
var sumf = new Float64Array(3),
|
||||
sumfstar = new Float64Array(3),
|
||||
sumvq = new Float64Array(3),
|
||||
fp = new Float64Array(3),
|
||||
fq = new Float64Array(3),
|
||||
gp = new Float64Array(3),
|
||||
gq = new Float64Array(3),
|
||||
subf = new Float64Array(3),
|
||||
naddr = new Int32Array(4),
|
||||
subg = new Float64Array(3);
|
||||
function pt1(y, offsetY, x, offsetX, w, h, sumf, blendData, m, sumvq, srcData, l, naddr) {
|
||||
if(y + offsetY >= 0 && x + offsetX >= 0 &&
|
||||
y + offsetY < h && x + offsetX < w) {
|
||||
for(n=0; n<4; n++) {
|
||||
for(var c=0; c<3; c++) {
|
||||
sumf[c] += blendData[naddr[n] + m + c];
|
||||
sumvq[c] += srcData[l + c] - srcData[naddr[n] + c];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function pt2(y, offsetY, x, offsetX, w, h, fp, gp, dstData, fq, gq, sumfstar, subf, subg, sumf, blendData, m, sumvq, srcData, l, naddr) {
|
||||
if(y + offsetY >= 0 && x + offsetX >= 0 &&
|
||||
y + offsetY < h && x + offsetX < w) {
|
||||
fp[0] = dstData[l + m];
|
||||
fp[1] = dstData[l + m + 1];
|
||||
fp[2] = dstData[l + m + 2];
|
||||
gp[0] = srcData[l];
|
||||
gp[1] = srcData[l + 1];
|
||||
gp[2] = srcData[l + 2];
|
||||
for(n=0; n<4; n++) {
|
||||
for(c=0; c<3; c++) {
|
||||
fq[c] = dstData[naddr[n] + m + c];
|
||||
// modification : we ignore pixels outside face mask, since these cause artifacts
|
||||
//gq[c] = srcData[naddr[n] + c];
|
||||
gq[c] = srcData[l+c];
|
||||
sumfstar[c] += fq[c];
|
||||
subf[c] = fp[c] - fq[c];
|
||||
subf[c] = subf[c] > 0 ? subf[c] : -subf[c];
|
||||
subg[c] = gp[c] - gq[c];
|
||||
subg[c] = subg[c] > 0 ? subg[c] : -subg[c];
|
||||
if(subf[c] > subg[c]) {
|
||||
sumvq[c] += subf[c];
|
||||
} else {
|
||||
sumvq[c] += subg[c];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function pt3(fp, c, sumf, sumfstar, sumvq, terminate, EPS, blendData, l, m, srcData, paste) {
|
||||
fp[c] = (sumf[c] + sumfstar[c] + sumvq[c])*0.25; // division 4
|
||||
error = Math.floor(fp[c] - blendData[l + m + c]);
|
||||
error = error > 0 ? error : -error;
|
||||
if(terminate[c] && error >
|
||||
EPS*(1 + (fp[c] > 0 ? fp[c] : -fp[c]))) {
|
||||
terminate[c] = false;
|
||||
//log[l+m+c] = {x:x,y:y,c:c,error:error};
|
||||
}
|
||||
blendData[l + m + c] = fp[c];
|
||||
if(paste) blendData[l + m + c] = srcData[l + c];
|
||||
}
|
||||
|
||||
var core = {
|
||||
// loads images
|
||||
load: function(srcCanvas, dstCanvas, maskCanvas, onComplete, onError) {
|
||||
ctx = document.createElement('canvas').getContext('2d');
|
||||
// load complete handler
|
||||
|
||||
var imgData = srcCanvas.getContext('2d').getImageData(0, 0, srcCanvas.width, srcCanvas.height);
|
||||
data[0] = imgData;
|
||||
imgData = dstCanvas.getContext('2d').getImageData(0, 0, dstCanvas.width, dstCanvas.height);
|
||||
data[1] = imgData;
|
||||
imgData = maskCanvas.getContext('2d').getImageData(0, 0, maskCanvas.width, maskCanvas.height);
|
||||
data[2] = imgData;
|
||||
ctx.drawImage(dstCanvas, 0, 0);
|
||||
data[3] = dstCanvas.getContext('2d').getImageData(0, 0, dstCanvas.width, dstCanvas.height);
|
||||
if(typeof onComplete === 'function') {
|
||||
onComplete(data);
|
||||
}
|
||||
|
||||
},
|
||||
reset: function() {
|
||||
ctx.drawImage(dstCanvas, 0, 0);
|
||||
data[3] = ctx.getImageData(0, 0, dstCanvas.width, dstCanvas.height);
|
||||
return data[3];
|
||||
},
|
||||
// applies poisson image editing
|
||||
blend: function(iteration, offsetX, offsetY, paste) {
|
||||
var w = data[0].width,
|
||||
h = data[0].height,
|
||||
len = w*h*4,
|
||||
srcData = data[0].data,
|
||||
dstData = data[1].data,
|
||||
maskData = data[2].data,
|
||||
blendData = data[3].data,
|
||||
edge = false,
|
||||
error = 0.0,
|
||||
threashold = 128,
|
||||
terminate = [],
|
||||
step, l, m;
|
||||
var log = {};
|
||||
// validation
|
||||
if(!(parseInt(iteration) && typeof(offsetX) == "number" && typeof(offsetY) == "number")) {
|
||||
throw TypeError('invalid parameter type');
|
||||
}
|
||||
// core operation
|
||||
for(var i=0; i<iteration; i++) {
|
||||
terminate = [true, true, true];
|
||||
for(var y=1; y<h-1; y++) {
|
||||
step = y*w << 2;
|
||||
for(var x=1; x<w-1; x++) {
|
||||
l = step + (x << 2);
|
||||
m = offsetY*w + offsetX << 2;
|
||||
naddr[0] = l - (w << 2);
|
||||
naddr[1] = l - 4;
|
||||
naddr[2] = l + 4;
|
||||
naddr[3] = l + (w << 2);
|
||||
if(maskData[l] > threashold) { // on the mask
|
||||
sumf[0] = 0;
|
||||
sumf[1] = 0;
|
||||
sumf[2] = 0;
|
||||
sumfstar[0] = 0;
|
||||
sumfstar[1] = 0;
|
||||
sumfstar[2] = 0;
|
||||
sumvq[0] = 0;
|
||||
sumvq[1] = 0;
|
||||
sumvq[2] = 0;
|
||||
edge = false;
|
||||
for(var n=0; n<4; n++) {
|
||||
if(maskData[naddr[n]] <= threashold) {
|
||||
edge = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!edge) {
|
||||
pt1(y, offsetY, x, offsetX, w, h, sumf, blendData, m, sumvq, srcData, l, naddr);
|
||||
} else {
|
||||
pt2(y, offsetY, x, offsetX, w, h, fp, gp, dstData, fq, gq, sumfstar, subf, subg, sumf, blendData, m, sumvq, srcData, l, naddr);
|
||||
}
|
||||
for(c=0; c<3; c++) {
|
||||
pt3(fp, c, sumf, sumfstar, sumvq, terminate, EPS, blendData, l, m, srcData, paste)
|
||||
}
|
||||
} // end mask
|
||||
} // end x loop
|
||||
} // end y loop
|
||||
if(terminate[0] && terminate[1] && terminate[2]) break;
|
||||
} // end iteration
|
||||
//core.debugPlot(blendData, log);
|
||||
return data[3];
|
||||
},
|
||||
debugPlot: function(img, data) {
|
||||
for(var i in data) {
|
||||
console.log(data[i]);
|
||||
img[i] = 255;
|
||||
}
|
||||
}
|
||||
};
|
||||
// aliases (public APIs)
|
||||
Poisson.load = core.load;
|
||||
Poisson.reset = core.reset;
|
||||
Poisson.blend = core.blend;
|
||||
}).call(this);
|
||||
44
www/trckr/js/libs/utils.js
Normal file
44
www/trckr/js/libs/utils.js
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
// helper functions
|
||||
|
||||
/**
|
||||
* Provides requestAnimationFrame in a cross browser way.
|
||||
*/
|
||||
window.requestAnimFrame = (function() {
|
||||
return window.requestAnimationFrame ||
|
||||
window.webkitRequestAnimationFrame ||
|
||||
window.mozRequestAnimationFrame ||
|
||||
window.oRequestAnimationFrame ||
|
||||
window.msRequestAnimationFrame ||
|
||||
function(/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
|
||||
return window.setTimeout(callback, 1000/60);
|
||||
};
|
||||
})();
|
||||
|
||||
/**
|
||||
* Provides cancelRequestAnimationFrame in a cross browser way.
|
||||
*/
|
||||
window.cancelRequestAnimFrame = (function() {
|
||||
return window.cancelAnimationFrame ||
|
||||
window.webkitCancelRequestAnimationFrame ||
|
||||
window.mozCancelRequestAnimationFrame ||
|
||||
window.oCancelRequestAnimationFrame ||
|
||||
window.msCancelRequestAnimationFrame ||
|
||||
window.clearTimeout;
|
||||
})();
|
||||
|
||||
// video support utility functions
|
||||
function supports_video() {
|
||||
return !!document.createElement('video').canPlayType;
|
||||
}
|
||||
|
||||
function supports_h264_baseline_video() {
|
||||
if (!supports_video()) { return false; }
|
||||
var v = document.createElement("video");
|
||||
return v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
|
||||
}
|
||||
|
||||
function supports_webm_video() {
|
||||
if (!supports_video()) { return false; }
|
||||
var v = document.createElement("video");
|
||||
return v.canPlayType('video/webm; codecs="vp8"');
|
||||
}
|
||||
333
www/trckr/js/libs/webgl-utils.js
Normal file
333
www/trckr/js/libs/webgl-utils.js
Normal file
|
|
@ -0,0 +1,333 @@
|
|||
// This code is based on webgl-utils.js authored by Gregg Tavares, license below:
|
||||
/*
|
||||
* Copyright (c) 2011, Gregg Tavares
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of greggman.com nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
||||
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
(function(){
|
||||
|
||||
var LOGGING_ENABLED = true;
|
||||
|
||||
/**
|
||||
* Wrapped logging function.
|
||||
* @param {string} msg The message to log.
|
||||
*/
|
||||
const log = function (msg) {
|
||||
if (!LOGGING_ENABLED) { return; }
|
||||
if (window.console && window.console.log) {
|
||||
window.console.log(msg);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Wrapped logging function.
|
||||
* @param {string} msg The message to log.
|
||||
*/
|
||||
const error = function (msg) {
|
||||
if (!LOGGING_ENABLED) { return; }
|
||||
if (window.console) {
|
||||
if (window.console.error) {
|
||||
window.console.error(msg);
|
||||
} else if (window.console.log) {
|
||||
window.console.log(msg);
|
||||
}
|
||||
}
|
||||
throw msg;
|
||||
};
|
||||
|
||||
/**
|
||||
* Turn off all logging.
|
||||
*/
|
||||
const loggingOff = function () {
|
||||
LOGGING_ENABLED = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if the page is embedded.
|
||||
* @return {boolean} True of we are in an iframe
|
||||
*/
|
||||
const isInIFrame = function () {
|
||||
return window !== window.top;
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts a WebGL enum to a string
|
||||
* @param {!WebGLContext} gl The WebGLContext to use.
|
||||
* @param {number} value The enum value.
|
||||
* @return {string} The enum as a string.
|
||||
*/
|
||||
const glEnumToString = function (gl, value) {
|
||||
for (var p in gl) {
|
||||
if (gl[p] === value) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return '0x' + value.toString(16);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Creates the HTLM for a failure message
|
||||
* @param {string} canvasContainerId id of container of th
|
||||
* canvas.
|
||||
* @return {string} The html.
|
||||
*/
|
||||
const makeFailHTML = function (msg) {
|
||||
return '' +
|
||||
'<table style="background-color: #8CE; width: 100%; height: 100%;"><tr>' +
|
||||
'<td align="center">' +
|
||||
'<div style="display: table-cell; vertical-align: middle;">' +
|
||||
'<div style="">' + msg + '</div>' +
|
||||
'</div>' +
|
||||
'</td></tr></table>';
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Mesasge for getting a webgl browser
|
||||
* @type {string}
|
||||
*/
|
||||
// const GET_A_WEBGL_BROWSER = '' +
|
||||
// 'This page requires a browser that supports WebGL.<br/>' +
|
||||
// '<a href="http://get.webgl.org">Click here to upgrade your browser.</a>';
|
||||
|
||||
|
||||
/**
|
||||
* Mesasge for need better hardware
|
||||
* @type {string}
|
||||
*/
|
||||
// const OTHER_PROBLEM = '' +
|
||||
// "It doesn't appear your computer can support WebGL.<br/>" +
|
||||
// '<a href="http://get.webgl.org/troubleshooting/">Click here for more information.</a>';
|
||||
|
||||
|
||||
/**
|
||||
* Creates a webgl context. If creation fails it will
|
||||
* change the contents of the container of the <canvas>
|
||||
* tag to an error message with the correct links for WebGL.
|
||||
* @param {Element} canvas. The canvas element to create a
|
||||
* context from.
|
||||
* @param {WebGLContextCreationAttirbutes} optAttribs Any
|
||||
* creation attributes you want to pass in.
|
||||
* @return {WebGLRenderingContext} The created context.
|
||||
*/
|
||||
const setupWebGL = function (canvas, optAttribs) {
|
||||
// const showLink = function (str) {
|
||||
// var container = canvas.parentNode;
|
||||
// if (container) {
|
||||
// container.innerHTML = makeFailHTML(str);
|
||||
// }
|
||||
// };
|
||||
|
||||
if (!window.WebGLRenderingContext) {
|
||||
// showLink(GET_A_WEBGL_BROWSER);
|
||||
return null;
|
||||
}
|
||||
|
||||
var context = create3DContext(canvas, optAttribs);
|
||||
if (!context) {
|
||||
// showLink(OTHER_PROBLEM);
|
||||
return null;
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Creates a webgl context.
|
||||
* @param {!Canvas} canvas The canvas tag to get context
|
||||
* from. If one is not passed in one will be created.
|
||||
* @return {!WebGLContext} The created context.
|
||||
*/
|
||||
const create3DContext = function (canvas, optAttribs) {
|
||||
var names = ['webgl', 'experimental-webgl'];
|
||||
var context = null;
|
||||
for (var ii = 0; ii < names.length; ++ii) {
|
||||
try {
|
||||
context = canvas.getContext(names[ii], optAttribs);
|
||||
} catch (e) {}
|
||||
if (context) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
||||
const updateCSSIfInIFrame = function () {
|
||||
if (isInIFrame()) {
|
||||
document.body.className = 'iframe';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Gets a WebGL context.
|
||||
* makes its backing store the size it is displayed.
|
||||
*/
|
||||
const getWebGLContext = function (canvas) {
|
||||
if (isInIFrame()) {
|
||||
updateCSSIfInIFrame();
|
||||
|
||||
// make the canvas backing store the size it's displayed.
|
||||
canvas.width = canvas.clientWidth;
|
||||
canvas.height = canvas.clientHeight;
|
||||
}
|
||||
|
||||
var gl = setupWebGL(canvas);
|
||||
return gl;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Loads a shader.
|
||||
* @param {!WebGLContext} gl The WebGLContext to use.
|
||||
* @param {string} shaderSource The shader source.
|
||||
* @param {number} shaderType The type of shader.
|
||||
* @param {function(string): void) optErrorCallback callback for errors.
|
||||
* @return {!WebGLShader} The created shader.
|
||||
*/
|
||||
const loadShader = function (gl, shaderSource, shaderType, optErrorCallback) {
|
||||
var errFn = optErrorCallback || error;
|
||||
// Create the shader object
|
||||
var shader = gl.createShader(shaderType);
|
||||
|
||||
// Load the shader source
|
||||
gl.shaderSource(shader, shaderSource);
|
||||
|
||||
// Compile the shader
|
||||
gl.compileShader(shader);
|
||||
|
||||
// Check the compile status
|
||||
var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
|
||||
if (!compiled) {
|
||||
// Something went wrong during compilation; get the error
|
||||
var lastError = gl.getShaderInfoLog(shader);
|
||||
errFn("*** Error compiling shader '" + shader + "':" + lastError);
|
||||
gl.deleteShader(shader);
|
||||
return null;
|
||||
}
|
||||
|
||||
return shader;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Creates a program, attaches shaders, binds attrib locations, links the
|
||||
* program and calls useProgram.
|
||||
* @param {!Array.<!WebGLShader>} shaders The shaders to attach
|
||||
* @param {!Array.<string>} optAttribs The attribs names.
|
||||
* @param {!Array.<number>} optLocations The locations for the attribs.
|
||||
*/
|
||||
const loadProgram = function (gl, shaders, optAttribs, optLocations) {
|
||||
var program = gl.createProgram();
|
||||
for (var i = 0; i < shaders.length; ++i) {
|
||||
gl.attachShader(program, shaders[i]);
|
||||
}
|
||||
if (optAttribs) {
|
||||
for (var i = 0; i < optAttribs.length; ++i) {
|
||||
gl.bindAttribLocation(
|
||||
program,
|
||||
optLocations ? optLocations[i] : i,
|
||||
optAttribs[i]);
|
||||
}
|
||||
}
|
||||
gl.linkProgram(program);
|
||||
|
||||
// Check the link status
|
||||
const linked = gl.getProgramParameter(program, gl.LINK_STATUS);
|
||||
if (!linked) {
|
||||
// something went wrong with the link
|
||||
const lastError = gl.getProgramInfoLog(program);
|
||||
error('Error in program linking:' + lastError);
|
||||
|
||||
gl.deleteProgram(program);
|
||||
return null;
|
||||
}
|
||||
return program;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Loads a shader from a script tag.
|
||||
* @param {!WebGLContext} gl The WebGLContext to use.
|
||||
* @param {string} scriptId The id of the script tag.
|
||||
* @param {number} optShaderType The type of shader. If not passed in it will
|
||||
* be derived from the type of the script tag.
|
||||
* @param {function(string): void) optErrorCallback callback for errors.
|
||||
* @return {!WebGLShader} The created shader.
|
||||
*/
|
||||
const createShaderFromScript = function (
|
||||
gl, scriptId, optShaderType, optErrorCallback
|
||||
) {
|
||||
var shaderSource = '';
|
||||
var shaderType;
|
||||
var shaderScript = document.getElementById(scriptId);
|
||||
if (!shaderScript) {
|
||||
throw new Error('*** Error: unknown script element' + scriptId);
|
||||
}
|
||||
shaderSource = shaderScript.text;
|
||||
|
||||
if (!optShaderType) {
|
||||
if (shaderScript.type === 'x-shader/x-vertex') {
|
||||
shaderType = gl.VERTEX_SHADER;
|
||||
} else if (shaderScript.type === 'x-shader/x-fragment') {
|
||||
shaderType = gl.FRAGMENT_SHADER;
|
||||
} else if (
|
||||
shaderType !== gl.VERTEX_SHADER &&
|
||||
shaderType !== gl.FRAGMENT_SHADER
|
||||
) {
|
||||
throw new Error('*** Error: unknown shader type');
|
||||
}
|
||||
}
|
||||
|
||||
return loadShader(
|
||||
gl,
|
||||
shaderSource,
|
||||
optShaderType || shaderType,
|
||||
optErrorCallback
|
||||
);
|
||||
};
|
||||
|
||||
if (typeof exports === 'object' && typeof module !== 'undefined') {
|
||||
module.exports = {
|
||||
setupWebGL : setupWebGL,
|
||||
createProgram : loadProgram,
|
||||
createShaderFromScript : createShaderFromScript,
|
||||
getWebGLContext : getWebGLContext,
|
||||
loadShader : loadShader
|
||||
}
|
||||
} else {
|
||||
// export to global
|
||||
window.setupWebGL = setupWebGL;
|
||||
window.createProgram = loadProgram;
|
||||
window.createShaderFromScriptElement = createShaderFromScript;
|
||||
window.getWebGLContext = getWebGLContext;
|
||||
window.loadShader = loadShader;
|
||||
}
|
||||
|
||||
}());
|
||||
Loading…
Add table
Add a link
Reference in a new issue