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

Updated whitenoise shader #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 16 additions & 43 deletions Sources/Inferno/Shaders/Transformation/WhiteNoise.metal
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,22 @@ using namespace metal;

/// A simple function that attempts to generate a random number based on various
/// fixed input parameters.
/// - Parameter offset: A fixed value that controls pseudorandomness.
/// - Parameter position: The position of the pixel we're working with.
/// - Parameter time: The number of elapsed seconds since the shader was created.
/// - Returns: The original pixel color.
float whiteRandom(float offset, float2 position, float time) {
// Pick two numbers that are unlikely to repeat.
float2 nonRepeating = float2(12.9898 * time, 78.233 * time);

// Multiply our texture coordinates by the
// non-repeating numbers, then add them together.
float sum = dot(position, nonRepeating);

// Calculate the sine of our sum to get a range
// between -1 and 1.
float sine = sin(sum);

// Multiply the sine by a big, non-repeating number
// so that even a small change will result in a big
// color jump.
float hugeNumber = sine * 43758.5453 * offset;

// Send back just the numbers after the decimal point.
return fract(hugeNumber);
half4 randomNoise(
float2 position, /// The position in 2D space for which we're generating noise
half4 color, /// The base color that will be modified by the noise
float time /// The current time, used to animate the noise
) {
// Generate a pseudo-random value based on the position and time.
// `dot(position + time, float2(12.9898, 78.233))` computes a dot product which helps in producing a pseudo-random effect.
// `sin(...) * 43758.5453` creates further randomness.
// `fract(...)` ensures that the value stays within the range [0, 1] by returning the fractional part.
float value = fract(sin(dot(position + time, float2(12.9898, 78.233))) * 43758.5453);

// Create a color based on the generated noise value.
// The resulting color is grayscale because all RGB components are set to the same value.
// The alpha channel is set to 1.
// The final result is multiplied by the alpha of the input color to maintain its transparency.
return half4(value, value, value, 1) * color.a;
}

/// A shader that generates dynamic, grayscale noise.
///
/// This works identically to the Rainbow Noise shader, except it uses grayscale
/// rather than rainbow colors.
///
/// - Parameter position: The user-space coordinate of the current pixel.
/// - Parameter color: The current color of the pixel.
/// - Parameter time: The number of elapsed seconds since the shader was created
/// - Returns: The new pixel color.
[[ stitchable ]] half4 whiteNoise(float2 position, half4 color, float time) {
// If it's not transparent…
if (color.a > 0.0h) {
// Make a color where the RGB values are the same
// random number and A is 1; multiply by the
// original alpha to get smooth edges.
return half4(half3(whiteRandom(1.0, position, time)), 1.0h) * color.a;
} else {
// Use the current (transparent) color.
return color;
}
}