Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
// viewer.js
|
| 2 |
// ==============================
|
| 3 |
|
| 4 |
-
let pc;
|
| 5 |
export let app = null;
|
| 6 |
let cameraEntity = null;
|
| 7 |
let modelEntity = null;
|
| 8 |
let viewerInitialized = false;
|
| 9 |
let resizeObserver = null;
|
|
|
|
| 10 |
|
| 11 |
let chosenCameraX, chosenCameraY, chosenCameraZ;
|
| 12 |
let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, minY;
|
| 13 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 14 |
let plyUrl, glbUrl;
|
| 15 |
-
let galleryEntity = null;
|
| 16 |
|
| 17 |
export async function initializeViewer(config, instanceId) {
|
| 18 |
if (viewerInitialized) return;
|
|
@@ -123,14 +123,13 @@ export async function initializeViewer(config, instanceId) {
|
|
| 123 |
window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
|
| 124 |
app.on('destroy', () => resizeObserver.disconnect());
|
| 125 |
|
| 126 |
-
// ---
|
| 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" }),
|
| 130 |
galerie: glbUrl ? new pc.Asset('galerie', 'container', { url: glbUrl }) : null,
|
| 131 |
hdr: new pc.Asset('hdr', 'texture', { url: "https://huggingface.co/datasets/bilca/ply_files/resolve/main/galeries/blanc.png" }, { type: pc.TEXTURETYPE_RGBP, mipmaps: false })
|
| 132 |
};
|
| 133 |
-
// filter out nulls for loader
|
| 134 |
const assetArray = Object.values(assets).filter(a => !!a);
|
| 135 |
|
| 136 |
const loader = new pc.AssetListLoader(assetArray, app.assets);
|
|
@@ -138,12 +137,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 138 |
app.start();
|
| 139 |
progressDialog.style.display = 'none';
|
| 140 |
|
| 141 |
-
//
|
| 142 |
-
if (assets.hdr && assets.hdr.resource) {
|
| 143 |
-
app.scene.envAtlas = assets.hdr.resource;
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
// Model
|
| 147 |
modelEntity = new pc.Entity('model');
|
| 148 |
modelEntity.addComponent('gsplat', { asset: assets.model });
|
| 149 |
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
|
@@ -151,12 +145,17 @@ export async function initializeViewer(config, instanceId) {
|
|
| 151 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 152 |
app.root.addChild(modelEntity);
|
| 153 |
|
| 154 |
-
// GLB gallery
|
| 155 |
if (assets.galerie && assets.galerie.resource && assets.galerie.resource.instantiateRenderEntity) {
|
| 156 |
galleryEntity = assets.galerie.resource.instantiateRenderEntity();
|
| 157 |
app.root.addChild(galleryEntity);
|
| 158 |
}
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
// Camera
|
| 161 |
cameraEntity = new pc.Entity('camera');
|
| 162 |
cameraEntity.addComponent('camera', { clearColor: new pc.Color(0.2, 0.2, 0.2, 1) });
|
|
@@ -183,7 +182,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 183 |
|
| 184 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 185 |
|
| 186 |
-
// Use Script B's camera reset logic after everything is ready
|
| 187 |
app.once('update', () => resetViewerCamera());
|
| 188 |
|
| 189 |
// Tooltips (if config.tooltips_url and tooltips.js available)
|
|
@@ -207,6 +206,7 @@ export async function initializeViewer(config, instanceId) {
|
|
| 207 |
});
|
| 208 |
}
|
| 209 |
|
|
|
|
| 210 |
export function resetViewerCamera() {
|
| 211 |
try {
|
| 212 |
if (!cameraEntity || !modelEntity || !app) return;
|
|
|
|
| 1 |
// viewer.js
|
| 2 |
// ==============================
|
| 3 |
|
| 4 |
+
let pc;
|
| 5 |
export let app = null;
|
| 6 |
let cameraEntity = null;
|
| 7 |
let modelEntity = null;
|
| 8 |
let viewerInitialized = false;
|
| 9 |
let resizeObserver = null;
|
| 10 |
+
let galleryEntity = null;
|
| 11 |
|
| 12 |
let chosenCameraX, chosenCameraY, chosenCameraZ;
|
| 13 |
let minZoom, maxZoom, minAngle, maxAngle, minAzimuth, maxAzimuth, minPivotY, minY;
|
| 14 |
let modelX, modelY, modelZ, modelScale, modelRotationX, modelRotationY, modelRotationZ;
|
| 15 |
let plyUrl, glbUrl;
|
|
|
|
| 16 |
|
| 17 |
export async function initializeViewer(config, instanceId) {
|
| 18 |
if (viewerInitialized) return;
|
|
|
|
| 123 |
window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
|
| 124 |
app.on('destroy', () => resizeObserver.disconnect());
|
| 125 |
|
| 126 |
+
// --- Assets: Add GLB and HDR support ---
|
| 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" }),
|
| 130 |
galerie: glbUrl ? new pc.Asset('galerie', 'container', { url: glbUrl }) : null,
|
| 131 |
hdr: new pc.Asset('hdr', 'texture', { url: "https://huggingface.co/datasets/bilca/ply_files/resolve/main/galeries/blanc.png" }, { type: pc.TEXTURETYPE_RGBP, mipmaps: false })
|
| 132 |
};
|
|
|
|
| 133 |
const assetArray = Object.values(assets).filter(a => !!a);
|
| 134 |
|
| 135 |
const loader = new pc.AssetListLoader(assetArray, app.assets);
|
|
|
|
| 137 |
app.start();
|
| 138 |
progressDialog.style.display = 'none';
|
| 139 |
|
| 140 |
+
// Add GSplat (PLY) first
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
modelEntity = new pc.Entity('model');
|
| 142 |
modelEntity.addComponent('gsplat', { asset: assets.model });
|
| 143 |
modelEntity.setLocalPosition(modelX, modelY, modelZ);
|
|
|
|
| 145 |
modelEntity.setLocalScale(modelScale, modelScale, modelScale);
|
| 146 |
app.root.addChild(modelEntity);
|
| 147 |
|
| 148 |
+
// Then add GLB gallery (if available)
|
| 149 |
if (assets.galerie && assets.galerie.resource && assets.galerie.resource.instantiateRenderEntity) {
|
| 150 |
galleryEntity = assets.galerie.resource.instantiateRenderEntity();
|
| 151 |
app.root.addChild(galleryEntity);
|
| 152 |
}
|
| 153 |
|
| 154 |
+
// Set HDR environment (after app.start)
|
| 155 |
+
if (assets.hdr && assets.hdr.resource) {
|
| 156 |
+
app.scene.envAtlas = assets.hdr.resource;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
// Camera
|
| 160 |
cameraEntity = new pc.Entity('camera');
|
| 161 |
cameraEntity.addComponent('camera', { clearColor: new pc.Color(0.2, 0.2, 0.2, 1) });
|
|
|
|
| 182 |
|
| 183 |
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 184 |
|
| 185 |
+
// Use Script B's robust camera reset logic after everything is ready
|
| 186 |
app.once('update', () => resetViewerCamera());
|
| 187 |
|
| 188 |
// Tooltips (if config.tooltips_url and tooltips.js available)
|
|
|
|
| 206 |
});
|
| 207 |
}
|
| 208 |
|
| 209 |
+
// Camera reset logic from Script B
|
| 210 |
export function resetViewerCamera() {
|
| 211 |
try {
|
| 212 |
if (!cameraEntity || !modelEntity || !app) return;
|