babylon.simpleMaterial.js 16 KB

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