cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
2 lines (1 loc) • 6.02 kB
JavaScript
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});var t=require("./core/Base.js"),e=require("./core/glenum.js"),i=require("./core/Cache.js"),r=require("./core/vendor.js");function s(t){return{byte:r.default.Int8Array,ubyte:r.default.Uint8Array,short:r.default.Int16Array,ushort:r.default.Uint16Array}[t]||r.default.Float32Array}function n(t){return"attr_"+t}function u(t,e,i,r){switch(this.name=t,this.type=e,this.size=i,this.semantic=r||"",this.value=null,i){case 1:this.get=function(t){return this.value[t]},this.set=function(t,e){this.value[t]=e},this.copy=function(t,e){this.value[t]=this.value[t]};break;case 2:this.get=function(t,e){var i=this.value;return e[0]=i[2*t],e[1]=i[2*t+1],e},this.set=function(t,e){var i=this.value;i[2*t]=e[0],i[2*t+1]=e[1]},this.copy=function(t,e){var i=this.value;e*=2,i[t*=2]=i[e],i[t+1]=i[e+1]};break;case 3:this.get=function(t,e){var i=3*t,r=this.value;return e[0]=r[i],e[1]=r[i+1],e[2]=r[i+2],e},this.set=function(t,e){var i=3*t,r=this.value;r[i]=e[0],r[i+1]=e[1],r[i+2]=e[2]},this.copy=function(t,e){var i=this.value;e*=3,i[t*=3]=i[e],i[t+1]=i[e+1],i[t+2]=i[e+2]};break;case 4:this.get=function(t,e){var i=this.value,r=4*t;return e[0]=i[r],e[1]=i[r+1],e[2]=i[r+2],e[3]=i[r+3],e},this.set=function(t,e){var i=this.value,r=4*t;i[r]=e[0],i[r+1]=e[1],i[r+2]=e[2],i[r+3]=e[3]},this.copy=function(t,e){var i=this.value;e*=4,i[t*=4]=i[e],i[t+1]=i[e+1],i[t+2]=i[e+2],i[t+3]=i[e+3]}}}function a(t,e,i,r,s){this.name=t,this.type=e,this.buffer=i,this.size=r,this.semantic=s,this.symbol="",this.needsRemove=!1}function h(t){this.buffer=t,this.count=0}u.prototype.init=function(t){if(!this.value||this.value.length!==t*this.size){var e=s(this.type);this.value=new e(t*this.size)}},u.prototype.fromArray=function(t){var e,i=s(this.type);if(t[0]&&t[0].length){var r=0,n=this.size;e=new i(t.length*n);for(var u=0;u<t.length;u++)for(var a=0;a<n;a++)e[r++]=t[u][a]}else e=new i(t);this.value=e},u.prototype.clone=function(t){return new u(this.name,this.type,this.size,this.semantic)};var f=t.default.extend((function(){return{attributes:{},indices:null,dynamic:!0,_enabledAttributes:null,__used:0}}),(function(){this._cache=new i.default,this._attributeList=Object.keys(this.attributes),this.__vaoCache={}}),{mainAttribute:"",pick:null,pickByRay:null,dirty:function(){for(var t=this.getEnabledAttributes(),e=0;e<t.length;e++)this.dirtyAttribute(t[e]);this.dirtyIndices(),this._enabledAttributes=null,this._cache.dirty("any")},dirtyIndices:function(){this._cache.dirtyAll("indices")},dirtyAttribute:function(t){this._cache.dirtyAll(n(t)),this._cache.dirtyAll("attributes")},getTriangleIndices:function(t,e){if(t<this.triangleCount&&t>=0){e||(e=[]);var i=this.indices;return e[0]=i[3*t],e[1]=i[3*t+1],e[2]=i[3*t+2],e}},setTriangleIndices:function(t,e){var i=this.indices;i[3*t]=e[0],i[3*t+1]=e[1],i[3*t+2]=e[2]},isUseIndices:function(){return!!this.indices},initIndicesFromArray:function(t){var e,i=this.vertexCount>65535?r.default.Uint32Array:r.default.Uint16Array;if(t[0]&&t[0].length){var s=0;e=new i(3*t.length);for(var n=0;n<t.length;n++)for(var u=0;u<3;u++)e[s++]=t[n][u]}else e=new i(t);this.indices=e},createAttribute:function(t,e,i,r){var s=new u(t,e,i,r);return this.attributes[t]&&this.removeAttribute(t),this.attributes[t]=s,this._attributeList.push(t),s},removeAttribute:function(t){var e=this._attributeList,i=e.indexOf(t);return i>=0&&(e.splice(i,1),delete this.attributes[t],!0)},getAttribute:function(t){return this.attributes[t]},getEnabledAttributes:function(){var t=this._enabledAttributes,e=this._attributeList;if(t)return t;for(var i=[],r=this.vertexCount,s=0;s<e.length;s++){var n=e[s],u=this.attributes[n];u.value&&u.value.length===r*u.size&&i.push(n)}return this._enabledAttributes=i,i},getBufferChunks:function(t){var e=this._cache;e.use(t.__uid__);var i=e.isDirty("attributes"),r=e.isDirty("indices");if(i||r){this._updateBuffer(t.gl,i,r);for(var s=this.getEnabledAttributes(),u=0;u<s.length;u++)e.fresh(n(s[u]));e.fresh("attributes"),e.fresh("indices")}return e.fresh("any"),e.get("chunks")},_updateBuffer:function(t,e,i){var r=this._cache,s=r.get("chunks"),u=!1;s||((s=[])[0]={attributeBuffers:[],indicesBuffer:null},r.put("chunks",s),u=!0);var f=s[0],c=f.attributeBuffers,l=f.indicesBuffer;if(e||u){var o=this.getEnabledAttributes(),v={};if(!u)for(var d=0;d<c.length;d++)v[c[d].name]=c[d];for(var b=0;b<o.length;b++){var _,A,y=o[b],g=this.attributes[y];u||(_=v[y]),A=_?_.buffer:t.createBuffer(),r.isDirty(n(y))&&(t.bindBuffer(t.ARRAY_BUFFER,A),t.bufferData(t.ARRAY_BUFFER,g.value,this.dynamic?t.DYNAMIC_DRAW:t.STATIC_DRAW)),c[b]=new a(y,g.type,A,g.size,g.semantic)}for(d=b;d<c.length;d++)t.deleteBuffer(c[d].buffer);c.length=b}this.isUseIndices()&&(i||u)&&(l||(l=new h(t.createBuffer()),f.indicesBuffer=l),l.count=this.indices.length,t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,l.buffer),t.bufferData(t.ELEMENT_ARRAY_BUFFER,this.indices,this.dynamic?t.DYNAMIC_DRAW:t.STATIC_DRAW))},dispose:function(t){var e=this._cache;e.use(t.__uid__);var i=e.get("chunks");if(i)for(var r=0;r<i.length;r++){for(var s=i[r],n=0;n<s.attributeBuffers.length;n++){var u=s.attributeBuffers[n];t.gl.deleteBuffer(u.buffer)}s.indicesBuffer&&t.gl.deleteBuffer(s.indicesBuffer.buffer)}if(this.__vaoCache){var a=t.getGLExtension("OES_vertex_array_object");for(var h in this.__vaoCache){var f=this.__vaoCache[h].vao;f&&a.deleteVertexArrayOES(f)}}this.__vaoCache={},e.deleteContext(t.__uid__)}});Object.defineProperty&&(Object.defineProperty(f.prototype,"vertexCount",{enumerable:!1,get:function(){var t=this.attributes[this.mainAttribute];return t||(t=this.attributes[this._attributeList[0]]),t&&t.value?t.value.length/t.size:0}}),Object.defineProperty(f.prototype,"triangleCount",{enumerable:!1,get:function(){var t=this.indices;return t?t.length/3:0}})),f.STATIC_DRAW=e.default.STATIC_DRAW,f.DYNAMIC_DRAW=e.default.DYNAMIC_DRAW,f.STREAM_DRAW=e.default.STREAM_DRAW,f.AttributeBuffer=a,f.IndicesBuffer=h,f.Attribute=u;var c=f;exports.default=c;