babylon.gradientMaterial.js 18 KB

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