UNPKG

@polygonjs/polygonjs

Version:

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

69 lines (68 loc) 2.26 kB
"use strict"; import { BaseController } from "./_BaseController"; import { TypedMatNode } from "../_Base"; import { NodeParamsConfig, ParamConfig } from "../../utils/params/ParamsConfig"; import { isBooleanTrue } from "../../../../core/BooleanValue"; export function UniformsTransparencyParamConfig(Base) { return class Mixin extends Base { constructor() { super(...arguments); /** @param sets the material to transparent */ this.transparent = ParamConfig.BOOLEAN(0); /** @param sets the material opacity */ this.opacity = ParamConfig.FLOAT(1); /** @param sets the min alpha below which the material is invisible */ this.alphaTest = ParamConfig.FLOAT(0); } }; } class TransparencyParamsConfig extends UniformsTransparencyParamConfig(NodeParamsConfig) { } class TransparencyMatNode extends TypedMatNode { async material() { const container = await this.compute(); return container.material(); } } export class UniformsTransparencyController extends BaseController { constructor(node) { super(node); this.node = node; } static async update(node) { const material = await node.material(); if (!material) { return; } node.controllers.uniformTransparency.updateMaterial(material); } updateMaterial(material) { const pv = this.node.pv; this._updateTransparency(material, pv); } _updateTransparency(mat, pv) { mat.transparent = isBooleanTrue(pv.transparent); this._updateCommon(mat, pv); } _updateCommon(mat, pv) { const shaderMaterial = mat; if (shaderMaterial.uniforms && shaderMaterial.uniforms.opacity) { shaderMaterial.uniforms.opacity.value = pv.opacity; } mat.opacity = pv.opacity; if (shaderMaterial.uniforms && shaderMaterial.uniforms.alphaTest) { shaderMaterial.uniforms.alphaTest.value = pv.alphaTest; } mat.alphaTest = pv.alphaTest; const customMaterials = mat.customMaterials; if (customMaterials) { const customNames = Object.keys(customMaterials); for (const customName of customNames) { const customMaterial = customMaterials[customName]; if (customMaterial) { this._updateCommon(customMaterial, pv); } } } } }