babylon.waterMaterial.js 34 KB

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