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

Update controller loader to enhance stateMachine and fix crossfade bug when dest state has no clip #2228

Merged
merged 1 commit into from
Jul 19, 2024

Conversation

luzhuang
Copy link
Contributor

@luzhuang luzhuang commented Jul 16, 2024

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

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

    • Enhanced Animator functionality to handle additional data for parameters, transitions, and entry transitions.
    • Introduced new exports for Animator-related entities, broadening the animation module's capabilities.
  • Bug Fixes

    • Improved control flow in Animator transitions, ensuring more reliable condition handling.
  • Refactor

    • Updated the Animator's _applyTransition method to return a boolean, improving method reliability and control flow.
  • Documentation

    • Added detailed data structures for state, transition, and condition data in Animator documentation.

@luzhuang luzhuang added the animation Built-in animation system related functions label Jul 16, 2024
@luzhuang luzhuang requested a review from GuoLei1990 July 16, 2024 09:52
Copy link

coderabbitai bot commented Jul 16, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The recent updates enhance the animation module and the AnimatorControllerLoader. The Animator class now returns a boolean from the _applyTransition method, allowing for more precise control flow. Additionally, new exports enrich the animation module, while the AnimatorControllerLoader has been refined to handle extra data for states and transitions. This includes the introduction of new interfaces that enhance data organization and type safety.

Changes

File Change Summary
packages/core/src/animation/Animator.ts Modified _applyTransition method to return a boolean instead of void, affecting control flow.
packages/core/src/animation/index.ts Added exports for AnimatorCondition, AnimatorControllerParameter, and LayerPathMask.
packages/loader/src/AnimatorControllerLoader.ts Added imports and adjusted class logic to handle additional data for parameters, transitions, and conditions. Introduced new interfaces: IStateData, ITransitionData, and IConditionData.

Poem

In the realm of code where logic flows,
The Animator dances, and now it shows,
A boolean truth in transitions it finds,
New states defined with structured minds.
With every change, our features gleam,
In the warren of code, we build our dream.
🐇✨


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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range, codebase verification and nitpick comments (1)
packages/loader/src/AnimatorControllerLoader.ts (1)

Line range hint 28-107: Review of load method in AnimatorControllerLoader.

  1. Data Handling: The method correctly deconstructs and uses the data object to create and configure AnimatorController instances. The handling of layers, states, and transitions appears logically correct.
  2. Promises Handling: The use of promises to asynchronously load clips and resolve them once all are loaded is appropriate. However, the use of //@ts-ignore should be avoided if possible. Proper typing or error handling should be implemented instead.
  3. Error Handling: The catch(reject) at the end is a good practice to handle potential errors in the promise chain.

Consider adding more detailed comments explaining the logic, especially how transitions and states are mapped and managed.

- //@ts-ignore
+ // TODO: Properly type or handle the potential error here.
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2ae6905 and e23d259.

Files selected for processing (3)
  • packages/core/src/animation/Animator.ts (5 hunks)
  • packages/core/src/animation/index.ts (1 hunks)
  • packages/loader/src/AnimatorControllerLoader.ts (4 hunks)
Files skipped from review due to trivial changes (1)
  • packages/core/src/animation/index.ts
Additional comments not posted (3)
packages/loader/src/AnimatorControllerLoader.ts (2)

10-15: Imports and dependencies are correctly added.

The new imports for AnimatorState, AnimatorConditionMode, AnimatorControllerParameterValueType, WrapMode, and AnimatorControllerParameter are correctly added and are necessary for the new features in the AnimatorControllerLoader.


137-167: Definition of interfaces IStateData, ITransitionData, and IConditionData.

The interfaces are well-defined and provide a clear structure for the data expected by the AnimatorControllerLoader. These interfaces will help ensure that the data used throughout the loader is consistent and correctly typed.

  1. Optional Properties: Ensure that the optional properties in these interfaces (id in IStateData and ITransitionData, threshold in IConditionData) are truly optional and handled appropriately in the code.
  2. Documentation: Consider adding comments to these interfaces to explain the purpose of each property, especially for those that might not be immediately obvious to other developers or future maintainers.
packages/core/src/animation/Animator.ts (1)

1167-1174: Review of changes to _applyTransition method.

The method now returns a boolean indicating the success of the transition application. This is a significant change as it affects the control flow within the Animator class.

  1. Correctness: The method now checks if the transition is an exit transition and always returns true in that case, which seems logical. For other cases, it returns the result of _prepareCrossFadeByTransition, which should be verified to ensure it handles all possible states correctly.
  2. Impact on Control Flow: This change in return type should be carefully tested to ensure that it integrates well with the rest of the class and does not introduce unintended side effects.

Consider adding unit tests to cover various scenarios of transitions, especially testing the new boolean return value in different contexts.

Verification successful

Verification of _applyTransition method changes:

The boolean return value of the _applyTransition method is correctly integrated and utilized in the control flow within the Animator class. The return value is used in conditional statements to determine subsequent actions, ensuring logical control flow.

  • Correctness: The method correctly checks if the transition is an exit transition and returns true in that case. For other cases, it returns the result of _prepareCrossFadeByTransition.
  • Impact on Control Flow: The return value is appropriately used to control the flow, as seen in the conditional checks and subsequent actions.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify integration of the new boolean return value in different scenarios.

# Test: Search for usage of `_applyTransition` method. Expect: Proper handling of the return value.
rg --type typescript -A 5 $'_applyTransition'

Length of output: 79


Script:

#!/bin/bash
# Description: Verify integration of the new boolean return value in different scenarios.

# Test: Search for usage of `_applyTransition` method. Expect: Proper handling of the return value.
rg --type ts -A 5 _applyTransition

Length of output: 4005

Comment on lines +120 to +134
private _createTransition(transitionData: ITransitionData, destinationState: AnimatorState): AnimatorStateTransition {
const transition = new AnimatorStateTransition();
transition.duration = transitionData.duration;
transition.offset = transitionData.offset;
transition.exitTime = transitionData.exitTime;
transition.solo = transitionData.solo;
transition.mute = transitionData.mute;
// @ts-ignore
transition._isExit = transitionData.isExit;
transition.destinationState = destinationState;
transitionData.conditions.forEach((conditionData) => {
transition.addCondition(conditionData.mode, conditionData.parameterName, conditionData.threshold);
});
return transition;
}
Copy link

Choose a reason for hiding this comment

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

Review of _createTransition private method.

This method correctly creates and configures AnimatorStateTransition instances based on the provided transitionData. The handling of conditions is robust, ensuring that all conditions are added to the transition.

  1. Typing Issues: The use of //@ts-ignore should be addressed. If the TypeScript compiler emits errors, consider refining the type definitions or handling the potential issues explicitly.
  2. Complexity: The method is straightforward and maintains a low complexity, which is good for maintainability.
- //@ts-ignore
+ // TODO: Address TypeScript typing issues here.
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.

Suggested change
private _createTransition(transitionData: ITransitionData, destinationState: AnimatorState): AnimatorStateTransition {
const transition = new AnimatorStateTransition();
transition.duration = transitionData.duration;
transition.offset = transitionData.offset;
transition.exitTime = transitionData.exitTime;
transition.solo = transitionData.solo;
transition.mute = transitionData.mute;
// @ts-ignore
transition._isExit = transitionData.isExit;
transition.destinationState = destinationState;
transitionData.conditions.forEach((conditionData) => {
transition.addCondition(conditionData.mode, conditionData.parameterName, conditionData.threshold);
});
return transition;
}
private _createTransition(transitionData: ITransitionData, destinationState: AnimatorState): AnimatorStateTransition {
const transition = new AnimatorStateTransition();
transition.duration = transitionData.duration;
transition.offset = transitionData.offset;
transition.exitTime = transitionData.exitTime;
transition.solo = transitionData.solo;
transition.mute = transitionData.mute;
// TODO: Address TypeScript typing issues here.
transition._isExit = transitionData.isExit;
transition.destinationState = destinationState;
transitionData.conditions.forEach((conditionData) => {
transition.addCondition(conditionData.mode, conditionData.parameterName, conditionData.threshold);
});
return transition;
}

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e23d259 and d66f62a.

Files selected for processing (1)
  • packages/loader/src/AnimatorControllerLoader.ts (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/loader/src/AnimatorControllerLoader.ts

@luzhuang luzhuang changed the title feat: update controller loader to enhance stateMachine feat: update controller loader to enhance stateMachine and fix crossfade bug when dest state has no clip Jul 19, 2024
@luzhuang luzhuang force-pushed the feat/animatorControllerLoader branch from 4cb5b08 to b49e319 Compare July 19, 2024 08:56
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Inline review comments failed to post

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d66f62a and 4cb5b08.

Files selected for processing (2)
  • packages/loader/src/AnimatorControllerLoader.ts (4 hunks)
  • packages/loader/src/gltf/parser/GLTFSceneParser.ts (1 hunks)
Additional context used
Biome
packages/loader/src/AnimatorControllerLoader.ts

[error] 129-129: This is an unexpected use of the debugger statement.

Unsafe fix: Remove debugger statement

(lint/suspicious/noDebugger)

Additional comments not posted (11)
packages/loader/src/AnimatorControllerLoader.ts (11)

11-15: LGTM! New imports are necessary for the changes.

The new imports for AnimatorState, AnimatorConditionMode, AnimatorControllerParameterValueType, WrapMode, and AnimatorControllerParameter are appropriate and necessary for the modifications in the file.


28-29: Ensure correct handling of data object.

The destructuring of data to extract layers and parameters is correct. Ensure that the data object always contains these properties.


36-39: Efficient state and transition management.

The introduction of statesMap and transitionsMap for efficient state and transition management is a good practice.


40-56: Correct handling of state data.

The handling of state data, including properties like id, name, speed, wrapMode, etc., is correct. Ensure that all properties are always present in the stateData object.


56-56: Ensure script classes are available.

The addition of state machine scripts using Loader.getClass(script) is correct. Ensure that the script classes are available and correctly loaded.


75-78: Efficient transition creation.

The creation of transitions using the _createTransition method and storing them in transitionsMap is efficient and correct.


80-84: Correctly adding transitions to states.

The addition of transitions to states using statesMap and transitionsMap is correct. Ensure that all transition IDs are valid and present in transitionsMap.


88-92: Correct handling of entry transitions.

The handling of entry transitions using the _createTransition method is correct. Ensure that all entry transition data is valid.


94-98: Correct handling of any transitions.

The handling of any transitions using the _createTransition method is correct. Ensure that all any transition data is valid.


102-107: Correct handling of parameters.

The handling of parameters, including creating instances of AnimatorControllerParameter and adding them to the animatorController, is correct. Ensure that all parameter data is valid.


138-163: LGTM! New interfaces enhance type safety and clarity.

The new interfaces IStateData, ITransitionData, and IConditionData are well-defined and improve the type safety and clarity of the code.

Comments failed to post (2)
packages/loader/src/AnimatorControllerLoader.ts

120-135: Address TypeScript typing issues and remove debugger statement.

The _createTransition method correctly creates and configures AnimatorStateTransition instances. However, the use of //@ts-ignore should be addressed, and the debugger statement should be removed.

-    // @ts-ignore
-    transition._isExit = transitionData.isExit;
-    debugger;
+    // TODO: Address TypeScript typing issues here.
+    transition._isExit = transitionData.isExit;
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 _createTransition(transitionData: ITransitionData, destinationState: AnimatorState): AnimatorStateTransition {
    const transition = new AnimatorStateTransition();
    transition.duration = transitionData.duration;
    transition.offset = transitionData.offset;
    transition.exitTime = transitionData.exitTime;
    transition.solo = transitionData.solo;
    transition.mute = transitionData.mute;
    // TODO: Address TypeScript typing issues here.
    transition._isExit = transitionData.isExit;
    transition.destinationState = destinationState;
    transitionData.conditions.forEach((conditionData) => {
      transition.addCondition(conditionData.mode, conditionData.parameterName, conditionData.threshold);
    });
    return transition;
  }
Tools
Biome

[error] 129-129: This is an unexpected use of the debugger statement.

Unsafe fix: Remove debugger statement

(lint/suspicious/noDebugger)

packages/loader/src/gltf/parser/GLTFSceneParser.ts

37-38: Address TypeScript typing issues and remove //@ts-ignore.

The new line of code marks sceneRoot as a template using glTFResource. The use of //@ts-ignore should be addressed to ensure type safety.

-      // @ts-ignore
-      sceneRoot._markAsTemplate(glTFResource);
+      // TODO: Address TypeScript typing issues here.
+      (sceneRoot as any)._markAsTemplate(glTFResource);
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.

      // TODO: Address TypeScript typing issues here.
      (sceneRoot as any)._markAsTemplate(glTFResource);

@luzhuang luzhuang force-pushed the feat/animatorControllerLoader branch from b49e319 to 5505473 Compare July 19, 2024 08:58
@GuoLei1990 GuoLei1990 merged commit 4171a97 into galacean:dev/1.3 Jul 19, 2024
6 checks passed
@GuoLei1990 GuoLei1990 changed the title feat: update controller loader to enhance stateMachine and fix crossfade bug when dest state has no clip Update controller loader to enhance stateMachine and fix crossfade bug when dest state has no clip Jul 19, 2024
@GuoLei1990 GuoLei1990 added the bug Something isn't working label Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
animation Built-in animation system related functions bug Something isn't working
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

2 participants