设置着色器 属性 (Propertyblock) 两次(使用 Lerp 淡出颜色并将其重置为原始颜色)
Setting a shader property (Propertyblock) twice (Fading out a color with Lerp and resetting it to the original color)
我正在尝试制作 "wet footprints",但在重复使用该步骤时遇到淡出颜色并将其重置为起始颜色的一些问题。我有一个带有几个足迹游戏对象的游戏对象数组,当玩家脚接触地面时,它们被放置在玩家脚的位置上。它目前正在汇集这些步骤,一切正常。因为我想让它尽可能轻便,所以我正在使用带有 属性 块的贴花着色器,我需要在放置对象时淡出 _MainColor 属性,以及再次使用同一对象时在不同的位置,它需要重新设置。然而,问题是褪色在其当前设置中不起作用。目前发生的情况是颜色在重复使用时被重置为黑色,并且应该再次淡出,但它只是立即再次透明。
void Update()
{
if (lastPos != transform.position)
{
stepPositioner = true;
lastPos = transform.position;
//Debug.Log("This Go changed position: ", this);
}
if (stepPositioner)
{
StartCoroutine(FadeTimer());
}
}
IEnumerator FadeTimer()
{
_renderer.GetPropertyBlock(_propertyBlock);
_propertyBlock.SetColor("_MainColor", startColor);
_renderer.SetPropertyBlock(_propertyBlock);
yield return new WaitForSeconds(1);
_renderer.GetPropertyBlock(_propertyBlock);
_propertyBlock.SetColor("_MainColor", Color.Lerp(startColor, endColor, Time.time * .5f));
_renderer.SetPropertyBlock(_propertyBlock);
stepPositioner = false;
}
那不是 Lerp
的工作方式。
Linearly interpolates between colors a and b by t.
t is clamped between 0 and 1. When t is 0 returns a. When t is 1 returns b.
你只调用它 一次 有一个大约 0.5/60 的小因子所以大约 0.008333
导致几乎是起始值。
此外,您在重置 stepPositiontimet
之前等待一秒钟,因此在一秒钟内,您 Update
方法会启动数百个并发协程!
要在一秒钟内淡出颜色,请使用类似
的东西
IEnumerator FadeTimer(float duration)
{
// This has to be done right away!
// Otherwise you get concurrent routines!
stepPositioner = false;
_renderer.GetPropertyBlock(_propertyBlock);
_propertyBlock.SetColor("_MainColor", startColor);
_renderer.SetPropertyBlock(_propertyBlock);
var timePassed = 0f;
while (timePassed < duration)
{
_renderer.GetPropertyBlock(_propertyBlock);
_propertyBlock.SetColor("_MainColor", Color.Lerp(startColor, endColor, timePassed / duration));
_renderer.SetPropertyBlock(_propertyBlock);
// Add the time since last frame
timePassed += Time.deltaTime;
// Tells Unity to "pause" the routine, render this frame
// and continue from here in the next frame
yield return null;
}
// Just to be sure set the final value in the end
_renderer.GetPropertyBlock(_propertyBlock);
_propertyBlock.SetColor("_MainColor", endColor, );
_renderer.SetPropertyBlock(_propertyBlock);
}
只是添加了持续时间作为参数,使其更清晰、更灵活。所以就把它叫做
StarCoroutine(FadeTimer(2));
例如在 2 秒内淡出
注意:在智能手机上打字,但我希望思路清晰
我正在尝试制作 "wet footprints",但在重复使用该步骤时遇到淡出颜色并将其重置为起始颜色的一些问题。我有一个带有几个足迹游戏对象的游戏对象数组,当玩家脚接触地面时,它们被放置在玩家脚的位置上。它目前正在汇集这些步骤,一切正常。因为我想让它尽可能轻便,所以我正在使用带有 属性 块的贴花着色器,我需要在放置对象时淡出 _MainColor 属性,以及再次使用同一对象时在不同的位置,它需要重新设置。然而,问题是褪色在其当前设置中不起作用。目前发生的情况是颜色在重复使用时被重置为黑色,并且应该再次淡出,但它只是立即再次透明。
void Update()
{
if (lastPos != transform.position)
{
stepPositioner = true;
lastPos = transform.position;
//Debug.Log("This Go changed position: ", this);
}
if (stepPositioner)
{
StartCoroutine(FadeTimer());
}
}
IEnumerator FadeTimer()
{
_renderer.GetPropertyBlock(_propertyBlock);
_propertyBlock.SetColor("_MainColor", startColor);
_renderer.SetPropertyBlock(_propertyBlock);
yield return new WaitForSeconds(1);
_renderer.GetPropertyBlock(_propertyBlock);
_propertyBlock.SetColor("_MainColor", Color.Lerp(startColor, endColor, Time.time * .5f));
_renderer.SetPropertyBlock(_propertyBlock);
stepPositioner = false;
}
那不是 Lerp
的工作方式。
Linearly interpolates between colors a and b by t.
t is clamped between 0 and 1. When t is 0 returns a. When t is 1 returns b.
你只调用它 一次 有一个大约 0.5/60 的小因子所以大约 0.008333
导致几乎是起始值。
此外,您在重置 stepPositiontimet
之前等待一秒钟,因此在一秒钟内,您 Update
方法会启动数百个并发协程!
要在一秒钟内淡出颜色,请使用类似
的东西IEnumerator FadeTimer(float duration)
{
// This has to be done right away!
// Otherwise you get concurrent routines!
stepPositioner = false;
_renderer.GetPropertyBlock(_propertyBlock);
_propertyBlock.SetColor("_MainColor", startColor);
_renderer.SetPropertyBlock(_propertyBlock);
var timePassed = 0f;
while (timePassed < duration)
{
_renderer.GetPropertyBlock(_propertyBlock);
_propertyBlock.SetColor("_MainColor", Color.Lerp(startColor, endColor, timePassed / duration));
_renderer.SetPropertyBlock(_propertyBlock);
// Add the time since last frame
timePassed += Time.deltaTime;
// Tells Unity to "pause" the routine, render this frame
// and continue from here in the next frame
yield return null;
}
// Just to be sure set the final value in the end
_renderer.GetPropertyBlock(_propertyBlock);
_propertyBlock.SetColor("_MainColor", endColor, );
_renderer.SetPropertyBlock(_propertyBlock);
}
只是添加了持续时间作为参数,使其更清晰、更灵活。所以就把它叫做
StarCoroutine(FadeTimer(2));
例如在 2 秒内淡出
注意:在智能手机上打字,但我希望思路清晰