-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
Add XR Tool for Galacean Editor #291
Conversation
Caution Review failedThe pull request is closed. WalkthroughThe recent updates to the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant XRManager
participant TrackedObjectManager
participant XRAnchorManager
participant XRImageManager
participant XRPlaneManager
User->>XRManager: Initialize XR session
XRManager->>TrackedObjectManager: Manage tracked objects
TrackedObjectManager->>XRAnchorManager: Track anchors
TrackedObjectManager->>XRImageManager: Track images
TrackedObjectManager->>XRPlaneManager: Track planes
XRManager->>User: XR session ready
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
rollup.config.mjs
Outdated
@@ -71,7 +71,8 @@ function makeRollupConfig(pkg) { | |||
Object.assign({}, pkg.pkgJson.dependencies, pkg.pkgJson.peerDependencies, pkg.pkgJson.devDependencies) | |||
); | |||
let globals = { | |||
"@galacean/engine": "Galacean" | |||
"@galacean/engine": "Galacean", | |||
"@galacean/engine-xr": "GalaceanXR" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Galacean.XR,按照 PhysX 这类方式命名 Global name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 11
Outside diff range, codebase verification and nitpick comments (2)
packages/xr/src/trackable/XRImageManager.ts (1)
10-10
: Consider renaming the property for clarity.The property
trackingImages
could be renamed totrackedImages
for better clarity and consistency with the class name.- trackingImages: XRReferenceImage[] = []; + trackedImages: XRReferenceImage[] = [];packages/xr/README.md (1)
11-13
: Show command output.According to Markdownlint, dollar signs should be used to show command output.
- $ npm install @galacean/engine-toolkit-xr + $ npm install @galacean/engine-toolkit-xr + # Output: ...Tools
Markdownlint
12-12: null
Dollar signs used before commands without showing output(MD014, commands-show-output)
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (12)
- packages/xr/README.md (1 hunks)
- packages/xr/package.json (1 hunks)
- packages/xr/src/component/TrackedComponent.ts (1 hunks)
- packages/xr/src/component/XROrigin.ts (1 hunks)
- packages/xr/src/index.ts (1 hunks)
- packages/xr/src/trackable/XRAnchorManager.ts (1 hunks)
- packages/xr/src/trackable/XRImageManager.ts (1 hunks)
- packages/xr/src/trackable/XRPlaneManager.ts (1 hunks)
- packages/xr/src/trackable/XRTrackedObjectManager.ts (1 hunks)
- packages/xr/tsconfig.json (1 hunks)
- rollup.config.mjs (1 hunks)
- rollup.miniprogram.plugin.mjs (1 hunks)
Files skipped from review due to trivial changes (2)
- packages/xr/src/index.ts
- packages/xr/tsconfig.json
Additional context used
Markdownlint
packages/xr/README.md
12-12: null
Dollar signs used before commands without showing output(MD014, commands-show-output)
Biome
packages/xr/src/component/XROrigin.ts
[error] 25-25: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.The declaration is defined in this switch clause:
Unsafe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
[error] 34-34: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.The declaration is defined in this switch clause:
Unsafe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
[error] 40-40: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.The declaration is defined in this switch clause:
Unsafe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
packages/xr/src/trackable/XRTrackedObjectManager.ts
[error] 59-59: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.The declaration is defined in this switch clause:
Unsafe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
Additional comments not posted (32)
packages/xr/src/trackable/XRImageManager.ts (2)
12-14
: Constructor looks good.The constructor correctly initializes the base class with the entity and
XRImageTracking
.
16-21
: Ensure error handling covers all edge cases.The
_initXRFeature
method correctly tries to add theXRImageTracking
feature and logs an error if it fails. Ensure that all potential edge cases are covered in the error handling.packages/xr/src/trackable/XRPlaneManager.ts (2)
12-14
: Constructor looks good.The constructor correctly initializes the base class with the entity and
XRPlaneTracking
.
16-21
: Ensure error handling covers all edge cases.The
_initXRFeature
method correctly tries to add theXRPlaneTracking
feature and logs an error if it fails. Ensure that all potential edge cases are covered in the error handling.Verification successful
Error handling in
_initXRFeature
methods is adequate.The
_initXRFeature
methods inXRPlaneManager.ts
,XRAnchorManager.ts
, andXRImageManager.ts
all use try-catch blocks to handle errors, ensuring that any exceptions thrown during the addition of features are logged appropriately. This approach covers potential edge cases effectively.
packages/xr/src/trackable/XRPlaneManager.ts
packages/xr/src/trackable/XRAnchorManager.ts
packages/xr/src/trackable/XRImageManager.ts
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that all potential edge cases are covered in the error handling. # Test: Search for the usage of `_initXRFeature` method. Expect: Proper error handling in all cases. rg --type python -A 5 $'_initXRFeature'Length of output: 69
Script:
#!/bin/bash # Description: Verify that all potential edge cases are covered in the error handling. # Test: Search for the usage of `_initXRFeature` method. Expect: Proper error handling in all cases. rg --type ts -A 5 '_initXRFeature'Length of output: 2584
Script:
#!/bin/bash # Description: Verify that all potential edge cases are covered in the error handling. # Test: Search for all instances of error handling within the _initXRFeature methods. rg --type ts 'try {' -A 5Length of output: 101
Script:
#!/bin/bash # Description: Verify that all potential edge cases are covered in the error handling. # Test: Search for all instances of error handling within the _initXRFeature methods. rg --type ts 'try \{' -A 5Length of output: 1429
packages/xr/src/component/TrackedComponent.ts (3)
19-21
: Setter method looks good.The setter method for
_data
correctly assigns the value.
30-32
: Setter method looks good.The setter method for
_destroyedOnRemoval
correctly assigns the value.
10-10
: Consider providing a default value for_destroyedOnRemoval
.The
_destroyedOnRemoval
property is set to true by default. Consider providing a default value in the declaration itself.- private _destroyedOnRemoval = true; + private _destroyedOnRemoval: boolean = true;Likely invalid or redundant comment.
packages/xr/package.json (6)
1-3
: Ensure the package versioning strategy aligns with semantic versioning.The version "1.3.0-beta.0" indicates a beta release. Ensure that this versioning strategy aligns with your overall release plan and semantic versioning.
4-7
: Verify the publish configuration.The
publishConfig
specifies public access and the npm registry. Ensure this is intended and aligns with your publishing strategy.
8-14
: Review the repository and issue tracking URLs.Ensure that the
homepage
,repository
, andbugs
URLs are correct and accessible.
15-18
: Review the scripts for building and linting.The
b:types
andlint:fix
scripts are defined. Ensure these scripts are correct and sufficient for your build and linting processes.
19-26
: Validate the entry points and included files.The
types
,main
,module
, andfiles
fields specify the entry points and included files. Ensure these paths are correct and align with your project structure.
27-30
: Check peer dependencies compatibility.The
peerDependencies
specify versions for@galacean/engine
and@galacean/engine-xr
. Ensure these versions are compatible with your project and other dependencies.packages/xr/src/trackable/XRAnchorManager.ts (4)
1-3
: Ensure all imported modules are necessary.The imports include
Entity
,Logger
,Quaternion
,Vector3
,XRAnchor
,XRAnchorTracking
, andXRTrackedObjectManager
. Ensure all these imports are necessary for the functionality of theXRAnchorManager
class.
8-10
: Ensure proper initialization of class properties.The
anchors
property is initialized as an empty array. Ensure this initialization aligns with the intended functionality.
12-14
: Constructor looks good.The constructor initializes the class with the entity and XRAnchorTracking. Ensure this initialization aligns with the intended functionality.
16-28
: Ensure proper error handling in_initXRFeature
method.The
_initXRFeature
method sets up the XR feature and handles errors. Ensure the error handling is sufficient and consider logging additional context if necessary.rollup.miniprogram.plugin.mjs (1)
45-45
: Modification looks good.The
regStr
constant is modified to include'@galacean/engine-xr'
. Ensure this modification aligns with the intended functionality and does not introduce any unintended side effects.packages/xr/README.md (4)
3-5
: LGTM!The feature description is clear and concise.
23-34
: LGTM!The usage example is clear and provides a good overview of how to use the tool.
36-40
: LGTM!The links are relevant and useful.
42-44
: LGTM!The license information is clear and provides a link to the MIT license.
packages/xr/src/component/XROrigin.ts (5)
8-9
: LGTM!The property
mode
is correctly defined and initialized.
10-11
: LGTM!The property
origin
is correctly defined.
12-13
: LGTM!The property
camera
is correctly defined.
14-15
: LGTM!The property
leftCamera
is correctly defined.
16-17
: LGTM!The property
rightCamera
is correctly defined.rollup.config.mjs (3)
74-75
: LGTM!The addition of the
@galacean/engine-xr
package to theglobals
object is correct and aligns with the intention to support the package.
Line range hint
66-73
:
LGTM!The function
makeRollupConfig
is well-structured and correctly defines the configuration.Also applies to: 76-103
Line range hint
105-105
:
LGTM!The default export is correctly defined.
packages/xr/src/trackable/XRTrackedObjectManager.ts (2)
42-47
: EnsurexrManager
is defined before accessing its properties.The method
onDisable
assumesxrManager
is defined. Add a check to avoid potential runtime errors.- const { xrManager } = this.engine; - if (!xrManager) return; + const { xrManager } = this.engine; + if (xrManager) { + xrManager.getFeature(this._feature)?.removeChangedListener(this._onTrackedChanged); + xrManager.sessionManager.removeStateChangedListener(this._onSessionChanged); + }Likely invalid or redundant comment.
33-36
: Ensure proper handling ofundefined
index.The method
getTrackedComponentByTrackId
returnsundefined
if the index is not found. Ensure that the calling code handles this case properly.Verification successful
Proper handling of
undefined
index verified.The method
getTrackedComponentByTrackId
properly handles theundefined
case by checking iftrackedComponent
is falsy (if (!trackedComponent)
).
packages/xr/src/trackable/XRTrackedObjectManager.ts
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify all function calls to `getTrackedComponentByTrackId` handle the `undefined` case. # Test: Search for the function usage. Expect: Proper handling of `undefined` return value. rg --type typescript -A 5 $'getTrackedComponentByTrackId'Length of output: 91
Script:
#!/bin/bash # Description: Verify all function calls to `getTrackedComponentByTrackId` handle the `undefined` case. # Test: Search for the function usage. Expect: Proper handling of `undefined` return value. rg --type ts -A 5 $'getTrackedComponentByTrackId'Length of output: 1315
*/ | ||
export class XRPlaneManager extends XRTrackedObjectManager<XRTrackedPlane> { | ||
/** The plane detection mode. */ | ||
detectionMode: XRPlaneMode = XRPlaneMode.EveryThing; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider making the detection mode configurable.
The property detectionMode
is set to XRPlaneMode.EveryThing
by default. Consider making this configurable through the constructor or a setter method.
- detectionMode: XRPlaneMode = XRPlaneMode.EveryThing;
+ detectionMode: XRPlaneMode;
+ constructor(entity: Entity, detectionMode: XRPlaneMode = XRPlaneMode.EveryThing) {
+ super(entity, XRPlaneTracking);
+ this.detectionMode = detectionMode;
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
detectionMode: XRPlaneMode = XRPlaneMode.EveryThing; | |
detectionMode: XRPlaneMode; | |
constructor(entity: Entity, detectionMode: XRPlaneMode = XRPlaneMode.EveryThing) { | |
super(entity, XRPlaneTracking); | |
this.detectionMode = detectionMode; | |
} |
constructor(entity: Entity, feature: any) { | ||
super(entity); | ||
this._feature = feature; | ||
this._onTrackedChanged = this._onTrackedChanged.bind(this); | ||
this._onSessionChanged = this._onSessionChanged.bind(this); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using any
type for the feature
parameter.
Using any
defeats the purpose of TypeScript. Consider defining a specific type or a more generic type.
- constructor(entity: Entity, feature: any) {
+ constructor(entity: Entity, feature: TFeatureConstructor<XRTrackableFeature>) {
Committable suggestion was skipped due to low confidence.
protected _initXRFeature(): void { | ||
throw new Error("You need to override this method."); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider making _initXRFeature
an abstract method.
The method _initXRFeature
is meant to be overridden. Consider making it abstract to enforce this.
- protected _initXRFeature(): void {
- throw new Error("You need to override this method.");
+ protected abstract _initXRFeature(): void;
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
protected _initXRFeature(): void { | |
throw new Error("You need to override this method."); | |
} | |
protected abstract _initXRFeature(): void; |
* If you set an Entity, the entity.Clone() will be used. | ||
* otherwise, a new empty Entity will be used instead | ||
*/ | ||
prefab: GLTFResource | PrefabResource | Entity; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure proper initialization of prefab
.
The prefab
property is not initialized in the constructor. Consider initializing it to a default value to avoid potential undefined
issues.
- prefab: GLTFResource | PrefabResource | Entity;
+ prefab: GLTFResource | PrefabResource | Entity = null;
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
prefab: GLTFResource | PrefabResource | Entity; | |
prefab: GLTFResource | PrefabResource | Entity = null; |
override onEnable(): void { | ||
this.engine.xrManager?.sessionManager.addStateChangedListener(this._onSessionChanged); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure xrManager
is defined.
The method onEnable
assumes xrManager
is defined. Add a check to avoid potential runtime errors.
- this.engine.xrManager?.sessionManager.addStateChangedListener(this._onSessionChanged);
+ const { xrManager } = this.engine;
+ if (xrManager) {
+ xrManager.sessionManager.addStateChangedListener(this._onSessionChanged);
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
override onEnable(): void { | |
this.engine.xrManager?.sessionManager.addStateChangedListener(this._onSessionChanged); | |
} | |
override onEnable(): void { | |
const { xrManager } = this.engine; | |
if (xrManager) { | |
xrManager.sessionManager.addStateChangedListener(this._onSessionChanged); | |
} | |
} |
private _createTrackedComponents(sessionRelativeData: T): TrackedComponent<T> { | ||
const { origin } = this._engine.xrManager; | ||
const { prefab } = this; | ||
let entity: Entity; | ||
if (!prefab) { | ||
entity = origin.createChild("TrackedImage" + sessionRelativeData.id); | ||
} else { | ||
if (prefab instanceof GLTFResource) { | ||
entity = prefab.instantiateSceneRoot(); | ||
} else if (prefab instanceof PrefabResource) { | ||
entity = prefab.instantiate(); | ||
} else { | ||
entity = prefab.clone(); | ||
} | ||
entity.name = "TrackedImage" + sessionRelativeData.id; | ||
origin.addChild(entity); | ||
} | ||
|
||
const trackedComponent = entity.addComponent(TrackedComponent<T>); | ||
return trackedComponent; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optimize entity creation in _createTrackedComponents
.
The method _createTrackedComponents
can be optimized by reducing redundant code.
- if (!prefab) {
- entity = origin.createChild("TrackedImage" + sessionRelativeData.id);
- } else {
- if (prefab instanceof GLTFResource) {
- entity = prefab.instantiateSceneRoot();
- } else if (prefab instanceof PrefabResource) {
- entity = prefab.instantiate();
- } else {
- entity = prefab.clone();
- }
- entity.name = "TrackedImage" + sessionRelativeData.id;
- origin.addChild(entity);
- }
+ entity = prefab ?
+ (prefab instanceof GLTFResource ? prefab.instantiateSceneRoot() :
+ (prefab instanceof PrefabResource ? prefab.instantiate() : prefab.clone())) :
+ origin.createChild("TrackedImage" + sessionRelativeData.id);
+ entity.name = "TrackedImage" + sessionRelativeData.id;
+ origin.addChild(entity);
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
private _createTrackedComponents(sessionRelativeData: T): TrackedComponent<T> { | |
const { origin } = this._engine.xrManager; | |
const { prefab } = this; | |
let entity: Entity; | |
if (!prefab) { | |
entity = origin.createChild("TrackedImage" + sessionRelativeData.id); | |
} else { | |
if (prefab instanceof GLTFResource) { | |
entity = prefab.instantiateSceneRoot(); | |
} else if (prefab instanceof PrefabResource) { | |
entity = prefab.instantiate(); | |
} else { | |
entity = prefab.clone(); | |
} | |
entity.name = "TrackedImage" + sessionRelativeData.id; | |
origin.addChild(entity); | |
} | |
const trackedComponent = entity.addComponent(TrackedComponent<T>); | |
return trackedComponent; | |
} | |
private _createTrackedComponents(sessionRelativeData: T): TrackedComponent<T> { | |
const { origin } = this._engine.xrManager; | |
const { prefab } = this; | |
let entity: Entity; | |
entity = prefab ? | |
(prefab instanceof GLTFResource ? prefab.instantiateSceneRoot() : | |
(prefab instanceof PrefabResource ? prefab.instantiate() : prefab.clone())) : | |
origin.createChild("TrackedImage" + sessionRelativeData.id); | |
entity.name = "TrackedImage" + sessionRelativeData.id; | |
origin.addChild(entity); | |
const trackedComponent = entity.addComponent(TrackedComponent<T>); | |
return trackedComponent; | |
} |
private _onSessionChanged(state: XRSessionState): void { | ||
switch (state) { | ||
case XRSessionState.Initializing: | ||
this._initXRFeature(); | ||
break; | ||
case XRSessionState.Initialized: | ||
const feature = this._engine.xrManager.getFeature(this._feature); | ||
if (feature) { | ||
feature.addChangedListener(this._onTrackedChanged); | ||
} else { | ||
Logger.error("XRTrackedObjectManager: Feature ", this._feature.name, " not found"); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix scope of feature
declaration in _onSessionChanged
.
The feature
variable is declared inside a switch case, which can lead to scope issues. Move the declaration outside the switch statement.
- switch (state) {
+ let feature;
+ switch (state) {
- case XRSessionState.Initialized:
- const feature = this._engine.xrManager.getFeature(this._feature);
+ case XRSessionState.Initialized:
+ feature = this._engine.xrManager.getFeature(this._feature);
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
private _onSessionChanged(state: XRSessionState): void { | |
switch (state) { | |
case XRSessionState.Initializing: | |
this._initXRFeature(); | |
break; | |
case XRSessionState.Initialized: | |
const feature = this._engine.xrManager.getFeature(this._feature); | |
if (feature) { | |
feature.addChangedListener(this._onTrackedChanged); | |
} else { | |
Logger.error("XRTrackedObjectManager: Feature ", this._feature.name, " not found"); | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
private _onSessionChanged(state: XRSessionState): void { | |
let feature; | |
switch (state) { | |
case XRSessionState.Initializing: | |
this._initXRFeature(); | |
break; | |
case XRSessionState.Initialized: | |
feature = this._engine.xrManager.getFeature(this._feature); | |
if (feature) { | |
feature.addChangedListener(this._onTrackedChanged); | |
} else { | |
Logger.error("XRTrackedObjectManager: Feature ", this._feature.name, " not found"); | |
} | |
break; | |
default: | |
break; | |
} | |
} |
Tools
Biome
[error] 59-59: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.The declaration is defined in this switch clause:
Unsafe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
private _createOrUpdateTrackedComponents(sessionRelativeData: T): TrackedComponent<T> { | ||
let trackedComponent = this.getTrackedComponentByTrackId(sessionRelativeData.id); | ||
if (!trackedComponent) { | ||
const { _trackIdToIndex: trackIdToIndex, _trackedComponents: trackedComponents } = this; | ||
trackedComponent = this._createTrackedComponents(sessionRelativeData); | ||
trackIdToIndex[sessionRelativeData.id] = trackedComponents.length; | ||
trackedComponents.push(trackedComponent); | ||
} | ||
trackedComponent.data = sessionRelativeData; | ||
const { transform } = trackedComponent.entity; | ||
const { pose } = sessionRelativeData; | ||
transform.position = pose.position; | ||
transform.rotationQuaternion = pose.rotation; | ||
return trackedComponent; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure sessionRelativeData
has valid pose
property.
The method _createOrUpdateTrackedComponents
assumes sessionRelativeData
has a valid pose
property. Add checks to avoid potential runtime errors.
- const { pose } = sessionRelativeData;
- transform.position = pose.position;
- transform.rotationQuaternion = pose.rotation;
+ if (sessionRelativeData.pose) {
+ const { pose } = sessionRelativeData;
+ transform.position = pose.position;
+ transform.rotationQuaternion = pose.rotation;
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
private _createOrUpdateTrackedComponents(sessionRelativeData: T): TrackedComponent<T> { | |
let trackedComponent = this.getTrackedComponentByTrackId(sessionRelativeData.id); | |
if (!trackedComponent) { | |
const { _trackIdToIndex: trackIdToIndex, _trackedComponents: trackedComponents } = this; | |
trackedComponent = this._createTrackedComponents(sessionRelativeData); | |
trackIdToIndex[sessionRelativeData.id] = trackedComponents.length; | |
trackedComponents.push(trackedComponent); | |
} | |
trackedComponent.data = sessionRelativeData; | |
const { transform } = trackedComponent.entity; | |
const { pose } = sessionRelativeData; | |
transform.position = pose.position; | |
transform.rotationQuaternion = pose.rotation; | |
return trackedComponent; | |
} | |
private _createOrUpdateTrackedComponents(sessionRelativeData: T): TrackedComponent<T> { | |
let trackedComponent = this.getTrackedComponentByTrackId(sessionRelativeData.id); | |
if (!trackedComponent) { | |
const { _trackIdToIndex: trackIdToIndex, _trackedComponents: trackedComponents } = this; | |
trackedComponent = this._createTrackedComponents(sessionRelativeData); | |
trackIdToIndex[sessionRelativeData.id] = trackedComponents.length; | |
trackedComponents.push(trackedComponent); | |
} | |
trackedComponent.data = sessionRelativeData; | |
const { transform } = trackedComponent.entity; | |
if (sessionRelativeData.pose) { | |
const { pose } = sessionRelativeData; | |
transform.position = pose.position; | |
transform.rotationQuaternion = pose.rotation; | |
} | |
return trackedComponent; | |
} |
private _data: T; | ||
private _destroyedOnRemoval = true; | ||
|
||
/** | ||
* Tracking data of the TrackedObject. | ||
*/ | ||
get data(): T { | ||
return this._data; | ||
} | ||
|
||
set data(value: T) { | ||
this._data = value; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider making _data
property private.
The _data
property is currently private but exposed through getter and setter methods. Consider making it truly private by using TypeScript's private modifier.
- private _data: T;
+ #_data: T;
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
private _data: T; | |
private _destroyedOnRemoval = true; | |
/** | |
* Tracking data of the TrackedObject. | |
*/ | |
get data(): T { | |
return this._data; | |
} | |
set data(value: T) { | |
this._data = value; | |
} | |
#_data: T; | |
private _destroyedOnRemoval = true; | |
/** | |
* Tracking data of the TrackedObject. | |
*/ | |
get data(): T { | |
return this.#_data; | |
} | |
set data(value: T) { | |
this.#_data = value; | |
} |
override onEnable(): void { | ||
const { xrManager } = this.engine; | ||
if (!xrManager) return; | ||
switch (this.mode) { | ||
case XRSessionMode.AR: | ||
xrManager.origin = this.origin; | ||
const camera = this.camera?.getComponent(Camera); | ||
if (camera) { | ||
xrManager.cameraManager.attachCamera(XRTrackedInputDevice.Camera, camera); | ||
} else { | ||
throw new Error("XROrigin: The Camera is not included on the Camera Entity"); | ||
} | ||
break; | ||
case XRSessionMode.VR: | ||
xrManager.origin = this.origin; | ||
const leftCamera = this.leftCamera?.getComponent(Camera); | ||
if (leftCamera) { | ||
xrManager.cameraManager.attachCamera(XRTrackedInputDevice.LeftCamera, leftCamera); | ||
} else { | ||
throw new Error("XROrigin: The Camera is not included on the Left Camera Entity"); | ||
} | ||
const rightCamera = this.rightCamera?.getComponent(Camera); | ||
if (rightCamera) { | ||
xrManager.cameraManager.attachCamera(XRTrackedInputDevice.RightCamera, rightCamera); | ||
} else { | ||
throw new Error("XROrigin: The Camera is not included on the Right Camera Entity"); | ||
} | ||
break; | ||
default: | ||
break; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap declarations in blocks to restrict access.
Static analysis tools suggest wrapping declarations in blocks to restrict access to the switch clauses.
- switch (this.mode) {
- case XRSessionMode.AR:
- xrManager.origin = this.origin;
- const camera = this.camera?.getComponent(Camera);
- if (camera) {
- xrManager.cameraManager.attachCamera(XRTrackedInputDevice.Camera, camera);
- } else {
- throw new Error("XROrigin: The Camera is not included on the Camera Entity");
- }
- break;
- case XRSessionMode.VR:
- xrManager.origin = this.origin;
- const leftCamera = this.leftCamera?.getComponent(Camera);
- if (leftCamera) {
- xrManager.cameraManager.attachCamera(XRTrackedInputDevice.LeftCamera, leftCamera);
- } else {
- throw new Error("XROrigin: The Camera is not included on the Left Camera Entity");
- }
- const rightCamera = this.rightCamera?.getComponent(Camera);
- if (rightCamera) {
- xrManager.cameraManager.attachCamera(XRTrackedInputDevice.RightCamera, rightCamera);
- } else {
- throw new Error("XROrigin: The Camera is not included on the Right Camera Entity");
- }
- break;
- default:
- break;
- }
+ switch (this.mode) {
+ case XRSessionMode.AR: {
+ xrManager.origin = this.origin;
+ const camera = this.camera?.getComponent(Camera);
+ if (camera) {
+ xrManager.cameraManager.attachCamera(XRTrackedInputDevice.Camera, camera);
+ } else {
+ throw new Error("XROrigin: The Camera is not included on the Camera Entity");
+ }
+ break;
+ }
+ case XRSessionMode.VR: {
+ xrManager.origin = this.origin;
+ const leftCamera = this.leftCamera?.getComponent(Camera);
+ if (leftCamera) {
+ xrManager.cameraManager.attachCamera(XRTrackedInputDevice.LeftCamera, leftCamera);
+ } else {
+ throw new Error("XROrigin: The Camera is not included on the Left Camera Entity");
+ }
+ const rightCamera = this.rightCamera?.getComponent(Camera);
+ if (rightCamera) {
+ xrManager.cameraManager.attachCamera(XRTrackedInputDevice.RightCamera, rightCamera);
+ } else {
+ throw new Error("XROrigin: The Camera is not included on the Right Camera Entity");
+ }
+ break;
+ }
+ default:
+ break;
+ }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
override onEnable(): void { | |
const { xrManager } = this.engine; | |
if (!xrManager) return; | |
switch (this.mode) { | |
case XRSessionMode.AR: | |
xrManager.origin = this.origin; | |
const camera = this.camera?.getComponent(Camera); | |
if (camera) { | |
xrManager.cameraManager.attachCamera(XRTrackedInputDevice.Camera, camera); | |
} else { | |
throw new Error("XROrigin: The Camera is not included on the Camera Entity"); | |
} | |
break; | |
case XRSessionMode.VR: | |
xrManager.origin = this.origin; | |
const leftCamera = this.leftCamera?.getComponent(Camera); | |
if (leftCamera) { | |
xrManager.cameraManager.attachCamera(XRTrackedInputDevice.LeftCamera, leftCamera); | |
} else { | |
throw new Error("XROrigin: The Camera is not included on the Left Camera Entity"); | |
} | |
const rightCamera = this.rightCamera?.getComponent(Camera); | |
if (rightCamera) { | |
xrManager.cameraManager.attachCamera(XRTrackedInputDevice.RightCamera, rightCamera); | |
} else { | |
throw new Error("XROrigin: The Camera is not included on the Right Camera Entity"); | |
} | |
break; | |
default: | |
break; | |
} | |
override onEnable(): void { | |
const { xrManager } = this.engine; | |
if (!xrManager) return; | |
switch (this.mode) { | |
case XRSessionMode.AR: { | |
xrManager.origin = this.origin; | |
const camera = this.camera?.getComponent(Camera); | |
if (camera) { | |
xrManager.cameraManager.attachCamera(XRTrackedInputDevice.Camera, camera); | |
} else { | |
throw new Error("XROrigin: The Camera is not included on the Camera Entity"); | |
} | |
break; | |
} | |
case XRSessionMode.VR: { | |
xrManager.origin = this.origin; | |
const leftCamera = this.leftCamera?.getComponent(Camera); | |
if (leftCamera) { | |
xrManager.cameraManager.attachCamera(XRTrackedInputDevice.LeftCamera, leftCamera); | |
} else { | |
throw new Error("XROrigin: The Camera is not included on the Left Camera Entity"); | |
} | |
const rightCamera = this.rightCamera?.getComponent(Camera); | |
if (rightCamera) { | |
xrManager.cameraManager.attachCamera(XRTrackedInputDevice.RightCamera, rightCamera); | |
} else { | |
throw new Error("XROrigin: The Camera is not included on the Right Camera Entity"); | |
} | |
break; | |
} | |
default: | |
break; | |
} |
Tools
Biome
[error] 25-25: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.The declaration is defined in this switch clause:
Unsafe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
[error] 34-34: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.The declaration is defined in this switch clause:
Unsafe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
[error] 40-40: Other switch clauses can erroneously access this declaration.
Wrap the declaration in a block to restrict its access to the switch clause.The declaration is defined in this switch clause:
Unsafe fix: Wrap the declaration in a block.
(lint/correctness/noSwitchDeclarations)
if (feature) { | ||
feature.addChangedListener(this._onTrackedChanged); | ||
} else { | ||
Logger.error("XRTrackedObjectManager: Feature ", this._feature.name, " not found"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里直接把 异常 console 出来,用 console.warn 应该会好一点
Please check if the PR fulfills these requirements
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
What is the current behavior? (You can also link to an open issue here)
What is the new behavior (if this is a feature change)?
Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)
Other information:
Summary by CodeRabbit
New Features
README.md
for theXR
package, detailing installation, usage examples, and package purpose.@galacean/engine-toolkit-xr
package with essential metadata for integration within the Galacean engine ecosystem.TrackedComponent
,XROrigin
,XRAnchorManager
,XRImageManager
, andXRPlaneManager
classes for managing tracked objects and XR functionalities.index.ts
for exporting core XR components and managers.Bug Fixes
Documentation
README.md
file for users.Chores