babylon.furMaterial.min.js 15 KB

1
  1. var BABYLON;!(function(e){var i=(function(e){function i(){var i=e.call(this)||this;return i.DIFFUSE=!1,i.HEIGHTMAP=!1,i.CLIPPLANE=!1,i.ALPHATEST=!1,i.POINTSIZE=!1,i.FOG=!1,i.NORMAL=!1,i.UV1=!1,i.UV2=!1,i.VERTEXCOLOR=!1,i.VERTEXALPHA=!1,i.NUM_BONE_INFLUENCERS=0,i.BonesPerMesh=0,i.INSTANCES=!1,i.HIGHLEVEL=!1,i.rebuild(),i}return __extends(i,e),i})(e.MaterialDefines),t=(function(t){function n(n,r){var f=t.call(this,n,r)||this;return f.diffuseColor=new e.Color3(1,1,1),f.furLength=1,f.furAngle=0,f.furColor=new e.Color3(.44,.21,.02),f.furOffset=0,f.furSpacing=12,f.furGravity=new e.Vector3(0,0,0),f.furSpeed=100,f.furDensity=20,f.disableLighting=!1,f.highLevelFur=!0,f.maxSimultaneousLights=4,f._worldViewProjectionMatrix=e.Matrix.Zero(),f._furTime=0,f._defines=new i,f._cachedDefines=new i,f._cachedDefines.BonesPerMesh=-1,f}return __extends(n,t),Object.defineProperty(n.prototype,"furTime",{get:function(){return this._furTime},set:function(e){this._furTime=e},enumerable:!0,configurable:!0}),n.prototype.needAlphaBlending=function(){return this.alpha<1},n.prototype.needAlphaTesting=function(){return!1},n.prototype.getAlphaTestTexture=function(){return null},n.prototype.updateFur=function(){for(var e=1;e<this._meshes.length;e++){var i=this._meshes[e].material;i.furLength=this.furLength,i.furAngle=this.furAngle,i.furGravity=this.furGravity,i.furSpacing=this.furSpacing,i.furSpeed=this.furSpeed,i.furColor=this.furColor,i.diffuseTexture=this.diffuseTexture,i.furTexture=this.furTexture,i.highLevelFur=this.highLevelFur,i.furTime=this.furTime,i.furDensity=this.furDensity}},n.prototype._checkCache=function(e,i,t){return!i||this._defines.INSTANCES===t&&!(!i._materialDefines||!i._materialDefines.isEqual(this._defines))},n.prototype.isReady=function(t,n){if(this.checkReadyOnlyOnce&&this._wasPreviouslyReady)return!0;var r=this.getScene();if(!this.checkReadyOnEveryCall&&this._renderId===r.getRenderId()&&this._checkCache(r,t,n))return!0;var f=r.getEngine(),s=!1,o=!1;if(this._defines.reset(),r.texturesEnabled){if(this.diffuseTexture&&e.StandardMaterial.DiffuseTextureEnabled){if(!this.diffuseTexture.isReady())return!1;o=!0,this._defines.DIFFUSE=!0}if(this.heightTexture){if(!this.heightTexture.isReady())return!1;o=!0,this._defines.HEIGHTMAP=!0}}if(r.clipPlane&&(this._defines.CLIPPLANE=!0),f.getAlphaTesting()&&(this._defines.ALPHATEST=!0),(this.pointsCloud||r.forcePointsCloud)&&(this._defines.POINTSIZE=!0),r.fogEnabled&&t&&t.applyFog&&r.fogMode!==e.Scene.FOGMODE_NONE&&this.fogEnabled&&(this._defines.FOG=!0),this.highLevelFur&&(this._defines.HIGHLEVEL=!0),r.lightsEnabled&&!this.disableLighting&&(s=e.MaterialHelper.PrepareDefinesForLights(r,t,this._defines,this.maxSimultaneousLights)),t&&(s&&t.isVerticesDataPresent(e.VertexBuffer.NormalKind)&&(this._defines.NORMAL=!0),o&&(t.isVerticesDataPresent(e.VertexBuffer.UVKind)&&(this._defines.UV1=!0),t.isVerticesDataPresent(e.VertexBuffer.UV2Kind)&&(this._defines.UV2=!0)),t.useVertexColors&&t.isVerticesDataPresent(e.VertexBuffer.ColorKind)&&(this._defines.VERTEXCOLOR=!0,t.hasVertexAlpha&&(this._defines.VERTEXALPHA=!0)),t.useBones&&t.computeBonesUsingShaders&&(this._defines.NUM_BONE_INFLUENCERS=t.numBoneInfluencers,this._defines.BonesPerMesh=t.skeleton.bones.length+1),n&&(this._defines.INSTANCES=!0)),!this._defines.isEqual(this._cachedDefines)){this._defines.cloneTo(this._cachedDefines),r.resetCachedMaterial();var a=new e.EffectFallbacks;this._defines.FOG&&a.addFallback(1,"FOG"),e.MaterialHelper.HandleFallbacksForShadows(this._defines,a,this.maxSimultaneousLights),this._defines.NUM_BONE_INFLUENCERS>0&&a.addCPUSkinningFallback(0,t);var u=[e.VertexBuffer.PositionKind];this._defines.NORMAL&&u.push(e.VertexBuffer.NormalKind),this._defines.UV1&&u.push(e.VertexBuffer.UVKind),this._defines.UV2&&u.push(e.VertexBuffer.UV2Kind),this._defines.VERTEXCOLOR&&u.push(e.VertexBuffer.ColorKind),e.MaterialHelper.PrepareAttributesForBones(u,t,this._defines,a),e.MaterialHelper.PrepareAttributesForInstances(u,this._defines);var l=this._defines.toString(),h=["world","view","viewProjection","vEyePosition","vLightsType","vDiffuseColor","vFogInfos","vFogColor","pointSize","vDiffuseInfos","mBones","vClipPlane","diffuseMatrix","furLength","furAngle","furColor","furOffset","furGravity","furTime","furSpacing","furDensity"],d=["diffuseSampler","heightTexture","furTexture"];e.MaterialHelper.PrepareUniformsAndSamplersList(h,d,this._defines,this.maxSimultaneousLights),this._effect=r.getEngine().createEffect("fur",u,h,d,l,a,this.onCompiled,this.onError,{maxSimultaneousLights:this.maxSimultaneousLights})}return!!this._effect.isReady()&&(this._renderId=r.getRenderId(),this._wasPreviouslyReady=!0,t&&(t._materialDefines||(t._materialDefines=new i),this._defines.cloneTo(t._materialDefines)),!0)},n.prototype.bindOnlyWorldMatrix=function(e){this._effect.setMatrix("world",e)},n.prototype.bind=function(i,n){var r=this.getScene();this.bindOnlyWorldMatrix(i),this._effect.setMatrix("viewProjection",r.getTransformMatrix()),e.MaterialHelper.BindBonesParameters(n,this._effect),r.getCachedMaterial()!==this&&(this.diffuseTexture&&e.StandardMaterial.DiffuseTextureEnabled&&(this._effect.setTexture("diffuseSampler",this.diffuseTexture),this._effect.setFloat2("vDiffuseInfos",this.diffuseTexture.coordinatesIndex,this.diffuseTexture.level),this._effect.setMatrix("diffuseMatrix",this.diffuseTexture.getTextureMatrix())),this.heightTexture&&this._effect.setTexture("heightTexture",this.heightTexture),e.MaterialHelper.BindClipPlane(this._effect,r),this.pointsCloud&&this._effect.setFloat("pointSize",this.pointSize),this._effect.setVector3("vEyePosition",r._mirroredCameraPosition?r._mirroredCameraPosition:r.activeCamera.position)),this._effect.setColor4("vDiffuseColor",this.diffuseColor,this.alpha*n.visibility),r.lightsEnabled&&!this.disableLighting&&e.MaterialHelper.BindLights(r,n,this._effect,this._defines,this.maxSimultaneousLights),r.fogEnabled&&n.applyFog&&r.fogMode!==e.Scene.FOGMODE_NONE&&this._effect.setMatrix("view",r.getViewMatrix()),e.MaterialHelper.BindFogParameters(r,n,this._effect),this._effect.setFloat("furLength",this.furLength),this._effect.setFloat("furAngle",this.furAngle),this._effect.setColor4("furColor",this.furColor,1),this.highLevelFur&&(this._effect.setVector3("furGravity",this.furGravity),this._effect.setFloat("furOffset",this.furOffset),this._effect.setFloat("furSpacing",this.furSpacing),this._effect.setFloat("furDensity",this.furDensity),this._furTime+=this.getScene().getEngine().getDeltaTime()/this.furSpeed,this._effect.setFloat("furTime",this._furTime),this._effect.setTexture("furTexture",this.furTexture)),t.prototype.bind.call(this,i,n)},n.prototype.getAnimatables=function(){var e=[];return this.diffuseTexture&&this.diffuseTexture.animations&&this.diffuseTexture.animations.length>0&&e.push(this.diffuseTexture),this.heightTexture&&this.heightTexture.animations&&this.heightTexture.animations.length>0&&e.push(this.heightTexture),e},n.prototype.dispose=function(e){if(this.diffuseTexture&&this.diffuseTexture.dispose(),this._meshes)for(var i=1;i<this._meshes.length;i++)this._meshes[i].material.dispose(e),this._meshes[i].dispose();t.prototype.dispose.call(this,e)},n.prototype.clone=function(i){var t=this;return e.SerializationHelper.Clone((function(){return new n(i,t.getScene())}),this)},n.prototype.serialize=function(){var i=e.SerializationHelper.Serialize(this);return i.customType="BABYLON.FurMaterial",this._meshes&&(i.sourceMeshName=this._meshes[0].name,i.quality=this._meshes.length),i},n.Parse=function(i,t,r){var f=e.SerializationHelper.Parse((function(){return new n(i.name,t)}),i,t,r);return i.sourceMeshName&&f.highLevelFur&&t.executeWhenReady((function(){var e=t.getMeshByName(i.sourceMeshName);if(e){var r=n.GenerateTexture("Fur Texture",t);f.furTexture=r,n.FurifyMesh(e,i.quality)}})),f},n.GenerateTexture=function(i,t){for(var n=new e.DynamicTexture("FurTexture "+i,256,t,!0),r=n.getContext(),f=0;f<2e4;++f)r.fillStyle="rgba(255, "+Math.floor(255*Math.random())+", "+Math.floor(255*Math.random())+", 1)",r.fillRect(Math.random()*n.getSize().width,Math.random()*n.getSize().height,2,2);return n.update(!1),n.wrapU=e.Texture.WRAP_ADDRESSMODE,n.wrapV=e.Texture.WRAP_ADDRESSMODE,n},n.FurifyMesh=function(i,t){var r,f=[i],s=i.material;if(!(s instanceof n))throw"The material of the source mesh must be a Fur Material";for(r=1;r<t;r++){var o=new e.FurMaterial(s.name+r,i.getScene());i.getScene().materials.pop(),e.Tags.EnableFor(o),e.Tags.AddTagsTo(o,"furShellMaterial"),o.furLength=s.furLength,o.furAngle=s.furAngle,o.furGravity=s.furGravity,o.furSpacing=s.furSpacing,o.furSpeed=s.furSpeed,o.furColor=s.furColor,o.diffuseTexture=s.diffuseTexture,o.furOffset=r/t,o.furTexture=s.furTexture,o.highLevelFur=s.highLevelFur,o.furTime=s.furTime,o.furDensity=s.furDensity;var a=i.clone(i.name+r);a.material=o,a.skeleton=i.skeleton,a.position=e.Vector3.Zero(),f.push(a)}for(r=1;r<f.length;r++)f[r].parent=i;return i.material._meshes=f,f},n})(e.Material);__decorate([e.serializeAsTexture()],t.prototype,"diffuseTexture",void 0),__decorate([e.serializeAsTexture()],t.prototype,"heightTexture",void 0),__decorate([e.serializeAsColor3()],t.prototype,"diffuseColor",void 0),__decorate([e.serialize()],t.prototype,"furLength",void 0),__decorate([e.serialize()],t.prototype,"furAngle",void 0),__decorate([e.serializeAsColor3()],t.prototype,"furColor",void 0),__decorate([e.serialize()],t.prototype,"furOffset",void 0),__decorate([e.serialize()],t.prototype,"furSpacing",void 0),__decorate([e.serializeAsVector3()],t.prototype,"furGravity",void 0),__decorate([e.serialize()],t.prototype,"furSpeed",void 0),__decorate([e.serialize()],t.prototype,"furDensity",void 0),__decorate([e.serialize()],t.prototype,"disableLighting",void 0),__decorate([e.serialize()],t.prototype,"highLevelFur",void 0),__decorate([e.serialize()],t.prototype,"maxSimultaneousLights",void 0),__decorate([e.serialize()],t.prototype,"furTime",null),e.FurMaterial=t})(BABYLON||(BABYLON={})),BABYLON.Effect.ShadersStore.furVertexShader="precision highp float;\n\nattribute vec3 position;\nattribute vec3 normal;\n#ifdef UV1\nattribute vec2 uv;\n#endif\n#ifdef UV2\nattribute vec2 uv2;\n#endif\n#ifdef VERTEXCOLOR\nattribute vec4 color;\n#endif\n#include<bonesDeclaration>\n\nuniform float furLength;\nuniform float furAngle;\n#ifdef HIGHLEVEL\nuniform float furOffset;\nuniform vec3 furGravity;\nuniform float furTime;\nuniform float furSpacing;\nuniform float furDensity;\n#endif\n#ifdef HEIGHTMAP\nuniform sampler2D heightTexture;\n#endif\n#ifdef HIGHLEVEL\nvarying vec2 vFurUV;\n#endif\n#include<instancesDeclaration>\nuniform mat4 view;\nuniform mat4 viewProjection;\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform mat4 diffuseMatrix;\nuniform vec2 vDiffuseInfos;\n#endif\n#ifdef POINTSIZE\nuniform float pointSize;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\nvarying float vfur_length;\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\n#include<shadowsVertexDeclaration>[0..maxSimultaneousLights]\nfloat Rand(vec3 rv) {\nfloat x=dot(rv,vec3(12.9898,78.233,24.65487));\nreturn fract(sin(x)*43758.5453);\n}\nvoid main(void) {\n#include<instancesVertex>\n#include<bonesVertex>\n\nfloat r=Rand(position);\n#ifdef HEIGHTMAP \nvfur_length=furLength*texture2D(heightTexture,uv).rgb.x;\n#else \nvfur_length=(furLength*r);\n#endif\nvec3 tangent1=vec3(normal.y,-normal.x,0);\nvec3 tangent2=vec3(-normal.z,0,normal.x);\nr=Rand(tangent1*r);\nfloat J=(2.0+4.0*r);\nr=Rand(tangent2*r);\nfloat K=(2.0+2.0*r);\ntangent1=tangent1*J+tangent2*K;\ntangent1=normalize(tangent1);\nvec3 newPosition=position+normal*vfur_length*cos(furAngle)+tangent1*vfur_length*sin(furAngle);\n#ifdef HIGHLEVEL\n\nvec3 forceDirection=vec3(0.0,0.0,0.0);\nforceDirection.x=sin(furTime+position.x*0.05)*0.2;\nforceDirection.y=cos(furTime*0.7+position.y*0.04)*0.2;\nforceDirection.z=sin(furTime*0.7+position.z*0.04)*0.2;\nvec3 displacement=vec3(0.0,0.0,0.0);\ndisplacement=furGravity+forceDirection;\nfloat displacementFactor=pow(furOffset,3.0);\nvec3 aNormal=normal;\naNormal.xyz+=displacement*displacementFactor;\nnewPosition=vec3(newPosition.x,newPosition.y,newPosition.z)+(normalize(aNormal)*furOffset*furSpacing);\n#endif\n#ifdef NORMAL\n#ifdef HIGHLEVEL\nvNormalW=normalize(vec3(finalWorld*vec4(normal,0.0))*aNormal);\n#else\nvNormalW=normalize(vec3(finalWorld*vec4(normal,0.0)));\n#endif\n#endif\n\ngl_Position=viewProjection*finalWorld*vec4(newPosition,1.0);\nvec4 worldPos=finalWorld*vec4(newPosition,1.0);\nvPositionW=vec3(worldPos);\n\n#ifndef UV1\nvec2 uv=vec2(0.,0.);\n#endif\n#ifndef UV2\nvec2 uv2=vec2(0.,0.);\n#endif\n#ifdef DIFFUSE\nif (vDiffuseInfos.x == 0.)\n{\nvDiffuseUV=vec2(diffuseMatrix*vec4(uv,1.0,0.0));\n}\nelse\n{\nvDiffuseUV=vec2(diffuseMatrix*vec4(uv2,1.0,0.0));\n}\n#ifdef HIGHLEVEL\nvFurUV=vDiffuseUV*furDensity;\n#endif\n#else\n#ifdef HIGHLEVEL\nvFurUV=uv*furDensity;\n#endif\n#endif\n\n#include<clipPlaneVertex>\n\n#include<fogVertex>\n\n#include<shadowsVertex>[0..maxSimultaneousLights]\n\n#ifdef VERTEXCOLOR\nvColor=color;\n#endif\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\n}\n",BABYLON.Effect.ShadersStore.furPixelShader="precision highp float;\n\nuniform vec3 vEyePosition;\nuniform vec4 vDiffuseColor;\n\nuniform vec4 furColor;\nuniform float furLength;\nvarying vec3 vPositionW;\nvarying float vfur_length;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#include<lightFragmentDeclaration>[0..maxSimultaneousLights]\n\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform sampler2D diffuseSampler;\nuniform vec2 vDiffuseInfos;\n#endif\n\n#ifdef HIGHLEVEL\nuniform float furOffset;\nuniform sampler2D furTexture;\nvarying vec2 vFurUV;\n#endif\n#include<lightsFragmentFunctions>\n#include<shadowsFragmentFunctions>\n#include<fogFragmentDeclaration>\n#include<clipPlaneFragmentDeclaration>\nfloat Rand(vec3 rv) {\nfloat x=dot(rv,vec3(12.9898,78.233,24.65487));\nreturn fract(sin(x)*43758.5453);\n}\nvoid main(void) {\n\n#include<clipPlaneFragment>\nvec3 viewDirectionW=normalize(vEyePosition-vPositionW);\n\nvec4 baseColor=furColor;\nvec3 diffuseColor=vDiffuseColor.rgb;\n\nfloat alpha=vDiffuseColor.a;\n#ifdef DIFFUSE\nbaseColor*=texture2D(diffuseSampler,vDiffuseUV);\n#ifdef ALPHATEST\nif (baseColor.a<0.4)\ndiscard;\n#endif\nbaseColor.rgb*=vDiffuseInfos.y;\n#endif\n#ifdef VERTEXCOLOR\nbaseColor.rgb*=vColor.rgb;\n#endif\n\n#ifdef NORMAL\nvec3 normalW=normalize(vNormalW);\n#else\nvec3 normalW=vec3(1.0,1.0,1.0);\n#endif\n#ifdef HIGHLEVEL\n\nvec4 furTextureColor=texture2D(furTexture,vec2(vFurUV.x,vFurUV.y));\nif (furTextureColor.a<=0.0 || furTextureColor.g<furOffset) {\ndiscard;\n}\nfloat occlusion=mix(0.0,furTextureColor.b*1.2,furOffset);\nbaseColor=vec4(baseColor.xyz*occlusion,1.1-furOffset);\n#endif\n\nvec3 diffuseBase=vec3(0.,0.,0.);\nlightingInfo info;\nfloat shadow=1.;\nfloat glossiness=0.;\n#include<lightFragment>[0..maxSimultaneousLights]\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\nvec3 finalDiffuse=clamp(diffuseBase.rgb*baseColor.rgb,0.0,1.0);\n\n#ifdef HIGHLEVEL\nvec4 color=vec4(finalDiffuse,alpha);\n#else\nfloat r=vfur_length/furLength*0.5;\nvec4 color=vec4(finalDiffuse*(0.5+r),alpha);\n#endif\n#include<fogFragment>\ngl_FragColor=color;\n}";