Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat: optimize resource loading in Validator and improve Viewer performance

nirholasbdd3a6273cd1
diff --git a/src/validator.js b/src/validator.js
index 407250cf4..ad054f869 100644
--- a/src/validator.js
+++ b/src/validator.js
@@ -1,6 +1,6 @@
import { LoaderUtils } from 'three';
import { validateBytes } from 'gltf-validator';
-import { ValidatorToggle } from './components/validator-toggle';
+
import { ValidatorReport } from './components/validator-report';
const SEVERITY_MAP = ['Errors', 'Warnings', 'Infos', 'Hints'];
@@ -13,9 +13,7 @@ export class Validator {
this.el = el;
this.report = null;
- this.toggleEl = document.createElement('div');
- this.toggleEl.classList.add('report-toggle-wrap', 'hidden');
- this.el.appendChild(this.toggleEl);
+
}
/**
@@ -27,10 +25,19 @@ export class Validator {
* @return {Promise}
*/
validate(rootFile, rootPath, assetMap, response) {
- // TODO: This duplicates a request of the three.js loader, and could
- // take advantage of THREE.Cache after r90.
- return fetch(rootFile)
- .then((response) => response.arrayBuffer())
+ // Use Three.js Cache to avoid re-downloading the model.
+ // Cache.enabled is set to true in viewer.js.
+ const fetchBuffer = window.THREE && window.THREE.Cache && window.THREE.Cache.get(rootFile)
+ ? Promise.resolve(window.THREE.Cache.get(rootFile))
+ .then((data) => {
+ if (data instanceof ArrayBuffer) return data;
+ // Cache may hold string for .gltf; convert
+ if (typeof data === 'string') return new TextEncoder().encode(data).buffer;
+ return fetch(rootFile).then((r) => r.arrayBuffer());
+ })
+ : fetch(rootFile).then((r) => r.arrayBuffer());
+
+ return fetchBuffer
.then((buffer) =>
validateBytes(new Uint8Array(buffer), {
externalResourceFunction: (uri) =>
@@ -93,9 +100,7 @@ export class Validator {
this.setResponse(response);
- this.toggleEl.innerHTML = ValidatorToggle(report);
- this.showToggle();
- this.bindListeners();
+
function groupMessages(report) {
const CODES = {
@@ -168,29 +173,8 @@ export class Validator {
*/
setReportException(e) {
this.report = null;
- this.toggleEl.innerHTML = this.toggleTpl({ reportError: e, level: 0 });
- this.showToggle();
- this.bindListeners();
}
- bindListeners() {
- const reportToggleBtn = this.toggleEl.querySelector('.report-toggle');
- reportToggleBtn.addEventListener('click', () => this.showLightbox());
-
- const reportToggleCloseBtn = this.toggleEl.querySelector('.report-toggle-close');
- reportToggleCloseBtn.addEventListener('click', (e) => {
- this.hideToggle();
- e.stopPropagation();
- });
- }
-
- showToggle() {
- this.toggleEl.classList.remove('hidden');
- }
-
- hideToggle() {
- this.toggleEl.classList.add('hidden');
- }
showLightbox() {
if (!this.report) return;
diff --git a/src/viewer.js b/src/viewer.js
index f83e12343..eac668d58 100644
--- a/src/viewer.js
+++ b/src/viewer.js
@@ -114,7 +114,7 @@ export class Viewer {
this.renderer = window.renderer = new WebGLRenderer({ antialias: true });
this.renderer.setClearColor(0xcccccc);
- this.renderer.setPixelRatio(window.devicePixelRatio);
+ this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
this.renderer.setSize(el.clientWidth, el.clientHeight);
this.pmremGenerator = new PMREMGenerator(this.renderer);
@@ -138,6 +138,7 @@ export class Viewer {
this.axesHelper = null;
this.modelInfo = null;
this.annotationEls = [];
+ this._tempVec = new Vector3();
this.addAxesHelper();
this.addGUI();
@@ -475,7 +476,6 @@ export class Viewer {
path,
(texture) => {
const envMap = this.pmremGenerator.fromEquirectangular(texture).texture;
- this.pmremGenerator.dispose();
resolve({ envMap });
},
@@ -572,7 +572,7 @@ export class Viewer {
const height = this.el.clientHeight;
const halfW = width / 2;
const halfH = height / 2;
- const tempVec = new Vector3();
+ const tempVec = this._tempVec;
this.annotationEls.forEach(({ el, position }) => {
tempVec.copy(position);
@@ -816,10 +816,16 @@ export class Viewer {
}
function traverseMaterials(object, callback) {
+ const seen = new Set();
object.traverse((node) => {
if (!node.geometry) return;
const materials = Array.isArray(node.material) ? node.material : [node.material];
- materials.forEach(callback);
+ materials.forEach((mat) => {
+ if (mat && !seen.has(mat.uuid)) {
+ seen.add(mat.uuid);
+ callback(mat);
+ }
+ });
});
}
diff --git a/style.css b/style.css
index 8b183fe85..a1284aefc 100644
--- a/style.css
+++ b/style.css
@@ -536,46 +536,6 @@ footer .separator {
background: rgba(255, 255, 255, 0.02);
}
-/******************************************************************************
- * Validation toggle
- */
-
-.report-toggle-wrap.hidden { display: none; }
-
-.report-toggle {
- cursor: pointer;
- display: flex;
- position: absolute;
- bottom: 0;
- left: 20px;
- height: 30px;
- box-shadow: 0px 0px 5px 0 rgba(0, 0, 0, 0.25);
- background: #FFF;
- box-sizing: border-box;
-
- color: #f0f0f0;
- background: #000;
- border-left: 6px solid #000;
-}
-
-.report-toggle.level-1 { color: #444; background: #ffeda0; border-left-color: #feb24c; }
-.report-toggle.level-0 { color: #444; background: #f4c2be; border-left-color: #b10026; }
-
-.report-toggle-text {
- line-height: 32px;
- padding: 0 var(--space-xs);
- font-weight: 300;
- font-size: var(--text-sm);
-}
-
-.report-toggle-close {
- width: 30px;
- height: 30px;
- line-height: 30px;
- font-size: 1.5em;
- text-align: center;
-}
-
/******************************************************************************
* CSS Spinner
*