c# - 如何实时运行 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;
}
}
解决方案
您将需要知道模拟的帧速率。如果在 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);
推荐阅读
- node.js - 如何发布没有任何 babel 依赖的 npm 模块?
- google-app-engine - 应用引擎自定义域不默认为 HTTPS
- kibana - 如何在 kibana 网址中包含索引“标题”?
- javascript - 是否可以修改网页的源并重新加载该页面而不是从服务器而是从编辑的源?
- go - 一个关于 Go Channel 死锁的简单例子以及为什么
- python-3.x - 如何在键值数据库中进行并行化?
- python - tensorflow.data 批处理方法不批处理数据集中的文本行
- sql - 如何替换句子中的特定单词而不替换 SQL Server 中的子字符串
- wix - Wix 工具集 - 使用计算机名称和 IIS Web 应用程序别名的 URL 快捷方式
- asp.net-core - 如何获取模型属性值并将其分配给另一个模型属性