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

Update utils.ts, replace the random function, to make particle scenes… #15838

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions cocos/core/curves/gradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import { CCClass } from '../data';
import { Enum } from '../value-types';
import { Color, lerp, repeat, EPSILON, approx } from '../math';
import { Color, lerp, repeat, EPSILON, approx, random } from '../math';

const Mode = Enum({
Blend: 0,
Expand Down Expand Up @@ -172,8 +172,8 @@ export class Gradient {
* @returns @en Randomized color. @zh 随机生成的颜色。
*/
public getRandomColor (out: Color): Color {
const c = this.colorKeys[Math.trunc(Math.random() * this.colorKeys.length)];
const a = this.alphaKeys[Math.trunc(Math.random() * this.alphaKeys.length)];
const c = this.colorKeys[Math.trunc(random() * this.colorKeys.length)];
const a = this.alphaKeys[Math.trunc(random() * this.alphaKeys.length)];
out.set(c.color);
out._set_a_unsafe(a.alpha);
return out;
Expand Down
19 changes: 16 additions & 3 deletions cocos/core/math/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const _d2r = Math.PI / 180.0;

const _r2d = 180.0 / Math.PI;

let _random = Math.random;

export const HALF_PI = Math.PI * 0.5;
export const TWO_PI = Math.PI * 2.0;

Expand Down Expand Up @@ -121,18 +123,29 @@ export function toDegree (a: number): number {
/**
* @method random
*/
export const random = Math.random;
export function random (): number {
return _random();
}

/**
* @en Set a custom random number generator, default to Math.random
* @zh 设置自定义随机数生成器,默认为 Math.random
* @param func custom random number generator
*/
export function setRandGenerator<TFunction extends (...any) => number> (func: TFunction): void {
_random = func;
}

/**
* @en Returns a floating-point random number between min (inclusive) and max (exclusive).<br/>
* @zh 返回最小(包含)和最大(不包含)之间的浮点随机数。
* @method randomRange
* @param min
* @param max
* @return The random number.
* @return {Number} The random number.
*/
export function randomRange (min: number, max: number): number {
return Math.random() * (max - min) + min;
return random() * (max - min) + min;
}

/**
Expand Down
58 changes: 29 additions & 29 deletions cocos/particle-2d/particle-simulator-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
THE SOFTWARE.
*/

import { Vec2, Color, js, misc } from '../core';
import { Vec2, Color, js, misc, random, IColorLike } from '../core';
import { vfmtPosUvColor, getComponentPerVertex } from '../2d/renderer/vertex-format';
import { PositionType, EmitterMode, START_SIZE_EQUAL_TO_END_SIZE, START_RADIUS_EQUAL_TO_END_RADIUS } from './define';
import { ParticleSystem2D } from './particle-system-2d';
Expand Down Expand Up @@ -150,12 +150,12 @@ export class Simulator {
// Init particle
// timeToLive
// no negative life. prevent division by 0
particle.timeToLive = psys.life + psys.lifeVar * (Math.random() - 0.5) * 2;
particle.timeToLive = psys.life + psys.lifeVar * (random() - 0.5) * 2;
const timeToLive = particle.timeToLive = Math.max(0, particle.timeToLive);

// position
particle.pos.x = psys.sourcePos.x + psys.posVar.x * (Math.random() - 0.5) * 2;
particle.pos.y = psys.sourcePos.y + psys.posVar.y * (Math.random() - 0.5) * 2;
particle.pos.x = psys.sourcePos.x + psys.posVar.x * (random() - 0.5) * 2;
particle.pos.y = psys.sourcePos.y + psys.posVar.y * (random() - 0.5) * 2;

// Color
let sr = 0;
Expand All @@ -167,30 +167,30 @@ export class Simulator {
const endColor = psys.endColor;
const endColorVar = psys.endColorVar;

particle.color.r = sr = misc.clampf(startColor.r + startColorVar.r * (Math.random() - 0.5) * 2, 0, 255);
particle.color.g = sg = misc.clampf(startColor.g + startColorVar.g * (Math.random() - 0.5) * 2, 0, 255);
particle.color.b = sb = misc.clampf(startColor.b + startColorVar.b * (Math.random() - 0.5) * 2, 0, 255);
particle.color.a = sa = misc.clampf(startColor.a + startColorVar.a * (Math.random() - 0.5) * 2, 0, 255);
particle.deltaColor.r = (misc.clampf(endColor.r + endColorVar.r * (Math.random() - 0.5) * 2, 0, 255) - sr) / timeToLive;
particle.deltaColor.g = (misc.clampf(endColor.g + endColorVar.g * (Math.random() - 0.5) * 2, 0, 255) - sg) / timeToLive;
particle.deltaColor.b = (misc.clampf(endColor.b + endColorVar.b * (Math.random() - 0.5) * 2, 0, 255) - sb) / timeToLive;
particle.deltaColor.a = (misc.clampf(endColor.a + endColorVar.a * (Math.random() - 0.5) * 2, 0, 255) - sa) / timeToLive;
particle.color.r = sr = misc.clampf(startColor.r + startColorVar.r * (random() - 0.5) * 2, 0, 255);
particle.color.g = sg = misc.clampf(startColor.g + startColorVar.g * (random() - 0.5) * 2, 0, 255);
particle.color.b = sb = misc.clampf(startColor.b + startColorVar.b * (random() - 0.5) * 2, 0, 255);
particle.color.a = sa = misc.clampf(startColor.a + startColorVar.a * (random() - 0.5) * 2, 0, 255);
particle.deltaColor.r = (misc.clampf(endColor.r + endColorVar.r * (random() - 0.5) * 2, 0, 255) - sr) / timeToLive;
particle.deltaColor.g = (misc.clampf(endColor.g + endColorVar.g * (random() - 0.5) * 2, 0, 255) - sg) / timeToLive;
particle.deltaColor.b = (misc.clampf(endColor.b + endColorVar.b * (random() - 0.5) * 2, 0, 255) - sb) / timeToLive;
particle.deltaColor.a = (misc.clampf(endColor.a + endColorVar.a * (random() - 0.5) * 2, 0, 255) - sa) / timeToLive;

// size
let startS = psys.startSize + psys.startSizeVar * (Math.random() - 0.5) * 2;
let startS = psys.startSize + psys.startSizeVar * (random() - 0.5) * 2;
startS = Math.max(0, startS); // No negative value
particle.size = startS;
if (psys.endSize === START_SIZE_EQUAL_TO_END_SIZE) {
particle.deltaSize = 0;
} else {
let endS = psys.endSize + psys.endSizeVar * (Math.random() - 0.5) * 2;
let endS = psys.endSize + psys.endSizeVar * (random() - 0.5) * 2;
endS = Math.max(0, endS); // No negative values
particle.deltaSize = (endS - startS) / timeToLive;
}

// rotation
const startA = psys.startSpin + psys.startSpinVar * (Math.random() - 0.5) * 2;
const endA = psys.endSpin + psys.endSpinVar * (Math.random() - 0.5) * 2;
const startA = psys.startSpin + psys.startSpinVar * (random() - 0.5) * 2;
const endA = psys.endSpin + psys.endSpinVar * (random() - 0.5) * 2;
particle.rotation = startA;
particle.deltaRotation = (endA - startA) / timeToLive;

Expand All @@ -202,31 +202,31 @@ export class Simulator {
particle.aspectRatio = psys.aspectRatio || 1;

// direction
const a = misc.degreesToRadians(psys.angle + this._worldRotation + psys.angleVar * (Math.random() - 0.5) * 2);
const a = misc.degreesToRadians(psys.angle + this._worldRotation + psys.angleVar * (random() - 0.5) * 2);
// Mode Gravity: A
if (psys.emitterMode === EmitterMode.GRAVITY) {
const s = psys.speed + psys.speedVar * (Math.random() - 0.5) * 2;
const s = psys.speed + psys.speedVar * (random() - 0.5) * 2;
// direction
particle.dir.x = Math.cos(a);
particle.dir.y = Math.sin(a);
particle.dir.multiplyScalar(s);
// radial accel
particle.radialAccel = psys.radialAccel + psys.radialAccelVar * (Math.random() - 0.5) * 2;
particle.radialAccel = psys.radialAccel + psys.radialAccelVar * (random() - 0.5) * 2;
// tangential accel
particle.tangentialAccel = psys.tangentialAccel + psys.tangentialAccelVar * (Math.random() - 0.5) * 2;
particle.tangentialAccel = psys.tangentialAccel + psys.tangentialAccelVar * (random() - 0.5) * 2;
// rotation is dir
if (psys.rotationIsDir) {
particle.rotation = -misc.radiansToDegrees(Math.atan2(particle.dir.y, particle.dir.x));
}
} else {
// Mode Radius: B
// Set the default diameter of the particle from the source position
const startRadius = psys.startRadius + psys.startRadiusVar * (Math.random() - 0.5) * 2;
const endRadius = psys.endRadius + psys.endRadiusVar * (Math.random() - 0.5) * 2;
const startRadius = psys.startRadius + psys.startRadiusVar * (random() - 0.5) * 2;
const endRadius = psys.endRadius + psys.endRadiusVar * (random() - 0.5) * 2;
particle.radius = startRadius;
particle.deltaRadius = (psys.endRadius === START_RADIUS_EQUAL_TO_END_RADIUS) ? 0 : (endRadius - startRadius) / timeToLive;
particle.angle = a;
particle.degreesPerSecond = misc.degreesToRadians(psys.rotatePerS + psys.rotatePerSVar * (Math.random() - 0.5) * 2);
particle.degreesPerSecond = misc.degreesToRadians(psys.rotatePerS + psys.rotatePerSVar * (random() - 0.5) * 2);
}
}

Expand Down Expand Up @@ -275,7 +275,7 @@ export class Simulator {
const y1 = -halfHeight;
const x2 = halfWidth;
const y2 = halfHeight;
const rad = -misc.degreesToRadians(particle.rotation);
const rad = -misc.degreesToRadians(particle.rotation as number);
const cr = Math.cos(rad);
const sr = Math.sin(rad);
// bl
Expand Down Expand Up @@ -313,13 +313,13 @@ export class Simulator {
vbuf[offset + 29] = 0;
}
// color
Color.toArray(vbuf, particle.color, offset + 5);
Color.toArray(vbuf, particle.color, offset + 14);
Color.toArray(vbuf, particle.color, offset + 23);
Color.toArray(vbuf, particle.color, offset + 32);
Color.toArray(vbuf, particle.color as IColorLike, offset + 5);
Color.toArray(vbuf, particle.color as IColorLike, offset + 14);
Color.toArray(vbuf, particle.color as IColorLike, offset + 23);
Color.toArray(vbuf, particle.color as IColorLike, offset + 32);
}

public step (dt): void {
public step (dt: number): void {
const assembler = this.sys.assembler!;
const psys = this.sys;
const node = psys.node;
Expand Down
6 changes: 3 additions & 3 deletions cocos/particle/animator/noise-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
THE SOFTWARE.
*/

import { CCFloat, CCInteger, _decorator, Vec3 } from '../../core';
import { CCFloat, CCInteger, _decorator, Vec3, random } from '../../core';
import { ParticleNoise } from '../noise';
import { Particle, PARTICLE_MODULE_NAME, ParticleModuleBase } from '../particle';

Expand Down Expand Up @@ -302,12 +302,12 @@ export class NoiseModule extends ParticleModuleBase {
this.noise.setOctaves(this.octaves, this.octaveMultiplier, this.octaveScale);

this.samplePosition.set(particle.position);
this.samplePosition.add3f(Math.random() * 1.0, Math.random() * 1.0, Math.random() * 1.0);
this.samplePosition.add3f(random() * 1.0, random() * 1.0, random() * 1.0);
this.noise.setSamplePoint(this.samplePosition);
this.noise.getNoiseParticle();

const noisePosition: Vec3 = this.noise.getResult();
noisePosition.multiply3f(Math.random(), Math.random(), Math.random());
noisePosition.multiply3f(random(), random(), random());
Vec3.add(particle.position, particle.position, noisePosition.multiplyScalar(dt));
}

Expand Down
4 changes: 2 additions & 2 deletions cocos/particle/animator/texture-animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

import { ccclass, tooltip, displayOrder, type, formerlySerializedAs, serializable, range } from 'cc.decorator';
import { lerp, pseudoRandom, repeat, Enum } from '../../core';
import { lerp, pseudoRandom, repeat, Enum, random } from '../../core';
import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle';
import CurveRange from './curve-range';
import { ModuleRandSeed } from '../enum';
Expand Down Expand Up @@ -117,7 +117,7 @@

set mode (val) {
if (val !== Mode.Grid) {
console.error('particle texture animation\'s sprites is not supported!');

Check failure on line 120 in cocos/particle/animator/texture-animation.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
}
}

Expand Down Expand Up @@ -207,7 +207,7 @@
}

set flipU (val) {
console.error('particle texture animation\'s flipU is not supported!');

Check failure on line 210 in cocos/particle/animator/texture-animation.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
}

@serializable
Expand All @@ -218,7 +218,7 @@
}

set flipV (val) {
console.error('particle texture animation\'s flipV is not supported!');

Check failure on line 221 in cocos/particle/animator/texture-animation.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
}

@serializable
Expand All @@ -229,7 +229,7 @@
}

set uvChannelMask (val) {
console.error('particle texture animation\'s uvChannelMask is not supported!');

Check failure on line 232 in cocos/particle/animator/texture-animation.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
}

/**
Expand Down Expand Up @@ -263,7 +263,7 @@
* @internal
*/
public init (p: Particle): void {
p.startRow = Math.floor(Math.random() * this.numTilesY);
p.startRow = Math.floor(random() * this.numTilesY);
}

/**
Expand Down
Loading