diff --git a/lib/minimal-gltf-loader.min.js b/lib/minimal-gltf-loader.min.js index 805bdfc..80f41dd 100644 --- a/lib/minimal-gltf-loader.min.js +++ b/lib/minimal-gltf-loader.min.js @@ -1 +1 @@ -var MinimalGLTFLoader=MinimalGLTFLoader||{};!function(){"use strict";function e(e,t,r,s){switch(s){case 5122:return new Int16Array(e,t,r);case 5123:return new Uint16Array(e,t,r);case 5124:return new Int32Array(e,t,r);case 5125:return new Uint32Array(e,t,r);case 5126:return new Float32Array(e,t,r);default:return null}}function t(t,r){return e(t,r.byteOffset,r.count*T[r.type],r.componentType)}function r(e){var t="",r=e.lastIndexOf("/");return-1!==r&&(t=e.substring(0,r+1)),t}function s(e,t){var r=new XMLHttpRequest;r.overrideMimeType("application/json"),r.open("GET",e,!0),r.onreadystatechange=function(){4==r.readyState&&"200"==r.status&&t(r.responseText,this)},r.send(null)}function a(e,t){var r=new XMLHttpRequest;r.responseType="arraybuffer",r.open("GET",e,!0),r.onreadystatechange=function(){if(4==r.readyState&&"200"==r.status){var e=r.response;e&&t&&t(e)}},r.send(null)}function i(e,t){var r=new XMLHttpRequest;r.responseType="text",r.open("GET",e,!0),r.onreadystatechange=function(){if(4==r.readyState&&"200"==r.status){var e=r.response;e&&t&&t(e)}},r.send(null)}function n(e,t){var r=new Image;r.src=e,r.onload=function(){t(r)}}function o(e,t,r){var s=e.createShader(r);return e.shaderSource(s,t),e.compileShader(s),s}function f(e,t,r){var s=e.createProgram(),a=o(e,t,e.VERTEX_SHADER),i=o(e,r,e.FRAGMENT_SHADER);e.attachShader(s,a),e.deleteShader(a),e.attachShader(s,i),e.deleteShader(i),e.linkProgram(s);var n=e.getProgramInfoLog(s);return n&&console.log(n),n=e.getShaderInfoLog(a),n&&console.log(n),n=e.getShaderInfoLog(i),n&&console.log(n),s}var h,u=MinimalGLTFLoader.Scene=function(){this.meshes=[]},c=MinimalGLTFLoader.Mesh=function(){this.meshID="",this.primitives=[]},d=MinimalGLTFLoader.Primitive=function(){this.mode=4,this.matrix=mat4.create(),this.indices=null,this.indicesComponentType=5123,this.vertexBuffer=null,this.attributes={},this.material=null,this.technique=null},l=MinimalGLTFLoader.glTFModel=function(){this.defaultScene="",this.scenes={},this.nodeMatrix={},this.json=null,this.shaders={},this.programs={},this.images={}},p=MinimalGLTFLoader.glTFLoader=function(e){h=e,this._init(),this.glTF=null};p.prototype._init=function(){this._parseDone=!1,this._loadDone=!1,this._bufferRequested=0,this._bufferLoaded=0,this._buffers={},this._bufferTasks={},this._bufferViews={},this._shaderRequested=0,this._shaderLoaded=0,this._imageRequested=0,this._imageLoaded=0,this._pendingTasks=0,this._finishedPendingTasks=0,this.onload=null},p.prototype._getBufferViewData=function(e,t,r){var s=this._bufferViews[t];if(s)r(s);else{var a=e.bufferViews[t],i=this._buffers[a.buffer];if(i)this._bufferViews[t]=i.slice(a.byteOffset,a.byteOffset+a.byteLength),r(s);else{this._pendingTasks++;var n=this._bufferTasks[a.buffer];n||(this._bufferTasks[a.buffer]=[],n=this._bufferTasks[a.buffer]);var o=this;n.push(function(e){var s=o._bufferViews[t];s||(console.log("create new BufferView Data for "+t),s=o._bufferViews[t]=e.slice(a.byteOffset,a.byteOffset+a.byteLength)),o._finishedPendingTasks++,r(s)})}}},p.prototype._checkComplete=function(){this._bufferRequested==this._bufferLoaded&&this._shaderRequested==this._shaderLoaded&&this._imageRequested==this._imageLoaded&&(this._loadDone=!0),this._loadDone&&this._parseDone&&this._pendingTasks==this._finishedPendingTasks&&this.onload(this.glTF)},p.prototype._parseGLTF=function(e){if(this.glTF.json=e,this.glTF.defaultScene=e.scene,e.scenes)for(var t in e.scenes){var r=new u;this.glTF.scenes[t]=r;for(var s=e.scenes[t],a=s.nodes,i=a.length,n=0;i>n;++n){var o=a[n];this._parseNode(e,o,r)}}this._parseDone=!0,this._checkComplete()};var m=vec3.create(),g=quat.create(),_=vec3.create(),v=mat4.create();p.prototype._parseNode=function(e,t,r,s){var a=e.nodes[t];void 0===s&&(s=mat4.create());var i=mat4.create();if(a.hasOwnProperty("matrix")){for(var n=0;16>n;++n)i[n]=a.matrix[n];mat4.multiply(i,s,i)}else vec3.set(m,a.translation[0],a.translation[1],a.translation[2]),quat.set(g,a.rotation[0],a.rotation[1],a.rotation[2],a.rotation[3]),mat4.fromRotationTranslation(v,g,m),mat4.multiply(i,i,v),vec3.set(_,a.scale[0],a.scale[1],a.scale[2]),mat4.scale(i,i,_);this.glTF.nodeMatrix[t]=i;var o=a.meshes;if(o)for(var f=o.length,h=0;f>h;++h){var u=new c;r.meshes.push(u);var l=o[h],p=e.meshes[l];u.meshID=l;for(var b=p.primitives,T=b.length,y=0;T>y;++y){var L=new d;u.primitives.push(L);var S=b[y];S.indices&&this._parseIndices(e,S,L),this._parseAttributes(e,S,L,i),L.material=e.materials[S.material],L.material.technique&&(L.technique=e.techniques[L.material.technique])}}for(var w=a.children,F=w.length,k=0;F>k;++k){var M=w[k];this._parseNode(e,M,r,i)}},p.prototype._parseIndices=function(e,r,s){var a=r.indices,i=e.accessors[a];s.mode=r.mode||4,s.indicesComponentType=i.componentType;var n=this;this._getBufferViewData(e,i.bufferView,function(e){s.indices=t(e,i),n._checkComplete()})},p.prototype._parseAttributes=function(t,r,s,a){var i=Object.keys(r.attributes)[0],n=t.accessors[r.attributes[i]],o=n.bufferView,f=t.bufferViews[o],h=this;this._getBufferViewData(t,o,function(i){s.vertexBuffer=e(i,0,f.byteLength/b[n.componentType],n.componentType);for(var o in r.attributes){var u=r.attributes[o],c=t.accessors[u],d=b[c.componentType];c.byteStride/d,c.byteOffset/d,c.count;mat4.copy(s.matrix,a),s.attributes[o]={size:T[c.type],type:c.componentType,stride:c.byteStride,offset:c.byteOffset}}h._checkComplete()})},p.prototype.loadGLTF=function(e,t){this._init(),this.onload=t||function(e){console.log("glTF model loaded."),console.log(e)},this.glTF=new l,this.baseUri=r(e);var o=this;s(e,function(e){var t,r=JSON.parse(e),s=function(e){if(o._buffers[t]=e,o._bufferLoaded++,o._bufferTasks[t]){var r,s;for(r=0,s=o._bufferTasks[t].length;s>r;++r)o._bufferTasks[t][r](e)}o._checkComplete()};if(r.buffers)for(t in r.buffers)o._bufferRequested++,a(o.baseUri+r.buffers[t].uri,s);var u,c=function(e){o._imageLoaded++,o.glTF.images[u]=e,o._checkComplete()};if(r.images)for(u in r.images)o._imageRequested++,n(o.baseUri+r.images[u].uri,c);var d,l,p=function(e){o._shaderLoaded++,l.vertexShader=e,l.fragmentShader&&(l.program=f(h,l.vertexShader,l.fragmentShader),o._checkComplete())},m=function(e){o._shaderLoaded++,l.fragmentShader=e,l.vertexShader&&(l.program=f(h,l.vertexShader,l.fragmentShader),o._checkComplete())};if(r.programs)for(d in r.programs){l=o.glTF.programs[d]={vertexShader:null,fragmentShader:null,program:null};var g=r.programs[d];o._shaderRequested+=2,i(o.baseUri+r.shaders[g.vertexShader].uri,p),i(o.baseUri+r.shaders[g.fragmentShader].uri,m)}o._parseGLTF(r)})};var b={5120:1,5121:1,5122:2,5123:2,5126:4},T={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT2:4,MAT3:9,MAT4:16};MinimalGLTFLoader.Attributes=["POSITION","NORMAL","TEXCOORD","COLOR","JOINT","WEIGHT"]}(); \ No newline at end of file +var MinimalGLTFLoader=MinimalGLTFLoader||{};!function(){"use strict";function e(e,t,r,s){switch(s){case 5122:return new Int16Array(e,t,r);case 5123:return new Uint16Array(e,t,r);case 5124:return new Int32Array(e,t,r);case 5125:return new Uint32Array(e,t,r);case 5126:return new Float32Array(e,t,r);default:return null}}function t(t,r){return e(t,r.byteOffset,r.count*T[r.type],r.componentType)}function r(e){var t="",r=e.lastIndexOf("/");return-1!==r&&(t=e.substring(0,r+1)),t}function s(e,t){var r=new XMLHttpRequest;r.overrideMimeType("application/json"),r.open("GET",e,!0),r.onreadystatechange=function(){4==r.readyState&&"200"==r.status&&t(r.responseText,this)},r.send(null)}function a(e,t){var r=new XMLHttpRequest;r.responseType="arraybuffer",r.open("GET",e,!0),r.onreadystatechange=function(){if(4==r.readyState&&"200"==r.status){var e=r.response;e&&t&&t(e)}},r.send(null)}function i(e,t){var r=new XMLHttpRequest;r.responseType="text",r.open("GET",e,!0),r.onreadystatechange=function(){if(4==r.readyState&&"200"==r.status){var e=r.response;e&&t&&t(e)}},r.send(null)}function n(e,t,r){var s=new Image;s.src=e,s.onload=function(){r(s,t)}}function o(e,t,r){var s=e.createShader(r);return e.shaderSource(s,t),e.compileShader(s),s}function f(e,t,r){var s=e.createProgram(),a=o(e,t,e.VERTEX_SHADER),i=o(e,r,e.FRAGMENT_SHADER);e.attachShader(s,a),e.deleteShader(a),e.attachShader(s,i),e.deleteShader(i),e.linkProgram(s);var n=e.getProgramInfoLog(s);return n&&console.log(n),n=e.getShaderInfoLog(a),n&&console.log(n),n=e.getShaderInfoLog(i),n&&console.log(n),s}var h,u=MinimalGLTFLoader.Scene=function(){this.meshes=[]},c=MinimalGLTFLoader.Mesh=function(){this.meshID="",this.primitives=[]},d=MinimalGLTFLoader.Primitive=function(){this.mode=4,this.matrix=mat4.create(),this.indices=null,this.indicesComponentType=5123,this.vertexBuffer=null,this.attributes={},this.material=null,this.technique=null},l=MinimalGLTFLoader.glTFModel=function(){this.defaultScene="",this.scenes={},this.nodeMatrix={},this.json=null,this.shaders={},this.programs={},this.images={}},p=MinimalGLTFLoader.glTFLoader=function(e){h=e,this._init(),this.glTF=null};p.prototype._init=function(){this._parseDone=!1,this._loadDone=!1,this._bufferRequested=0,this._bufferLoaded=0,this._buffers={},this._bufferTasks={},this._bufferViews={},this._shaderRequested=0,this._shaderLoaded=0,this._imageRequested=0,this._imageLoaded=0,this._pendingTasks=0,this._finishedPendingTasks=0,this.onload=null},p.prototype._getBufferViewData=function(e,t,r){var s=this._bufferViews[t];if(s)r(s);else{var a=e.bufferViews[t],i=this._buffers[a.buffer];if(i)this._bufferViews[t]=i.slice(a.byteOffset,a.byteOffset+a.byteLength),r(s);else{this._pendingTasks++;var n=this._bufferTasks[a.buffer];n||(this._bufferTasks[a.buffer]=[],n=this._bufferTasks[a.buffer]);var o=this;n.push(function(e){var s=o._bufferViews[t];s||(console.log("create new BufferView Data for "+t),s=o._bufferViews[t]=e.slice(a.byteOffset,a.byteOffset+a.byteLength)),o._finishedPendingTasks++,r(s)})}}},p.prototype._checkComplete=function(){this._bufferRequested==this._bufferLoaded&&this._shaderRequested==this._shaderLoaded&&this._imageRequested==this._imageLoaded&&(this._loadDone=!0),this._loadDone&&this._parseDone&&this._pendingTasks==this._finishedPendingTasks&&this.onload(this.glTF)},p.prototype._parseGLTF=function(e){if(this.glTF.json=e,this.glTF.defaultScene=e.scene,e.scenes)for(var t in e.scenes){var r=new u;this.glTF.scenes[t]=r;for(var s=e.scenes[t],a=s.nodes,i=a.length,n=0;i>n;++n){var o=a[n];this._parseNode(e,o,r)}}this._parseDone=!0,this._checkComplete()};var m=vec3.create(),g=quat.create(),_=vec3.create(),v=mat4.create();p.prototype._parseNode=function(e,t,r,s){var a=e.nodes[t];void 0===s&&(s=mat4.create());var i=mat4.create();if(a.hasOwnProperty("matrix")){for(var n=0;16>n;++n)i[n]=a.matrix[n];mat4.multiply(i,s,i)}else vec3.set(m,a.translation[0],a.translation[1],a.translation[2]),quat.set(g,a.rotation[0],a.rotation[1],a.rotation[2],a.rotation[3]),mat4.fromRotationTranslation(v,g,m),mat4.multiply(i,i,v),vec3.set(_,a.scale[0],a.scale[1],a.scale[2]),mat4.scale(i,i,_);this.glTF.nodeMatrix[t]=i;var o=a.meshes;if(o)for(var f=o.length,h=0;f>h;++h){var u=new c;r.meshes.push(u);var l=o[h],p=e.meshes[l];u.meshID=l;for(var b=p.primitives,T=b.length,y=0;T>y;++y){var L=new d;u.primitives.push(L);var S=b[y];S.indices&&this._parseIndices(e,S,L),this._parseAttributes(e,S,L,i),L.material=e.materials[S.material],L.material.technique&&(L.technique=e.techniques[L.material.technique])}}for(var w=a.children,F=w.length,k=0;F>k;++k){var M=w[k];this._parseNode(e,M,r,i)}},p.prototype._parseIndices=function(e,r,s){var a=r.indices,i=e.accessors[a];s.mode=r.mode||4,s.indicesComponentType=i.componentType;var n=this;this._getBufferViewData(e,i.bufferView,function(e){s.indices=t(e,i),n._checkComplete()})},p.prototype._parseAttributes=function(t,r,s,a){var i=Object.keys(r.attributes)[0],n=t.accessors[r.attributes[i]],o=n.bufferView,f=t.bufferViews[o],h=this;this._getBufferViewData(t,o,function(i){s.vertexBuffer=e(i,0,f.byteLength/b[n.componentType],n.componentType);for(var o in r.attributes){var u=r.attributes[o],c=t.accessors[u],d=b[c.componentType];c.byteStride/d,c.byteOffset/d,c.count;mat4.copy(s.matrix,a),s.attributes[o]={size:T[c.type],type:c.componentType,stride:c.byteStride,offset:c.byteOffset}}h._checkComplete()})},p.prototype.loadGLTF=function(e,t){this._init(),this.onload=t||function(e){console.log("glTF model loaded."),console.log(e)},this.glTF=new l,this.baseUri=r(e);var o=this;s(e,function(e){var t,r=JSON.parse(e),s=function(e){if(o._buffers[t]=e,o._bufferLoaded++,o._bufferTasks[t]){var r,s;for(r=0,s=o._bufferTasks[t].length;s>r;++r)o._bufferTasks[t][r](e)}o._checkComplete()};if(r.buffers)for(t in r.buffers)o._bufferRequested++,a(o.baseUri+r.buffers[t].uri,s);var u,c=function(e,t){o._imageLoaded++,o.glTF.images[t]=e,o._checkComplete()};if(r.images)for(u in r.images)o._imageRequested++,n(o.baseUri+r.images[u].uri,u,c);var d,l,p=function(e){o._shaderLoaded++,l.vertexShader=e,l.fragmentShader&&(l.program=f(h,l.vertexShader,l.fragmentShader),o._checkComplete())},m=function(e){o._shaderLoaded++,l.fragmentShader=e,l.vertexShader&&(l.program=f(h,l.vertexShader,l.fragmentShader),o._checkComplete())};if(r.programs)for(d in r.programs){l=o.glTF.programs[d]={vertexShader:null,fragmentShader:null,program:null};var g=r.programs[d];o._shaderRequested+=2,i(o.baseUri+r.shaders[g.vertexShader].uri,p),i(o.baseUri+r.shaders[g.fragmentShader].uri,m)}o._parseGLTF(r)})};var b={5120:1,5121:1,5122:2,5123:2,5126:4},T={SCALAR:1,VEC2:2,VEC3:3,VEC4:4,MAT2:4,MAT3:9,MAT4:16};MinimalGLTFLoader.Attributes=["POSITION","NORMAL","TEXCOORD","COLOR","JOINT","WEIGHT"]}(); \ No newline at end of file