cocos-engine icon indicating copy to clipboard operation
cocos-engine copied to clipboard

Optimize particle noise module by not use a temp array for noise function on every call

Open Yuki001 opened this issue 1 year ago • 0 comments

Use Case

Performance profiler points out noise function cost a lot of time .

Problem Description

the noise function use a temp array to assign noise values every time. but it's not need to do so.

Proposed Solution

using member variables and pre-calculate it. noise.ts :

    constructor (permutation?: number[]) {
        if (permutation) {
            this.permutation = permutation;
            const p: number[] = new Array(512);
            for (let i = 0; i < 256; i++) { p[256 + i] = p[i] = this.permutation[i]; }
            this.permutation_2 = p;
        } else {
            this.permutation = ParticleNoise.s_permutation; // static array
            this.permutation_2 = ParticleNoise.s_permutation_2; // static array copy 2 times
        }
    }
    public noise (x: number, y: number, z: number, min = 0, max = 1): number {
        const p: number[] = this.permutation_2;

How it works

No response

Alternatives Considered

None

Additional Information

No response

Yuki001 avatar Jan 19 '24 02:01 Yuki001