stratux/web/js/j3di-all.min.js

3 wiersze
32 KiB
JavaScript
Czysty Zwykły widok Historia

function initWebGL(t){var r=document.getElementById(t);return gl=WebGLUtils.setupWebGL(r)}function log(t){window.console&&window.console.log&&window.console.log(t)}function simpleSetup(t,r,i,e,a,m){var n=loadShader(t,r),x=loadShader(t,i),s=t.createProgram();t.attachShader(s,n),t.attachShader(s,x);for(var o=0;o<e.length;++o)t.bindAttribLocation(s,o,e[o]);t.linkProgram(s);var h=t.getProgramParameter(s,t.LINK_STATUS);if(!h&&!t.isContextLost()){var $=t.getProgramInfoLog(s);return log("Error in program linking:"+$),t.deleteProgram(s),t.deleteProgram(x),t.deleteProgram(n),null}return t.useProgram(s),t.clearColor(a[0],a[1],a[2],a[3]),t.clearDepth(m),t.enable(t.DEPTH_TEST),t.enable(t.BLEND),t.blendFunc(t.SRC_ALPHA,t.ONE_MINUS_SRC_ALPHA),s}function loadShader(t,r){var i=document.getElementById(r);return i?loadShaderScript(t,i.text,i.type):(log("*** Error: shader script '"+r+"' not found"),null)}function loadShaderVertexScript(t,r){return loadShaderScript(t,r,"x-shader/x-vertex")}function loadShaderFragmentScript(t,r){return loadShaderScript(t,r,"x-shader/x-fragment")}function loadShaderScript(t,r,i){if(!r)return log("*** Error: shader script missing"),null;if("x-shader/x-vertex"==i)var e=t.VERTEX_SHADER;else{if("x-shader/x-fragment"!=i)return log("*** Error: shader script of undefined type '"+i+"'"),null;var e=t.FRAGMENT_SHADER}var a=t.createShader(e);t.shaderSource(a,r),t.compileShader(a);var m=t.getShaderParameter(a,t.COMPILE_STATUS);if(!m&&!t.isContextLost()){var n=t.getShaderInfoLog(a);return log("*** Error compiling shader: "+n),t.deleteShader(a),null}return a}function clearLoadingObjects(){for(var t=0;t<g_loadingObjects.length;++t)g_loadingObjects[t].onreadystatechange=void 0;g_loadingObjects=[]}function loadObj(t,r){var i={loaded:!1};i.ctx=t;var e=new XMLHttpRequest;return e.obj=i,g_loadingObjects.push(e),e.onreadystatechange=function(){processLoadObj(e)},e.open("GET",r,!0),e.send(null),i}function processLoadObj(t){log("req="+t),4==t.readyState&&(g_loadingObjects.splice(g_loadingObjects.indexOf(t),1),doLoadObj(t.obj,t.responseText))}function doLoadObj(t,r){vertexArray=[],normalArray=[],textureArray=[],indexArray=[];var i=[],e=[],a=[],m={},n=0,x={},s=[-1,0];x._unnamed=s;var o=r.split("\n");for(var h in o){var $=o[h].replace(/[ \t]+/g," ").replace(/\s\s*$/,"");if("#"!=$[0]){var u=$.split(" ");if("g"==u[0])s=[indexArray.length,0],x[u[1]]=s;else if("v"==u[0])i.push(parseFloat(u[1])),i.push(parseFloat(u[2])),i.push(parseFloat(u[3]));else if("vt"==u[0])a.push(parseFloat(u[1])),a.push(parseFloat(u[2]));else if("vn"==u[0])e.push(parseFloat(u[1])),e.push(parseFloat(u[2])),e.push(parseFloat(u[3]));else if("f"==u[0]){if(4!=u.length){log("*** Error: face '"+$+"' not handled");continue}for(var f=1;4>f;++f){if(!(u[f]in m)){var l,c,d,g=u[f].split("/");if(1==g.length)l=parseInt(g[0])-1,c=l,d=l;else{if(!(g.length=3))return t.ctx.console.log("*** Error: did not understand face '"+u[f]+"'"),null;l=parseInt(g[0])-1,d=parseInt(g[1])-1,c=parseInt(g[2])-1}var p=0,v=0,A=0;3*l+2<i.length&&(p=i[3*l],v=i[3*l+1],A=i[3*l+2]),vertexArray.push(p),vertexArray.push(v),vertexArray.push(A),p=0,v=0,2*d+1<a.length&&(p=a[2*d],v=a[2*d+1]),textureArray.push(p),textureArray.push(v),p=0,v=0,A=1,3*c+2<e.length&&(p=e[3*c],v=e[3*c+1],A=e[3*c+2]),normalArray.push(p),normalArray.push(v),normalArray.push(A),m[u[f]]=n++}indexArray.push(m[u[f]]),s[1]++}}}}t.normalObject=t.ctx.createBuffer(),t.ctx.bindBuffer(t.ctx.ARRAY_BUFFER,t.normalObject),t.ctx.bufferData(t.ctx.ARRAY_BUFFER,new Float32Array(normalArray),t.ctx.STATIC_DRAW),t.texCoordObject=t.ctx.createBuffer(),t.ctx.bindBuffer(t.ctx.ARRAY_BUFFER,t.texCoordObject),t.ctx.bufferData(t.ctx.ARRAY_BUFFER,new Float32Array(textureArray),t.ctx.STATIC_DRAW),t.vertexObject=t.ctx.createBuffer(),t.ctx.bindBuffer(t.ctx.ARRAY_BUFFER,t.vertexObject),t.ctx.bufferData(t.ctx.ARRAY_BUFFER,new Float32Array(vertexArray),t.ctx.STATIC_DRAW),t.numIndices=indexArray.length,t.indexObject=t.ctx.createBuffer(),t.ctx.bindBuffer(t.ctx.ELEMENT_ARRAY_BUFFER,t.indexObject),t.ctx.bufferData(t.ctx.ELEMENT_ARRAY_BUFFER,new Uint16Array(indexArray),t.ctx.ST
var a=t.$matrix.m44+r*t.$matrix.m14+i*t.$matrix.m24+e*t.$matrix.m34;1!=a&&0!=a&&(this[0]/=a,this[1]/=a,this[2]/=a)},J3DIVector3.prototype.toString=function(){return"["+this[0]+","+this[1]+","+this[2]+"]"};