babylon.simpleMaterial.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var maxSimultaneousLights = 4;
  5. var SimpleMaterialDefines = (function (_super) {
  6. __extends(SimpleMaterialDefines, _super);
  7. function SimpleMaterialDefines() {
  8. _super.call(this);
  9. this.DIFFUSE = false;
  10. this.CLIPPLANE = false;
  11. this.ALPHATEST = false;
  12. this.POINTSIZE = false;
  13. this.FOG = false;
  14. this.LIGHT0 = false;
  15. this.LIGHT1 = false;
  16. this.LIGHT2 = false;
  17. this.LIGHT3 = false;
  18. this.SPOTLIGHT0 = false;
  19. this.SPOTLIGHT1 = false;
  20. this.SPOTLIGHT2 = false;
  21. this.SPOTLIGHT3 = false;
  22. this.HEMILIGHT0 = false;
  23. this.HEMILIGHT1 = false;
  24. this.HEMILIGHT2 = false;
  25. this.HEMILIGHT3 = false;
  26. this.DIRLIGHT0 = false;
  27. this.DIRLIGHT1 = false;
  28. this.DIRLIGHT2 = false;
  29. this.DIRLIGHT3 = false;
  30. this.POINTLIGHT0 = false;
  31. this.POINTLIGHT1 = false;
  32. this.POINTLIGHT2 = false;
  33. this.POINTLIGHT3 = false;
  34. this.SHADOW0 = false;
  35. this.SHADOW1 = false;
  36. this.SHADOW2 = false;
  37. this.SHADOW3 = false;
  38. this.SHADOWS = false;
  39. this.SHADOWVSM0 = false;
  40. this.SHADOWVSM1 = false;
  41. this.SHADOWVSM2 = false;
  42. this.SHADOWVSM3 = false;
  43. this.SHADOWPCF0 = false;
  44. this.SHADOWPCF1 = false;
  45. this.SHADOWPCF2 = false;
  46. this.SHADOWPCF3 = false;
  47. this.NORMAL = false;
  48. this.UV1 = false;
  49. this.UV2 = false;
  50. this.VERTEXCOLOR = false;
  51. this.VERTEXALPHA = false;
  52. this.NUM_BONE_INFLUENCERS = 0;
  53. this.BonesPerMesh = 0;
  54. this.INSTANCES = false;
  55. this._keys = Object.keys(this);
  56. }
  57. return SimpleMaterialDefines;
  58. })(BABYLON.MaterialDefines);
  59. var SimpleMaterial = (function (_super) {
  60. __extends(SimpleMaterial, _super);
  61. function SimpleMaterial(name, scene) {
  62. _super.call(this, name, scene);
  63. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  64. this.disableLighting = false;
  65. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  66. this._scaledDiffuse = new BABYLON.Color3();
  67. this._defines = new SimpleMaterialDefines();
  68. this._cachedDefines = new SimpleMaterialDefines();
  69. this._cachedDefines.BonesPerMesh = -1;
  70. }
  71. SimpleMaterial.prototype.needAlphaBlending = function () {
  72. return (this.alpha < 1.0);
  73. };
  74. SimpleMaterial.prototype.needAlphaTesting = function () {
  75. return false;
  76. };
  77. SimpleMaterial.prototype.getAlphaTestTexture = function () {
  78. return null;
  79. };
  80. // Methods
  81. SimpleMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  82. if (!mesh) {
  83. return true;
  84. }
  85. if (this._defines.INSTANCES !== useInstances) {
  86. return false;
  87. }
  88. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  89. return true;
  90. }
  91. return false;
  92. };
  93. SimpleMaterial.prototype.isReady = function (mesh, useInstances) {
  94. if (this.checkReadyOnlyOnce) {
  95. if (this._wasPreviouslyReady) {
  96. return true;
  97. }
  98. }
  99. var scene = this.getScene();
  100. if (!this.checkReadyOnEveryCall) {
  101. if (this._renderId === scene.getRenderId()) {
  102. if (this._checkCache(scene, mesh, useInstances)) {
  103. return true;
  104. }
  105. }
  106. }
  107. var engine = scene.getEngine();
  108. var needNormals = false;
  109. var needUVs = false;
  110. this._defines.reset();
  111. // Textures
  112. if (scene.texturesEnabled) {
  113. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  114. if (!this.diffuseTexture.isReady()) {
  115. return false;
  116. }
  117. else {
  118. needUVs = true;
  119. this._defines.DIFFUSE = true;
  120. }
  121. }
  122. }
  123. // Effect
  124. if (scene.clipPlane) {
  125. this._defines.CLIPPLANE = true;
  126. }
  127. if (engine.getAlphaTesting()) {
  128. this._defines.ALPHATEST = true;
  129. }
  130. // Point size
  131. if (this.pointsCloud || scene.forcePointsCloud) {
  132. this._defines.POINTSIZE = true;
  133. }
  134. // Fog
  135. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  136. this._defines.FOG = true;
  137. }
  138. var lightIndex = 0;
  139. if (scene.lightsEnabled && !this.disableLighting) {
  140. needNormals = BABYLON.StandardMaterial.PrepareDefinesForLights(scene, mesh, this._defines);
  141. }
  142. // Attribs
  143. if (mesh) {
  144. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  145. this._defines.NORMAL = true;
  146. }
  147. if (needUVs) {
  148. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  149. this._defines.UV1 = true;
  150. }
  151. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  152. this._defines.UV2 = true;
  153. }
  154. }
  155. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  156. this._defines.VERTEXCOLOR = true;
  157. if (mesh.hasVertexAlpha) {
  158. this._defines.VERTEXALPHA = true;
  159. }
  160. }
  161. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  162. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  163. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  164. }
  165. // Instances
  166. if (useInstances) {
  167. this._defines.INSTANCES = true;
  168. }
  169. }
  170. // Get correct effect
  171. if (!this._defines.isEqual(this._cachedDefines)) {
  172. this._defines.cloneTo(this._cachedDefines);
  173. scene.resetCachedMaterial();
  174. // Fallbacks
  175. var fallbacks = new BABYLON.EffectFallbacks();
  176. if (this._defines.FOG) {
  177. fallbacks.addFallback(1, "FOG");
  178. }
  179. for (lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  180. if (!this._defines["LIGHT" + lightIndex]) {
  181. continue;
  182. }
  183. if (lightIndex > 0) {
  184. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  185. }
  186. if (this._defines["SHADOW" + lightIndex]) {
  187. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  188. }
  189. if (this._defines["SHADOWPCF" + lightIndex]) {
  190. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  191. }
  192. if (this._defines["SHADOWVSM" + lightIndex]) {
  193. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  194. }
  195. }
  196. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  197. fallbacks.addCPUSkinningFallback(0, mesh);
  198. }
  199. //Attributes
  200. var attribs = [BABYLON.VertexBuffer.PositionKind];
  201. if (this._defines.NORMAL) {
  202. attribs.push(BABYLON.VertexBuffer.NormalKind);
  203. }
  204. if (this._defines.UV1) {
  205. attribs.push(BABYLON.VertexBuffer.UVKind);
  206. }
  207. if (this._defines.UV2) {
  208. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  209. }
  210. if (this._defines.VERTEXCOLOR) {
  211. attribs.push(BABYLON.VertexBuffer.ColorKind);
  212. }
  213. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  214. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  215. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  216. if (this._defines.NUM_BONE_INFLUENCERS > 4) {
  217. attribs.push(BABYLON.VertexBuffer.MatricesIndicesExtraKind);
  218. attribs.push(BABYLON.VertexBuffer.MatricesWeightsExtraKind);
  219. }
  220. }
  221. if (this._defines.INSTANCES) {
  222. attribs.push("world0");
  223. attribs.push("world1");
  224. attribs.push("world2");
  225. attribs.push("world3");
  226. }
  227. // Legacy browser patch
  228. var shaderName = "simple";
  229. var join = this._defines.toString();
  230. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  231. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  232. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  233. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  234. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  235. "vFogInfos", "vFogColor", "pointSize",
  236. "vDiffuseInfos",
  237. "mBones",
  238. "vClipPlane", "diffuseMatrix",
  239. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues"
  240. ], ["diffuseSampler",
  241. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  242. ], join, fallbacks, this.onCompiled, this.onError);
  243. }
  244. if (!this._effect.isReady()) {
  245. return false;
  246. }
  247. this._renderId = scene.getRenderId();
  248. this._wasPreviouslyReady = true;
  249. if (mesh) {
  250. if (!mesh._materialDefines) {
  251. mesh._materialDefines = new SimpleMaterialDefines();
  252. }
  253. this._defines.cloneTo(mesh._materialDefines);
  254. }
  255. return true;
  256. };
  257. SimpleMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  258. this._effect.setMatrix("world", world);
  259. };
  260. SimpleMaterial.prototype.bind = function (world, mesh) {
  261. var scene = this.getScene();
  262. // Matrices
  263. this.bindOnlyWorldMatrix(world);
  264. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  265. // Bones
  266. BABYLON.StandardMaterial.ApplyBonesParameters(mesh, this._effect);
  267. if (scene.getCachedMaterial() !== this) {
  268. // Textures
  269. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  270. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  271. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  272. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  273. }
  274. // Clip plane
  275. if (scene.clipPlane) {
  276. var clipPlane = scene.clipPlane;
  277. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  278. }
  279. // Point size
  280. if (this.pointsCloud) {
  281. this._effect.setFloat("pointSize", this.pointSize);
  282. }
  283. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  284. }
  285. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  286. // Lights
  287. if (scene.lightsEnabled && !this.disableLighting) {
  288. BABYLON.StandardMaterial.BindLights(scene, mesh, this._effect, this._defines);
  289. }
  290. // View
  291. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  292. this._effect.setMatrix("view", scene.getViewMatrix());
  293. }
  294. // Fog
  295. BABYLON.StandardMaterial.ApplyFogParameters(scene, mesh, this._effect);
  296. _super.prototype.bind.call(this, world, mesh);
  297. };
  298. SimpleMaterial.prototype.getAnimatables = function () {
  299. var results = [];
  300. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  301. results.push(this.diffuseTexture);
  302. }
  303. return results;
  304. };
  305. SimpleMaterial.prototype.dispose = function (forceDisposeEffect) {
  306. if (this.diffuseTexture) {
  307. this.diffuseTexture.dispose();
  308. }
  309. _super.prototype.dispose.call(this, forceDisposeEffect);
  310. };
  311. SimpleMaterial.prototype.clone = function (name) {
  312. var newMaterial = new SimpleMaterial(name, this.getScene());
  313. // Base material
  314. this.copyTo(newMaterial);
  315. // Simple material
  316. if (this.diffuseTexture && this.diffuseTexture.clone) {
  317. newMaterial.diffuseTexture = this.diffuseTexture.clone();
  318. }
  319. newMaterial.diffuseColor = this.diffuseColor.clone();
  320. return newMaterial;
  321. };
  322. SimpleMaterial.prototype.serialize = function () {
  323. var serializationObject = _super.prototype.serialize.call(this);
  324. serializationObject.customType = "BABYLON.SimpleMaterial";
  325. serializationObject.diffuseColor = this.diffuseColor.asArray();
  326. serializationObject.disableLighting = this.disableLighting;
  327. if (this.diffuseTexture) {
  328. serializationObject.diffuseTexture = this.diffuseTexture.serialize();
  329. }
  330. return serializationObject;
  331. };
  332. SimpleMaterial.Parse = function (source, scene, rootUrl) {
  333. var material = new SimpleMaterial(source.name, scene);
  334. material.diffuseColor = BABYLON.Color3.FromArray(source.diffuseColor);
  335. material.disableLighting = source.disableLighting;
  336. material.alpha = source.alpha;
  337. material.id = source.id;
  338. BABYLON.Tags.AddTagsTo(material, source.tags);
  339. material.backFaceCulling = source.backFaceCulling;
  340. material.wireframe = source.wireframe;
  341. if (source.diffuseTexture) {
  342. material.diffuseTexture = BABYLON.Texture.Parse(source.diffuseTexture, scene, rootUrl);
  343. }
  344. if (source.checkReadyOnlyOnce) {
  345. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  346. }
  347. return material;
  348. };
  349. return SimpleMaterial;
  350. })(BABYLON.Material);
  351. BABYLON.SimpleMaterial = SimpleMaterial;
  352. })(BABYLON || (BABYLON = {}));
  353. 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#ifdef CLIPPLANE\r\nuniform vec4 vClipPlane;\r\nvarying float fClipDistance;\r\n#endif\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#ifdef CLIPPLANE\r\n\tfClipDistance = dot(worldPos, vClipPlane);\r\n#endif\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";
  354. 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\r\n#endif\r\n\r\n#ifdef CLIPPLANE\r\nvarying float fClipDistance;\r\n#endif\r\n\r\n// Fog\r\n#include<fogFragmentDeclaration>\r\n\r\nvoid main(void) {\r\n\t// Clip plane\r\n#ifdef CLIPPLANE\r\n\tif (fClipDistance > 0.0)\r\n\t\tdiscard;\r\n#endif\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// Bump\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}";