babylon.simpleMaterial.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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.POINTDIRLIGHT0 = false;
  27. this.POINTDIRLIGHT1 = false;
  28. this.POINTDIRLIGHT2 = false;
  29. this.POINTDIRLIGHT3 = false;
  30. this.SHADOW0 = false;
  31. this.SHADOW1 = false;
  32. this.SHADOW2 = false;
  33. this.SHADOW3 = false;
  34. this.SHADOWS = false;
  35. this.SHADOWVSM0 = false;
  36. this.SHADOWVSM1 = false;
  37. this.SHADOWVSM2 = false;
  38. this.SHADOWVSM3 = false;
  39. this.SHADOWPCF0 = false;
  40. this.SHADOWPCF1 = false;
  41. this.SHADOWPCF2 = false;
  42. this.SHADOWPCF3 = false;
  43. this.NORMAL = false;
  44. this.UV1 = false;
  45. this.UV2 = false;
  46. this.VERTEXCOLOR = false;
  47. this.VERTEXALPHA = false;
  48. this.BONES = false;
  49. this.BONES4 = false;
  50. this.BonesPerMesh = 0;
  51. this.INSTANCES = false;
  52. this._keys = Object.keys(this);
  53. }
  54. return SimpleMaterialDefines;
  55. })(BABYLON.MaterialDefines);
  56. var SimpleMaterial = (function (_super) {
  57. __extends(SimpleMaterial, _super);
  58. function SimpleMaterial(name, scene) {
  59. _super.call(this, name, scene);
  60. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  61. this.disableLighting = false;
  62. this._worldViewProjectionMatrix = BABYLON.Matrix.Zero();
  63. this._scaledDiffuse = new BABYLON.Color3();
  64. this._defines = new SimpleMaterialDefines();
  65. this._cachedDefines = new SimpleMaterialDefines();
  66. this._cachedDefines.BonesPerMesh = -1;
  67. }
  68. SimpleMaterial.prototype.needAlphaBlending = function () {
  69. return (this.alpha < 1.0);
  70. };
  71. SimpleMaterial.prototype.needAlphaTesting = function () {
  72. return false;
  73. };
  74. SimpleMaterial.prototype.getAlphaTestTexture = function () {
  75. return null;
  76. };
  77. // Methods
  78. SimpleMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  79. if (!mesh) {
  80. return true;
  81. }
  82. if (this._defines.INSTANCES !== useInstances) {
  83. return false;
  84. }
  85. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  86. return true;
  87. }
  88. return false;
  89. };
  90. SimpleMaterial.prototype.isReady = function (mesh, useInstances) {
  91. if (this.checkReadyOnlyOnce) {
  92. if (this._wasPreviouslyReady) {
  93. return true;
  94. }
  95. }
  96. var scene = this.getScene();
  97. if (!this.checkReadyOnEveryCall) {
  98. if (this._renderId === scene.getRenderId()) {
  99. if (this._checkCache(scene, mesh, useInstances)) {
  100. return true;
  101. }
  102. }
  103. }
  104. var engine = scene.getEngine();
  105. var needNormals = false;
  106. var needUVs = false;
  107. this._defines.reset();
  108. // Textures
  109. if (scene.texturesEnabled) {
  110. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  111. if (!this.diffuseTexture.isReady()) {
  112. return false;
  113. }
  114. else {
  115. needUVs = true;
  116. this._defines.DIFFUSE = true;
  117. }
  118. }
  119. }
  120. // Effect
  121. if (scene.clipPlane) {
  122. this._defines.CLIPPLANE = true;
  123. }
  124. if (engine.getAlphaTesting()) {
  125. this._defines.ALPHATEST = true;
  126. }
  127. // Point size
  128. if (this.pointsCloud || scene.forcePointsCloud) {
  129. this._defines.POINTSIZE = true;
  130. }
  131. // Fog
  132. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  133. this._defines.FOG = true;
  134. }
  135. var lightIndex = 0;
  136. if (scene.lightsEnabled && !this.disableLighting) {
  137. for (var index = 0; index < scene.lights.length; index++) {
  138. var light = scene.lights[index];
  139. if (!light.isEnabled()) {
  140. continue;
  141. }
  142. // Excluded check
  143. if (light._excludedMeshesIds.length > 0) {
  144. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  145. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  146. if (excludedMesh) {
  147. light.excludedMeshes.push(excludedMesh);
  148. }
  149. }
  150. light._excludedMeshesIds = [];
  151. }
  152. // Included check
  153. if (light._includedOnlyMeshesIds.length > 0) {
  154. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  155. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  156. if (includedOnlyMesh) {
  157. light.includedOnlyMeshes.push(includedOnlyMesh);
  158. }
  159. }
  160. light._includedOnlyMeshesIds = [];
  161. }
  162. if (!light.canAffectMesh(mesh)) {
  163. continue;
  164. }
  165. needNormals = true;
  166. this._defines["LIGHT" + lightIndex] = true;
  167. var type;
  168. if (light instanceof BABYLON.SpotLight) {
  169. type = "SPOTLIGHT" + lightIndex;
  170. }
  171. else if (light instanceof BABYLON.HemisphericLight) {
  172. type = "HEMILIGHT" + lightIndex;
  173. }
  174. else {
  175. type = "POINTDIRLIGHT" + lightIndex;
  176. }
  177. this._defines[type] = true;
  178. // Shadows
  179. if (scene.shadowsEnabled) {
  180. var shadowGenerator = light.getShadowGenerator();
  181. if (mesh && mesh.receiveShadows && shadowGenerator) {
  182. this._defines["SHADOW" + lightIndex] = true;
  183. this._defines.SHADOWS = true;
  184. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  185. this._defines["SHADOWVSM" + lightIndex] = true;
  186. }
  187. if (shadowGenerator.usePoissonSampling) {
  188. this._defines["SHADOWPCF" + lightIndex] = true;
  189. }
  190. }
  191. }
  192. lightIndex++;
  193. if (lightIndex === maxSimultaneousLights)
  194. break;
  195. }
  196. }
  197. // Attribs
  198. if (mesh) {
  199. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  200. this._defines.NORMAL = true;
  201. }
  202. if (needUVs) {
  203. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  204. this._defines.UV1 = true;
  205. }
  206. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  207. this._defines.UV2 = true;
  208. }
  209. }
  210. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  211. this._defines.VERTEXCOLOR = true;
  212. if (mesh.hasVertexAlpha) {
  213. this._defines.VERTEXALPHA = true;
  214. }
  215. }
  216. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  217. this._defines.BONES = true;
  218. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  219. this._defines.BONES4 = true;
  220. }
  221. // Instances
  222. if (useInstances) {
  223. this._defines.INSTANCES = true;
  224. }
  225. }
  226. // Get correct effect
  227. if (!this._defines.isEqual(this._cachedDefines)) {
  228. this._defines.cloneTo(this._cachedDefines);
  229. scene.resetCachedMaterial();
  230. // Fallbacks
  231. var fallbacks = new BABYLON.EffectFallbacks();
  232. if (this._defines.FOG) {
  233. fallbacks.addFallback(1, "FOG");
  234. }
  235. for (lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  236. if (!this._defines["LIGHT" + lightIndex]) {
  237. continue;
  238. }
  239. if (lightIndex > 0) {
  240. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  241. }
  242. if (this._defines["SHADOW" + lightIndex]) {
  243. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  244. }
  245. if (this._defines["SHADOWPCF" + lightIndex]) {
  246. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  247. }
  248. if (this._defines["SHADOWVSM" + lightIndex]) {
  249. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  250. }
  251. }
  252. if (this._defines.BONES4) {
  253. fallbacks.addFallback(0, "BONES4");
  254. }
  255. //Attributes
  256. var attribs = [BABYLON.VertexBuffer.PositionKind];
  257. if (this._defines.NORMAL) {
  258. attribs.push(BABYLON.VertexBuffer.NormalKind);
  259. }
  260. if (this._defines.UV1) {
  261. attribs.push(BABYLON.VertexBuffer.UVKind);
  262. }
  263. if (this._defines.UV2) {
  264. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  265. }
  266. if (this._defines.VERTEXCOLOR) {
  267. attribs.push(BABYLON.VertexBuffer.ColorKind);
  268. }
  269. if (this._defines.BONES) {
  270. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  271. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  272. }
  273. if (this._defines.INSTANCES) {
  274. attribs.push("world0");
  275. attribs.push("world1");
  276. attribs.push("world2");
  277. attribs.push("world3");
  278. }
  279. // Legacy browser patch
  280. var shaderName = "default";
  281. if (!scene.getEngine().getCaps().standardDerivatives) {
  282. shaderName = "legacydefault";
  283. }
  284. var join = this._defines.toString();
  285. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  286. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  287. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  288. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  289. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  290. "vFogInfos", "vFogColor", "pointSize",
  291. "vDiffuseInfos",
  292. "mBones",
  293. "vClipPlane", "diffuseMatrix",
  294. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3"
  295. ], ["diffuseSampler",
  296. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  297. ], join, fallbacks, this.onCompiled, this.onError);
  298. }
  299. if (!this._effect.isReady()) {
  300. return false;
  301. }
  302. this._renderId = scene.getRenderId();
  303. this._wasPreviouslyReady = true;
  304. if (mesh) {
  305. if (!mesh._materialDefines) {
  306. mesh._materialDefines = new SimpleMaterialDefines();
  307. }
  308. this._defines.cloneTo(mesh._materialDefines);
  309. }
  310. return true;
  311. };
  312. SimpleMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  313. this._effect.setMatrix("world", world);
  314. };
  315. SimpleMaterial.prototype.bind = function (world, mesh) {
  316. var scene = this.getScene();
  317. // Matrices
  318. this.bindOnlyWorldMatrix(world);
  319. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  320. // Bones
  321. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  322. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  323. }
  324. if (scene.getCachedMaterial() !== this) {
  325. // Textures
  326. if (this.diffuseTexture && BABYLON.StandardMaterial.DiffuseTextureEnabled) {
  327. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  328. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  329. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  330. }
  331. // Clip plane
  332. if (scene.clipPlane) {
  333. var clipPlane = scene.clipPlane;
  334. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  335. }
  336. // Point size
  337. if (this.pointsCloud) {
  338. this._effect.setFloat("pointSize", this.pointSize);
  339. }
  340. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  341. }
  342. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  343. if (scene.lightsEnabled && !this.disableLighting) {
  344. var lightIndex = 0;
  345. for (var index = 0; index < scene.lights.length; index++) {
  346. var light = scene.lights[index];
  347. if (!light.isEnabled()) {
  348. continue;
  349. }
  350. if (!light.canAffectMesh(mesh)) {
  351. continue;
  352. }
  353. if (light instanceof BABYLON.PointLight) {
  354. // Point Light
  355. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  356. }
  357. else if (light instanceof BABYLON.DirectionalLight) {
  358. // Directional Light
  359. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  360. }
  361. else if (light instanceof BABYLON.SpotLight) {
  362. // Spot Light
  363. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  364. }
  365. else if (light instanceof BABYLON.HemisphericLight) {
  366. // Hemispheric Light
  367. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  368. }
  369. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  370. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  371. // Shadows
  372. if (scene.shadowsEnabled) {
  373. var shadowGenerator = light.getShadowGenerator();
  374. if (mesh.receiveShadows && shadowGenerator) {
  375. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  376. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  377. this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  378. }
  379. }
  380. lightIndex++;
  381. if (lightIndex === maxSimultaneousLights)
  382. break;
  383. }
  384. }
  385. // View
  386. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  387. this._effect.setMatrix("view", scene.getViewMatrix());
  388. }
  389. // Fog
  390. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  391. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  392. this._effect.setColor3("vFogColor", scene.fogColor);
  393. }
  394. _super.prototype.bind.call(this, world, mesh);
  395. };
  396. SimpleMaterial.prototype.getAnimatables = function () {
  397. var results = [];
  398. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  399. results.push(this.diffuseTexture);
  400. }
  401. return results;
  402. };
  403. SimpleMaterial.prototype.dispose = function (forceDisposeEffect) {
  404. if (this.diffuseTexture) {
  405. this.diffuseTexture.dispose();
  406. }
  407. _super.prototype.dispose.call(this, forceDisposeEffect);
  408. };
  409. SimpleMaterial.prototype.clone = function (name) {
  410. var newMaterial = new SimpleMaterial(name, this.getScene());
  411. // Base material
  412. this.copyTo(newMaterial);
  413. // Simple material
  414. if (this.diffuseTexture && this.diffuseTexture.clone) {
  415. newMaterial.diffuseTexture = this.diffuseTexture.clone();
  416. }
  417. newMaterial.diffuseColor = this.diffuseColor.clone();
  418. return newMaterial;
  419. };
  420. return SimpleMaterial;
  421. })(BABYLON.Material);
  422. BABYLON.SimpleMaterial = SimpleMaterial;
  423. })(BABYLON || (BABYLON = {}));
  424. 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#ifdef BONES\r\nattribute vec4 matricesIndices;\r\nattribute vec4 matricesWeights;\r\n#endif\r\n\r\n// Uniforms\r\n\r\n#ifdef INSTANCES\r\nattribute vec4 world0;\r\nattribute vec4 world1;\r\nattribute vec4 world2;\r\nattribute vec4 world3;\r\n#else\r\nuniform mat4 world;\r\n#endif\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 BONES\r\nuniform mat4 mBones[BonesPerMesh];\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#ifdef FOG\r\nvarying float fFogDistance;\r\n#endif\r\n\r\n#ifdef SHADOWS\r\n#ifdef LIGHT0\r\nuniform mat4 lightMatrix0;\r\nvarying vec4 vPositionFromLight0;\r\n#endif\r\n#ifdef LIGHT1\r\nuniform mat4 lightMatrix1;\r\nvarying vec4 vPositionFromLight1;\r\n#endif\r\n#ifdef LIGHT2\r\nuniform mat4 lightMatrix2;\r\nvarying vec4 vPositionFromLight2;\r\n#endif\r\n#ifdef LIGHT3\r\nuniform mat4 lightMatrix3;\r\nvarying vec4 vPositionFromLight3;\r\n#endif\r\n#endif\r\n\r\nvoid main(void) {\r\n\tmat4 finalWorld;\r\n\r\n#ifdef INSTANCES\r\n\tfinalWorld = mat4(world0, world1, world2, world3);\r\n#else\r\n\tfinalWorld = world;\r\n#endif\r\n\r\n#ifdef BONES\r\n\tmat4 m0 = mBones[int(matricesIndices.x)] * matricesWeights.x;\r\n\tmat4 m1 = mBones[int(matricesIndices.y)] * matricesWeights.y;\r\n\tmat4 m2 = mBones[int(matricesIndices.z)] * matricesWeights.z;\r\n\r\n#ifdef BONES4\r\n\tmat4 m3 = mBones[int(matricesIndices.w)] * matricesWeights.w;\r\n\tfinalWorld = finalWorld * (m0 + m1 + m2 + m3);\r\n#else\r\n\tfinalWorld = finalWorld * (m0 + m1 + m2);\r\n#endif \r\n\r\n#endif\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\t// Fog\r\n#ifdef FOG\r\n\tfFogDistance = (view * worldPos).z;\r\n#endif\r\n\r\n\t// Shadows\r\n#ifdef SHADOWS\r\n#ifdef LIGHT0\r\n\tvPositionFromLight0 = lightMatrix0 * worldPos;\r\n#endif\r\n#ifdef LIGHT1\r\n\tvPositionFromLight1 = lightMatrix1 * worldPos;\r\n#endif\r\n#ifdef LIGHT2\r\n\tvPositionFromLight2 = lightMatrix2 * worldPos;\r\n#endif\r\n#ifdef LIGHT3\r\n\tvPositionFromLight3 = lightMatrix3 * worldPos;\r\n#endif\r\n#endif\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";
  425. 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#ifdef LIGHT0\r\nuniform vec4 vLightData0;\r\nuniform vec4 vLightDiffuse0;\r\n#ifdef SHADOW0\r\nvarying vec4 vPositionFromLight0;\r\nuniform sampler2D shadowSampler0;\r\nuniform vec3 shadowsInfo0;\r\n#endif\r\n#ifdef SPOTLIGHT0\r\nuniform vec4 vLightDirection0;\r\n#endif\r\n#ifdef HEMILIGHT0\r\nuniform vec3 vLightGround0;\r\n#endif\r\n#endif\r\n\r\n#ifdef LIGHT1\r\nuniform vec4 vLightData1;\r\nuniform vec4 vLightDiffuse1;\r\n#ifdef SHADOW1\r\nvarying vec4 vPositionFromLight1;\r\nuniform sampler2D shadowSampler1;\r\nuniform vec3 shadowsInfo1;\r\n#endif\r\n#ifdef SPOTLIGHT1\r\nuniform vec4 vLightDirection1;\r\n#endif\r\n#ifdef HEMILIGHT1\r\nuniform vec3 vLightGround1;\r\n#endif\r\n#endif\r\n\r\n#ifdef LIGHT2\r\nuniform vec4 vLightData2;\r\nuniform vec4 vLightDiffuse2;\r\n#ifdef SHADOW2\r\nvarying vec4 vPositionFromLight2;\r\nuniform sampler2D shadowSampler2;\r\nuniform vec3 shadowsInfo2;\r\n#endif\r\n#ifdef SPOTLIGHT2\r\nuniform vec4 vLightDirection2;\r\n#endif\r\n#ifdef HEMILIGHT2\r\nuniform vec3 vLightGround2;\r\n#endif\r\n#endif\r\n\r\n#ifdef LIGHT3\r\nuniform vec4 vLightData3;\r\nuniform vec4 vLightDiffuse3;\r\n#ifdef SHADOW3\r\nvarying vec4 vPositionFromLight3;\r\nuniform sampler2D shadowSampler3;\r\nuniform vec3 shadowsInfo3;\r\n#endif\r\n#ifdef SPOTLIGHT3\r\nuniform vec4 vLightDirection3;\r\n#endif\r\n#ifdef HEMILIGHT3\r\nuniform vec3 vLightGround3;\r\n#endif\r\n#endif\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// Shadows\r\n#ifdef SHADOWS\r\n\r\nfloat unpack(vec4 color)\r\n{\r\n\tconst vec4 bit_shift = vec4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);\r\n\treturn dot(color, bit_shift);\r\n}\r\n\r\nfloat unpackHalf(vec2 color)\r\n{\r\n\treturn color.x + (color.y / 255.0);\r\n}\r\n\r\nfloat computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness, float bias)\r\n{\r\n\tvec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\r\n\tdepth = 0.5 * depth + vec3(0.5);\r\n\tvec2 uv = depth.xy;\r\n\r\n\tif (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)\r\n\t{\r\n\t\treturn 1.0;\r\n\t}\r\n\r\n\tfloat shadow = unpack(texture2D(shadowSampler, uv)) + bias;\r\n\r\n\tif (depth.z > shadow)\r\n\t{\r\n\t\treturn darkness;\r\n\t}\r\n\treturn 1.;\r\n}\r\n\r\nfloat computeShadowWithPCF(vec4 vPositionFromLight, sampler2D shadowSampler, float mapSize, float bias, float darkness)\r\n{\r\n\tvec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\r\n\tdepth = 0.5 * depth + vec3(0.5);\r\n\tvec2 uv = depth.xy;\r\n\r\n\tif (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)\r\n\t{\r\n\t\treturn 1.0;\r\n\t}\r\n\r\n\tfloat visibility = 1.;\r\n\r\n\tvec2 poissonDisk[4];\r\n\tpoissonDisk[0] = vec2(-0.94201624, -0.39906216);\r\n\tpoissonDisk[1] = vec2(0.94558609, -0.76890725);\r\n\tpoissonDisk[2] = vec2(-0.094184101, -0.92938870);\r\n\tpoissonDisk[3] = vec2(0.34495938, 0.29387760);\r\n\r\n\t// Poisson Sampling\r\n\tfloat biasedDepth = depth.z - bias;\r\n\r\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[0] / mapSize)) < biasedDepth) visibility -= 0.25;\r\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[1] / mapSize)) < biasedDepth) visibility -= 0.25;\r\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[2] / mapSize)) < biasedDepth) visibility -= 0.25;\r\n\tif (unpack(texture2D(shadowSampler, uv + poissonDisk[3] / mapSize)) < biasedDepth) visibility -= 0.25;\r\n\r\n\treturn min(1.0, visibility + darkness);\r\n}\r\n\r\n// Thanks to http://devmaster.net/\r\nfloat linstep(float low, float high, float v) {\r\n\treturn clamp((v - low) / (high - low), 0.0, 1.0);\r\n}\r\n\r\nfloat ChebychevInequality(vec2 moments, float compare, float bias)\r\n{\r\n\tfloat p = smoothstep(compare - bias, compare, moments.x);\r\n\tfloat variance = max(moments.y - moments.x * moments.x, 0.02);\r\n\tfloat d = compare - moments.x;\r\n\tfloat p_max = linstep(0.2, 1.0, variance / (variance + d * d));\r\n\r\n\treturn clamp(max(p, p_max), 0.0, 1.0);\r\n}\r\n\r\nfloat computeShadowWithVSM(vec4 vPositionFromLight, sampler2D shadowSampler, float bias, float darkness)\r\n{\r\n\tvec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;\r\n\tdepth = 0.5 * depth + vec3(0.5);\r\n\tvec2 uv = depth.xy;\r\n\r\n\tif (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0 || depth.z >= 1.0)\r\n\t{\r\n\t\treturn 1.0;\r\n\t}\r\n\r\n\tvec4 texel = texture2D(shadowSampler, uv);\r\n\r\n\tvec2 moments = vec2(unpackHalf(texel.xy), unpackHalf(texel.zw));\r\n\treturn min(1.0, 1.0 - ChebychevInequality(moments, depth.z, bias) + darkness);\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#ifdef FOG\r\n\r\n#define FOGMODE_NONE 0.\r\n#define FOGMODE_EXP 1.\r\n#define FOGMODE_EXP2 2.\r\n#define FOGMODE_LINEAR 3.\r\n#define E 2.71828\r\n\r\nuniform vec4 vFogInfos;\r\nuniform vec3 vFogColor;\r\nvarying float fFogDistance;\r\n\r\nfloat CalcFogFactor()\r\n{\r\n\tfloat fogCoeff = 1.0;\r\n\tfloat fogStart = vFogInfos.y;\r\n\tfloat fogEnd = vFogInfos.z;\r\n\tfloat fogDensity = vFogInfos.w;\r\n\r\n\tif (FOGMODE_LINEAR == vFogInfos.x)\r\n\t{\r\n\t\tfogCoeff = (fogEnd - fFogDistance) / (fogEnd - fogStart);\r\n\t}\r\n\telse if (FOGMODE_EXP == vFogInfos.x)\r\n\t{\r\n\t\tfogCoeff = 1.0 / pow(E, fFogDistance * fogDensity);\r\n\t}\r\n\telse if (FOGMODE_EXP2 == vFogInfos.x)\r\n\t{\r\n\t\tfogCoeff = 1.0 / pow(E, fFogDistance * fFogDistance * fogDensity * fogDensity);\r\n\t}\r\n\r\n\treturn clamp(fogCoeff, 0.0, 1.0);\r\n}\r\n#endif\r\n\r\n// Light Computing\r\nstruct lightingInfo\r\n{\r\n\tvec3 diffuse;\r\n};\r\n\r\nlightingInfo computeLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, float range) {\r\n\tlightingInfo result;\r\n\r\n\tvec3 lightVectorW;\r\n\tfloat attenuation = 1.0;\r\n\tif (lightData.w == 0.)\r\n\t{\r\n\t\tvec3 direction = lightData.xyz - vPositionW;\r\n\r\n\t\tattenuation = max(0., 1.0 - length(direction) / range);\r\n\t\tlightVectorW = normalize(direction);\r\n\t}\r\n\telse\r\n\t{\r\n\t\tlightVectorW = normalize(-lightData.xyz);\r\n\t}\r\n\r\n\t// diffuse\r\n\tfloat ndl = max(0., dot(vNormal, lightVectorW));\r\n\tresult.diffuse = ndl * diffuseColor * attenuation;\r\n\r\n\treturn result;\r\n}\r\n\r\nlightingInfo computeSpotLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec4 lightDirection, vec3 diffuseColor, float range) {\r\n\tlightingInfo result;\r\n\r\n\tvec3 direction = lightData.xyz - vPositionW;\r\n\tvec3 lightVectorW = normalize(direction);\r\n\tfloat attenuation = max(0., 1.0 - length(direction) / range);\r\n\r\n\t// diffuse\r\n\tfloat cosAngle = max(0., dot(-lightDirection.xyz, lightVectorW));\r\n\tfloat spotAtten = 0.0;\r\n\r\n\tif (cosAngle >= lightDirection.w)\r\n\t{\r\n\t\tcosAngle = max(0., pow(cosAngle, lightData.w));\r\n\t\tspotAtten = clamp((cosAngle - lightDirection.w) / (1. - cosAngle), 0.0, 1.0);\r\n\r\n\t\t// Diffuse\r\n\t\tfloat ndl = max(0., dot(vNormal, -lightDirection.xyz));\r\n\t\tresult.diffuse = ndl * spotAtten * diffuseColor * attenuation;\r\n\r\n\t\treturn result;\r\n\t}\r\n\r\n\tresult.diffuse = vec3(0.);\r\n\r\n\treturn result;\r\n}\r\n\r\nlightingInfo computeHemisphericLighting(vec3 viewDirectionW, vec3 vNormal, vec4 lightData, vec3 diffuseColor, vec3 groundColor) {\r\n\tlightingInfo result;\r\n\r\n\t// Diffuse\r\n\tfloat ndl = dot(vNormal, lightData.xyz) * 0.5 + 0.5;\r\n\tresult.diffuse = mix(groundColor, diffuseColor, ndl);\r\n\r\n\treturn result;\r\n}\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\r\n#ifdef LIGHT0\r\n#ifdef SPOTLIGHT0\r\n\tlightingInfo info = computeSpotLighting(viewDirectionW, normalW, vLightData0, vLightDirection0, vLightDiffuse0.rgb, vLightDiffuse0.a);\r\n#endif\r\n#ifdef HEMILIGHT0\r\n\tlightingInfo info = computeHemisphericLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightGround0);\r\n#endif\r\n#ifdef POINTDIRLIGHT0\r\n\tlightingInfo info = computeLighting(viewDirectionW, normalW, vLightData0, vLightDiffuse0.rgb, vLightDiffuse0.a);\r\n#endif\r\n#ifdef SHADOW0\r\n#ifdef SHADOWVSM0\r\n\tshadow = computeShadowWithVSM(vPositionFromLight0, shadowSampler0, shadowsInfo0.z, shadowsInfo0.x);\r\n#else\r\n#ifdef SHADOWPCF0\r\n\tshadow = computeShadowWithPCF(vPositionFromLight0, shadowSampler0, shadowsInfo0.y, shadowsInfo0.z, shadowsInfo0.x);\r\n#else\r\n\tshadow = computeShadow(vPositionFromLight0, shadowSampler0, shadowsInfo0.x, shadowsInfo0.z);\r\n#endif\r\n#endif\r\n#else\r\n\tshadow = 1.;\r\n#endif\r\n\tdiffuseBase += info.diffuse * shadow;\r\n#endif\r\n\r\n#ifdef LIGHT1\r\n\r\n#ifdef SPOTLIGHT1\r\n\tinfo = computeSpotLighting(viewDirectionW, normalW, vLightData1, vLightDirection1, vLightDiffuse1.rgb, vLightDiffuse1.a);\r\n#endif\r\n#ifdef HEMILIGHT1\r\n\tinfo = computeHemisphericLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightGround1);\r\n#endif\r\n#ifdef POINTDIRLIGHT1\r\n\tinfo = computeLighting(viewDirectionW, normalW, vLightData1, vLightDiffuse1.rgb, vLightDiffuse1.a);\r\n#endif\r\n#ifdef SHADOW1\r\n#ifdef SHADOWVSM1\r\n\tshadow = computeShadowWithVSM(vPositionFromLight1, shadowSampler1, shadowsInfo1.z, shadowsInfo1.x);\r\n#else\r\n#ifdef SHADOWPCF1\r\n\tshadow = computeShadowWithPCF(vPositionFromLight1, shadowSampler1, shadowsInfo1.y, shadowsInfo1.z, shadowsInfo1.x);\r\n#else\r\n\tshadow = computeShadow(vPositionFromLight1, shadowSampler1, shadowsInfo1.x, shadowsInfo1.z);\r\n#endif\r\n#endif\r\n#else\r\n\tshadow = 1.;\r\n#endif\r\n\tdiffuseBase += info.diffuse * shadow;\r\n\r\n#endif\r\n\r\n#ifdef LIGHT2\r\n#ifdef SPOTLIGHT2\r\n\tinfo = computeSpotLighting(viewDirectionW, normalW, vLightData2, vLightDirection2, vLightDiffuse2.rgb, vLightDiffuse2.a);\r\n#endif\r\n#ifdef HEMILIGHT2\r\n\tinfo = computeHemisphericLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightGround2);\r\n#endif\r\n#ifdef POINTDIRLIGHT2\r\n\tinfo = computeLighting(viewDirectionW, normalW, vLightData2, vLightDiffuse2.rgb, vLightDiffuse2.a);\r\n#endif\r\n#ifdef SHADOW2\r\n#ifdef SHADOWVSM2\r\n\tshadow = computeShadowWithVSM(vPositionFromLight2, shadowSampler2, shadowsInfo2.z, shadowsInfo2.x);\r\n#else\r\n#ifdef SHADOWPCF2\r\n\tshadow = computeShadowWithPCF(vPositionFromLight2, shadowSampler2, shadowsInfo2.y, shadowsInfo2.z, shadowsInfo2.x);\r\n#else\r\n\tshadow = computeShadow(vPositionFromLight2, shadowSampler2, shadowsInfo2.x, shadowsInfo2.z);\r\n#endif\t\r\n#endif\t\r\n#else\r\n\tshadow = 1.;\r\n#endif\r\n\tdiffuseBase += info.diffuse * shadow;\r\n\r\n#endif\r\n\r\n#ifdef LIGHT3\r\n\r\n#ifdef SPOTLIGHT3\r\n\tinfo = computeSpotLighting(viewDirectionW, normalW, vLightData3, vLightDirection3, vLightDiffuse3.rgb, vLightDiffuse3.a);\r\n#endif\r\n#ifdef HEMILIGHT3\r\n\tinfo = computeHemisphericLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightGround3);\r\n#endif\r\n#ifdef POINTDIRLIGHT3\r\n\tinfo = computeLighting(viewDirectionW, normalW, vLightData3, vLightDiffuse3.rgb, vLightDiffuse3.a);\r\n#endif\r\n#ifdef SHADOW3\r\n#ifdef SHADOWVSM3\r\n\tshadow = computeShadowWithVSM(vPositionFromLight3, shadowSampler3, shadowsInfo3.z, shadowsInfo3.x);\r\n#else\r\n#ifdef SHADOWPCF3\r\n\tshadow = computeShadowWithPCF(vPositionFromLight3, shadowSampler3, shadowsInfo3.y, shadowsInfo3.z, shadowsInfo3.x);\r\n#else\r\n\tshadow = computeShadow(vPositionFromLight3, shadowSampler3, shadowsInfo3.x, shadowsInfo3.z);\r\n#endif\t\r\n#endif\t\r\n#else\r\n\tshadow = 1.;\r\n#endif\r\n\tdiffuseBase += info.diffuse * shadow;\r\n#endif\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#ifdef FOG\r\n\tfloat fog = CalcFogFactor();\r\n\tcolor.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;\r\n#endif\r\n\r\n\tgl_FragColor = color;\r\n}";