MikaFil commited on
Commit
5df6c84
·
verified ·
1 Parent(s): 74fc7a3

Update viewer.js

Browse files
Files changed (1) hide show
  1. viewer.js +103 -100
viewer.js CHANGED
@@ -1,108 +1,111 @@
1
- // Minimal viewer.js (for one GLB with KHR_materials_transmission)
2
-
3
  let pc;
4
  export let app = null;
5
 
6
  export async function initializeViewer(config, instanceId) {
7
- if (app) return; // Only allow one instance for this minimal demo
8
-
9
- // Get URLs from config
10
- const glbUrl = config.glb_url;
11
-
12
- // Get the container
13
- const viewerContainer = document.getElementById('viewer-container-' + instanceId);
14
-
15
- // Prepare the canvas
16
- let oldCanvas = document.getElementById('canvas-' + instanceId);
17
- if (oldCanvas) oldCanvas.remove();
18
-
19
- const canvas = document.createElement('canvas');
20
- canvas.id = 'canvas-' + instanceId;
21
- canvas.style.width = "100%";
22
- canvas.style.height = "100%";
23
- viewerContainer.appendChild(canvas);
24
-
25
- // Load PlayCanvas if not loaded yet
26
- if (!pc) {
27
- pc = await import("https://esm.run/playcanvas");
28
- window.pc = pc;
29
- }
30
-
31
- // Create graphics device
32
- const device = await pc.createGraphicsDevice(canvas, {
33
- deviceTypes: ["webgl2"],
34
- glslangUrl: "https://playcanvas.vercel.app/static/lib/glslang/glslang.js",
35
- twgslUrl: "https://playcanvas.vercel.app/static/lib/twgsl/twgsl.js"
36
- });
37
-
38
- device.maxPixelRatio = Math.min(window.devicePixelRatio, 2);
39
-
40
- const opts = new pc.AppOptions();
41
- opts.graphicsDevice = device;
42
- opts.mouse = new pc.Mouse(canvas);
43
- opts.touch = new pc.TouchDevice(canvas);
44
- opts.componentSystems = [
45
- pc.RenderComponentSystem,
46
- pc.CameraComponentSystem,
47
- pc.LightComponentSystem,
48
- pc.ScriptComponentSystem
49
- ];
50
- opts.resourceHandlers = [
51
- pc.TextureHandler,
52
- pc.ContainerHandler,
53
- pc.ScriptHandler
54
- ];
55
-
56
- app = new pc.Application(canvas, opts);
57
- app.setCanvasFillMode(pc.FILLMODE_NONE);
58
- app.setCanvasResolution(pc.RESOLUTION_AUTO);
59
-
60
- // GLB asset
61
- const glbAsset = new pc.Asset('model', 'container', { url: glbUrl });
62
- app.assets.add(glbAsset);
63
-
64
- // Optional: set up KHR extension flags (not always required, but safe)
65
- const containerHandler = app.loader.getHandler('container');
66
- if (containerHandler) {
67
- containerHandler.pcMaterialOptions = {
68
- processTransmission: true,
69
- processVolume: true,
70
- processIOR: true,
71
- processSheen: true,
72
- processClearCoat: true,
73
- processVariants: true
74
- };
75
- }
76
-
77
- // Wait for GLB to load
78
- glbAsset.ready(() => {
79
- // Instantiate as-is (DO NOT TOUCH MATERIALS)
80
- const entity = glbAsset.resource.instantiateRenderEntity();
81
- app.root.addChild(entity);
82
-
83
- // Add a camera
84
- const camera = new pc.Entity('camera');
85
- camera.addComponent('camera', { clearColor: new pc.Color(0.95, 0.95, 0.95, 1) });
86
- camera.setPosition(0, 1, 4);
87
- camera.lookAt(0, 0, 0);
88
- app.root.addChild(camera);
89
-
90
- // Add a light
91
- const light = new pc.Entity('mainLight');
92
- light.addComponent('light', {
93
- type: "directional",
94
- color: new pc.Color(1, 1, 1),
95
- intensity: 1.0
96
- });
97
- light.setPosition(0, 2, 2);
98
- light.lookAt(0, 0, 0);
99
- app.root.addChild(light);
100
 
101
- // Optionally: set skybox, env map, etc, as you like
 
 
 
 
102
 
103
- app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight);
104
- window.addEventListener('resize', () => app.resizeCanvas(viewerContainer.clientWidth, viewerContainer.clientHeight));
105
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
- app.assets.load(glbAsset);
 
108
  }
 
 
 
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
  }