UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

60 lines (58 loc) 2.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebGLCubeMaps = WebGLCubeMaps; var _constants = require("../../constants.js"); var _WebGLCubeRenderTarget = require("../WebGLCubeRenderTarget.js"); function WebGLCubeMaps(renderer) { let cubemaps = new WeakMap(); function mapTextureMapping(texture, mapping) { if (mapping === _constants.EquirectangularReflectionMapping) { texture.mapping = _constants.CubeReflectionMapping; } else if (mapping === _constants.EquirectangularRefractionMapping) { texture.mapping = _constants.CubeRefractionMapping; } return texture; } function get(texture) { if (texture && texture.isTexture) { const mapping = texture.mapping; if (mapping === _constants.EquirectangularReflectionMapping || mapping === _constants.EquirectangularRefractionMapping) { if (cubemaps.has(texture)) { const cubemap = cubemaps.get(texture).texture; return mapTextureMapping(cubemap, texture.mapping); } else { const image = texture.image; if (image && image.height > 0) { const renderTarget = new _WebGLCubeRenderTarget.WebGLCubeRenderTarget(image.height); renderTarget.fromEquirectangularTexture(renderer, texture); cubemaps.set(texture, renderTarget); texture.addEventListener('dispose', onTextureDispose); return mapTextureMapping(renderTarget.texture, texture.mapping); } else { // image not yet ready. try the conversion next frame return null; } } } } return texture; } function onTextureDispose(event) { const texture = event.target; texture.removeEventListener('dispose', onTextureDispose); const cubemap = cubemaps.get(texture); if (cubemap !== undefined) { cubemaps.delete(texture); cubemap.dispose(); } } function dispose() { cubemaps = new WeakMap(); } return { get: get, dispose: dispose }; }