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

Fix for double use of seed in random Particle shader variables. #55607

Merged

Conversation

RPicster
Copy link
Contributor

@RPicster RPicster commented Dec 3, 2021

This PR fixes #55585

ParticleMaterial generated shaders have one aspect that I observed a couple of times when working with them.

When creating random values using rand_from_seed(alt_seed) the same value was returned and used for different calls of the function. That was because the seed is only changed when the particle is restarted.

That would result in the behaviour of creating the following random values (exemplary):

restart = true
if (restart) {
   position = rand_from_seed(alt_seed) = 1
   rotation = rand_from_seed(alt_seed) = 2
   angle = rand_from_seed(alt_seed) = 3
}
color = rand_from_seed(alt_seed) = 4
speed = rand_from_seed(alt_seed) = 5
transparency = rand_from_seed(alt_seed) = 6

but after the restart was done it would result in:

restart = false
if (restart) {
   position = rand_from_seed(alt_seed) = NOT CALLED
   rotation = rand_from_seed(alt_seed) = NOT CALLED
   angle = rand_from_seed(alt_seed) = NOT CALLED
}
color = rand_from_seed(alt_seed) = 1
speed = rand_from_seed(alt_seed) = 2
transparency = rand_from_seed(alt_seed) = 3

So certain random would appear as being "linked" - in the example above, the random color would be linked to the restart spawn position.

Before this PR:
https://user-images.githubusercontent.com/9423774/144683085-7ac472aa-d61b-479c-b29b-c5189c7ece7c.mp4

After this PR:
https://user-images.githubusercontent.com/9423774/144683228-9715fa93-f550-453d-83cd-0dc41396bcef.mp4

To fix this behavior a second seed value for all restart values was introduce.

Here is a reproduction/test project:
3.4-shader-random-problem.zip

Copy link
Member

@clayjohn clayjohn left a comment

Choose a reason for hiding this comment

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

We discussed this over RocketChat, the implementation looks great. Thanks!

@Calinou Calinou added this to the 4.0 milestone Dec 4, 2021
@Calinou Calinou modified the milestones: 4.0, 3.5 Dec 4, 2021
@akien-mga akien-mga merged commit 7c82db2 into godotengine:3.x Dec 7, 2021
@akien-mga
Copy link
Member

Thanks!

@akien-mga
Copy link
Member

Cherry-picked for 3.4.1.

@RPicster RPicster deleted the 3.x-particle-double-seed-use-fix branch December 7, 2021 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants