UNPKG

@babylonjs/materials

Version:

Babylon.js Materials Library =====================

283 lines 11.4 kB
import { __decorate } from "@babylonjs/core/tslib.es6.js"; import { 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 { VertexBuffer } from "@babylonjs/core/Buffers/buffer.js"; import { Scene } from "@babylonjs/core/scene.js"; import { RegisterClass } from "@babylonjs/core/Misc/typeStore.js"; import "./gradient.fragment.js"; import "./gradient.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 GradientMaterialDefines extends MaterialDefines { constructor() { super(); this.EMISSIVE = 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.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.IMAGEPROCESSINGPOSTPROCESS = false; this.SKIPFINALCOLORCLAMP = false; this.LOGARITHMICDEPTH = false; this.AREALIGHTSUPPORTED = true; this.AREALIGHTNOROUGHTNESS = true; this.rebuild(); } } export class GradientMaterial extends PushMaterial { constructor(name, scene) { super(name, scene); this._maxSimultaneousLights = 4; // The gradient top color, red by default this.topColor = new Color3(1, 0, 0); this.topColorAlpha = 1.0; // The gradient top color, blue by default this.bottomColor = new Color3(0, 0, 1); this.bottomColorAlpha = 1.0; // Gradient offset this.offset = 0; this.scale = 1.0; this.smoothness = 1.0; this._disableLighting = false; } needAlphaBlending() { return this.alpha < 1.0 || this.topColorAlpha < 1.0 || this.bottomColorAlpha < 1.0; } needAlphaTesting() { return true; } 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 GradientMaterialDefines(); } const defines = subMesh.materialDefines; const scene = this.getScene(); if (this._isReadyForSubMesh(subMesh)) { return true; } const engine = scene.getEngine(); PrepareDefinesForFrameBoundValues(scene, engine, this, defines, useInstances ? true : false); PrepareDefinesForMisc(mesh, scene, this._useLogarithmicDepth, this.pointsCloud, this.fogEnabled, this.needAlphaTestingForMesh(mesh), defines, undefined, undefined, undefined, this._setVertexOutputInvariant); defines._needNormals = PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting); defines.EMISSIVE = this._disableLighting; // Attribs PrepareDefinesForAttributes(mesh, defines, false, 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); 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 = "gradient"; const join = defines.toString(); const uniforms = [ "world", "view", "viewProjection", "vEyePosition", "vLightsType", "vFogInfos", "vFogColor", "pointSize", "mBones", "logarithmicDepthConstant", "topColor", "bottomColor", "offset", "smoothness", "scale", ]; AddClipPlaneUniforms(uniforms); const samplers = ["areaLightsLTC1Sampler", "areaLightsLTC2Sampler"]; const uniformBuffers = []; PrepareUniformsAndSamplersList({ uniformsNames: uniforms, uniformBuffersNames: uniformBuffers, samplers: samplers, defines: defines, maxSimultaneousLights: 4, }); 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: 4 }, }, engine), defines, this._materialContext); } // Check if Area Lights have LTC texture. if (defines["AREALIGHTUSED"]) { for (let index = 0; index < mesh.lightSources.length; index++) { if (!mesh.lightSources[index]._isReady()) { return false; } } } 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, effect); if (this._mustRebind(scene, effect, subMesh)) { // 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); } 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._activeEffect.setColor4("topColor", this.topColor, this.topColorAlpha); this._activeEffect.setColor4("bottomColor", this.bottomColor, this.bottomColorAlpha); this._activeEffect.setFloat("offset", this.offset); this._activeEffect.setFloat("scale", this.scale); this._activeEffect.setFloat("smoothness", this.smoothness); this._afterBind(mesh, this._activeEffect, subMesh); } getAnimatables() { return []; } dispose(forceDisposeEffect) { super.dispose(forceDisposeEffect); } clone(name) { return SerializationHelper.Clone(() => new GradientMaterial(name, this.getScene()), this); } serialize() { const serializationObject = super.serialize(); serializationObject.customType = "BABYLON.GradientMaterial"; return serializationObject; } getClassName() { return "GradientMaterial"; } // Statics static Parse(source, scene, rootUrl) { return SerializationHelper.Parse(() => new GradientMaterial(source.name, scene), source, scene, rootUrl); } } __decorate([ serialize("maxSimultaneousLights") ], GradientMaterial.prototype, "_maxSimultaneousLights", void 0); __decorate([ expandToProperty("_markAllSubMeshesAsLightsDirty") ], GradientMaterial.prototype, "maxSimultaneousLights", void 0); __decorate([ serializeAsColor3() ], GradientMaterial.prototype, "topColor", void 0); __decorate([ serialize() ], GradientMaterial.prototype, "topColorAlpha", void 0); __decorate([ serializeAsColor3() ], GradientMaterial.prototype, "bottomColor", void 0); __decorate([ serialize() ], GradientMaterial.prototype, "bottomColorAlpha", void 0); __decorate([ serialize() ], GradientMaterial.prototype, "offset", void 0); __decorate([ serialize() ], GradientMaterial.prototype, "scale", void 0); __decorate([ serialize() ], GradientMaterial.prototype, "smoothness", void 0); __decorate([ serialize("disableLighting") ], GradientMaterial.prototype, "_disableLighting", void 0); __decorate([ expandToProperty("_markAllSubMeshesAsLightsDirty") ], GradientMaterial.prototype, "disableLighting", void 0); RegisterClass("BABYLON.GradientMaterial", GradientMaterial); //# sourceMappingURL=gradientMaterial.js.map