简体   繁体   English

如何在Unity中制作正弦波发生器?

[英]How make a Sine wave generator in Unity?

I want to create a Sine Wave generated from a unit circle in Unity whose amplitude and frequency can be dynamically changed by changing the speed and radius of the circle. 我想创建一个从Unity中的单位圆生成的正弦波,其振幅和频率可以通过更改圆的速度和半径来动态更改。 Ex: 例如: 在此处输入图片说明

The result should look something like this(see pictures below) where one continuous curve shows the sustained changes in frequency and amplitude when looked over time: 结果应如下所示(请参见下图),其中一条连续曲线显示了随着时间的推移,频率和幅度的持续变化: 在此处输入图片说明 在此处输入图片说明

This is how I'm currently approaching the problem to produce Sine wave (this script is not linked to the circle right now) but it doesn't allow for the older values to be sustained overtime.. 这就是我目前正在处理产生正弦波的问题的方式(此脚本目前尚未链接到圆),但不允许较旧的值随着时间的推移而持续。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class generateSineWave : MonoBehaviour {

    [Range(0.1f,20.0f)]
    public float heightScale = 5.0f;
    [Range(0.1f,40.0f)]
    public float detailScale = 5.0f;

    public GameObject wavePlane;

    private Mesh myMesh;
    private Vector3[] vertices;
    /*
    //Unit Circle properties
    public Transform target;
    public float orbitDistance = 1.0f;
    public float orbitDegreesPerSec = 90.0f;
    public GameObject Ball;
    */

    void Update()
    {
        GenerateWave();
    }

    void GenerateWave()
    {
         myMesh = wavePlane.GetComponent<MeshFilter>().mesh;
         vertices = myMesh.vertices;

         int counter = 0; //i
         int yLevel = 0; //j

        for (int i = 0; i < 11; i++)
        {
            for (int j = 0; j < 11; j++)
            {
                CalculationMethod(counter, yLevel);
                counter ++;
            }
            yLevel ++;
        }

        myMesh.vertices = vertices;
        myMesh.RecalculateBounds();
        myMesh.RecalculateNormals();

        Destroy(wavePlane.GetComponent<MeshCollider>());
        MeshCollider collider = wavePlane.AddComponent<MeshCollider>();
        collider.sharedMesh = null;
        collider.sharedMesh = myMesh;

    }

    //public bool waves = false;
    //public float waveSpeed = 5.0f;

    void CalculationMethod(int i, int j)
    {
                    //radius * Mathf.Sin(speed* Time.time + vertices[i].x + phase shift)
        vertices[i].z = Mathf.Sin(Time.time + vertices[i].x);
        vertices[i].z -=j;

    }

}

I looked at the Unity Asset Store, there are a few wave generators, but none of them work with varying amplitude and frequency such that the resulting curve is one continuous curve where previous amplitudes and frequency still show in the editor. 我看了一下Unity Asset Store,那里有一些波发生器,但是它们都不以变化的振幅和频率工作,因此生成的曲线是一条连续的曲线,以前的振幅和频率仍显示在编辑器中。

I don't quite understand how you're going to use generator, but you can just calculate next value for your graph or whatever on update. 我不太了解您将如何使用生成器,但是您可以只计算图形的下一个值或更新时的任何值。

void Generate(double previousPositionOnCircleInRadian, double speed,
    double amplitude, out double sinValueMultiliedByAmplitude, 
    out double nextPositionOnCircleInRadian)
{
    nextPositionOnCircleInRadian = previousPositionOnCircleInRadian + Time.deltaTime*speed;
    sinValueMultiliedByAmplitude = amplitude * Math.Sin(nextPositionOnCircleInRadian);
}

I guess it's all you need to calculate position on your circle and sinwave. 我想这就是您计算圆和sinwave上的位置所需的全部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM