Razaekel / noise-rs

Procedural noise generation library for Rust.
Apache License 2.0
854 stars 120 forks source link

Rewrite Vector Implementation #317

Closed Razaekel closed 1 year ago

Razaekel commented 1 year ago

All credit to @Cifram

Macro Implementation

Previously had a general trait for vectors of any number of dimensions, and then had a seperate implemention for 2d, 3d and 4d vectors, which were almost entirely redundant.

Now, there's a big vector_type macro, which takes the name of each dimension and builds a vector type with that number of dimensions. The old Vector trait was removed, as nothing used it directly.

This should reduce the opportunities for bugs, as any change to one type of vector is automatically replicated across all of them.This resulted in significant speed improvements for perlin, perlin surflet and value noise, and minimal effect on the rest.

Scalar Algebra

Adding or subtracting a scalar to a vector adds or subtracts that value to all elements of the vector. In this way, it is equivalent to multiplying or dividing by a scalar.

Sadly this made some type inference hiccups, so this also required putting explicit types in a number of places where they hadn't been needed before.

Replace floor on vectors with floor_to_isize

Nearly every noise function was calling floor, and this compiled down to a slow function call, one for each dimension. The new floor_to_isize function doesn't call the standard floor function, but instead casts the value to an isize, which implicitly truncates. This is much faster. For all the noise functions that need the floored value as a f64 still, they now convert back to f64 after calling floor_to_isize.

This got a significant speed improvement on nearly every noise function.

Add new functionality