groundMaterial.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. var WORLDMONGER = WORLDMONGER || {};
  2. (function () {
  3. WORLDMONGER.GroundMaterial = function (name, scene, light) {
  4. this.name = name;
  5. this.id = name;
  6. this.light = light;
  7. this._scene = scene;
  8. scene.materials.push(this);
  9. this.groundTexture = new BABYLON.Texture("Shaders/Ground/ground.jpg", scene);
  10. this.groundTexture.uScale = 6.0;
  11. this.groundTexture.vScale = 6.0;
  12. this.grassTexture = new BABYLON.Texture("Shaders/Ground/grass.jpg", scene);
  13. this.grassTexture.uScale = 6.0;
  14. this.grassTexture.vScale = 6.0;
  15. this.snowTexture = new BABYLON.Texture("Shaders/Ground/snow.jpg", scene);
  16. this.snowTexture.uScale = 20.0;
  17. this.snowTexture.vScale = 20.0;
  18. this.sandTexture = new BABYLON.Texture("Shaders/Ground/sand.jpg", scene);
  19. this.sandTexture.uScale = 4.0;
  20. this.sandTexture.vScale = 4.0;
  21. this.rockTexture = new BABYLON.Texture("Shaders/Ground/rock.jpg", scene);
  22. this.rockTexture.uScale = 15.0;
  23. this.rockTexture.vScale = 15.0;
  24. this.blendTexture = new BABYLON.Texture("Shaders/Ground/blend.png", scene);
  25. this.blendTexture.uOffset = Math.random();
  26. this.blendTexture.vOffset = Math.random();
  27. this.blendTexture.wrapU = BABYLON.Texture.MIRROR_ADDRESSMODE;
  28. this.blendTexture.wrapV = BABYLON.Texture.MIRROR_ADDRESSMODE;
  29. this.sandLimit = 1;
  30. this.rockLimit = 5;
  31. this.snowLimit = 8;
  32. };
  33. WORLDMONGER.GroundMaterial.prototype = Object.create(BABYLON.Material.prototype);
  34. // Properties
  35. WORLDMONGER.GroundMaterial.prototype.needAlphaBlending = function () {
  36. return false;
  37. };
  38. WORLDMONGER.GroundMaterial.prototype.needAlphaTesting = function () {
  39. return false;
  40. };
  41. // Methods
  42. WORLDMONGER.GroundMaterial.prototype.isReady = function (mesh) {
  43. var engine = this._scene.getEngine();
  44. if (!this.groundTexture.isReady)
  45. return false;
  46. if (!this.snowTexture.isReady)
  47. return false;
  48. if (!this.sandTexture.isReady)
  49. return false;
  50. if (!this.rockTexture.isReady)
  51. return false;
  52. if (!this.grassTexture.isReady)
  53. return false;
  54. var defines = [];
  55. if (BABYLON.clipPlane) {
  56. defines.push("#define CLIPPLANE");
  57. }
  58. var join = defines.join("\n");
  59. if (this._cachedDefines != join) {
  60. this._cachedDefines = join;
  61. this._effect = engine.createEffect("./Shaders/Ground/ground",
  62. ["position", "normal", "uv"],
  63. ["worldViewProjection", "groundMatrix", "sandMatrix", "rockMatrix", "snowMatrix", "grassMatrix", "blendMatrix", "world", "vLightPosition", "vEyePosition", "vLimits", "vClipPlane"],
  64. ["groundSampler", "sandSampler", "rockSampler", "snowSampler", "grassSampler", "blendSampler"],
  65. join);
  66. }
  67. if (!this._effect.isReady()) {
  68. return false;
  69. }
  70. return true;
  71. };
  72. WORLDMONGER.GroundMaterial.prototype.bind = function (world, mesh) {
  73. this._effect.setMatrix("world", world);
  74. this._effect.setMatrix("worldViewProjection", world.multiply(this._scene.getTransformMatrix()));
  75. this._effect.setVector3("vEyePosition", this._scene.activeCamera.position);
  76. this._effect.setVector3("vLightPosition", this.light.position);
  77. // Textures
  78. if (this.groundTexture) {
  79. this._effect.setTexture("groundSampler", this.groundTexture);
  80. this._effect.setMatrix("groundMatrix", this.groundTexture._computeTextureMatrix());
  81. }
  82. if (this.sandTexture) {
  83. this._effect.setTexture("sandSampler", this.sandTexture);
  84. this._effect.setMatrix("sandMatrix", this.sandTexture._computeTextureMatrix());
  85. }
  86. if (this.rockTexture) {
  87. this._effect.setTexture("rockSampler", this.rockTexture);
  88. this._effect.setMatrix("rockMatrix", this.rockTexture._computeTextureMatrix());
  89. }
  90. if (this.snowTexture) {
  91. this._effect.setTexture("snowSampler", this.snowTexture);
  92. this._effect.setMatrix("snowMatrix", this.snowTexture._computeTextureMatrix());
  93. }
  94. if (this.grassTexture) {
  95. this._effect.setTexture("grassSampler", this.grassTexture);
  96. this._effect.setMatrix("grassMatrix", this.grassTexture._computeTextureMatrix());
  97. }
  98. if (this.blendTexture) {
  99. this._effect.setTexture("blendSampler", this.blendTexture);
  100. this._effect.setMatrix("blendMatrix", this.blendTexture._computeTextureMatrix());
  101. }
  102. this._effect.setFloat3("vLimits", this.sandLimit, this.rockLimit, this.snowLimit);
  103. if (BABYLON.clipPlane) {
  104. this._effect.setFloat4("vClipPlane", BABYLON.clipPlane.normal.x, BABYLON.clipPlane.normal.y, BABYLON.clipPlane.normal.z, BABYLON.clipPlane.d);
  105. }
  106. };
  107. WORLDMONGER.GroundMaterial.prototype.dispose = function () {
  108. if (this.grassTexture) {
  109. this.grassTexture.dispose();
  110. }
  111. if (this.groundTexture) {
  112. this.groundTexture.dispose();
  113. }
  114. if (this.snowTexture) {
  115. this.snowTexture.dispose();
  116. }
  117. if (this.sandTexture) {
  118. this.sandTexture.dispose();
  119. }
  120. if (this.rockTexture) {
  121. this.rockTexture.dispose();
  122. }
  123. this.baseDispose();
  124. };
  125. })();