UNPKG

awayjs-display

Version:
282 lines 10.9 kB
"use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var Box_1 = require("awayjs-core/lib/geom/Box"); var AssetBase_1 = require("awayjs-core/lib/library/AssetBase"); var Graphic_1 = require("../graphics/Graphic"); var ElementsEvent_1 = require("../events/ElementsEvent"); var StyleEvent_1 = require("../events/StyleEvent"); var ElementsUtils_1 = require("../utils/ElementsUtils"); /** * * Graphics is a collection of SubGeometries, each of which contain the actual geometrical data such as vertices, * normals, uvs, etc. It also contains a reference to an animation class, which defines how the geometry moves. * A Graphics object is assigned to a Sprite, a scene graph occurence of the geometry, which in turn assigns * the SubGeometries to its respective TriangleGraphic objects. * * * * @see away.core.base.SubGraphics * @see away.entities.Sprite * * @class Graphics */ var Graphics = (function (_super) { __extends(Graphics, _super); /** * Creates a new Graphics object. */ function Graphics() { var _this = this; _super.call(this); this._boxBoundsInvalid = true; this._sphereBoundsInvalid = true; this._graphics = new Array(); this._onInvalidatePropertiesDelegate = function (event) { return _this._onInvalidateProperties(event); }; this._onInvalidateVerticesDelegate = function (event) { return _this._onInvalidateVertices(event); }; } Object.defineProperty(Graphics.prototype, "assetType", { get: function () { return Graphics.assetType; }, enumerable: true, configurable: true }); Object.defineProperty(Graphics.prototype, "count", { get: function () { return this._graphics.length; }, enumerable: true, configurable: true }); Object.defineProperty(Graphics.prototype, "animator", { /** * Defines the animator of the graphics object. Default value is <code>null</code>. */ get: function () { return this._animator; }, set: function (value) { this._animator = value; var len = this._graphics.length; var graphic; for (var i = 0; i < len; ++i) { graphic = this._graphics[i]; // cause material to be unregistered and registered again to work with the new animation type (if possible) if (graphic.material) { graphic.material.iRemoveOwner(graphic); graphic.material.iAddOwner(graphic); } //invalidate any existing graphic objects in case they need to pull new elements graphic.invalidateElements(); } }, enumerable: true, configurable: true }); Object.defineProperty(Graphics.prototype, "style", { /** * */ get: function () { return this._style; }, set: function (value) { if (this._style == value) return; if (this._style) this._style.removeEventListener(StyleEvent_1.default.INVALIDATE_PROPERTIES, this._onInvalidatePropertiesDelegate); this._style = value; if (this._style) this._style.addEventListener(StyleEvent_1.default.INVALIDATE_PROPERTIES, this._onInvalidatePropertiesDelegate); this._iInvalidateSurfaces(); }, enumerable: true, configurable: true }); Object.defineProperty(Graphics.prototype, "material", { /** * The material with which to render the Graphics. */ get: function () { return this._material; }, set: function (value) { if (value == this._material) return; var i; var len = this._graphics.length; var graphic; if (this._material) for (i = 0; i < len; i++) if (!(graphic = this._graphics[i])._iGetExplicitMaterial()) this._material.iRemoveOwner(graphic); this._material = value; if (this._material) for (i = 0; i < len; i++) if (!(graphic = this._graphics[i])._iGetExplicitMaterial()) this._material.iAddOwner(graphic); }, enumerable: true, configurable: true }); /** * Adds a GraphicBase wrapping a Elements. * * @param elements */ Graphics.prototype.addGraphic = function (elements, material, style) { if (material === void 0) { material = null; } if (style === void 0) { style = null; } var graphic; if (Graphic_1.default._available.length) { graphic = Graphic_1.default._available.pop(); graphic._iIndex = this._graphics.length; graphic.parent = this; graphic.elements = elements; graphic.material = material; graphic.style = style; } else { graphic = new Graphic_1.default(this._graphics.length, this, elements, material, style); } this._graphics.push(graphic); elements.addEventListener(ElementsEvent_1.default.INVALIDATE_VERTICES, this._onInvalidateVerticesDelegate); this.invalidate(); return graphic; }; Graphics.prototype.removeGraphic = function (graphic) { this._graphics.splice(this._graphics.indexOf(graphic), 1); graphic.elements.removeEventListener(ElementsEvent_1.default.INVALIDATE_VERTICES, this._onInvalidateVerticesDelegate); graphic.elements = null; graphic.material = null; graphic.style = null; graphic.clear(); this.invalidate(); }; Graphics.prototype.getGraphicAt = function (index) { return this._graphics[index]; }; Graphics.prototype.applyTransformation = function (transform) { var len = this._graphics.length; for (var i = 0; i < len; ++i) this._graphics[i].elements.applyTransformation(transform); }; Graphics.prototype.copyTo = function (graphics) { graphics.material = this._material; graphics.style = this.style; graphics.particles = this.particles; graphics.numParticles = this.numParticles; var graphic; var len = this._graphics.length; for (var i = 0; i < len; ++i) { graphic = this._graphics[i]; graphics.addGraphic(graphic.elements, graphic._iGetExplicitMaterial(), graphic._iGetExplicitStyle()); } if (this._animator) graphics.animator = this._animator.clone(); }; /** * Scales the geometry. * @param scale The amount by which to scale. */ Graphics.prototype.scale = function (scale) { var len = this._graphics.length; for (var i = 0; i < len; ++i) this._graphics[i].elements.scale(scale); }; Graphics.prototype.clear = function () { for (var i = this._graphics.length - 1; i >= 0; i--) this._graphics[i].clear(); }; /** * Clears all resources used by the Graphics object, including SubGeometries. */ Graphics.prototype.dispose = function () { this.material = null; for (var i = this._graphics.length - 1; i >= 0; i--) this._graphics[i].dispose(); if (this._animator) this._animator.dispose(); }; /** * Scales the uv coordinates (tiling) * @param scaleU The amount by which to scale on the u axis. Default is 1; * @param scaleV The amount by which to scale on the v axis. Default is 1; */ Graphics.prototype.scaleUV = function (scaleU, scaleV) { if (scaleU === void 0) { scaleU = 1; } if (scaleV === void 0) { scaleV = 1; } var len = this._graphics.length; for (var i = 0; i < len; ++i) this._graphics[i].elements.scaleUV(scaleU, scaleV); }; Graphics.prototype.getBoxBounds = function () { if (this._boxBoundsInvalid) { this._boxBoundsInvalid = false; if (!this._boxBounds) this._boxBounds = new Box_1.default(); if (this._graphics.length) { this._boxBounds.setBoundIdentity(); var len = this._graphics.length; for (var i = 0; i < len; i++) this._boxBounds = this._graphics[i].elements.getBoxBounds(this._boxBounds); } else { this._boxBounds.setEmpty(); } } return this._boxBounds; }; Graphics.prototype.getSphereBounds = function (center, target) { if (target === void 0) { target = null; } var len = this._graphics.length; for (var i = 0; i < len; i++) target = this._graphics[i].elements.getSphereBounds(center, target); return target; }; Graphics.prototype.invalidate = function () { _super.prototype.invalidate.call(this); this._boxBoundsInvalid = true; this._sphereBoundsInvalid = true; }; Graphics.prototype._iInvalidateSurfaces = function () { var len = this._graphics.length; for (var i = 0; i < len; ++i) this._graphics[i].invalidateSurface(); }; Graphics.prototype.invalidateElements = function () { var len = this._graphics.length; for (var i = 0; i < len; ++i) this._graphics[i].invalidateElements(); }; Graphics.prototype._hitTestPointInternal = function (x, y) { //TODO: handle lines as well var len = this._graphics.length; for (var i = 0; i < len; i++) if (ElementsUtils_1.default.hitTestTriangleElements(x, y, 0, this.getBoxBounds(), this._graphics[i].elements)) return true; return false; }; Graphics.prototype.acceptTraverser = function (traverser) { var len = this._graphics.length; for (var i = 0; i < len; i++) traverser.applyRenderable(this._graphics[i]); }; Graphics.prototype._onInvalidateProperties = function (event) { this._iInvalidateSurfaces(); }; Graphics.prototype._onInvalidateVertices = function (event) { if (event.attributesView != event.target.positions) return; this.invalidate(); }; Graphics.assetType = "[asset Graphics]"; return Graphics; }(AssetBase_1.default)); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = Graphics; //# sourceMappingURL=Graphics.js.map