Spaces:
Running
Running
Update viewer.js
Browse files
viewer.js
CHANGED
|
@@ -1,111 +1,122 @@
|
|
| 1 |
let pc;
|
| 2 |
export let app = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
export async function initializeViewer(config, instanceId) {
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
});
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
app.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
// Load both assets before continuing
|
| 75 |
-
const loader = new pc.AssetListLoader([glbAsset, envAtlasAsset], app.assets);
|
| 76 |
-
loader.load(() => {
|
| 77 |
-
// Environment map
|
| 78 |
-
app.scene.envAtlas = envAtlasAsset.resource;
|
| 79 |
-
app.scene.skyboxMip = 1;
|
| 80 |
-
app.scene.skyboxIntensity = 1.8;
|
| 81 |
-
app.scene.skyboxRotation = new pc.Quat().setFromEulerAngles(0, 70, 0);
|
| 82 |
-
|
| 83 |
-
// Add model
|
| 84 |
-
const entity = glbAsset.resource.instantiateRenderEntity();
|
| 85 |
-
app.root.addChild(entity);
|
| 86 |
-
|
| 87 |
-
// Add a camera
|
| 88 |
-
const camera = new pc.Entity('camera');
|
| 89 |
-
camera.addComponent('camera', { clearColor: new pc.Color(0.93, 0.93, 0.93, 1) });
|
| 90 |
-
camera.setPosition(0, 1, 6);
|
| 91 |
-
camera.lookAt(0, 0.5, 0);
|
| 92 |
-
app.root.addChild(camera);
|
| 93 |
-
|
| 94 |
-
// Add a light (for diffuse shading)
|
| 95 |
-
const light = new pc.Entity('mainLight');
|
| 96 |
-
light.addComponent('light', {
|
| 97 |
-
type: "directional",
|
| 98 |
-
color: new pc.Color(1, 1, 1),
|
| 99 |
-
intensity: 1.1
|
| 100 |
-
});
|
| 101 |
-
light.setPosition(0, 2, 2);
|
| 102 |
-
light.lookAt(0, 0, 0);
|
| 103 |
-
app.root.addChild(light);
|
| 104 |
-
|
| 105 |
-
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 106 |
-
window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
|
| 107 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
}
|
|
|
|
| 1 |
let pc;
|
| 2 |
export let app = null;
|
| 3 |
+
let cameraEntity = null;
|
| 4 |
+
let bouchonEntity = null;
|
| 5 |
+
let viewerInitialized = false;
|
| 6 |
+
let resizeObserver = null;
|
| 7 |
|
| 8 |
export async function initializeViewer(config, instanceId) {
|
| 9 |
+
if (viewerInitialized) return;
|
| 10 |
+
|
| 11 |
+
const glbUrl3 = config.glb_url_3;
|
| 12 |
+
const envAtlasUrl = "https://playcanvas.github.io/static/assets/cubemaps/helipad-env-atlas.png"; // official PlayCanvas env
|
| 13 |
+
|
| 14 |
+
const canvasId = 'canvas-' + instanceId;
|
| 15 |
+
const viewerContainer = document.getElementById('viewer-container-' + instanceId);
|
| 16 |
+
|
| 17 |
+
let oldCanvas = document.getElementById(canvasId);
|
| 18 |
+
if (oldCanvas) oldCanvas.remove();
|
| 19 |
+
|
| 20 |
+
const canvas = document.createElement('canvas');
|
| 21 |
+
canvas.id = canvasId;
|
| 22 |
+
canvas.className = 'ply-canvas';
|
| 23 |
+
canvas.style.width = "100%";
|
| 24 |
+
canvas.style.height = "100%";
|
| 25 |
+
viewerContainer.appendChild(canvas);
|
| 26 |
+
|
| 27 |
+
if (!pc) {
|
| 28 |
+
pc = await import("https://esm.run/playcanvas");
|
| 29 |
+
window.pc = pc;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
// Create app first
|
| 33 |
+
const device = await pc.createGraphicsDevice(canvas, {
|
| 34 |
+
deviceTypes: ["webgl2"],
|
| 35 |
+
glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
|
| 36 |
+
twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js",
|
| 37 |
+
antialias: false
|
| 38 |
+
});
|
| 39 |
+
device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
|
| 40 |
+
|
| 41 |
+
const opts = new pc.AppOptions();
|
| 42 |
+
opts.graphicsDevice = device;
|
| 43 |
+
opts.mouse = new pc.Mouse(canvas);
|
| 44 |
+
opts.touch = new pc.TouchDevice(canvas);
|
| 45 |
+
opts.componentSystems = [
|
| 46 |
+
pc.RenderComponentSystem,
|
| 47 |
+
pc.CameraComponentSystem,
|
| 48 |
+
pc.LightComponentSystem,
|
| 49 |
+
pc.ScriptComponentSystem
|
| 50 |
+
];
|
| 51 |
+
opts.resourceHandlers = [
|
| 52 |
+
pc.TextureHandler,
|
| 53 |
+
pc.ContainerHandler,
|
| 54 |
+
pc.ScriptHandler
|
| 55 |
+
];
|
| 56 |
+
|
| 57 |
+
app = new pc.Application(canvas, opts);
|
| 58 |
+
app.setCanvasFillMode(pc.FILLMODE_NONE);
|
| 59 |
+
app.setCanvasResolution(pc.RESOLUTION_AUTO);
|
| 60 |
+
|
| 61 |
+
resizeObserver = new ResizeObserver(entries => {
|
| 62 |
+
entries.forEach(entry => {
|
| 63 |
+
app.resizeCanvas(entry.contentRect.width, entry.contentRect.height);
|
| 64 |
});
|
| 65 |
+
});
|
| 66 |
+
resizeObserver.observe(viewerContainer);
|
| 67 |
+
|
| 68 |
+
window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
|
| 69 |
+
app.on('destroy', () => resizeObserver.disconnect());
|
| 70 |
+
|
| 71 |
+
// --- ASSETS
|
| 72 |
+
const assets = {
|
| 73 |
+
bouchon: new pc.Asset('bouchon', 'container', {url: glbUrl3}),
|
| 74 |
+
helipad: new pc.Asset(
|
| 75 |
+
'helipad-env-atlas',
|
| 76 |
+
'texture',
|
| 77 |
+
{ url: envAtlasUrl },
|
| 78 |
+
{ type: pc.TEXTURETYPE_RGBP, mipmaps: false }
|
| 79 |
+
)
|
| 80 |
+
};
|
| 81 |
+
for (const key in assets) app.assets.add(assets[key]);
|
| 82 |
+
|
| 83 |
+
const loader = new pc.AssetListLoader(Object.values(assets), app.assets);
|
| 84 |
+
loader.load(() => {
|
| 85 |
+
app.start();
|
| 86 |
+
|
| 87 |
+
// --- Instantiate GLB as-is (NO material changes)
|
| 88 |
+
bouchonEntity = assets.bouchon.resource.instantiateRenderEntity();
|
| 89 |
+
bouchonEntity.setLocalPosition(0, 0, 0);
|
| 90 |
+
bouchonEntity.setLocalScale(1, 1, 1);
|
| 91 |
+
app.root.addChild(bouchonEntity);
|
| 92 |
+
|
| 93 |
+
// --- Camera
|
| 94 |
+
cameraEntity = new pc.Entity('camera');
|
| 95 |
+
cameraEntity.addComponent('camera', { clearColor: new pc.Color(0.95, 0.95, 0.95, 1) });
|
| 96 |
+
cameraEntity.setPosition(0, 1, 5.5);
|
| 97 |
+
cameraEntity.lookAt(0, 0.5, 0);
|
| 98 |
+
app.root.addChild(cameraEntity);
|
| 99 |
+
|
| 100 |
+
// --- Directional light
|
| 101 |
+
const light = new pc.Entity("mainLight");
|
| 102 |
+
light.addComponent('light',{
|
| 103 |
+
type: "directional",
|
| 104 |
+
color: new pc.Color(1, 1, 1),
|
| 105 |
+
intensity:1.5,
|
| 106 |
+
castShadows: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
});
|
| 108 |
+
light.setPosition(1, 3, 2);
|
| 109 |
+
light.lookAt(0, 0, 0);
|
| 110 |
+
app.root.addChild(light);
|
| 111 |
+
|
| 112 |
+
// --- HDR environment/skybox
|
| 113 |
+
app.scene.envAtlas = assets.helipad.resource;
|
| 114 |
+
app.scene.skyboxMip = 1;
|
| 115 |
+
app.scene.skyboxIntensity = 2.0;
|
| 116 |
+
app.scene.skyboxRotation = new pc.Quat().setFromEulerAngles(0, 70, 0);
|
| 117 |
+
|
| 118 |
+
app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
|
| 119 |
|
| 120 |
+
viewerInitialized = true;
|
| 121 |
+
});
|
| 122 |
}
|