CannonBodyWireframe.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import * as RE from 'rogue-engine';
  2. import * as THREE from 'three';
  3. import CannonBox from '../Shapes/CannonBox.re';
  4. import CannonSphere from '../Shapes/CannonSphere.re';
  5. import CannonShape from '../Shapes/CannonShape';
  6. import CannonCylinder from '../Shapes/CannonCylinder.re';
  7. import CannonTrimesh from '../Shapes/CannonTrimesh.re';
  8. export default class CannonBodyWireframe extends RE.Component {
  9. static isEditorComponent = true;
  10. selectedObjects: THREE.Object3D[] = [];
  11. colliders: THREE.Object3D[] = [];
  12. wireframeMaterial = new THREE.MeshStandardMaterial({wireframe: true, emissive: new THREE.Color("#00FF00"), color: new THREE.Color("#000000")});
  13. private handleOnComponentAdded = {stop: () => {}};
  14. private handleOnComponentRemoved = {stop: () => {}};
  15. private handleOnPlay = {stop: () => {}};
  16. private resetHandler = (component: RE.Component) => {
  17. component instanceof CannonShape && this.setupImpostors();
  18. }
  19. start() {
  20. this.handleOnComponentAdded.stop();
  21. this.handleOnComponentRemoved.stop();
  22. this.handleOnPlay.stop();
  23. this.handleOnComponentAdded = RE.onComponentAdded(this.resetHandler);
  24. this.handleOnComponentRemoved = RE.onComponentRemoved(this.resetHandler);
  25. this.handleOnPlay = RE.Runtime.onPlay(() => {
  26. this.handleOnComponentAdded.stop();
  27. this.handleOnComponentRemoved.stop();
  28. this.cleanupImpostors();
  29. });
  30. }
  31. afterUpdate() {
  32. const selectedObjects = window["rogue-editor"].Project.selectedObjects as THREE.Object3D[];
  33. if (!this.arraysAreEqual(selectedObjects, this.selectedObjects)) {
  34. this.selectedObjects = selectedObjects.slice(0);
  35. this.setupImpostors();
  36. }
  37. if (this.selectedObjects.length === 0) return;
  38. this.updateImpostors();
  39. }
  40. private updateImpostors() {
  41. this.colliders.forEach(impostor => {
  42. this.updateColliderMesh(impostor.userData.cannonShape, impostor as THREE.Mesh);
  43. });
  44. }
  45. private cleanupImpostors() {
  46. this.colliders.forEach(impostor => {
  47. impostor.userData.cannonShape = null;
  48. RE.App.currentScene.remove(impostor);
  49. RE.dispose(impostor);
  50. });
  51. this.colliders = [];
  52. }
  53. private setupImpostors() {
  54. this.cleanupImpostors();
  55. this.selectedObjects.forEach(selected => {
  56. selected.traverse(object => {
  57. const objComponents = RE.components[object.uuid];
  58. if (!objComponents) return;
  59. objComponents.forEach(component => {
  60. if (!(component instanceof CannonShape)) return;
  61. let impostor = RE.App.currentScene.getObjectByName("EDITOR_OBJECT_BB_" + object.uuid);
  62. if (impostor) return;
  63. impostor = this.getColliderMesh(component);
  64. if (impostor) {
  65. impostor.name = "EDITOR_OBJECT_BB_" + object.uuid;
  66. impostor.userData.isEditorObject = true;
  67. RE.App.currentScene.add(impostor);
  68. } else {
  69. return;
  70. }
  71. impostor.userData.cannonShape = component;
  72. this.colliders.push(impostor);
  73. });
  74. });
  75. });
  76. }
  77. private arraysAreEqual(array1: any[], array2: any[]) {
  78. if (array1.length !== array2.length) return false;
  79. return array1.every((element, i) => {
  80. return array2[i] === element;
  81. });
  82. }
  83. private getColliderMesh(component: CannonShape): THREE.Mesh | undefined {
  84. if (component instanceof CannonBox) {
  85. return new THREE.Mesh(
  86. new THREE.BoxBufferGeometry(),
  87. this.wireframeMaterial,
  88. );
  89. }
  90. if (component instanceof CannonCylinder) {
  91. const radiusTop = component.radiusTopOffset * component.object3d.scale.x;
  92. const radiusBottom = component.radiusBottomOffset * component.object3d.scale.x;
  93. const height = component.heightOffset * component.object3d.scale.y;
  94. return new THREE.Mesh(
  95. new THREE.CylinderBufferGeometry(radiusTop, radiusBottom, height, component.segments),
  96. this.wireframeMaterial,
  97. );
  98. }
  99. if (component instanceof CannonSphere) {
  100. const scale = component.object3d.scale;
  101. const maxSide = Math.max(scale.x, scale.y, scale.z);
  102. const radius = component.radiusOffset * (maxSide);
  103. const compensatedRadius = radius + (radius * 0.01);
  104. const segments = 15;
  105. return new THREE.Mesh(
  106. new THREE.SphereBufferGeometry(compensatedRadius, segments, segments),
  107. this.wireframeMaterial,
  108. );
  109. }
  110. if (component instanceof CannonTrimesh) {
  111. if (!component.shape) component.createShape();
  112. if (component.shape) {
  113. const geometry = new THREE.BufferGeometry();
  114. const mesh = new THREE.Mesh(geometry, this.wireframeMaterial);
  115. const points: THREE.Vector3[] = [];
  116. for (let i = 0; i < component.shape.vertices.length; i+=3) {
  117. const point = new THREE.Vector3(
  118. component.shape.vertices[i],
  119. component.shape.vertices[i + 1],
  120. component.shape.vertices[i + 2]
  121. );
  122. points.push(point);
  123. }
  124. geometry.setFromPoints(points);
  125. return mesh;
  126. }
  127. }
  128. return;
  129. }
  130. private updateColliderMesh(component: CannonShape, mesh: THREE.Mesh) {
  131. if (component instanceof CannonBox) {
  132. component.object3d.getWorldScale(mesh.scale);
  133. mesh.scale.set(
  134. component.sizeOffset.x * (mesh.scale.x),
  135. component.sizeOffset.y * (mesh.scale.y),
  136. component.sizeOffset.z * (mesh.scale.z)
  137. );
  138. }
  139. if (component instanceof CannonCylinder) {
  140. component.object3d.getWorldScale(mesh.scale);
  141. const radiusTop = component.radiusTopOffset * mesh.scale.x
  142. const radiusBottom = component.radiusBottomOffset * mesh.scale.x;
  143. const height = component.heightOffset * mesh.scale.y;
  144. if (mesh.geometry instanceof THREE.CylinderBufferGeometry) {
  145. if (
  146. mesh.geometry.parameters.radiusTop !== radiusTop ||
  147. mesh.geometry.parameters.radiusBottom !== radiusBottom ||
  148. mesh.geometry.parameters.height !== height ||
  149. mesh.geometry.parameters.radialSegments !== component.segments
  150. ) {
  151. mesh.geometry.dispose();
  152. mesh.geometry = new THREE.CylinderBufferGeometry(radiusTop, radiusBottom, height, component.segments);
  153. }
  154. }
  155. }
  156. if (component instanceof CannonSphere) {
  157. const scale = component.object3d.scale;
  158. const maxSide = Math.max(scale.x, scale.y, scale.z);
  159. const radius = component.radiusOffset * (maxSide);
  160. if (mesh.geometry instanceof THREE.SphereBufferGeometry) {
  161. if (mesh.geometry.parameters.radius !== radius) {
  162. let segments = 10 * radius;
  163. if (segments < 15) segments = 15;
  164. if (segments > 50) segments = 50;
  165. mesh.geometry.dispose();
  166. mesh.geometry = new THREE.SphereBufferGeometry(radius, segments, segments);
  167. }
  168. }
  169. }
  170. component.object3d.getWorldPosition(mesh.position);
  171. component.object3d.getWorldQuaternion(mesh.quaternion);
  172. }
  173. onBeforeRemoved() {
  174. this.handleOnComponentAdded.stop();
  175. this.handleOnComponentRemoved.stop();
  176. this.handleOnPlay.stop();
  177. this.cleanupImpostors();
  178. }
  179. }
  180. RE.registerComponent(CannonBodyWireframe);