babylon.waterMaterial.js 36 KB

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