基于2个对象之间的距离的色变颜色 [英] Lerp color based on distance between 2 objects

查看:172
本文介绍了基于2个对象之间的距离的色变颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何根据2个对象之间的距离来制作平滑的彩色lerp.颜色应从绿色变为红色,再由绿色变为红色...立方体远=颜色为红色,立方体近=颜色为绿色.

I would like to know how to make a smooth color lerp based on distance between 2 objects. The color should lerp from green to red to green to red... Cube far = color red, cube near = color green.

我已经使一切正常,但是唯一认为不正常的是颜色不平滑.目前是这样的. https://i.gyazo.com/a85852e76d2418ab7d44c18e152647c0.mp4

I already got everything working, but the only thinks that isn't working is the fact that the color doesn't lerp smooth. This is how it looks like at the moment. https://i.gyazo.com/a85852e76d2418ab7d44c18e152647c0.mp4

我正在使用此脚本进行颜色更改:

I am using this script for the color change:

    FindClosestCube ();
    float lerpProgress = 0f;
    GameObject cubeChildTop = null;
    GameObject closestCube = FindClosestCube (); 
    cubeChildTop = closestCube.transform.Find("Top").gameObject;

    if (cubeDiffX >= 0.8f || cubeDiffX <= -0.8f) 
    {
        lerpProgress = 0.5f;
    }
    if (cubeDiffX <= 0.8f || cubeDiffX <= -0.8f) 
    {
        lerpProgress = 1f;
    }
    if (cubeDiffX >= 1.6f || cubeDiffX <= -1.6f) 
    {
        lerpProgress = 0f;
    }
    if(closestCube != GameObject.Find("Cube (1)2"))
    {
        cubeChildTop.GetComponent<Renderer>().material.color = Color.Lerp(redColor, greenColor, lerpProgress);
    }

那么...如何使它从红色平滑过渡到绿色?

So... how do I make it lerping smooth from red to green?

推荐答案

这真的很简单.

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;
}

结果:

这篇关于基于2个对象之间的距离的色变颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆