From aad941159e47a199d55a3fa02f99fa48bf2f744e Mon Sep 17 00:00:00 2001 From: zxx43 Date: Wed, 19 Apr 2023 10:37:25 +0800 Subject: [PATCH] Fix gradient sample value when ratio is one (#14538) * fix gradient value issue https://github.com/cocos/cocos-engine/issues/14534 * use last color when last time * add EPSILON when repeat * use epsilon to check last frame * use approx * modify input parameters --- cocos/particle/animator/gradient.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cocos/particle/animator/gradient.ts b/cocos/particle/animator/gradient.ts index acb5d1468d..5be0f04b78 100644 --- a/cocos/particle/animator/gradient.ts +++ b/cocos/particle/animator/gradient.ts @@ -22,7 +22,7 @@ THE SOFTWARE. */ -import { CCClass, Color, lerp, repeat, Enum } from '../../core'; +import { CCClass, Color, lerp, repeat, Enum, EPSILON, approx } from '../../core'; const Mode = Enum({ Blend: 0, @@ -161,7 +161,7 @@ export default class Gradient { private getRGB (time: number) { if (this.colorKeys.length > 1) { - time = repeat(time, 1); + time = repeat(time, 1.0 + EPSILON); for (let i = 1; i < this.colorKeys.length; ++i) { const preTime = this.colorKeys[i - 1].time; const curTime = this.colorKeys[i].time; @@ -175,7 +175,9 @@ export default class Gradient { } } const lastIndex = this.colorKeys.length - 1; - if (time < this.colorKeys[0].time) { + if (approx(time, this.colorKeys[lastIndex].time, EPSILON)) { + this._color.set(this.colorKeys[lastIndex].color); + } else if (time < this.colorKeys[0].time) { Color.lerp(this._color, Color.BLACK, this.colorKeys[0].color, time / this.colorKeys[0].time); } else if (time > this.colorKeys[lastIndex].time) { Color.lerp(this._color, this.colorKeys[lastIndex].color, Color.BLACK, (time - this.colorKeys[lastIndex].time) / (1 - this.colorKeys[lastIndex].time)); @@ -194,7 +196,7 @@ export default class Gradient { private getAlpha (time: number) { const basicAlpha = 0; // default alpha is 0 if (this.alphaKeys.length > 1) { - time = repeat(time, 1); + time = repeat(time, 1.0 + EPSILON); for (let i = 1; i < this.alphaKeys.length; ++i) { const preTime = this.alphaKeys[i - 1].time; const curTime = this.alphaKeys[i].time; @@ -207,7 +209,9 @@ export default class Gradient { } } const lastIndex = this.alphaKeys.length - 1; - if (time < this.alphaKeys[0].time) { + if (approx(time, this.alphaKeys[lastIndex].time, EPSILON)) { + return this.alphaKeys[lastIndex].alpha; + } else if (time < this.alphaKeys[0].time) { return lerp(basicAlpha, this.alphaKeys[0].alpha, time / this.alphaKeys[0].time); } else if (time > this.alphaKeys[lastIndex].time) { return lerp(this.alphaKeys[lastIndex].alpha, basicAlpha, (time - this.alphaKeys[lastIndex].time) / (1 - this.alphaKeys[lastIndex].time));