UNPKG

@awayjs/renderer

Version:
215 lines (214 loc) 7.45 kB
import { __extends } from "tslib"; import { AbstractMethodError, AbstractionBase, } from '@awayjs/core'; import { MaterialUtils } from '../utils/MaterialUtils'; /** * @class RenderableListItem */ var _Render_RenderableBase = /** @class */ (function (_super) { __extends(_Render_RenderableBase, _super); function _Render_RenderableBase() { var _this = _super.call(this) || this; _this._materialDirty = true; _this._elementsDirty = true; _this._styleDirty = true; _this._images = []; _this._samplers = []; _this._count = 0; _this._offset = 0; return _this; } Object.defineProperty(_Render_RenderableBase.prototype, "images", { /** * */ get: function () { if (this._styleDirty) this._updateStyle(); return this._images; }, enumerable: false, configurable: true }); Object.defineProperty(_Render_RenderableBase.prototype, "samplers", { /** * */ get: function () { if (this._styleDirty) this._updateStyle(); return this._samplers; }, enumerable: false, configurable: true }); Object.defineProperty(_Render_RenderableBase.prototype, "uvMatrix", { /** * */ get: function () { if (this._styleDirty) this._updateStyle(); return this._uvMatrix; }, enumerable: false, configurable: true }); Object.defineProperty(_Render_RenderableBase.prototype, "stageElements", { /** * * @returns {_Stage_ElementsBase} */ get: function () { if (this._elementsDirty) this._updateElements(); return this._stageElements; }, enumerable: false, configurable: true }); Object.defineProperty(_Render_RenderableBase.prototype, "renderMaterial", { /** * * @returns {_Render_MaterialBase} */ get: function () { if (this._materialDirty) this._updateMaterial(); return this._renderMaterial; }, enumerable: false, configurable: true }); Object.defineProperty(_Render_RenderableBase.prototype, "entity", { get: function () { return this._pool; }, enumerable: false, configurable: true }); Object.defineProperty(_Render_RenderableBase.prototype, "renderable", { get: function () { return this._useWeak ? this._asset.deref() : this._asset; }, enumerable: false, configurable: true }); /** * * @param renderable * @param sourceEntity * @param surface * @param renderer */ _Render_RenderableBase.prototype.init = function (renderable, entity) { _super.prototype.init.call(this, renderable, entity, true); //store references this._stage = entity.renderer.stage; renderable._renderObjects[entity.id] = this; }; /** * Renders an object to the current render target. * * @private */ _Render_RenderableBase.prototype.draw = function () { var pass = this._renderMaterial._activePass; pass._setRenderState(this); var shader = pass.shader; var elements = this.stageElements; if (shader.activeElements != elements) { shader.activeElements = elements; elements._setRenderState(this, shader); } elements.draw(this, shader, this._count, this._offset); }; _Render_RenderableBase.prototype.onClear = function () { var renderable = this.renderable; if (renderable) delete renderable._renderObjects[this._pool.id]; this._stage = null; this.next = null; this._renderMaterial.removeOwner(this); this._renderMaterial = null; this._stageElements = null; this._materialDirty = true; this._elementsDirty = true; this._styleDirty = true; this._images.length = 0; this._samplers.length = 0; this._uvMatrix = null; _super.prototype.onClear.call(this); }; _Render_RenderableBase.prototype._onInvalidateElements = function () { this._elementsDirty = true; }; _Render_RenderableBase.prototype._onInvalidateMaterial = function () { this._materialDirty = true; this._styleDirty = true; }; _Render_RenderableBase.prototype._onInvalidateStyle = function () { this._styleDirty = true; }; _Render_RenderableBase.prototype._getStageElements = function () { throw new AbstractMethodError(); }; _Render_RenderableBase.prototype._getRenderMaterial = function () { throw new AbstractMethodError(); }; _Render_RenderableBase.prototype._getStyle = function () { throw new AbstractMethodError(); }; /** * //TODO * * @private */ _Render_RenderableBase.prototype._updateElements = function () { this._stageElements = this._getStageElements(); this._elementsDirty = false; }; _Render_RenderableBase.prototype._updateMaterial = function () { var renderMaterial = this._getRenderMaterial(); if (this._renderMaterial != renderMaterial) { if (this._renderMaterial) this._renderMaterial.removeOwner(this); this._renderMaterial = renderMaterial; this._renderMaterial.addOwner(this); } this._materialDirty = false; }; _Render_RenderableBase.prototype._updateStyle = function () { var style = this._getStyle(); if (this._materialDirty) this._updateMaterial(); //create a cache of image & sampler objects for the renderable var numImages = this._renderMaterial.numImages; var material = this._renderMaterial.material; this._images.length = numImages; this._samplers.length = numImages; var uvMatrix = style ? style.uvMatrix : material.style ? material.style.uvMatrix : null; if (uvMatrix) this._uvMatrix = this.stageElements.transformMatrix(this, uvMatrix); var numTextures = this._renderMaterial.material.getNumTextures(); var texture; var index; for (var i = 0; i < numTextures; i++) { texture = material.getTextureAt(i); numImages = texture.getNumImages(); for (var j = 0; j < numImages; j++) { index = this._renderMaterial.getImageIndex(texture, j); this._images[index] = this._stage.abstractions .getAbstraction((style === null || style === void 0 ? void 0 : style.getImageAt(texture, j)) || this._renderMaterial.images[index]); this._samplers[index] = (style === null || style === void 0 ? void 0 : style.getSamplerAt(texture, j)) || this._renderMaterial.samplers[index]; } } this._styleDirty = false; }; _Render_RenderableBase.prototype.getDefaultMaterial = function () { return MaterialUtils.getDefaultTextureMaterial(); }; return _Render_RenderableBase; }(AbstractionBase)); export { _Render_RenderableBase };