babylon.waterMaterial.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 WaterMaterialDefines = (function (_super) {
  14. __extends(WaterMaterialDefines, _super);
  15. function WaterMaterialDefines() {
  16. _super.call(this);
  17. this.BUMP = false;
  18. this.REFLECTION = false;
  19. this.CLIPPLANE = false;
  20. this.ALPHATEST = false;
  21. this.POINTSIZE = false;
  22. this.FOG = false;
  23. this.NORMAL = false;
  24. this.UV1 = false;
  25. this.UV2 = false;
  26. this.VERTEXCOLOR = false;
  27. this.VERTEXALPHA = false;
  28. this.NUM_BONE_INFLUENCERS = 0;
  29. this.BonesPerMesh = 0;
  30. this.INSTANCES = false;
  31. this.SPECULARTERM = false;
  32. this.rebuild();
  33. }
  34. return WaterMaterialDefines;
  35. })(BABYLON.MaterialDefines);
  36. var WaterMaterial = (function (_super) {
  37. __extends(WaterMaterial, _super);
  38. /**
  39. * Constructor
  40. */
  41. function WaterMaterial(name, scene, renderTargetSize) {
  42. if (renderTargetSize === void 0) { renderTargetSize = new BABYLON.Vector2(512, 512); }
  43. _super.call(this, name, scene);
  44. this.renderTargetSize = renderTargetSize;
  45. this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  46. this.specularColor = new BABYLON.Color3(0, 0, 0);
  47. this.specularPower = 64;
  48. this.disableLighting = false;
  49. this.maxSimultaneousLights = 4;
  50. /**
  51. * @param {number}: Represents the wind force
  52. */
  53. this.windForce = 6;
  54. /**
  55. * @param {Vector2}: The direction of the wind in the plane (X, Z)
  56. */
  57. this.windDirection = new BABYLON.Vector2(0, 1);
  58. /**
  59. * @param {number}: Wave height, represents the height of the waves
  60. */
  61. this.waveHeight = 0.4;
  62. /**
  63. * @param {number}: Bump height, represents the bump height related to the bump map
  64. */
  65. this.bumpHeight = 0.4;
  66. /**
  67. * @param {number}: The water color blended with the reflection and refraction samplers
  68. */
  69. this.waterColor = new BABYLON.Color3(0.1, 0.1, 0.6);
  70. /**
  71. * @param {number}: The blend factor related to the water color
  72. */
  73. this.colorBlendFactor = 0.2;
  74. /**
  75. * @param {number}: Represents the maximum length of a wave
  76. */
  77. this.waveLength = 0.1;
  78. /**
  79. * @param {number}: Defines the waves speed
  80. */
  81. this.waveSpeed = 1.0;
  82. /*
  83. * Private members
  84. */
  85. this._mesh = null;
  86. this._reflectionTransform = BABYLON.Matrix.Zero();
  87. this._lastTime = 0;
  88. this._defines = new WaterMaterialDefines();
  89. this._cachedDefines = new WaterMaterialDefines();
  90. // Create render targets
  91. this._createRenderTargets(scene, renderTargetSize);
  92. }
  93. Object.defineProperty(WaterMaterial.prototype, "refractionTexture", {
  94. // Get / Set
  95. get: function () {
  96. return this._refractionRTT;
  97. },
  98. enumerable: true,
  99. configurable: true
  100. });
  101. Object.defineProperty(WaterMaterial.prototype, "reflectionTexture", {
  102. get: function () {
  103. return this._reflectionRTT;
  104. },
  105. enumerable: true,
  106. configurable: true
  107. });
  108. // Methods
  109. WaterMaterial.prototype.addToRenderList = function (node) {
  110. this._refractionRTT.renderList.push(node);
  111. this._reflectionRTT.renderList.push(node);
  112. };
  113. WaterMaterial.prototype.enableRenderTargets = function (enable) {
  114. var refreshRate = enable ? 1 : 0;
  115. this._refractionRTT.refreshRate = refreshRate;
  116. this._reflectionRTT.refreshRate = refreshRate;
  117. };
  118. WaterMaterial.prototype.getRenderList = function () {
  119. return this._refractionRTT.renderList;
  120. };
  121. Object.defineProperty(WaterMaterial.prototype, "renderTargetsEnabled", {
  122. get: function () {
  123. return !(this._refractionRTT.refreshRate === 0);
  124. },
  125. enumerable: true,
  126. configurable: true
  127. });
  128. WaterMaterial.prototype.needAlphaBlending = function () {
  129. return (this.alpha < 1.0);
  130. };
  131. WaterMaterial.prototype.needAlphaTesting = function () {
  132. return false;
  133. };
  134. WaterMaterial.prototype.getAlphaTestTexture = function () {
  135. return null;
  136. };
  137. WaterMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  138. if (!mesh) {
  139. return true;
  140. }
  141. if (this._defines.INSTANCES !== useInstances) {
  142. return false;
  143. }
  144. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  145. return true;
  146. }
  147. return false;
  148. };
  149. WaterMaterial.prototype.isReady = function (mesh, useInstances) {
  150. if (this.checkReadyOnlyOnce) {
  151. if (this._wasPreviouslyReady) {
  152. return true;
  153. }
  154. }
  155. var scene = this.getScene();
  156. if (!this.checkReadyOnEveryCall) {
  157. if (this._renderId === scene.getRenderId()) {
  158. if (this._checkCache(scene, mesh, useInstances)) {
  159. return true;
  160. }
  161. }
  162. }
  163. var engine = scene.getEngine();
  164. var needNormals = false;
  165. var needUVs = false;
  166. this._defines.reset();
  167. // Textures
  168. if (scene.texturesEnabled) {
  169. if (this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled) {
  170. if (!this.bumpTexture.isReady()) {
  171. return false;
  172. }
  173. else {
  174. needUVs = true;
  175. this._defines.BUMP = true;
  176. }
  177. }
  178. if (BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  179. this._defines.REFLECTION = true;
  180. }
  181. }
  182. // Effect
  183. if (scene.clipPlane) {
  184. this._defines.CLIPPLANE = true;
  185. }
  186. if (engine.getAlphaTesting()) {
  187. this._defines.ALPHATEST = true;
  188. }
  189. // Point size
  190. if (this.pointsCloud || scene.forcePointsCloud) {
  191. this._defines.POINTSIZE = true;
  192. }
  193. // Fog
  194. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  195. this._defines.FOG = true;
  196. }
  197. // Lights
  198. if (scene.lightsEnabled && !this.disableLighting) {
  199. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights);
  200. }
  201. // Attribs
  202. if (mesh) {
  203. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  204. this._defines.NORMAL = true;
  205. }
  206. if (needUVs) {
  207. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  208. this._defines.UV1 = true;
  209. }
  210. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  211. this._defines.UV2 = true;
  212. }
  213. }
  214. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  215. this._defines.VERTEXCOLOR = true;
  216. if (mesh.hasVertexAlpha) {
  217. this._defines.VERTEXALPHA = true;
  218. }
  219. }
  220. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  221. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  222. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  223. }
  224. // Instances
  225. if (useInstances) {
  226. this._defines.INSTANCES = true;
  227. }
  228. }
  229. this._mesh = mesh;
  230. // Get correct effect
  231. if (!this._defines.isEqual(this._cachedDefines)) {
  232. this._defines.cloneTo(this._cachedDefines);
  233. scene.resetCachedMaterial();
  234. // Fallbacks
  235. var fallbacks = new BABYLON.EffectFallbacks();
  236. if (this._defines.FOG) {
  237. fallbacks.addFallback(1, "FOG");
  238. }
  239. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
  240. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  241. fallbacks.addCPUSkinningFallback(0, mesh);
  242. }
  243. //Attributes
  244. var attribs = [BABYLON.VertexBuffer.PositionKind];
  245. if (this._defines.NORMAL) {
  246. attribs.push(BABYLON.VertexBuffer.NormalKind);
  247. }
  248. if (this._defines.UV1) {
  249. attribs.push(BABYLON.VertexBuffer.UVKind);
  250. }
  251. if (this._defines.UV2) {
  252. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  253. }
  254. if (this._defines.VERTEXCOLOR) {
  255. attribs.push(BABYLON.VertexBuffer.ColorKind);
  256. }
  257. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  258. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  259. // Legacy browser patch
  260. var shaderName = "water";
  261. var join = this._defines.toString();
  262. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  263. "vFogInfos", "vFogColor", "pointSize",
  264. "vNormalInfos",
  265. "mBones",
  266. "vClipPlane", "normalMatrix",
  267. // Water
  268. "worldReflectionViewProjection", "windDirection", "waveLength", "time", "windForce",
  269. "cameraPosition", "bumpHeight", "waveHeight", "waterColor", "colorBlendFactor", "waveSpeed"
  270. ];
  271. var samplers = ["normalSampler",
  272. // Water
  273. "refractionSampler", "reflectionSampler"
  274. ];
  275. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, this._defines, this.maxSimultaneousLights);
  276. this._effect = scene.getEngine().createEffect(shaderName, attribs, uniforms, samplers, join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: this.maxSimultaneousLights });
  277. }
  278. if (!this._effect.isReady()) {
  279. return false;
  280. }
  281. this._renderId = scene.getRenderId();
  282. this._wasPreviouslyReady = true;
  283. if (mesh) {
  284. if (!mesh._materialDefines) {
  285. mesh._materialDefines = new WaterMaterialDefines();
  286. }
  287. this._defines.cloneTo(mesh._materialDefines);
  288. }
  289. return true;
  290. };
  291. WaterMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  292. this._effect.setMatrix("world", world);
  293. };
  294. WaterMaterial.prototype.bind = function (world, mesh) {
  295. var scene = this.getScene();
  296. // Matrices
  297. this.bindOnlyWorldMatrix(world);
  298. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  299. // Bones
  300. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  301. if (scene.getCachedMaterial() !== this) {
  302. // Textures
  303. if (this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled) {
  304. this._effect.setTexture("normalSampler", this.bumpTexture);
  305. this._effect.setFloat2("vNormalInfos", this.bumpTexture.coordinatesIndex, this.bumpTexture.level);
  306. this._effect.setMatrix("normalMatrix", this.bumpTexture.getTextureMatrix());
  307. }
  308. // Clip plane
  309. BABYLON.MaterialHelper.BindClipPlane(this._effect, scene);
  310. // Point size
  311. if (this.pointsCloud) {
  312. this._effect.setFloat("pointSize", this.pointSize);
  313. }
  314. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  315. }
  316. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  317. if (this._defines.SPECULARTERM) {
  318. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  319. }
  320. if (scene.lightsEnabled && !this.disableLighting) {
  321. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines, this.maxSimultaneousLights);
  322. }
  323. // View
  324. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  325. this._effect.setMatrix("view", scene.getViewMatrix());
  326. }
  327. // Fog
  328. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  329. // Water
  330. if (BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  331. this._effect.setTexture("refractionSampler", this._refractionRTT);
  332. this._effect.setTexture("reflectionSampler", this._reflectionRTT);
  333. }
  334. var wrvp = this._mesh.getWorldMatrix().multiply(this._reflectionTransform).multiply(scene.getProjectionMatrix());
  335. this._lastTime += scene.getEngine().getDeltaTime();
  336. this._effect.setMatrix("worldReflectionViewProjection", wrvp);
  337. this._effect.setVector2("windDirection", this.windDirection);
  338. this._effect.setFloat("waveLength", this.waveLength);
  339. this._effect.setFloat("time", this._lastTime / 100000);
  340. this._effect.setFloat("windForce", this.windForce);
  341. this._effect.setFloat("waveHeight", this.waveHeight);
  342. this._effect.setFloat("bumpHeight", this.bumpHeight);
  343. this._effect.setColor4("waterColor", this.waterColor, 1.0);
  344. this._effect.setFloat("colorBlendFactor", this.colorBlendFactor);
  345. this._effect.setFloat("waveSpeed", this.waveSpeed);
  346. _super.prototype.bind.call(this, world, mesh);
  347. };
  348. WaterMaterial.prototype._createRenderTargets = function (scene, renderTargetSize) {
  349. var _this = this;
  350. // Render targets
  351. this._refractionRTT = new BABYLON.RenderTargetTexture(name + "_refraction", { width: renderTargetSize.x, height: renderTargetSize.y }, scene, false, true);
  352. this._reflectionRTT = new BABYLON.RenderTargetTexture(name + "_reflection", { width: renderTargetSize.x, height: renderTargetSize.y }, scene, false, true);
  353. scene.customRenderTargets.push(this._refractionRTT);
  354. scene.customRenderTargets.push(this._reflectionRTT);
  355. var isVisible;
  356. var clipPlane = null;
  357. var savedViewMatrix;
  358. var mirrorMatrix = BABYLON.Matrix.Zero();
  359. this._refractionRTT.onBeforeRender = function () {
  360. if (_this._mesh) {
  361. isVisible = _this._mesh.isVisible;
  362. _this._mesh.isVisible = false;
  363. }
  364. // Clip plane
  365. clipPlane = scene.clipPlane;
  366. var positiony = _this._mesh ? _this._mesh.position.y : 0.0;
  367. scene.clipPlane = BABYLON.Plane.FromPositionAndNormal(new BABYLON.Vector3(0, positiony + 0.05, 0), new BABYLON.Vector3(0, 1, 0));
  368. };
  369. this._refractionRTT.onAfterRender = function () {
  370. if (_this._mesh) {
  371. _this._mesh.isVisible = isVisible;
  372. }
  373. // Clip plane
  374. scene.clipPlane = clipPlane;
  375. };
  376. this._reflectionRTT.onBeforeRender = function () {
  377. if (_this._mesh) {
  378. isVisible = _this._mesh.isVisible;
  379. _this._mesh.isVisible = false;
  380. }
  381. // Clip plane
  382. clipPlane = scene.clipPlane;
  383. var positiony = _this._mesh ? _this._mesh.position.y : 0.0;
  384. scene.clipPlane = BABYLON.Plane.FromPositionAndNormal(new BABYLON.Vector3(0, positiony - 0.05, 0), new BABYLON.Vector3(0, -1, 0));
  385. // Transform
  386. BABYLON.Matrix.ReflectionToRef(scene.clipPlane, mirrorMatrix);
  387. savedViewMatrix = scene.getViewMatrix();
  388. mirrorMatrix.multiplyToRef(savedViewMatrix, _this._reflectionTransform);
  389. scene.setTransformMatrix(_this._reflectionTransform, scene.getProjectionMatrix());
  390. scene.getEngine().cullBackFaces = false;
  391. scene._mirroredCameraPosition = BABYLON.Vector3.TransformCoordinates(scene.activeCamera.position, mirrorMatrix);
  392. };
  393. this._reflectionRTT.onAfterRender = function () {
  394. if (_this._mesh) {
  395. _this._mesh.isVisible = isVisible;
  396. }
  397. // Clip plane
  398. scene.clipPlane = clipPlane;
  399. // Transform
  400. scene.setTransformMatrix(savedViewMatrix, scene.getProjectionMatrix());
  401. scene.getEngine().cullBackFaces = true;
  402. scene._mirroredCameraPosition = null;
  403. };
  404. };
  405. WaterMaterial.prototype.getAnimatables = function () {
  406. var results = [];
  407. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  408. results.push(this.bumpTexture);
  409. }
  410. if (this._reflectionRTT && this._reflectionRTT.animations && this._reflectionRTT.animations.length > 0) {
  411. results.push(this._reflectionRTT);
  412. }
  413. if (this._refractionRTT && this._refractionRTT.animations && this._refractionRTT.animations.length > 0) {
  414. results.push(this._refractionRTT);
  415. }
  416. return results;
  417. };
  418. WaterMaterial.prototype.dispose = function (forceDisposeEffect) {
  419. if (this.bumpTexture) {
  420. this.bumpTexture.dispose();
  421. }
  422. var index = this.getScene().customRenderTargets.indexOf(this._refractionRTT);
  423. if (index != -1) {
  424. this.getScene().customRenderTargets.splice(index, 1);
  425. }
  426. index = -1;
  427. index = this.getScene().customRenderTargets.indexOf(this._reflectionRTT);
  428. if (index != -1) {
  429. this.getScene().customRenderTargets.splice(index, 1);
  430. }
  431. if (this._reflectionRTT) {
  432. this._reflectionRTT.dispose();
  433. }
  434. if (this._refractionRTT) {
  435. this._refractionRTT.dispose();
  436. }
  437. _super.prototype.dispose.call(this, forceDisposeEffect);
  438. };
  439. WaterMaterial.prototype.clone = function (name) {
  440. var _this = this;
  441. return BABYLON.SerializationHelper.Clone(function () { return new WaterMaterial(name, _this.getScene()); }, this);
  442. };
  443. WaterMaterial.prototype.serialize = function () {
  444. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  445. serializationObject.customType = "BABYLON.WaterMaterial";
  446. serializationObject.reflectionTexture.isRenderTarget = true;
  447. serializationObject.refractionTexture.isRenderTarget = true;
  448. return serializationObject;
  449. };
  450. // Statics
  451. WaterMaterial.Parse = function (source, scene, rootUrl) {
  452. return BABYLON.SerializationHelper.Parse(function () { return new WaterMaterial(source.name, scene); }, source, scene, rootUrl);
  453. };
  454. WaterMaterial.CreateDefaultMesh = function (name, scene) {
  455. var mesh = BABYLON.Mesh.CreateGround(name, 512, 512, 32, scene, false);
  456. return mesh;
  457. };
  458. __decorate([
  459. BABYLON.serializeAsTexture()
  460. ], WaterMaterial.prototype, "bumpTexture");
  461. __decorate([
  462. BABYLON.serializeAsColor3()
  463. ], WaterMaterial.prototype, "diffuseColor");
  464. __decorate([
  465. BABYLON.serializeAsColor3()
  466. ], WaterMaterial.prototype, "specularColor");
  467. __decorate([
  468. BABYLON.serialize()
  469. ], WaterMaterial.prototype, "specularPower");
  470. __decorate([
  471. BABYLON.serialize()
  472. ], WaterMaterial.prototype, "disableLighting");
  473. __decorate([
  474. BABYLON.serialize()
  475. ], WaterMaterial.prototype, "maxSimultaneousLights");
  476. __decorate([
  477. BABYLON.serialize()
  478. ], WaterMaterial.prototype, "windForce");
  479. __decorate([
  480. BABYLON.serializeAsVector2()
  481. ], WaterMaterial.prototype, "windDirection");
  482. __decorate([
  483. BABYLON.serialize()
  484. ], WaterMaterial.prototype, "waveHeight");
  485. __decorate([
  486. BABYLON.serialize()
  487. ], WaterMaterial.prototype, "bumpHeight");
  488. __decorate([
  489. BABYLON.serializeAsColor3()
  490. ], WaterMaterial.prototype, "waterColor");
  491. __decorate([
  492. BABYLON.serialize()
  493. ], WaterMaterial.prototype, "colorBlendFactor");
  494. __decorate([
  495. BABYLON.serialize()
  496. ], WaterMaterial.prototype, "waveLength");
  497. __decorate([
  498. BABYLON.serialize()
  499. ], WaterMaterial.prototype, "waveSpeed");
  500. return WaterMaterial;
  501. })(BABYLON.Material);
  502. BABYLON.WaterMaterial = WaterMaterial;
  503. })(BABYLON || (BABYLON = {}));
  504. 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#include<bonesDeclaration>\n\n#include<instancesDeclaration>\nuniform mat4 view;\nuniform mat4 viewProjection;\n#ifdef BUMP\nvarying vec2 vNormalUV;\nuniform mat4 normalMatrix;\nuniform vec2 vNormalInfos;\n#endif\n#ifdef POINTSIZE\nuniform float pointSize;\n#endif\n\nvarying vec3 vPositionW;\n#ifdef NORMAL\nvarying vec3 vNormalW;\n#endif\n#ifdef VERTEXCOLOR\nvarying vec4 vColor;\n#endif\n#include<clipPlaneVertexDeclaration>\n#include<fogVertexDeclaration>\n#include<shadowsVertexDeclaration>\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) {\n#include<instancesVertex>\n#include<bonesVertex>\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#include<clipPlaneVertex>\n\n#include<fogVertex>\n\n#include<shadowsVertex>\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";
  505. 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#include<lightFragmentDeclaration>[0..maxSimultaneousLights]\n#include<lightsFragmentFunctions>\n#include<shadowsFragmentFunctions>\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#include<clipPlaneFragmentDeclaration>\n\n#include<fogFragmentDeclaration>\nvoid main(void) {\n\n#include<clipPlaneFragment>\nvec3 viewDirectionW=normalize(vEyePosition-vPositionW);\n\nvec4 baseColor=vec4(1.,1.,1.,1.);\nvec3 diffuseColor=vDiffuseColor.rgb;\n\nfloat alpha=vDiffuseColor.a;\n#ifdef 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.);\nlightingInfo info;\nfloat shadow=1.;\n#ifdef SPECULARTERM\nfloat glossiness=vSpecularColor.a;\nvec3 specularBase=vec3(0.,0.,0.);\nvec3 specularColor=vSpecularColor.rgb;\n#else\nfloat glossiness=0.;\n#endif\n#include<lightFragment>[0..maxSimultaneousLights]\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#include<fogFragment>\ngl_FragColor=color;\n}";