CannonBodyWireframe.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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;
  92. const radiusBottom = component.radiusBottomOffset;
  93. const height = component.heightOffset;
  94. const mesh = new THREE.Mesh(
  95. new THREE.CylinderBufferGeometry(radiusTop, radiusBottom, height, component.segments),
  96. this.wireframeMaterial,
  97. );
  98. return mesh;
  99. }
  100. if (component instanceof CannonSphere) {
  101. const scale = component.object3d.scale;
  102. const maxSide = Math.max(scale.x, scale.y, scale.z);
  103. const radius = component.radiusOffset * (maxSide);
  104. const compensatedRadius = radius + (radius * 0.01);
  105. const segments = 15;
  106. return new THREE.Mesh(
  107. new THREE.SphereBufferGeometry(compensatedRadius, segments, segments),
  108. this.wireframeMaterial,
  109. );
  110. }
  111. if (component instanceof CannonTrimesh) {
  112. if (!component.shape) component.createShape();
  113. if (component.shape) {
  114. const geometry = new THREE.BufferGeometry();
  115. const mesh = new THREE.Mesh(geometry, this.wireframeMaterial);
  116. const points: THREE.Vector3[] = [];
  117. for (let i = 0; i < component.shape.vertices.length; i+=3) {
  118. const point = new THREE.Vector3(
  119. component.shape.vertices[i],
  120. component.shape.vertices[i + 1],
  121. component.shape.vertices[i + 2]
  122. );
  123. points.push(point);
  124. }
  125. geometry.setFromPoints(points);
  126. return mesh;
  127. }
  128. }
  129. return;
  130. }
  131. private updateColliderMesh(component: CannonShape, mesh: THREE.Mesh) {
  132. if (component instanceof CannonBox) {
  133. component.object3d.getWorldScale(mesh.scale);
  134. mesh.scale.set(
  135. component.sizeOffset.x * (mesh.scale.x),
  136. component.sizeOffset.y * (mesh.scale.y),
  137. component.sizeOffset.z * (mesh.scale.z)
  138. );
  139. }
  140. if (component instanceof CannonCylinder) {
  141. const radiusTop = component.radiusTopOffset;
  142. const radiusBottom = component.radiusBottomOffset;
  143. const height = component.heightOffset;
  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. component.object3d.getWorldScale(mesh.scale);
  156. }
  157. if (component instanceof CannonSphere) {
  158. const scale = component.object3d.scale;
  159. const maxSide = Math.max(scale.x, scale.y, scale.z);
  160. const radius = component.radiusOffset * (maxSide);
  161. if (mesh.geometry instanceof THREE.SphereBufferGeometry) {
  162. if (mesh.geometry.parameters.radius !== radius) {
  163. let segments = 10 * radius;
  164. if (segments < 15) segments = 15;
  165. if (segments > 50) segments = 50;
  166. mesh.geometry.dispose();
  167. mesh.geometry = new THREE.SphereBufferGeometry(radius, segments, segments);
  168. }
  169. }
  170. }
  171. component.object3d.getWorldPosition(mesh.position);
  172. component.object3d.getWorldQuaternion(mesh.quaternion);
  173. // component.object3d.getWorldScale(mesh.scale);
  174. }
  175. onBeforeRemoved() {
  176. this.handleOnComponentAdded.stop();
  177. this.handleOnComponentRemoved.stop();
  178. this.handleOnPlay.stop();
  179. this.cleanupImpostors();
  180. }
  181. }
  182. RE.registerComponent(CannonBodyWireframe);