UNPKG

@awayjs/stage

Version:
306 lines (305 loc) 11.4 kB
import { __extends } from "tslib"; import { AssetBase } from '@awayjs/core'; var AttributesBuffer = /** @class */ (function (_super) { __extends(AttributesBuffer, _super); /** * */ function AttributesBuffer(stride, count) { if (stride === void 0) { stride = 0; } if (count === void 0) { count = 0; } var _this = _super.call(this) || this; _this._count = 0; _this._newCount = 0; _this._stride = 0; _this._newStride = 0; _this._viewVOs = new Array(); _this._stride = _this._newStride = stride; _this._count = _this._newCount = count; _this._buffer = new ArrayBuffer(_this._stride * _this._count); _this._bufferView = new Uint8Array(_this._buffer, 0, _this._buffer.byteLength); return _this; } Object.defineProperty(AttributesBuffer.prototype, "assetType", { /** * @returns {string} */ get: function () { return AttributesBuffer.assetType; }, enumerable: false, configurable: true }); Object.defineProperty(AttributesBuffer.prototype, "stride", { get: function () { if (this._lengthDirty) this._updateLength(); return this._stride; }, set: function (value) { if (this._newStride == value) return; this._newStride = value; this.resize(); }, enumerable: false, configurable: true }); Object.defineProperty(AttributesBuffer.prototype, "count", { get: function () { return this._newCount; }, set: function (value) { if (this._newCount == value) return; this._newCount = value; this.resize(); }, enumerable: false, configurable: true }); Object.defineProperty(AttributesBuffer.prototype, "buffer", { get: function () { if (this._lengthDirty) this._updateLength(); this._contentDirty = false; return this._buffer; }, enumerable: false, configurable: true }); Object.defineProperty(AttributesBuffer.prototype, "bufferView", { get: function () { if (this._lengthDirty) this._updateLength(); this._contentDirty = false; return this._bufferView; }, set: function (value) { this._bufferView = value; this._buffer = this._bufferView.buffer; }, enumerable: false, configurable: true }); Object.defineProperty(AttributesBuffer.prototype, "length", { get: function () { return this._count * this.stride; }, enumerable: false, configurable: true }); /** * */ AttributesBuffer.prototype.invalidate = function () { if (this._contentDirty) return; _super.prototype.invalidate.call(this); this._contentDirty = true; }; /** * * @private */ AttributesBuffer.prototype.resize = function () { if (this._lengthDirty) return; this.clear(); this._lengthDirty = true; //dispose buffer if stride is 0 if (!this._newStride) { this._buffer = null; this._bufferView = null; } }; AttributesBuffer.prototype.cloneBufferView = function () { var attributesBuffer = new AttributesBuffer(this._stride, this._count); attributesBuffer.bufferView.set(this.bufferView); return attributesBuffer; }; AttributesBuffer.prototype.clone = function () { var attributesBuffer = new AttributesBuffer(this._stride, this._count); attributesBuffer.bufferView.set(this.bufferView); var len = this._viewVOs.length; for (var i = 0; i < len; i++) this._viewVOs[i].view._internalClone(attributesBuffer); return attributesBuffer; }; AttributesBuffer.prototype.getView = function (index) { if (index < this._viewVOs.length) return this._viewVOs[index].view; return null; }; AttributesBuffer.prototype._setAttributes = function (viewIndex, arrayBufferView, offset) { if (offset === void 0) { offset = 0; } var array = (arrayBufferView instanceof Uint8Array) ? arrayBufferView : new Uint8Array(arrayBufferView.buffer); var viewVO = this._viewVOs[viewIndex]; var vLength = viewVO.length; var vOffset = viewVO.offset; var vCount = array.length / vLength; //make sure there is enough space in the buffer if (this.count < vCount + offset) this.count = vCount + offset; if (this._lengthDirty) this._updateLength(); //fast path for separate buffers if (this._viewVOs.length == 1) { this._bufferView.set(array); } else { for (var i = 0; i < vCount; i++) { this._bufferView.set(array.subarray(i * vLength, (i + 1) * vLength), (i + offset) * this._stride + vOffset); } } this.invalidate(); }; AttributesBuffer.prototype._addView = function (view) { var viewVO = new ViewVO(view); var len = this._viewVOs.length; viewVO.offset = len ? this._viewVOs[len - 1].offset + this._viewVOs[len - 1].length : 0; this._viewVOs.push(viewVO); if (this._newStride < viewVO.offset + viewVO.length) { this._newStride = viewVO.offset + viewVO.length; this.resize(); } view._index = len; }; AttributesBuffer.prototype._removeView = function (view) { var viewIndex = view._index; var viewVO = this._viewVOs.splice(viewIndex, 1)[0]; var len = this._viewVOs.length; viewVO.dispose(); for (var i = viewIndex; i < len; i++) { viewVO = this._viewVOs[i]; viewVO.offset = i ? this._viewVOs[i - 1].offset + this._viewVOs[i - 1].length : 0; viewVO.view._index = i; } this._newStride = len ? this._viewVOs[len - 1].offset + this._viewVOs[len - 1].length : 0; this.resize(); }; AttributesBuffer.prototype._getOffset = function (viewIndex) { return this._viewVOs[viewIndex].offset; }; AttributesBuffer.prototype._updateLength = function () { this._lengthDirty = false; var i; var j; var len = this._viewVOs.length; var newLength = this._newStride * this._newCount; if (!this._buffer || this._buffer.byteLength != newLength) { var newBuffer = new ArrayBuffer(newLength); var newView = new Uint8Array(newBuffer, 0, newBuffer.byteLength); var viewVO = void 0; var vLength = void 0; var vOffset = void 0; var vOldOffset = void 0; if (this._buffer) { if (this._stride != this._newStride) { for (i = 0; i < len; i++) { viewVO = this._viewVOs[i]; vLength = viewVO.length; vOffset = viewVO.offset; vOldOffset = viewVO.oldOffset; for (j = 0; j < this._count; j++) { if (vOldOffset != null) { newView.set(new Uint8Array(this._buffer, j * this._stride + vOldOffset, vLength), j * this._newStride + vOffset); } } viewVO.oldOffset = viewVO.offset; } this._stride = this._newStride; } else { //TODO: bypass quantisation of bytearray on instantiation newView.set(new Uint8Array(this._buffer, 0, Math.min(newLength, this._buffer.byteLength))); } } this._buffer = newBuffer; this._bufferView = newView; } this._count = this._newCount; }; AttributesBuffer.assetType = '[assets AttributesBuffer]'; return AttributesBuffer; }(AssetBase)); export { AttributesBuffer }; var ViewVO = /** @class */ (function () { function ViewVO(view) { this.view = view; this.length = view.size * view.dimensions; } ViewVO.prototype.dispose = function () { this.view = null; }; ViewVO.prototype.clone = function () { return new ViewVO(this.view); }; return ViewVO; }()); import { AbstractionBase } from '@awayjs/core'; import { Stage } from '../Stage'; /** * * @class away.pool._Stage_AttributesBuffer */ var _Stage_AttributesBuffer = /** @class */ (function (_super) { __extends(_Stage_AttributesBuffer, _super); function _Stage_AttributesBuffer() { return _super !== null && _super.apply(this, arguments) || this; } _Stage_AttributesBuffer.prototype.init = function (attributesBuffer, stage) { _super.prototype.init.call(this, attributesBuffer, stage); this._stage = stage; this._attributesBuffer = attributesBuffer; }; /** * */ _Stage_AttributesBuffer.prototype.onClear = function (event) { _super.prototype.onClear.call(this, event); this._attributesBuffer = null; if (this._indexBuffer) { this._indexBuffer.dispose(); this._indexBuffer = null; } if (this._vertexBuffer) { this._vertexBuffer.dispose(); this._vertexBuffer = null; } }; _Stage_AttributesBuffer.prototype.activate = function (index, size, dimensions, offset, unsigned) { if (unsigned === void 0) { unsigned = false; } this._stage.setVertexBuffer(index, this._getVertexBuffer(), size, dimensions, offset, unsigned); }; _Stage_AttributesBuffer.prototype.draw = function (mode, firstIndex, numIndices) { this._stage.context.drawIndices(mode, this._getIndexBuffer(), firstIndex, numIndices); }; _Stage_AttributesBuffer.prototype._getIndexBuffer = function () { if (!this._indexBuffer) { this._invalid = true; this._indexBuffer = this._stage.context.createIndexBuffer(this._attributesBuffer.count * this._attributesBuffer.stride / 2); //hardcoded assuming UintArray } if (this._invalid) { this._invalid = false; this._indexBuffer.uploadFromByteArray(this._attributesBuffer.buffer, 0, this._attributesBuffer.length); } return this._indexBuffer; }; _Stage_AttributesBuffer.prototype._getVertexBuffer = function () { if (!this._vertexBuffer) { this._invalid = true; this._vertexBuffer = this._stage.context.createVertexBuffer(this._attributesBuffer.count, this._attributesBuffer.stride); } if (this._invalid) { this._invalid = false; this._vertexBuffer.uploadFromByteArray(this._attributesBuffer.buffer, 0, this._attributesBuffer.count); } return this._vertexBuffer; }; return _Stage_AttributesBuffer; }(AbstractionBase)); export { _Stage_AttributesBuffer }; Stage.registerAbstraction(_Stage_AttributesBuffer, AttributesBuffer);