UNPKG

@polygonjs/polygonjs

Version:

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

136 lines (135 loc) 5.36 kB
"use strict"; import { TypedSopNode } from "./_Base"; import { DEFAULT_PARAMS, DEFAULT_OCEAN_PARAMS } from "../../operations/sop/OceanPlane"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { Poly } from "../../Poly"; import { isBooleanTrue } from "../../../core/Type"; import { Water } from "../../../modules/core/objects/Water"; import { replaceChild } from "../../poly/PolyOnObjectsAddRemoveHooksController"; const DEFAULT = DEFAULT_PARAMS; class OceanPlaneSopParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); this.main = ParamConfig.FOLDER(); /** @param reflection direction */ this.direction = ParamConfig.VECTOR3(DEFAULT.direction.toArray()); /** @param sun direction */ this.sunDirection = ParamConfig.VECTOR3(DEFAULT.sunDirection.toArray()); /** @param sun color */ this.sunColor = ParamConfig.COLOR(DEFAULT.sunColor.toArray()); /** @param water color */ this.waterColor = ParamConfig.COLOR(DEFAULT.waterColor.toArray()); /** @param reflection color */ this.reflectionColor = ParamConfig.COLOR(DEFAULT.reflectionColor.toArray()); /** @param reflection fresnel */ this.reflectionFresnel = ParamConfig.FLOAT(DEFAULT.reflectionFresnel); /** @param waves Height */ this.wavesHeight = ParamConfig.FLOAT(DEFAULT.wavesHeight, { range: [0, 10], rangeLocked: [false, false] }); /** @param distortion scale */ this.distortionScale = ParamConfig.FLOAT(DEFAULT.distortionScale, { range: [0, 1], rangeLocked: [true, false] }); /** @param distortion speed */ this.timeScale = ParamConfig.FLOAT(DEFAULT.timeScale, { range: [0, 2], rangeLocked: [true, false] }); /** @param size */ this.size = ParamConfig.FLOAT(DEFAULT.size, { range: [0, 100], rangeLocked: [true, false] }); this.advanced = ParamConfig.FOLDER(); /** @param render reflection */ this.renderReflection = ParamConfig.BOOLEAN(DEFAULT.renderReflection); /** @param normal Bias - adjusts this if the reflections are too grainy */ this.normalBias = ParamConfig.FLOAT(DEFAULT.normalBias, { range: [0, 0.1], rangeLocked: [false, false] }); /** @param multisamples */ this.multisamples = ParamConfig.INTEGER(DEFAULT.multisamples, { range: [0, 4], rangeLocked: [true, false] }); /** @param reacts to fog */ this.useFog = ParamConfig.BOOLEAN(DEFAULT.useFog); } } const ParamsConfig = new OceanPlaneSopParamsConfig(); export class OceanPlaneSopNode extends TypedSopNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return "oceanPlane"; } initializeNode() { this.io.inputs.setCount(1); } cook(inputCoreGroups) { const coreGroup = inputCoreGroups[0]; const objects = coreGroup.threejsObjectsWithGeo(); for (const object of objects) { Poly.onObjectsAddRemoveHooks.assignOnAddHookHandler(object, this); } this.setCoreGroup(coreGroup); } updateObjectOnAdd(object, parent) { const _geometry = object.geometry; if (!_geometry) { return; } const clonedGeometry = _geometry.clone(); const scene = this.scene(); const renderer = scene.renderersRegister.lastRegisteredRenderer(); Water.rotateGeometry(clonedGeometry, this.pv.direction); const waterOptions = { polyScene: this.scene(), scene: scene.threejsScene(), renderer, ...DEFAULT_OCEAN_PARAMS, direction: this.pv.direction, sunDirection: this.pv.sunDirection, sunColor: this.pv.sunColor, wavesHeight: this.pv.wavesHeight, waterColor: this.pv.waterColor, reflectionColor: this.pv.reflectionColor, reflectionFresnel: this.pv.reflectionFresnel, distortionScale: this.pv.distortionScale, timeScale: this.pv.timeScale, size: this.pv.size, // renderReflection: params.renderReflection, normalBias: this.pv.normalBias, multisamples: this.pv.multisamples, useFog: this.pv.useFog }; const water = new Water(clonedGeometry, waterOptions); water.matrixAutoUpdate = false; object.matrix.decompose(object.position, object.quaternion, object.scale); water.position.copy(object.position); water.rotation.copy(object.rotation); water.scale.copy(object.scale); water.updateMatrix(); Water.compensateGeometryRotation(water, this.pv.direction); const material = water.material; material.uniforms.direction.value.copy(this.pv.direction); material.uniforms.sunDirection.value.copy(this.pv.sunDirection); material.uniforms.sunColor.value.copy(this.pv.sunColor); material.uniforms.wavesHeight.value = this.pv.wavesHeight; material.uniforms.waterColor.value.copy(this.pv.waterColor); material.uniforms.reflectionColor.value.copy(this.pv.reflectionColor); material.uniforms.reflectionFresnel.value = this.pv.reflectionFresnel; material.uniforms.distortionScale.value = this.pv.distortionScale; material.uniforms.timeScale.value = this.pv.timeScale; material.uniforms.size.value = this.pv.size; material.uniforms.normalBias.value = this.pv.normalBias; water.setReflectionActive(isBooleanTrue(this.pv.renderReflection)); replaceChild(parent, object, water); } }