Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -80,7 +80,50 @@ export async function initializeViewer(config, instanceId) {
|
|
| 80 |
window.pc = pc;
|
| 81 |
}
|
| 82 |
|
| 83 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
const assets = {
|
| 85 |
model: new pc.Asset('gsplat', 'gsplat', { url: plyUrl }),
|
| 86 |
orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" }),
|
|
@@ -90,12 +133,9 @@ export async function initializeViewer(config, instanceId) {
|
|
| 90 |
{ type: pc.TEXTURETYPE_RGBP, mipmaps: false }
|
| 91 |
),
|
| 92 |
};
|
| 93 |
-
|
| 94 |
-
// Register all assets to app.assets so they are tracked and loaded
|
| 95 |
for (const key in assets) {
|
| 96 |
-
|
| 97 |
-
app.assets.add(assets[key]);
|
| 98 |
-
}
|
| 99 |
}
|
| 100 |
|
| 101 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
|
@@ -103,7 +143,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 103 |
app.start();
|
| 104 |
progressDialog.style.display = 'none';
|
| 105 |
|
| 106 |
-
//
|
| 107 |
if (assets.hdr && assets.hdr.resource) {
|
| 108 |
app.scene.envAtlas = assets.hdr.resource;
|
| 109 |
}
|
|
@@ -132,10 +172,8 @@ export async function initializeViewer(config, instanceId) {
|
|
| 132 |
|
| 133 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 134 |
|
| 135 |
-
// Use Script B's robust camera reset logic after everything is ready
|
| 136 |
app.once('update', () => resetViewerCamera());
|
| 137 |
|
| 138 |
-
// --- Tooltips functionality from Script B ---
|
| 139 |
try {
|
| 140 |
if (config.tooltips_url) {
|
| 141 |
import('./tooltips.js').then(tooltipsModule => {
|
|
|
|
| 80 |
window.pc = pc;
|
| 81 |
}
|
| 82 |
|
| 83 |
+
// Create app *before* creating assets!
|
| 84 |
+
const device = await pc.createGraphicsDevice(canvas, {
|
| 85 |
+
deviceTypes: ["webgl2"],
|
| 86 |
+
glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
|
| 87 |
+
twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
|
| 88 |
+
antialias: false
|
| 89 |
+
});
|
| 90 |
+
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 91 |
+
|
| 92 |
+
const opts = new pc.AppOptions();
|
| 93 |
+
opts.graphicsDevice = device;
|
| 94 |
+
opts.mouse = new pc.Mouse(document.body);
|
| 95 |
+
opts.touch = new pc.TouchDevice(document.body);
|
| 96 |
+
opts.componentSystems = [
|
| 97 |
+
pc.RenderComponentSystem,
|
| 98 |
+
pc.CameraComponentSystem,
|
| 99 |
+
pc.LightComponentSystem,
|
| 100 |
+
pc.ScriptComponentSystem,
|
| 101 |
+
pc.GSplatComponentSystem,
|
| 102 |
+
pc.CollisionComponentSystem,
|
| 103 |
+
pc.RigidbodyComponentSystem
|
| 104 |
+
];
|
| 105 |
+
opts.resourceHandlers = [
|
| 106 |
+
pc.TextureHandler,
|
| 107 |
+
pc.ContainerHandler,
|
| 108 |
+
pc.ScriptHandler,
|
| 109 |
+
pc.GSplatHandler
|
| 110 |
+
];
|
| 111 |
+
|
| 112 |
+
app = new pc.Application(canvas, opts);
|
| 113 |
+
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 114 |
+
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 115 |
+
|
| 116 |
+
resizeObserver = new ResizeObserver(entries => {
|
| 117 |
+
entries.forEach(entry => {
|
| 118 |
+
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
| 119 |
+
});
|
| 120 |
+
});
|
| 121 |
+
resizeObserver.observe(viewerContainer);
|
| 122 |
+
|
| 123 |
+
window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
|
| 124 |
+
app.on('destroy', () => resizeObserver.disconnect());
|
| 125 |
+
|
| 126 |
+
// Now create and register assets (with app.assets ready)
|
| 127 |
const assets = {
|
| 128 |
model: new pc.Asset('gsplat', 'gsplat', { url: plyUrl }),
|
| 129 |
orbit: new pc.Asset('script', 'script', { url: "https://mikafil-viewer-gs.static.hf.space/orbit-camera.js" }),
|
|
|
|
| 133 |
{ type: pc.TEXTURETYPE_RGBP, mipmaps: false }
|
| 134 |
),
|
| 135 |
};
|
| 136 |
+
// Register all assets to app.assets
|
|
|
|
| 137 |
for (const key in assets) {
|
| 138 |
+
app.assets.add(assets[key]);
|
|
|
|
|
|
|
| 139 |
}
|
| 140 |
|
| 141 |
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
|
|
|
| 143 |
app.start();
|
| 144 |
progressDialog.style.display = 'none';
|
| 145 |
|
| 146 |
+
// Apply HDR environment if available
|
| 147 |
if (assets.hdr && assets.hdr.resource) {
|
| 148 |
app.scene.envAtlas = assets.hdr.resource;
|
| 149 |
}
|
|
|
|
| 172 |
|
| 173 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 174 |
|
|
|
|
| 175 |
app.once('update', () => resetViewerCamera());
|
| 176 |
|
|
|
|
| 177 |
try {
|
| 178 |
if (config.tooltips_url) {
|
| 179 |
import('./tooltips.js').then(tooltipsModule => {
|