babylon.gradientMaterial.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 GradientMaterialDefines = /** @class */ (function (_super) {
  21. __extends(GradientMaterialDefines, _super);
  22. function GradientMaterialDefines() {
  23. var _this = _super.call(this) || this;
  24. _this.DIFFUSE = false;
  25. _this.CLIPPLANE = false;
  26. _this.ALPHATEST = false;
  27. _this.DEPTHPREPASS = 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.SHADOWPOISSON0 = false;
  60. _this.SHADOWPOISSON1 = false;
  61. _this.SHADOWPOISSON2 = false;
  62. _this.SHADOWPOISSON3 = false;
  63. _this.SHADOWPCF0 = false;
  64. _this.SHADOWPCF1 = false;
  65. _this.SHADOWPCF2 = false;
  66. _this.SHADOWPCF3 = false;
  67. _this.SHADOWPCSS0 = false;
  68. _this.SHADOWPCSS1 = false;
  69. _this.SHADOWPCSS2 = false;
  70. _this.SHADOWPCSS3 = false;
  71. _this.NORMAL = false;
  72. _this.UV1 = false;
  73. _this.UV2 = false;
  74. _this.VERTEXCOLOR = false;
  75. _this.VERTEXALPHA = false;
  76. _this.NUM_BONE_INFLUENCERS = 0;
  77. _this.BonesPerMesh = 0;
  78. _this.INSTANCES = false;
  79. _this.rebuild();
  80. return _this;
  81. }
  82. return GradientMaterialDefines;
  83. }(BABYLON.MaterialDefines));
  84. var GradientMaterial = /** @class */ (function (_super) {
  85. __extends(GradientMaterial, _super);
  86. function GradientMaterial(name, scene) {
  87. var _this = _super.call(this, name, scene) || this;
  88. _this._maxSimultaneousLights = 4;
  89. // The gradient top color, red by default
  90. _this.topColor = new BABYLON.Color3(1, 0, 0);
  91. _this.topColorAlpha = 1.0;
  92. // The gradient top color, blue by default
  93. _this.bottomColor = new BABYLON.Color3(0, 0, 1);
  94. _this.bottomColorAlpha = 1.0;
  95. // Gradient offset
  96. _this.offset = 0;
  97. _this.smoothness = 1.0;
  98. _this.disableLighting = false;
  99. _this._scaledDiffuse = new BABYLON.Color3();
  100. return _this;
  101. }
  102. GradientMaterial.prototype.needAlphaBlending = function () {
  103. return (this.alpha < 1.0 || this.topColorAlpha < 1.0 || this.bottomColorAlpha < 1.0);
  104. };
  105. GradientMaterial.prototype.needAlphaTesting = function () {
  106. return true;
  107. };
  108. GradientMaterial.prototype.getAlphaTestTexture = function () {
  109. return null;
  110. };
  111. // Methods
  112. GradientMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  113. if (this.isFrozen) {
  114. if (this._wasPreviouslyReady && subMesh.effect) {
  115. return true;
  116. }
  117. }
  118. if (!subMesh._materialDefines) {
  119. subMesh._materialDefines = new GradientMaterialDefines();
  120. }
  121. var defines = subMesh._materialDefines;
  122. var scene = this.getScene();
  123. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  124. if (this._renderId === scene.getRenderId()) {
  125. return true;
  126. }
  127. }
  128. var engine = scene.getEngine();
  129. BABYLON.MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  130. BABYLON.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  131. defines._needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights);
  132. // Attribs
  133. BABYLON.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  134. // Get correct effect
  135. if (defines.isDirty) {
  136. defines.markAsProcessed();
  137. scene.resetCachedMaterial();
  138. // Fallbacks
  139. var fallbacks = new BABYLON.EffectFallbacks();
  140. if (defines.FOG) {
  141. fallbacks.addFallback(1, "FOG");
  142. }
  143. BABYLON.MaterialHelper.HandleFallbacksForShadows(defines, fallbacks);
  144. if (defines.NUM_BONE_INFLUENCERS > 0) {
  145. fallbacks.addCPUSkinningFallback(0, mesh);
  146. }
  147. //Attributes
  148. var attribs = [BABYLON.VertexBuffer.PositionKind];
  149. if (defines.NORMAL) {
  150. attribs.push(BABYLON.VertexBuffer.NormalKind);
  151. }
  152. if (defines.UV1) {
  153. attribs.push(BABYLON.VertexBuffer.UVKind);
  154. }
  155. if (defines.UV2) {
  156. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  157. }
  158. if (defines.VERTEXCOLOR) {
  159. attribs.push(BABYLON.VertexBuffer.ColorKind);
  160. }
  161. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  162. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  163. // Legacy browser patch
  164. var shaderName = "gradient";
  165. var join = defines.toString();
  166. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  167. "vFogInfos", "vFogColor", "pointSize",
  168. "vDiffuseInfos",
  169. "mBones",
  170. "vClipPlane", "diffuseMatrix",
  171. "topColor", "bottomColor", "offset", "smoothness"
  172. ];
  173. var samplers = ["diffuseSampler"];
  174. var uniformBuffers = new Array();
  175. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList({
  176. uniformsNames: uniforms,
  177. uniformBuffersNames: uniformBuffers,
  178. samplers: samplers,
  179. defines: defines,
  180. maxSimultaneousLights: 4
  181. });
  182. subMesh.setEffect(scene.getEngine().createEffect(shaderName, {
  183. attributes: attribs,
  184. uniformsNames: uniforms,
  185. uniformBuffersNames: uniformBuffers,
  186. samplers: samplers,
  187. defines: join,
  188. fallbacks: fallbacks,
  189. onCompiled: this.onCompiled,
  190. onError: this.onError,
  191. indexParameters: { maxSimultaneousLights: 4 }
  192. }, engine), defines);
  193. }
  194. if (!subMesh.effect || !subMesh.effect.isReady()) {
  195. return false;
  196. }
  197. this._renderId = scene.getRenderId();
  198. this._wasPreviouslyReady = true;
  199. return true;
  200. };
  201. GradientMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  202. var scene = this.getScene();
  203. var defines = subMesh._materialDefines;
  204. if (!defines) {
  205. return;
  206. }
  207. var effect = subMesh.effect;
  208. if (!effect) {
  209. return;
  210. }
  211. this._activeEffect = effect;
  212. // Matrices
  213. this.bindOnlyWorldMatrix(world);
  214. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  215. // Bones
  216. BABYLON.MaterialHelper.BindBonesParameters(mesh, effect);
  217. if (this._mustRebind(scene, effect)) {
  218. // Clip plane
  219. BABYLON.MaterialHelper.BindClipPlane(effect, scene);
  220. // Point size
  221. if (this.pointsCloud) {
  222. this._activeEffect.setFloat("pointSize", this.pointSize);
  223. }
  224. BABYLON.MaterialHelper.BindEyePosition(effect, scene);
  225. }
  226. this._activeEffect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  227. if (scene.lightsEnabled && !this.disableLighting) {
  228. BABYLON.MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines);
  229. }
  230. // View
  231. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  232. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  233. }
  234. // Fog
  235. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  236. this._activeEffect.setColor4("topColor", this.topColor, this.topColorAlpha);
  237. this._activeEffect.setColor4("bottomColor", this.bottomColor, this.bottomColorAlpha);
  238. this._activeEffect.setFloat("offset", this.offset);
  239. this._activeEffect.setFloat("smoothness", this.smoothness);
  240. this._afterBind(mesh, this._activeEffect);
  241. };
  242. GradientMaterial.prototype.getAnimatables = function () {
  243. return [];
  244. };
  245. GradientMaterial.prototype.dispose = function (forceDisposeEffect) {
  246. _super.prototype.dispose.call(this, forceDisposeEffect);
  247. };
  248. GradientMaterial.prototype.clone = function (name) {
  249. var _this = this;
  250. return BABYLON.SerializationHelper.Clone(function () { return new GradientMaterial(name, _this.getScene()); }, this);
  251. };
  252. GradientMaterial.prototype.serialize = function () {
  253. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  254. serializationObject.customType = "BABYLON.GradientMaterial";
  255. return serializationObject;
  256. };
  257. GradientMaterial.prototype.getClassName = function () {
  258. return "GradientMaterial";
  259. };
  260. // Statics
  261. GradientMaterial.Parse = function (source, scene, rootUrl) {
  262. return BABYLON.SerializationHelper.Parse(function () { return new GradientMaterial(source.name, scene); }, source, scene, rootUrl);
  263. };
  264. __decorate([
  265. BABYLON.serialize("maxSimultaneousLights")
  266. ], GradientMaterial.prototype, "_maxSimultaneousLights", void 0);
  267. __decorate([
  268. BABYLON.expandToProperty("_markAllSubMeshesAsLightsDirty")
  269. ], GradientMaterial.prototype, "maxSimultaneousLights", void 0);
  270. __decorate([
  271. BABYLON.serializeAsColor3()
  272. ], GradientMaterial.prototype, "topColor", void 0);
  273. __decorate([
  274. BABYLON.serialize()
  275. ], GradientMaterial.prototype, "topColorAlpha", void 0);
  276. __decorate([
  277. BABYLON.serializeAsColor3()
  278. ], GradientMaterial.prototype, "bottomColor", void 0);
  279. __decorate([
  280. BABYLON.serialize()
  281. ], GradientMaterial.prototype, "bottomColorAlpha", void 0);
  282. __decorate([
  283. BABYLON.serialize()
  284. ], GradientMaterial.prototype, "offset", void 0);
  285. __decorate([
  286. BABYLON.serialize()
  287. ], GradientMaterial.prototype, "smoothness", void 0);
  288. __decorate([
  289. BABYLON.serialize()
  290. ], GradientMaterial.prototype, "disableLighting", void 0);
  291. return GradientMaterial;
  292. }(BABYLON.PushMaterial));
  293. BABYLON.GradientMaterial = GradientMaterial;
  294. })(BABYLON || (BABYLON = {}));
  295. //# sourceMappingURL=babylon.gradientMaterial.js.map
  296. 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;\nvarying vec3 vPosition;\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);\nvPosition=position;\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";
  297. 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;\nvarying vec3 vPosition;\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(vPosition).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";