напиши на swift FBM noise 2D
- User
напиши на swift FBM noise 2D
- Assistant
Sure, here's an example of how to write a 2D Fractal Brownian Motion (FBM) noise function in Swift:
swift - User
напиши на swift FBM noise 3D
- Assistant
напиши на swift FBM noise 2D
Sure, here's an example of how to write a 2D Fractal Brownian Motion (FBM) noise function in Swift:
напиши на swift FBM noise 3D
import Foundation
func noise(_ x: Double, _ y: Double) -> Double {
let xi = Int(floor(x))
let yi = Int(floor(y))
let xf = x - Double(xi)
let yf = y - Double(yi)
let u = fade(xf)
let v = fade(yf)
let aa = grad1(xi, yi)
let ba = grad1(xi + 1, yi)
let ab = grad1(xi, yi + 1)
let bb = grad1(xi + 1, yi + 1)
let x1 = lerp(grad2(aa, xf, yf), grad2(ba, xf - 1, yf), u)
let x2 = lerp(grad2(ab, xf, yf - 1), grad2(bb, xf - 1, yf - 1), u)
return lerp(x1, x2, v)
}
func fade(_ t: Double) -> Double {
return t * t * t * (t * (t * 6 - 15) + 10)
}
func lerp(_ a: Double, _ b: Double, _ x: Double) -> Double {
return a + x * (b - a)
}
func grad1(_ x: Int, _ y: Int) -> Double {
let hash = Int(x) * 7807 + Int(y) * 32503
let hashMask = (1 << 15) - 1
return Double((hash & hashMask) * 2 - hashMask) / Double(hashMask)
}
func grad2(_ hash: Double, _ x: Double, _ y: Double) -> Double {
return (hash * x + hash * y)
}
// Example usage
let x = 1.234
let y = 5.678
let result = noise(x, y)
print(result)
This code uses the Perlin noise algorithm to generate FBM noise in 2D. The noise function takes two parameters, x and y, which represent the coordinates in the noise space. The function returns a double value representing the generated noise value for the given coordinates.
The code also includes auxiliary functions fade, lerp, grad1, and grad2 to perform the necessary calculations for generating the noise. You can adjust the implementation of these functions to customize the output of the noise function.
Finally, an example usage of the noise function is shown, where a specific set of coordinates (x = 1.234, y = 5.678) is passed to the function and the resulting noise value is printed.
Certainly! Here's an example of how to write a 3D Fractal Brownian Motion (FBM) noise function in Swift:
import Foundation
func noise(_ x: Double, _ y: Double, _ z: Double) -> Double {
let xi = Int(floor(x))
let yi = Int(floor(y))
let zi = Int(floor(z))
let xf = x - Double(xi)
let yf = y - Double(yi)
let zf = z - Double(zi)
let u = fade(xf)
let v = fade(yf)
let w = fade(zf)
let aaa = grad1(xi, yi, zi)
let baa = grad1(xi + 1, yi, zi)
let aba = grad1(xi, yi + 1, zi)
let bba = grad1(xi + 1, yi + 1, zi)
let aab = grad1(xi, yi, zi + 1)
let bab = grad1(xi + 1, yi, zi + 1)
let abb = grad1(xi, yi + 1, zi + 1)
let bbb = grad1(xi + 1, yi + 1, zi + 1)
let x1 = lerp(grad3(aaa, xf, yf, zf), grad3(baa, xf - 1, yf, zf), u)
let x2 = lerp(grad3(aba, xf, yf - 1, zf), grad3(bba, xf - 1, yf - 1, zf), u)
let y1 = lerp(x1, x2, v)
let x3 = lerp(grad3(aab, xf, yf, zf - 1), grad3(bab, xf - 1, yf, zf - 1), u)
let x4 = lerp(grad3(abb, xf, yf - 1, zf - 1), grad3(bbb, xf - 1, yf - 1, zf - 1), u)
let y2 = lerp(x3, x4, v)
return lerp(y1, y2, w)
}
func fade(_ t: Double) -> Double {
return t * t * t * (t * (t * 6 - 15) + 10)
}
func lerp(_ a: Double, _ b: Double, _ x: Double) -> Double {
return a + x * (b - a)
}
func grad1(_ x: Int, _ y: Int, _ z: Int) -> Double {
let hash = (x & 15) + (y & 15) * 240 + (z & 15) * 3840
let hashMask = (1 << 15) - 1
return Double((hash & hashMask) * 2 - hashMask) / Double(hashMask)
}
func grad3(_ hash: Double, _ x: Double, _ y: Double, _ z: Double) -> Double {
return (hash * x + hash * y + hash * z)
}
// Example usage
let x = 1.234
let y = 5.678
let z = 9.012
let result = noise(x, y, z)
print(result)
Similar to the 2D implementation, the code uses the Perlin noise algorithm to generate FBM noise in 3D. The noise function takes three parameters, x, y, and z, representing the coordinates in the noise space. The function returns a double value representing the generated noise value for the given coordinates.
The code also includes auxiliary functions fade, lerp, grad1, and grad3 to perform the necessary calculations for generating the noise. Again, you can tweak the implementation of these functions to customize the output of the noise function.
Finally, an example usage of the noise function is shown, where a specific set of coordinates (x = 1.234, y = 5.678, z = 9.012) is passed to the function, and the resulting noise value is printed.
Not yet included in a published release.