Skip to content
This repository was archived by the owner on May 23, 2025. It is now read-only.

Commit e74c68e

Browse files
committed
viewport bouding size to 1.0 by default
1 parent 82a7dd8 commit e74c68e

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

src/living/helpers/babylonjs/InteractiveDynamicTexture.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export class InteractiveDynamicTexture extends BABYLON.DynamicTexture {
225225
if (this._started) {
226226
this.renderToTexture();
227227
}
228-
window.requestAnimationFrame(this._onTextureFrame.bind(this));
228+
requestAnimationFrame(this._onTextureFrame.bind(this));
229229
}
230230

231231
private _onResize(): void {

src/living/nodes/HTMLMetaElement.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ class ViewportMeta {
3737
viewport.userScalable = value === 'yes';
3838
break;
3939
case 'bounding-size':
40-
viewport.boundingSize = parseFloat(value);
40+
if (value === 'auto') {
41+
viewport.boundingSize = 1.0;
42+
} else if (value === 'unlimited') {
43+
viewport.boundingSize = Infinity;
44+
} else {
45+
viewport.boundingSize = parseFloat(value);
46+
}
4147
break;
4248
case 'depth':
4349
viewport.depth = parseFloat(value);
@@ -83,19 +89,10 @@ export default class HTMLMetaElementImpl extends HTMLElementImpl implements HTML
8389
return;
8490
}
8591
const viewportConfig = ViewportMeta.Parse(input);
86-
this._ownerDocument.addEventListener('load', () => {
87-
const spaceTransform = this._ownerDocument.space.asNativeType<BABYLON.TransformNode>();
88-
const recommendedContentSize = this._hostObject.getRecommendedBoudingSize?.() || 1.0;
89-
const targetSize = viewportConfig.boundingSize * recommendedContentSize;
90-
this.#fitTo(spaceTransform, targetSize);
91-
});
92-
}
93-
94-
#fitTo(node: BABYLON.TransformNode, targetSize: number) {
95-
const boundingVectors = node.getHierarchyBoundingVectors(true);
96-
const totalSize = boundingVectors.max.subtract(boundingVectors.min);
97-
const scalingFactor = Math.min(targetSize / totalSize.x, targetSize / totalSize.y, targetSize / totalSize.z);
98-
node.scaling.multiplyInPlace(new BABYLON.Vector3(scalingFactor, scalingFactor, scalingFactor));
99-
console.log('Fitting to', targetSize, totalSize, scalingFactor);
92+
if (viewportConfig.boundingSize === Infinity) {
93+
this._ownerDocument._disableSpaceFitting = true;
94+
} else {
95+
this._ownerDocument._spaceViewportBoundingSize = viewportConfig.boundingSize;
96+
}
10097
}
10198
}

src/living/nodes/SpatialDocument.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,13 @@ export class SpatialDocumentImpl<T extends NativeDocument = NativeDocument> exte
415415
}
416416
Promise.all(waitlist).then(dispatchSpaceReadyEvent);
417417
}
418-
});
418+
}, { once: true });
419+
this.addEventListener('spaceReady', () => {
420+
if (!this._disableSpaceFitting) {
421+
const recommendedContentSize = nativeDocument.getRecommendedBoudingSize?.() || 1.0;
422+
this._fitSpaceTo(this._spaceViewportBoundingSize * recommendedContentSize);
423+
}
424+
}, { once: true });
419425

420426
// Bypass the GOMContentLoaded event from the XSML document.
421427
nativeDocument.addEventListener('DOMContentLoaded', (event) => {
@@ -1258,6 +1264,21 @@ export class SpatialDocumentImpl<T extends NativeDocument = NativeDocument> exte
12581264
_createAttribute(privateData: ConstructorParameters<typeof AttrImpl>[2]) {
12591265
return new AttrImpl(this._hostObject, [], privateData);
12601266
}
1267+
1268+
_disableSpaceFitting: boolean = false;
1269+
_spaceViewportBoundingSize: number = 1.0;
1270+
1271+
/**
1272+
* Fit the <space /> to the target size.
1273+
* @param targetSize
1274+
*/
1275+
_fitSpaceTo(targetSize: number) {
1276+
const targetNode = this._ownerDocument.space.asNativeType<BABYLON.TransformNode>();
1277+
const boundingVectors = targetNode.getHierarchyBoundingVectors(true);
1278+
const totalSize = boundingVectors.max.subtract(boundingVectors.min);
1279+
const scalingFactor = Math.min(targetSize / totalSize.x, targetSize / totalSize.y, targetSize / totalSize.z);
1280+
targetNode.scaling.multiplyInPlace(new BABYLON.Vector3(scalingFactor, scalingFactor, scalingFactor));
1281+
}
12611282
}
12621283

12631284
applyMixins(SpatialDocumentImpl, [DocumentOrShadowRootImpl, GlobalEventHandlersImpl, ParentNodeImpl]);

src/living/nodes/SpatialMeshElement.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { NativeDocument } from '../../impl-interfaces';
22
import DOMExceptionImpl from '../domexception';
33
import { SpatialElement } from './SpatialElement';
44

5-
const defaultFitSize = 0.3;
6-
75
export default class SpatialMeshElement extends SpatialElement {
86
private _cachedName: string;
97

@@ -34,7 +32,7 @@ export default class SpatialMeshElement extends SpatialElement {
3432
get fitSize(): number {
3533
const v = parseFloat(this.getAttribute('fit-size'));
3634
if (v <= 0 || v > 1 || isNaN(v)) {
37-
return defaultFitSize;
35+
return 0;
3836
} else {
3937
return v;
4038
}
@@ -75,7 +73,9 @@ export default class SpatialMeshElement extends SpatialElement {
7573
contentElement.asNativeType().parent = containerNode; // Set the parent of the content native node to be the container node.
7674

7775
// Fit the content element to the size
78-
this._fitTo(containerNode, this.fitSize);
76+
if (this.fitSize > 0) {
77+
this._fitTo(containerNode, this.fitSize);
78+
}
7979
containerNode.rotate(BABYLON.Axis.Y, Math.PI, BABYLON.Space.LOCAL);
8080
}
8181

@@ -86,7 +86,7 @@ export default class SpatialMeshElement extends SpatialElement {
8686
return assetsBundle.instantiate(selector, this._getName());
8787
}
8888

89-
private _fitTo(node: BABYLON.TransformNode, ratio: number = defaultFitSize) {
89+
private _fitTo(node: BABYLON.TransformNode, ratio: number) {
9090
const boundingVectors = node.getHierarchyBoundingVectors(true);
9191
const totalSize = boundingVectors.max.subtract(boundingVectors.min);
9292
const scalingFactor = Math.min(ratio / totalSize.x, ratio / totalSize.y, ratio / totalSize.z);

0 commit comments

Comments
 (0)