optimize noise function (#338)

* optimize noise function

* format
This commit is contained in:
zxx43
2026-09-14 16:06:30 +08:00
committed by GitHub
parent 60f9352c56
commit 6b455561f4
+14 -4
View File
@@ -29,7 +29,8 @@ import { Vec2, Vec3 } from '../core/math';
* @zh 此类生成噪声纹理。
*/
export class ParticleNoise {
private permutation: number[] = [151, 160, 137, 91, 90, 15,
private static readonly DEFAULT_PERMUTATION: readonly number[] = [
151, 160, 137, 91, 90, 15,
131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23,
190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33,
88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166,
@@ -44,9 +45,19 @@ export class ParticleNoise {
138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
];
private static build512 (src: readonly number[] | number[]): Uint8Array {
const p = new Uint8Array(512);
for (let i = 0; i < 256; i++) { p[256 + i] = p[i] = src[i]; }
return p;
}
private permutation: number[] = ParticleNoise.DEFAULT_PERMUTATION as number[];
private permutation512: Uint8Array = ParticleNoise.build512(ParticleNoise.DEFAULT_PERMUTATION);
constructor (permutation?: number[]) {
if (permutation) {
this.permutation = permutation;
this.permutation = permutation.slice();
this.permutation512 = ParticleNoise.build512(this.permutation);
}
}
@@ -61,8 +72,7 @@ export class ParticleNoise {
* @returns @en Texture pixel generated. @zh 返回生成的噪声纹理值。
*/
public noise (x: number, y: number, z: number, min = 0, max = 1): number {
const p: number[] = new Array(512);
for (let i = 0; i < 256; i++) { p[256 + i] = p[i] = this.permutation[i]; }
const p: Uint8Array = this.permutation512;
const X = Math.floor(x) & 255; // FIND UNIT CUBE THAT
const Y = Math.floor(y) & 255; // CONTAINS POINT.