elevationControl.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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._groundVertices = this._ground.getVertices();
  96. this._groundIndices = this._ground.getIndices();
  97. this._groundPositions = [];
  98. var index;
  99. for (index = 0; index < this._groundVertices.length; index += this._ground.getFloatVertexStrideSize()) {
  100. this._groundPositions.push(new BABYLON.Vector3(this._groundVertices[index], this._groundVertices[index + 1], this._groundVertices[index + 2]));
  101. }
  102. this._groundFacesNormals = [];
  103. for (index = 0; index < this._ground.getTotalIndices() / 3; index++) {
  104. this._computeFaceNormal(index);
  105. }
  106. this._getFacesOfVertices();
  107. }
  108. };
  109. WORLDMONGER.ElevationControl.prototype._getFaceVerticesIndex = function (faceID) {
  110. return {
  111. v1: this._groundIndices[faceID * 3],
  112. v2: this._groundIndices[faceID * 3 + 1],
  113. v3: this._groundIndices[faceID * 3 + 2]
  114. };
  115. };
  116. WORLDMONGER.ElevationControl.prototype._computeFaceNormal = function (face) {
  117. var faceInfo = this._getFaceVerticesIndex(face);
  118. var v1v2 = this._groundPositions[faceInfo.v1].subtract(this._groundPositions[faceInfo.v2]);
  119. var v3v2 = this._groundPositions[faceInfo.v3].subtract(this._groundPositions[faceInfo.v2]);
  120. this._groundFacesNormals[face] = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(v1v2, v3v2));
  121. };
  122. WORLDMONGER.ElevationControl.prototype._getFacesOfVertices = function () {
  123. this._facesOfVertices = [];
  124. this._subdivisionsOfVertices = [];
  125. var index;
  126. for (index = 0; index < this._groundPositions.length; index++) {
  127. this._facesOfVertices[index] = [];
  128. this._subdivisionsOfVertices[index] = [];
  129. }
  130. for (index = 0; index < this._groundIndices.length; index++) {
  131. this._facesOfVertices[this._groundIndices[index]].push((index / 3) | 0);
  132. }
  133. for (var subIndex = 0; subIndex < this._ground.subMeshes.length; subIndex++) {
  134. var subMesh = this._ground.subMeshes[subIndex];
  135. for (index = subMesh.verticesStart; index < subMesh.verticesStart + subMesh.verticesCount; index++) {
  136. this._subdivisionsOfVertices[index].push(subMesh);
  137. }
  138. }
  139. };
  140. WORLDMONGER.ElevationControl.prototype._isBoxSphereIntersected = function(box, sphereCenter, sphereRadius) {
  141. var vector = BABYLON.Vector3.Clamp(sphereCenter, box.minimumWorld, box.maximumWorld);
  142. var num = BABYLON.Vector3.DistanceSquared(sphereCenter, vector);
  143. return (num <= (sphereRadius * sphereRadius));
  144. };
  145. WORLDMONGER.ElevationControl.prototype._elevateFaces = function (pickInfo, radius, height) {
  146. this._prepareDataModelForElevation();
  147. this._selectedVertices = [];
  148. // Impact zone
  149. var sphereCenter = pickInfo.pickedPoint;
  150. sphereCenter.y = 0;
  151. var index;
  152. // Determine list of vertices
  153. for (var subIndex = 0; subIndex < this._ground.subMeshes.length; subIndex++) {
  154. var subMesh = this._ground.subMeshes[subIndex];
  155. if (!this._isBoxSphereIntersected(subMesh.getBoundingInfo().boundingBox, sphereCenter, radius)) {
  156. continue;
  157. }
  158. for (index = subMesh.verticesStart; index < subMesh.verticesStart + subMesh.verticesCount; index++) {
  159. var position = this._groundPositions[index];
  160. sphereCenter.y = position.y;
  161. var distance = BABYLON.Vector3.Distance(position, sphereCenter);
  162. if (distance < radius) {
  163. this._selectedVertices[index] = distance;
  164. }
  165. }
  166. }
  167. // Elevate vertices
  168. var stride = this._ground.getFloatVertexStrideSize();
  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._groundVertices[selectedVertice * stride + 1] = position.y;
  183. this._updateSubdivisions(selectedVertice);
  184. }
  185. // Normals
  186. this._reComputeNormals()
  187. // Update vertex buffer
  188. this._ground.updateVertices(this._groundVertices);
  189. };
  190. WORLDMONGER.ElevationControl.prototype._reComputeNormals = function () {
  191. var faces = [];
  192. var face;
  193. for (selectedVertice in this._selectedVertices) {
  194. var faceOfVertices = this._facesOfVertices[selectedVertice];
  195. for (var index = 0; index < faceOfVertices.length; index++) {
  196. faces[faceOfVertices[index]] = true;
  197. }
  198. }
  199. for (face in faces) {
  200. this._computeFaceNormal(face);
  201. }
  202. for (face in faces) {
  203. var faceInfo = this._getFaceVerticesIndex(face);
  204. this._computeNormal(faceInfo.v1);
  205. this._computeNormal(faceInfo.v2);
  206. this._computeNormal(faceInfo.v3);
  207. }
  208. };
  209. WORLDMONGER.ElevationControl.prototype._computeNormal = function (vertexIndex) {
  210. var faces = this._facesOfVertices[vertexIndex];
  211. var normal = BABYLON.Vector3.Zero();
  212. for (var index = 0; index < faces.length; index++) {
  213. normal = normal.add(this._groundFacesNormals[faces[index]]);
  214. }
  215. normal = BABYLON.Vector3.Normalize(normal.scale(1.0 / faces.length));
  216. var stride = this._ground.getFloatVertexStrideSize();
  217. this._groundVertices[vertexIndex * stride + 3] = normal.x;
  218. this._groundVertices[vertexIndex * stride + 4] = normal.y;
  219. this._groundVertices[vertexIndex * stride + 5] = 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. })();