UNPKG

@polygonjs/polygonjs

Version:

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

208 lines (207 loc) 5.96 kB
"use strict"; import { UNIFORM_PARAM_PREFIX, UNIFORM_TEXTURE_PREFIX } from "../../core/material/uniform"; import { isArray } from "../../core/Type"; import { MaterialUserDataUniforms } from "../nodes/gl/code/assemblers/materials/OnBeforeCompile"; import { NamedFunction2, NamedFunction3, NamedFunction5, NamedFunction6, ObjectNamedFunction1, ObjectNamedFunction2 } from "./_Base"; function _setMaterialColor(material, targetColor, lerp) { const color = material.color; if (!color) { return; } if (lerp >= 1) { color.copy(targetColor); } else { color.lerp(targetColor, lerp); } } function _setMaterialEmissiveColor(material, targetColor, lerp) { const color = material.emissive; if (!color) { return; } if (lerp >= 1) { color.copy(targetColor); } else { color.lerp(targetColor, lerp); } } function _addParamUniformNamePrefix(uniformName, addPrefix) { return addPrefix ? `${UNIFORM_PARAM_PREFIX}${uniformName}` : uniformName; } function _addTextureUniformNamePrefix(uniformName, addPrefix) { return addPrefix ? `${UNIFORM_TEXTURE_PREFIX}${uniformName}` : uniformName; } export class setObjectMaterial extends ObjectNamedFunction1 { static type() { return "setObjectMaterial"; } func(object3D, material) { object3D.material = material; } } export class setObjectMaterialColor extends ObjectNamedFunction2 { static type() { return "setObjectMaterialColor"; } func(object3D, color, lerp) { const material = object3D.material; if (isArray(material)) { for (let mat of material) { _setMaterialColor(mat, color, lerp); } } else { _setMaterialColor(material, color, lerp); } } } export class setMaterialColor extends NamedFunction3 { static type() { return "setMaterialColor"; } func(material, color, lerp) { _setMaterialColor(material, color, lerp); } } export class setMaterialEmissiveColor extends NamedFunction3 { static type() { return "setMaterialEmissiveColor"; } func(material, color, lerp) { _setMaterialEmissiveColor(material, color, lerp); } } function setMaterialTextureFactory(options) { const { type, mapName } = options; return class setMaterialTexture extends NamedFunction2 { static type() { return type; } func(material, texture) { material[mapName] = texture; material.needsUpdate = true; } }; } export class setMaterialMap extends setMaterialTextureFactory({ type: "setMaterialMap", mapName: "map" }) { } export class setMaterialAlphaMap extends setMaterialTextureFactory({ type: "setMaterialAlphaMap", mapName: "alphaMap" }) { } export class setMaterialAOMap extends setMaterialTextureFactory({ type: "setMaterialAOMap", mapName: "aoMap" }) { } export class setMaterialDisplacementMap extends setMaterialTextureFactory({ type: "setMaterialDisplacementMap", mapName: "displacementMap" }) { } export class setMaterialEnvMap extends setMaterialTextureFactory({ type: "setMaterialEnvMap", mapName: "envMap" }) { } export class setMaterialEmissiveMap extends setMaterialTextureFactory({ type: "setMaterialEmissiveMap", mapName: "emissiveMap" }) { } export class setMaterialMetalnessMap extends setMaterialTextureFactory({ type: "setMaterialMetalnessMap", mapName: "metalnessMap" }) { } export class setMaterialRoughnessMap extends setMaterialTextureFactory({ type: "setMaterialRoughnessMap", mapName: "roughnessMap" }) { } export class setMaterialOpacity extends NamedFunction3 { static type() { return "setMaterialOpacity"; } func(material, opacity, lerp) { material.opacity = lerp * opacity + (1 - lerp) * material.opacity; } } export class setMaterialUniformNumber extends NamedFunction6 { static type() { return "setMaterialUniformNumber"; } func(material, uniformName, value, lerp, addPrefix, printWarnings) { const uniforms = MaterialUserDataUniforms.getUniforms(material); if (!uniforms) { if (printWarnings) { console.warn(`uniforms not found`, material); } return; } uniformName = _addParamUniformNamePrefix(uniformName, addPrefix); const uniform = uniforms[uniformName]; if (!uniform) { if (printWarnings) { console.warn(`uniform '${uniformName}' not found`, material, uniforms); } return; } if (lerp == 1) { uniform.value = value; } else { uniform.value = lerp * value + (1 - lerp) * uniform.value; } } } export class setMaterialUniformVectorColor extends NamedFunction6 { static type() { return "setMaterialUniformVectorColor"; } func(material, uniformName, value, lerp, addPrefix, printWarnings) { const uniforms = MaterialUserDataUniforms.getUniforms(material); if (!uniforms) { if (printWarnings) { console.warn(`uniforms not found`, material); } return; } uniformName = _addParamUniformNamePrefix(uniformName, addPrefix); const uniform = uniforms[uniformName]; if (!uniform) { if (printWarnings) { console.warn(`uniform '${uniformName}' not found`, material, uniforms); } return; } if (lerp >= 1) { uniform.value.copy(value); } else { uniform.value.lerp(value, lerp); } } } export class setMaterialUniformTexture extends NamedFunction5 { static type() { return "setMaterialUniformTexture"; } func(material, uniformName, value, addPrefix, printWarnings) { const uniforms = MaterialUserDataUniforms.getUniforms(material); if (!uniforms) { if (printWarnings) { console.warn(`uniforms not found`, material); } return; } uniformName = _addTextureUniformNamePrefix(uniformName, addPrefix); const uniform = uniforms[uniformName]; if (!uniform) { if (printWarnings) { console.warn(`uniform '${uniformName}' not found`, material, uniforms); } return; } uniform.value = value; } }