babylon.simpleMaterial.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.LIGHT0 = false;
  23. this.LIGHT1 = false;
  24. this.LIGHT2 = false;
  25. this.LIGHT3 = false;
  26. this.SPOTLIGHT0 = false;
  27. this.SPOTLIGHT1 = false;
  28. this.SPOTLIGHT2 = false;
  29. this.SPOTLIGHT3 = false;
  30. this.HEMILIGHT0 = false;
  31. this.HEMILIGHT1 = false;
  32. this.HEMILIGHT2 = false;
  33. this.HEMILIGHT3 = false;
  34. this.DIRLIGHT0 = false;
  35. this.DIRLIGHT1 = false;
  36. this.DIRLIGHT2 = false;
  37. this.DIRLIGHT3 = false;
  38. this.POINTLIGHT0 = false;
  39. this.POINTLIGHT1 = false;
  40. this.POINTLIGHT2 = false;
  41. this.POINTLIGHT3 = false;
  42. this.SHADOW0 = false;
  43. this.SHADOW1 = false;
  44. this.SHADOW2 = false;
  45. this.SHADOW3 = false;
  46. this.SHADOWS = false;
  47. this.SHADOWVSM0 = false;
  48. this.SHADOWVSM1 = false;
  49. this.SHADOWVSM2 = false;
  50. this.SHADOWVSM3 = false;
  51. this.SHADOWPCF0 = false;
  52. this.SHADOWPCF1 = false;
  53. this.SHADOWPCF2 = false;
  54. this.SHADOWPCF3 = false;
  55. this.NORMAL = false;
  56. this.UV1 = false;
  57. this.UV2 = false;
  58. this.VERTEXCOLOR = false;
  59. this.VERTEXALPHA = false;
  60. this.NUM_BONE_INFLUENCERS = 0;
  61. this.BonesPerMesh = 0;
  62. this.INSTANCES = false;
  63. this._keys = Object.keys(this);
  64. }
  65. return SimpleMaterialDefines;
  66. })(BABYLON.MaterialDefines);
  67. var SimpleMaterial = (function (_super) {
  68. __extends(SimpleMaterial, _super);
  69. function SimpleMaterial(name, scene) {
  70. _super.call(this, name, scene);
  71. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  72. this.disableLighting = false;
  73. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  74. this._scaledDiffuse = new BABYLON.Color3();
  75. this._defines = new SimpleMaterialDefines();
  76. this._cachedDefines = new SimpleMaterialDefines();
  77. this._cachedDefines.BonesPerMesh = -1;
  78. }
  79. SimpleMaterial.prototype.needAlphaBlending = function () {
  80. return (this.alpha < 1.0);
  81. };
  82. SimpleMaterial.prototype.needAlphaTesting = function () {
  83. return false;
  84. };
  85. SimpleMaterial.prototype.getAlphaTestTexture = function () {
  86. return null;
  87. };
  88. // Methods
  89. SimpleMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  90. if (!mesh) {
  91. return true;
  92. }
  93. if (this._defines.INSTANCES !== useInstances) {
  94. return false;
  95. }
  96. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  97. return true;
  98. }
  99. return false;
  100. };
  101. SimpleMaterial.prototype.isReady = function (mesh, useInstances) {
  102. if (this.checkReadyOnlyOnce) {
  103. if (this._wasPreviouslyReady) {
  104. return true;
  105. }
  106. }
  107. var scene = this.getScene();
  108. if (!this.checkReadyOnEveryCall) {
  109. if (this._renderId === scene.getRenderId()) {
  110. if (this._checkCache(scene, mesh, useInstances)) {
  111. return true;
  112. }
  113. }
  114. }
  115. var engine = scene.getEngine();
  116. var needNormals = false;
  117. var needUVs = false;
  118. this._defines.reset();
  119. // Textures
  120. if (scene.texturesEnabled) {
  121. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  122. if (!this.diffuseTexture.isReady()) {
  123. return false;
  124. }
  125. else {
  126. needUVs = true;
  127. this._defines.DIFFUSE = true;
  128. }
  129. }
  130. }
  131. // Effect
  132. if (scene.clipPlane) {
  133. this._defines.CLIPPLANE = true;
  134. }
  135. if (engine.getAlphaTesting()) {
  136. this._defines.ALPHATEST = true;
  137. }
  138. // Point size
  139. if (this.pointsCloud || scene.forcePointsCloud) {
  140. this._defines.POINTSIZE = true;
  141. }
  142. // Fog
  143. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  144. this._defines.FOG = true;
  145. }
  146. var lightIndex = 0;
  147. if (scene.lightsEnabled && !this.disableLighting) {
  148. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines);
  149. }
  150. // Attribs
  151. if (mesh) {
  152. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  153. this._defines.NORMAL = true;
  154. }
  155. if (needUVs) {
  156. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  157. this._defines.UV1 = true;
  158. }
  159. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  160. this._defines.UV2 = true;
  161. }
  162. }
  163. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  164. this._defines.VERTEXCOLOR = true;
  165. if (mesh.hasVertexAlpha) {
  166. this._defines.VERTEXALPHA = true;
  167. }
  168. }
  169. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  170. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  171. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  172. }
  173. // Instances
  174. if (useInstances) {
  175. this._defines.INSTANCES = true;
  176. }
  177. }
  178. // Get correct effect
  179. if (!this._defines.isEqual(this._cachedDefines)) {
  180. this._defines.cloneTo(this._cachedDefines);
  181. scene.resetCachedMaterial();
  182. // Fallbacks
  183. var fallbacks = new BABYLON.EffectFallbacks();
  184. if (this._defines.FOG) {
  185. fallbacks.addFallback(1, "FOG");
  186. }
  187. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  188. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  189. fallbacks.addCPUSkinningFallback(0, mesh);
  190. }
  191. //Attributes
  192. var attribs = [BABYLON.VertexBuffer.PositionKind];
  193. if (this._defines.NORMAL) {
  194. attribs.push(BABYLON.VertexBuffer.NormalKind);
  195. }
  196. if (this._defines.UV1) {
  197. attribs.push(BABYLON.VertexBuffer.UVKind);
  198. }
  199. if (this._defines.UV2) {
  200. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  201. }
  202. if (this._defines.VERTEXCOLOR) {
  203. attribs.push(BABYLON.VertexBuffer.ColorKind);
  204. }
  205. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  206. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  207. var shaderName = "simple";
  208. var join = this._defines.toString();
  209. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  210. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  211. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  212. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  213. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  214. "vFogInfos", "vFogColor", "pointSize",
  215. "vDiffuseInfos",
  216. "mBones",
  217. "vClipPlane", "diffuseMatrix",
  218. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues"
  219. ], ["diffuseSampler",
  220. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  221. ], join, fallbacks, this.onCompiled, this.onError);
  222. }
  223. if (!this._effect.isReady()) {
  224. return false;
  225. }
  226. this._renderId = scene.getRenderId();
  227. this._wasPreviouslyReady = true;
  228. if (mesh) {
  229. if (!mesh._materialDefines) {
  230. mesh._materialDefines = new SimpleMaterialDefines();
  231. }
  232. this._defines.cloneTo(mesh._materialDefines);
  233. }
  234. return true;
  235. };
  236. SimpleMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  237. this._effect.setMatrix("world", world);
  238. };
  239. SimpleMaterial.prototype.bind = function (world, mesh) {
  240. var scene = this.getScene();
  241. // Matrices
  242. this.bindOnlyWorldMatrix(world);
  243. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  244. // Bones
  245. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  246. if (scene.getCachedMaterial() !== this) {
  247. // Textures
  248. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  249. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  250. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  251. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  252. }
  253. // Clip plane
  254. BABYLON.MaterialHelper.BindClipPlane(this._effect, scene);
  255. // Point size
  256. if (this.pointsCloud) {
  257. this._effect.setFloat("pointSize", this.pointSize);
  258. }
  259. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  260. }
  261. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  262. // Lights
  263. if (scene.lightsEnabled && !this.disableLighting) {
  264. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
  265. }
  266. // View
  267. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  268. this._effect.setMatrix("view", scene.getViewMatrix());
  269. }
  270. // Fog
  271. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  272. _super.prototype.bind.call(this, world, mesh);
  273. };
  274. SimpleMaterial.prototype.getAnimatables = function () {
  275. var results = [];
  276. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  277. results.push(this.diffuseTexture);
  278. }
  279. return results;
  280. };
  281. SimpleMaterial.prototype.dispose = function (forceDisposeEffect) {
  282. if (this.diffuseTexture) {
  283. this.diffuseTexture.dispose();
  284. }
  285. _super.prototype.dispose.call(this, forceDisposeEffect);
  286. };
  287. SimpleMaterial.prototype.clone = function (name) {
  288. var _this = this;
  289. return BABYLON.SerializationHelper.Clone(function () { return new SimpleMaterial(name, _this.getScene()); }, this);
  290. };
  291. SimpleMaterial.prototype.serialize = function () {
  292. var serializationObject = _super.prototype.serialize.call(this);
  293. serializationObject.customType = "BABYLON.SimpleMaterial";
  294. serializationObject.diffuseColor = this.diffuseColor.asArray();
  295. serializationObject.disableLighting = this.disableLighting;
  296. if (this.diffuseTexture) {
  297. serializationObject.diffuseTexture = this.diffuseTexture.serialize();
  298. }
  299. return serializationObject;
  300. };
  301. SimpleMaterial.Parse = function (source, scene, rootUrl) {
  302. var material = new SimpleMaterial(source.name, scene);
  303. material.diffuseColor = BABYLON.Color3.FromArray(source.diffuseColor);
  304. material.disableLighting = source.disableLighting;
  305. material.alpha = source.alpha;
  306. material.id = source.id;
  307. BABYLON.Tags.AddTagsTo(material, source.tags);
  308. material.backFaceCulling = source.backFaceCulling;
  309. material.wireframe = source.wireframe;
  310. if (source.diffuseTexture) {
  311. material.diffuseTexture = BABYLON.Texture.Parse(source.diffuseTexture, scene, rootUrl);
  312. }
  313. if (source.checkReadyOnlyOnce) {
  314. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  315. }
  316. return material;
  317. };
  318. __decorate([
  319. BABYLON.serializeAsTexture()
  320. ], SimpleMaterial.prototype, "diffuseTexture");
  321. __decorate([
  322. BABYLON.serializeAsColor3("diffuseColor")
  323. ], SimpleMaterial.prototype, "diffuseColor");
  324. return SimpleMaterial;
  325. })(BABYLON.Material);
  326. BABYLON.SimpleMaterial = SimpleMaterial;
  327. })(BABYLON || (BABYLON = {}));
  328. BABYLON.Effect.ShadersStore['simpleVertexShader'] = "precision highp float;\r\n\r\n// Attributes\r\nattribute vec3 position;\r\n#ifdef NORMAL\r\nattribute vec3 normal;\r\n#endif\r\n#ifdef UV1\r\nattribute vec2 uv;\r\n#endif\r\n#ifdef UV2\r\nattribute vec2 uv2;\r\n#endif\r\n#ifdef VERTEXCOLOR\r\nattribute vec4 color;\r\n#endif\r\n\r\n#include<bonesDeclaration>\r\n\r\n// Uniforms\r\n#include<instancesDeclaration>\r\n\r\nuniform mat4 view;\r\nuniform mat4 viewProjection;\r\n\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\nuniform mat4 diffuseMatrix;\r\nuniform vec2 vDiffuseInfos;\r\n#endif\r\n\r\n#ifdef POINTSIZE\r\nuniform float pointSize;\r\n#endif\r\n\r\n// Output\r\nvarying vec3 vPositionW;\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n\r\n#include<clipPlaneVertexDeclaration>\r\n\r\n#include<fogVertexDeclaration>\r\n#include<shadowsVertexDeclaration>\r\n\r\nvoid main(void) {\r\n\r\n#include<instancesVertex>\r\n#include<bonesVertex>\r\n\r\n\tgl_Position = viewProjection * finalWorld * vec4(position, 1.0);\r\n\r\n\tvec4 worldPos = finalWorld * vec4(position, 1.0);\r\n\tvPositionW = vec3(worldPos);\r\n\r\n#ifdef NORMAL\r\n\tvNormalW = normalize(vec3(finalWorld * vec4(normal, 0.0)));\r\n#endif\r\n\r\n\t// Texture coordinates\r\n#ifndef UV1\r\n\tvec2 uv = vec2(0., 0.);\r\n#endif\r\n#ifndef UV2\r\n\tvec2 uv2 = vec2(0., 0.);\r\n#endif\r\n\r\n#ifdef DIFFUSE\r\n\tif (vDiffuseInfos.x == 0.)\r\n\t{\r\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv, 1.0, 0.0));\r\n\t}\r\n\telse\r\n\t{\r\n\t\tvDiffuseUV = vec2(diffuseMatrix * vec4(uv2, 1.0, 0.0));\r\n\t}\r\n#endif\r\n\r\n\t// Clip plane\r\n#include<clipPlaneVertex>\r\n\r\n // Fog\r\n#include<fogVertex>\r\n#include<shadowsVertex>\r\n\r\n\t// Vertex color\r\n#ifdef VERTEXCOLOR\r\n\tvColor = color;\r\n#endif\r\n\r\n\t// Point size\r\n#ifdef POINTSIZE\r\n\tgl_PointSize = pointSize;\r\n#endif\r\n}\r\n";
  329. BABYLON.Effect.ShadersStore['simplePixelShader'] = "precision highp float;\r\n\r\n// Constants\r\nuniform vec3 vEyePosition;\r\nuniform vec4 vDiffuseColor;\r\n\r\n// Input\r\nvarying vec3 vPositionW;\r\n\r\n#ifdef NORMAL\r\nvarying vec3 vNormalW;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\nvarying vec4 vColor;\r\n#endif\r\n\r\n// Lights\r\n#include<light0FragmentDeclaration>\r\n#include<light1FragmentDeclaration>\r\n#include<light2FragmentDeclaration>\r\n#include<light3FragmentDeclaration>\r\n\r\n\r\n#include<lightsFragmentFunctions>\r\n#include<shadowsFragmentFunctions>\r\n\r\n// Samplers\r\n#ifdef DIFFUSE\r\nvarying vec2 vDiffuseUV;\r\nuniform sampler2D diffuseSampler;\r\nuniform vec2 vDiffuseInfos;\r\n#endif\r\n\r\n#include<clipPlaneFragmentDeclaration>\r\n\r\n// Fog\r\n#include<fogFragmentDeclaration>\r\n\r\nvoid main(void) {\r\n#include<clipPlaneFragment>\r\n\r\n\tvec3 viewDirectionW = normalize(vEyePosition - vPositionW);\r\n\r\n\t// Base color\r\n\tvec4 baseColor = vec4(1., 1., 1., 1.);\r\n\tvec3 diffuseColor = vDiffuseColor.rgb;\r\n\r\n\t// Alpha\r\n\tfloat alpha = vDiffuseColor.a;\r\n\r\n#ifdef DIFFUSE\r\n\tbaseColor = texture2D(diffuseSampler, vDiffuseUV);\r\n\r\n#ifdef ALPHATEST\r\n\tif (baseColor.a < 0.4)\r\n\t\tdiscard;\r\n#endif\r\n\r\n\tbaseColor.rgb *= vDiffuseInfos.y;\r\n#endif\r\n\r\n#ifdef VERTEXCOLOR\r\n\tbaseColor.rgb *= vColor.rgb;\r\n#endif\r\n\r\n\t// Normal\r\n#ifdef NORMAL\r\n\tvec3 normalW = normalize(vNormalW);\r\n#else\r\n\tvec3 normalW = vec3(1.0, 1.0, 1.0);\r\n#endif\r\n\r\n\t// Lighting\r\n\tvec3 diffuseBase = vec3(0., 0., 0.);\r\n\tfloat shadow = 1.;\r\n float glossiness = 0.;\r\n \r\n#include<light0Fragment>\r\n#include<light1Fragment>\r\n#include<light2Fragment>\r\n#include<light3Fragment>\r\n\r\n\r\n#ifdef VERTEXALPHA\r\n\talpha *= vColor.a;\r\n#endif\r\n\r\n\tvec3 finalDiffuse = clamp(diffuseBase * diffuseColor, 0.0, 1.0) * baseColor.rgb;\r\n\r\n\t// Composition\r\n\tvec4 color = vec4(finalDiffuse, alpha);\r\n\r\n#include<fogFragment>\r\n\r\n\tgl_FragColor = color;\r\n}";