Gradient.E内部如何工作?

问题描述 投票:1回答:2

我一直在尝试使结构类似于Unity的内置Gradient

我一直想了解的是Gradient.Evaluate函数,它在内部如何工作?

任何代码段都将非常有用!

注意:我没有使用Unity,这就是为什么我问这个问题。

unity3d gradient evaluation
2个回答
1
投票

一种颜色由4个成分组成:RGBA。红色-绿色-蓝色-Alpha

这些分量存储为0-255之间的整数。

白色是((1,1,1,1)]

黑色是:(0,0,0,1)

如果A值不为1,则颜色为透明。

[计算梯度时,您可能会询问混合模式。在混合模式下,您将在两个键之间插入一个值。举例来说,您有以下两种颜色:

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)
© www.soinside.com 2019 - 2024. All rights reserved.