Skip to content

Commit cee02f7

Browse files
authored
Add gltf sample viewer (#1962)
* WIP add gltf sample viewer * Add index.html * Further integration (renderer already running) * Fix rotations * Fix clearcolor and zoom * Change ACES parameter * Use gltf-viewer from npm * Add package-lock * Update sample viewer and resolve issues * Update package-lock * Disable environment blur * Upload goldens * Update version of sample viewer
1 parent eea64b5 commit cee02f7

File tree

65 files changed

+22737
-3657
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+22737
-3657
lines changed

packages/render-fidelity-tools/package-lock.json

Lines changed: 22534 additions & 3654 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/render-fidelity-tools/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@babylonjs/core": "^4.2.0-alpha.26",
2828
"@babylonjs/loaders": "^4.2.0-alpha.26",
2929
"@google/model-viewer": "^1.5.0",
30+
"@khronosgroup/gltf-viewer": "^1.0.2",
3031
"@polymer/paper-button": "^3.0.1",
3132
"@polymer/paper-radio-group": "^3.0.1",
3233
"@polymer/paper-slider": "^3.0.1",
@@ -51,9 +52,11 @@
5152
},
5253
"devDependencies": {
5354
"@google/model-viewer-shared-assets": "^0.0.1",
54-
"rollup": "^2.26.6",
55+
"@rollup/plugin-commonjs": "^17.0.0",
5556
"@rollup/plugin-node-resolve": "^9.0.0",
5657
"@rollup/plugin-replace": "^2.3.0",
58+
"polymer-build": "^3.1.4",
59+
"rollup": "^2.26.6",
5760
"rollup-plugin-external-globals": "^0.6.0",
5861
"typescript": "4.0.3"
5962
}

packages/render-fidelity-tools/rollup.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {nodeResolve: resolve} = require('@rollup/plugin-node-resolve');
1717
const replace = require('@rollup/plugin-replace');
1818
const externalGlobals = require('rollup-plugin-external-globals');
1919
const {basename} = require('path');
20+
const commonjs = require('@rollup/plugin-commonjs')
2021

2122
const onwarn = (warning, warn) => {
2223
// Suppress non-actionable warning caused by TypeScript boilerplate:
@@ -26,7 +27,8 @@ const onwarn = (warning, warn) => {
2627
};
2728

2829
const plugins = [
29-
resolve({preferBuiltins: true}),
30+
commonjs(),
31+
resolve({preferBuiltins: true, browser: true}),
3032
replace({'Reflect.decorate': 'undefined'}),
3133
externalGlobals({filament: 'Filament'})
3234
];
@@ -59,6 +61,7 @@ const outputOptions = [
5961
buildTarget('./lib/components/renderers/filament-viewer.js', 'esm'),
6062
buildTarget('./lib/components/renderers/dspbr-pt-viewer.js', 'esm'),
6163
buildTarget('./lib/components/renderers/babylon-viewer.js', 'esm'),
64+
buildTarget('./lib/components/renderers/gltf-sample-viewer.js', 'esm'),
6265
buildTarget('./lib/image-comparison-worker.js', 'iife')
6366
];
6467

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// @ts-ignore
2+
import {GltfState, GltfView, ResourceLoader} from '@khronosgroup/gltf-viewer';
3+
import {css, customElement, html, LitElement, property} from 'lit-element';
4+
5+
import {ScenarioConfig} from '../../common.js';
6+
7+
8+
const $updateScenario = Symbol('updateScenario');
9+
const $updateSize = Symbol('updateSize');
10+
const $canvas = Symbol('canvas');
11+
const $view = Symbol('view');
12+
const $state = Symbol('state');
13+
const $resourceLoader = Symbol('resourceLoader');
14+
const $degToRadians = Symbol('degToRadians');
15+
16+
@customElement('gltf-sample-viewer')
17+
export class GltfSampleViewer extends LitElement {
18+
@property({type: Object}) scenario: ScenarioConfig|null = null;
19+
private[$canvas]: HTMLCanvasElement|null;
20+
private[$view]: GltfView;
21+
private[$state]: GltfState;
22+
private[$resourceLoader]: ResourceLoader;
23+
24+
static get styles() {
25+
return css`
26+
:host {
27+
display: block;
28+
}
29+
`;
30+
}
31+
32+
render() {
33+
return html`<canvas id="canvas"></canvas>`;
34+
}
35+
36+
37+
updated(changedProperties: Map<string, any>) {
38+
super.updated(changedProperties);
39+
this[$updateSize]();
40+
41+
if (changedProperties.has('scenario') && this.scenario != null) {
42+
this[$updateScenario](this.scenario);
43+
}
44+
}
45+
46+
47+
private async[$updateScenario](scenario: ScenarioConfig) {
48+
if (this[$view] == null) {
49+
this[$canvas] = this.shadowRoot!.querySelector('canvas');
50+
this[$view] = new GltfView(
51+
this[$canvas]!.getContext('webgl2', {alpha: true, antialias: true}));
52+
this[$state] = this[$view].createState();
53+
this[$resourceLoader] = this[$view].createResourceLoader();
54+
}
55+
this[$updateSize]();
56+
57+
this[$state].gltf = await this[$resourceLoader].loadGltf(scenario.model);
58+
59+
const defaultScene = this[$state].gltf.scene;
60+
this[$state].sceneIndex = defaultScene === undefined ? 0 : defaultScene;
61+
const scene = this[$state].gltf.scenes[this[$state].sceneIndex];
62+
scene.applyTransformHierarchy(this[$state].gltf);
63+
64+
const {target, orbit, verticalFoV} = scenario;
65+
const camera = this[$state].userCamera;
66+
camera.setVerticalFoV(this[$degToRadians](verticalFoV));
67+
camera.fitViewToScene(this[$state].gltf, this[$state].sceneIndex);
68+
69+
const yaw = this[$degToRadians](orbit.theta);
70+
const pitch = this[$degToRadians]((orbit.phi - 90));
71+
camera.setRotation(yaw, pitch);
72+
camera.setDistanceFromTarget(orbit.radius, [target.x, target.y, target.z]);
73+
this[$state].renderingParameters.clearColor = [0, 0, 0, 0];
74+
75+
const luts = {
76+
lut_ggx_file:
77+
'../../../node_modules/@khronosgroup/gltf-viewer/dist/assets/lut_ggx.png',
78+
lut_charlie_file:
79+
'../../../node_modules/@khronosgroup/gltf-viewer/dist/assets/lut_charlie.png',
80+
lut_sheen_E_file:
81+
'../../../node_modules/@khronosgroup/gltf-viewer/dist/assets/lut_sheen_E.png'
82+
};
83+
84+
this[$state].renderingParameters.environmentRotation = 0;
85+
86+
this[$state].environment =
87+
await this[$resourceLoader].loadEnvironment(scenario.lighting, luts);
88+
89+
this[$state].renderingParameters.renderEnvironmentMap =
90+
scenario.renderSkybox;
91+
this[$state].renderingParameters.blurEnvironmentMap = false;
92+
93+
this[$state].renderingParameters.toneMap = GltfState.ToneMaps.ACES;
94+
95+
this[$view].renderFrame(
96+
this[$state], this[$canvas]!.width, this[$canvas]!.height);
97+
requestAnimationFrame(() => {
98+
this.dispatchEvent(
99+
// This notifies the framework that the model is visible and the
100+
// screenshot can be taken
101+
new CustomEvent('model-visibility', {detail: {visible: true}}));
102+
});
103+
}
104+
105+
106+
private[$updateSize]() {
107+
if (this[$canvas] == null || this.scenario == null) {
108+
return;
109+
}
110+
111+
const canvas = this[$canvas]!;
112+
const {dimensions} = this.scenario;
113+
114+
const dpr = window.devicePixelRatio;
115+
const width = dimensions.width * dpr;
116+
const height = dimensions.height * dpr;
117+
118+
canvas.width = width;
119+
canvas.height = height;
120+
canvas.style.width = `${dimensions.width}px`;
121+
canvas.style.height = `${dimensions.height}px`;
122+
123+
const aspect = width / height;
124+
125+
const camera = this[$state].userCamera;
126+
camera.aspectRatio = aspect;
127+
}
128+
129+
private[$degToRadians](degree: number) {
130+
return Math.PI * (degree / 180);
131+
}
132+
}

packages/render-fidelity-tools/test/config.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
{
1919
"name": "dspbr-pt",
2020
"description": "dspbr-pt"
21+
},
22+
{
23+
"name": "gltf-sample-viewer",
24+
"description": "gltf-sample-viewer"
2125
}
2226
],
2327
"scenarios": [
@@ -188,7 +192,8 @@
188192
},
189193
"exclude": [
190194
"dspbr-pt",
191-
"babylon"
195+
"babylon",
196+
"gltf-sample-viewer"
192197
],
193198
"renderSkybox": true
194199
},
238 KB
133 KB
60.2 KB
182 KB
1.07 MB

0 commit comments

Comments
 (0)