babylon.waterMaterial.js 37 KB

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