【问题标题】:Non Square Radial Gradient Image Generation非方形径向渐变图像生成
【发布时间】:2020-08-29 23:58:32
【问题描述】:

我正在 Unity 中创建一个函数,以使用渐变填充纹理,允许水平、垂直、对角线和径向方向。前三个很简单,可以处理矩形图像,但是,我能找到的径向渐变的唯一算法取决于方形图像,如下所示:

Vector2 center = new Vector2 ( texture.width * 0.5f, texture.height * 0.5f );
for ( int y = 0; y < texture.height; y++ )
{
    for ( int x = 0; x < texture.width; x++ )
    {
        float distanceFromCenter = Vector2.Distance ( center, new Vector2 ( x, y ) );
        float t = invert ? 1 - ( 0.5f - ( distanceFromCenter / texture.width ) ) : 0.5f - ( distanceFromCenter / texture.width );
        texture.SetPixel ( x, y, gradient.Evaluate ( t ) );
    }
}

如何使它适用于矩形图像?

【问题讨论】:

    标签: c# unity3d imaging


    【解决方案1】:

    问题在于纹理宽度的划分。对于方形纹理,这无关紧要,因为它的高度和宽度都是相同的长度。

    您需要将当前点除以纹理的尺寸。这将为您提供介于 0 和 1 之间的 x 和 y 值。

    Vector2 relativePoint = new Vector2 ( x / texture.width, y / texture.height );
    

    接下来,获取新中心点 (0.5, 0.5) 和这个新点之间的距离,为您提供点 (x, y) 的居中采样值。

    float centeredPointSampleValue = 0.5f - Vector2.Distance ( new Vector2(0.5, 0.5), relativePoint );
    

    把它们放在一起:

    for ( int y = 0; y < texture.height; y++ )
    {
        for ( int x = 0; x < texture.width; x++ )
        {
            Vector2 relativePoint = new Vector2 ( x / texture.width, y / texture.height );
            float centeredPointSampleValue = 0.5f - Vector2.Distance ( new Vector2( 0.5, 0.5 ), relativePoint );
            float t = invert ? 1 - centeredPointSampleValue  : centeredPointSampleValue ;
            texture.SetPixel ( x, y, gradient.Evaluate ( t ) );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多