import * as pc from "playcanvas";
import { Debugger } from './toolkit/core.mjs';
import { GpuInstancingRender } from './toolkit/extension.mjs';
export class ViverseGpuInstancing extends pc.Script {
    static scriptName = 'viverseGpuInstancing';
    /**
     * The GLB assets as type of binary which contains gltf EXT_mesh_gpu_instancing extension.
     * NOTE: To update the GLB as binary asset, you need to rename the ".glb" extension to ".bin".
     * @attribute
     * @title Binary Asset
     * @type {pc.Asset}
     * @resource binary
     */
    binaryAsset;
    /**
     * Called when the script is about to run for the first time.
     */
    initialize() {
        if (!this.binaryAsset) {
            Debugger.error('[ViverseGpuInstancing] Binary asset is not set.');
            return;
        }
        const filename = this.binaryAsset.name;
        const assetUrl = this.binaryAsset.getFileUrl() ?? '';
        // @ts-expect-error - window.editor is only available in the editor
        const url = window.editor?.api ? this.getEditorUrl(assetUrl) : assetUrl;
        const gpuInstancingRender = new GpuInstancingRender(this.app, this.entity, filename, url);
        this.entity.on('destroy', () => {
            gpuInstancingRender.destroy();
        });
    }
    getEditorUrl(assetUrl) {
        // Hack: getFileUrl() contains extra "/api/" in Launch mode
        return assetUrl.startsWith('/api/') ? assetUrl.slice(5) : assetUrl;
    }
}
