CannonTrimesh.re.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import * as RE from 'rogue-engine';
  2. import * as THREE from 'three';
  3. import * as CANNON from 'cannon-es';
  4. import CannonShape from './CannonShape';
  5. export default class CannonTrimesh extends CannonShape {
  6. shape: CANNON.Trimesh;
  7. @RE.Prop("Vector3") sizeOffset: THREE.Vector3 = new THREE.Vector3(1, 1, 1);
  8. worldScale = new THREE.Vector3();
  9. worldPos = new THREE.Vector3();
  10. tmpVec0 = new CANNON.Vec3();
  11. tmpVec1 = new CANNON.Vec3();
  12. tmpVec2 = new CANNON.Vec3();
  13. tmpQuat0 = new CANNON.Vec3();
  14. createShape() {
  15. if (!(this.object3d instanceof THREE.Mesh)) return;
  16. this.object3d.updateWorldMatrix(true, true);
  17. this.object3d.getWorldScale(this.worldScale);
  18. this.object3d.getWorldPosition(this.worldPos);
  19. this.object3d.getWorldQuaternion(this.worldQuaternion);
  20. const mesh = this.object3d;
  21. let geometry = (mesh.geometry as THREE.BufferGeometry);
  22. geometry.computeBoundingSphere();
  23. geometry.normalizeNormals();
  24. if (geometry.index !== null) {
  25. const nonIndexedGeo = geometry.toNonIndexed();
  26. geometry.copy(nonIndexedGeo);
  27. }
  28. const vertices = this.getVertices(geometry);
  29. if (!vertices.length) return;
  30. const indices = Object.keys(vertices).map(Number);
  31. this.shape = new CANNON.Trimesh(vertices as unknown as number[], indices);
  32. }
  33. getVertices (geometry: THREE.BufferGeometry) {
  34. const position = geometry.attributes.position;
  35. const vertices = new Float32Array(position.count * 3);
  36. for (let i = 0; i < position.count; i++) {
  37. vertices[i * 3] = position.getX(i) * this.worldScale.x;
  38. vertices[i * 3 + 1] = position.getY(i) * this.worldScale.y;
  39. vertices[i * 3 + 2] = position.getZ(i) * this.worldScale.z;
  40. }
  41. return vertices;
  42. }
  43. }
  44. RE.registerComponent(CannonTrimesh);