babylon.simpleMaterial.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
  4. switch (arguments.length) {
  5. case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
  6. case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
  7. case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
  8. }
  9. };
  10. var BABYLON;
  11. (function (BABYLON) {
  12. var maxSimultaneousLights = 4;
  13. var SimpleMaterialDefines = (function (_super) {
  14. __extends(SimpleMaterialDefines, _super);
  15. function SimpleMaterialDefines() {
  16. _super.call(this);
  17. this.DIFFUSE = false;
  18. this.CLIPPLANE = false;
  19. this.ALPHATEST = false;
  20. this.POINTSIZE = false;
  21. this.FOG = false;
  22. this.NORMAL = false;
  23. this.UV1 = false;
  24. this.UV2 = false;
  25. this.VERTEXCOLOR = false;
  26. this.VERTEXALPHA = false;
  27. this.NUM_BONE_INFLUENCERS = 0;
  28. this.BonesPerMesh = 0;
  29. this.INSTANCES = false;
  30. this.rebuild();
  31. }
  32. return SimpleMaterialDefines;
  33. })(BABYLON.MaterialDefines);
  34. var SimpleMaterial = (function (_super) {
  35. __extends(SimpleMaterial, _super);
  36. function SimpleMaterial(name, scene) {
  37. _super.call(this, name, scene);
  38. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  39. this.disableLighting = false;
  40. this.maxSimultaneousLights = 4;
  41. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  42. this._scaledDiffuse = new BABYLON.Color3();
  43. this._defines = new SimpleMaterialDefines();
  44. this._cachedDefines = new SimpleMaterialDefines();
  45. this._cachedDefines.BonesPerMesh = -1;
  46. }
  47. SimpleMaterial.prototype.needAlphaBlending = function () {
  48. return (this.alpha < 1.0);
  49. };
  50. SimpleMaterial.prototype.needAlphaTesting = function () {
  51. return false;
  52. };
  53. SimpleMaterial.prototype.getAlphaTestTexture = function () {
  54. return null;
  55. };
  56. // Methods
  57. SimpleMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  58. if (!mesh) {
  59. return true;
  60. }
  61. if (this._defines.INSTANCES !== useInstances) {
  62. return false;
  63. }
  64. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  65. return true;
  66. }
  67. return false;
  68. };
  69. SimpleMaterial.prototype.isReady = function (mesh, useInstances) {
  70. if (this.checkReadyOnlyOnce) {
  71. if (this._wasPreviouslyReady) {
  72. return true;
  73. }
  74. }
  75. var scene = this.getScene();
  76. if (!this.checkReadyOnEveryCall) {
  77. if (this._renderId === scene.getRenderId()) {
  78. if (this._checkCache(scene, mesh, useInstances)) {
  79. return true;
  80. }
  81. }
  82. }
  83. var engine = scene.getEngine();
  84. var needNormals = false;
  85. var needUVs = false;
  86. this._defines.reset();
  87. // Textures
  88. if (scene.texturesEnabled) {
  89. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  90. if (!this.diffuseTexture.isReady()) {
  91. return false;
  92. }
  93. else {
  94. needUVs = true;
  95. this._defines.DIFFUSE = true;
  96. }
  97. }
  98. }
  99. // Effect
  100. if (scene.clipPlane) {
  101. this._defines.CLIPPLANE = true;
  102. }
  103. if (engine.getAlphaTesting()) {
  104. this._defines.ALPHATEST = true;
  105. }
  106. // Point size
  107. if (this.pointsCloud || scene.forcePointsCloud) {
  108. this._defines.POINTSIZE = true;
  109. }
  110. // Fog
  111. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  112. this._defines.FOG = true;
  113. }
  114. var lightIndex = 0;
  115. if (scene.lightsEnabled && !this.disableLighting) {
  116. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights);
  117. }
  118. // Attribs
  119. if (mesh) {
  120. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  121. this._defines.NORMAL = true;
  122. }
  123. if (needUVs) {
  124. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  125. this._defines.UV1 = true;
  126. }
  127. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  128. this._defines.UV2 = true;
  129. }
  130. }
  131. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  132. this._defines.VERTEXCOLOR = true;
  133. if (mesh.hasVertexAlpha) {
  134. this._defines.VERTEXALPHA = true;
  135. }
  136. }
  137. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  138. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  139. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  140. }
  141. // Instances
  142. if (useInstances) {
  143. this._defines.INSTANCES = true;
  144. }
  145. }
  146. // Get correct effect
  147. if (!this._defines.isEqual(this._cachedDefines)) {
  148. this._defines.cloneTo(this._cachedDefines);
  149. scene.resetCachedMaterial();
  150. // Fallbacks
  151. var fallbacks = new BABYLON.EffectFallbacks();
  152. if (this._defines.FOG) {
  153. fallbacks.addFallback(1, "FOG");
  154. }
  155. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
  156. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  157. fallbacks.addCPUSkinningFallback(0, mesh);
  158. }
  159. //Attributes
  160. var attribs = [BABYLON.VertexBuffer.PositionKind];
  161. if (this._defines.NORMAL) {
  162. attribs.push(BABYLON.VertexBuffer.NormalKind);
  163. }
  164. if (this._defines.UV1) {
  165. attribs.push(BABYLON.VertexBuffer.UVKind);
  166. }
  167. if (this._defines.UV2) {
  168. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  169. }
  170. if (this._defines.VERTEXCOLOR) {
  171. attribs.push(BABYLON.VertexBuffer.ColorKind);
  172. }
  173. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  174. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  175. var shaderName = "simple";
  176. var join = this._defines.toString();
  177. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  178. "vFogInfos", "vFogColor", "pointSize",
  179. "vDiffuseInfos",
  180. "mBones",
  181. "vClipPlane", "diffuseMatrix", "depthValues"
  182. ];
  183. BABYLON.MaterialHelper.PrepareUniformsListForList(uniforms, this._defines, this.maxSimultaneousLights);
  184. this._effect = scene.getEngine().createEffect(shaderName, attribs, uniforms, ["diffuseSampler",
  185. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  186. ], join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: this.maxSimultaneousLights });
  187. }
  188. if (!this._effect.isReady()) {
  189. return false;
  190. }
  191. this._renderId = scene.getRenderId();
  192. this._wasPreviouslyReady = true;
  193. if (mesh) {
  194. if (!mesh._materialDefines) {
  195. mesh._materialDefines = new SimpleMaterialDefines();
  196. }
  197. this._defines.cloneTo(mesh._materialDefines);
  198. }
  199. return true;
  200. };
  201. SimpleMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  202. this._effect.setMatrix("world", world);
  203. };
  204. SimpleMaterial.prototype.bind = function (world, mesh) {
  205. var scene = this.getScene();
  206. // Matrices
  207. this.bindOnlyWorldMatrix(world);
  208. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  209. // Bones
  210. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  211. if (scene.getCachedMaterial() !== this) {
  212. // Textures
  213. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  214. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  215. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  216. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  217. }
  218. // Clip plane
  219. BABYLON.MaterialHelper.BindClipPlane(this._effect, scene);
  220. // Point size
  221. if (this.pointsCloud) {
  222. this._effect.setFloat("pointSize", this.pointSize);
  223. }
  224. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  225. }
  226. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  227. // Lights
  228. if (scene.lightsEnabled && !this.disableLighting) {
  229. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines, this.maxSimultaneousLights);
  230. }
  231. // View
  232. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  233. this._effect.setMatrix("view", scene.getViewMatrix());
  234. }
  235. // Fog
  236. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  237. _super.prototype.bind.call(this, world, mesh);
  238. };
  239. SimpleMaterial.prototype.getAnimatables = function () {
  240. var results = [];
  241. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  242. results.push(this.diffuseTexture);
  243. }
  244. return results;
  245. };
  246. SimpleMaterial.prototype.dispose = function (forceDisposeEffect) {
  247. if (this.diffuseTexture) {
  248. this.diffuseTexture.dispose();
  249. }
  250. _super.prototype.dispose.call(this, forceDisposeEffect);
  251. };
  252. SimpleMaterial.prototype.clone = function (name) {
  253. var _this = this;
  254. return BABYLON.SerializationHelper.Clone(function () { return new SimpleMaterial(name, _this.getScene()); }, this);
  255. };
  256. SimpleMaterial.prototype.serialize = function () {
  257. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  258. serializationObject.customType = "BABYLON.SimpleMaterial";
  259. return serializationObject;
  260. };
  261. // Statics
  262. SimpleMaterial.Parse = function (source, scene, rootUrl) {
  263. return BABYLON.SerializationHelper.Parse(function () { return new SimpleMaterial(source.name, scene); }, source, scene, rootUrl);
  264. };
  265. __decorate([
  266. BABYLON.serializeAsTexture()
  267. ], SimpleMaterial.prototype, "diffuseTexture");
  268. __decorate([
  269. BABYLON.serializeAsColor3("diffuseColor")
  270. ], SimpleMaterial.prototype, "diffuseColor");
  271. __decorate([
  272. BABYLON.serialize()
  273. ], SimpleMaterial.prototype, "disableLighting");
  274. __decorate([
  275. BABYLON.serialize()
  276. ], SimpleMaterial.prototype, "maxSimultaneousLights");
  277. return SimpleMaterial;
  278. })(BABYLON.Material);
  279. BABYLON.SimpleMaterial = SimpleMaterial;
  280. })(BABYLON || (BABYLON = {}));
  281. 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<shadowsVertexDeclaration>\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>\n\n#ifdef VERTEXCOLOR\nvColor=color;\n#endif\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\n}\n";
  282. 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<lightFragmentDeclaration>[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\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#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}";