Advertisement
thephox1982

test-babylon-water

Apr 2nd, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. (function () {
  2. WaterMaterial = function (name, scene, light) {
  3. this.name = name;
  4. this.id = name;
  5. this.light = light;
  6.  
  7. this._scene = scene;
  8. scene.materials.push(this);
  9.  
  10. this.bumpTexture = new BABYLON.Texture("water/waterbm.png", scene);
  11. this.bumpTexture.uScale = 2;
  12. this.bumpTexture.vScale = 2;
  13. this.bumpTexture.wrapU = BABYLON.Texture.MIRROR_ADDRESSMODE;
  14. this.bumpTexture.wrapV = BABYLON.Texture.MIRROR_ADDRESSMODE;
  15.  
  16. this.reflectionTexture = new BABYLON.MirrorTexture("reflection", 512, scene, true);
  17. this.refractionTexture = new BABYLON.RenderTargetTexture("refraction", 512, scene, true);
  18. this.reflectionTexture.mirrorPlane = new BABYLON.Plane(0, -1, 0, 0);
  19.  
  20. this.refractionTexture.onBeforeRender = function() {
  21. BABYLON.clipPlane = new BABYLON.Plane(0, 1, 0, 0);
  22. }
  23.  
  24. this.refractionTexture.onAfterRender = function () {
  25. BABYLON.clipPlane = null;
  26. }
  27.  
  28. this.waterColor = new BABYLON.Color3(0.0, 0.3, 0.1);
  29. this.waterColorLevel = 0.2;
  30. this.fresnelLevel = 1.0;
  31. this.reflectionLevel = 0.6;
  32. this.refractionLevel = 0.8;
  33.  
  34. this.waveLength = 0.1;
  35. this.waveHeight = 0.15;
  36.  
  37. this.waterDirection = new BABYLON.Vector2(0, 1.0);
  38.  
  39. this._time = 0;
  40. };
  41.  
  42. WaterMaterial.prototype = Object.create(BABYLON.Material.prototype);
  43.  
  44. // Properties
  45. WaterMaterial.prototype.needAlphaBlending = function () {
  46. return false;
  47. };
  48.  
  49. WaterMaterial.prototype.needAlphaTesting = function () {
  50. return false;
  51. };
  52.  
  53. // Methods
  54. WaterMaterial.prototype.getRenderTargetTextures = function () {
  55. var results = [];
  56.  
  57. results.push(this.reflectionTexture);
  58. results.push(this.refractionTexture);
  59.  
  60. return results;
  61. };
  62.  
  63. WaterMaterial.prototype.isReady = function (mesh) {
  64. var engine = this._scene.getEngine();
  65.  
  66. if (this.bumpTexture && !this.bumpTexture.isReady) {
  67. return false;
  68. }
  69.  
  70. this._effect = engine.createEffect("water/water",
  71. ["position", "normal", "uv"],
  72. ["worldViewProjection", "world", "view", "vLightPosition", "vEyePosition", "waterColor", "vLevels", "waveData", "windMatrix"],
  73. ["reflectionSampler", "refractionSampler", "bumpSampler"],
  74. "");
  75.  
  76. if (!this._effect.isReady()) {
  77. return false;
  78. }
  79.  
  80. return true;
  81. };
  82.  
  83. WaterMaterial.prototype.bind = function (world, mesh) {
  84. this._time += 0.0001 * this._scene.getAnimationRatio();
  85.  
  86. this._effect.setMatrix("world", world);
  87. this._effect.setMatrix("worldViewProjection", world.multiply(this._scene.getTransformMatrix()));
  88. this._effect.setVector3("vEyePosition", this._scene.activeCamera.position);
  89. this._effect.setVector3("vLightPosition", this.light.position);
  90. this._effect.setColor3("waterColor", this.waterColor);
  91. this._effect.setFloat4("vLevels", this.waterColorLevel, this.fresnelLevel, this.reflectionLevel, this.refractionLevel);
  92. this._effect.setFloat2("waveData", this.waveLength, this.waveHeight);
  93.  
  94. // Textures
  95. this._effect.setMatrix("windMatrix", this.bumpTexture._computeTextureMatrix().multiply(BABYLON.Matrix.Translation(this.waterDirection.x * this._time, this.waterDirection.y * this._time, 0)));
  96. this._effect.setTexture("bumpSampler", this.bumpTexture);
  97. this._effect.setTexture("reflectionSampler", this.reflectionTexture);
  98. this._effect.setTexture("refractionSampler", this.refractionTexture);
  99. };
  100.  
  101. WaterMaterial.prototype.dispose = function () {
  102. if (this.bumpTexture) {
  103. this.bumpTexture.dispose();
  104. }
  105.  
  106. if (this.groundTexture) {
  107. this.groundTexture.dispose();
  108. }
  109.  
  110. if (this.snowTexture) {
  111. this.snowTexture.dispose();
  112. }
  113. this.baseDispose();
  114. };
  115. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement