@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
94 lines (93 loc) • 3.5 kB
JavaScript
"use strict";
import { TypedMatNode } from "../_Base";
import { BaseTextureMapController, BooleanParamOptions, NodePathOptions } from "./_BaseTextureController";
import { NodeParamsConfig, ParamConfig } from "../../utils/params/ParamsConfig";
import { Euler, Vector3 } from "three";
import { TypedNodePathParamValue } from "../../../../core/Walker";
import { degToRad, radToDeg } from "three/src/math/MathUtils";
export const ENV_MAP_OPERATION_DEFAULT_PARAMS = {
useEnvMap: false,
envMap: new TypedNodePathParamValue(""),
envMapIntensity: 1,
envMapRotation: new Vector3(0, 0, 0)
};
const _rotationInDegrees = new Vector3();
const _euler = new Euler();
const DEFAULT_PARAMS = ENV_MAP_OPERATION_DEFAULT_PARAMS;
export function EnvMapParamConfig(Base) {
return class Mixin extends Base {
constructor() {
super(...arguments);
/** @param toggle if you want to use an environment map */
this.useEnvMap = ParamConfig.BOOLEAN(DEFAULT_PARAMS.useEnvMap, {
separatorBefore: true,
...BooleanParamOptions(TextureEnvMapController)
});
/** @param specify the environment map COP node */
this.envMap = ParamConfig.NODE_PATH("", NodePathOptions(TextureEnvMapController, "useEnvMap"));
/** @param environment intensity */
this.envMapIntensity = ParamConfig.FLOAT(DEFAULT_PARAMS.envMapIntensity, { visibleIf: { useEnvMap: 1 } });
/** @param environment rotation */
this.envMapRotation = ParamConfig.VECTOR3(DEFAULT_PARAMS.envMapRotation.toArray(), {
visibleIf: { useEnvMap: 1 }
});
}
};
}
export function isValidEnvMapMaterial(material) {
if (!material) {
return false;
}
return material.isMeshStandardMaterial || material.isMeshPhysicalMaterial;
}
class TextureEnvMapParamsConfig extends EnvMapParamConfig(NodeParamsConfig) {
}
class TextureEnvMapMatNode extends TypedMatNode {
async material() {
const container = await this.compute();
return container.material();
}
}
export class TextureEnvMapController extends BaseTextureMapController {
constructor(node) {
super(node);
this.node = node;
}
initializeNode() {
this.add_hooks(this.node.p.useEnvMap, this.node.p.envMap);
}
static async update(node) {
node.controllers.envMap.update();
}
async update() {
const material = await this.node.material();
if (!isValidEnvMapMaterial(material)) {
return;
}
this.updateMaterial(material);
}
async updateMaterial(material) {
await this._update(material, "envMap", this.node.p.useEnvMap, this.node.p.envMap);
material.envMapIntensity = this.node.pv.envMapIntensity;
_rotationInDegrees.copy(this.node.pv.envMapRotation);
const x = degToRad(_rotationInDegrees.x);
const y = degToRad(_rotationInDegrees.y);
const z = degToRad(_rotationInDegrees.z);
_euler.set(x, y, z);
material.envMapRotation.copy(_euler);
}
getTextures(material, record) {
record.set("envMap", material.envMap);
}
setParamsFromMaterial(material, record) {
const mapNode = record.get("envMap");
this.node.p.useEnvMap.set(mapNode != null);
if (mapNode) {
this.node.p.envMap.setNode(mapNode, { relative: true });
}
this.node.p.envMapIntensity.set(material.envMapIntensity);
this.node.p.envMapRotation.x.set(radToDeg(material.envMapRotation.x));
this.node.p.envMapRotation.y.set(radToDeg(material.envMapRotation.y));
this.node.p.envMapRotation.z.set(radToDeg(material.envMapRotation.z));
}
}