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
This commit is contained in:
zxx43
2023-04-19 10:37:25 +08:00
committed by GitHub
parent 790f9339e7
commit aad941159e
+9 -5
View File
@@ -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));