浏览代码

Improve gradient sample code

Unroll loops
Remove control flow
Remove dynamic indexing
/main
Matt Dean 7 年前
当前提交
dcfde51b
共有 1 个文件被更改,包括 14 次插入45 次删除
  1. 59
      com.unity.shadergraph/Editor/Data/Nodes/Input/Gradient/SampleGradientNode.cs

59
com.unity.shadergraph/Editor/Data/Nodes/Input/Gradient/SampleGradientNode.cs


return
@"
{
int colorKey1 = 0;
int colorKey2 = Gradient.colorsLength-1;
for(int c1 = 0; c1 < Gradient.colorsLength; c1++)
{precision}3 color = Gradient.colors[0].rgb;
[unroll]
for (int c = 1; c < 8; c++)
if(Gradient.colors[c1].w <= Time)
colorKey1 = c1;
else
break;
{precision} colorPos = saturate((Time - Gradient.colors[c-1].w) / (Gradient.colors[c].w - Gradient.colors[c-1].w)) * step(c, Gradient.colorsLength-1);
color = lerp(color, Gradient.colors[c].rgb, lerp(colorPos, step(0.01, colorPos), Gradient.type));
for(int c2 = Gradient.colorsLength-1; c2 >= 0; c2--)
#ifndef UNITY_COLORSPACE_GAMMA
color = SRGBToLinear(color);
#endif
{precision} alpha = Gradient.alphas[0].x;
[unroll]
for (int a = 1; a < 8; a++)
if(Gradient.colors[c2].w >= Time)
colorKey2 = c2;
else
break;
{precision} alphaPos = saturate((Time - Gradient.alphas[a-1].y) / (Gradient.alphas[a].y - Gradient.alphas[a-1].y)) * step(a, Gradient.alphasLength-1);
alpha = lerp(alpha, Gradient.alphas[a].x, lerp(alphaPos, step(0.01, alphaPos), Gradient.type));
int alphaKey1 = 0;
int alphaKey2 = Gradient.alphasLength-1;
for(int a1 = 0; a1 < Gradient.alphasLength; a1++)
{
if(Gradient.alphas[a1].y <= Time)
alphaKey1 = a1;
else
break;
}
for(int a2 = Gradient.alphasLength-1; a2 >= 0; a2--)
{
if(Gradient.alphas[a2].y >= Time)
alphaKey2 = a2;
else
break;
}
if(Gradient.type == 1)
{
float colorPos = min(1, max(0, (Time - Gradient.colors[colorKey1].w) / (Gradient.colors[colorKey2].w - Gradient.colors[colorKey1].w)));
float3 color = lerp(Gradient.colors[colorKey1].rgb, Gradient.colors[colorKey2].rgb, step(0.01, colorPos));
float alphaPos = min(1, max(0, (Time - Gradient.alphas[alphaKey1].y) / (Gradient.alphas[alphaKey2].y - Gradient.alphas[alphaKey1].y)));
float alpha = lerp(Gradient.alphas[alphaKey1].r, Gradient.alphas[alphaKey2].r, step(0.01, alphaPos));
Out = float4(color, alpha);
}
else
{
float colorPos = min(1, max(0, (Time - Gradient.colors[colorKey1].w) / (Gradient.colors[colorKey2].w - Gradient.colors[colorKey1].w)));
float3 color = lerp(Gradient.colors[colorKey1].rgb, Gradient.colors[colorKey2].rgb, colorPos);
float alphaPos = min(1, max(0, (Time - Gradient.alphas[alphaKey1].y) / (Gradient.alphas[alphaKey2].y - Gradient.alphas[alphaKey1].y)));
float alpha = lerp(Gradient.alphas[alphaKey1].r, Gradient.alphas[alphaKey2].r, alphaPos);
Out = float4(color, alpha);
}
Out = {precision}4(color, alpha);
}
";
}
正在加载...
取消
保存