elevationControl.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. var WORLDMONGER = WORLDMONGER || {};
  2. (function () {
  3. WORLDMONGER.ElevationControl = function (ground) {
  4. this._ground = ground;
  5. this.radius = 5.0;
  6. this._invertDirection = 1.0;
  7. this.heightMin = 0;
  8. this.heightMax = 11.0;
  9. // Particle system
  10. var scene = ground.getScene();
  11. var particleSystem = new BABYLON.ParticleSystem("particles", 4000, scene);
  12. particleSystem.particleTexture = new BABYLON.Texture("Assets/Flare.png", scene);
  13. particleSystem.minAngularSpeed = -4.5;
  14. particleSystem.maxAngularSpeed = 4.5;
  15. particleSystem.minSize = 0.5;
  16. particleSystem.maxSize = 4.0;
  17. particleSystem.minLifeTime = 0.5;
  18. particleSystem.maxLifeTime = 2.0;
  19. particleSystem.minEmitPower = 0.5;
  20. particleSystem.maxEmitPower = 1.0;
  21. particleSystem.emitRate = 400;
  22. particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
  23. particleSystem.minEmitBox = new BABYLON.Vector3(0, 0, 0);
  24. particleSystem.maxEmitBox = new BABYLON.Vector3(0, 0, 0);
  25. particleSystem.direction1 = new BABYLON.Vector3(0, 1, 0);
  26. particleSystem.direction2 = new BABYLON.Vector3(0, 1, 0);
  27. particleSystem.color1 = new BABYLON.Color4(0, 0, 1, 1);
  28. particleSystem.color2 = new BABYLON.Color4(1, 1, 1, 1);
  29. particleSystem.gravity = new BABYLON.Vector3(0, 5, 0);
  30. particleSystem.manualEmitCount = 0;
  31. particleSystem.emitter = new BABYLON.Vector3(0, 0, 0);
  32. particleSystem.start();
  33. this._particleSystem = particleSystem;
  34. };
  35. WORLDMONGER.ElevationControl.prototype.direction = 1;
  36. WORLDMONGER.ElevationControl.prototype.attachControl = function (canvas) {
  37. var currentPosition;
  38. var that = this;
  39. this._onBeforeRender = function () {
  40. if (!currentPosition) {
  41. return;
  42. }
  43. var pickInfo = that._ground.getScene().pick(currentPosition.x, currentPosition.y);
  44. if (!pickInfo.hit)
  45. return;
  46. if (pickInfo.pickedMesh != that._ground)
  47. return;
  48. that._particleSystem.emitter = pickInfo.pickedPoint.add(new BABYLON.Vector3(0, 3, 0));
  49. that._particleSystem.manualEmitCount += 400;
  50. that._elevateFaces(pickInfo, that.radius, 0.2);
  51. };
  52. this._onPointerDown = function (evt) {
  53. evt.preventDefault();
  54. currentPosition = {
  55. x: evt.clientX,
  56. y: evt.clientY
  57. };
  58. };
  59. this._onPointerUp = function (evt) {
  60. evt.preventDefault();
  61. currentPosition = null;
  62. };
  63. this._onPointerMove = function (evt) {
  64. evt.preventDefault();
  65. if (!currentPosition) {
  66. return;
  67. }
  68. that._invertDirection = evt.button == 2 ? -1 : 1;
  69. currentPosition = {
  70. x: evt.clientX,
  71. y: evt.clientY
  72. };
  73. };
  74. this._onLostFocus = function () {
  75. currentPosition = null;
  76. };
  77. canvas.addEventListener("pointerdown", this._onPointerDown, true);
  78. canvas.addEventListener("pointerup", this._onPointerUp, true);
  79. canvas.addEventListener("pointerout", this._onPointerUp, true);
  80. canvas.addEventListener("pointermove", this._onPointerMove, true);
  81. window.addEventListener("blur", this._onLostFocus, true);
  82. this._ground.getScene().registerBeforeRender(this._onBeforeRender);
  83. };
  84. WORLDMONGER.ElevationControl.prototype.detachControl = function (canvas) {
  85. canvas.removeEventListener("pointerdown", this._onPointerDown);
  86. canvas.removeEventListener("pointerup", this._onPointerUp);
  87. canvas.removeEventListener("pointerout", this._onPointerUp);
  88. canvas.removeEventListener("pointermove", this._onPointerMove);
  89. window.removeEventListener("blur", this._onLostFocus);
  90. this._ground.getScene().unregisterBeforeRender(this._onBeforeRender);
  91. };
  92. WORLDMONGER.ElevationControl.prototype._prepareDataModelForElevation = function () {
  93. if (this._facesOfVertices == null) {
  94. this._facesOfVertices = [];
  95. this._groundVerticesPositions = this._ground.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  96. this._groundVerticesNormals = this._ground.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  97. this._groundIndices = this._ground.getIndices();
  98. this._groundPositions = [];
  99. var index;
  100. for (index = 0; index < this._groundVerticesPositions.length; index += 3) {
  101. this._groundPositions.push(new BABYLON.Vector3(this._groundVerticesPositions[index], this._groundVerticesPositions[index + 1], this._groundVerticesPositions[index + 2]));
  102. }
  103. this._groundFacesNormals = [];
  104. for (index = 0; index < this._ground.getTotalIndices() / 3; index++) {
  105. this._computeFaceNormal(index);
  106. }
  107. this._getFacesOfVertices();
  108. }
  109. };
  110. WORLDMONGER.ElevationControl.prototype._getFaceVerticesIndex = function (faceID) {
  111. return {
  112. v1: this._groundIndices[faceID * 3],
  113. v2: this._groundIndices[faceID * 3 + 1],
  114. v3: this._groundIndices[faceID * 3 + 2]
  115. };
  116. };
  117. WORLDMONGER.ElevationControl.prototype._computeFaceNormal = function (face) {
  118. var faceInfo = this._getFaceVerticesIndex(face);
  119. var v1v2 = this._groundPositions[faceInfo.v1].subtract(this._groundPositions[faceInfo.v2]);
  120. var v3v2 = this._groundPositions[faceInfo.v3].subtract(this._groundPositions[faceInfo.v2]);
  121. this._groundFacesNormals[face] = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(v1v2, v3v2));
  122. };
  123. WORLDMONGER.ElevationControl.prototype._getFacesOfVertices = function () {
  124. this._facesOfVertices = [];
  125. this._subdivisionsOfVertices = [];
  126. var index;
  127. for (index = 0; index < this._groundPositions.length; index++) {
  128. this._facesOfVertices[index] = [];
  129. this._subdivisionsOfVertices[index] = [];
  130. }
  131. for (index = 0; index < this._groundIndices.length; index++) {
  132. this._facesOfVertices[this._groundIndices[index]].push((index / 3) | 0);
  133. }
  134. for (var subIndex = 0; subIndex < this._ground.subMeshes.length; subIndex++) {
  135. var subMesh = this._ground.subMeshes[subIndex];
  136. for (index = subMesh.verticesStart; index < subMesh.verticesStart + subMesh.verticesCount; index++) {
  137. this._subdivisionsOfVertices[index].push(subMesh);
  138. }
  139. }
  140. };
  141. WORLDMONGER.ElevationControl.prototype._isBoxSphereIntersected = function(box, sphereCenter, sphereRadius) {
  142. var vector = BABYLON.Vector3.Clamp(sphereCenter, box.minimumWorld, box.maximumWorld);
  143. var num = BABYLON.Vector3.DistanceSquared(sphereCenter, vector);
  144. return (num <= (sphereRadius * sphereRadius));
  145. };
  146. WORLDMONGER.ElevationControl.prototype._elevateFaces = function (pickInfo, radius, height) {
  147. this._prepareDataModelForElevation();
  148. this._selectedVertices = [];
  149. // Impact zone
  150. var sphereCenter = pickInfo.pickedPoint;
  151. sphereCenter.y = 0;
  152. var index;
  153. // Determine list of vertices
  154. for (var subIndex = 0; subIndex < this._ground.subMeshes.length; subIndex++) {
  155. var subMesh = this._ground.subMeshes[subIndex];
  156. if (!this._isBoxSphereIntersected(subMesh.getBoundingInfo().boundingBox, sphereCenter, radius)) {
  157. continue;
  158. }
  159. for (index = subMesh.verticesStart; index < subMesh.verticesStart + subMesh.verticesCount; index++) {
  160. var position = this._groundPositions[index];
  161. sphereCenter.y = position.y;
  162. var distance = BABYLON.Vector3.Distance(position, sphereCenter);
  163. if (distance < radius) {
  164. this._selectedVertices[index] = distance;
  165. }
  166. }
  167. }
  168. // Elevate vertices
  169. for (var selectedVertice in this._selectedVertices) {
  170. var position = this._groundPositions[selectedVertice];
  171. var distance = this._selectedVertices[selectedVertice];
  172. var fullHeight = height * this.direction * this._invertDirection;
  173. if (distance < radius * 0.3) {
  174. position.y += fullHeight;
  175. } else {
  176. position.y += fullHeight * (1.0 - (distance - radius * 0.3) / (radius * 0.7));
  177. }
  178. if (position.y > this.heightMax)
  179. position.y = this.heightMax;
  180. else if (position.y < this.heightMin)
  181. position.y = this.heightMin;
  182. this._groundVerticesPositions[selectedVertice * 3 + 1] = position.y;
  183. this._updateSubdivisions(selectedVertice);
  184. }
  185. // Normals
  186. this._reComputeNormals();
  187. // Update vertex buffer
  188. this._ground.updateVerticesData(BABYLON.VertexBuffer.PositionKind, this._groundVerticesPositions);
  189. this._ground.updateVerticesData(BABYLON.VertexBuffer.NormalKind,this._groundVerticesNormals);
  190. };
  191. WORLDMONGER.ElevationControl.prototype._reComputeNormals = function () {
  192. var faces = [];
  193. var face;
  194. for (var selectedVertice in this._selectedVertices) {
  195. var faceOfVertices = this._facesOfVertices[selectedVertice];
  196. for (var index = 0; index < faceOfVertices.length; index++) {
  197. faces[faceOfVertices[index]] = true;
  198. }
  199. }
  200. for (face in faces) {
  201. this._computeFaceNormal(face);
  202. }
  203. for (face in faces) {
  204. var faceInfo = this._getFaceVerticesIndex(face);
  205. this._computeNormal(faceInfo.v1);
  206. this._computeNormal(faceInfo.v2);
  207. this._computeNormal(faceInfo.v3);
  208. }
  209. };
  210. WORLDMONGER.ElevationControl.prototype._computeNormal = function(vertexIndex) {
  211. var faces = this._facesOfVertices[vertexIndex];
  212. var normal = BABYLON.Vector3.Zero();
  213. for (var index = 0; index < faces.length; index++) {
  214. normal = normal.add(this._groundFacesNormals[faces[index]]);
  215. }
  216. normal = BABYLON.Vector3.Normalize(normal.scale(1.0 / faces.length));
  217. this._groundVerticesNormals[vertexIndex * 3] = normal.x;
  218. this._groundVerticesNormals[vertexIndex * 3 + 1] = normal.y;
  219. this._groundVerticesNormals[vertexIndex * 3 + 2] = normal.z;
  220. };
  221. WORLDMONGER.ElevationControl.prototype._updateSubdivisions = function (vertexIndex) {
  222. for (var index = 0; index < this._subdivisionsOfVertices[vertexIndex].length; index++) {
  223. var sub = this._subdivisionsOfVertices[vertexIndex][index];
  224. var boundingBox = sub.getBoundingInfo().boundingBox;
  225. var boundingSphere = sub.getBoundingInfo().boundingSphere;
  226. if (this._groundPositions[vertexIndex].y < boundingBox.minimum.y) {
  227. boundingSphere.radius += Math.abs(this._groundPositions[vertexIndex].y - boundingBox.minimum.y);
  228. boundingBox.minimum.y = this._groundPositions[vertexIndex].y;
  229. } else if (this._groundPositions[vertexIndex].y > boundingBox.maximum.y) {
  230. boundingBox.maximum.y = this._groundPositions[vertexIndex].y;
  231. }
  232. }
  233. var boundingBox = this._ground.getBoundingInfo().boundingBox;
  234. var boundingSphere = this._ground.getBoundingInfo().boundingSphere;
  235. if (this._groundPositions[vertexIndex].y < boundingBox.minimum.y) {
  236. boundingSphere.Radius += Math.abs(this._groundPositions[vertexIndex].y - boundingBox.minimum.y);
  237. boundingBox.minimum.y = this._groundPositions[vertexIndex].y;
  238. } else if (this._groundPositions[vertexIndex].y > boundingBox.maximum.y) {
  239. boundingBox.maximum.y = this._groundPositions[vertexIndex].y;
  240. }
  241. };
  242. })();