UNPKG

@awayjs/scene

Version:
289 lines (288 loc) 10.8 kB
import { __extends } from "tslib"; import { AssetEvent } from '@awayjs/core'; import { BlendMode, ImageCube } from '@awayjs/stage'; import { BoundingVolumeType, ContainerNode } from '@awayjs/view'; import { MaterialEvent, Style, ImageTextureCube, DefaultRenderer } from '@awayjs/renderer'; import { DisplayObjectContainer } from './DisplayObjectContainer'; /** * A Skybox class is used to render a sky in the scene. It's always considered static and 'at infinity', and as * such it's always centered at the camera's position and sized to exactly fit within the camera's frustum, ensuring * the sky box is always as large as possible without being clipped. */ var Skybox = /** @class */ (function (_super) { __extends(Skybox, _super); function Skybox(imageColor, alpha) { if (imageColor === void 0) { imageColor = 0xFFFFFF; } if (alpha === void 0) { alpha = 1; } var _this = _super.call(this) || this; _this._textures = new Array(); _this._blendMode = BlendMode.NORMAL; _this.animateUVs = false; _this.bothSides = false; _this.curves = false; _this.imageRect = false; _this.useColorTransform = false; _this.alphaThreshold = 0; _this.alphaBlending = true; _this._onTextureInvalidateDelegate = function (event) { return _this.onTextureInvalidate(event); }; _this.style = new Style(); if (imageColor instanceof ImageCube) { _this._style.image = imageColor; _this.texture = new ImageTextureCube(); } else { _this._style.color = Number(imageColor); } return _this; } Object.defineProperty(Skybox.prototype, "animationSet", { /** * */ get: function () { return this._animationSet; }, enumerable: false, configurable: true }); Object.defineProperty(Skybox.prototype, "blendMode", { /** * The blend mode to use when drawing this renderable. The following blend modes are supported: * <ul> * <li>BlendMode.NORMAL: No blending, unless the material inherently needs it</li> * <li>BlendMode.LAYER: Force blending. * This will draw the object the same as NORMAL, but without writing depth writes.</li> * <li>BlendMode.MULTIPLY</li> * <li>BlendMode.ADD</li> * <li>BlendMode.ALPHA</li> * </ul> */ get: function () { return this._blendMode; }, set: function (value) { if (this._blendMode == value) return; this._blendMode = value; this.invalidate(); }, enumerable: false, configurable: true }); Object.defineProperty(Skybox.prototype, "texture", { /** * The cube texture to use as the skybox. */ get: function () { return this._texture; }, set: function (value) { if (this._texture == value) return; if (this._texture) this.removeTexture(this._texture); this._texture = value; if (this._texture) this.addTexture(this._texture); this.invalidatePasses(); }, enumerable: false, configurable: true }); Skybox.prototype.getNumTextures = function () { return this._textures.length; }; Skybox.prototype.getTextureAt = function (index) { return this._textures[index]; }; Skybox.prototype.getEntity = function () { return this; }; Object.defineProperty(Skybox.prototype, "assetType", { get: function () { return Skybox.assetType; }, enumerable: false, configurable: true }); /** * Marks the shader programs for all passes as invalid, so they will be recompiled before the next use. * * @private */ Skybox.prototype.invalidatePasses = function () { this.dispatchEvent(new MaterialEvent(MaterialEvent.INVALIDATE_PASSES, this)); }; Skybox.prototype.addTexture = function (texture) { this._textures.push(texture); texture.addEventListener(AssetEvent.INVALIDATE, this._onTextureInvalidateDelegate); this.onTextureInvalidate(); }; Skybox.prototype.removeTexture = function (texture) { this._textures.splice(this._textures.indexOf(texture), 1); texture.removeEventListener(AssetEvent.INVALIDATE, this._onTextureInvalidateDelegate); this.onTextureInvalidate(); }; Skybox.prototype.onTextureInvalidate = function (event) { if (event === void 0) { event = null; } this.invalidate(); }; Skybox.prototype._onInvalidateProperties = function (event) { this._invalidateMaterial(); this.invalidatePasses(); }; Skybox.prototype._acceptTraverser = function (traverser) { traverser.applyTraversable(this); }; Skybox.prototype.iAddOwner = function (owner) { }; /** * Removes an IEntity as owner. * @param owner * * @internal */ Skybox.prototype.iRemoveOwner = function (owner) { }; Skybox.prototype._getDefaultBoundingVolume = function () { return BoundingVolumeType.NULL; }; Skybox.prototype.testCollision = function (collision, closestFlag) { collision.pickable = null; return false; }; Skybox.assetType = '[asset Skybox]'; return Skybox; }(DisplayObjectContainer)); export { Skybox }; import { _Render_RenderableBase, ShaderBase, RenderEntity } from '@awayjs/renderer'; import { ContextGLCompareMode, AttributesBuffer } from '@awayjs/stage'; import { _Render_MaterialPassBase } from '@awayjs/renderer'; import { SkyboxElements } from '../elements/SkyboxElements'; /** * _Render_SkyboxMaterial forms an abstract base class for the default shaded materials provided by Stage, * using material methods to define their appearance. */ var _Render_SkyboxMaterial = /** @class */ (function (_super) { __extends(_Render_SkyboxMaterial, _super); function _Render_SkyboxMaterial() { return _super !== null && _super.apply(this, arguments) || this; } _Render_SkyboxMaterial.prototype.init = function (skybox, renderElements) { _super.prototype.init.call(this, skybox, renderElements); this._shader = new ShaderBase(renderElements, this, this, this._stage); this._texture = this._shader.abstractions.getAbstraction(this.material.texture); this._pAddPass(this); }; _Render_SkyboxMaterial.prototype.onClear = function () { _super.prototype.onClear.call(this); this._texture.onClear(); this._texture = null; }; /** * @inheritDoc */ _Render_SkyboxMaterial.prototype._pUpdateRender = function () { _super.prototype._pUpdateRender.call(this); var material = this.material; this.requiresBlending = (material.blendMode != BlendMode.NORMAL); this.shader.setBlendMode((material.blendMode == BlendMode.NORMAL && this.requiresBlending) ? BlendMode.LAYER : material.blendMode); }; _Render_SkyboxMaterial.prototype._includeDependencies = function (shader) { _super.prototype._includeDependencies.call(this, shader); shader.usesPositionFragment = true; }; /** * @inheritDoc */ _Render_SkyboxMaterial.prototype._getFragmentCode = function (registerCache, sharedRegisters) { return this._texture._getFragmentCode(sharedRegisters.shadedTarget, registerCache, sharedRegisters, sharedRegisters.positionVarying); }; _Render_SkyboxMaterial.prototype._setRenderState = function (renderable) { _super.prototype._setRenderState.call(this, renderable); this._texture._setRenderState(renderable); }; /** * @inheritDoc */ _Render_SkyboxMaterial.prototype._activate = function () { _super.prototype._activate.call(this); this._stage.context.setDepthTest(false, ContextGLCompareMode.LESS); this._texture.activate(); }; return _Render_SkyboxMaterial; }(_Render_MaterialPassBase)); export { _Render_SkyboxMaterial }; /** * @class away.pool._Render_Skybox */ var _Render_Skybox = /** @class */ (function (_super) { __extends(_Render_Skybox, _super); function _Render_Skybox() { return _super !== null && _super.apply(this, arguments) || this; } /** * //TODO * * @returns {away.base.TriangleElements} * @private */ _Render_Skybox.prototype._getStageElements = function () { var elements = _Render_Skybox._elements; if (!elements) { elements = new SkyboxElements(new AttributesBuffer(11, 4)); elements.autoDeriveNormals = false; elements.autoDeriveTangents = false; elements.setIndices(Array(0, 1, 2, 2, 3, 0, 6, 5, 4, 4, 7, 6, 2, 6, 7, 7, 3, 2, 4, 5, 1, 1, 0, 4, 4, 0, 3, 3, 7, 4, 2, 1, 5, 5, 6, 2)); elements.setPositions(Array(-1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1)); } return this._stage.abstractions.getAbstraction(elements); }; _Render_Skybox.prototype._getRenderMaterial = function () { return this.entity.renderer .getRenderElements(this.stageElements.elements).abstractions .getAbstraction(this.renderable); }; _Render_Skybox.prototype._getStyle = function () { return this.renderable.style; }; return _Render_Skybox; }(_Render_RenderableBase)); export { _Render_Skybox }; // import { CacheRenderer } from '@awayjs/renderer'; /** * SkyboxNode is a space partitioning leaf node that contains a Skybox object. * * @class away.partition.SkyboxNode */ var SkyboxNode = /** @class */ (function (_super) { __extends(SkyboxNode, _super); function SkyboxNode() { return _super !== null && _super.apply(this, arguments) || this; } /** * * @param planes * @param numPlanes * @returns {boolean} */ SkyboxNode.prototype.isInFrustum = function (rootEntity, planes, numPlanes, pickGroup) { if (this.isInvisible()) return false; //a skybox is always in view unless its visibility is set to false return true; }; /** * * @returns {boolean} */ SkyboxNode.prototype.isCastingShadow = function () { return false; //skybox never casts shadows }; return SkyboxNode; }(ContainerNode)); export { SkyboxNode }; //CacheRenderer.registerMaterial(_Render_SkyboxMaterial, Skybox); DefaultRenderer.registerMaterial(_Render_SkyboxMaterial, Skybox); RenderEntity.registerRenderable(_Render_Skybox, Skybox);