MikaFil commited on
Commit
8fe43ef
·
verified ·
1 Parent(s): 5df6c84

Update viewer.js

Browse files
Files changed (1) hide show
  1. viewer.js +114 -103
viewer.js CHANGED
@@ -1,111 +1,122 @@
1
  let pc;
2
  export let app = null;
 
 
 
 
3
 
4
  export async function initializeViewer(config, instanceId) {
5
- if (app) return;
6
-
7
- const glbUrl = config.glb_url;
8
- const envAtlasUrl = config.envAtlas_url || "https://playcanvas.github.io/static/assets/cubemaps/helipad-env-atlas.png"; // Default PlayCanvas envAtlas
9
-
10
- const viewerContainer = document.getElementById('viewer-container-' + instanceId);
11
-
12
- let oldCanvas = document.getElementById('canvas-' + instanceId);
13
- if (oldCanvas) oldCanvas.remove();
14
-
15
- const canvas = document.createElement('canvas');
16
- canvas.id = 'canvas-' + instanceId;
17
- canvas.style.width = "100%";
18
- canvas.style.height = "100%";
19
- viewerContainer.appendChild(canvas);
20
-
21
- if (!pc) {
22
- pc = await import("https://esm.run/playcanvas");
23
- window.pc = pc;
24
- }
25
-
26
- const device = await pc.createGraphicsDevice(canvas, {
27
- deviceTypes: ["webgl2"],
28
- glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
29
- twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  });
31
-
32
- device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
33
-
34
- const opts = new pc.AppOptions();
35
- opts.graphicsDevice = device;
36
- opts.mouse = new pc.Mouse(canvas);
37
- opts.touch = new pc.TouchDevice(canvas);
38
- opts.componentSystems = [
39
- pc.RenderComponentSystem,
40
- pc.CameraComponentSystem,
41
- pc.LightComponentSystem,
42
- pc.ScriptComponentSystem
43
- ];
44
- opts.resourceHandlers = [
45
- pc.TextureHandler,
46
- pc.ContainerHandler,
47
- pc.ScriptHandler
48
- ];
49
-
50
- app = new pc.Application(canvas, opts);
51
- app.setCanvasFillMode(pc.FILLMODE_NONE);
52
- app.setCanvasResolution(pc.RESOLUTION_AUTO);
53
-
54
- // Set up assets (GLB and envAtlas)
55
- const glbAsset = new pc.Asset('model', 'container', { url: glbUrl });
56
- const envAtlasAsset = new pc.Asset('helipad-env-atlas', 'texture', { url: envAtlasUrl }, { type: pc.TEXTURETYPE_RGBP, mipmaps: false });
57
-
58
- app.assets.add(glbAsset);
59
- app.assets.add(envAtlasAsset);
60
-
61
- // KHR extensions (usually not necessary, but included for completeness)
62
- const containerHandler = app.loader.getHandler('container');
63
- if (containerHandler) {
64
- containerHandler.pcMaterialOptions = {
65
- processTransmission: true,
66
- processVolume: true,
67
- processIOR: true,
68
- processSheen: true,
69
- processClearCoat: true,
70
- processVariants: true
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
- app.assets.load(glbAsset);
110
- app.assets.load(envAtlasAsset);
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
  }