diff --git a/cocos/core/curves/gradient.ts b/cocos/core/curves/gradient.ts index 9ad938e15de..9d1b76ac11b 100644 --- a/cocos/core/curves/gradient.ts +++ b/cocos/core/curves/gradient.ts @@ -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, @@ -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; diff --git a/cocos/core/math/utils.ts b/cocos/core/math/utils.ts index 0af2c7d624a..3b653aba193 100644 --- a/cocos/core/math/utils.ts +++ b/cocos/core/math/utils.ts @@ -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; @@ -121,7 +123,18 @@ 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 number> (func: TFunction): void { + _random = func; +} /** * @en Returns a floating-point random number between min (inclusive) and max (exclusive).
@@ -129,10 +142,10 @@ export const random = Math.random; * @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; } /** diff --git a/cocos/particle-2d/particle-simulator-2d.ts b/cocos/particle-2d/particle-simulator-2d.ts index 2902322ea8a..96e68cf67b7 100644 --- a/cocos/particle-2d/particle-simulator-2d.ts +++ b/cocos/particle-2d/particle-simulator-2d.ts @@ -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'; @@ -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; @@ -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; @@ -202,18 +202,18 @@ 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)); @@ -221,12 +221,12 @@ export class Simulator { } 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); } } @@ -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 @@ -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; diff --git a/cocos/particle/animator/noise-module.ts b/cocos/particle/animator/noise-module.ts index b4106e54f90..0eb677aed3f 100644 --- a/cocos/particle/animator/noise-module.ts +++ b/cocos/particle/animator/noise-module.ts @@ -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'; @@ -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)); } diff --git a/cocos/particle/animator/texture-animation.ts b/cocos/particle/animator/texture-animation.ts index a08dd4f5b05..fd15feccbc1 100644 --- a/cocos/particle/animator/texture-animation.ts +++ b/cocos/particle/animator/texture-animation.ts @@ -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, error } from '../../core'; import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle'; import CurveRange from './curve-range'; import { ModuleRandSeed } from '../enum'; @@ -117,7 +117,7 @@ export default class TextureAnimationModule extends ParticleModuleBase { set mode (val) { if (val !== Mode.Grid) { - console.error('particle texture animation\'s sprites is not supported!'); + error('particle texture animation\'s sprites is not supported!'); } } @@ -207,7 +207,7 @@ export default class TextureAnimationModule extends ParticleModuleBase { } set flipU (val) { - console.error('particle texture animation\'s flipU is not supported!'); + error('particle texture animation\'s flipU is not supported!'); } @serializable @@ -218,7 +218,7 @@ export default class TextureAnimationModule extends ParticleModuleBase { } set flipV (val) { - console.error('particle texture animation\'s flipV is not supported!'); + error('particle texture animation\'s flipV is not supported!'); } @serializable @@ -229,7 +229,7 @@ export default class TextureAnimationModule extends ParticleModuleBase { } set uvChannelMask (val) { - console.error('particle texture animation\'s uvChannelMask is not supported!'); + error('particle texture animation\'s uvChannelMask is not supported!'); } /** @@ -263,7 +263,7 @@ export default class TextureAnimationModule extends ParticleModuleBase { * @internal */ public init (p: Particle): void { - p.startRow = Math.floor(Math.random() * this.numTilesY); + p.startRow = Math.floor(random() * this.numTilesY); } /**