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

Make the library compatible with react-native-reanimated #67

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 11 additions & 1 deletion simplex-noise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
// I'm really not sure why this | 0 (basically a coercion to int)
// is making this faster but I get ~5 million ops/sec more on the
// benchmarks across the board or a ~10% speedup.
const fastFloor = (x: number) => Math.floor(x) | 0;
const fastFloor = (x: number) => {
"worklet";

Check failure on line 44 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (12.x)

Strings must use singlequote

Check failure on line 44 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (14.x)

Strings must use singlequote

Check failure on line 44 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Strings must use singlequote
return Math.floor(x) | 0;
}

Check failure on line 46 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (12.x)

Missing semicolon

Check failure on line 46 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (14.x)

Missing semicolon

Check failure on line 46 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Missing semicolon

const grad2 = /*#__PURE__*/ new Float64Array([1, 1,
-1, 1,
Expand Down Expand Up @@ -107,11 +110,13 @@
* @returns {NoiseFunction2D}
*/
export function createNoise2D(random: RandomFn = Math.random): NoiseFunction2D {
"worklet";

Check failure on line 113 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (12.x)

Strings must use singlequote

Check failure on line 113 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (14.x)

Strings must use singlequote

Check failure on line 113 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Strings must use singlequote
const perm = buildPermutationTable(random);
// precalculating this yields a little ~3% performance improvement.
const permGrad2x = new Float64Array(perm).map(v => grad2[(v % 12) * 2]);
const permGrad2y = new Float64Array(perm).map(v => grad2[(v % 12) * 2 + 1]);
return function noise2D(x: number, y: number): number {
"worklet";

Check failure on line 119 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (12.x)

Strings must use singlequote

Check failure on line 119 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (14.x)

Strings must use singlequote

Check failure on line 119 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Strings must use singlequote
// if(!isFinite(x) || !isFinite(y)) return 0;
let n0 = 0; // Noise contributions from the three corners
let n1 = 0;
Expand Down Expand Up @@ -197,12 +202,14 @@
* @returns {NoiseFunction3D}
*/
export function createNoise3D(random: RandomFn = Math.random): NoiseFunction3D {
"worklet";

Check failure on line 205 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (12.x)

Strings must use singlequote

Check failure on line 205 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (14.x)

Strings must use singlequote

Check failure on line 205 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Strings must use singlequote
const perm = buildPermutationTable(random);
// precalculating these seems to yield a speedup of over 15%
const permGrad3x = new Float64Array(perm).map(v => grad3[(v % 12) * 3]);
const permGrad3y = new Float64Array(perm).map(v => grad3[(v % 12) * 3 + 1]);
const permGrad3z = new Float64Array(perm).map(v => grad3[(v % 12) * 3 + 2]);
return function noise3D(x: number, y: number, z: number): number {
"worklet";

Check failure on line 212 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (12.x)

Strings must use singlequote

Check failure on line 212 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (14.x)

Strings must use singlequote

Check failure on line 212 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Strings must use singlequote
let n0, n1, n2, n3; // Noise contributions from the four corners
// Skew the input space to determine which simplex cell we're in
const s = (x + y + z) * F3; // Very nice and simple skew factor for 3D
Expand Down Expand Up @@ -342,13 +349,15 @@
* @returns {NoiseFunction4D}
*/
export function createNoise4D(random: RandomFn = Math.random): NoiseFunction4D {
"worklet";

Check failure on line 352 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (12.x)

Strings must use singlequote

Check failure on line 352 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (14.x)

Strings must use singlequote

Check failure on line 352 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Strings must use singlequote
const perm = buildPermutationTable(random);
// precalculating these leads to a ~10% speedup
const permGrad4x = new Float64Array(perm).map(v => grad4[(v % 32) * 4]);
const permGrad4y = new Float64Array(perm).map(v => grad4[(v % 32) * 4 + 1]);
const permGrad4z = new Float64Array(perm).map(v => grad4[(v % 32) * 4 + 2]);
const permGrad4w = new Float64Array(perm).map(v => grad4[(v % 32) * 4 + 3]);
return function noise4D(x: number, y: number, z: number, w: number): number {
"worklet";

Check failure on line 360 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (12.x)

Strings must use singlequote

Check failure on line 360 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (14.x)

Strings must use singlequote

Check failure on line 360 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Strings must use singlequote
let n0, n1, n2, n3, n4; // Noise contributions from the five corners
// Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
const s = (x + y + z + w) * F4; // Factor for 4D skewing
Expand Down Expand Up @@ -480,6 +489,7 @@
* @private
*/
export function buildPermutationTable(random: RandomFn): Uint8Array {
"worklet";

Check failure on line 492 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (12.x)

Strings must use singlequote

Check failure on line 492 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (14.x)

Strings must use singlequote

Check failure on line 492 in simplex-noise.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Strings must use singlequote
const tableSize = 512;
const p = new Uint8Array(tableSize);
for (let i = 0; i < tableSize / 2; i++) {
Expand Down
Loading