@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
103 lines (102 loc) • 3.17 kB
JavaScript
;
import { NodeParamsConfig, ParamConfig } from "../../../utils/params/ParamsConfig";
import { isBooleanTrue } from "../../../../../core/BooleanValue";
import { Fog } from "three";
import { FogExp2 } from "three";
const CallbackOptions = {
computeOnDirty: false,
callback: (node) => {
SceneFogController.update(node);
}
};
export var FogType = /* @__PURE__ */ ((FogType2) => {
FogType2["LINEAR"] = "linear";
FogType2["EXPONENTIAL"] = "exponential";
return FogType2;
})(FogType || {});
export const FOG_TYPES = ["linear" /* LINEAR */, "exponential" /* EXPONENTIAL */];
export function SceneFogParamConfig(Base) {
return class Mixin extends Base {
constructor() {
super(...arguments);
/** @param toggle on to use fog */
this.useFog = ParamConfig.BOOLEAN(0, {
...CallbackOptions,
separatorBefore: true
});
/** @param fog type */
this.fogType = ParamConfig.INTEGER(FOG_TYPES.indexOf("exponential" /* EXPONENTIAL */), {
visibleIf: { useFog: 1 },
menu: {
entries: FOG_TYPES.map((mode, i) => {
return { name: mode, value: i };
})
},
...CallbackOptions
});
/** @param fog color */
this.fogColor = ParamConfig.COLOR([1, 1, 1], {
visibleIf: { useFog: 1 },
...CallbackOptions
});
/** @param fog near */
this.fogNear = ParamConfig.FLOAT(1, {
range: [0, 100],
rangeLocked: [true, false],
visibleIf: { useFog: 1, fogType: FOG_TYPES.indexOf("linear" /* LINEAR */) },
...CallbackOptions
});
/** @param fog far */
this.fogFar = ParamConfig.FLOAT(100, {
range: [0, 100],
rangeLocked: [true, false],
visibleIf: { useFog: 1, fogType: FOG_TYPES.indexOf("linear" /* LINEAR */) },
...CallbackOptions
});
/** @param fog density */
this.fogDensity = ParamConfig.FLOAT(25e-5, {
visibleIf: { useFog: 1, fogType: FOG_TYPES.indexOf("exponential" /* EXPONENTIAL */) },
...CallbackOptions
});
}
};
}
class SceneFogParamsConfig extends SceneFogParamConfig(NodeParamsConfig) {
}
export class SceneFogController {
constructor(node) {
this.node = node;
}
async update() {
const scene = this.node.object;
const pv = this.node.pv;
if (isBooleanTrue(pv.useFog)) {
if (pv.fogType == FOG_TYPES.indexOf("linear" /* LINEAR */)) {
const fog = this.fog2(pv);
scene.fog = fog;
fog.color = pv.fogColor;
fog.near = pv.fogNear;
fog.far = pv.fogFar;
} else {
const fogExp2 = this.fogExp2(pv);
scene.fog = this.fogExp2(pv);
fogExp2.color = pv.fogColor;
fogExp2.density = pv.fogDensity;
}
} else {
const current_fog = scene.fog;
if (current_fog) {
scene.fog = null;
}
}
}
fog2(pv) {
return this._fog = this._fog || new Fog(16777215, pv.fogNear, pv.fogFar);
}
fogExp2(pv) {
return this._fogExp2 = this._fogExp2 || new FogExp2(16777215, pv.fogDensity);
}
static async update(node) {
node.sceneFogController.update();
}
}