UNPKG

@awayjs/renderer

Version:
251 lines (250 loc) 9.46 kB
import { __extends } from "tslib"; import { AbstractMethodError, AbstractionBase, } from '@awayjs/core'; import { RenderableEvent } from '../events/RenderableEvent'; import { MaterialUtils } from '../utils/MaterialUtils'; import { Settings } from '../Settings'; /** * @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 = new Array(); _this._samplers = new Array(); _this._count = 0; _this._offset = 0; /** * */ _this._maskId = -1; _this._onInvalidateElementsDelegate = function (event) { return _this._onInvalidateElements(event); }; _this._onInvalidateMaterialDelegate = function (event) { return _this._onInvalidateMaterial(event); }; _this._onInvalidateStyleDelegate = function (event) { return _this._onInvalidateStyle(event); }; 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, "maskId", { get: function () { return this._maskId; }, set: function (v) { if (v !== this._maskId) { this._updateMaskHack(v >= 0); } this._maskId = v; }, 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._useWeak ? this._pool.deref() : this._pool; }, 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.stage; entity.addRenderable(this); this._asset.addEventListener(RenderableEvent.INVALIDATE_ELEMENTS, this._onInvalidateElementsDelegate); this._asset.addEventListener(RenderableEvent.INVALIDATE_MATERIAL, this._onInvalidateMaterialDelegate); this._asset.addEventListener(RenderableEvent.INVALIDATE_STYLE, this._onInvalidateStyleDelegate); }; _Render_RenderableBase.prototype._updateMaskHack = function (enable) { var _a; if (!Settings.USE_ALPHA_CUTOFF || Settings.ALPHA_CUTOFF_VALUE < 0 || Settings.ALPHA_CUTOFF_VALUE > 1) { return; } var mat = (_a = this.renderMaterial) === null || _a === void 0 ? void 0 : _a.material; if (!mat) { return; } mat.alphaThreshold = enable ? Settings.ALPHA_CUTOFF_VALUE : 0; }; /** * Renders an object to the current render target. * * @private */ _Render_RenderableBase.prototype.executeRender = function (enableDepthAndStencil, surfaceSelector, mipmapSelector, maskConfig) { if (enableDepthAndStencil === void 0) { enableDepthAndStencil = true; } if (surfaceSelector === void 0) { surfaceSelector = 0; } if (mipmapSelector === void 0) { mipmapSelector = 0; } if (maskConfig === void 0) { maskConfig = 0; } 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); } this._stageElements.draw(this, shader, this._count, this._offset); }; _Render_RenderableBase.prototype.onClear = function (event) { var _a; this._asset.removeEventListener(RenderableEvent.INVALIDATE_ELEMENTS, this._onInvalidateElementsDelegate); this._asset.removeEventListener(RenderableEvent.INVALIDATE_MATERIAL, this._onInvalidateMaterialDelegate); this._asset.removeEventListener(RenderableEvent.INVALIDATE_STYLE, this._onInvalidateStyleDelegate); (_a = this.entity) === null || _a === void 0 ? void 0 : _a.removeRenderable(this); this.renderSceneTransform = null; this._stage = null; this.next = null; this.maskOwners = null; this._renderMaterial.removeOwner(this); this._renderMaterial = null; this._stageElements = null; this._materialDirty = true; this._elementsDirty = true; this._styleDirty = true; _super.prototype.onClear.call(this, event); }; _Render_RenderableBase.prototype._onInvalidateElements = function (event) { if (event === void 0) { event = null; } this._elementsDirty = true; }; _Render_RenderableBase.prototype._onInvalidateMaterial = function (event) { if (event === void 0) { event = null; } this._materialDirty = true; this._styleDirty = true; }; _Render_RenderableBase.prototype._onInvalidateStyle = function (event) { if (event === void 0) { event = null; } 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; this._uvMatrix = style ? style.uvMatrix : material.style ? material.style.uvMatrix : null; var numTextures = this._renderMaterial.material.getNumTextures(); var texture; var image; 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); image = style ? style.getImageAt(texture, j) : null; this._images[index] = image ? image.getAbstraction(this._stage) : null; this._samplers[index] = style ? style.getSamplerAt(texture, j) : null; } } this._styleDirty = false; }; _Render_RenderableBase.prototype.getDefaultMaterial = function () { return MaterialUtils.getDefaultTextureMaterial(); }; return _Render_RenderableBase; }(AbstractionBase)); export { _Render_RenderableBase };