babylon.gradientMaterial.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. _super.call(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. }
  68. return GradientMaterialDefines;
  69. }(BABYLON.MaterialDefines));
  70. var GradientMaterial = (function (_super) {
  71. __extends(GradientMaterial, _super);
  72. function GradientMaterial(name, scene) {
  73. _super.call(this, name, scene);
  74. // The gradient top color, red by default
  75. this.topColor = new BABYLON.Color3(1, 0, 0);
  76. this.topColorAlpha = 1.0;
  77. // The gradient top color, blue by default
  78. this.bottomColor = new BABYLON.Color3(0, 0, 1);
  79. this.bottomColorAlpha = 1.0;
  80. // Gradient offset
  81. this.offset = 0;
  82. this.smoothness = 1.0;
  83. this.disableLighting = false;
  84. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  85. this._scaledDiffuse = new BABYLON.Color3();
  86. this._defines = new GradientMaterialDefines();
  87. this._cachedDefines = new GradientMaterialDefines();
  88. this._cachedDefines.BonesPerMesh = -1;
  89. }
  90. GradientMaterial.prototype.needAlphaBlending = function () {
  91. return (this.alpha < 1.0 || this.topColorAlpha < 1.0 || this.bottomColorAlpha < 1.0);
  92. };
  93. GradientMaterial.prototype.needAlphaTesting = function () {
  94. return true;
  95. };
  96. GradientMaterial.prototype.getAlphaTestTexture = function () {
  97. return null;
  98. };
  99. // Methods
  100. GradientMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  101. if (!mesh) {
  102. return true;
  103. }
  104. if (this._defines.INSTANCES !== useInstances) {
  105. return false;
  106. }
  107. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  108. return true;
  109. }
  110. return false;
  111. };
  112. GradientMaterial.prototype.isReady = function (mesh, useInstances) {
  113. if (this.checkReadyOnlyOnce) {
  114. if (this._wasPreviouslyReady) {
  115. return true;
  116. }
  117. }
  118. var scene = this.getScene();
  119. if (!this.checkReadyOnEveryCall) {
  120. if (this._renderId === scene.getRenderId()) {
  121. if (this._checkCache(scene, mesh, useInstances)) {
  122. return true;
  123. }
  124. }
  125. }
  126. var engine = scene.getEngine();
  127. var needNormals = false;
  128. var needUVs = false;
  129. this._defines.reset();
  130. // No textures
  131. // Effect
  132. if (scene.clipPlane) {
  133. this._defines.CLIPPLANE = true;
  134. }
  135. if (engine.getAlphaTesting()) {
  136. this._defines.ALPHATEST = true;
  137. }
  138. // Point size
  139. if (this.pointsCloud || scene.forcePointsCloud) {
  140. this._defines.POINTSIZE = true;
  141. }
  142. // Fog
  143. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  144. this._defines.FOG = true;
  145. }
  146. var lightIndex = 0;
  147. if (scene.lightsEnabled && !this.disableLighting) {
  148. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines);
  149. }
  150. // Attribs
  151. if (mesh) {
  152. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  153. this._defines.NORMAL = true;
  154. }
  155. if (needUVs) {
  156. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  157. this._defines.UV1 = true;
  158. }
  159. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  160. this._defines.UV2 = true;
  161. }
  162. }
  163. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  164. this._defines.VERTEXCOLOR = true;
  165. if (mesh.hasVertexAlpha) {
  166. this._defines.VERTEXALPHA = true;
  167. }
  168. }
  169. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  170. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  171. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  172. }
  173. // Instances
  174. if (useInstances) {
  175. this._defines.INSTANCES = true;
  176. }
  177. }
  178. // Get correct effect
  179. if (!this._defines.isEqual(this._cachedDefines)) {
  180. this._defines.cloneTo(this._cachedDefines);
  181. scene.resetCachedMaterial();
  182. // Fallbacks
  183. var fallbacks = new BABYLON.EffectFallbacks();
  184. if (this._defines.FOG) {
  185. fallbacks.addFallback(1, "FOG");
  186. }
  187. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  188. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  189. fallbacks.addCPUSkinningFallback(0, mesh);
  190. }
  191. //Attributes
  192. var attribs = [BABYLON.VertexBuffer.PositionKind];
  193. if (this._defines.NORMAL) {
  194. attribs.push(BABYLON.VertexBuffer.NormalKind);
  195. }
  196. if (this._defines.UV1) {
  197. attribs.push(BABYLON.VertexBuffer.UVKind);
  198. }
  199. if (this._defines.UV2) {
  200. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  201. }
  202. if (this._defines.VERTEXCOLOR) {
  203. attribs.push(BABYLON.VertexBuffer.ColorKind);
  204. }
  205. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  206. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  207. // Legacy browser patch
  208. var shaderName = "gradient";
  209. var join = this._defines.toString();
  210. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  211. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  212. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  213. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  214. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  215. "vFogInfos", "vFogColor", "pointSize",
  216. "vDiffuseInfos",
  217. "mBones",
  218. "vClipPlane", "diffuseMatrix",
  219. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues", "topColor", "bottomColor", "offset", "smoothness"
  220. ], ["diffuseSampler",
  221. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  222. ], join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: 4 });
  223. }
  224. if (!this._effect.isReady()) {
  225. return false;
  226. }
  227. this._renderId = scene.getRenderId();
  228. this._wasPreviouslyReady = true;
  229. if (mesh) {
  230. if (!mesh._materialDefines) {
  231. mesh._materialDefines = new GradientMaterialDefines();
  232. }
  233. this._defines.cloneTo(mesh._materialDefines);
  234. }
  235. return true;
  236. };
  237. GradientMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  238. this._effect.setMatrix("world", world);
  239. };
  240. GradientMaterial.prototype.bind = function (world, mesh) {
  241. var scene = this.getScene();
  242. // Matrices
  243. this.bindOnlyWorldMatrix(world);
  244. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  245. // Bones
  246. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  247. if (scene.getCachedMaterial() !== this) {
  248. // Clip plane
  249. BABYLON.MaterialHelper.BindClipPlane(this._effect, scene);
  250. // Point size
  251. if (this.pointsCloud) {
  252. this._effect.setFloat("pointSize", this.pointSize);
  253. }
  254. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  255. }
  256. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  257. if (scene.lightsEnabled && !this.disableLighting) {
  258. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
  259. }
  260. // View
  261. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  262. this._effect.setMatrix("view", scene.getViewMatrix());
  263. }
  264. // Fog
  265. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  266. this._effect.setColor4("topColor", this.topColor, this.topColorAlpha);
  267. this._effect.setColor4("bottomColor", this.bottomColor, this.bottomColorAlpha);
  268. this._effect.setFloat("offset", this.offset);
  269. this._effect.setFloat("smoothness", this.smoothness);
  270. _super.prototype.bind.call(this, world, mesh);
  271. };
  272. GradientMaterial.prototype.getAnimatables = function () {
  273. return [];
  274. };
  275. GradientMaterial.prototype.dispose = function (forceDisposeEffect) {
  276. _super.prototype.dispose.call(this, forceDisposeEffect);
  277. };
  278. GradientMaterial.prototype.clone = function (name) {
  279. var _this = this;
  280. return BABYLON.SerializationHelper.Clone(function () { return new GradientMaterial(name, _this.getScene()); }, this);
  281. };
  282. GradientMaterial.prototype.serialize = function () {
  283. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  284. serializationObject.customType = "BABYLON.GradientMaterial";
  285. return serializationObject;
  286. };
  287. // Statics
  288. GradientMaterial.Parse = function (source, scene, rootUrl) {
  289. return BABYLON.SerializationHelper.Parse(function () { return new GradientMaterial(source.name, scene); }, source, scene, rootUrl);
  290. };
  291. __decorate([
  292. BABYLON.serializeAsColor3()
  293. ], GradientMaterial.prototype, "topColor", void 0);
  294. __decorate([
  295. BABYLON.serialize()
  296. ], GradientMaterial.prototype, "topColorAlpha", void 0);
  297. __decorate([
  298. BABYLON.serializeAsColor3()
  299. ], GradientMaterial.prototype, "bottomColor", void 0);
  300. __decorate([
  301. BABYLON.serialize()
  302. ], GradientMaterial.prototype, "bottomColorAlpha", void 0);
  303. __decorate([
  304. BABYLON.serialize()
  305. ], GradientMaterial.prototype, "offset", void 0);
  306. __decorate([
  307. BABYLON.serialize()
  308. ], GradientMaterial.prototype, "smoothness", void 0);
  309. __decorate([
  310. BABYLON.serialize()
  311. ], GradientMaterial.prototype, "disableLighting", void 0);
  312. return GradientMaterial;
  313. }(BABYLON.Material));
  314. BABYLON.GradientMaterial = GradientMaterial;
  315. })(BABYLON || (BABYLON = {}));
  316. //# sourceMappingURL=babylon.gradientMaterial.js.map
  317. 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";
  318. 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";