babylon.gradientMaterial.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. 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 extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  16. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  17. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  18. 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;
  19. return c > 3 && r && Object.defineProperty(target, key, r), r;
  20. };
  21. var BABYLON;
  22. (function (BABYLON) {
  23. var GradientMaterialDefines = /** @class */ (function (_super) {
  24. __extends(GradientMaterialDefines, _super);
  25. function GradientMaterialDefines() {
  26. var _this = _super.call(this) || this;
  27. _this.EMISSIVE = false;
  28. _this.CLIPPLANE = false;
  29. _this.CLIPPLANE2 = false;
  30. _this.CLIPPLANE3 = false;
  31. _this.CLIPPLANE4 = false;
  32. _this.ALPHATEST = false;
  33. _this.DEPTHPREPASS = false;
  34. _this.POINTSIZE = false;
  35. _this.FOG = false;
  36. _this.NORMAL = false;
  37. _this.UV1 = false;
  38. _this.UV2 = false;
  39. _this.VERTEXCOLOR = false;
  40. _this.VERTEXALPHA = false;
  41. _this.NUM_BONE_INFLUENCERS = 0;
  42. _this.BonesPerMesh = 0;
  43. _this.INSTANCES = false;
  44. _this.rebuild();
  45. return _this;
  46. }
  47. return GradientMaterialDefines;
  48. }(BABYLON.MaterialDefines));
  49. var GradientMaterial = /** @class */ (function (_super) {
  50. __extends(GradientMaterial, _super);
  51. function GradientMaterial(name, scene) {
  52. var _this = _super.call(this, name, scene) || this;
  53. _this._maxSimultaneousLights = 4;
  54. // The gradient top color, red by default
  55. _this.topColor = new BABYLON.Color3(1, 0, 0);
  56. _this.topColorAlpha = 1.0;
  57. // The gradient top color, blue by default
  58. _this.bottomColor = new BABYLON.Color3(0, 0, 1);
  59. _this.bottomColorAlpha = 1.0;
  60. // Gradient offset
  61. _this.offset = 0;
  62. _this.scale = 1.0;
  63. _this.smoothness = 1.0;
  64. _this._disableLighting = false;
  65. return _this;
  66. }
  67. GradientMaterial.prototype.needAlphaBlending = function () {
  68. return (this.alpha < 1.0 || this.topColorAlpha < 1.0 || this.bottomColorAlpha < 1.0);
  69. };
  70. GradientMaterial.prototype.needAlphaTesting = function () {
  71. return true;
  72. };
  73. GradientMaterial.prototype.getAlphaTestTexture = function () {
  74. return null;
  75. };
  76. // Methods
  77. GradientMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  78. if (this.isFrozen) {
  79. if (this._wasPreviouslyReady && subMesh.effect) {
  80. return true;
  81. }
  82. }
  83. if (!subMesh._materialDefines) {
  84. subMesh._materialDefines = new GradientMaterialDefines();
  85. }
  86. var defines = subMesh._materialDefines;
  87. var scene = this.getScene();
  88. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  89. if (this._renderId === scene.getRenderId()) {
  90. return true;
  91. }
  92. }
  93. var engine = scene.getEngine();
  94. BABYLON.MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  95. BABYLON.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  96. defines._needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  97. // Disable lighting?
  98. defines.EMISSIVE = this._disableLighting;
  99. // Attribs
  100. BABYLON.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  101. // Get correct effect
  102. if (defines.isDirty) {
  103. defines.markAsProcessed();
  104. scene.resetCachedMaterial();
  105. // Fallbacks
  106. var fallbacks = new BABYLON.EffectFallbacks();
  107. if (defines.FOG) {
  108. fallbacks.addFallback(1, "FOG");
  109. }
  110. BABYLON.MaterialHelper.HandleFallbacksForShadows(defines, fallbacks);
  111. if (defines.NUM_BONE_INFLUENCERS > 0) {
  112. fallbacks.addCPUSkinningFallback(0, mesh);
  113. }
  114. //Attributes
  115. var attribs = [BABYLON.VertexBuffer.PositionKind];
  116. if (defines.NORMAL) {
  117. attribs.push(BABYLON.VertexBuffer.NormalKind);
  118. }
  119. if (defines.UV1) {
  120. attribs.push(BABYLON.VertexBuffer.UVKind);
  121. }
  122. if (defines.UV2) {
  123. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  124. }
  125. if (defines.VERTEXCOLOR) {
  126. attribs.push(BABYLON.VertexBuffer.ColorKind);
  127. }
  128. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  129. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  130. // Legacy browser patch
  131. var shaderName = "gradient";
  132. var join = defines.toString();
  133. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType",
  134. "vFogInfos", "vFogColor", "pointSize",
  135. "mBones",
  136. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4",
  137. "topColor", "bottomColor", "offset", "smoothness", "scale"
  138. ];
  139. var samplers = [];
  140. var uniformBuffers = new Array();
  141. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList({
  142. uniformsNames: uniforms,
  143. uniformBuffersNames: uniformBuffers,
  144. samplers: samplers,
  145. defines: defines,
  146. maxSimultaneousLights: 4
  147. });
  148. subMesh.setEffect(scene.getEngine().createEffect(shaderName, {
  149. attributes: attribs,
  150. uniformsNames: uniforms,
  151. uniformBuffersNames: uniformBuffers,
  152. samplers: samplers,
  153. defines: join,
  154. fallbacks: fallbacks,
  155. onCompiled: this.onCompiled,
  156. onError: this.onError,
  157. indexParameters: { maxSimultaneousLights: 4 }
  158. }, engine), defines);
  159. }
  160. if (!subMesh.effect || !subMesh.effect.isReady()) {
  161. return false;
  162. }
  163. this._renderId = scene.getRenderId();
  164. this._wasPreviouslyReady = true;
  165. return true;
  166. };
  167. GradientMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  168. var scene = this.getScene();
  169. var defines = subMesh._materialDefines;
  170. if (!defines) {
  171. return;
  172. }
  173. var effect = subMesh.effect;
  174. if (!effect) {
  175. return;
  176. }
  177. this._activeEffect = effect;
  178. // Matrices
  179. this.bindOnlyWorldMatrix(world);
  180. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  181. // Bones
  182. BABYLON.MaterialHelper.BindBonesParameters(mesh, effect);
  183. if (this._mustRebind(scene, effect)) {
  184. // Clip plane
  185. BABYLON.MaterialHelper.BindClipPlane(effect, scene);
  186. // Point size
  187. if (this.pointsCloud) {
  188. this._activeEffect.setFloat("pointSize", this.pointSize);
  189. }
  190. BABYLON.MaterialHelper.BindEyePosition(effect, scene);
  191. }
  192. if (scene.lightsEnabled && !this.disableLighting) {
  193. BABYLON.MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this.maxSimultaneousLights);
  194. }
  195. // View
  196. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  197. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  198. }
  199. // Fog
  200. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  201. this._activeEffect.setColor4("topColor", this.topColor, this.topColorAlpha);
  202. this._activeEffect.setColor4("bottomColor", this.bottomColor, this.bottomColorAlpha);
  203. this._activeEffect.setFloat("offset", this.offset);
  204. this._activeEffect.setFloat("scale", this.scale);
  205. this._activeEffect.setFloat("smoothness", this.smoothness);
  206. this._afterBind(mesh, this._activeEffect);
  207. };
  208. GradientMaterial.prototype.getAnimatables = function () {
  209. return [];
  210. };
  211. GradientMaterial.prototype.dispose = function (forceDisposeEffect) {
  212. _super.prototype.dispose.call(this, forceDisposeEffect);
  213. };
  214. GradientMaterial.prototype.clone = function (name) {
  215. var _this = this;
  216. return BABYLON.SerializationHelper.Clone(function () { return new GradientMaterial(name, _this.getScene()); }, this);
  217. };
  218. GradientMaterial.prototype.serialize = function () {
  219. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  220. serializationObject.customType = "BABYLON.GradientMaterial";
  221. return serializationObject;
  222. };
  223. GradientMaterial.prototype.getClassName = function () {
  224. return "GradientMaterial";
  225. };
  226. // Statics
  227. GradientMaterial.Parse = function (source, scene, rootUrl) {
  228. return BABYLON.SerializationHelper.Parse(function () { return new GradientMaterial(source.name, scene); }, source, scene, rootUrl);
  229. };
  230. __decorate([
  231. BABYLON.serialize("maxSimultaneousLights")
  232. ], GradientMaterial.prototype, "_maxSimultaneousLights", void 0);
  233. __decorate([
  234. BABYLON.expandToProperty("_markAllSubMeshesAsLightsDirty")
  235. ], GradientMaterial.prototype, "maxSimultaneousLights", void 0);
  236. __decorate([
  237. BABYLON.serializeAsColor3()
  238. ], GradientMaterial.prototype, "topColor", void 0);
  239. __decorate([
  240. BABYLON.serialize()
  241. ], GradientMaterial.prototype, "topColorAlpha", void 0);
  242. __decorate([
  243. BABYLON.serializeAsColor3()
  244. ], GradientMaterial.prototype, "bottomColor", void 0);
  245. __decorate([
  246. BABYLON.serialize()
  247. ], GradientMaterial.prototype, "bottomColorAlpha", void 0);
  248. __decorate([
  249. BABYLON.serialize()
  250. ], GradientMaterial.prototype, "offset", void 0);
  251. __decorate([
  252. BABYLON.serialize()
  253. ], GradientMaterial.prototype, "scale", void 0);
  254. __decorate([
  255. BABYLON.serialize()
  256. ], GradientMaterial.prototype, "smoothness", void 0);
  257. __decorate([
  258. BABYLON.serialize("disableLighting")
  259. ], GradientMaterial.prototype, "_disableLighting", void 0);
  260. __decorate([
  261. BABYLON.expandToProperty("_markAllSubMeshesAsLightsDirty")
  262. ], GradientMaterial.prototype, "disableLighting", void 0);
  263. return GradientMaterial;
  264. }(BABYLON.PushMaterial));
  265. BABYLON.GradientMaterial = GradientMaterial;
  266. })(BABYLON || (BABYLON = {}));
  267. //# sourceMappingURL=babylon.gradientMaterial.js.map
  268. 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 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\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";
  269. BABYLON.Effect.ShadersStore['gradientPixelShader'] = "precision highp float;\n\nuniform vec3 vEyePosition;\n\nuniform vec4 topColor;\nuniform vec4 bottomColor;\nuniform float offset;\nuniform float scale;\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#include<clipPlaneFragmentDeclaration>\n\n#include<fogFragmentDeclaration>\nvoid main(void) {\n#include<clipPlaneFragment>\nvec3 viewDirectionW=normalize(vEyePosition-vPositionW);\nfloat h=vPosition.y*scale+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\n#ifdef EMISSIVE\nvec3 diffuseBase=baseColor.rgb;\n#else\nvec3 diffuseBase=vec3(0.,0.,0.);\n#endif\nlightingInfo info;\nfloat shadow=1.;\nfloat glossiness=0.;\n#include<lightFragment>[0..maxSimultaneousLights]\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";