首页 > 解决方案 > 如何实时运行 60 秒信号?

问题描述

我正在 Unity 上模拟肺部。我有一个控制我的肺部对象的音量信号,它被采样了 60 秒。当我运行它时,它会在几秒钟内完成整个信号。但是,我需要实时模拟并运行整个 60 秒。我不想以慢动作运行它,因为它看起来不真实。

private int i = 0;                  // initialize  iterator
void Update()
{
 if (i < MainFlow.Length)
 {
  float f = (float)MainFlow[i];    // value of current volume
  float k = f + 2.5f;              // adding initial volume
  transform.localScale = new Vector3(k, k, k);
  i++;          
 }
 else
 {  
  i = 1;
 }
}

标签: c#visual-studiounity3d

解决方案


您将需要知道模拟的帧速率。如果在 60 秒内有 1500 个样本,那大概是 25Hz。然后,您可以对每帧的两个 MainFlow 值进行采样,并在它们之间进行插值以产生平滑的输出。

像这样的东西:

float frequency = 25.0f;
float simulationTime = Time.time * frequency;
int firstFrameIndex = Mathf.Clamp(Mathf.FloorToInt(simulationTime), 0, MainFlow.length);
int secondFrameIndex = Mathf.Clamp(firstFrameIndex + 1, 0, MainFlow.length);
float fraction = simulationTime - firstFrameIndex;
float sample1 = (float)MainFlow[firstFrameIndex];
float sample2 = (float)MainFlow[secondFrameIndex];
float k = Mathf.Lerp(sample1, sample2, fraction) + 2.5f;
transform.localScale = new Vector3(k, k, k);

推荐阅读