babylon.waterMaterial.ts 28 KB

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