-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgl-shorthand-v1.0.min.js
26 lines (26 loc) · 5.08 KB
/
webgl-shorthand-v1.0.min.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
var SHADER=(function(){if(!gl){console.error("ERROR: gl has not been defined.");return}
SHADER={};SHADER.vertex=function(source){this.id="vertex";if(source.includes("void main")){this.source=source}else{let s=document.getElementById(source);s=s.innerHTML;s=s.toString();if(!s){console.error("ERROR: Shader not found at SHADER.vertex().");return!1}
this.source=s}
this.shader=gl.createShader(gl.VERTEX_SHADER);gl.shaderSource(this.shader,this.source);return this};SHADER.fragment=function(source){this.id="fragment";if(source.includes("void main")){this.source=source}else{let s=document.getElementById(source);s=s.innerHTML;s=s.toString();if(!s){console.error("ERROR: Shader not found at SHADER.fragment().");return!1}
this.source=s}
this.shader=gl.createShader(gl.FRAGMENT_SHADER);gl.shaderSource(this.shader,this.source);return this};SHADER.compile=function(shader){if(!this.shader){console.error("ERROR: No shader specified at SHADER.compile().");return!1}
gl.compileShader(this.shader);if(!gl.getShaderParameter(this.shader,gl.COMPILE_STATUS)){console.error("ERROR: could not compile shader.",gl.getShaderInfoLog(this.shader));return!1}
return this};SHADER.vertex.prototype.compile=SHADER.compile;SHADER.fragment.prototype.compile=SHADER.compile;return SHADER})();var PROGRAM=(function(){if(!gl){console.error("ERROR: gl has not been defined.");return}
PROGRAM={};PROGRAM.new=function(){this.id="program";if(arguments.length<2){console.error("ERROR: Missing a shader as input at PROGRAM.new().");return!1}
let hasVert=!1,hasFrag=!1;this.program=gl.createProgram();for(let i in arguments){gl.attachShader(this.program,arguments[i].shader);if(!hasVert&&arguments[i].id==="vertex"){hasVert=!0}
if(!hasFrag&&arguments[i].id==="fragment"){hasFrag=!0}}
if(!hasVert){console.warn("WARNING: No vertex shader given at PROGRAM.new().")}
if(!hasFrag){console.warn("WARNING: No fragment shader given at PROGRAM.new().")}
return this};PROGRAM.link=function(){gl.linkProgram(this.program);if(!gl.getProgramParameter(this.program,gl.LINK_STATUS)){console.error("ERROR: Unsuccesful program linking.",gl.getProgramInfoLog(this.program));return!1}
return this};PROGRAM.use=function(){gl.useProgram(this.program);return this};PROGRAM.newHandle=function(type,identifier){let location;if(!identifier){console.error("ERROR: Missing argument at PROGRAM.newHandle().");return!1}
switch(type){case "attribute":location=gl.getAttribLocation(this.program,identifier);break;case "uniform":location=gl.getUniformLocation(this.program,identifier);break;default:console.error("ERROR: type is not a defined type.");return!1;break}
return location};PROGRAM.useHandle=function(type,location,elementsPer,vertexSize,offsetTo,input){if(!location&&location!==0){console.error("ERROR: Missing argument at PROGRAM.useHandle().");return!1}
offsetTo=offsetTo||0;vertexSize=vertexSize||1;elementsPer=elementsPer||1;switch(type){case "attribute":gl.vertexAttribPointer(location,elementsPer,gl.FLOAT,gl.FALSE,vertexSize*Float32Array.BYTES_PER_ELEMENT,offsetTo*Float32Array.BYTES_PER_ELEMENT);gl.enableVertexAttribArray(location);break;case "uniform":switch(input.id){case "matrix4x4":gl.uniformMatrix4fv(location,gl.FALSE,input.value);break}
break;default:console.error("ERROR: type is not a defined type.");return!1;break}
return this};PROGRAM.new.prototype.link=PROGRAM.link;PROGRAM.new.prototype.use=PROGRAM.use;PROGRAM.new.prototype.useHandle=PROGRAM.useHandle;PROGRAM.new.prototype.newHandle=PROGRAM.newHandle;return PROGRAM})();var BUFFER=(function(){if(!gl){console.error("ERROR: gl has not been defined.");return}
BUFFER={};BUFFER.new=function(buffer,type){this.id="buffer";if(!buffer){console.error("ERROR: No buffer specified at BUFFER.new().");return!1}
this.buffer=gl.createBuffer();this.type=type;if(type==="array"||!type){buffer=new Float32Array(buffer);gl.bindBuffer(gl.ARRAY_BUFFER,this.buffer);gl.bufferData(gl.ARRAY_BUFFER,buffer,gl.STATIC_DRAW)}else if(type==="elements"||type!=="array"){buffer=new Uint16Array(buffer);gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,this.buffer);gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,buffer,gl.STATIC_DRAW)}
return this};BUFFER.unbind=function(){if(this.type==="array"||!this.type){gl.bindBuffer(gl.ARRAY_BUFFER,null)}else if(this.type==="elements"||this.type!=="array"){gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,null)}
return this};BUFFER.new.prototype.unbind=BUFFER.unbind;return BUFFER})();var TEXTURE=(function(){if(!gl){console.error("ERROR: gl has not been defined.");return}
TEXTURE={};TEXTURE.sample=function(texture,width,height){if(!width||!height){console.error("ERROR: Invalid dimensions provided at TEXTURE.sample().");return!1}
this.texture=gl.createTexture();gl.bindTexture(gl.TEXTURE_2D,this.texture);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.REPEAT);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.REPEAT);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.NEAREST);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST);gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,width,height,0,gl.RGBA,gl.UNSIGNED_BYTE,texture);return this};TEXTURE.unbind=function(){gl.bindTexture(gl.TEXTURE_2D,null);return this};TEXTURE.sample.prototype.unbind=TEXTURE.unbind;return TEXTURE})()