babylon.simpleMaterial.js 16 KB

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