babylon.gradientMaterial.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
  4. switch (arguments.length) {
  5. case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
  6. case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
  7. case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
  8. }
  9. };
  10. var BABYLON;
  11. (function (BABYLON) {
  12. var maxSimultaneousLights = 4;
  13. var GradientMaterialDefines = (function (_super) {
  14. __extends(GradientMaterialDefines, _super);
  15. function GradientMaterialDefines() {
  16. _super.call(this);
  17. this.DIFFUSE = false;
  18. this.CLIPPLANE = false;
  19. this.ALPHATEST = false;
  20. this.POINTSIZE = false;
  21. this.FOG = false;
  22. this.LIGHT0 = false;
  23. this.LIGHT1 = false;
  24. this.LIGHT2 = false;
  25. this.LIGHT3 = false;
  26. this.SPOTLIGHT0 = false;
  27. this.SPOTLIGHT1 = false;
  28. this.SPOTLIGHT2 = false;
  29. this.SPOTLIGHT3 = false;
  30. this.HEMILIGHT0 = false;
  31. this.HEMILIGHT1 = false;
  32. this.HEMILIGHT2 = false;
  33. this.HEMILIGHT3 = false;
  34. this.DIRLIGHT0 = false;
  35. this.DIRLIGHT1 = false;
  36. this.DIRLIGHT2 = false;
  37. this.DIRLIGHT3 = false;
  38. this.POINTLIGHT0 = false;
  39. this.POINTLIGHT1 = false;
  40. this.POINTLIGHT2 = false;
  41. this.POINTLIGHT3 = false;
  42. this.SHADOW0 = false;
  43. this.SHADOW1 = false;
  44. this.SHADOW2 = false;
  45. this.SHADOW3 = false;
  46. this.SHADOWS = false;
  47. this.SHADOWVSM0 = false;
  48. this.SHADOWVSM1 = false;
  49. this.SHADOWVSM2 = false;
  50. this.SHADOWVSM3 = false;
  51. this.SHADOWPCF0 = false;
  52. this.SHADOWPCF1 = false;
  53. this.SHADOWPCF2 = false;
  54. this.SHADOWPCF3 = false;
  55. this.NORMAL = false;
  56. this.UV1 = false;
  57. this.UV2 = false;
  58. this.VERTEXCOLOR = false;
  59. this.VERTEXALPHA = false;
  60. this.NUM_BONE_INFLUENCERS = 0;
  61. this.BonesPerMesh = 0;
  62. this.INSTANCES = false;
  63. this._keys = Object.keys(this);
  64. }
  65. return GradientMaterialDefines;
  66. })(BABYLON.MaterialDefines);
  67. var GradientMaterial = (function (_super) {
  68. __extends(GradientMaterial, _super);
  69. function GradientMaterial(name, scene) {
  70. _super.call(this, name, scene);
  71. // The gradient top color, red by default
  72. this.topColor = new BABYLON.Color3(1, 0, 0);
  73. this.topColorAlpha = 1.0;
  74. // The gradient top color, blue by default
  75. this.bottomColor = new BABYLON.Color3(0, 0, 1);
  76. this.bottomColorAlpha = 1.0;
  77. // Gradient offset
  78. this.offset = 0;
  79. this.smoothness = 1.0;
  80. this.disableLighting = false;
  81. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  82. this._scaledDiffuse = new BABYLON.Color3();
  83. this._defines = new GradientMaterialDefines();
  84. this._cachedDefines = new GradientMaterialDefines();
  85. this._cachedDefines.BonesPerMesh = -1;
  86. }
  87. GradientMaterial.prototype.needAlphaBlending = function () {
  88. return (this.alpha < 1.0 || this.topColorAlpha < 1.0 || this.bottomColorAlpha < 1.0);
  89. };
  90. GradientMaterial.prototype.needAlphaTesting = function () {
  91. return true;
  92. };
  93. GradientMaterial.prototype.getAlphaTestTexture = function () {
  94. return null;
  95. };
  96. // Methods
  97. GradientMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  98. if (!mesh) {
  99. return true;
  100. }
  101. if (this._defines.INSTANCES !== useInstances) {
  102. return false;
  103. }
  104. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  105. return true;
  106. }
  107. return false;
  108. };
  109. GradientMaterial.prototype.isReady = function (mesh, useInstances) {
  110. if (this.checkReadyOnlyOnce) {
  111. if (this._wasPreviouslyReady) {
  112. return true;
  113. }
  114. }
  115. var scene = this.getScene();
  116. if (!this.checkReadyOnEveryCall) {
  117. if (this._renderId === scene.getRenderId()) {
  118. if (this._checkCache(scene, mesh, useInstances)) {
  119. return true;
  120. }
  121. }
  122. }
  123. var engine = scene.getEngine();
  124. var needNormals = false;
  125. var needUVs = false;
  126. this._defines.reset();
  127. // No textures
  128. // Effect
  129. if (scene.clipPlane) {
  130. this._defines.CLIPPLANE = true;
  131. }
  132. if (engine.getAlphaTesting()) {
  133. this._defines.ALPHATEST = true;
  134. }
  135. // Point size
  136. if (this.pointsCloud || scene.forcePointsCloud) {
  137. this._defines.POINTSIZE = true;
  138. }
  139. // Fog
  140. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  141. this._defines.FOG = true;
  142. }
  143. var lightIndex = 0;
  144. if (scene.lightsEnabled && !this.disableLighting) {
  145. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines);
  146. }
  147. // Attribs
  148. if (mesh) {
  149. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  150. this._defines.NORMAL = true;
  151. }
  152. if (needUVs) {
  153. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  154. this._defines.UV1 = true;
  155. }
  156. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  157. this._defines.UV2 = true;
  158. }
  159. }
  160. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  161. this._defines.VERTEXCOLOR = true;
  162. if (mesh.hasVertexAlpha) {
  163. this._defines.VERTEXALPHA = true;
  164. }
  165. }
  166. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  167. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  168. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  169. }
  170. // Instances
  171. if (useInstances) {
  172. this._defines.INSTANCES = true;
  173. }
  174. }
  175. // Get correct effect
  176. if (!this._defines.isEqual(this._cachedDefines)) {
  177. this._defines.cloneTo(this._cachedDefines);
  178. scene.resetCachedMaterial();
  179. // Fallbacks
  180. var fallbacks = new BABYLON.EffectFallbacks();
  181. if (this._defines.FOG) {
  182. fallbacks.addFallback(1, "FOG");
  183. }
  184. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  185. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  186. fallbacks.addCPUSkinningFallback(0, mesh);
  187. }
  188. //Attributes
  189. var attribs = [BABYLON.VertexBuffer.PositionKind];
  190. if (this._defines.NORMAL) {
  191. attribs.push(BABYLON.VertexBuffer.NormalKind);
  192. }
  193. if (this._defines.UV1) {
  194. attribs.push(BABYLON.VertexBuffer.UVKind);
  195. }
  196. if (this._defines.UV2) {
  197. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  198. }
  199. if (this._defines.VERTEXCOLOR) {
  200. attribs.push(BABYLON.VertexBuffer.ColorKind);
  201. }
  202. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  203. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  204. // Legacy browser patch
  205. var shaderName = "gradient";
  206. var join = this._defines.toString();
  207. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  208. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  209. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  210. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  211. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  212. "vFogInfos", "vFogColor", "pointSize",
  213. "vDiffuseInfos",
  214. "mBones",
  215. "vClipPlane", "diffuseMatrix",
  216. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues", "topColor", "bottomColor", "offset", "smoothness"
  217. ], ["diffuseSampler",
  218. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  219. ], join, fallbacks, this.onCompiled, this.onError);
  220. }
  221. if (!this._effect.isReady()) {
  222. return false;
  223. }
  224. this._renderId = scene.getRenderId();
  225. this._wasPreviouslyReady = true;
  226. if (mesh) {
  227. if (!mesh._materialDefines) {
  228. mesh._materialDefines = new GradientMaterialDefines();
  229. }
  230. this._defines.cloneTo(mesh._materialDefines);
  231. }
  232. return true;
  233. };
  234. GradientMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  235. this._effect.setMatrix("world", world);
  236. };
  237. GradientMaterial.prototype.bind = function (world, mesh) {
  238. var scene = this.getScene();
  239. // Matrices
  240. this.bindOnlyWorldMatrix(world);
  241. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  242. // Bones
  243. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  244. if (scene.getCachedMaterial() !== this) {
  245. // Clip plane
  246. BABYLON.MaterialHelper.BindClipPlane(this._effect, scene);
  247. // Point size
  248. if (this.pointsCloud) {
  249. this._effect.setFloat("pointSize", this.pointSize);
  250. }
  251. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  252. }
  253. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  254. if (scene.lightsEnabled && !this.disableLighting) {
  255. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
  256. }
  257. // View
  258. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  259. this._effect.setMatrix("view", scene.getViewMatrix());
  260. }
  261. // Fog
  262. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  263. this._effect.setColor4("topColor", this.topColor, this.topColorAlpha);
  264. this._effect.setColor4("bottomColor", this.bottomColor, this.bottomColorAlpha);
  265. this._effect.setFloat("offset", this.offset);
  266. this._effect.setFloat("smoothness", this.smoothness);
  267. _super.prototype.bind.call(this, world, mesh);
  268. };
  269. GradientMaterial.prototype.getAnimatables = function () {
  270. return [];
  271. };
  272. GradientMaterial.prototype.dispose = function (forceDisposeEffect) {
  273. _super.prototype.dispose.call(this, forceDisposeEffect);
  274. };
  275. GradientMaterial.prototype.clone = function (name) {
  276. var _this = this;
  277. return BABYLON.SerializationHelper.Clone(function () { return new GradientMaterial(name, _this.getScene()); }, this);
  278. };
  279. GradientMaterial.prototype.serialize = function () {
  280. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  281. serializationObject.customType = "BABYLON.GradientMaterial";
  282. return serializationObject;
  283. };
  284. // Statics
  285. GradientMaterial.Parse = function (source, scene, rootUrl) {
  286. return BABYLON.SerializationHelper.Parse(function () { return new GradientMaterial(source.name, scene); }, source, scene, rootUrl);
  287. };
  288. __decorate([
  289. BABYLON.serializeAsColor3()
  290. ], GradientMaterial.prototype, "topColor");
  291. __decorate([
  292. BABYLON.serialize()
  293. ], GradientMaterial.prototype, "topColorAlpha");
  294. __decorate([
  295. BABYLON.serializeAsColor3()
  296. ], GradientMaterial.prototype, "bottomColor");
  297. __decorate([
  298. BABYLON.serialize()
  299. ], GradientMaterial.prototype, "bottomColorAlpha");
  300. __decorate([
  301. BABYLON.serialize()
  302. ], GradientMaterial.prototype, "offset");
  303. __decorate([
  304. BABYLON.serialize()
  305. ], GradientMaterial.prototype, "smoothness");
  306. __decorate([
  307. BABYLON.serialize()
  308. ], GradientMaterial.prototype, "disableLighting");
  309. return GradientMaterial;
  310. })(BABYLON.Material);
  311. BABYLON.GradientMaterial = GradientMaterial;
  312. })(BABYLON || (BABYLON = {}));
  313. BABYLON.Effect.ShadersStore['gradientVertexShader'] = "precision highp float;\n\nattribute vec3 position;\n#ifdef NORMAL\nattribute vec3 normal;\n#endif\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\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\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\n#include<shadowsVertexDeclaration>\nvoid main(void) {\n#include<instancesVertex>\n#include<bonesVertex> \ngl_Position=viewProjection*finalWorld*vec4(position,1.0);\nvec4 worldPos=finalWorld*vec4(position,1.0);\nvPositionW=vec3(worldPos);\n#ifdef NORMAL\nvNormalW=normalize(vec3(finalWorld*vec4(normal,0.0)));\n#endif\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#endif\n\n#include<clipPlaneVertex>\n\n#include<fogVertex>\n#include<shadowsVertex>\n\n#ifdef VERTEXCOLOR\nvColor=color;\n#endif\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\n}\n";
  314. BABYLON.Effect.ShadersStore['gradientPixelShader'] = "precision highp float;\n\nuniform vec3 vEyePosition;\nuniform vec4 vDiffuseColor;\n\nuniform vec4 topColor;\nuniform vec4 bottomColor;\nuniform float offset;\nuniform float smoothness;\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#include<lightFragmentDeclaration>[0]\n#include<lightFragmentDeclaration>[1]\n#include<lightFragmentDeclaration>[2]\n#include<lightFragmentDeclaration>[3]\n#include<lightsFragmentFunctions>\n#include<shadowsFragmentFunctions>\n\n#ifdef DIFFUSE\nvarying vec2 vDiffuseUV;\nuniform sampler2D diffuseSampler;\nuniform vec2 vDiffuseInfos;\n#endif\n#include<clipPlaneFragmentDeclaration>\n\n#include<fogFragmentDeclaration>\nvoid main(void) {\n#include<clipPlaneFragment>\nvec3 viewDirectionW=normalize(vEyePosition-vPositionW);\nfloat h=normalize(vPositionW).y+offset;\nfloat mysmoothness=clamp(smoothness,0.01,max(smoothness,10.));\nvec4 baseColor=mix(bottomColor,topColor,max(pow(max(h,0.0),mysmoothness),0.0));\n\nvec3 diffuseColor=baseColor.rgb;\n\nfloat alpha=baseColor.a;\n#ifdef ALPHATEST\nif (baseColor.a<0.4)\ndiscard;\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\nvec3 diffuseBase=vec3(0.,0.,0.);\nlightingInfo info;\nfloat shadow=1.;\nfloat glossiness=0.;\n#include<lightFragment>[0]\n#include<lightFragment>[1]\n#include<lightFragment>[2]\n#include<lightFragment>[3]\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\nvec3 finalDiffuse=clamp(diffuseBase*diffuseColor,0.0,1.0)*baseColor.rgb;\n\nvec4 color=vec4(finalDiffuse,alpha);\n#include<fogFragment>\ngl_FragColor=color;\n}\n";