babylon.gradientMaterial.min.js 11 KB

1
  1. var BABYLON;!function(e){var i=function(e){function i(){e.call(this),this.DIFFUSE=!1,this.CLIPPLANE=!1,this.ALPHATEST=!1,this.POINTSIZE=!1,this.FOG=!1,this.LIGHT0=!1,this.LIGHT1=!1,this.LIGHT2=!1,this.LIGHT3=!1,this.SPOTLIGHT0=!1,this.SPOTLIGHT1=!1,this.SPOTLIGHT2=!1,this.SPOTLIGHT3=!1,this.HEMILIGHT0=!1,this.HEMILIGHT1=!1,this.HEMILIGHT2=!1,this.HEMILIGHT3=!1,this.DIRLIGHT0=!1,this.DIRLIGHT1=!1,this.DIRLIGHT2=!1,this.DIRLIGHT3=!1,this.POINTLIGHT0=!1,this.POINTLIGHT1=!1,this.POINTLIGHT2=!1,this.POINTLIGHT3=!1,this.SHADOW0=!1,this.SHADOW1=!1,this.SHADOW2=!1,this.SHADOW3=!1,this.SHADOWS=!1,this.SHADOWVSM0=!1,this.SHADOWVSM1=!1,this.SHADOWVSM2=!1,this.SHADOWVSM3=!1,this.SHADOWPCF0=!1,this.SHADOWPCF1=!1,this.SHADOWPCF2=!1,this.SHADOWPCF3=!1,this.NORMAL=!1,this.UV1=!1,this.UV2=!1,this.VERTEXCOLOR=!1,this.VERTEXALPHA=!1,this.NUM_BONE_INFLUENCERS=0,this.BonesPerMesh=0,this.INSTANCES=!1,this._keys=Object.keys(this)}return __extends(i,e),i}(e.MaterialDefines),n=function(n){function r(r,t){n.call(this,r,t),this.topColor=new e.Color3(1,0,0),this.topColorAlpha=1,this.bottomColor=new e.Color3(0,0,1),this.bottomColorAlpha=1,this.offset=0,this.smoothness=1,this.disableLighting=!1,this._worldViewProjectionMatrix=e.Matrix.Zero(),this._scaledDiffuse=new e.Color3,this._defines=new i,this._cachedDefines=new i,this._cachedDefines.BonesPerMesh=-1}return __extends(r,n),r.prototype.needAlphaBlending=function(){return this.alpha<1||this.topColorAlpha<1||this.bottomColorAlpha<1},r.prototype.needAlphaTesting=function(){return!0},r.prototype.getAlphaTestTexture=function(){return null},r.prototype._checkCache=function(e,i,n){return i?this._defines.INSTANCES!==n?!1:i._materialDefines&&i._materialDefines.isEqual(this._defines)?!0:!1:!0},r.prototype.isReady=function(n,r){if(this.checkReadyOnlyOnce&&this._wasPreviouslyReady)return!0;var t=this.getScene();if(!this.checkReadyOnEveryCall&&this._renderId===t.getRenderId()&&this._checkCache(t,n,r))return!0;var o=t.getEngine(),s=!1,a=!1;this._defines.reset(),t.clipPlane&&(this._defines.CLIPPLANE=!0),o.getAlphaTesting()&&(this._defines.ALPHATEST=!0),(this.pointsCloud||t.forcePointsCloud)&&(this._defines.POINTSIZE=!0),t.fogEnabled&&n&&n.applyFog&&t.fogMode!==e.Scene.FOGMODE_NONE&&this.fogEnabled&&(this._defines.FOG=!0);if(t.lightsEnabled&&!this.disableLighting&&(s=e.MaterialHelper.PrepareDefinesForLights(t,n,this._defines)),n&&(s&&n.isVerticesDataPresent(e.VertexBuffer.NormalKind)&&(this._defines.NORMAL=!0),a&&(n.isVerticesDataPresent(e.VertexBuffer.UVKind)&&(this._defines.UV1=!0),n.isVerticesDataPresent(e.VertexBuffer.UV2Kind)&&(this._defines.UV2=!0)),n.useVertexColors&&n.isVerticesDataPresent(e.VertexBuffer.ColorKind)&&(this._defines.VERTEXCOLOR=!0,n.hasVertexAlpha&&(this._defines.VERTEXALPHA=!0)),n.useBones&&n.computeBonesUsingShaders&&(this._defines.NUM_BONE_INFLUENCERS=n.numBoneInfluencers,this._defines.BonesPerMesh=n.skeleton.bones.length+1),r&&(this._defines.INSTANCES=!0)),!this._defines.isEqual(this._cachedDefines)){this._defines.cloneTo(this._cachedDefines),t.resetCachedMaterial();var f=new e.EffectFallbacks;this._defines.FOG&&f.addFallback(1,"FOG"),e.MaterialHelper.HandleFallbacksForShadows(this._defines,f),this._defines.NUM_BONE_INFLUENCERS>0&&f.addCPUSkinningFallback(0,n);var l=[e.VertexBuffer.PositionKind];this._defines.NORMAL&&l.push(e.VertexBuffer.NormalKind),this._defines.UV1&&l.push(e.VertexBuffer.UVKind),this._defines.UV2&&l.push(e.VertexBuffer.UV2Kind),this._defines.VERTEXCOLOR&&l.push(e.VertexBuffer.ColorKind),e.MaterialHelper.PrepareAttributesForBones(l,n,this._defines,f),e.MaterialHelper.PrepareAttributesForInstances(l,this._defines);var h="gradient",d=this._defines.toString();this._effect=t.getEngine().createEffect(h,l,["world","view","viewProjection","vEyePosition","vLightsType","vDiffuseColor","vLightData0","vLightDiffuse0","vLightSpecular0","vLightDirection0","vLightGround0","lightMatrix0","vLightData1","vLightDiffuse1","vLightSpecular1","vLightDirection1","vLightGround1","lightMatrix1","vLightData2","vLightDiffuse2","vLightSpecular2","vLightDirection2","vLightGround2","lightMatrix2","vLightData3","vLightDiffuse3","vLightSpecular3","vLightDirection3","vLightGround3","lightMatrix3","vFogInfos","vFogColor","pointSize","vDiffuseInfos","mBones","vClipPlane","diffuseMatrix","shadowsInfo0","shadowsInfo1","shadowsInfo2","shadowsInfo3","depthValues","topColor","bottomColor","offset","smoothness"],["diffuseSampler","shadowSampler0","shadowSampler1","shadowSampler2","shadowSampler3"],d,f,this.onCompiled,this.onError)}return this._effect.isReady()?(this._renderId=t.getRenderId(),this._wasPreviouslyReady=!0,n&&(n._materialDefines||(n._materialDefines=new i),this._defines.cloneTo(n._materialDefines)),!0):!1},r.prototype.bindOnlyWorldMatrix=function(e){this._effect.setMatrix("world",e)},r.prototype.bind=function(i,r){var t=this.getScene();this.bindOnlyWorldMatrix(i),this._effect.setMatrix("viewProjection",t.getTransformMatrix()),e.MaterialHelper.BindBonesParameters(r,this._effect),t.getCachedMaterial()!==this&&(e.MaterialHelper.BindClipPlane(this._effect,t),this.pointsCloud&&this._effect.setFloat("pointSize",this.pointSize),this._effect.setVector3("vEyePosition",t._mirroredCameraPosition?t._mirroredCameraPosition:t.activeCamera.position)),this._effect.setColor4("vDiffuseColor",this._scaledDiffuse,this.alpha*r.visibility),t.lightsEnabled&&!this.disableLighting&&e.MaterialHelper.BindLights(t,r,this._effect,this._defines),t.fogEnabled&&r.applyFog&&t.fogMode!==e.Scene.FOGMODE_NONE&&this._effect.setMatrix("view",t.getViewMatrix()),e.MaterialHelper.BindFogParameters(t,r,this._effect),this._effect.setColor4("topColor",this.topColor,this.topColorAlpha),this._effect.setColor4("bottomColor",this.bottomColor,this.bottomColorAlpha),this._effect.setFloat("offset",this.offset),this._effect.setFloat("smoothness",this.smoothness),n.prototype.bind.call(this,i,r)},r.prototype.getAnimatables=function(){return[]},r.prototype.dispose=function(e){n.prototype.dispose.call(this,e)},r.prototype.clone=function(e){var i=new r(e,this.getScene());return this.copyTo(i),i.topColor=this.topColor.clone(),i.bottomColor=this.bottomColor.clone(),i.offset=this.offset,i},r.prototype.serialize=function(){var e=n.prototype.serialize.call(this);return e.customType="BABYLON.GradientMaterial",e.topColor=this.topColor.asArray(),e.bottomColor=this.bottomColor.asArray(),e.offset=this.offset,e.disableLighting=this.disableLighting,e},r.Parse=function(i,n,t){var o=new r(i.name,n);return o.topColor=e.Color3.FromArray(i.topColor),o.bottomColor=e.Color3.FromArray(i.bottomColor),o.offset=i.offset,o.disableLighting=i.disableLighting,o.alpha=i.alpha,o.id=i.id,e.Tags.AddTagsTo(o,i.tags),o.backFaceCulling=i.backFaceCulling,o.wireframe=i.wireframe,i.checkReadyOnlyOnce&&(o.checkReadyOnlyOnce=i.checkReadyOnlyOnce),o},r}(e.Material);e.GradientMaterial=n}(BABYLON||(BABYLON={})),BABYLON.Effect.ShadersStore.gradientVertexShader="precision highp float;\r\n\r\n\r\n// Attributes\r\nattribute vec3 position;\r\n#ifdef NORMAL\r\nattribute vec3 normal;\r\n#endif\r\n#ifdef UV1\r\nattribute vec2 uv;\r\n#endif\r\n#ifdef UV2\r\nattribute vec2 uv2;\r\n#endif\r\n#ifdef VERTEXCOLOR\r\nattribute vec4 color;\r\n#endif\r\n#include<bonesDeclaration>\r\n\r\n// Uniforms\r\n#include<instancesDeclaration>\r\n\r\nuniform mat4 view;\r\nuniform mat4 viewProjection;\r\n\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\nuniform mat4 diffuseMatrix;\r\nuniform vec2 vDiffuseInfos;\r\n#endif\r\n\r\n#ifdef POINTSIZE\r\nuniform float pointSize;\r\n#endif\r\n\r\n// Output\r\nvarying vec3 vPositionW;\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n#include<clipPlaneVertexDeclaration>\r\n\r\n#include<fogVertexDeclaration>\r\n#include<shadowsVertexDeclaration>\r\n\r\nvoid main(void) {\r\n#include<instancesVertex>\r\n#include<bonesVertex> \r\n\r\n gl_Position = viewProjection * finalWorld * vec4(position, 1.0);\r\n\r\n vec4 worldPos = finalWorld * vec4(position, 1.0);\r\n vPositionW = vec3(worldPos);\r\n\r\n#ifdef NORMAL\r\n vNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));\r\n#endif\r\n\r\n // Texture coordinates\r\n#ifndef UV1\r\n vec2 uv = vec2(0., 0.);\r\n#endif\r\n#ifndef UV2\r\n vec2 uv2 = vec2(0., 0.);\r\n#endif\r\n\r\n#ifdef DIFFUSE\r\n if (vDiffuseInfos.x == 0.)\r\n {\r\n vDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));\r\n }\r\n else\r\n {\r\n vDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));\r\n }\r\n#endif\r\n\r\n // Clip plane\r\n#include<clipPlaneVertex>\r\n\r\n // Fog\r\n#include<fogVertex>\r\n#include<shadowsVertex>\r\n\r\n // Vertex color\r\n#ifdef VERTEXCOLOR\r\n vColor = color;\r\n#endif\r\n\r\n // Point size\r\n#ifdef POINTSIZE\r\n gl_PointSize = pointSize;\r\n#endif\r\n}\r\n",BABYLON.Effect.ShadersStore.gradientPixelShader="precision highp float;\r\n\r\n// Constants\r\nuniform vec3 vEyePosition;\r\nuniform vec4 vDiffuseColor;\r\n\r\n// Gradient variables\r\nuniform vec4 topColor;\r\nuniform vec4 bottomColor;\r\nuniform float offset;\r\nuniform float smoothness;\r\n\r\n// Input\r\nvarying vec3 vPositionW;\r\n\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n// Lights\r\n\r\n#include<light0FragmentDeclaration>\r\n#include<light1FragmentDeclaration>\r\n#include<light2FragmentDeclaration>\r\n#include<light3FragmentDeclaration>\r\n\r\n\r\n#include<lightsFragmentFunctions>\r\n#include<shadowsFragmentFunctions>\r\n\r\n// Samplers\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\nuniform sampler2D diffuseSampler;\r\nuniform vec2 vDiffuseInfos;\r\n#endif\r\n\r\n#include<clipPlaneFragmentDeclaration>\r\n\r\n// Fog\r\n#include<fogFragmentDeclaration>\r\n\r\nvoid main(void) {\r\n#include<clipPlaneFragment>\r\n\r\n vec3 viewDirectionW = normalize(vEyePosition - vPositionW);\r\n\r\n float h = normalize(vPositionW).y + offset;\r\n float mysmoothness = clamp(smoothness, 0.01, max(smoothness, 10.));\r\n\r\n vec4 baseColor = mix(bottomColor, topColor, max(pow(max(h, 0.0), mysmoothness), 0.0));\r\n\r\n // Base color\r\n vec3 diffuseColor = baseColor.rgb;\r\n\r\n // Alpha\r\n float alpha = baseColor.a;\r\n\r\n\r\n#ifdef ALPHATEST\r\n if (baseColor.a < 0.4)\r\n discard;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\n baseColor.rgb *= vColor.rgb;\r\n#endif\r\n\r\n // Bump\r\n#ifdef NORMAL\r\n vec3 normalW = normalize(vNormalW);\r\n#else\r\n vec3 normalW = vec3(1.0, 1.0, 1.0);\r\n#endif\r\n\r\n // Lighting\r\n vec3 diffuseBase = vec3(0., 0., 0.);\r\n float shadow = 1.;\r\n float glossiness = 0.;\r\n \r\n#include<light0Fragment>\r\n#include<light1Fragment>\r\n#include<light2Fragment>\r\n#include<light3Fragment>\r\n\r\n#ifdef VERTEXALPHA\r\n alpha *= vColor.a;\r\n#endif\r\n\r\n vec3 finalDiffuse = clamp(diffuseBase * diffuseColor, 0.0, 1.0) * baseColor.rgb;\r\n\r\n // Composition\r\n vec4 color = vec4(finalDiffuse, alpha);\r\n\r\n#include<fogFragment>\r\n\r\n gl_FragColor = color;\r\n}";