Skip to content

Commit

Permalink
fix: xr source autostart
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Nov 29, 2023
1 parent acc83f8 commit 32b72c9
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions modules/webxr/src/nodes/source/XRSource.ts
Expand Up @@ -16,30 +16,41 @@ export class XRSource extends SourceNode<XRDataFrame> {
this.options.source = this.options.source || new CameraObject(this.uid);
this.options.output = this.options.output === undefined ? true : this.options.output;

this.on('build', this._initReferenceSpace.bind(this));
this.on('build', this._onBuild.bind(this));
this.once('destroy', this._onDestroy.bind(this));
}

protected get source(): CameraObject {
return super.source as CameraObject;
}

private _initReferenceSpace(): Promise<void> {
private _onBuild(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this._service = this.model.findService<WebXRService>('WebXRService');
if (this._service === undefined || this._service === null) {
return reject(new Error(`WebXR service was not added to model!`));
}
if (this.options.autoStart) {
return this.start().then(resolve).catch(reject);
} else {
resolve();
}
});
}

start(): Promise<void> {
return new Promise((resolve, reject) => {
// Request local reference spaces at the time this session was created
this._service.session.requestReferenceSpace('local').then((refSpace: XRReferenceSpace) => {
this.logger('debug', 'Local reference space created. Starting animation frames ...');
this._refSpace = refSpace;

this._service.session.requestAnimationFrame(this._onXRFrame.bind(this));
});

resolve();
this._service.session
.requestReferenceSpace('local')
.then((refSpace: XRReferenceSpace) => {
this.logger('debug', 'Local reference space created. Starting animation frames ...');
this._refSpace = refSpace;

this._service.session.requestAnimationFrame(this._onXRFrame.bind(this));
resolve();
})
.catch(reject);
});
}

Expand Down Expand Up @@ -142,4 +153,5 @@ export interface XRSourceOptions extends SourceNodeOptions {
* @default true
*/
output?: boolean;
autoStart?: boolean;
}

0 comments on commit 32b72c9

Please sign in to comment.