babylon.waterMaterial.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  8. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  9. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  10. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  11. return c > 3 && r && Object.defineProperty(target, key, r), r;
  12. };
  13. var BABYLON;
  14. (function (BABYLON) {
  15. var WaterMaterialDefines = (function (_super) {
  16. __extends(WaterMaterialDefines, _super);
  17. function WaterMaterialDefines() {
  18. var _this = _super.call(this) || this;
  19. _this.BUMP = false;
  20. _this.REFLECTION = false;
  21. _this.CLIPPLANE = false;
  22. _this.ALPHATEST = false;
  23. _this.POINTSIZE = false;
  24. _this.FOG = false;
  25. _this.NORMAL = false;
  26. _this.UV1 = false;
  27. _this.UV2 = false;
  28. _this.VERTEXCOLOR = false;
  29. _this.VERTEXALPHA = false;
  30. _this.NUM_BONE_INFLUENCERS = 0;
  31. _this.BonesPerMesh = 0;
  32. _this.INSTANCES = false;
  33. _this.SPECULARTERM = false;
  34. _this.LOGARITHMICDEPTH = false;
  35. _this.FRESNELSEPARATE = false;
  36. _this.BUMPSUPERIMPOSE = false;
  37. _this.BUMPAFFECTSREFLECTION = false;
  38. _this.rebuild();
  39. return _this;
  40. }
  41. return WaterMaterialDefines;
  42. }(BABYLON.MaterialDefines));
  43. var WaterMaterial = (function (_super) {
  44. __extends(WaterMaterial, _super);
  45. /**
  46. * Constructor
  47. */
  48. function WaterMaterial(name, scene, renderTargetSize) {
  49. if (renderTargetSize === void 0) { renderTargetSize = new BABYLON.Vector2(512, 512); }
  50. var _this = _super.call(this, name, scene) || this;
  51. _this.renderTargetSize = renderTargetSize;
  52. _this.diffuseColor = new BABYLON.Color3(1, 1, 1);
  53. _this.specularColor = new BABYLON.Color3(0, 0, 0);
  54. _this.specularPower = 64;
  55. _this.disableLighting = false;
  56. _this.maxSimultaneousLights = 4;
  57. /**
  58. * @param {number}: Represents the wind force
  59. */
  60. _this.windForce = 6;
  61. /**
  62. * @param {Vector2}: The direction of the wind in the plane (X, Z)
  63. */
  64. _this.windDirection = new BABYLON.Vector2(0, 1);
  65. /**
  66. * @param {number}: Wave height, represents the height of the waves
  67. */
  68. _this.waveHeight = 0.4;
  69. /**
  70. * @param {number}: Bump height, represents the bump height related to the bump map
  71. */
  72. _this.bumpHeight = 0.4;
  73. /**
  74. * @param {boolean}: Add a smaller moving bump to less steady waves.
  75. */
  76. _this.bumpSuperimpose = false;
  77. /**
  78. * @param {boolean}: Color refraction and reflection differently with .waterColor2 and .colorBlendFactor2. Non-linear (physically correct) fresnel.
  79. */
  80. _this.fresnelSeparate = false;
  81. /**
  82. * @param {boolean}: bump Waves modify the reflection.
  83. */
  84. _this.bumpAffectsReflection = false;
  85. /**
  86. * @param {number}: The water color blended with the refraction (near)
  87. */
  88. _this.waterColor = new BABYLON.Color3(0.1, 0.1, 0.6);
  89. /**
  90. * @param {number}: The blend factor related to the water color
  91. */
  92. _this.colorBlendFactor = 0.2;
  93. /**
  94. * @param {number}: The water color blended with the reflection (far)
  95. */
  96. _this.waterColor2 = new BABYLON.Color3(0.1, 0.1, 0.6);
  97. /**
  98. * @param {number}: The blend factor related to the water color (reflection, far)
  99. */
  100. _this.colorBlendFactor2 = 0.2;
  101. /**
  102. * @param {number}: Represents the maximum length of a wave
  103. */
  104. _this.waveLength = 0.1;
  105. /**
  106. * @param {number}: Defines the waves speed
  107. */
  108. _this.waveSpeed = 1.0;
  109. /*
  110. * Private members
  111. */
  112. _this._mesh = null;
  113. _this._reflectionTransform = BABYLON.Matrix.Zero();
  114. _this._lastTime = 0;
  115. _this._defines = new WaterMaterialDefines();
  116. _this._cachedDefines = new WaterMaterialDefines();
  117. // Create render targets
  118. _this._createRenderTargets(scene, renderTargetSize);
  119. return _this;
  120. }
  121. Object.defineProperty(WaterMaterial.prototype, "useLogarithmicDepth", {
  122. get: function () {
  123. return this._useLogarithmicDepth;
  124. },
  125. set: function (value) {
  126. this._useLogarithmicDepth = value && this.getScene().getEngine().getCaps().fragmentDepthSupported;
  127. },
  128. enumerable: true,
  129. configurable: true
  130. });
  131. Object.defineProperty(WaterMaterial.prototype, "refractionTexture", {
  132. // Get / Set
  133. get: function () {
  134. return this._refractionRTT;
  135. },
  136. enumerable: true,
  137. configurable: true
  138. });
  139. Object.defineProperty(WaterMaterial.prototype, "reflectionTexture", {
  140. get: function () {
  141. return this._reflectionRTT;
  142. },
  143. enumerable: true,
  144. configurable: true
  145. });
  146. // Methods
  147. WaterMaterial.prototype.addToRenderList = function (node) {
  148. this._refractionRTT.renderList.push(node);
  149. this._reflectionRTT.renderList.push(node);
  150. };
  151. WaterMaterial.prototype.enableRenderTargets = function (enable) {
  152. var refreshRate = enable ? 1 : 0;
  153. this._refractionRTT.refreshRate = refreshRate;
  154. this._reflectionRTT.refreshRate = refreshRate;
  155. };
  156. WaterMaterial.prototype.getRenderList = function () {
  157. return this._refractionRTT.renderList;
  158. };
  159. Object.defineProperty(WaterMaterial.prototype, "renderTargetsEnabled", {
  160. get: function () {
  161. return !(this._refractionRTT.refreshRate === 0);
  162. },
  163. enumerable: true,
  164. configurable: true
  165. });
  166. WaterMaterial.prototype.needAlphaBlending = function () {
  167. return (this.alpha < 1.0);
  168. };
  169. WaterMaterial.prototype.needAlphaTesting = function () {
  170. return false;
  171. };
  172. WaterMaterial.prototype.getAlphaTestTexture = function () {
  173. return null;
  174. };
  175. WaterMaterial.prototype._checkCache = function (scene, mesh, useInstances) {
  176. if (!mesh) {
  177. return true;
  178. }
  179. if (this._defines.INSTANCES !== useInstances) {
  180. return false;
  181. }
  182. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  183. return true;
  184. }
  185. return false;
  186. };
  187. WaterMaterial.prototype.isReady = function (mesh, useInstances) {
  188. if (this.checkReadyOnlyOnce) {
  189. if (this._wasPreviouslyReady) {
  190. return true;
  191. }
  192. }
  193. var scene = this.getScene();
  194. if (!this.checkReadyOnEveryCall) {
  195. if (this._renderId === scene.getRenderId()) {
  196. if (this._checkCache(scene, mesh, useInstances)) {
  197. return true;
  198. }
  199. }
  200. }
  201. var engine = scene.getEngine();
  202. var needNormals = false;
  203. var needUVs = false;
  204. this._defines.reset();
  205. // Textures
  206. if (scene.texturesEnabled) {
  207. if (this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled) {
  208. if (!this.bumpTexture.isReady()) {
  209. return false;
  210. }
  211. else {
  212. needUVs = true;
  213. this._defines.BUMP = true;
  214. }
  215. }
  216. if (BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  217. this._defines.REFLECTION = true;
  218. }
  219. }
  220. // Effect
  221. if (scene.clipPlane) {
  222. this._defines.CLIPPLANE = true;
  223. }
  224. if (engine.getAlphaTesting()) {
  225. this._defines.ALPHATEST = true;
  226. }
  227. // Point size
  228. if (this.pointsCloud || scene.forcePointsCloud) {
  229. this._defines.POINTSIZE = true;
  230. }
  231. if (this.useLogarithmicDepth) {
  232. this._defines.LOGARITHMICDEPTH = true;
  233. }
  234. if (this.fresnelSeparate) {
  235. this._defines.FRESNELSEPARATE = true;
  236. }
  237. if (this.bumpSuperimpose) {
  238. this._defines.BUMPSUPERIMPOSE = true;
  239. }
  240. if (this.bumpAffectsReflection) {
  241. this._defines.BUMPAFFECTSREFLECTION = true;
  242. }
  243. // Fog
  244. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE && this.fogEnabled) {
  245. this._defines.FOG = true;
  246. }
  247. // Lights
  248. if (scene.lightsEnabled && !this.disableLighting) {
  249. needNormals = BABYLON.MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, this.maxSimultaneousLights);
  250. }
  251. // Attribs
  252. if (mesh) {
  253. if (needNormals && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  254. this._defines.NORMAL = true;
  255. }
  256. if (needUVs) {
  257. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  258. this._defines.UV1 = true;
  259. }
  260. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  261. this._defines.UV2 = true;
  262. }
  263. }
  264. if (mesh.useVertexColors && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  265. this._defines.VERTEXCOLOR = true;
  266. if (mesh.hasVertexAlpha) {
  267. this._defines.VERTEXALPHA = true;
  268. }
  269. }
  270. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  271. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  272. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  273. }
  274. // Instances
  275. if (useInstances) {
  276. this._defines.INSTANCES = true;
  277. }
  278. }
  279. this._mesh = mesh;
  280. // Get correct effect
  281. if (!this._defines.isEqual(this._cachedDefines)) {
  282. this._defines.cloneTo(this._cachedDefines);
  283. scene.resetCachedMaterial();
  284. // Fallbacks
  285. var fallbacks = new BABYLON.EffectFallbacks();
  286. if (this._defines.FOG) {
  287. fallbacks.addFallback(1, "FOG");
  288. }
  289. if (this._defines.LOGARITHMICDEPTH) {
  290. fallbacks.addFallback(0, "LOGARITHMICDEPTH");
  291. }
  292. BABYLON.MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks, this.maxSimultaneousLights);
  293. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  294. fallbacks.addCPUSkinningFallback(0, mesh);
  295. }
  296. //Attributes
  297. var attribs = [BABYLON.VertexBuffer.PositionKind];
  298. if (this._defines.NORMAL) {
  299. attribs.push(BABYLON.VertexBuffer.NormalKind);
  300. }
  301. if (this._defines.UV1) {
  302. attribs.push(BABYLON.VertexBuffer.UVKind);
  303. }
  304. if (this._defines.UV2) {
  305. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  306. }
  307. if (this._defines.VERTEXCOLOR) {
  308. attribs.push(BABYLON.VertexBuffer.ColorKind);
  309. }
  310. BABYLON.MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  311. BABYLON.MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  312. // Legacy browser patch
  313. var shaderName = "water";
  314. var join = this._defines.toString();
  315. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  316. "vFogInfos", "vFogColor", "pointSize",
  317. "vNormalInfos",
  318. "mBones",
  319. "vClipPlane", "normalMatrix",
  320. "logarithmicDepthConstant",
  321. // Water
  322. "worldReflectionViewProjection", "windDirection", "waveLength", "time", "windForce",
  323. "cameraPosition", "bumpHeight", "waveHeight", "waterColor", "waterColor2", "colorBlendFactor", "colorBlendFactor2", "waveSpeed"
  324. ];
  325. var samplers = ["normalSampler",
  326. // Water
  327. "refractionSampler", "reflectionSampler"
  328. ];
  329. BABYLON.MaterialHelper.PrepareUniformsAndSamplersList(uniforms, samplers, this._defines, this.maxSimultaneousLights);
  330. this._effect = scene.getEngine().createEffect(shaderName, attribs, uniforms, samplers, join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: this.maxSimultaneousLights });
  331. }
  332. if (!this._effect.isReady()) {
  333. return false;
  334. }
  335. this._renderId = scene.getRenderId();
  336. this._wasPreviouslyReady = true;
  337. if (mesh) {
  338. if (!mesh._materialDefines) {
  339. mesh._materialDefines = new WaterMaterialDefines();
  340. }
  341. this._defines.cloneTo(mesh._materialDefines);
  342. }
  343. return true;
  344. };
  345. WaterMaterial.prototype.bindOnlyWorldMatrix = function (world) {
  346. this._effect.setMatrix("world", world);
  347. };
  348. WaterMaterial.prototype.bind = function (world, mesh) {
  349. var scene = this.getScene();
  350. // Matrices
  351. this.bindOnlyWorldMatrix(world);
  352. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  353. // Bones
  354. BABYLON.MaterialHelper.BindBonesParameters(mesh, this._effect);
  355. if (scene.getCachedMaterial() !== this) {
  356. // Textures
  357. if (this.bumpTexture && BABYLON.StandardMaterial.BumpTextureEnabled) {
  358. this._effect.setTexture("normalSampler", this.bumpTexture);
  359. this._effect.setFloat2("vNormalInfos", this.bumpTexture.coordinatesIndex, this.bumpTexture.level);
  360. this._effect.setMatrix("normalMatrix", this.bumpTexture.getTextureMatrix());
  361. }
  362. // Clip plane
  363. BABYLON.MaterialHelper.BindClipPlane(this._effect, scene);
  364. // Point size
  365. if (this.pointsCloud) {
  366. this._effect.setFloat("pointSize", this.pointSize);
  367. }
  368. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  369. }
  370. this._effect.setColor4("vDiffuseColor", this.diffuseColor, this.alpha * mesh.visibility);
  371. if (this._defines.SPECULARTERM) {
  372. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  373. }
  374. if (scene.lightsEnabled && !this.disableLighting) {
  375. BABYLON.MaterialHelper.BindLights(scene, mesh, this._effect, this._defines, this.maxSimultaneousLights);
  376. }
  377. // View
  378. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== BABYLON.Scene.FOGMODE_NONE) {
  379. this._effect.setMatrix("view", scene.getViewMatrix());
  380. }
  381. // Fog
  382. BABYLON.MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  383. // Log. depth
  384. BABYLON.MaterialHelper.BindLogDepth(this._defines, this._effect, scene);
  385. // Water
  386. if (BABYLON.StandardMaterial.ReflectionTextureEnabled) {
  387. this._effect.setTexture("refractionSampler", this._refractionRTT);
  388. this._effect.setTexture("reflectionSampler", this._reflectionRTT);
  389. }
  390. var wrvp = this._mesh.getWorldMatrix().multiply(this._reflectionTransform).multiply(scene.getProjectionMatrix());
  391. this._lastTime += scene.getEngine().getDeltaTime();
  392. this._effect.setMatrix("worldReflectionViewProjection", wrvp);
  393. this._effect.setVector2("windDirection", this.windDirection);
  394. this._effect.setFloat("waveLength", this.waveLength);
  395. this._effect.setFloat("time", this._lastTime / 100000);
  396. this._effect.setFloat("windForce", this.windForce);
  397. this._effect.setFloat("waveHeight", this.waveHeight);
  398. this._effect.setFloat("bumpHeight", this.bumpHeight);
  399. this._effect.setColor4("waterColor", this.waterColor, 1.0);
  400. this._effect.setFloat("colorBlendFactor", this.colorBlendFactor);
  401. this._effect.setColor4("waterColor2", this.waterColor2, 1.0);
  402. this._effect.setFloat("colorBlendFactor2", this.colorBlendFactor2);
  403. this._effect.setFloat("waveSpeed", this.waveSpeed);
  404. _super.prototype.bind.call(this, world, mesh);
  405. };
  406. WaterMaterial.prototype._createRenderTargets = function (scene, renderTargetSize) {
  407. var _this = this;
  408. // Render targets
  409. this._refractionRTT = new BABYLON.RenderTargetTexture(name + "_refraction", { width: renderTargetSize.x, height: renderTargetSize.y }, scene, false, true);
  410. this._refractionRTT.wrapU = BABYLON.Texture.MIRROR_ADDRESSMODE;
  411. this._refractionRTT.wrapV = BABYLON.Texture.MIRROR_ADDRESSMODE;
  412. this._reflectionRTT = new BABYLON.RenderTargetTexture(name + "_reflection", { width: renderTargetSize.x, height: renderTargetSize.y }, scene, false, true);
  413. this._reflectionRTT.wrapU = BABYLON.Texture.MIRROR_ADDRESSMODE;
  414. this._reflectionRTT.wrapV = BABYLON.Texture.MIRROR_ADDRESSMODE;
  415. scene.customRenderTargets.push(this._refractionRTT);
  416. scene.customRenderTargets.push(this._reflectionRTT);
  417. var isVisible;
  418. var clipPlane = null;
  419. var savedViewMatrix;
  420. var mirrorMatrix = BABYLON.Matrix.Zero();
  421. this._refractionRTT.onBeforeRender = function () {
  422. if (_this._mesh) {
  423. isVisible = _this._mesh.isVisible;
  424. _this._mesh.isVisible = false;
  425. }
  426. // Clip plane
  427. clipPlane = scene.clipPlane;
  428. var positiony = _this._mesh ? _this._mesh.position.y : 0.0;
  429. scene.clipPlane = BABYLON.Plane.FromPositionAndNormal(new BABYLON.Vector3(0, positiony + 0.05, 0), new BABYLON.Vector3(0, 1, 0));
  430. };
  431. this._refractionRTT.onAfterRender = function () {
  432. if (_this._mesh) {
  433. _this._mesh.isVisible = isVisible;
  434. }
  435. // Clip plane
  436. scene.clipPlane = clipPlane;
  437. };
  438. this._reflectionRTT.onBeforeRender = function () {
  439. if (_this._mesh) {
  440. isVisible = _this._mesh.isVisible;
  441. _this._mesh.isVisible = false;
  442. }
  443. // Clip plane
  444. clipPlane = scene.clipPlane;
  445. var positiony = _this._mesh ? _this._mesh.position.y : 0.0;
  446. scene.clipPlane = BABYLON.Plane.FromPositionAndNormal(new BABYLON.Vector3(0, positiony - 0.05, 0), new BABYLON.Vector3(0, -1, 0));
  447. // Transform
  448. BABYLON.Matrix.ReflectionToRef(scene.clipPlane, mirrorMatrix);
  449. savedViewMatrix = scene.getViewMatrix();
  450. mirrorMatrix.multiplyToRef(savedViewMatrix, _this._reflectionTransform);
  451. scene.setTransformMatrix(_this._reflectionTransform, scene.getProjectionMatrix());
  452. scene.getEngine().cullBackFaces = false;
  453. scene._mirroredCameraPosition = BABYLON.Vector3.TransformCoordinates(scene.activeCamera.position, mirrorMatrix);
  454. };
  455. this._reflectionRTT.onAfterRender = function () {
  456. if (_this._mesh) {
  457. _this._mesh.isVisible = isVisible;
  458. }
  459. // Clip plane
  460. scene.clipPlane = clipPlane;
  461. // Transform
  462. scene.setTransformMatrix(savedViewMatrix, scene.getProjectionMatrix());
  463. scene.getEngine().cullBackFaces = true;
  464. scene._mirroredCameraPosition = null;
  465. };
  466. };
  467. WaterMaterial.prototype.getAnimatables = function () {
  468. var results = [];
  469. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  470. results.push(this.bumpTexture);
  471. }
  472. if (this._reflectionRTT && this._reflectionRTT.animations && this._reflectionRTT.animations.length > 0) {
  473. results.push(this._reflectionRTT);
  474. }
  475. if (this._refractionRTT && this._refractionRTT.animations && this._refractionRTT.animations.length > 0) {
  476. results.push(this._refractionRTT);
  477. }
  478. return results;
  479. };
  480. WaterMaterial.prototype.dispose = function (forceDisposeEffect) {
  481. if (this.bumpTexture) {
  482. this.bumpTexture.dispose();
  483. }
  484. var index = this.getScene().customRenderTargets.indexOf(this._refractionRTT);
  485. if (index != -1) {
  486. this.getScene().customRenderTargets.splice(index, 1);
  487. }
  488. index = -1;
  489. index = this.getScene().customRenderTargets.indexOf(this._reflectionRTT);
  490. if (index != -1) {
  491. this.getScene().customRenderTargets.splice(index, 1);
  492. }
  493. if (this._reflectionRTT) {
  494. this._reflectionRTT.dispose();
  495. }
  496. if (this._refractionRTT) {
  497. this._refractionRTT.dispose();
  498. }
  499. _super.prototype.dispose.call(this, forceDisposeEffect);
  500. };
  501. WaterMaterial.prototype.clone = function (name) {
  502. var _this = this;
  503. return BABYLON.SerializationHelper.Clone(function () { return new WaterMaterial(name, _this.getScene()); }, this);
  504. };
  505. WaterMaterial.prototype.serialize = function () {
  506. var serializationObject = BABYLON.SerializationHelper.Serialize(this);
  507. serializationObject.customType = "BABYLON.WaterMaterial";
  508. serializationObject.reflectionTexture.isRenderTarget = true;
  509. serializationObject.refractionTexture.isRenderTarget = true;
  510. return serializationObject;
  511. };
  512. // Statics
  513. WaterMaterial.Parse = function (source, scene, rootUrl) {
  514. return BABYLON.SerializationHelper.Parse(function () { return new WaterMaterial(source.name, scene); }, source, scene, rootUrl);
  515. };
  516. WaterMaterial.CreateDefaultMesh = function (name, scene) {
  517. var mesh = BABYLON.Mesh.CreateGround(name, 512, 512, 32, scene, false);
  518. return mesh;
  519. };
  520. return WaterMaterial;
  521. }(BABYLON.Material));
  522. __decorate([
  523. BABYLON.serializeAsTexture()
  524. ], WaterMaterial.prototype, "bumpTexture", void 0);
  525. __decorate([
  526. BABYLON.serializeAsColor3()
  527. ], WaterMaterial.prototype, "diffuseColor", void 0);
  528. __decorate([
  529. BABYLON.serializeAsColor3()
  530. ], WaterMaterial.prototype, "specularColor", void 0);
  531. __decorate([
  532. BABYLON.serialize()
  533. ], WaterMaterial.prototype, "specularPower", void 0);
  534. __decorate([
  535. BABYLON.serialize()
  536. ], WaterMaterial.prototype, "disableLighting", void 0);
  537. __decorate([
  538. BABYLON.serialize()
  539. ], WaterMaterial.prototype, "maxSimultaneousLights", void 0);
  540. __decorate([
  541. BABYLON.serialize()
  542. ], WaterMaterial.prototype, "windForce", void 0);
  543. __decorate([
  544. BABYLON.serializeAsVector2()
  545. ], WaterMaterial.prototype, "windDirection", void 0);
  546. __decorate([
  547. BABYLON.serialize()
  548. ], WaterMaterial.prototype, "waveHeight", void 0);
  549. __decorate([
  550. BABYLON.serialize()
  551. ], WaterMaterial.prototype, "bumpHeight", void 0);
  552. __decorate([
  553. BABYLON.serialize()
  554. ], WaterMaterial.prototype, "bumpSuperimpose", void 0);
  555. __decorate([
  556. BABYLON.serialize()
  557. ], WaterMaterial.prototype, "fresnelSeparate", void 0);
  558. __decorate([
  559. BABYLON.serialize()
  560. ], WaterMaterial.prototype, "bumpAffectsReflection", void 0);
  561. __decorate([
  562. BABYLON.serializeAsColor3()
  563. ], WaterMaterial.prototype, "waterColor", void 0);
  564. __decorate([
  565. BABYLON.serialize()
  566. ], WaterMaterial.prototype, "colorBlendFactor", void 0);
  567. __decorate([
  568. BABYLON.serializeAsColor3()
  569. ], WaterMaterial.prototype, "waterColor2", void 0);
  570. __decorate([
  571. BABYLON.serialize()
  572. ], WaterMaterial.prototype, "colorBlendFactor2", void 0);
  573. __decorate([
  574. BABYLON.serialize()
  575. ], WaterMaterial.prototype, "waveLength", void 0);
  576. __decorate([
  577. BABYLON.serialize()
  578. ], WaterMaterial.prototype, "waveSpeed", void 0);
  579. __decorate([
  580. BABYLON.serialize()
  581. ], WaterMaterial.prototype, "useLogarithmicDepth", null);
  582. BABYLON.WaterMaterial = WaterMaterial;
  583. })(BABYLON || (BABYLON = {}));
  584. //# sourceMappingURL=babylon.waterMaterial.js.map
  585. 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;\n#ifdef BUMPSUPERIMPOSE\nvarying vec2 vNormalUV2;\n#endif\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>[0..maxSimultaneousLights]\n#include<logDepthDeclaration>\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#ifdef BUMPSUPERIMPOSE\nvNormalUV2=vec2(normalMatrix*vec4((uv*0.721)/waveLength+time*1.2*windForce*windDirection,1.0,0.0));\n#endif\n}\nelse\n{\nvNormalUV=vec2(normalMatrix*vec4((uv2*1.0)/waveLength+time*windForce*windDirection ,1.0,0.0));\n#ifdef BUMPSUPERIMPOSE\nvNormalUV2=vec2(normalMatrix*vec4((uv2*0.721)/waveLength+time*1.2*windForce*windDirection ,1.0,0.0));\n#endif\n}\n#endif\n\n#include<clipPlaneVertex>\n\n#include<fogVertex>\n\n#include<shadowsVertex>[0..maxSimultaneousLights]\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#include<logDepthVertex>\n}\n";
  586. BABYLON.Effect.ShadersStore['waterPixelShader'] = "#ifdef LOGARITHMICDEPTH\n#extension GL_EXT_frag_depth : enable\n#endif\nprecision 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;\nvarying vec2 vNormalUV2;\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 vec4 waterColor2;\nuniform float colorBlendFactor2;\nuniform float bumpHeight;\nuniform float time;\n\nvarying vec3 vRefractionMapTexCoord;\nvarying vec3 vReflectionMapTexCoord;\nvarying vec3 vPosition;\n#include<clipPlaneFragmentDeclaration>\n#include<logDepthDeclaration>\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\n#ifdef BUMPSUPERIMPOSE\nbaseColor=0.6*texture2D(normalSampler,vNormalUV)+0.4*texture2D(normalSampler,vec2(vNormalUV2.x,vNormalUV2.y));\n#else\nbaseColor=texture2D(normalSampler,vNormalUV);\n#endif\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\nvec2 perturbation=bumpHeight*(baseColor.rg-0.5);\n#ifdef BUMPAFFECTSREFLECTION\nvec3 normalW=normalize(vNormalW+vec3(perturbation.x*8.0,0.0,perturbation.y*8.0));\nif (normalW.y<0.0) {\nnormalW.y=-normalW.y;\n}\n#else\nvec3 normalW=normalize(vNormalW);\n#endif\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 FRESNELSEPARATE\n#ifdef REFLECTION\n\nvec3 eyeVector=normalize(vEyePosition-vPosition);\nvec2 projectedRefractionTexCoords=clamp(vRefractionMapTexCoord.xy/vRefractionMapTexCoord.z+perturbation*0.5,0.0,1.0);\nvec4 refractiveColor=texture2D(refractionSampler,projectedRefractionTexCoords);\nvec2 projectedReflectionTexCoords=clamp(vec2(\nvReflectionMapTexCoord.x/vReflectionMapTexCoord.z+perturbation.x*0.3,\nvReflectionMapTexCoord.y/vReflectionMapTexCoord.z+perturbation.y\n),0.0,1.0);\nvec4 reflectiveColor=texture2D(reflectionSampler,projectedReflectionTexCoords);\nvec3 upVector=vec3(0.0,1.0,0.0);\nfloat fresnelTerm=clamp(abs(pow(dot(eyeVector,upVector),3.0)),0.05,0.65);\nfloat IfresnelTerm=1.0-fresnelTerm;\nrefractiveColor=colorBlendFactor*waterColor+(1.0-colorBlendFactor)*refractiveColor;\nreflectiveColor=IfresnelTerm*colorBlendFactor2*waterColor+(1.0-colorBlendFactor2*IfresnelTerm)*reflectiveColor;\nvec4 combinedColor=refractiveColor*fresnelTerm+reflectiveColor*IfresnelTerm;\nbaseColor=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]\nvec3 finalDiffuse=clamp(baseColor.rgb,0.0,1.0);\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\n#ifdef SPECULARTERM\nvec3 finalSpecular=specularBase*specularColor;\n#else\nvec3 finalSpecular=vec3(0.0);\n#endif\n#else \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]\nvec3 finalDiffuse=clamp(baseColor.rgb,0.0,1.0);\n#ifdef VERTEXALPHA\nalpha*=vColor.a;\n#endif\n#ifdef SPECULARTERM\nvec3 finalSpecular=specularBase*specularColor;\n#else\nvec3 finalSpecular=vec3(0.0);\n#endif\n#endif\n\nvec4 color=vec4(finalDiffuse+finalSpecular,alpha);\n#include<logDepthFragment>\n#include<fogFragment>\ngl_FragColor=color;\n}\n";