tayloraswift / swift-noise

Generate and compose commonly-used procedural noises and distributions, in pure Swift
https://swiftinit.org/docs/swift-noise/noise
Apache License 2.0
116 stars 11 forks source link

“Fatal error: Array index is out of range” crash in `GradientNoise2D` `evaluate(_:Double,_:Double)` #11

Open capnslipp opened 1 year ago

capnslipp commented 1 year ago

I'm getting consistent “Fatal error: Array index is out of range” crashes in GradientNoise2D's evaluate(_ x:Double, _ y:Double) at gradient.swift on line 540.

The issue seems to be that the staticGradientNoise2D.points is initialized to have 32 members on line 354 , then when evaluate(…) is called, base_vertex_index:Int ends up with value of 32 _(Int((2*sample_uv_rel.y - sample_uv_rel.x - Double(a))*0.5 + 1) << 4 with sample_uv_rel = (1, 0) & a = 0Int((2*1 - 0 - Double(0))*0.5 + 1) << 4Int(2*0.5 + 1) << 4Int(2) << 432, on line 457)._

Finally on line 540, the code attempts to iterate in a for loop over GradientNoise2D.points[base_vertex_index ..< base_vertex_index + 4]… so GradientNoise2D.points[32..<36] when GradientNoise2D.points is only 32 elements long.

I don't really know how this noise math is meant to work, so I can't speculate as to what a fix should be other than… missing modulus? IDK.

capnslipp commented 1 year ago

Looks like it's dependent on inputs. This is calling it with x: 80, y: -80, and frequency: 0.05.