babylon.simpleMaterial.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 SimpleMaterialDefines = /** @class */ (function (_super) {
  24. __extends(SimpleMaterialDefines, _super);
  25. function SimpleMaterialDefines() {
  26. var _this = _super.call(this) || this;
  27. _this.DIFFUSE = 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 SimpleMaterialDefines;
  48. }(BABYLON.MaterialDefines));
  49. var SimpleMaterial = /** @class */ (function (_super) {
  50. __extends(SimpleMaterial, _super);
  51. function SimpleMaterial(name, scene) {
  52. var _this = _super.call(this, name, scene) || this;
  53. _this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  54. _this._disableLighting = false;
  55. _this._maxSimultaneousLights = 4;
  56. return _this;
  57. }
  58. SimpleMaterial.prototype.needAlphaBlending = function () {
  59. return (this.alpha < 1.0);
  60. };
  61. SimpleMaterial.prototype.needAlphaTesting = function () {
  62. return false;
  63. };
  64. SimpleMaterial.prototype.getAlphaTestTexture = function () {
  65. return null;
  66. };
  67. // Methods
  68. SimpleMaterial.prototype.isReadyForSubMesh = function (mesh, subMesh, useInstances) {
  69. if (this.isFrozen) {
  70. if (this._wasPreviouslyReady && subMesh.effect) {
  71. return true;
  72. }
  73. }
  74. if (!subMesh._materialDefines) {
  75. subMesh._materialDefines = new SimpleMaterialDefines();
  76. }
  77. var defines = subMesh._materialDefines;
  78. var scene = this.getScene();
  79. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  80. if (this._renderId === scene.getRenderId()) {
  81. return true;
  82. }
  83. }
  84. var engine = scene.getEngine();
  85. // Textures
  86. if (defines._areTexturesDirty) {
  87. defines._needUVs = false;
  88. if (scene.texturesEnabled) {
  89. if (this._diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  90. if (!this._diffuseTexture.isReady()) {
  91. return false;
  92. }
  93. else {
  94. defines._needUVs = true;
  95. defines.DIFFUSE = true;
  96. }
  97. }
  98. }
  99. }
  100. // Misc.
  101. BABYLON.MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  102. // Lights
  103. defines._needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  104. // Values that need to be evaluated on every frame
  105. BABYLON.MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  106. // Attribs
  107. BABYLON.MaterialHelper.PrepareDefinesForAttributes(mesh, defines, true, true);
  108. // Get correct effect
  109. if (defines.isDirty) {
  110. defines.markAsProcessed();
  111. scene.resetCachedMaterial();
  112. // Fallbacks
  113. var fallbacks = new BABYLON.EffectFallbacks();
  114. if (defines.FOG) {
  115. fallbacks.addFallback(1, "FOG");
  116. }
  117. BABYLON.MaterialHelper.HandleFallbacksForShadows(defines, fallbacks, this.maxSimultaneousLights);
  118. if (defines.NUM_BONE_INFLUENCERS > 0) {
  119. fallbacks.addCPUSkinningFallback(0, mesh);
  120. }
  121. //Attributes
  122. var attribs = [BABYLON.VertexBuffer.PositionKind];
  123. if (defines.NORMAL) {
  124. attribs.push(BABYLON.VertexBuffer.NormalKind);
  125. }
  126. if (defines.UV1) {
  127. attribs.push(BABYLON.VertexBuffer.UVKind);
  128. }
  129. if (defines.UV2) {
  130. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  131. }
  132. if (defines.VERTEXCOLOR) {
  133. attribs.push(BABYLON.VertexBuffer.ColorKind);
  134. }
  135. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  136. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  137. var shaderName = "simple";
  138. var join = defines.toString();
  139. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  140. "vFogInfos", "vFogColor", "pointSize",
  141. "vDiffuseInfos",
  142. "mBones",
  143. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "diffuseMatrix"
  144. ];
  145. var samplers = ["diffuseSampler"];
  146. var uniformBuffers = new Array();
  147. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList({
  148. uniformsNames: uniforms,
  149. uniformBuffersNames: uniformBuffers,
  150. samplers: samplers,
  151. defines: defines,
  152. maxSimultaneousLights: this.maxSimultaneousLights
  153. });
  154. subMesh.setEffect(scene.getEngine().createEffect(shaderName, {
  155. attributes: attribs,
  156. uniformsNames: uniforms,
  157. uniformBuffersNames: uniformBuffers,
  158. samplers: samplers,
  159. defines: join,
  160. fallbacks: fallbacks,
  161. onCompiled: this.onCompiled,
  162. onError: this.onError,
  163. indexParameters: { maxSimultaneousLights: this._maxSimultaneousLights - 1 }
  164. }, engine), defines);
  165. }
  166. if (!subMesh.effect || !subMesh.effect.isReady()) {
  167. return false;
  168. }
  169. this._renderId = scene.getRenderId();
  170. this._wasPreviouslyReady = true;
  171. return true;
  172. };
  173. SimpleMaterial.prototype.bindForSubMesh = function (world, mesh, subMesh) {
  174. var scene = this.getScene();
  175. var defines = subMesh._materialDefines;
  176. if (!defines) {
  177. return;
  178. }
  179. var effect = subMesh.effect;
  180. if (!effect) {
  181. return;
  182. }
  183. this._activeEffect = effect;
  184. // Matrices
  185. this.bindOnlyWorldMatrix(world);
  186. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  187. // Bones
  188. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  189. if (this._mustRebind(scene, effect)) {
  190. // Textures
  191. if (this._diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  192. this._activeEffect.setTexture("diffuseSampler", this._diffuseTexture);
  193. this._activeEffect.setFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  194. this._activeEffect.setMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
  195. }
  196. // Clip plane
  197. BABYLON.MaterialHelper.BindClipPlane(this._activeEffect, scene);
  198. // Point size
  199. if (this.pointsCloud) {
  200. this._activeEffect.setFloat("pointSize", this.pointSize);
  201. }
  202. BABYLON.MaterialHelper.BindEyePosition(effect, scene);
  203. }
  204. this._activeEffect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  205. // Lights
  206. if (scene.lightsEnabled && !this.disableLighting) {
  207. BABYLON.MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this.maxSimultaneousLights);
  208. }
  209. // View
  210. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  211. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  212. }
  213. // Fog
  214. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  215. this._afterBind(mesh, this._activeEffect);
  216. };
  217. SimpleMaterial.prototype.getAnimatables = function () {
  218. var results = [];
  219. if (this._diffuseTexture && this._diffuseTexture.animations && this._diffuseTexture.animations.length > 0) {
  220. results.push(this._diffuseTexture);
  221. }
  222. return results;
  223. };
  224. SimpleMaterial.prototype.getActiveTextures = function () {
  225. var activeTextures = _super.prototype.getActiveTextures.call(this);
  226. if (this._diffuseTexture) {
  227. activeTextures.push(this._diffuseTexture);
  228. }
  229. return activeTextures;
  230. };
  231. SimpleMaterial.prototype.hasTexture = function (texture) {
  232. if (_super.prototype.hasTexture.call(this, texture)) {
  233. return true;
  234. }
  235. if (this.diffuseTexture === texture) {
  236. return true;
  237. }
  238. return false;
  239. };
  240. SimpleMaterial.prototype.dispose = function (forceDisposeEffect) {
  241. if (this._diffuseTexture) {
  242. this._diffuseTexture.dispose();
  243. }
  244. _super.prototype.dispose.call(this, forceDisposeEffect);
  245. };
  246. SimpleMaterial.prototype.clone = function (name) {
  247. var _this = this;
  248. return BABYLON.SerializationHelper.Clone(function () { return new SimpleMaterial(name, _this.getScene()); }, this);
  249. };
  250. SimpleMaterial.prototype.serialize = function () {
  251. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  252. serializationObject.customType = "BABYLON.SimpleMaterial";
  253. return serializationObject;
  254. };
  255. SimpleMaterial.prototype.getClassName = function () {
  256. return "SimpleMaterial";
  257. };
  258. // Statics
  259. SimpleMaterial.Parse = function (source, scene, rootUrl) {
  260. return BABYLON.SerializationHelper.Parse(function () { return new SimpleMaterial(source.name, scene); }, source, scene, rootUrl);
  261. };
  262. __decorate([
  263. BABYLON.serializeAsTexture("diffuseTexture")
  264. ], SimpleMaterial.prototype, "_diffuseTexture", void 0);
  265. __decorate([
  266. BABYLON.expandToProperty("_markAllSubMeshesAsTexturesDirty")
  267. ], SimpleMaterial.prototype, "diffuseTexture", void 0);
  268. __decorate([
  269. BABYLON.serializeAsColor3("diffuse")
  270. ], SimpleMaterial.prototype, "diffuseColor", void 0);
  271. __decorate([
  272. BABYLON.serialize("disableLighting")
  273. ], SimpleMaterial.prototype, "_disableLighting", void 0);
  274. __decorate([
  275. BABYLON.expandToProperty("_markAllSubMeshesAsLightsDirty")
  276. ], SimpleMaterial.prototype, "disableLighting", void 0);
  277. __decorate([
  278. BABYLON.serialize("maxSimultaneousLights")
  279. ], SimpleMaterial.prototype, "_maxSimultaneousLights", void 0);
  280. __decorate([
  281. BABYLON.expandToProperty("_markAllSubMeshesAsLightsDirty")
  282. ], SimpleMaterial.prototype, "maxSimultaneousLights", void 0);
  283. return SimpleMaterial;
  284. }(BABYLON.PushMaterial));
  285. BABYLON.SimpleMaterial = SimpleMaterial;
  286. })(BABYLON || (BABYLON = {}));
  287. //# sourceMappingURL=babylon.simpleMaterial.js.map
  288. BABYLON.Effect.ShadersStore['simpleVertexShader'] = "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";
  289. BABYLON.Effect.ShadersStore['simplePixelShader'] = "precision highp float;\n\nuniform vec3 vEyePosition;\nuniform vec4 vDiffuseColor;\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..maxSimultaneousLights]\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);\n\nvec4 baseColor=vec4(1.,1.,1.,1.);\nvec3 diffuseColor=vDiffuseColor.rgb;\n\nfloat alpha=vDiffuseColor.a;\n#ifdef DIFFUSE\nbaseColor=texture2D(diffuseSampler,vDiffuseUV);\n#ifdef ALPHATEST\nif (baseColor.a<0.4)\ndiscard;\n#endif\n#include<depthPrePass>\nbaseColor.rgb*=vDiffuseInfos.y;\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#ifdef SPECULARTERM\nvec3 specularBase=vec3(0.,0.,0.);\n#endif \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}";