babylon.waterMaterial.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. /// <reference path="../simple/babylon.simpleMaterial.ts"/>
  3. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
  5. switch (arguments.length) {
  6. case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
  7. case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
  8. case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
  9. }
  10. };
  11. var BABYLON;
  12. (function (BABYLON) {
  13. var maxSimultaneousLights = 4;
  14. var WaterMaterialDefines = (function (_super) {
  15. __extends(WaterMaterialDefines, _super);
  16. function WaterMaterialDefines() {
  17. _super.call(this);
  18. this.BUMP = false;
  19. this.REFLECTION = false;
  20. this.CLIPPLANE = false;
  21. this.ALPHATEST = false;
  22. this.POINTSIZE = false;
  23. this.FOG = false;
  24. this.LIGHT0 = false;
  25. this.LIGHT1 = false;
  26. this.LIGHT2 = false;
  27. this.LIGHT3 = false;
  28. this.SPOTLIGHT0 = false;
  29. this.SPOTLIGHT1 = false;
  30. this.SPOTLIGHT2 = false;
  31. this.SPOTLIGHT3 = false;
  32. this.HEMILIGHT0 = false;
  33. this.HEMILIGHT1 = false;
  34. this.HEMILIGHT2 = false;
  35. this.HEMILIGHT3 = false;
  36. this.DIRLIGHT0 = false;
  37. this.DIRLIGHT1 = false;
  38. this.DIRLIGHT2 = false;
  39. this.DIRLIGHT3 = false;
  40. this.POINTLIGHT0 = false;
  41. this.POINTLIGHT1 = false;
  42. this.POINTLIGHT2 = false;
  43. this.POINTLIGHT3 = false;
  44. this.SHADOW0 = false;
  45. this.SHADOW1 = false;
  46. this.SHADOW2 = false;
  47. this.SHADOW3 = false;
  48. this.SHADOWS = false;
  49. this.SHADOWVSM0 = false;
  50. this.SHADOWVSM1 = false;
  51. this.SHADOWVSM2 = false;
  52. this.SHADOWVSM3 = false;
  53. this.SHADOWPCF0 = false;
  54. this.SHADOWPCF1 = false;
  55. this.SHADOWPCF2 = false;
  56. this.SHADOWPCF3 = false;
  57. this.NORMAL = false;
  58. this.UV1 = false;
  59. this.UV2 = false;
  60. this.VERTEXCOLOR = false;
  61. this.VERTEXALPHA = false;
  62. this.BONES = false;
  63. this.BONES4 = false;
  64. this.BonesPerMesh = 0;
  65. this.INSTANCES = false;
  66. this.SPECULARTERM = false;
  67. this._keys = Object.keys(this);
  68. }
  69. return WaterMaterialDefines;
  70. })(BABYLON.MaterialDefines);
  71. var WaterMaterial = (function (_super) {
  72. __extends(WaterMaterial, _super);
  73. /**
  74. * Constructor
  75. */
  76. function WaterMaterial(name, scene, renderTargetSize) {
  77. if (renderTargetSize === void 0) { renderTargetSize = new BABYLON.Vector2(512, 512); }
  78. _super.call(this, name, scene);
  79. this.renderTargetSize = renderTargetSize;
  80. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  81. this.specularColor = new BABYLON.Color3(0, 0, 0);
  82. this.specularPower = 64;
  83. this.disableLighting = false;
  84. /**
  85. * @param {number}: Represents the wind force
  86. */
  87. this.windForce = 6;
  88. /**
  89. * @param {Vector2}: The direction of the wind in the plane (X, Z)
  90. */
  91. this.windDirection = new BABYLON.Vector2(0, 1);
  92. /**
  93. * @param {number}: Wave height, represents the height of the waves
  94. */
  95. this.waveHeight = 0.4;
  96. /**
  97. * @param {number}: Bump height, represents the bump height related to the bump map
  98. */
  99. this.bumpHeight = 0.4;
  100. /**
  101. * @param {number}: The water color blended with the reflection and refraction samplers
  102. */
  103. this.waterColor = new BABYLON.Color3(0.1, 0.1, 0.6);
  104. /**
  105. * @param {number}: The blend factor related to the water color
  106. */
  107. this.colorBlendFactor = 0.2;
  108. /**
  109. * @param {number}: Represents the maximum length of a wave
  110. */
  111. this.waveLength = 0.1;
  112. /**
  113. * @param {number}: Defines the waves speed
  114. */
  115. this.waveSpeed = 1.0;
  116. /*
  117. * Private members
  118. */
  119. this._mesh = null;
  120. this._reflectionTransform = BABYLON.Matrix.Zero();
  121. this._lastTime = 0;
  122. this._scaledDiffuse = new BABYLON.Color3();
  123. this._scaledSpecular = new BABYLON.Color3();
  124. this._defines = new WaterMaterialDefines();
  125. this._cachedDefines = new WaterMaterialDefines();
  126. // Create render targets
  127. this._createRenderTargets(scene, renderTargetSize);
  128. }
  129. Object.defineProperty(WaterMaterial.prototype, "refractionTexture", {
  130. // Get / Set
  131. get: function () {
  132. return this._refractionRTT;
  133. },
  134. enumerable: true,
  135. configurable: true
  136. });
  137. Object.defineProperty(WaterMaterial.prototype, "reflectionTexture", {
  138. get: function () {
  139. return this._reflectionRTT;
  140. },
  141. enumerable: true,
  142. configurable: true
  143. });
  144. // Methods
  145. WaterMaterial.prototype.addToRenderList = function (node) {
  146. this._refractionRTT.renderList.push(node);
  147. this._reflectionRTT.renderList.push(node);
  148. };
  149. WaterMaterial.prototype.enableRenderTargets = function (enable) {
  150. var refreshRate = enable ? 1 : 0;
  151. this._refractionRTT.refreshRate = refreshRate;
  152. this._reflectionRTT.refreshRate = refreshRate;
  153. };
  154. WaterMaterial.prototype.needAlphaBlending = function () {
  155. return (this.alpha < 1.0);
  156. };
  157. WaterMaterial.prototype.needAlphaTesting = function () {
  158. return false;
  159. };
  160. WaterMaterial.prototype.getAlphaTestTexture = function () {
  161. return null;
  162. };
  163. WaterMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  164. if (!mesh) {
  165. return true;
  166. }
  167. if (this._defines.INSTANCES !== useInstances) {
  168. return false;
  169. }
  170. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  171. return true;
  172. }
  173. return false;
  174. };
  175. WaterMaterial.prototype.isReady = function (mesh, useInstances) {
  176. if (this.checkReadyOnlyOnce) {
  177. if (this._wasPreviouslyReady) {
  178. return true;
  179. }
  180. }
  181. var scene = this.getScene();
  182. if (!this.checkReadyOnEveryCall) {
  183. if (this._renderId === scene.getRenderId()) {
  184. if (this._checkCache(scene, mesh, useInstances)) {
  185. return true;
  186. }
  187. }
  188. }
  189. var engine = scene.getEngine();
  190. var needNormals = false;
  191. var needUVs = false;
  192. this._defines.reset();
  193. // Textures
  194. if (scene.texturesEnabled) {
  195. if (this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled) {
  196. if (!this.bumpTexture.isReady()) {
  197. return false;
  198. }
  199. else {
  200. needUVs = true;
  201. this._defines.BUMP = true;
  202. }
  203. }
  204. if (BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  205. this._defines.REFLECTION = true;
  206. }
  207. }
  208. // Effect
  209. if (scene.clipPlane) {
  210. this._defines.CLIPPLANE = true;
  211. }
  212. if (engine.getAlphaTesting()) {
  213. this._defines.ALPHATEST = true;
  214. }
  215. // Point size
  216. if (this.pointsCloud || scene.forcePointsCloud) {
  217. this._defines.POINTSIZE = true;
  218. }
  219. // Fog
  220. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  221. this._defines.FOG = true;
  222. }
  223. var lightIndex = 0;
  224. if (scene.lightsEnabled && !this.disableLighting) {
  225. for (var index = 0; index < scene.lights.length; index++) {
  226. var light = scene.lights[index];
  227. if (!light.isEnabled()) {
  228. continue;
  229. }
  230. // Excluded check
  231. if (light._excludedMeshesIds.length > 0) {
  232. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  233. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  234. if (excludedMesh) {
  235. light.excludedMeshes.push(excludedMesh);
  236. }
  237. }
  238. light._excludedMeshesIds = [];
  239. }
  240. // Included check
  241. if (light._includedOnlyMeshesIds.length > 0) {
  242. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  243. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  244. if (includedOnlyMesh) {
  245. light.includedOnlyMeshes.push(includedOnlyMesh);
  246. }
  247. }
  248. light._includedOnlyMeshesIds = [];
  249. }
  250. if (!light.canAffectMesh(mesh)) {
  251. continue;
  252. }
  253. needNormals = true;
  254. this._defines["LIGHT" + lightIndex] = true;
  255. var type;
  256. if (light instanceof BABYLON.SpotLight) {
  257. type = "SPOTLIGHT" + lightIndex;
  258. }
  259. else if (light instanceof BABYLON.HemisphericLight) {
  260. type = "HEMILIGHT" + lightIndex;
  261. }
  262. else if (light instanceof BABYLON.PointLight) {
  263. type = "POINTLIGHT" + lightIndex;
  264. }
  265. else {
  266. type = "DIRLIGHT" + lightIndex;
  267. }
  268. this._defines[type] = true;
  269. // Specular
  270. if (!light.specular.equalsFloats(0, 0, 0)) {
  271. this._defines.SPECULARTERM = true;
  272. }
  273. // Shadows
  274. if (scene.shadowsEnabled) {
  275. var shadowGenerator = light.getShadowGenerator();
  276. if (mesh && mesh.receiveShadows && shadowGenerator) {
  277. this._defines["SHADOW" + lightIndex] = true;
  278. this._defines.SHADOWS = true;
  279. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  280. this._defines["SHADOWVSM" + lightIndex] = true;
  281. }
  282. if (shadowGenerator.usePoissonSampling) {
  283. this._defines["SHADOWPCF" + lightIndex] = true;
  284. }
  285. }
  286. }
  287. lightIndex++;
  288. if (lightIndex === maxSimultaneousLights)
  289. break;
  290. }
  291. }
  292. // Attribs
  293. if (mesh) {
  294. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  295. this._defines.NORMAL = true;
  296. }
  297. if (needUVs) {
  298. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  299. this._defines.UV1 = true;
  300. }
  301. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  302. this._defines.UV2 = true;
  303. }
  304. }
  305. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  306. this._defines.VERTEXCOLOR = true;
  307. if (mesh.hasVertexAlpha) {
  308. this._defines.VERTEXALPHA = true;
  309. }
  310. }
  311. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  312. this._defines.BONES = true;
  313. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  314. this._defines.BONES4 = true;
  315. }
  316. // Instances
  317. if (useInstances) {
  318. this._defines.INSTANCES = true;
  319. }
  320. }
  321. this._mesh = mesh;
  322. // Get correct effect
  323. if (!this._defines.isEqual(this._cachedDefines)) {
  324. this._defines.cloneTo(this._cachedDefines);
  325. scene.resetCachedMaterial();
  326. // Fallbacks
  327. var fallbacks = new BABYLON.EffectFallbacks();
  328. if (this._defines.FOG) {
  329. fallbacks.addFallback(1, "FOG");
  330. }
  331. for (lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  332. if (!this._defines["LIGHT" + lightIndex]) {
  333. continue;
  334. }
  335. if (lightIndex > 0) {
  336. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  337. }
  338. if (this._defines["SHADOW" + lightIndex]) {
  339. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  340. }
  341. if (this._defines["SHADOWPCF" + lightIndex]) {
  342. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  343. }
  344. if (this._defines["SHADOWVSM" + lightIndex]) {
  345. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  346. }
  347. }
  348. if (this._defines.BONES4) {
  349. fallbacks.addFallback(0, "BONES4");
  350. }
  351. //Attributes
  352. var attribs = [BABYLON.VertexBuffer.PositionKind];
  353. if (this._defines.NORMAL) {
  354. attribs.push(BABYLON.VertexBuffer.NormalKind);
  355. }
  356. if (this._defines.UV1) {
  357. attribs.push(BABYLON.VertexBuffer.UVKind);
  358. }
  359. if (this._defines.UV2) {
  360. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  361. }
  362. if (this._defines.VERTEXCOLOR) {
  363. attribs.push(BABYLON.VertexBuffer.ColorKind);
  364. }
  365. if (this._defines.BONES) {
  366. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  367. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  368. }
  369. if (this._defines.INSTANCES) {
  370. attribs.push("world0");
  371. attribs.push("world1");
  372. attribs.push("world2");
  373. attribs.push("world3");
  374. }
  375. // Legacy browser patch
  376. var shaderName = "water";
  377. var join = this._defines.toString();
  378. this._effect = scene.getEngine().createEffect(shaderName, attribs, ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  379. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  380. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  381. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  382. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  383. "vFogInfos", "vFogColor", "pointSize",
  384. "vNormalInfos",
  385. "mBones",
  386. "vClipPlane", "normalMatrix",
  387. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3",
  388. // Water
  389. "worldReflectionViewProjection", "windDirection", "waveLength", "time", "windForce",
  390. "cameraPosition", "bumpHeight", "waveHeight", "waterColor", "colorBlendFactor", "waveSpeed"
  391. ], ["normalSampler",
  392. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3",
  393. // Water
  394. "refractionSampler", "reflectionSampler"
  395. ], join, fallbacks, this.onCompiled, this.onError);
  396. }
  397. if (!this._effect.isReady()) {
  398. return false;
  399. }
  400. this._renderId = scene.getRenderId();
  401. this._wasPreviouslyReady = true;
  402. if (mesh) {
  403. if (!mesh._materialDefines) {
  404. mesh._materialDefines = new WaterMaterialDefines();
  405. }
  406. this._defines.cloneTo(mesh._materialDefines);
  407. }
  408. return true;
  409. };
  410. WaterMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  411. this._effect.setMatrix("world", world);
  412. };
  413. WaterMaterial.prototype.bind = function (world, mesh) {
  414. var scene = this.getScene();
  415. // Matrices
  416. this.bindOnlyWorldMatrix(world);
  417. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  418. // Bones
  419. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  420. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  421. }
  422. if (scene.getCachedMaterial() !== this) {
  423. // Textures
  424. if (this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled) {
  425. this._effect.setTexture("normalSampler", this.bumpTexture);
  426. this._effect.setFloat2("vNormalInfos", this.bumpTexture.coordinatesIndex, this.bumpTexture.level);
  427. this._effect.setMatrix("normalMatrix", this.bumpTexture.getTextureMatrix());
  428. }
  429. // Clip plane
  430. if (scene.clipPlane) {
  431. var clipPlane = scene.clipPlane;
  432. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  433. }
  434. // Point size
  435. if (this.pointsCloud) {
  436. this._effect.setFloat("pointSize", this.pointSize);
  437. }
  438. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  439. }
  440. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  441. if (this._defines.SPECULARTERM) {
  442. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  443. }
  444. if (scene.lightsEnabled && !this.disableLighting) {
  445. var lightIndex = 0;
  446. for (var index = 0; index < scene.lights.length; index++) {
  447. var light = scene.lights[index];
  448. if (!light.isEnabled()) {
  449. continue;
  450. }
  451. if (!light.canAffectMesh(mesh)) {
  452. continue;
  453. }
  454. if (light instanceof BABYLON.PointLight) {
  455. // Point Light
  456. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  457. }
  458. else if (light instanceof BABYLON.DirectionalLight) {
  459. // Directional Light
  460. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  461. }
  462. else if (light instanceof BABYLON.SpotLight) {
  463. // Spot Light
  464. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  465. }
  466. else if (light instanceof BABYLON.HemisphericLight) {
  467. // Hemispheric Light
  468. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  469. }
  470. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  471. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  472. if (this._defines.SPECULARTERM) {
  473. light.specular.scaleToRef(light.intensity, this._scaledSpecular);
  474. this._effect.setColor3("vLightSpecular" + lightIndex, this._scaledSpecular);
  475. }
  476. // Shadows
  477. if (scene.shadowsEnabled) {
  478. var shadowGenerator = light.getShadowGenerator();
  479. if (mesh.receiveShadows && shadowGenerator) {
  480. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  481. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  482. this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  483. }
  484. }
  485. lightIndex++;
  486. if (lightIndex === maxSimultaneousLights)
  487. break;
  488. }
  489. }
  490. // View
  491. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  492. this._effect.setMatrix("view", scene.getViewMatrix());
  493. }
  494. // Fog
  495. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  496. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  497. this._effect.setColor3("vFogColor", scene.fogColor);
  498. }
  499. // Water
  500. if (BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  501. this._effect.setTexture("refractionSampler", this._refractionRTT);
  502. this._effect.setTexture("reflectionSampler", this._reflectionRTT);
  503. }
  504. var wrvp = this._mesh.getWorldMatrix().multiply(this._reflectionTransform).multiply(scene.getProjectionMatrix());
  505. this._lastTime += scene.getEngine().getDeltaTime();
  506. this._effect.setMatrix("worldReflectionViewProjection", wrvp);
  507. this._effect.setVector2("windDirection", this.windDirection);
  508. this._effect.setFloat("waveLength", this.waveLength);
  509. this._effect.setFloat("time", this._lastTime / 100000);
  510. this._effect.setFloat("windForce", this.windForce);
  511. this._effect.setFloat("waveHeight", this.waveHeight);
  512. this._effect.setFloat("bumpHeight", this.bumpHeight);
  513. this._effect.setColor4("waterColor", this.waterColor, 1.0);
  514. this._effect.setFloat("colorBlendFactor", this.colorBlendFactor);
  515. this._effect.setFloat("waveSpeed", this.waveSpeed);
  516. _super.prototype.bind.call(this, world, mesh);
  517. };
  518. WaterMaterial.prototype._createRenderTargets = function (scene, renderTargetSize) {
  519. var _this = this;
  520. // Render targets
  521. this._refractionRTT = new BABYLON.RenderTargetTexture(name + "_refraction", { width: renderTargetSize.x, height: renderTargetSize.y }, scene, false, true);
  522. this._reflectionRTT = new BABYLON.RenderTargetTexture(name + "_reflection", { width: renderTargetSize.x, height: renderTargetSize.y }, scene, false, true);
  523. scene.customRenderTargets.push(this._refractionRTT);
  524. scene.customRenderTargets.push(this._reflectionRTT);
  525. var isVisible;
  526. var clipPlane = null;
  527. var savedViewMatrix;
  528. var mirrorMatrix = BABYLON.Matrix.Zero();
  529. this._refractionRTT.onBeforeRender = function () {
  530. if (_this._mesh) {
  531. isVisible = _this._mesh.isVisible;
  532. _this._mesh.isVisible = false;
  533. }
  534. // Clip plane
  535. clipPlane = scene.clipPlane;
  536. var positiony = _this._mesh ? _this._mesh.position.y : 0.0;
  537. scene.clipPlane = BABYLON.Plane.FromPositionAndNormal(new BABYLON.Vector3(0, positiony + 0.05, 0), new BABYLON.Vector3(0, 1, 0));
  538. };
  539. this._refractionRTT.onAfterRender = function () {
  540. if (_this._mesh) {
  541. _this._mesh.isVisible = isVisible;
  542. }
  543. // Clip plane
  544. scene.clipPlane = clipPlane;
  545. };
  546. this._reflectionRTT.onBeforeRender = function () {
  547. if (_this._mesh) {
  548. isVisible = _this._mesh.isVisible;
  549. _this._mesh.isVisible = false;
  550. }
  551. // Clip plane
  552. clipPlane = scene.clipPlane;
  553. var positiony = _this._mesh ? _this._mesh.position.y : 0.0;
  554. scene.clipPlane = BABYLON.Plane.FromPositionAndNormal(new BABYLON.Vector3(0, positiony - 0.05, 0), new BABYLON.Vector3(0, -1, 0));
  555. // Transform
  556. BABYLON.Matrix.ReflectionToRef(scene.clipPlane, mirrorMatrix);
  557. savedViewMatrix = scene.getViewMatrix();
  558. mirrorMatrix.multiplyToRef(savedViewMatrix, _this._reflectionTransform);
  559. scene.setTransformMatrix(_this._reflectionTransform, scene.getProjectionMatrix());
  560. scene.getEngine().cullBackFaces = false;
  561. scene._mirroredCameraPosition = BABYLON.Vector3.TransformCoordinates(scene.activeCamera.position, mirrorMatrix);
  562. };
  563. this._reflectionRTT.onAfterRender = function () {
  564. if (_this._mesh) {
  565. _this._mesh.isVisible = isVisible;
  566. }
  567. // Clip plane
  568. scene.clipPlane = clipPlane;
  569. // Transform
  570. scene.setTransformMatrix(savedViewMatrix, scene.getProjectionMatrix());
  571. scene.getEngine().cullBackFaces = true;
  572. scene._mirroredCameraPosition = null;
  573. };
  574. };
  575. WaterMaterial.prototype.getAnimatables = function () {
  576. var results = [];
  577. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  578. results.push(this.bumpTexture);
  579. }
  580. if (this._reflectionRTT && this._reflectionRTT.animations && this._reflectionRTT.animations.length > 0) {
  581. results.push(this._reflectionRTT);
  582. }
  583. if (this._refractionRTT && this._refractionRTT.animations && this._refractionRTT.animations.length > 0) {
  584. results.push(this._refractionRTT);
  585. }
  586. return results;
  587. };
  588. WaterMaterial.prototype.dispose = function (forceDisposeEffect) {
  589. if (this.bumpTexture) {
  590. this.bumpTexture.dispose();
  591. }
  592. var index = this.getScene().customRenderTargets.indexOf(this._refractionRTT);
  593. if (index != -1) {
  594. this.getScene().customRenderTargets.splice(index, 1);
  595. }
  596. index = -1;
  597. index = this.getScene().customRenderTargets.indexOf(this._reflectionRTT);
  598. if (index != -1) {
  599. this.getScene().customRenderTargets.splice(index, 1);
  600. }
  601. if (this._reflectionRTT) {
  602. this._reflectionRTT.dispose();
  603. }
  604. if (this._refractionRTT) {
  605. this._refractionRTT.dispose();
  606. }
  607. _super.prototype.dispose.call(this, forceDisposeEffect);
  608. };
  609. WaterMaterial.prototype.clone = function (name) {
  610. var _this = this;
  611. return BABYLON.SerializationHelper.Clone(function () { return new WaterMaterial(name, _this.getScene()); }, this);
  612. };
  613. WaterMaterial.prototype.serialize = function () {
  614. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  615. serializationObject.customType = "BABYLON.WaterMaterial";
  616. return serializationObject;
  617. };
  618. // Statics
  619. WaterMaterial.Parse = function (source, scene, rootUrl) {
  620. return BABYLON.SerializationHelper.Parse(function () { return new WaterMaterial(source.name, scene); }, source, scene, rootUrl);
  621. };
  622. WaterMaterial.CreateDefaultMesh = function (name, scene) {
  623. var mesh = BABYLON.Mesh.CreateGround(name, 512, 512, 32, scene, false);
  624. return mesh;
  625. };
  626. __decorate([
  627. BABYLON.serializeAsTexture()
  628. ], WaterMaterial.prototype, "bumpTexture");
  629. __decorate([
  630. BABYLON.serializeAsColor3()
  631. ], WaterMaterial.prototype, "diffuseColor");
  632. __decorate([
  633. BABYLON.serializeAsColor3()
  634. ], WaterMaterial.prototype, "specularColor");
  635. __decorate([
  636. BABYLON.serialize()
  637. ], WaterMaterial.prototype, "specularPower");
  638. __decorate([
  639. BABYLON.serialize()
  640. ], WaterMaterial.prototype, "disableLighting");
  641. __decorate([
  642. BABYLON.serialize()
  643. ], WaterMaterial.prototype, "windForce");
  644. __decorate([
  645. BABYLON.serializeAsVector2()
  646. ], WaterMaterial.prototype, "windDirection");
  647. __decorate([
  648. BABYLON.serialize()
  649. ], WaterMaterial.prototype, "waveHeight");
  650. __decorate([
  651. BABYLON.serialize()
  652. ], WaterMaterial.prototype, "bumpHeight");
  653. __decorate([
  654. BABYLON.serializeAsColor3()
  655. ], WaterMaterial.prototype, "waterColor");
  656. __decorate([
  657. BABYLON.serialize()
  658. ], WaterMaterial.prototype, "colorBlendFactor");
  659. __decorate([
  660. BABYLON.serialize()
  661. ], WaterMaterial.prototype, "waveLength");
  662. __decorate([
  663. BABYLON.serialize()
  664. ], WaterMaterial.prototype, "waveSpeed");
  665. return WaterMaterial;
  666. })(BABYLON.Material);
  667. BABYLON.WaterMaterial = WaterMaterial;
  668. })(BABYLON || (BABYLON = {}));
  669. BABYLON.Effect.ShadersStore['waterVertexShader'] = "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#ifdef BONES\nattribute vec4 matricesIndices;\nattribute vec4 matricesWeights;\n#endif\n\n#ifdef INSTANCES\nattribute vec4 world0;\nattribute vec4 world1;\nattribute vec4 world2;\nattribute vec4 world3;\n#else\nuniform mat4 world;\n#endif\nuniform mat4 view;\nuniform mat4 viewProjection;\n#ifdef BUMP\nvarying vec2 vNormalUV;\nuniform mat4 normalMatrix;\nuniform vec2 vNormalInfos;\n#endif\n#ifdef BONES\nuniform mat4 mBones[BonesPerMesh];\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#ifdef CLIPPLANE\nuniform vec4 vClipPlane;\nvarying float fClipDistance;\n#endif\n#ifdef FOG\nvarying float fFogDistance;\n#endif\n#ifdef SHADOWS\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\nuniform mat4 lightMatrix0;\nvarying vec4 vPositionFromLight0;\n#endif\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\nuniform mat4 lightMatrix1;\nvarying vec4 vPositionFromLight1;\n#endif\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\nuniform mat4 lightMatrix2;\nvarying vec4 vPositionFromLight2;\n#endif\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\nuniform mat4 lightMatrix3;\nvarying vec4 vPositionFromLight3;\n#endif\n#endif\n\nuniform mat4 worldReflectionViewProjection;\nuniform vec2 windDirection;\nuniform float waveLength;\nuniform float time;\nuniform float windForce;\nuniform float waveHeight;\nuniform float waveSpeed;\n\nvarying vec3 vPosition;\nvarying vec3 vRefractionMapTexCoord;\nvarying vec3 vReflectionMapTexCoord;\nvoid main(void) {\nmat4 finalWorld;\n#ifdef INSTANCES\nfinalWorld=mat4(world0,world1,world2,world3);\n#else\nfinalWorld=world;\n#endif\n#ifdef BONES\nmat4 m0=mBones[int(matricesIndices.x)]*matricesWeights.x;\nmat4 m1=mBones[int(matricesIndices.y)]*matricesWeights.y;\nmat4 m2=mBones[int(matricesIndices.z)]*matricesWeights.z;\n#ifdef BONES4\nmat4 m3=mBones[int(matricesIndices.w)]*matricesWeights.w;\nfinalWorld=finalWorld*(m0+m1+m2+m3);\n#else\nfinalWorld=finalWorld*(m0+m1+m2);\n#endif \n#endif\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 BUMP\nif (vNormalInfos.x == 0.)\n{\nvNormalUV=vec2(normalMatrix*vec4((uv*1.0)/waveLength+time*windForce*windDirection,1.0,0.0));\n}\nelse\n{\nvNormalUV=vec2(normalMatrix*vec4((uv2*1.0)/waveLength+time*windForce*windDirection,1.0,0.0));\n}\n#endif\n\n#ifdef CLIPPLANE\nfClipDistance=dot(worldPos,vClipPlane);\n#endif\n\n#ifdef FOG\nfFogDistance=(view*worldPos).z;\n#endif\n\n#ifdef SHADOWS\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\nvPositionFromLight0=lightMatrix0*worldPos;\n#endif\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\nvPositionFromLight1=lightMatrix1*worldPos;\n#endif\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\nvPositionFromLight2=lightMatrix2*worldPos;\n#endif\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\nvPositionFromLight3=lightMatrix3*worldPos;\n#endif\n#endif\n\n#ifdef VERTEXCOLOR\nvColor=color;\n#endif\n\n#ifdef POINTSIZE\ngl_PointSize=pointSize;\n#endif\nvec3 p=position;\nfloat newY=(sin(((p.x/0.05)+time*waveSpeed))*waveHeight*windDirection.x*5.0)\n+(cos(((p.z/0.05)+time*waveSpeed))*waveHeight*windDirection.y*5.0);\np.y+=abs(newY);\ngl_Position=viewProjection*finalWorld*vec4(p,1.0);\n#ifdef REFLECTION\nworldPos=viewProjection*finalWorld*vec4(p,1.0);\n\nvPosition=position;\nvRefractionMapTexCoord.x=0.5*(worldPos.w+worldPos.x);\nvRefractionMapTexCoord.y=0.5*(worldPos.w+worldPos.y);\nvRefractionMapTexCoord.z=worldPos.w;\nworldPos=worldReflectionViewProjection*vec4(position,1.0);\nvReflectionMapTexCoord.x=0.5*(worldPos.w+worldPos.x);\nvReflectionMapTexCoord.y=0.5*(worldPos.w+worldPos.y);\nvReflectionMapTexCoord.z=worldPos.w;\n#endif\n}\n";
  670. BABYLON.Effect.ShadersStore['waterPixelShader'] = "precision highp float;\n\nuniform vec3 vEyePosition;\nuniform vec4 vDiffuseColor;\n#ifdef SPECULARTERM\nuniform vec4 vSpecularColor;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n\n#ifdef LIGHT0\nuniform vec4 vLightData0;\nuniform vec4 vLightDiffuse0;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular0;\n#endif\n#ifdef SHADOW0\n#if defined(SPOTLIGHT0) || defined(DIRLIGHT0)\nvarying vec4 vPositionFromLight0;\nuniform sampler2D shadowSampler0;\n#else\nuniform samplerCube shadowSampler0;\n#endif\nuniform vec3 shadowsInfo0;\n#endif\n#ifdef SPOTLIGHT0\nuniform vec4 vLightDirection0;\n#endif\n#ifdef HEMILIGHT0\nuniform vec3 vLightGround0;\n#endif\n#endif\n#ifdef LIGHT1\nuniform vec4 vLightData1;\nuniform vec4 vLightDiffuse1;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular1;\n#endif\n#ifdef SHADOW1\n#if defined(SPOTLIGHT1) || defined(DIRLIGHT1)\nvarying vec4 vPositionFromLight1;\nuniform sampler2D shadowSampler1;\n#else\nuniform samplerCube shadowSampler1;\n#endif\nuniform vec3 shadowsInfo1;\n#endif\n#ifdef SPOTLIGHT1\nuniform vec4 vLightDirection1;\n#endif\n#ifdef HEMILIGHT1\nuniform vec3 vLightGround1;\n#endif\n#endif\n#ifdef LIGHT2\nuniform vec4 vLightData2;\nuniform vec4 vLightDiffuse2;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular2;\n#endif\n#ifdef SHADOW2\n#if defined(SPOTLIGHT2) || defined(DIRLIGHT2)\nvarying vec4 vPositionFromLight2;\nuniform sampler2D shadowSampler2;\n#else\nuniform samplerCube shadowSampler2;\n#endif\nuniform vec3 shadowsInfo2;\n#endif\n#ifdef SPOTLIGHT2\nuniform vec4 vLightDirection2;\n#endif\n#ifdef HEMILIGHT2\nuniform vec3 vLightGround2;\n#endif\n#endif\n#ifdef LIGHT3\nuniform vec4 vLightData3;\nuniform vec4 vLightDiffuse3;\n#ifdef SPECULARTERM\nuniform vec3 vLightSpecular3;\n#endif\n#ifdef SHADOW3\n#if defined(SPOTLIGHT3) || defined(DIRLIGHT3)\nvarying vec4 vPositionFromLight3;\nuniform sampler2D shadowSampler3;\n#else\nuniform samplerCube shadowSampler3;\n#endif\nuniform vec3 shadowsInfo3;\n#endif\n#ifdef SPOTLIGHT3\nuniform vec4 vLightDirection3;\n#endif\n#ifdef HEMILIGHT3\nuniform vec3 vLightGround3;\n#endif\n#endif\n\n#ifdef BUMP\nvarying vec2 vNormalUV;\nuniform sampler2D normalSampler;\nuniform vec2 vNormalInfos;\n#endif\nuniform sampler2D refractionSampler;\nuniform sampler2D reflectionSampler;\n\nconst float LOG2=1.442695;\nuniform vec3 cameraPosition;\nuniform vec4 waterColor;\nuniform float colorBlendFactor;\nuniform float bumpHeight;\n\nvarying vec3 vRefractionMapTexCoord;\nvarying vec3 vReflectionMapTexCoord;\nvarying vec3 vPosition;\n\n#ifdef SHADOWS\nfloat unpack(vec4 color)\n{\nconst vec4 bit_shift=vec4(1.0/(255.0*255.0*255.0),1.0/(255.0*255.0),1.0/255.0,1.0);\nreturn dot(color,bit_shift);\n}\n#if defined(POINTLIGHT0) || defined(POINTLIGHT1) || defined(POINTLIGHT2) || defined(POINTLIGHT3)\nfloat computeShadowCube(vec3 lightPosition,samplerCube shadowSampler,float darkness,float bias)\n{\nvec3 directionToLight=vPositionW-lightPosition;\nfloat depth=length(directionToLight);\ndepth=clamp(depth,0.,1.0);\ndirectionToLight=normalize(directionToLight);\ndirectionToLight.y =-directionToLight.y;\nfloat shadow=unpack(textureCube(shadowSampler,directionToLight))+bias;\nif (depth>shadow)\n{\nreturn darkness;\n}\nreturn 1.0;\n}\nfloat computeShadowWithPCFCube(vec3 lightPosition,samplerCube shadowSampler,float bias,float darkness,float mapSize)\n{\nvec3 directionToLight=vPositionW-lightPosition;\nfloat depth=length(directionToLight);\ndepth=clamp(depth,0.,1.0);\nfloat diskScale=2.0/mapSize;\ndirectionToLight=normalize(directionToLight);\ndirectionToLight.y=-directionToLight.y;\nfloat visibility=1.;\nvec3 poissonDisk[4];\npoissonDisk[0]=vec3(-0.094201624,0.04,-0.039906216);\npoissonDisk[1]=vec3(0.094558609,-0.04,-0.076890725);\npoissonDisk[2]=vec3(-0.094184101,0.01,-0.092938870);\npoissonDisk[3]=vec3(0.034495938,-0.01,0.029387760);\n\nfloat biasedDepth=depth-bias;\nif (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[0]))<biasedDepth) visibility-=0.25;\nif (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[1]))<biasedDepth) visibility-=0.25;\nif (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[2]))<biasedDepth) visibility-=0.25;\nif (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[3]))<biasedDepth) visibility-=0.25;\nreturn min(1.0,visibility+darkness);\n}\n#endif\n#if defined(SPOTLIGHT0) || defined(SPOTLIGHT1) || defined(SPOTLIGHT2) || defined(SPOTLIGHT3) || defined(DIRLIGHT0) || defined(DIRLIGHT1) || defined(DIRLIGHT2) || defined(DIRLIGHT3)\nfloat computeShadow(vec4 vPositionFromLight,sampler2D shadowSampler,float darkness,float bias)\n{\nvec3 depth=vPositionFromLight.xyz/vPositionFromLight.w;\ndepth=0.5*depth+vec3(0.5);\nvec2 uv=depth.xy;\nif (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)\n{\nreturn 1.0;\n}\nfloat shadow=unpack(texture2D(shadowSampler,uv))+bias;\nif (depth.z>shadow)\n{\nreturn darkness;\n}\nreturn 1.;\n}\nfloat computeShadowWithPCF(vec4 vPositionFromLight,sampler2D shadowSampler,float mapSize,float bias,float darkness)\n{\nvec3 depth=vPositionFromLight.xyz/vPositionFromLight.w;\ndepth=0.5*depth+vec3(0.5);\nvec2 uv=depth.xy;\nif (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)\n{\nreturn 1.0;\n}\nfloat visibility=1.;\nvec2 poissonDisk[4];\npoissonDisk[0]=vec2(-0.94201624,-0.39906216);\npoissonDisk[1]=vec2(0.94558609,-0.76890725);\npoissonDisk[2]=vec2(-0.094184101,-0.92938870);\npoissonDisk[3]=vec2(0.34495938,0.29387760);\n\nfloat biasedDepth=depth.z-bias;\nif (unpack(texture2D(shadowSampler,uv+poissonDisk[0]/mapSize))<biasedDepth) visibility-=0.25;\nif (unpack(texture2D(shadowSampler,uv+poissonDisk[1]/mapSize))<biasedDepth) visibility-=0.25;\nif (unpack(texture2D(shadowSampler,uv+poissonDisk[2]/mapSize))<biasedDepth) visibility-=0.25;\nif (unpack(texture2D(shadowSampler,uv+poissonDisk[3]/mapSize))<biasedDepth) visibility-=0.25;\nreturn min(1.0,visibility+darkness);\n}\n\nfloat unpackHalf(vec2 color)\n{\nreturn color.x+(color.y/255.0);\n}\nfloat linstep(float low,float high,float v) {\nreturn clamp((v-low)/(high-low),0.0,1.0);\n}\nfloat ChebychevInequality(vec2 moments,float compare,float bias)\n{\nfloat p=smoothstep(compare-bias,compare,moments.x);\nfloat variance=max(moments.y-moments.x*moments.x,0.02);\nfloat d=compare-moments.x;\nfloat p_max=linstep(0.2,1.0,variance/(variance+d*d));\nreturn clamp(max(p,p_max),0.0,1.0);\n}\nfloat computeShadowWithVSM(vec4 vPositionFromLight,sampler2D shadowSampler,float bias,float darkness)\n{\nvec3 depth=vPositionFromLight.xyz/vPositionFromLight.w;\ndepth=0.5*depth+vec3(0.5);\nvec2 uv=depth.xy;\nif (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0 || depth.z>=1.0)\n{\nreturn 1.0;\n}\nvec4 texel=texture2D(shadowSampler,uv);\nvec2 moments=vec2(unpackHalf(texel.xy),unpackHalf(texel.zw));\nreturn min(1.0,1.0-ChebychevInequality(moments,depth.z,bias)+darkness);\n}\n#endif\n#endif\n#ifdef CLIPPLANE\nvarying float fClipDistance;\n#endif\n\n#ifdef FOG\n#define FOGMODE_NONE 0.\n#define FOGMODE_EXP 1.\n#define FOGMODE_EXP2 2.\n#define FOGMODE_LINEAR 3.\n#define E 2.71828\nuniform vec4 vFogInfos;\nuniform vec3 vFogColor;\nvarying float fFogDistance;\nfloat CalcFogFactor()\n{\nfloat fogCoeff=1.0;\nfloat fogStart=vFogInfos.y;\nfloat fogEnd=vFogInfos.z;\nfloat fogDensity=vFogInfos.w;\nif (FOGMODE_LINEAR == vFogInfos.x)\n{\nfogCoeff=(fogEnd-fFogDistance)/(fogEnd-fogStart);\n}\nelse if (FOGMODE_EXP == vFogInfos.x)\n{\nfogCoeff=1.0/pow(E,fFogDistance*fogDensity);\n}\nelse if (FOGMODE_EXP2 == vFogInfos.x)\n{\nfogCoeff=1.0/pow(E,fFogDistance*fFogDistance*fogDensity*fogDensity);\n}\nreturn clamp(fogCoeff,0.0,1.0);\n}\n#endif\n\nstruct lightingInfo\n{\nvec3 diffuse;\n#ifdef SPECULARTERM\nvec3 specular;\n#endif\n};\nlightingInfo computeLighting(vec3 viewDirectionW,vec3 vNormal,vec4 lightData,vec3 diffuseColor,vec3 specularColor,float range,float glossiness,vec3 bumpColor) {\nlightingInfo result;\nvec3 lightVectorW;\nfloat attenuation=1.0;\nif (lightData.w == 0.)\n{\nvec3 direction=lightData.xyz-vPositionW;\nattenuation=max(0.,1.0-length(direction)/range);\nlightVectorW=normalize(direction);\n}\nelse\n{\nlightVectorW=normalize(-lightData.xyz);\n}\n\nfloat ndl=max(0.,dot(vNormal,lightVectorW));\nresult.diffuse=ndl*diffuseColor*attenuation;\n\n#ifdef SPECULARTERM\nvec3 angleW=normalize(viewDirectionW+lightVectorW);\nvec3 perturbation=bumpHeight*(bumpColor.rgb-0.5);\nvec3 halfvec=normalize(angleW+lightVectorW+vec3(perturbation.x,perturbation.y,perturbation.z));\nfloat temp=max(0.,dot(vNormal,halfvec));\ntemp=pow(temp,max(1.,glossiness));\nresult.specular=temp*specularColor*attenuation;\n#endif\nreturn result;\n}\nlightingInfo computeSpotLighting(vec3 viewDirectionW,vec3 vNormal,vec4 lightData,vec4 lightDirection,vec3 specularColor,vec3 diffuseColor,float range,float glossiness,vec3 bumpColor) {\nlightingInfo result;\nvec3 direction=lightData.xyz-vPositionW;\nvec3 lightVectorW=normalize(direction);\nfloat attenuation=max(0.,1.0-length(direction)/range);\n\nfloat cosAngle=max(0.,dot(-lightDirection.xyz,lightVectorW));\nfloat spotAtten=0.0;\nif (cosAngle>=lightDirection.w)\n{\ncosAngle=max(0.,pow(cosAngle,lightData.w));\nspotAtten=clamp((cosAngle-lightDirection.w)/(1.-cosAngle),0.0,1.0);\n\nfloat ndl=max(0.,dot(vNormal,-lightDirection.xyz));\nresult.diffuse=ndl*spotAtten*diffuseColor*attenuation;\n\n#ifdef SPECULARTERM \nvec3 angleW=normalize(viewDirectionW-lightDirection.xyz);\nvec3 perturbation=bumpHeight*(bumpColor.rgb-0.5);\nvec3 halfvec=normalize(angleW+vec3(perturbation.x,perturbation.y,perturbation.z));\nfloat temp=max(0.,dot(vNormal,halfvec));\ntemp=pow(temp,max(1.,glossiness));\nresult.specular=specularColor*temp*spotAtten*attenuation;\n#endif\nreturn result;\n}\nresult.diffuse=vec3(0.);\n#ifdef SPECULARTERM\nresult.specular=vec3(0.);\n#endif\nreturn result;\n}\nlightingInfo computeHemisphericLighting(vec3 viewDirectionW,vec3 vNormal,vec4 lightData,vec3 diffuseColor,vec3 specularColor,vec3 groundColor,float glossiness,vec3 bumpColor) {\nlightingInfo result;\n\nfloat ndl=dot(vNormal,lightData.xyz)*0.5+0.5;\nresult.diffuse=mix(groundColor,diffuseColor,ndl);\n\n#ifdef SPECULARTERM\nvec3 angleW=normalize(viewDirectionW+lightData.xyz);\nvec3 perturbation=bumpHeight*(bumpColor.rgb-0.5);\nvec3 halfvec=normalize(angleW+vec3(perturbation.x,perturbation.y,perturbation.z));\nfloat temp=max(0.0,dot(vNormal,halfvec));\ntemp=pow(temp,max(1.0,glossiness));\nresult.specular=temp*specularColor;\n#endif\nreturn result;\n}\nvoid main(void) {\n\n#ifdef CLIPPLANE\nif (fClipDistance>0.0)\ndiscard;\n#endif\nvec3 viewDirectionW=normalize(vEyePosition-vPositionW);\n\nvec4 baseColor=vec4(1.,1.,1.,1.);\nvec3 diffuseColor=vDiffuseColor.rgb;\n#ifdef SPECULARTERM\nfloat glossiness=vSpecularColor.a;\nvec3 specularColor=vSpecularColor.rgb;\n#else\nfloat glossiness=0.;\n#endif\n\nfloat alpha=vDiffuseColor.a;\n#ifdef BUMP\nbaseColor=texture2D(normalSampler,vNormalUV);\nvec3 bumpColor=baseColor.rgb;\n#ifdef ALPHATEST\nif (baseColor.a<0.4)\ndiscard;\n#endif\nbaseColor.rgb*=vNormalInfos.y;\n#else\nvec3 bumpColor=vec3(1.0);\n#endif\n#ifdef VERTEXCOLOR\nbaseColor.rgb*=vColor.rgb;\n#endif\n\n#ifdef NORMAL\nvec3 normalW=normalize(vNormalW);\nvec2 perturbation=bumpHeight*(baseColor.rg-0.5);\n#else\nvec3 normalW=vec3(1.0,1.0,1.0);\nvec2 perturbation=bumpHeight*(vec2(1.0,1.0)-0.5);\n#endif\n#ifdef REFLECTION\n\nvec3 eyeVector=normalize(vEyePosition-vPosition);\nvec2 projectedRefractionTexCoords=clamp(vRefractionMapTexCoord.xy/vRefractionMapTexCoord.z+perturbation,0.0,1.0);\nvec4 refractiveColor=texture2D(refractionSampler,projectedRefractionTexCoords);\nvec2 projectedReflectionTexCoords=clamp(vReflectionMapTexCoord.xy/vReflectionMapTexCoord.z+perturbation,0.0,1.0);\nvec4 reflectiveColor=texture2D(reflectionSampler,projectedReflectionTexCoords);\nvec3 upVector=vec3(0.0,1.0,0.0);\nfloat fresnelTerm=max(dot(eyeVector,upVector),0.0);\nvec4 combinedColor=refractiveColor*fresnelTerm+reflectiveColor*(1.0-fresnelTerm);\nbaseColor=colorBlendFactor*waterColor+(1.0-colorBlendFactor)*combinedColor;\n#endif\n\nvec3 diffuseBase=vec3(0.,0.,0.);\n#ifdef SPECULARTERM\nvec3 specularBase=vec3(0.,0.,0.);\n#endif\nfloat shadow=1.;\n#ifdef LIGHT0\n#ifndef SPECULARTERM\nvec3 vLightSpecular0=vec3(0.0);\n#endif\n#ifdef SPOTLIGHT0\nlightingInfo info=computeSpotLighting(viewDirectionW,normalW,vLightData0,vLightDirection0,vLightDiffuse0.rgb,vLightSpecular0,vLightDiffuse0.a,glossiness,bumpColor);\n#endif\n#ifdef HEMILIGHT0\nlightingInfo info=computeHemisphericLighting(viewDirectionW,normalW,vLightData0,vLightDiffuse0.rgb,vLightSpecular0,vLightGround0,glossiness,bumpColor);\n#endif\n#if defined(POINTLIGHT0) || defined(DIRLIGHT0)\nlightingInfo info=computeLighting(viewDirectionW,normalW,vLightData0,vLightDiffuse0.rgb,vLightSpecular0,vLightDiffuse0.a,glossiness,bumpColor);\n#endif\n#ifdef SHADOW0\n#ifdef SHADOWVSM0\nshadow=computeShadowWithVSM(vPositionFromLight0,shadowSampler0,shadowsInfo0.z,shadowsInfo0.x);\n#else\n#ifdef SHADOWPCF0\n#if defined(POINTLIGHT0)\nshadow=computeShadowWithPCFCube(vLightData0.xyz,shadowSampler0,shadowsInfo0.z,shadowsInfo0.x,shadowsInfo0.y);\n#else\nshadow=computeShadowWithPCF(vPositionFromLight0,shadowSampler0,shadowsInfo0.y,shadowsInfo0.z,shadowsInfo0.x);\n#endif\n#else\n#if defined(POINTLIGHT0)\nshadow=computeShadowCube(vLightData0.xyz,shadowSampler0,shadowsInfo0.x,shadowsInfo0.z);\n#else\nshadow=computeShadow(vPositionFromLight0,shadowSampler0,shadowsInfo0.x,shadowsInfo0.z);\n#endif\n#endif\n#endif\n#else\nshadow=1.;\n#endif\ndiffuseBase+=info.diffuse*shadow;\n#ifdef SPECULARTERM\nspecularBase+=info.specular*shadow;\n#endif\n#endif\n#ifdef LIGHT1\n#ifndef SPECULARTERM\nvec3 vLightSpecular1=vec3(0.0);\n#endif\n#ifdef SPOTLIGHT1\ninfo=computeSpotLighting(viewDirectionW,normalW,vLightData1,vLightDirection1,vLightDiffuse1.rgb,vLightSpecular1,vLightDiffuse1.a,glossiness,bumpColor);\n#endif\n#ifdef HEMILIGHT1\ninfo=computeHemisphericLighting(viewDirectionW,normalW,vLightData1,vLightDiffuse1.rgb,vLightSpecular1,vLightGround1.a,glossiness,bumpColor);\n#endif\n#if defined(POINTLIGHT1) || defined(DIRLIGHT1)\ninfo=computeLighting(viewDirectionW,normalW,vLightData1,vLightDiffuse1.rgb,vLightSpecular1,vLightDiffuse1.a,glossiness,bumpColor);\n#endif\n#ifdef SHADOW1\n#ifdef SHADOWVSM1\nshadow=computeShadowWithVSM(vPositionFromLight1,shadowSampler1,shadowsInfo1.z,shadowsInfo1.x);\n#else\n#ifdef SHADOWPCF1\n#if defined(POINTLIGHT1)\nshadow=computeShadowWithPCFCube(vLightData1.xyz,shadowSampler1,shadowsInfo1.z,shadowsInfo1.x,shadowsInfo1.y);\n#else\nshadow=computeShadowWithPCF(vPositionFromLight1,shadowSampler1,shadowsInfo1.y,shadowsInfo1.z,shadowsInfo1.x);\n#endif\n#else\n#if defined(POINTLIGHT1)\nshadow=computeShadowCube(vLightData1.xyz,shadowSampler1,shadowsInfo1.x,shadowsInfo1.z);\n#else\nshadow=computeShadow(vPositionFromLight1,shadowSampler1,shadowsInfo1.x,shadowsInfo1.z);\n#endif\n#endif\n#endif\n#else\nshadow=1.;\n#endif\ndiffuseBase+=info.diffuse*shadow;\n#ifdef SPECULARTERM\nspecularBase+=info.specular*shadow;\n#endif\n#endif\n#ifdef LIGHT2\n#ifndef SPECULARTERM\nvec3 vLightSpecular2=vec3(0.0);\n#endif\n#ifdef SPOTLIGHT2\ninfo=computeSpotLighting(viewDirectionW,normalW,vLightData2,vLightDirection2,vLightDiffuse2.rgb,vLightSpecular2,vLightDiffuse2.a,glossiness,bumpColor);\n#endif\n#ifdef HEMILIGHT2\ninfo=computeHemisphericLighting(viewDirectionW,normalW,vLightData2,vLightDiffuse2.rgb,vLightSpecular2,vLightGround2,glossiness,bumpColor);\n#endif\n#if defined(POINTLIGHT2) || defined(DIRLIGHT2)\ninfo=computeLighting(viewDirectionW,normalW,vLightData2,vLightDiffuse2.rgb,vLightSpecular2,vLightDiffuse2.a,glossiness,bumpColor);\n#endif\n#ifdef SHADOW2\n#ifdef SHADOWVSM2\nshadow=computeShadowWithVSM(vPositionFromLight2,shadowSampler2,shadowsInfo2.z,shadowsInfo2.x);\n#else\n#ifdef SHADOWPCF2\n#if defined(POINTLIGHT2)\nshadow=computeShadowWithPCFCube(vLightData2.xyz,shadowSampler2,shadowsInfo2.z,shadowsInfo2.x,shadowsInfo2.y);\n#else\nshadow=computeShadowWithPCF(vPositionFromLight2,shadowSampler2,shadowsInfo2.y,shadowsInfo2.z,shadowsInfo2.x);\n#endif\n#else\n#if defined(POINTLIGHT2)\nshadow=computeShadowCube(vLightData2.xyz,shadowSampler2,shadowsInfo2.x,shadowsInfo2.z);\n#else\nshadow=computeShadow(vPositionFromLight2,shadowSampler2,shadowsInfo2.x,shadowsInfo2.z);\n#endif\n#endif \n#endif \n#else\nshadow=1.;\n#endif\ndiffuseBase+=info.diffuse*shadow;\n#ifdef SPECULARTERM\nspecularBase+=info.specular*shadow;\n#endif\n#endif\n#ifdef LIGHT3\n#ifndef SPECULARTERM\nvec3 vLightSpecular3=vec3(0.0);\n#endif\n#ifdef SPOTLIGHT3\ninfo=computeSpotLighting(viewDirectionW,normalW,vLightData3,vLightDirection3,vLightDiffuse3.rgb,vLightSpecular3,vLightDiffuse3.a,glossiness,bumpColor);\n#endif\n#ifdef HEMILIGHT3\ninfo=computeHemisphericLighting(viewDirectionW,normalW,vLightData3,vLightDiffuse3.rgb,vLightSpecular3,vLightGround3,glossiness,bumpColor);\n#endif\n#if defined(POINTLIGHT3) || defined(DIRLIGHT3)\ninfo=computeLighting(viewDirectionW,normalW,vLightData3,vLightDiffuse3.rgb,vLightSpecular3,vLightDiffuse3.a,glossiness,bumpColor);\n#endif\n#ifdef SHADOW3\n#ifdef SHADOWVSM3\nshadow=computeShadowWithVSM(vPositionFromLight3,shadowSampler3,shadowsInfo3.z,shadowsInfo3.x);\n#else\n#ifdef SHADOWPCF3\n#if defined(POINTLIGHT3)\nshadow=computeShadowWithPCFCube(vLightData3.xyz,shadowSampler3,shadowsInfo3.z,shadowsInfo3.x,shadowsInfo3.y);\n#else\nshadow=computeShadowWithPCF(vPositionFromLight3,shadowSampler3,shadowsInfo3.y,shadowsInfo3.z,shadowsInfo3.x);\n#endif\n#else\n#if defined(POINTLIGHT3)\nshadow=computeShadowCube(vLightData3.xyz,shadowSampler3,shadowsInfo3.x,shadowsInfo3.z);\n#else\nshadow=computeShadow(vPositionFromLight3,shadowSampler3,shadowsInfo3.x,shadowsInfo3.z);\n#endif\n#endif \n#endif \n#else\nshadow=1.;\n#endif\ndiffuseBase+=info.diffuse*shadow;\n#ifdef SPECULARTERM\nspecularBase+=info.specular*shadow;\n#endif\n#endif\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\n#ifdef SPECULARTERM\nvec3 finalSpecular=specularBase*specularColor;\n#else\nvec3 finalSpecular=vec3(0.0);\n#endif\nvec3 finalDiffuse=clamp(diffuseBase*diffuseColor,0.0,1.0)*baseColor.rgb;\n\nvec4 color=vec4(finalDiffuse+finalSpecular,alpha);\n#ifdef FOG\nfloat fog=CalcFogFactor();\ncolor.rgb=fog*color.rgb+(1.0-fog)*vFogColor;\n#endif\ngl_FragColor=color;\n}";