babylon.simpleMaterial.js 16 KB

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