import { Vector3, Script, MeshRenderer, StaticCollider, BoxColliderShape } from "oasis-engine"; import { PaladinAdapter } from "../adapter/PaladinAdapter"; export class GlTFCollider extends Script { private _tempVec30: Vector3 = new Vector3(); private _tempVec31: Vector3 = new Vector3(); onStart(): void { const renderers = this.entity.getComponentsIncludeChildren(MeshRenderer, []); for (let i = renderers.length - 1; i >= 0; i--) { this._addBoundingBox(renderers[i]); } } private _addBoundingBox(renderer: MeshRenderer): void { const { _tempVec30: localSize, _tempVec31: localPosition } = this; // Calculate the position and size of the collider. const boundingBox = renderer.mesh.bounds; const entity = renderer.entity; boundingBox.getCenter(localPosition); Vector3.subtract(boundingBox.max, boundingBox.min, localSize); // Add collider. const boxCollider = entity.addComponent(StaticCollider); const boxColliderShape = new BoxColliderShape(); boxColliderShape.setPosition(localPosition.x, localPosition.y, localPosition.z); boxColliderShape.setSize(localSize.x, localSize.y, localSize.z); boxCollider.addShape(boxColliderShape); // Add click script. entity.addComponent(Script).onPointerClick = () => { PaladinAdapter.toast("Click:" + entity.name); }; } }