@babylonjs/materials
Version:
Babylon.js Materials Library =====================
508 lines • 20.8 kB
JavaScript
import { __decorate } from "@babylonjs/core/tslib.es6.js";
import { serializeAsTexture, serialize, expandToProperty, serializeAsColor3 } from "@babylonjs/core/Misc/decorators.js";
import { SerializationHelper } from "@babylonjs/core/Misc/decorators.serialization.js";
import { Color3 } from "@babylonjs/core/Maths/math.color.js";
import { MaterialDefines } from "@babylonjs/core/Materials/materialDefines.js";
import { PushMaterial } from "@babylonjs/core/Materials/pushMaterial.js";
import { MaterialFlags } from "@babylonjs/core/Materials/materialFlags.js";
import { VertexBuffer } from "@babylonjs/core/Buffers/buffer.js";
import { Scene } from "@babylonjs/core/scene.js";
import { RegisterClass } from "@babylonjs/core/Misc/typeStore.js";
import "./mix.fragment.js";
import "./mix.vertex.js";
import { EffectFallbacks } from "@babylonjs/core/Materials/effectFallbacks.js";
import { AddClipPlaneUniforms, BindClipPlane } from "@babylonjs/core/Materials/clipPlaneMaterialHelper.js";
import { BindBonesParameters, BindFogParameters, BindLights, BindLogDepth, HandleFallbacksForShadows, PrepareAttributesForBones, PrepareAttributesForInstances, PrepareDefinesForAttributes, PrepareDefinesForFrameBoundValues, PrepareDefinesForLights, PrepareDefinesForMisc, PrepareUniformsAndSamplersList, } from "@babylonjs/core/Materials/materialHelper.functions.js";
class MixMaterialDefines extends MaterialDefines {
constructor() {
super();
this.DIFFUSE = false;
this.CLIPPLANE = false;
this.CLIPPLANE2 = false;
this.CLIPPLANE3 = false;
this.CLIPPLANE4 = false;
this.CLIPPLANE5 = false;
this.CLIPPLANE6 = false;
this.ALPHATEST = false;
this.DEPTHPREPASS = false;
this.POINTSIZE = false;
this.FOG = false;
this.SPECULARTERM = false;
this.NORMAL = false;
this.UV1 = false;
this.UV2 = false;
this.VERTEXCOLOR = false;
this.VERTEXALPHA = false;
this.NUM_BONE_INFLUENCERS = 0;
this.BonesPerMesh = 0;
this.INSTANCES = false;
this.INSTANCESCOLOR = false;
this.MIXMAP2 = false;
this.IMAGEPROCESSINGPOSTPROCESS = false;
this.SKIPFINALCOLORCLAMP = false;
this.LOGARITHMICDEPTH = false;
this.rebuild();
}
}
export class MixMaterial extends PushMaterial {
constructor(name, scene) {
super(name, scene);
/**
* Uniforms
*/
this.diffuseColor = new Color3(1, 1, 1);
this.specularColor = new Color3(0, 0, 0);
this.specularPower = 64;
this._disableLighting = false;
this._maxSimultaneousLights = 4;
}
needAlphaBlending() {
return this.alpha < 1.0;
}
needAlphaTesting() {
return false;
}
getAlphaTestTexture() {
return null;
}
// Methods
isReadyForSubMesh(mesh, subMesh, useInstances) {
const drawWrapper = subMesh._drawWrapper;
if (this.isFrozen) {
if (drawWrapper.effect && drawWrapper._wasPreviouslyReady && drawWrapper._wasPreviouslyUsingInstances === useInstances) {
return true;
}
}
if (!subMesh.materialDefines) {
subMesh.materialDefines = new MixMaterialDefines();
}
const defines = subMesh.materialDefines;
const scene = this.getScene();
if (this._isReadyForSubMesh(subMesh)) {
return true;
}
const engine = scene.getEngine();
// Textures
if (scene.texturesEnabled) {
if (!this._mixTexture1 || !this._mixTexture1.isReady()) {
return false;
}
defines._needUVs = true;
if (MaterialFlags.DiffuseTextureEnabled) {
if (!this._diffuseTexture1 || !this._diffuseTexture1.isReady()) {
return false;
}
defines.DIFFUSE = true;
if (!this._diffuseTexture2 || !this._diffuseTexture2.isReady()) {
return false;
}
if (!this._diffuseTexture3 || !this._diffuseTexture3.isReady()) {
return false;
}
if (!this._diffuseTexture4 || !this._diffuseTexture4.isReady()) {
return false;
}
if (this._mixTexture2) {
if (!this._mixTexture2.isReady()) {
return false;
}
defines.MIXMAP2 = true;
if (!this._diffuseTexture5 || !this._diffuseTexture5.isReady()) {
return false;
}
if (!this._diffuseTexture6 || !this._diffuseTexture6.isReady()) {
return false;
}
if (!this._diffuseTexture7 || !this._diffuseTexture7.isReady()) {
return false;
}
if (!this._diffuseTexture8 || !this._diffuseTexture8.isReady()) {
return false;
}
}
}
}
// Misc.
PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, this.needAlphaTestingForMesh(mesh), defines, undefined, undefined, undefined, this._setVertexOutputInvariant);
// Lights
defines._needNormals = PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
// Values that need to be evaluated on every frame
PrepareDefinesForFrameBoundValues(scene, engine, this, defines, useInstances ? true : false);
// Attribs
PrepareDefinesForAttributes(mesh, defines, true, true);
// Get correct effect
if (defines.isDirty) {
defines.markAsProcessed();
scene.resetCachedMaterial();
// Fallbacks
const fallbacks = new EffectFallbacks();
if (defines.FOG) {
fallbacks.addFallback(1, "FOG");
}
HandleFallbacksForShadows(defines, fallbacks, this.maxSimultaneousLights);
if (defines.NUM_BONE_INFLUENCERS > 0) {
fallbacks.addCPUSkinningFallback(0, mesh);
}
defines.IMAGEPROCESSINGPOSTPROCESS = scene.imageProcessingConfiguration.applyByPostProcess;
//Attributes
const attribs = [VertexBuffer.PositionKind];
if (defines.NORMAL) {
attribs.push(VertexBuffer.NormalKind);
}
if (defines.UV1) {
attribs.push(VertexBuffer.UVKind);
}
if (defines.UV2) {
attribs.push(VertexBuffer.UV2Kind);
}
if (defines.VERTEXCOLOR) {
attribs.push(VertexBuffer.ColorKind);
}
PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
PrepareAttributesForInstances(attribs, defines);
// Legacy browser patch
const shaderName = "mix";
const join = defines.toString();
const uniforms = [
"world",
"view",
"viewProjection",
"vEyePosition",
"vLightsType",
"vDiffuseColor",
"vSpecularColor",
"vFogInfos",
"vFogColor",
"pointSize",
"vTextureInfos",
"mBones",
"textureMatrix",
"logarithmicDepthConstant",
"diffuse1Infos",
"diffuse2Infos",
"diffuse3Infos",
"diffuse4Infos",
"diffuse5Infos",
"diffuse6Infos",
"diffuse7Infos",
"diffuse8Infos",
];
const samplers = [
"mixMap1Sampler",
"mixMap2Sampler",
"diffuse1Sampler",
"diffuse2Sampler",
"diffuse3Sampler",
"diffuse4Sampler",
"diffuse5Sampler",
"diffuse6Sampler",
"diffuse7Sampler",
"diffuse8Sampler",
];
const uniformBuffers = [];
AddClipPlaneUniforms(uniforms);
PrepareUniformsAndSamplersList({
uniformsNames: uniforms,
uniformBuffersNames: uniformBuffers,
samplers: samplers,
defines: defines,
maxSimultaneousLights: this.maxSimultaneousLights,
});
subMesh.setEffect(scene.getEngine().createEffect(shaderName, {
attributes: attribs,
uniformsNames: uniforms,
uniformBuffersNames: uniformBuffers,
samplers: samplers,
defines: join,
fallbacks: fallbacks,
onCompiled: this.onCompiled,
onError: this.onError,
indexParameters: { maxSimultaneousLights: this.maxSimultaneousLights },
}, engine), defines, this._materialContext);
}
if (!subMesh.effect || !subMesh.effect.isReady()) {
return false;
}
defines._renderId = scene.getRenderId();
drawWrapper._wasPreviouslyReady = true;
drawWrapper._wasPreviouslyUsingInstances = !!useInstances;
return true;
}
bindForSubMesh(world, mesh, subMesh) {
const scene = this.getScene();
const defines = subMesh.materialDefines;
if (!defines) {
return;
}
const effect = subMesh.effect;
if (!effect) {
return;
}
this._activeEffect = effect;
// Matrices
this.bindOnlyWorldMatrix(world);
this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
// Bones
BindBonesParameters(mesh, this._activeEffect);
if (this._mustRebind(scene, effect, subMesh)) {
// Textures
if (this._mixTexture1) {
this._activeEffect.setTexture("mixMap1Sampler", this._mixTexture1);
this._activeEffect.setFloat2("vTextureInfos", this._mixTexture1.coordinatesIndex, this._mixTexture1.level);
this._activeEffect.setMatrix("textureMatrix", this._mixTexture1.getTextureMatrix());
if (MaterialFlags.DiffuseTextureEnabled) {
if (this._diffuseTexture1) {
this._activeEffect.setTexture("diffuse1Sampler", this._diffuseTexture1);
this._activeEffect.setFloat2("diffuse1Infos", this._diffuseTexture1.uScale, this._diffuseTexture1.vScale);
}
if (this._diffuseTexture2) {
this._activeEffect.setTexture("diffuse2Sampler", this._diffuseTexture2);
this._activeEffect.setFloat2("diffuse2Infos", this._diffuseTexture2.uScale, this._diffuseTexture2.vScale);
}
if (this._diffuseTexture3) {
this._activeEffect.setTexture("diffuse3Sampler", this._diffuseTexture3);
this._activeEffect.setFloat2("diffuse3Infos", this._diffuseTexture3.uScale, this._diffuseTexture3.vScale);
}
if (this._diffuseTexture4) {
this._activeEffect.setTexture("diffuse4Sampler", this._diffuseTexture4);
this._activeEffect.setFloat2("diffuse4Infos", this._diffuseTexture4.uScale, this._diffuseTexture4.vScale);
}
}
}
if (this._mixTexture2) {
this._activeEffect.setTexture("mixMap2Sampler", this._mixTexture2);
if (MaterialFlags.DiffuseTextureEnabled) {
if (this._diffuseTexture5) {
this._activeEffect.setTexture("diffuse5Sampler", this._diffuseTexture5);
this._activeEffect.setFloat2("diffuse5Infos", this._diffuseTexture5.uScale, this._diffuseTexture5.vScale);
}
if (this._diffuseTexture6) {
this._activeEffect.setTexture("diffuse6Sampler", this._diffuseTexture6);
this._activeEffect.setFloat2("diffuse6Infos", this._diffuseTexture6.uScale, this._diffuseTexture6.vScale);
}
if (this._diffuseTexture7) {
this._activeEffect.setTexture("diffuse7Sampler", this._diffuseTexture7);
this._activeEffect.setFloat2("diffuse7Infos", this._diffuseTexture7.uScale, this._diffuseTexture7.vScale);
}
if (this._diffuseTexture8) {
this._activeEffect.setTexture("diffuse8Sampler", this._diffuseTexture8);
this._activeEffect.setFloat2("diffuse8Infos", this._diffuseTexture8.uScale, this._diffuseTexture8.vScale);
}
}
}
// Clip plane
BindClipPlane(effect, this, scene);
// Point size
if (this.pointsCloud) {
this._activeEffect.setFloat("pointSize", this.pointSize);
}
// Log. depth
if (this._useLogarithmicDepth) {
BindLogDepth(defines, effect, scene);
}
scene.bindEyePosition(effect);
}
this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
if (defines.SPECULARTERM) {
this._activeEffect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
}
if (scene.lightsEnabled && !this.disableLighting) {
BindLights(scene, mesh, this._activeEffect, defines, this.maxSimultaneousLights);
}
// View
if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
this._activeEffect.setMatrix("view", scene.getViewMatrix());
}
// Fog
BindFogParameters(scene, mesh, this._activeEffect);
this._afterBind(mesh, this._activeEffect, subMesh);
}
getAnimatables() {
const results = [];
if (this._mixTexture1 && this._mixTexture1.animations && this._mixTexture1.animations.length > 0) {
results.push(this._mixTexture1);
}
if (this._mixTexture2 && this._mixTexture2.animations && this._mixTexture2.animations.length > 0) {
results.push(this._mixTexture2);
}
return results;
}
getActiveTextures() {
const activeTextures = super.getActiveTextures();
// Mix map 1
if (this._mixTexture1) {
activeTextures.push(this._mixTexture1);
}
if (this._diffuseTexture1) {
activeTextures.push(this._diffuseTexture1);
}
if (this._diffuseTexture2) {
activeTextures.push(this._diffuseTexture2);
}
if (this._diffuseTexture3) {
activeTextures.push(this._diffuseTexture3);
}
if (this._diffuseTexture4) {
activeTextures.push(this._diffuseTexture4);
}
// Mix map 2
if (this._mixTexture2) {
activeTextures.push(this._mixTexture2);
}
if (this._diffuseTexture5) {
activeTextures.push(this._diffuseTexture5);
}
if (this._diffuseTexture6) {
activeTextures.push(this._diffuseTexture6);
}
if (this._diffuseTexture7) {
activeTextures.push(this._diffuseTexture7);
}
if (this._diffuseTexture8) {
activeTextures.push(this._diffuseTexture8);
}
return activeTextures;
}
hasTexture(texture) {
if (super.hasTexture(texture)) {
return true;
}
// Mix map 1
if (this._mixTexture1 === texture) {
return true;
}
if (this._diffuseTexture1 === texture) {
return true;
}
if (this._diffuseTexture2 === texture) {
return true;
}
if (this._diffuseTexture3 === texture) {
return true;
}
if (this._diffuseTexture4 === texture) {
return true;
}
// Mix map 2
if (this._mixTexture2 === texture) {
return true;
}
if (this._diffuseTexture5 === texture) {
return true;
}
if (this._diffuseTexture6 === texture) {
return true;
}
if (this._diffuseTexture7 === texture) {
return true;
}
if (this._diffuseTexture8 === texture) {
return true;
}
return false;
}
dispose(forceDisposeEffect) {
if (this._mixTexture1) {
this._mixTexture1.dispose();
}
super.dispose(forceDisposeEffect);
}
clone(name) {
return SerializationHelper.Clone(() => new MixMaterial(name, this.getScene()), this);
}
serialize() {
const serializationObject = super.serialize();
serializationObject.customType = "BABYLON.MixMaterial";
return serializationObject;
}
getClassName() {
return "MixMaterial";
}
// Statics
static Parse(source, scene, rootUrl) {
return SerializationHelper.Parse(() => new MixMaterial(source.name, scene), source, scene, rootUrl);
}
}
__decorate([
serializeAsTexture("mixTexture1")
], MixMaterial.prototype, "_mixTexture1", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsTexturesDirty")
], MixMaterial.prototype, "mixTexture1", void 0);
__decorate([
serializeAsTexture("mixTexture2")
], MixMaterial.prototype, "_mixTexture2", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsTexturesDirty")
], MixMaterial.prototype, "mixTexture2", void 0);
__decorate([
serializeAsTexture("diffuseTexture1")
], MixMaterial.prototype, "_diffuseTexture1", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsTexturesDirty")
], MixMaterial.prototype, "diffuseTexture1", void 0);
__decorate([
serializeAsTexture("diffuseTexture2")
], MixMaterial.prototype, "_diffuseTexture2", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsTexturesDirty")
], MixMaterial.prototype, "diffuseTexture2", void 0);
__decorate([
serializeAsTexture("diffuseTexture3")
], MixMaterial.prototype, "_diffuseTexture3", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsTexturesDirty")
], MixMaterial.prototype, "diffuseTexture3", void 0);
__decorate([
serializeAsTexture("diffuseTexture4")
], MixMaterial.prototype, "_diffuseTexture4", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsTexturesDirty")
], MixMaterial.prototype, "diffuseTexture4", void 0);
__decorate([
serializeAsTexture("diffuseTexture1")
], MixMaterial.prototype, "_diffuseTexture5", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsTexturesDirty")
], MixMaterial.prototype, "diffuseTexture5", void 0);
__decorate([
serializeAsTexture("diffuseTexture2")
], MixMaterial.prototype, "_diffuseTexture6", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsTexturesDirty")
], MixMaterial.prototype, "diffuseTexture6", void 0);
__decorate([
serializeAsTexture("diffuseTexture3")
], MixMaterial.prototype, "_diffuseTexture7", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsTexturesDirty")
], MixMaterial.prototype, "diffuseTexture7", void 0);
__decorate([
serializeAsTexture("diffuseTexture4")
], MixMaterial.prototype, "_diffuseTexture8", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsTexturesDirty")
], MixMaterial.prototype, "diffuseTexture8", void 0);
__decorate([
serializeAsColor3()
], MixMaterial.prototype, "diffuseColor", void 0);
__decorate([
serializeAsColor3()
], MixMaterial.prototype, "specularColor", void 0);
__decorate([
serialize()
], MixMaterial.prototype, "specularPower", void 0);
__decorate([
serialize("disableLighting")
], MixMaterial.prototype, "_disableLighting", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsLightsDirty")
], MixMaterial.prototype, "disableLighting", void 0);
__decorate([
serialize("maxSimultaneousLights")
], MixMaterial.prototype, "_maxSimultaneousLights", void 0);
__decorate([
expandToProperty("_markAllSubMeshesAsLightsDirty")
], MixMaterial.prototype, "maxSimultaneousLights", void 0);
RegisterClass("BABYLON.MixMaterial", MixMaterial);
//# sourceMappingURL=mixMaterial.js.map