Skip to content

Commit

Permalink
Add noise view
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Oct 15, 2024
1 parent a452d20 commit d751371
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ let package = Package(
],
targets: [
.target(
name: "MeshingKit"
name: "MeshingKit",
resources: [
.process("ParameterizedNoise.metal")
]
)
]
)
21 changes: 21 additions & 0 deletions Sources/MeshingKit/ParameterizedNoise.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Shader.metal
// MeshingShared
//
// Created by Rudrank Riyam on 8/9/24.
//

#include <SwiftUI/SwiftUI_Metal.h>
#include <metal_stdlib>
using namespace metal;

[[ stitchable ]]
half4 parameterizedNoise(float2 position, half4 color, float intensity, float frequency, float opacity) {
float value = fract(cos(dot(position * frequency, float2(12.9898, 78.233))) * 43758.5453);

float r = color.r * mix(1.0, value, intensity);
float g = color.g * mix(1.0, value, intensity);
float b = color.b * mix(1.0, value, intensity);

return half4(r, g, b, opacity);
}
61 changes: 61 additions & 0 deletions Sources/MeshingKit/ParameterizedNoiseView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import SwiftUI

/// A view that applies a parameterized noise effect to a MeshGradient.
///
/// Use `ParameterizedNoiseView` to add a customizable noise effect to a `MeshGradient` view.
/// The noise effect is controlled by three parameters: intensity, frequency, and opacity.
///
/// Example usage:
/// ```swift
/// ParameterizedNoiseView(intensity: .constant(0.5), frequency: .constant(0.2), opacity: .constant(0.9)) {
/// MeshingKit.gradientSize3(template: .cosmicAurora)
/// }
/// ```
///
/// - Important: This view requires iOS 18.0, macOS 15.0, tvOS 18.0, watchOS 11.0, or visionOS 2.0 and later.
public struct ParameterizedNoiseView: View {

/// The MeshGradient to which the noise effect is applied.
let gradient: MeshGradient

/// The intensity of the noise effect.
///
/// Values typically range from 0 to 1, where 0 means no effect and 1 means maximum intensity.
@Binding var intensity: Float

/// The frequency of the noise pattern.
///
/// Higher values create a finer, more detailed noise pattern, while lower values create a broader, more spread-out pattern.
@Binding var frequency: Float

/// The opacity of the noise effect.
///
/// Values range from 0 to 1, where 0 is completely transparent and 1 is fully opaque.
@Binding var opacity: Float

/// Creates a new `ParameterizedNoiseView` with the specified parameters and MeshGradient.
///
/// - Parameters:
/// - intensity: A binding to the intensity of the noise effect.
/// - frequency: A binding to the frequency of the noise pattern.
/// - opacity: A binding to the opacity of the noise effect.
/// - content: A closure that returns the MeshGradient to which the noise effect will be applied.
public init(intensity: Binding<Float>, frequency: Binding<Float>, opacity: Binding<Float>, @ViewBuilder content: () -> MeshGradient) {
self._intensity = intensity
self._frequency = frequency
self._opacity = opacity
self.gradient = content()
}

/// The body of the view, applying the noise effect to the MeshGradient.
public var body: some View {
gradient
.colorEffect(
ShaderLibrary.parameterizedNoise(
.float(intensity),
.float(frequency),
.float(opacity)
)
)
}
}

0 comments on commit d751371

Please sign in to comment.