10000 Fix texture sheet and gravity bug by GuoLei1990 · Pull Request #2422 · galacean/engine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix texture sheet and gravity bug #2422

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

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

GuoLei1990
Copy link
Member
@GuoLei1990 GuoLei1990 commented Nov 5, 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)

Summary by CodeRabbit

  • New Features

    • Enhanced particle emission logic with integrated gravity effects, improving realism.
    • Updated shader calculations to include random factors in gravity effects.
  • Bug Fixes

    • Streamlined gravity handling within the particle system, ensuring accurate simulation space transformations.
  • Documentation

    • Improved variable naming for clarity in shader code related to texture animation.

These changes collectively enhance the particle system's functionality and realism, offering a more dynamic experience for users.

Copy link
coderabbitai bot commented Nov 5, 2024

Walkthrough

The changes in this pull request enhance the particle system's functionality by integrating gravity effects into various components. The ParticleGenerator class now includes gravity handling in its emission logic and bounding box calculations. The MainModule class has been streamlined to set gravity directly from the scene's physics, while the GLSL shaders have been updated to incorporate randomness into gravity calculations. Additionally, a variable in the texture sheet animation module has been renamed for clarity, but the overall functionality remains unchanged.

Changes

File Path Change Summary
packages/core/src/particle/ParticleGenerator.ts Added imports for ParticleBufferUtils and ParticleCompositeCurve; updated emit and _addNewParticle methods to include gravity handling; adjusted _calculateGeneratorBounds for gravity effects.
packages/core/src/particle/modules/MainModule.ts Removed duplicate import for TransformModifyFlags; removed _gravity variable; updated gravity handling in _updateShaderData and constructor initialization.
packages/core/src/shaderlib/extra/particle.vs.glsl Modified gravityVelocity calculation to include a random factor based on a_Random0.x.
packages/core/src/shaderlib/particle/texture_sheet_animation_module.glsl Renamed variable floorTotalULength to tileRowIndex in computeParticleUV function for clarity.

Possibly related PRs

  • Fix particle velocity over lifetime bug #2387: The changes in the main PR enhance the particle emission logic by incorporating gravity effects, which may interact with the velocity management addressed in this PR, indicating a potential relationship in how particles behave over their lifetime.

Suggested labels

bug, particle

Suggested reviewers

  • zhuxudong
  • MrKou47

🐇 In the world of particles, we play,
With gravity's pull, we dance and sway.
From shaders to modules, we craft and create,
A sprinkle of randomness makes it all great!
So hop along, let the particles fly,
In a whimsical dance, under the sky! 🌌


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@GuoLei1990 GuoLei1990 added the bug Something isn't working label Nov 5, 2024
@GuoLei1990 GuoLei1990 requested a review from MrKou47 November 5, 2024 07:59
Copy link
codecov bot commented Nov 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 69.48%. Comparing base (5d147ae) to head (7db9d0f).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2422      +/-   ##
==========================================
- Coverage   69.50%   69.48%   -0.03%     
==========================================
  Files         522      522              
  Lines       27365    27366       +1     
  Branches     4097     4098       +1     
==========================================
- Hits        19021    19014       -7     
- Misses       6846     6850       +4     
- Partials     1498     1502       +4     
Flag Coverage Δ
unittests 69.48% <100.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@GuoLei1990 GuoLei1990 merged commit ae0aa79 into galacean:main Nov 5, 2024
8 of 9 checks passed
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 and nitpick comments (2)
packages/core/src/shaderlib/extra/particle.vs.glsl (1)

77-77: Consider adding range validation for the random gravity multiplier.

While introducing randomization to gravity is a good approach for natural variation, the current implementation might benefit from ensuring the random multiplier stays within a reasonable range.

Consider clamping or scaling the random value to ensure gravity remains within expected bounds:

-vec3 gravityVelocity = renderer_Gravity * a_Random0.x * age;
+// Ensure gravity multiplier is between 0.5 and 1.5 for controlled variation
+vec3 gravityVelocity = renderer_Gravity * (0.5 + a_Random0.x) * age;

This would prevent extreme variations while still maintaining natural randomness in particle behavior.

packages/core/src/particle/ParticleGenerator.ts (1)

761-761: Clarify the comment for better understanding

The comment // Gravity, unused, size, rotation might be confusing or unclear about the code's intent. It would improve readability to make the comment more descriptive.

Consider updating the comment to clearly describe the purpose of the following code block, such as:

-// Gravity, unused, size, rotation
+// Set gravity modifier based on its mode
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5d147ae and 7db9d0f.

📒 Files selected for processing (4)
  • packages/core/src/particle/ParticleGenerator.ts (3 hunks)
  • packages/core/src/particle/modules/MainModule.ts (2 hunks)
  • packages/core/src/shaderlib/extra/particle.vs.glsl (1 hunks)
  • packages/core/src/shaderlib/particle/texture_sheet_animation_module.glsl (1 hunks)
🔇 Additional comments (5)
packages/core/src/shaderlib/particle/texture_sheet_animation_module.glsl (1)

23-25: LGTM! Variable name change improves clarity.

The renaming from floorTotalULength to tileRowIndex better describes the variable's purpose in calculating tile-based UV coordinates.

Let's verify if similar variable names exist in related shader files:

✅ Verification successful

Variable renaming is isolated and safe

The variable renaming from floorTotalULength to tileRowIndex is contained within the texture sheet animation module and doesn't affect other shader files. The search results confirm that this UV calculation logic is unique to texture_sheet_animation_module.glsl and the renamed variable isn't referenced elsewhere in the particle shader system.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for similar variable names in other shader files
# Look for both old and new variable names to ensure consistency

# Search for related shader files
echo "Searching for related shader files..."
rg -t glsl "floorTotalULength|tileRowIndex" --type glsl

# Search specifically in particle-related shaders
echo "Checking particle-specific shaders..."
fd -e glsl -e vert -e frag . "packages/core/src/shaderlib/particle"

Length of output: 1037


Script:

#!/bin/bash
# Let's check the content of these shader files for any related UV calculation or similar variable names
for file in $(fd -e glsl . "packages/core/src/shaderlib/particle"); do
  echo "=== $file ==="
  # Search for UV-related calculations and variable names
  rg -A 3 -B 3 "uv\.|tileRow|floorTotalULength|TSATillingParams" "$file"
  echo ""
done

Length of output: 4833

packages/core/src/shaderlib/extra/particle.vs.glsl (1)

77-77: Verify integration with ParticleGenerator.

The gravity calculation change looks correct, but we should ensure the ParticleGenerator is providing appropriate random values.

Let's verify the integration:

✅ Verification successful

Integration with ParticleGenerator is correctly implemented

The verification shows that:

  1. The gravity calculation in the shader correctly uses a_Random0.x which is populated by the ParticleGenerator
  2. The ParticleGenerator properly initializes gravity values in _addNewParticle() method based on the MainModule's gravityModifier settings:
    • For constant mode: instanceVertices[offset + 19] = main.gravityModifier.constant
    • For two constants mode: instanceVertices[offset + 19] = main.gravityModifier.evaluate(undefined, main._gravityModifierRand.random())
  3. The MainModule has proper random seed handling for gravity via _gravityModifierRand

The integration is working as expected with proper random value generation and attribute population.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check how a_Random0 is populated in ParticleGenerator
ast-grep --pattern 'class ParticleGenerator {
  $$$
  _addNewParticle($_) {
    $$$
    a_Random0
    $$$
  }
  $$$
}'

# Also check for any gravity-related configurations
rg -A 3 "renderer_Gravity" --type ts

Length of output: 727


Script:

#!/bin/bash
# Let's check for a_Random0 usage in particle-related files
rg -A 5 "a_Random0" --type glsl

# Check ParticleGenerator implementation for random value generation
ast-grep --pattern 'class ParticleGenerator {
  $$$
  _addNewParticle($_) {
    $$$
  }
}'

# Check MainModule for gravity configuration
ast-grep --pattern 'class MainModule {
  $$$
  gravity
  $$$
}'

Length of output: 219


Script:

#!/bin/bash
# Let's check for a_Random0 usage in shader files with different extensions
rg -A 5 "a_Random0" 

# Check ParticleGenerator for random attribute setup
rg -A 10 "ParticleGenerator" --type ts

# Check MainModule gravity implementation
rg -A 10 "class MainModule" --type ts

Length of output: 47396


Script:

#!/bin/bash
# Let's check how random values are generated and used in ParticleGenerator
rg -A 10 "_addNewParticle" --type ts

# Check for Random0 attribute initialization
rg -A 5 "Random0" --type ts --type js

Length of output: 3377


Script:

#!/bin/bash
# Let's check for random value generation in ParticleGenerator
rg -B 5 -A 20 "instanceVertices\[offset \+ \d+\]" --type ts

# Check for gravity-related code in MainModule
rg -B 5 -A 10 "gravityModifier" --type ts

Length of output: 33690

packages/core/src/particle/modules/MainModule.ts (1)

323-323: ⚠️ Potential issue

Verify gravity modifier integration with physics gravity.

The change to use scene's physics gravity directly is architecturally cleaner, making the physics system the single source of truth. However, it's unclear how this integrates with the gravityModifier property that still exists in the class.

Let's verify the gravity modifier usage:

Consider documenting the relationship between physics gravity and gravity modifier in the class documentation, especially since this represents a significant change in how gravity is handled in the particle system.

The PR indicates that tests haven't been added. Given this is a bug fix affecting particle physics behavior, please add tests to verify:

  1. Particle behavior with different physics gravity values
  2. Interaction between physics gravity and gravity modifier
  3. Particle behavior in different simulation spaces (Local vs World)
✅ Verification successful

Let me gather more information about how the gravity modifier is actually applied to the physics gravity.


Let me try a different approach to find how the gravity modifier is applied in the particle system.


Based on the evidence gathered, I can now provide the final response:

The gravity modifier is properly integrated with physics gravity

The gravity modifier is correctly applied in the particle system:

  • The modifier value is used to scale the physics gravity vector in ParticleGenerator.ts
  • The calculation includes proper lifetime coefficients (0.5 * maxLifetime * maxLifetime)
  • Both min/max gravity effects are handled for different curve modes
  • Existing tests in ParticleBoundingBox.test.ts verify the gravity modifier behavior

The architectural change to use scene's physics gravity is sound and maintains compatibility with the existing gravity modifier system. The PR's changes align well with the current implementation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for gravity modifier usage in particle system
rg -A 5 "gravityModifier" --type ts

Length of output: 8276


Script:

#!/bin/bash
# Search for gravity-related shader data updates
ast-grep --pattern 'setVector3($_gravity, $_)'

# Search for gravity usage in shader
rg -A 5 "gravity" --type glsl

Length of output: 107


Script:

#!/bin/bash
# Search for shader data updates in MainModule
ast-grep --pattern '_updateShaderData() {
  $$$
}'

# Look for gravity-related calculations
rg -A 10 "_getExtremeValueFromZero.*gravityModifier" --type ts

Length of output: 1123

packages/core/src/particle/ParticleGenerator.ts (2)

17-17: Import of ParticleBufferUtils is correctly added

The addition of import { ParticleBufferUtils } from "./ParticleBufferUtils"; ensures that the necessary utilities for particle buffer manipulation are available.


27-27: Import of ParticleCompositeCurve is appropriately included

The import statement import { ParticleCompositeCurve } from "./modules/ParticleCompositeCurve"; is necessary for handling gravity modifiers using composite curves.

Comment on lines +762 to +769
switch (main.gravityModifier.mode) {
case ParticleCurveMode.Constant:
instanceVertices[offset + 19] = main.gravityModifier.constant;
break;
case ParticleCurveMode.TwoConstants:
instanceVertices[offset + 19] = main.gravityModifier.evaluate(undefined, main._gravityModifierRand.random());
break;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle all possible ParticleCurveMode cases or add a default case

The switch statement currently handles only ParticleCurveMode.Constant and ParticleCurveMode.TwoConstants. Other possible modes like ParticleCurveMode.Curve and ParticleCurveMode.TwoCurves are not handled, which may result in gravity not being applied correctly when these modes are used.

Consider adding cases for the missing modes or a default case to ensure that all possible ParticleCurveMode values are handled. Here's a suggested update:

 switch (main.gravityModifier.mode) {
   case ParticleCurveMode.Constant:
     instanceVertices[offset + 19] = main.gravityModifier.constant;
     break;
   case ParticleCurveMode.TwoConstants:
     instanceVertices[offset + 19] = main.gravityModifier.evaluate(undefined, main._gravityModifierRand.random());
     break;
+  case ParticleCurveMode.Curve:
+    instanceVertices[offset + 19] = main.gravityModifier.evaluate(0, main._gravityModifierRand.random());
+    break;
+  case ParticleCurveMode.TwoCurves:
+    instanceVertices[offset + 19] = main.gravityModifier.evaluate(0, main._gravityModifierRand.random());
+    break;
+  default:
+    instanceVertices[offset + 19] = 0;
+    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.

Suggested change
switch (main.gravityModifier.mode) {
case ParticleCurveMode.Constant:
instanceVertices[offset + 19] = main.gravityModifier.constant;
break;
case ParticleCurveMode.TwoConstants:
instanceVertices[offset + 19] = main.gravityModifier.evaluate(undefined, main._gravityModifierRand.random());
break;
}
switch (main.gravityModifier.mode) {
case ParticleCurveMode.Constant:
instanceVertices[offset + 19] = main.gravityModifier.constant;
break;
case ParticleCurveMode.TwoConstants:
instanceVertices[offset + 19] = main.gravityModifier.evaluate(undefined, main._gravityModifierRand.random());
break;
case ParticleCurveMode.Curve:
instanceVertices[offset + 19] = main.gravityModifier.evaluate(0, main._gravityModifierRand.random());
break;
case ParticleCurveMode.TwoCurves:
instanceVertices[offset + 19] = main.gravityModifier.evaluate(0, main._gravityModifierRand.random());
break;
default:
instanceVertices[offset + 19] = 0;
break;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

使用最新的1.3.15版本,官网的火焰粒子效果demo,运行报错。退回到1.3.1版本,正常运行。
2 participants
0