babylon.waterMaterial.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. /// <reference path="../simple/babylon.simpleMaterial.ts"/>
  3. module BABYLON {
  4. var maxSimultaneousLights = 4;
  5. class WaterMaterialDefines extends MaterialDefines {
  6. public BUMP = false;
  7. public REFLECTION = false;
  8. public CLIPPLANE = false;
  9. public ALPHATEST = false;
  10. public POINTSIZE = false;
  11. public FOG = false;
  12. public LIGHT0 = false;
  13. public LIGHT1 = false;
  14. public LIGHT2 = false;
  15. public LIGHT3 = false;
  16. public SPOTLIGHT0 = false;
  17. public SPOTLIGHT1 = false;
  18. public SPOTLIGHT2 = false;
  19. public SPOTLIGHT3 = false;
  20. public HEMILIGHT0 = false;
  21. public HEMILIGHT1 = false;
  22. public HEMILIGHT2 = false;
  23. public HEMILIGHT3 = false;
  24. public DIRLIGHT0 = false;
  25. public DIRLIGHT1 = false;
  26. public DIRLIGHT2 = false;
  27. public DIRLIGHT3 = false;
  28. public POINTLIGHT0 = false;
  29. public POINTLIGHT1 = false;
  30. public POINTLIGHT2 = false;
  31. public POINTLIGHT3 = false;
  32. public SHADOW0 = false;
  33. public SHADOW1 = false;
  34. public SHADOW2 = false;
  35. public SHADOW3 = false;
  36. public SHADOWS = false;
  37. public SHADOWVSM0 = false;
  38. public SHADOWVSM1 = false;
  39. public SHADOWVSM2 = false;
  40. public SHADOWVSM3 = false;
  41. public SHADOWPCF0 = false;
  42. public SHADOWPCF1 = false;
  43. public SHADOWPCF2 = false;
  44. public SHADOWPCF3 = false;
  45. public NORMAL = false;
  46. public UV1 = false;
  47. public UV2 = false;
  48. public VERTEXCOLOR = false;
  49. public VERTEXALPHA = false;
  50. public NUM_BONE_INFLUENCERS = 0;
  51. public BonesPerMesh = 0;
  52. public INSTANCES = false;
  53. public SPECULARTERM = false;
  54. constructor() {
  55. super();
  56. this._keys = Object.keys(this);
  57. }
  58. }
  59. export class WaterMaterial extends Material {
  60. /*
  61. * Public members
  62. */
  63. public bumpTexture: BaseTexture;
  64. public diffuseColor = new Color3(1, 1, 1);
  65. public specularColor = new Color3(0, 0, 0);
  66. public specularPower = 64;
  67. public disableLighting = false;
  68. /**
  69. * @param {number}: Represents the wind force
  70. */
  71. public windForce: number = 6;
  72. /**
  73. * @param {Vector2}: The direction of the wind in the plane (X, Z)
  74. */
  75. public windDirection: Vector2 = new Vector2(0, 1);
  76. /**
  77. * @param {number}: Wave height, represents the height of the waves
  78. */
  79. public waveHeight: number = 0.4;
  80. /**
  81. * @param {number}: Bump height, represents the bump height related to the bump map
  82. */
  83. public bumpHeight: number = 0.4;
  84. /**
  85. * @param {number}: The water color blended with the reflection and refraction samplers
  86. */
  87. public waterColor: Color3 = new Color3(0.1, 0.1, 0.6);
  88. /**
  89. * @param {number}: The blend factor related to the water color
  90. */
  91. public colorBlendFactor: number = 0.2;
  92. /**
  93. * @param {number}: Represents the maximum length of a wave
  94. */
  95. public waveLength: number = 0.1;
  96. /**
  97. * @param {number}: Defines the waves speed
  98. */
  99. public waveSpeed: number = 1.0;
  100. /*
  101. * Private members
  102. */
  103. private _mesh: AbstractMesh = null;
  104. private _refractionRTT: RenderTargetTexture;
  105. private _reflectionRTT: RenderTargetTexture;
  106. private _material: ShaderMaterial;
  107. private _reflectionTransform: Matrix = Matrix.Zero();
  108. private _lastTime: number = 0;
  109. private _scaledDiffuse = new Color3();
  110. private _scaledSpecular = new Color3();
  111. private _renderId: number;
  112. private _defines = new WaterMaterialDefines();
  113. private _cachedDefines = new WaterMaterialDefines();
  114. /**
  115. * Constructor
  116. */
  117. constructor(name: string, scene: Scene, renderTargetSize: Vector2 = new Vector2(512, 512)) {
  118. super(name, scene);
  119. // Create render targets
  120. this._createRenderTargets(scene, renderTargetSize);
  121. }
  122. // Get / Set
  123. public get refractionTexture(): RenderTargetTexture {
  124. return this._refractionRTT;
  125. }
  126. public get reflectionTexture(): RenderTargetTexture {
  127. return this._reflectionRTT;
  128. }
  129. // Methods
  130. public addToRenderList(node: any): void {
  131. this._refractionRTT.renderList.push(node);
  132. this._reflectionRTT.renderList.push(node);
  133. }
  134. public enableRenderTargets(enable: boolean): void {
  135. var refreshRate = enable ? 1 : 0;
  136. this._refractionRTT.refreshRate = refreshRate;
  137. this._reflectionRTT.refreshRate = refreshRate;
  138. }
  139. public needAlphaBlending(): boolean {
  140. return (this.alpha < 1.0);
  141. }
  142. public needAlphaTesting(): boolean {
  143. return false;
  144. }
  145. public getAlphaTestTexture(): BaseTexture {
  146. return null;
  147. }
  148. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  149. if (!mesh) {
  150. return true;
  151. }
  152. if (this._defines.INSTANCES !== useInstances) {
  153. return false;
  154. }
  155. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  156. return true;
  157. }
  158. return false;
  159. }
  160. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  161. if (this.checkReadyOnlyOnce) {
  162. if (this._wasPreviouslyReady) {
  163. return true;
  164. }
  165. }
  166. var scene = this.getScene();
  167. if (!this.checkReadyOnEveryCall) {
  168. if (this._renderId === scene.getRenderId()) {
  169. if (this._checkCache(scene, mesh, useInstances)) {
  170. return true;
  171. }
  172. }
  173. }
  174. var engine = scene.getEngine();
  175. var needNormals = false;
  176. var needUVs = false;
  177. this._defines.reset();
  178. // Textures
  179. if (scene.texturesEnabled) {
  180. if (this.bumpTexture && StandardMaterial.BumpTextureEnabled) {
  181. if (!this.bumpTexture.isReady()) {
  182. return false;
  183. } else {
  184. needUVs = true;
  185. this._defines.BUMP = true;
  186. }
  187. }
  188. if (StandardMaterial.ReflectionTextureEnabled) {
  189. this._defines.REFLECTION = true;
  190. }
  191. }
  192. // Effect
  193. if (scene.clipPlane) {
  194. this._defines.CLIPPLANE = true;
  195. }
  196. if (engine.getAlphaTesting()) {
  197. this._defines.ALPHATEST = true;
  198. }
  199. // Point size
  200. if (this.pointsCloud || scene.forcePointsCloud) {
  201. this._defines.POINTSIZE = true;
  202. }
  203. // Fog
  204. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  205. this._defines.FOG = true;
  206. }
  207. var lightIndex = 0;
  208. if (scene.lightsEnabled && !this.disableLighting) {
  209. for (var index = 0; index < scene.lights.length; index++) {
  210. var light = scene.lights[index];
  211. if (!light.isEnabled()) {
  212. continue;
  213. }
  214. // Excluded check
  215. if (light._excludedMeshesIds.length > 0) {
  216. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  217. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  218. if (excludedMesh) {
  219. light.excludedMeshes.push(excludedMesh);
  220. }
  221. }
  222. light._excludedMeshesIds = [];
  223. }
  224. // Included check
  225. if (light._includedOnlyMeshesIds.length > 0) {
  226. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  227. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  228. if (includedOnlyMesh) {
  229. light.includedOnlyMeshes.push(includedOnlyMesh);
  230. }
  231. }
  232. light._includedOnlyMeshesIds = [];
  233. }
  234. if (!light.canAffectMesh(mesh)) {
  235. continue;
  236. }
  237. needNormals = true;
  238. this._defines["LIGHT" + lightIndex] = true;
  239. var type;
  240. if (light instanceof SpotLight) {
  241. type = "SPOTLIGHT" + lightIndex;
  242. } else if (light instanceof HemisphericLight) {
  243. type = "HEMILIGHT" + lightIndex;
  244. } else if (light instanceof PointLight) {
  245. type = "POINTLIGHT" + lightIndex;
  246. } else {
  247. type = "DIRLIGHT" + lightIndex;
  248. }
  249. this._defines[type] = true;
  250. // Specular
  251. if (!light.specular.equalsFloats(0, 0, 0)) {
  252. this._defines.SPECULARTERM = true;
  253. }
  254. // Shadows
  255. if (scene.shadowsEnabled) {
  256. var shadowGenerator = light.getShadowGenerator();
  257. if (mesh && mesh.receiveShadows && shadowGenerator) {
  258. this._defines["SHADOW" + lightIndex] = true;
  259. this._defines.SHADOWS = true;
  260. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  261. this._defines["SHADOWVSM" + lightIndex] = true;
  262. }
  263. if (shadowGenerator.usePoissonSampling) {
  264. this._defines["SHADOWPCF" + lightIndex] = true;
  265. }
  266. }
  267. }
  268. lightIndex++;
  269. if (lightIndex === maxSimultaneousLights)
  270. break;
  271. }
  272. }
  273. // Attribs
  274. if (mesh) {
  275. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  276. this._defines.NORMAL = true;
  277. }
  278. if (needUVs) {
  279. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  280. this._defines.UV1 = true;
  281. }
  282. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  283. this._defines.UV2 = true;
  284. }
  285. }
  286. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  287. this._defines.VERTEXCOLOR = true;
  288. if (mesh.hasVertexAlpha) {
  289. this._defines.VERTEXALPHA = true;
  290. }
  291. }
  292. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  293. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  294. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  295. }
  296. // Instances
  297. if (useInstances) {
  298. this._defines.INSTANCES = true;
  299. }
  300. }
  301. this._mesh = mesh;
  302. // Get correct effect
  303. if (!this._defines.isEqual(this._cachedDefines)) {
  304. this._defines.cloneTo(this._cachedDefines);
  305. scene.resetCachedMaterial();
  306. // Fallbacks
  307. var fallbacks = new EffectFallbacks();
  308. if (this._defines.FOG) {
  309. fallbacks.addFallback(1, "FOG");
  310. }
  311. for (lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  312. if (!this._defines["LIGHT" + lightIndex]) {
  313. continue;
  314. }
  315. if (lightIndex > 0) {
  316. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  317. }
  318. if (this._defines["SHADOW" + lightIndex]) {
  319. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  320. }
  321. if (this._defines["SHADOWPCF" + lightIndex]) {
  322. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  323. }
  324. if (this._defines["SHADOWVSM" + lightIndex]) {
  325. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  326. }
  327. }
  328. if (this._defines.NUM_BONE_INFLUENCERS > 0){
  329. fallbacks.addCPUSkinningFallback(0, mesh);
  330. }
  331. //Attributes
  332. var attribs = [VertexBuffer.PositionKind];
  333. if (this._defines.NORMAL) {
  334. attribs.push(VertexBuffer.NormalKind);
  335. }
  336. if (this._defines.UV1) {
  337. attribs.push(VertexBuffer.UVKind);
  338. }
  339. if (this._defines.UV2) {
  340. attribs.push(VertexBuffer.UV2Kind);
  341. }
  342. if (this._defines.VERTEXCOLOR) {
  343. attribs.push(VertexBuffer.ColorKind);
  344. }
  345. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  346. attribs.push(VertexBuffer.MatricesIndicesKind);
  347. attribs.push(VertexBuffer.MatricesWeightsKind);
  348. if (this._defines.NUM_BONE_INFLUENCERS > 4) {
  349. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  350. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  351. }
  352. }
  353. if (this._defines.INSTANCES) {
  354. attribs.push("world0");
  355. attribs.push("world1");
  356. attribs.push("world2");
  357. attribs.push("world3");
  358. }
  359. // Legacy browser patch
  360. var shaderName = "water";
  361. var join = this._defines.toString();
  362. this._effect = scene.getEngine().createEffect(shaderName,
  363. attribs,
  364. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor", "vSpecularColor",
  365. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  366. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  367. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  368. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  369. "vFogInfos", "vFogColor", "pointSize",
  370. "vNormalInfos",
  371. "mBones",
  372. "vClipPlane", "normalMatrix",
  373. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3",
  374. // Water
  375. "worldReflectionViewProjection", "windDirection", "waveLength", "time", "windForce",
  376. "cameraPosition", "bumpHeight", "waveHeight", "waterColor", "colorBlendFactor", "waveSpeed"
  377. ],
  378. ["normalSampler",
  379. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3",
  380. // Water
  381. "refractionSampler", "reflectionSampler"
  382. ],
  383. join, fallbacks, this.onCompiled, this.onError);
  384. }
  385. if (!this._effect.isReady()) {
  386. return false;
  387. }
  388. this._renderId = scene.getRenderId();
  389. this._wasPreviouslyReady = true;
  390. if (mesh) {
  391. if (!mesh._materialDefines) {
  392. mesh._materialDefines = new WaterMaterialDefines();
  393. }
  394. this._defines.cloneTo(mesh._materialDefines);
  395. }
  396. return true;
  397. }
  398. public bindOnlyWorldMatrix(world: Matrix): void {
  399. this._effect.setMatrix("world", world);
  400. }
  401. public bind(world: Matrix, mesh?: Mesh): void {
  402. var scene = this.getScene();
  403. // Matrices
  404. this.bindOnlyWorldMatrix(world);
  405. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  406. // Bones
  407. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  408. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  409. }
  410. if (scene.getCachedMaterial() !== this) {
  411. // Textures
  412. if (this.bumpTexture && StandardMaterial.BumpTextureEnabled) {
  413. this._effect.setTexture("normalSampler", this.bumpTexture);
  414. this._effect.setFloat2("vNormalInfos", this.bumpTexture.coordinatesIndex, this.bumpTexture.level);
  415. this._effect.setMatrix("normalMatrix", this.bumpTexture.getTextureMatrix());
  416. }
  417. // Clip plane
  418. if (scene.clipPlane) {
  419. var clipPlane = scene.clipPlane;
  420. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  421. }
  422. // Point size
  423. if (this.pointsCloud) {
  424. this._effect.setFloat("pointSize", this.pointSize);
  425. }
  426. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  427. }
  428. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  429. if (this._defines.SPECULARTERM) {
  430. this._effect.setColor4("vSpecularColor", this.specularColor, this.specularPower);
  431. }
  432. if (scene.lightsEnabled && !this.disableLighting) {
  433. var lightIndex = 0;
  434. for (var index = 0; index < scene.lights.length; index++) {
  435. var light = scene.lights[index];
  436. if (!light.isEnabled()) {
  437. continue;
  438. }
  439. if (!light.canAffectMesh(mesh)) {
  440. continue;
  441. }
  442. if (light instanceof PointLight) {
  443. // Point Light
  444. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  445. } else if (light instanceof DirectionalLight) {
  446. // Directional Light
  447. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  448. } else if (light instanceof SpotLight) {
  449. // Spot Light
  450. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  451. } else if (light instanceof HemisphericLight) {
  452. // Hemispheric Light
  453. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  454. }
  455. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  456. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  457. if (this._defines.SPECULARTERM) {
  458. light.specular.scaleToRef(light.intensity, this._scaledSpecular);
  459. this._effect.setColor3("vLightSpecular" + lightIndex, this._scaledSpecular);
  460. }
  461. // Shadows
  462. if (scene.shadowsEnabled) {
  463. var shadowGenerator = light.getShadowGenerator();
  464. if (mesh.receiveShadows && shadowGenerator) {
  465. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  466. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  467. this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  468. }
  469. }
  470. lightIndex++;
  471. if (lightIndex === maxSimultaneousLights)
  472. break;
  473. }
  474. }
  475. // View
  476. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  477. this._effect.setMatrix("view", scene.getViewMatrix());
  478. }
  479. // Fog
  480. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  481. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  482. this._effect.setColor3("vFogColor", scene.fogColor);
  483. }
  484. // Water
  485. if (StandardMaterial.ReflectionTextureEnabled) {
  486. this._effect.setTexture("refractionSampler", this._refractionRTT);
  487. this._effect.setTexture("reflectionSampler", this._reflectionRTT);
  488. }
  489. var wrvp = this._mesh.getWorldMatrix().multiply(this._reflectionTransform).multiply(scene.getProjectionMatrix());
  490. this._lastTime += scene.getEngine().getDeltaTime();
  491. this._effect.setMatrix("worldReflectionViewProjection", wrvp);
  492. this._effect.setVector2("windDirection", this.windDirection);
  493. this._effect.setFloat("waveLength", this.waveLength);
  494. this._effect.setFloat("time", this._lastTime / 100000);
  495. this._effect.setFloat("windForce", this.windForce);
  496. this._effect.setFloat("waveHeight", this.waveHeight);
  497. this._effect.setFloat("bumpHeight", this.bumpHeight);
  498. this._effect.setColor4("waterColor", this.waterColor, 1.0);
  499. this._effect.setFloat("colorBlendFactor", this.colorBlendFactor);
  500. this._effect.setFloat("waveSpeed", this.waveSpeed);
  501. super.bind(world, mesh);
  502. }
  503. private _createRenderTargets(scene: Scene, renderTargetSize: Vector2): void {
  504. // Render targets
  505. this._refractionRTT = new RenderTargetTexture(name + "_refraction", {width: renderTargetSize.x, height: renderTargetSize.y}, scene, false, true);
  506. this._reflectionRTT = new RenderTargetTexture(name + "_reflection", {width: renderTargetSize.x, height: renderTargetSize.y}, scene, false, true);
  507. scene.customRenderTargets.push(this._refractionRTT);
  508. scene.customRenderTargets.push(this._reflectionRTT);
  509. var isVisible: boolean;
  510. var clipPlane = null;
  511. var savedViewMatrix;
  512. var mirrorMatrix = Matrix.Zero();
  513. this._refractionRTT.onBeforeRender = () => {
  514. if (this._mesh) {
  515. isVisible = this._mesh.isVisible;
  516. this._mesh.isVisible = false;
  517. }
  518. // Clip plane
  519. clipPlane = scene.clipPlane;
  520. var positiony = this._mesh ? this._mesh.position.y : 0.0;
  521. scene.clipPlane = Plane.FromPositionAndNormal(new Vector3(0, positiony, 0), new Vector3(0, 1, 0));
  522. };
  523. this._refractionRTT.onAfterRender = () => {
  524. if (this._mesh) {
  525. this._mesh.isVisible = isVisible;
  526. }
  527. // Clip plane
  528. scene.clipPlane = clipPlane;
  529. };
  530. this._reflectionRTT.onBeforeRender = () => {
  531. if (this._mesh) {
  532. isVisible = this._mesh.isVisible;
  533. this._mesh.isVisible = false;
  534. }
  535. // Clip plane
  536. clipPlane = scene.clipPlane;
  537. var positiony = this._mesh ? this._mesh.position.y : 0.0;
  538. scene.clipPlane = Plane.FromPositionAndNormal(new Vector3(0, positiony, 0), new Vector3(0, -1, 0));
  539. // Transform
  540. Matrix.ReflectionToRef(scene.clipPlane, mirrorMatrix);
  541. savedViewMatrix = scene.getViewMatrix();
  542. mirrorMatrix.multiplyToRef(savedViewMatrix, this._reflectionTransform);
  543. scene.setTransformMatrix(this._reflectionTransform, scene.getProjectionMatrix());
  544. scene.getEngine().cullBackFaces = false;
  545. scene._mirroredCameraPosition = Vector3.TransformCoordinates(scene.activeCamera.position, mirrorMatrix);
  546. };
  547. this._reflectionRTT.onAfterRender = () => {
  548. if (this._mesh) {
  549. this._mesh.isVisible = isVisible;
  550. }
  551. // Clip plane
  552. scene.clipPlane = clipPlane;
  553. // Transform
  554. scene.setTransformMatrix(savedViewMatrix, scene.getProjectionMatrix());
  555. scene.getEngine().cullBackFaces = true;
  556. scene._mirroredCameraPosition = null;
  557. };
  558. }
  559. public getAnimatables(): IAnimatable[] {
  560. var results = [];
  561. if (this.bumpTexture && this.bumpTexture.animations && this.bumpTexture.animations.length > 0) {
  562. results.push(this.bumpTexture);
  563. }
  564. if (this._reflectionRTT && this._reflectionRTT.animations && this._reflectionRTT.animations.length > 0) {
  565. results.push(this._reflectionRTT);
  566. }
  567. if (this._refractionRTT && this._refractionRTT.animations && this._refractionRTT.animations.length > 0) {
  568. results.push(this._refractionRTT);
  569. }
  570. return results;
  571. }
  572. public dispose(forceDisposeEffect?: boolean): void {
  573. if (this.bumpTexture) {
  574. this.bumpTexture.dispose();
  575. }
  576. if (this._reflectionRTT) {
  577. this._reflectionRTT.dispose();
  578. }
  579. if (this._refractionRTT) {
  580. this._refractionRTT.dispose();
  581. }
  582. super.dispose(forceDisposeEffect);
  583. }
  584. public clone(name: string): WaterMaterial {
  585. var newMaterial = new WaterMaterial(name, this.getScene());
  586. // Base material
  587. this.copyTo(newMaterial);
  588. // water material
  589. if (this.bumpTexture && this.bumpTexture.clone) {
  590. newMaterial.bumpTexture = this.bumpTexture.clone();
  591. }
  592. newMaterial.diffuseColor = this.diffuseColor.clone();
  593. return newMaterial;
  594. }
  595. public static CreateDefaultMesh(name: string, scene: Scene): Mesh {
  596. var mesh = Mesh.CreateGround(name, 512, 512, 32, scene, false);
  597. return mesh;
  598. }
  599. }
  600. }