-
Notifications
You must be signed in to change notification settings - Fork 335
Caustics effect #1561
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
Open
MartinDRChacon
wants to merge
16
commits into
PintaProject:master
Choose a base branch
from
MartinDRChacon:Feature/caustics/effect
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Caustics effect #1561
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2ac6481
Caustics effect
d8a5a96
Merge branch 'PintaProject:master' into Feature/caustics/effect
MartinDRChacon 62c454d
Removed some redundant methods
b81356a
Moved noise algorithm to its own file
401329d
Merge branch 'PintaProject:master' into Feature/caustics/effect
MartinDRChacon 7dfd6f7
Update name to PerlinNoise3D
4acb20a
Named updated to Time Offset.
b5c73d4
Conversion to the premultiplied alpha.
620ddea
Make code more concise
4b5768d
Revert "Make code more concise"
af0b3af
Revert "Conversion to the premultiplied alpha."
bf4cbff
validated solution of alpha.
f1b07c0
Merge from master
3062f55
In alphabetical order
f355c87
Waves have the same alpha as background
463c759
Revert "Waves have the same alpha as background"
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Copyright (c) 2025 Jerry Huxtable | ||
| // | ||
| // MIT License: http://www.opensource.org/licenses/mit-license.php | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in | ||
| // all copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| // THE SOFTWARE. | ||
| // | ||
| // Ported to Pinta by Martin del Rio | ||
|
|
||
| using System; | ||
|
|
||
| namespace Pinta.Core; | ||
|
|
||
| public sealed class ClassicNoise | ||
| { | ||
| private const int GRADIENT_SIZE = 256; | ||
|
|
||
| private readonly int[] permutation = new int[GRADIENT_SIZE * 2]; | ||
| private readonly double[] gradient_x = new double[GRADIENT_SIZE]; | ||
| private readonly double[] gradient_y = new double[GRADIENT_SIZE]; | ||
| private readonly double[] gradient_z = new double[GRADIENT_SIZE]; | ||
|
|
||
| // The constructor now takes a seed to generate a unique, repeatable noise pattern. | ||
| public ClassicNoise (int seed) | ||
| { | ||
| var randomGen = new Random (seed); | ||
| int[] p = new int[GRADIENT_SIZE]; | ||
|
|
||
| for (int i = 0; i < GRADIENT_SIZE; i++) | ||
| p[i] = i; | ||
|
|
||
| for (int i = 0; i < GRADIENT_SIZE; i++) { | ||
| int j = randomGen.Next (GRADIENT_SIZE); | ||
| (p[i], p[j]) = (p[j], p[i]); | ||
| } | ||
|
|
||
| for (int i = 0; i < GRADIENT_SIZE; i++) { | ||
| permutation[i] = permutation[i + GRADIENT_SIZE] = p[i]; | ||
| double invLen; | ||
| do { | ||
| gradient_x[i] = randomGen.NextDouble () * 2.0 - 1.0; | ||
| gradient_y[i] = randomGen.NextDouble () * 2.0 - 1.0; | ||
| gradient_z[i] = randomGen.NextDouble () * 2.0 - 1.0; | ||
| invLen = gradient_x[i] * gradient_x[i] + gradient_y[i] * gradient_y[i] + gradient_z[i] * gradient_z[i]; | ||
| } while (invLen == 0); | ||
| invLen = 1.0 / Math.Sqrt (invLen); | ||
| gradient_x[i] *= invLen; | ||
| gradient_y[i] *= invLen; | ||
| gradient_z[i] *= invLen; | ||
| } | ||
| } | ||
| private double Grad (int hash, double x, double y, double z) | ||
| { | ||
| int h = hash & (GRADIENT_SIZE - 1); | ||
| return gradient_x[h] * x + gradient_y[h] * y + gradient_z[h] * z; | ||
| } | ||
|
|
||
| public double Noise3 (double x, double y, double z) | ||
| { | ||
| int X = (int) Math.Floor (x) & (GRADIENT_SIZE - 1); | ||
| int Y = (int) Math.Floor (y) & (GRADIENT_SIZE - 1); | ||
| int Z = (int) Math.Floor (z) & (GRADIENT_SIZE - 1); | ||
|
|
||
| x -= Math.Floor (x); | ||
| y -= Math.Floor (y); | ||
| z -= Math.Floor (z); | ||
|
|
||
| double u = PerlinNoise.Fade (x); | ||
| double v = PerlinNoise.Fade (y); | ||
| double w = PerlinNoise.Fade (z); | ||
|
|
||
| int A = permutation[X] + Y; | ||
| int AA = permutation[A] + Z; | ||
| int AB = permutation[A + 1] + Z; | ||
| int B = permutation[X + 1] + Y; | ||
| int BA = permutation[B] + Z; | ||
| int BB = permutation[B + 1] + Z; | ||
|
|
||
| return Mathematics.Lerp ( | ||
| Mathematics.Lerp ( | ||
| Mathematics.Lerp ( | ||
| Grad (permutation[AA], x, y, z), | ||
| Grad (permutation[BA], x - 1, y, z), | ||
| u), | ||
| Mathematics.Lerp ( | ||
| Grad ( | ||
| permutation[AB], | ||
| x, | ||
| y - 1, | ||
| z), | ||
| Grad ( | ||
| permutation[BB], | ||
| x - 1, | ||
| y - 1, | ||
| z), | ||
| u), | ||
| v), | ||
| Mathematics.Lerp ( | ||
| Mathematics.Lerp ( | ||
| Grad ( | ||
| permutation[AA + 1], | ||
| x, | ||
| y, | ||
| z - 1), | ||
| Grad ( | ||
| permutation[BA + 1], | ||
| x - 1, | ||
| y, | ||
| z - 1), | ||
| u), | ||
| Mathematics.Lerp ( | ||
| Grad ( | ||
| permutation[AB + 1], | ||
| x, | ||
| y - 1, | ||
| z - 1), | ||
| Grad ( | ||
| permutation[BB + 1], | ||
| x - 1, | ||
| y - 1, | ||
| z - 1), | ||
| u), | ||
| v), | ||
| w); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Naming-wise, perhaps something like PerlinNoise3D? I worry that ClassicNoise might be too generic
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.
I just changed the name :)