C# 设置着色器属性(Propertyblock)两次(使用Lerp淡出颜色并将其重置为原始颜色)
我试图制作“湿脚印”,但遇到了一些问题,当重复使用该步骤时,淡出颜色并将其重置为起始颜色。我有一个带有两个足迹游戏对象的游戏对象阵列,当它接触地面时,它们被放置在玩家脚的位置上。它目前正在汇集这些步骤,这些步骤都运作良好。因为我想让它尽可能轻量化,所以我使用了一个带有propertyblock的贴花着色器,我需要在放置对象时淡出_MainColor属性,并且当在不同位置再次使用同一对象时,需要重置该对象。但问题是,在其当前设置中,淡入不起作用。当前发生的情况是,当重新使用时,颜色被重置为黑色,并且假设它再次淡出,但它只是立即再次透明C# 设置着色器属性(Propertyblock)两次(使用Lerp淡出颜色并将其重置为原始颜色),c#,unity3d,shader,C#,Unity3d,Shader,我试图制作“湿脚印”,但遇到了一些问题,当重复使用该步骤时,淡出颜色并将其重置为起始颜色。我有一个带有两个足迹游戏对象的游戏对象阵列,当它接触地面时,它们被放置在玩家脚的位置上。它目前正在汇集这些步骤,这些步骤都运作良好。因为我想让它尽可能轻量化,所以我使用了一个带有propertyblock的贴花着色器,我需要在放置对象时淡出_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;
}
这不是工作原理
通过t在颜色a和b之间线性插值
t被夹在0和1之间。当t为0时,返回a。当t为1时,返回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);
}
e、 g.在2秒内消失
注意:在智能手机上输入,但我希望想法变得清晰这确实很有魅力,但我如何才能决定淡出需要多长时间?谢谢你指出lerping的问题,我用这个错误已经很久了。只是添加了持续时间作为参数,使它更清晰、更灵活。因此,只需将其称为
StarCoroutine(FadeTimer(2))代码>例如,在2秒内消失。你是一个传奇人物!
StarCoroutine(FadeTimer(2));