babylon.simpleMaterial.js 16 KB

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