UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

133 lines (132 loc) 4.21 kB
"use strict"; import { ParamEvent } from "./../../../../poly/ParamEvent"; import { TypedCopNode } from "./../../_Base"; import { NodeParamsConfig, ParamConfig } from "../../../utils/params/ParamsConfig"; import { TextureParamsController, TextureParamConfig } from "./../../utils/TextureParamsController"; import { isUrlStaticImage } from "../../../../../core/FileTypeController"; import { InputCloneMode } from "../../../../poly/InputCloneMode"; import { isBooleanTrue } from "../../../../../core/BooleanValue"; import { FileTypeCheckCopParamConfig } from "./../../utils/CheckFileType"; import { Poly } from "../../../../Poly"; function imageCopParamConfig(Base) { return class Mixin extends Base { constructor() { super(...arguments); /** @param url to fetch the image from */ this.url = ParamConfig.STRING(""); /** @param reload the image */ this.reload = ParamConfig.BUTTON(null); } }; } class BaseImageCopParamsConfig extends FileTypeCheckCopParamConfig( TextureParamConfig(imageCopParamConfig(NodeParamsConfig)) ) { } const ParamsConfig = new BaseImageCopParamsConfig(); export class copImageNodeFactoryFactoryResult extends TypedCopNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; this.textureParamsController = new TextureParamsController(this); } } export function copImageNodeFactoryFactory(options) { function imageCopParamConfig2(Base) { return class Mixin extends Base { constructor() { super(...arguments); /** @param url to fetch the image from */ this.url = ParamConfig.STRING(options.defaultUrl, { fileBrowse: { extensions: options.extensions } }); /** @param reload the image */ this.reload = ParamConfig.BUTTON(null, { callback: (node) => { BaseImageCopNode.PARAM_CALLBACK_reload(node); } }); } }; } class BaseImageCopParamsConfig2 extends FileTypeCheckCopParamConfig( TextureParamConfig(imageCopParamConfig2(NodeParamsConfig)) ) { } const ParamsConfig2 = new BaseImageCopParamsConfig2(); class BaseImageCopNode extends TypedCopNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig2; this.textureParamsController = new TextureParamsController(this); } static type() { return options.type; } dispose() { super.dispose(); Poly.blobs.clearBlobsForNode(this); } initializeNode() { this.io.inputs.setCount(0, 1); this.io.inputs.initInputsClonedState(InputCloneMode.NEVER); } async cook(inputContents) { if (isBooleanTrue(this.pv.checkFileType) && !isUrlStaticImage(this.pv.url)) { this.states.error.set("url is not an image"); } else { const texture = await this._loadTexture(); if (texture) { const inputTexture = inputContents[0]; if (inputTexture) { TextureParamsController.copyTextureAttributes(texture, inputTexture); } await this.textureParamsController.update(texture); this.setTexture(texture); } else { this._clearTexture(); } } } // // // UTILS // // static PARAM_CALLBACK_reload(node) { node.paramCallbackReload(); } paramCallbackReload() { this.p.url.setDirty(); this.p.url.emit(ParamEvent.ASSET_RELOAD_REQUEST); } _loader() { return options.getLoader(this.pv.url, this); } async _loadTexture() { let texture = null; try { const loader = this._loader(); texture = await loader.loadImage({ tdataType: this.pv.ttype && this.pv.tadvanced, dataType: this.pv.type }); if (texture) { texture.matrixAutoUpdate = false; } } catch (e) { } if (!texture) { if (!this.states.error.active()) { this.states.error.set(`could not load texture '${this.pv.url}'`); } } return texture; } // clearLoadedBlob() { // const loader = this._loader(); // loader.deregisterUrl(); // } } return BaseImageCopNode; }