babylon.gradientMaterial.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. "use strict";
  2. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  3. var __extends = (this && this.__extends) || (function () {
  4. var extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return function (d, b) {
  8. extendStatics(d, b);
  9. function __() { this.constructor = d; }
  10. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  11. };
  12. })();
  13. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  14. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  15. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  16. 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;
  17. return c > 3 && r && Object.defineProperty(target, key, r), r;
  18. };
  19. var BABYLON;
  20. (function (BABYLON) {
  21. var GradientMaterialDefines = /** @class */ (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.DEPTHPREPASS = false;
  29. _this.POINTSIZE = false;
  30. _this.FOG = false;
  31. _this.LIGHT0 = false;
  32. _this.LIGHT1 = false;
  33. _this.LIGHT2 = false;
  34. _this.LIGHT3 = false;
  35. _this.SPOTLIGHT0 = false;
  36. _this.SPOTLIGHT1 = false;
  37. _this.SPOTLIGHT2 = false;
  38. _this.SPOTLIGHT3 = false;
  39. _this.HEMILIGHT0 = false;
  40. _this.HEMILIGHT1 = false;
  41. _this.HEMILIGHT2 = false;
  42. _this.HEMILIGHT3 = false;
  43. _this.DIRLIGHT0 = false;
  44. _this.DIRLIGHT1 = false;
  45. _this.DIRLIGHT2 = false;
  46. _this.DIRLIGHT3 = false;
  47. _this.POINTLIGHT0 = false;
  48. _this.POINTLIGHT1 = false;
  49. _this.POINTLIGHT2 = false;
  50. _this.POINTLIGHT3 = false;
  51. _this.SHADOW0 = false;
  52. _this.SHADOW1 = false;
  53. _this.SHADOW2 = false;
  54. _this.SHADOW3 = false;
  55. _this.SHADOWS = false;
  56. _this.SHADOWESM0 = false;
  57. _this.SHADOWESM1 = false;
  58. _this.SHADOWESM2 = false;
  59. _this.SHADOWESM3 = false;
  60. _this.SHADOWPOISSON0 = false;
  61. _this.SHADOWPOISSON1 = false;
  62. _this.SHADOWPOISSON2 = false;
  63. _this.SHADOWPOISSON3 = false;
  64. _this.SHADOWPCF0 = false;
  65. _this.SHADOWPCF1 = false;
  66. _this.SHADOWPCF2 = false;
  67. _this.SHADOWPCF3 = false;
  68. _this.SHADOWPCSS0 = false;
  69. _this.SHADOWPCSS1 = false;
  70. _this.SHADOWPCSS2 = false;
  71. _this.SHADOWPCSS3 = false;
  72. _this.NORMAL = false;
  73. _this.UV1 = false;
  74. _this.UV2 = false;
  75. _this.VERTEXCOLOR = false;
  76. _this.VERTEXALPHA = false;
  77. _this.NUM_BONE_INFLUENCERS = 0;
  78. _this.BonesPerMesh = 0;
  79. _this.INSTANCES = false;
  80. _this.rebuild();
  81. return _this;
  82. }
  83. return GradientMaterialDefines;
  84. }(BABYLON.MaterialDefines));
  85. var GradientMaterial = /** @class */ (function (_super) {
  86. __extends(GradientMaterial, _super);
  87. function GradientMaterial(name, scene) {
  88. var _this = _super.call(this, name, scene) || this;
  89. _this._maxSimultaneousLights = 4;
  90. // The gradient top color, red by default
  91. _this.topColor = new BABYLON.Color3(1, 0, 0);
  92. _this.topColorAlpha = 1.0;
  93. // The gradient top color, blue by default
  94. _this.bottomColor = new BABYLON.Color3(0, 0, 1);
  95. _this.bottomColorAlpha = 1.0;
  96. // Gradient offset
  97. _this.offset = 0;
  98. _this.smoothness = 1.0;
  99. _this.disableLighting = false;
  100. _this._scaledDiffuse = new BABYLON.Color3();
  101. return _this;
  102. }
  103. GradientMaterial.prototype.needAlphaBlending = function () {
  104. return (this.alpha < 1.0 || this.topColorAlpha < 1.0 || this.bottomColorAlpha < 1.0);
  105. };
  106. GradientMaterial.prototype.needAlphaTesting = function () {
  107. return true;
  108. };
  109. GradientMaterial.prototype.getAlphaTestTexture = function () {
  110. return null;
  111. };
  112. // Methods
  113. GradientMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  114. if (this.isFrozen) {
  115. if (this._wasPreviouslyReady && subMesh.effect) {
  116. return true;
  117. }
  118. }
  119. if (!subMesh._materialDefines) {
  120. subMesh._materialDefines = new GradientMaterialDefines();
  121. }
  122. var defines = subMesh._materialDefines;
  123. var scene = this.getScene();
  124. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  125. if (this._renderId === scene.getRenderId()) {
  126. return true;
  127. }
  128. }
  129. var engine = scene.getEngine();
  130. BABYLON.MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  131. BABYLON.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  132. defines._needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights);
  133. // Attribs
  134. BABYLON.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  135. // Get correct effect
  136. if (defines.isDirty) {
  137. defines.markAsProcessed();
  138. scene.resetCachedMaterial();
  139. // Fallbacks
  140. var fallbacks = new BABYLON.EffectFallbacks();
  141. if (defines.FOG) {
  142. fallbacks.addFallback(1, "FOG");
  143. }
  144. BABYLON.MaterialHelper.HandleFallbacksForShadows(defines, fallbacks);
  145. if (defines.NUM_BONE_INFLUENCERS > 0) {
  146. fallbacks.addCPUSkinningFallback(0, mesh);
  147. }
  148. //Attributes
  149. var attribs = [BABYLON.VertexBuffer.PositionKind];
  150. if (defines.NORMAL) {
  151. attribs.push(BABYLON.VertexBuffer.NormalKind);
  152. }
  153. if (defines.UV1) {
  154. attribs.push(BABYLON.VertexBuffer.UVKind);
  155. }
  156. if (defines.UV2) {
  157. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  158. }
  159. if (defines.VERTEXCOLOR) {
  160. attribs.push(BABYLON.VertexBuffer.ColorKind);
  161. }
  162. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  163. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  164. // Legacy browser patch
  165. var shaderName = "gradient";
  166. var join = defines.toString();
  167. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  168. "vFogInfos", "vFogColor", "pointSize",
  169. "vDiffuseInfos",
  170. "mBones",
  171. "vClipPlane", "diffuseMatrix",
  172. "topColor", "bottomColor", "offset", "smoothness"
  173. ];
  174. var samplers = ["diffuseSampler"];
  175. var uniformBuffers = new Array();
  176. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList({
  177. uniformsNames: uniforms,
  178. uniformBuffersNames: uniformBuffers,
  179. samplers: samplers,
  180. defines: defines,
  181. maxSimultaneousLights: 4
  182. });
  183. subMesh.setEffect(scene.getEngine().createEffect(shaderName, {
  184. attributes: attribs,
  185. uniformsNames: uniforms,
  186. uniformBuffersNames: uniformBuffers,
  187. samplers: samplers,
  188. defines: join,
  189. fallbacks: fallbacks,
  190. onCompiled: this.onCompiled,
  191. onError: this.onError,
  192. indexParameters: { maxSimultaneousLights: 4 }
  193. }, engine), defines);
  194. }
  195. if (!subMesh.effect || !subMesh.effect.isReady()) {
  196. return false;
  197. }
  198. this._renderId = scene.getRenderId();
  199. this._wasPreviouslyReady = true;
  200. return true;
  201. };
  202. GradientMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  203. var scene = this.getScene();
  204. var defines = subMesh._materialDefines;
  205. if (!defines) {
  206. return;
  207. }
  208. var effect = subMesh.effect;
  209. if (!effect) {
  210. return;
  211. }
  212. this._activeEffect = effect;
  213. // Matrices
  214. this.bindOnlyWorldMatrix(world);
  215. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  216. // Bones
  217. BABYLON.MaterialHelper.BindBonesParameters(mesh, effect);
  218. if (this._mustRebind(scene, effect)) {
  219. // Clip plane
  220. BABYLON.MaterialHelper.BindClipPlane(effect, scene);
  221. // Point size
  222. if (this.pointsCloud) {
  223. this._activeEffect.setFloat("pointSize", this.pointSize);
  224. }
  225. BABYLON.MaterialHelper.BindEyePosition(effect, scene);
  226. }
  227. this._activeEffect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  228. if (scene.lightsEnabled && !this.disableLighting) {
  229. BABYLON.MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines);
  230. }
  231. // View
  232. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  233. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  234. }
  235. // Fog
  236. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  237. this._activeEffect.setColor4("topColor", this.topColor, this.topColorAlpha);
  238. this._activeEffect.setColor4("bottomColor", this.bottomColor, this.bottomColorAlpha);
  239. this._activeEffect.setFloat("offset", this.offset);
  240. this._activeEffect.setFloat("smoothness", this.smoothness);
  241. this._afterBind(mesh, this._activeEffect);
  242. };
  243. GradientMaterial.prototype.getAnimatables = function () {
  244. return [];
  245. };
  246. GradientMaterial.prototype.dispose = function (forceDisposeEffect) {
  247. _super.prototype.dispose.call(this, forceDisposeEffect);
  248. };
  249. GradientMaterial.prototype.clone = function (name) {
  250. var _this = this;
  251. return BABYLON.SerializationHelper.Clone(function () { return new GradientMaterial(name, _this.getScene()); }, this);
  252. };
  253. GradientMaterial.prototype.serialize = function () {
  254. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  255. serializationObject.customType = "BABYLON.GradientMaterial";
  256. return serializationObject;
  257. };
  258. GradientMaterial.prototype.getClassName = function () {
  259. return "GradientMaterial";
  260. };
  261. // Statics
  262. GradientMaterial.Parse = function (source, scene, rootUrl) {
  263. return BABYLON.SerializationHelper.Parse(function () { return new GradientMaterial(source.name, scene); }, source, scene, rootUrl);
  264. };
  265. __decorate([
  266. BABYLON.serialize("maxSimultaneousLights")
  267. ], GradientMaterial.prototype, "_maxSimultaneousLights", void 0);
  268. __decorate([
  269. BABYLON.expandToProperty("_markAllSubMeshesAsLightsDirty")
  270. ], GradientMaterial.prototype, "maxSimultaneousLights", void 0);
  271. __decorate([
  272. BABYLON.serializeAsColor3()
  273. ], GradientMaterial.prototype, "topColor", void 0);
  274. __decorate([
  275. BABYLON.serialize()
  276. ], GradientMaterial.prototype, "topColorAlpha", void 0);
  277. __decorate([
  278. BABYLON.serializeAsColor3()
  279. ], GradientMaterial.prototype, "bottomColor", void 0);
  280. __decorate([
  281. BABYLON.serialize()
  282. ], GradientMaterial.prototype, "bottomColorAlpha", void 0);
  283. __decorate([
  284. BABYLON.serialize()
  285. ], GradientMaterial.prototype, "offset", void 0);
  286. __decorate([
  287. BABYLON.serialize()
  288. ], GradientMaterial.prototype, "smoothness", void 0);
  289. __decorate([
  290. BABYLON.serialize()
  291. ], GradientMaterial.prototype, "disableLighting", void 0);
  292. return GradientMaterial;
  293. }(BABYLON.PushMaterial));
  294. BABYLON.GradientMaterial = GradientMaterial;
  295. })(BABYLON || (BABYLON = {}));
  296. //# sourceMappingURL=babylon.gradientMaterial.js.map
  297. 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<__decl__lightFragment>[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";
  298. 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<helperFunctions>\n\n#include<__decl__lightFragment>[0]\n#include<__decl__lightFragment>[1]\n#include<__decl__lightFragment>[2]\n#include<__decl__lightFragment>[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#include<depthPrePass>\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";