Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XR editor adaptation #2165

Merged
merged 37 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
641308a
feat: xr support editor
cptbtptpbcptdtptp Jul 10, 2024
2be2a8f
feat: update code
cptbtptpbcptdtptp Jul 11, 2024
58d6775
feat: xr support editor
cptbtptpbcptdtptp Jul 15, 2024
2a83546
feat: xr support editor
cptbtptpbcptdtptp Jul 15, 2024
de7b57c
feat: update code
cptbtptpbcptdtptp Jul 15, 2024
5d473c6
feat: xr support editor
cptbtptpbcptdtptp Jul 15, 2024
3c4a6da
feat: xr support editor
cptbtptpbcptdtptp Jul 15, 2024
cd886e7
feat: update code
cptbtptpbcptdtptp Jul 15, 2024
7b64ee9
feat: merge code
cptbtptpbcptdtptp Jul 16, 2024
7fe7999
feat: merge code
cptbtptpbcptdtptp Jul 16, 2024
dcb2d30
feat: merge code
cptbtptpbcptdtptp Jul 16, 2024
296275e
fix: lint
cptbtptpbcptdtptp Jul 16, 2024
0a48f8b
feat: udpate code
cptbtptpbcptdtptp Jul 16, 2024
35850e3
feat: udpate code
cptbtptpbcptdtptp Jul 16, 2024
20adaaa
"v0.0.0-experimental-1.3-xr.0"
cptbtptpbcptdtptp Jul 16, 2024
e12114b
"v0.0.0-experimental-1.3-xr.1"
cptbtptpbcptdtptp Jul 16, 2024
f9a81ca
"v0.0.0-experimental-1.3-xr.2"
cptbtptpbcptdtptp Jul 16, 2024
09c4a95
"v0.0.0-experimental-1.3-xr.3"
cptbtptpbcptdtptp Jul 16, 2024
5d254aa
feat: update code
cptbtptpbcptdtptp Jul 16, 2024
46e4e73
"v0.0.0-experimental-1.3-xr.4"
cptbtptpbcptdtptp Jul 16, 2024
d9f4e92
feat: update code
cptbtptpbcptdtptp Jul 17, 2024
57d0845
"v0.0.0-experimental-1.3-xr.5"
cptbtptpbcptdtptp Jul 17, 2024
be861d8
feat: update code
cptbtptpbcptdtptp Jul 18, 2024
33f031a
feat: update code
cptbtptpbcptdtptp Jul 18, 2024
3e4d0c3
feat: update code
cptbtptpbcptdtptp Jul 18, 2024
c0b09dc
feat: update code
cptbtptpbcptdtptp Jul 18, 2024
2843773
feat: update code
cptbtptpbcptdtptp Jul 18, 2024
fdca396
feat: update code
cptbtptpbcptdtptp Jul 18, 2024
902d387
feat: update code
cptbtptpbcptdtptp Jul 19, 2024
4da9b99
feat: update code
cptbtptpbcptdtptp Jul 19, 2024
a4ed5b6
feat: update code
cptbtptpbcptdtptp Jul 19, 2024
b240cbb
feat: update code
cptbtptpbcptdtptp Jul 19, 2024
8e5fe39
feat: update code
cptbtptpbcptdtptp Jul 19, 2024
228b3de
feat: update code
cptbtptpbcptdtptp Jul 19, 2024
573b429
feat: update code
cptbtptpbcptdtptp Jul 19, 2024
bce8fbb
feat: update code
cptbtptpbcptdtptp Jul 19, 2024
472fc6d
feat: update code
cptbtptpbcptdtptp Jul 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions packages/loader/src/PrefabLoader.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import {
AssetPromise,
AssetType,
Engine,
EngineConfiguration,
Loader,
LoadItem,
resourceLoader,
ResourceManager
} from "@galacean/engine-core";
import { AssetPromise, AssetType, Loader, LoadItem, resourceLoader, ResourceManager } from "@galacean/engine-core";
import { PrefabParser } from "./prefab/PrefabParser";
import { PrefabResource } from "./prefab/PrefabResource";
import { IHierarchyFile } from "./resource-deserialize";
import { PrefabParser } from "./prefab/PrefabParser";

@resourceLoader(AssetType.Prefab, ["prefab"])
export class PrefabLoader extends Loader<PrefabResource> {
Expand Down
1 change: 0 additions & 1 deletion packages/loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ export { KTX2Loader, KTX2Transcoder } from "./ktx2/KTX2Loader";
export { KTX2TargetFormat } from "./ktx2/KTX2TargetFormat";
export * from "./resource-deserialize";
export * from "./prefab/PrefabResource";
export { PrefabLoader } from "./PrefabLoader";
6 changes: 5 additions & 1 deletion packages/loader/src/resource-deserialize/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Engine } from "@galacean/engine-core";
import { BufferReader } from "./utils/BufferReader";
import { decoderMap, decoder } from "./utils/Decorator";
import { decoderMap } from "./utils/Decorator";
import { FileHeader } from "./utils/FileHeader";

export { MeshDecoder } from "./resources/mesh/MeshDecoder";
Expand Down Expand Up @@ -29,3 +29,7 @@ export * from "./resources/scene/SceneParser";
export * from "./resources/scene/MeshLoader";
export * from "./resources/scene/EditorTextureLoader";
export * from "./resources/parser/ParserContext";

export * from "./utils/BufferReader";
export * from "./utils/Decorator";
export * from "./utils/FileHeader";
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Engine, ReferResource, Scene } from "@galacean/engine-core";
import { Engine, Scene } from "@galacean/engine-core";
import type { IScene } from "../schema";
import { HierarchyParser } from "../parser/HierarchyParser";
import { ParserContext, ParserType } from "../parser/ParserContext";
Expand Down
11 changes: 10 additions & 1 deletion packages/xr-webxr/src/WebXRDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ export class WebXRDevice implements IXRDevice {
}

isSupportedFeature(type: XRFeatureType): boolean {
return true;
switch (type) {
case XRFeatureType.HitTest:
case XRFeatureType.PlaneTracking:
return typeof XRPlane !== "undefined";
case XRFeatureType.AnchorTracking:
return typeof XRAnchor !== "undefined";
case XRFeatureType.ImageTracking:
// @ts-ignore
return typeof XRImageTrackingResult !== "undefined";
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
}
}

createPlatformFeature(type: XRFeatureType, ...args: any[]): WebXRFeature {
Expand Down
2 changes: 1 addition & 1 deletion packages/xr-webxr/src/WebXRSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class WebXRSession implements IXRSession {
session.removeEventListener("squeeze", onSessionEvent);
session.removeEventListener("squeezestart", onSessionEvent);
session.removeEventListener("squeezeend", onSessionEvent);
session.addEventListener("end", this._onSessionExit);
session.removeEventListener("end", this._onSessionExit);
this._events.length = 0;
}

Expand Down
64 changes: 20 additions & 44 deletions packages/xr/src/XRManagerExtended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ export class XRManagerExtended extends XRManager {
override sessionManager: XRSessionManager;
/** Camera manager for XR. */
override cameraManager: XRCameraManager;
/** All initialized features at this moment. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opt this comments, Too colloquial!

readonly features: XRFeature[] = [];
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved

/** @internal */
_platformDevice: IXRDevice;

private _origin: Entity;
private _features: XRFeature[];

/**
* The current origin of XR space.
Expand Down Expand Up @@ -60,17 +61,19 @@ export class XRManagerExtended extends XRManager {
override addFeature<T extends new (xrManager: XRManagerExtended, ...args: any[]) => XRFeature>(
type: T,
...args: TFeatureConstructorArguments<T>
): XRFeature | null {
): InstanceType<T> | null {
if (this.sessionManager._platformSession) {
throw new Error("Cannot add feature when the session is initialized.");
}
const { _features: features } = this;
if (!this._platformDevice.isSupportedFeature(XRManagerExtended._featureMap.get(type))) {
throw new Error("The feature is not supported");
}
const { features } = this;
for (let i = 0, n = features.length; i < n; i++) {
const feature = features[i];
if (feature instanceof type) throw new Error("The feature has been added");
if (features[i] instanceof type) throw new Error("The feature has been added");
}
const feature = new type(this, ...args);
this._features.push(feature);
const feature = new type(this, ...args) as InstanceType<T>;
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
features.push(feature);
return feature;
}

Expand All @@ -80,7 +83,7 @@ export class XRManagerExtended extends XRManager {
* @returns The feature which match type
*/
override getFeature<T extends XRFeature>(type: TFeatureConstructor<T>): T | null {
const { _features: features } = this;
const { features } = this;
for (let i = 0, n = features.length; i < n; i++) {
const feature = features[i];
if (feature instanceof type) {
Expand All @@ -89,20 +92,6 @@ export class XRManagerExtended extends XRManager {
}
}

override getFeatures<T extends XRFeature>(type: TFeatureConstructor<T>, out?: T[]): T[] {
if (out) {
out.length = 0;
} else {
out = [];
}
const { _features: features } = this;
for (let i = 0, n = features.length; i < n; i--) {
const feature = features[i];
feature instanceof type && out.push(feature);
}
return out;
}

/**
* Enter XR immersive mode, when you call this method, it will initialize and display the XR virtual world.
* @param sessionMode - The mode of the session
Expand All @@ -120,8 +109,9 @@ export class XRManagerExtended extends XRManager {
return new Promise((resolve, reject) => {
// 1. Check if this xr mode is supported
sessionManager.isSupportedMode(sessionMode).then(() => {
sessionManager.state = XRSessionState.Requesting;
// 2. Initialize session
sessionManager._initialize(sessionMode, this._features).then(() => {
sessionManager._initialize(sessionMode, this.features).then(() => {
autoRun && sessionManager.run();
resolve();
}, reject);
Expand All @@ -145,7 +135,7 @@ export class XRManagerExtended extends XRManager {
* @internal
*/
override _initialize(engine: Engine, xrDevice: IXRDevice): void {
this._features = [];
this.features.length = 0;
GuoLei1990 marked this conversation as resolved.
Show resolved Hide resolved
this._platformDevice = xrDevice;
this.sessionManager = new XRSessionManager(this, engine);
this.inputManager = new XRInputManager(this, engine);
Expand All @@ -161,7 +151,7 @@ export class XRManagerExtended extends XRManager {
sessionManager._onUpdate();
this.inputManager._onUpdate();
this.cameraManager._onUpdate();
const { _features: features } = this;
const { features } = this;
for (let i = 0, n = features.length; i < n; i++) {
const feature = features[i];
feature.enabled && feature._onUpdate();
Expand Down Expand Up @@ -210,7 +200,7 @@ export class XRManagerExtended extends XRManager {
* @internal
*/
_onSessionStop(): void {
const { _features: features } = this;
const { features } = this;
for (let i = 0, n = features.length; i < n; i++) {
const feature = features[i];
feature.enabled && feature._onSessionStop();
Expand All @@ -221,7 +211,7 @@ export class XRManagerExtended extends XRManager {
* @internal
*/
_onSessionInit(): void {
const { _features: features } = this;
const { features } = this;
for (let i = 0, n = features.length; i < n; i++) {
const feature = features[i];
feature.enabled && feature._onSessionInit();
Expand All @@ -233,7 +223,7 @@ export class XRManagerExtended extends XRManager {
*/
_onSessionStart(): void {
this.cameraManager._onSessionStart();
const { _features: features } = this;
const { features } = this;
for (let i = 0, n = features.length; i < n; i++) {
const feature = features[i];
feature.enabled && feature._onSessionStart();
Expand All @@ -245,7 +235,7 @@ export class XRManagerExtended extends XRManager {
*/
_onSessionExit(): void {
this.cameraManager._onSessionExit();
const { _features: features } = this;
const { features } = this;
for (let i = 0, n = features.length; i < n; i++) {
const feature = features[i];
feature.enabled && feature._onSessionExit();
Expand Down Expand Up @@ -285,19 +275,6 @@ declare module "@galacean/engine" {
get origin(): Entity;
set origin(value: Entity);

/**
* Get all initialized features at this moment.
* @param type - The type of the feature
*/
getFeatures<T extends XRFeature>(type: TFeatureConstructor<T>): T[];

/**
* Get all initialized features at this moment.
* @param type - The type of the feature
* @param out - Save all features in `out`
*/
getFeatures<T extends XRFeature>(type: TFeatureConstructor<T>, out: T[]): T[];

/**
* Check if the specified feature is supported.
* @param type - The type of the feature
Expand All @@ -314,15 +291,14 @@ declare module "@galacean/engine" {
addFeature<T extends new (xrManager: XRManagerExtended, ...args: any[]) => XRFeature>(
type: T,
...args: TFeatureConstructorArguments<T>
): XRFeature | null;
): InstanceType<T> | null;

/**
* Get feature which match the type.
* @param type - The type of the feature
* @returns The feature which match type
*/
getFeature<T extends XRFeature>(type: TFeatureConstructor<T>): T | null;
getFeatures<T extends XRFeature>(type: TFeatureConstructor<T>, out?: T[]): T[];
/**
* Enter XR immersive mode, when you call this method, it will initialize and display the XR virtual world.
* @param sessionMode - The mode of the session
Expand Down
12 changes: 4 additions & 8 deletions packages/xr/src/feature/trackable/XRTrackableFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { XRTracked } from "./XRTracked";
/**
* The base class of XR trackable manager.
*/
export abstract class XRTrackableFeature<T extends XRTracked, K extends XRRequestTracking<T>> extends XRFeature<
IXRTrackablePlatformFeature<T, K>
> {
export abstract class XRTrackableFeature<
T extends XRTracked = XRTracked,
K extends XRRequestTracking<T> = XRRequestTracking<T>
> extends XRFeature<IXRTrackablePlatformFeature<T, K>> {
protected static _uuid = 0;

protected _requestTrackings: K[] = [];
Expand Down Expand Up @@ -123,11 +124,6 @@ export abstract class XRTrackableFeature<T extends XRTracked, K extends XRReques
this._requestTrackings.length = this._tracked.length = this._added.length = this._updated.length = this._removed.length = 0;
}

override _onDestroy(): void {
// prettier-ignore
this._requestTrackings.length = this._tracked.length = this._added.length = this._updated.length = this._removed.length = 0;
}

protected _addRequestTracking(requestTracking: K): void {
const { _platformFeature: platformFeature } = this;
if (this._xrManager.sessionManager._platformSession && !platformFeature.canModifyRequestTrackingAfterInit) {
Expand Down
5 changes: 5 additions & 0 deletions packages/xr/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import "./XRManagerExtended";
export { XRPose } from "./XRPose";
// xr feature
export { XRFeature } from "./feature/XRFeature";
export { XRTrackableFeature } from "./feature/trackable/XRTrackableFeature";
export { XRTracked } from "./feature/trackable/XRTracked";
// camera
export { XRCameraManager } from "./feature/camera/XRCameraManager";
// hitTest
Expand Down Expand Up @@ -38,3 +40,6 @@ export { XRFeatureType } from "./feature/XRFeatureType";
export { XRRequestTrackingState } from "./feature/trackable/XRRequestTrackingState";
export { XRInputEventType } from "./input/XRInputEventType";
export { XRTargetRayMode } from "./input/XRTargetRayMode";

export * from "./loader/XRReferenceImageDecoder";
export * from "./loader/XRReferenceImageLoader";
17 changes: 17 additions & 0 deletions packages/xr/src/loader/XRReferenceImageDecoder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BufferReader, Engine, decoder } from "@galacean/engine";
import { XRReferenceImage } from "../feature/trackable/image/XRReferenceImage";

@decoder("XRReferenceImage")
export class XRReferenceImageDecoder {
static decode(engine: Engine, bufferReader: BufferReader): Promise<XRReferenceImage> {
return new Promise((resolve, reject) => {
const physicalWidth = bufferReader.nextFloat32();
bufferReader.nextUint8();
const img = new Image();
img.onload = () => {
resolve(new XRReferenceImage("", img, physicalWidth));
};
img.src = URL.createObjectURL(new window.Blob([bufferReader.nextImagesData(1)[0]]));
});
}
}
Comment on lines +5 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid classes with only static members.

The XRReferenceImageDecoder class contains only static members. It would be better to use a simple function instead.

import { BufferReader, Engine } from "@galacean/engine";
import { XRReferenceImage } from "../feature/trackable/image/XRReferenceImage";

export const decodeXRReferenceImage = (engine: Engine, bufferReader: BufferReader): Promise<XRReferenceImage> => {
  return new Promise((resolve, reject) => {
    const physicalWidth = bufferReader.nextFloat32();
    bufferReader.nextUint8();
    const img = new Image();
    img.onload = () => {
      resolve(new XRReferenceImage("", img, physicalWidth));
    };
    img.src = URL.createObjectURL(new window.Blob([bufferReader.nextImagesData(1)[0]]));
  });
};
Tools
Biome

[error] 5-17: Avoid classes that contain only static members.

Prefer using simple functions instead of classes with only static members.

(lint/complexity/noStaticOnlyClass)

17 changes: 17 additions & 0 deletions packages/xr/src/loader/XRReferenceImageLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { AssetPromise, decode, Loader, LoadItem, resourceLoader, ResourceManager } from "@galacean/engine";
import { XRReferenceImage } from "../feature/trackable/image/XRReferenceImage";

@resourceLoader("XRReferenceImage", [])
export class XRReferenceImageLoader extends Loader<XRReferenceImage> {
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<XRReferenceImage> {
return new AssetPromise((resolve, reject) => {
this.request<ArrayBuffer>(item.url, { ...item, type: "arraybuffer" })
.then((data) => {
decode<XRReferenceImage>(data, resourceManager.engine).then((referenceImage) => {
resolve(referenceImage);
});
})
.catch(reject);
});
}
}
Loading
Loading