Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 生成具有多个中心点的径向渐变_C#_Unity3d - Fatal编程技术网

C# 生成具有多个中心点的径向渐变

C# 生成具有多个中心点的径向渐变,c#,unity3d,C#,Unity3d,我遇到了一个奇怪的问题,就是用多个圆来制作径向渐变。目前,我正在获取一个中心点列表,以及一个相关半径列表,并从中生成一个黑白纹理。我有一个切换,以及改变方向的梯度 这是我的代码,直接来自它所在的系统: using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; [CreateAssetMenu(menuName =("Texture/Gradient

我遇到了一个奇怪的问题,就是用多个圆来制作径向渐变。目前,我正在获取一个中心点列表,以及一个相关半径列表,并从中生成一个黑白纹理。我有一个切换,以及改变方向的梯度

这是我的代码,直接来自它所在的系统:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

[CreateAssetMenu(menuName =("Texture/Gradient/Circle"))]
public class Circle : GradientShape
{
    public override Texture2D CreateGradient(NoiseGenerator.GradientTexture gt, int width, int height)
    {
        //Ensures no errors
        //If there is no ceneterpoints given, and by extension, no falloff distances given, then default to a circle in the middle of the texture, with a radius of half the width
        List<Vector2> centerPoints = new List<Vector2>();
        List<float> fallOffDistances = new List<float>();
        if (gt.points == null)
        {
            centerPoints.Add(new Vector2(width / 2, height / 2));
            fallOffDistances.Add(width/2);
        }
        else
        {
            if (gt.points.Count == 0)
            {
                centerPoints.Add(new Vector2(width / 2, height / 2));
                fallOffDistances.Add(width/2);
            }
            else
            {
                centerPoints = gt.points;
                fallOffDistances = gt.fallOffDistances;
            }
        }


        Texture2D texture = new Texture2D(width, height);
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                Vector2 currentPixel = new Vector2(x, y);
                Color newColor;

                int index = FindClosestPointIndex(currentPixel, centerPoints, fallOffDistances);
                Vector2 closestPoint = centerPoints[index];
                float falloffDistance = fallOffDistances[index];
                float distance = Vector2.Distance(currentPixel, closestPoint);
                if (gt.direction)
                {
                    if (distance <= falloffDistance)
                    {
                        float sample = distance / (falloffDistance * gt.fallOffStrength);
                        newColor = new Color(sample, sample, sample); //Turn black the closer you are to the point 
                    }
                    else
                    {
                        newColor = Color.white;
                    }
                }
                else
                {
                    if (distance >= falloffDistance)
                    {
                        float sample = falloffDistance / (distance * gt.fallOffStrength);
                        newColor = new Color(sample, sample, sample); //Turn white the closer you are to the point 
                    }
                    else
                    {
                        newColor = Color.white;
                    }
                }

                texture.SetPixel(x, y, newColor);
            }
        }
        texture.Apply();
        return texture;
    }


    int FindClosestPointIndex(Vector2 start, List<Vector2> points, List<float> fallOffDistances)
    {
        List<float> distances = new List<float>();
        for(int i = 0; i < points.Count; i++)
        {
            distances.Insert(0, (Vector2.Distance(start, points[i])));
        }

        int index = distances.IndexOf(distances.Min());

        return index;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用System.Linq;
[CreateAssetMenu(菜单=(“纹理/渐变/圆形”)]
公共类圆形:渐变形状
{
公共覆盖纹理2D CreateGradient(NoiseGenerator.GradientTexture gt,整数宽度,整数高度)
{
//确保没有错误
//如果没有给定的插补点,并且通过扩展,没有给出衰减距离,则默认为纹理中部的圆,宽度为一半的半径。
列表中心点=新列表();
列表衰减距离=新列表();
如果(gt.points==null)
{
添加(新矢量2(宽度/2,高度/2));
衰减距离。添加(宽度/2);
}
其他的
{
如果(gt.points.Count==0)
{
添加(新矢量2(宽度/2,高度/2));
衰减距离。添加(宽度/2);
}
其他的
{
中心点=gt点;
衰减距离=gt.衰减距离;
}
}
纹理2D纹理=新纹理2D(宽度、高度);
对于(int x=0;x
它可以在一个圆圈内正常工作。

但从来没有两个

还是三个

我期望的结果是:

()

我知道这个问题与我的FindClosestPointIndex方法有关。
我只是不知道怎么修。任何帮助都将不胜感激。)

您根据到最近中心的距离计算点颜色。所以,如果你有3个中心,你只需将你的纹理分割成围绕这些中心的3个独立区域。这就是为什么没有平滑的渐变:每个区域的颜色都是独立于其他区域计算的->您可以看到边

你需要的是根据每个渐变中心计算纹理的每个点。类似这样的内容(伪代码):

float brightness1 = distanceFromCenter1 * falloff1;
float brightness2 = distanceFromCenter2 * falloff2;
float brightness3 = distanceFromCenter3 * falloff3;
float pointBrightness = (brightness1 + brightness2 + brightness3) / 3f;