babylon.waterMaterial.ts 30 KB

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