UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

232 lines (209 loc) 6.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rtt = exports.default = exports.convertToTexture = void 0; var _TSLCore = require("../tsl/TSLCore.js"); var _TextureNode = _interopRequireDefault(require("../accessors/TextureNode.js")); var _constants = require("../core/constants.js"); var _UV = require("../accessors/UV.js"); var _NodeMaterial = _interopRequireDefault(require("../../materials/nodes/NodeMaterial.js")); var _QuadMesh = _interopRequireDefault(require("../../renderers/common/QuadMesh.js")); var _RenderTarget = require("../../core/RenderTarget.js"); var _Vector = require("../../math/Vector2.js"); var _constants2 = require("../../constants.js"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const _size = /*@__PURE__*/new _Vector.Vector2(); /** * `RTTNode` takes another node and uses it with a `QuadMesh` to render into a texture (RTT). * This module is especially relevant in context of post processing where certain nodes require * texture input for their effects. With the helper function `convertToTexture()` which is based * on this module, the node system can automatically ensure texture input if required. * * @augments TextureNode */ class RTTNode extends _TextureNode.default { static get type() { return 'RTTNode'; } /** * Constructs a new RTT node. * * @param {Node} node - The node to render a texture with. * @param {?number} [width=null] - The width of the internal render target. If not width is applied, the render target is automatically resized. * @param {?number} [height=null] - The height of the internal render target. * @param {Object} [options={type:HalfFloatType}] - The options for the internal render target. */ constructor(node, width = null, height = null, options = { type: _constants2.HalfFloatType }) { const renderTarget = new _RenderTarget.RenderTarget(width, height, options); super(renderTarget.texture, (0, _UV.uv)()); /** * The node to render a texture with. * * @type {Node} */ this.node = node; /** * The width of the internal render target. * If not width is applied, the render target is automatically resized. * * @type {?number} * @default null */ this.width = width; /** * The height of the internal render target. * * @type {?number} * @default null */ this.height = height; /** * The pixel ratio * * @type {number} * @default 1 */ this.pixelRatio = 1; /** * The render target * * @type {RenderTarget} */ this.renderTarget = renderTarget; /** * Whether the texture requires an update or not. * * @type {boolean} * @default true */ this.textureNeedsUpdate = true; /** * Whether the texture should automatically be updated or not. * * @type {boolean} * @default true */ this.autoUpdate = true; /** * The node which is used with the quad mesh for RTT. * * @private * @type {Node} * @default null */ this._rttNode = null; /** * The internal quad mesh for RTT. * * @private * @type {QuadMesh} */ this._quadMesh = new _QuadMesh.default(new _NodeMaterial.default()); /** * The `updateBeforeType` is set to `NodeUpdateType.RENDER` since the node updates * the texture once per render in its {@link RTTNode#updateBefore} method. * * @type {string} * @default 'render' */ this.updateBeforeType = _constants.NodeUpdateType.RENDER; } /** * Whether the internal render target should automatically be resized or not. * * @type {boolean} * @readonly * @default true */ get autoSize() { return this.width === null; } setup(builder) { this._rttNode = this.node.context(builder.getSharedContext()); this._quadMesh.material.name = 'RTT'; this._quadMesh.material.needsUpdate = true; return super.setup(builder); } /** * Sets the size of the internal render target * * @param {number} width - The width to set. * @param {number} height - The width to set. */ setSize(width, height) { this.width = width; this.height = height; const effectiveWidth = width * this.pixelRatio; const effectiveHeight = height * this.pixelRatio; this.renderTarget.setSize(effectiveWidth, effectiveHeight); this.textureNeedsUpdate = true; } /** * Sets the pixel ratio. This will also resize the render target. * * @param {number} pixelRatio - The pixel ratio to set. */ setPixelRatio(pixelRatio) { this.pixelRatio = pixelRatio; this.setSize(this.width, this.height); } updateBefore({ renderer }) { if (this.textureNeedsUpdate === false && this.autoUpdate === false) return; this.textureNeedsUpdate = false; // if (this.autoSize === true) { this.pixelRatio = renderer.getPixelRatio(); const size = renderer.getSize(_size); this.setSize(size.width, size.height); } // this._quadMesh.material.fragmentNode = this._rttNode; // const currentRenderTarget = renderer.getRenderTarget(); renderer.setRenderTarget(this.renderTarget); this._quadMesh.render(renderer); renderer.setRenderTarget(currentRenderTarget); } clone() { const newNode = new _TextureNode.default(this.value, this.uvNode, this.levelNode); newNode.sampler = this.sampler; newNode.referenceNode = this; return newNode; } } var _default = exports.default = RTTNode; /** * TSL function for creating a RTT node. * * @tsl * @function * @param {Node} node - The node to render a texture with. * @param {?number} [width=null] - The width of the internal render target. If not width is applied, the render target is automatically resized. * @param {?number} [height=null] - The height of the internal render target. * @param {Object} [options={type:HalfFloatType}] - The options for the internal render target. * @returns {RTTNode} */ const rtt = (node, ...params) => (0, _TSLCore.nodeObject)(new RTTNode((0, _TSLCore.nodeObject)(node), ...params)); /** * TSL function for converting nodes to textures nodes. * * @tsl * @function * @param {Node} node - The node to render a texture with. * @param {?number} [width=null] - The width of the internal render target. If not width is applied, the render target is automatically resized. * @param {?number} [height=null] - The height of the internal render target. * @param {Object} [options={type:HalfFloatType}] - The options for the internal render target. * @returns {RTTNode} */ exports.rtt = rtt; const convertToTexture = (node, ...params) => { if (node.isTextureNode) return node; if (node.isPassNode) return node.getTextureNode(); return rtt(node, ...params); }; exports.convertToTexture = convertToTexture;