且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

基于2个对象之间的距离的色变颜色

更新时间:2023-02-26 20:22:01

这真的很简单.

1 .找到您认为这两个对象可以分开传播的最大距离.

1.Find the max distance that you think both of those Objects can travel apart.

您需要做的第一件事是确定两个GameObject将要分开的最大距离值.您需要将该值传递给#2 mapValue函数的inValueMax参数.

The first thing you need to do is determine the max distance value those two GameObjects will be apart from. You need to pass that value into the inValueMax parameter of the mapValue function fro #2.

您可以使用以下代码确定最大值:

You can determine that max value with this code:

public GameObject obj1;
public GameObject obj2;

void Update()
{
    UnityEngine.Debug.Log(getSqrDistance(obj1.transform.position, obj2.transform.position));
}

public float getSqrDistance(Vector3 v1, Vector3 v2)
{
    return (v1 - v2).sqrMagnitude;
}

运行它,手动移动每个对象/多维数据集,然后通过Debug.Log消息获得两个对象可以相互传播的最大值.

Run it, manually move each Object/Cube then get the highest value both Objects can travel from each other with the Debug.Log message.

看您发布的视频,我估计200的距离值是合适的,但是如果要获得理想的结果,您仍然必须使用上面的脚本进行实验.

Looking that the video you posted I estimated that the distance value of 200 is fine for that but you still have to do your experiment with the script above if you want a perfect result.

2 .使用 map 来将0以及MAX_DISTANCE距离范围转换为0f1f范围

2.Use the map to convert 0 and that MAX_DISTANCE distance range to 0f and 1f range

float mapValue(float mainValue, float inValueMin, float inValueMax, float outValueMin, float outValueMax)
{
    return (mainValue - inValueMin) * (outValueMax - outValueMin) / (inValueMax - inValueMin) + outValueMin;
}

它将值缩放到某个特定点到另一个之间.

It scales values between some certain point to another.

例如,您需要使用Lerp函数执行此操作,而Lerp函数将0取为1值. mapValue函数可以将任何数字缩放到Lerp函数所需的01之间.

For example, you need to use the Lerp function to do this and the Lerp function takes 0 to 1 values. The mapValue function can scale any number to range between 0 and 1 that the Lerp function need.

对我来说,我将使用mapValue函数将0200范围值缩放到0f1f范围.

For me, I will scale 0 to 200 range values to 0f and 1f range with the mapValue function.

3 .最后,使用Color.Lerp(near, far, lerp);在各种颜色之间切换. lerp值是#2 的结果值.

3.Finally, use Color.Lerp(near, far, lerp); to lerp between Colors. The lerp value is the result value from #2.

在代码中:

找到#1 后,将其插入下面脚本中的MAX_DISTANCE变量即可:

Once you find #1, plug that value to the MAX_DISTANCE variable from the script below should work:

public GameObject obj1;
public GameObject obj2;

Color near = Color.green;
Color far = Color.red;
const float MAX_DISTANCE = 200;

void Update()
{

    //Get distance between those two Objects
    float distanceApart = getSqrDistance(obj1.transform.position, obj2.transform.position);
    UnityEngine.Debug.Log(getSqrDistance(obj1.transform.position, obj2.transform.position));

    //Convert 0 and 200 distance range to 0f and 1f range
    float lerp = mapValue(distanceApart, 0, MAX_DISTANCE, 0f, 1f);

    //Lerp Color between near and far color
    Color lerpColor = Color.Lerp(near, far, lerp);
    obj1.GetComponent<Renderer>().material.color = lerpColor;
}

public float getSqrDistance(Vector3 v1, Vector3 v2)
{
    return (v1 - v2).sqrMagnitude;
}

float mapValue(float mainValue, float inValueMin, float inValueMax, float outValueMin, float outValueMax)
{
    return (mainValue - inValueMin) * (outValueMax - outValueMin) / (inValueMax - inValueMin) + outValueMin;
}

结果: