SetCannonMaterial.re.ts 863 B

12345678910111213141516171819202122232425262728293031323334
  1. import * as RE from 'rogue-engine';
  2. import * as CANNON from 'cannon-es';
  3. import CannonBody from '../CannonBody.re';
  4. import * as RogueCannon from '../../Lib/RogueCannon';
  5. export default class SetCannonMaterial extends RE.Component {
  6. material: CANNON.Material;
  7. @RE.Prop("String") materialName: string;
  8. start() {
  9. this.setMaterial();
  10. }
  11. private getMaterial() {
  12. return RogueCannon.getWorld().materials.find(material => material.name === this.materialName)
  13. }
  14. private setMaterial() {
  15. const material = this.getMaterial();
  16. if (!material) return;
  17. this.material = material;
  18. const cannonBody = RE.getComponent(CannonBody, this.object3d);
  19. if (cannonBody) {
  20. cannonBody.body.shapes.forEach(shape => (shape.material = this.material));
  21. }
  22. }
  23. }
  24. RE.registerComponent(SetCannonMaterial);