babylon.gradientMaterial.js 18 KB

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