Gradient.Evaluate 在内部是如何工作的?
How does Gradient.Evaluate work internally?
我一直在尝试做一个类似于Unity内置的结构Gradient
。
我一直在思考的是 Gradient.Evaluate
函数,它在内部是如何工作的?
任何代码片段都会非常有用!
注意:我没有使用 Unity,这就是我问这个问题的原因。
一种颜色由 4 个分量组成:RGBA。红 - 绿 - 蓝 - Alpha
这些分量存储为 0-255 之间的整数。
白色是:(1,1,1,1)
黑色为:(0,0,0,1)
如果A值不为1,则颜色是透明的。
当您计算渐变时,您可能会询问混合模式。在混合模式下,您在 2 个键之间插入一个值。举个例子,您有以下两种颜色:
colorA = (0,0,1,1) //This is blue
colorB = (1,0,0,1) //This is red
让我们用一些伪代码来简化梯度计算:
gradient.ColorKey[0].color = colorA;
gradient.ColorKey[0].time = 0f;
gradient.ColorKey[1].color = colorB;
gradient.ColorKey[1].time = 2f;
gradient.Evaluate(0f);
// (0,0,1,1)
gradient.Evaluate(0.6f); // 0.6 seconds in, or 70% key0 and 30% key1
// 0.7 * (0,0,1,1) + 0.3 * (1,0,0,1) =
//(0.3, 0, 0.7, 1)
gradient.Evaluate(1.8f); // 1.8 seconds in, or 10% key0 and 90% key1
// 0.1 * (0,0,1,1) + 0.9 * (1,0,0,1) =
//(0.9, 0, 0.1, 1)
gradient.Evaluate(2f);
// (1,0,0,1)
如果你的梯度有超过 2 个键,梯度计算只使用与评估时间相邻的 2 个键。例如:
A (0,0,1,1) at 0s
B (1,0,0,1) at 1s
C (0,1,0,1) at 2s
gradient.Evaluate(0.7f); //results in 30% of A + 70% of B (0.7, 0, 0.3, 1)
gradient.Evaluate(1.5f); //results in 50% of B + 50% of C (0.5, 0.5, 0, 1)
我一直在尝试做一个类似于Unity内置的结构Gradient
。
我一直在思考的是 Gradient.Evaluate
函数,它在内部是如何工作的?
任何代码片段都会非常有用!
注意:我没有使用 Unity,这就是我问这个问题的原因。
一种颜色由 4 个分量组成:RGBA。红 - 绿 - 蓝 - Alpha
这些分量存储为 0-255 之间的整数。
白色是:(1,1,1,1)
黑色为:(0,0,0,1)
如果A值不为1,则颜色是透明的。
当您计算渐变时,您可能会询问混合模式。在混合模式下,您在 2 个键之间插入一个值。举个例子,您有以下两种颜色:
colorA = (0,0,1,1) //This is blue
colorB = (1,0,0,1) //This is red
让我们用一些伪代码来简化梯度计算:
gradient.ColorKey[0].color = colorA;
gradient.ColorKey[0].time = 0f;
gradient.ColorKey[1].color = colorB;
gradient.ColorKey[1].time = 2f;
gradient.Evaluate(0f);
// (0,0,1,1)
gradient.Evaluate(0.6f); // 0.6 seconds in, or 70% key0 and 30% key1
// 0.7 * (0,0,1,1) + 0.3 * (1,0,0,1) =
//(0.3, 0, 0.7, 1)
gradient.Evaluate(1.8f); // 1.8 seconds in, or 10% key0 and 90% key1
// 0.1 * (0,0,1,1) + 0.9 * (1,0,0,1) =
//(0.9, 0, 0.1, 1)
gradient.Evaluate(2f);
// (1,0,0,1)
如果你的梯度有超过 2 个键,梯度计算只使用与评估时间相邻的 2 个键。例如:
A (0,0,1,1) at 0s
B (1,0,0,1) at 1s
C (0,1,0,1) at 2s
gradient.Evaluate(0.7f); //results in 30% of A + 70% of B (0.7, 0, 0.3, 1)
gradient.Evaluate(1.5f); //results in 50% of B + 50% of C (0.5, 0.5, 0, 1)