Unity 拋物線,直線,Sine曲線等

1.用粒子製作拋物線。

1.創建一個枚舉

public enum FunctionOption {
        Linear,
        Exponential,
        Parabola,
        Sine
    }

2.創建靜態方法

//直線
private static float Linear (float x) {
        return x;
    }
    //曲線
    private static float Exponential (float x) {
        return x * x;
    }
   //拋物線
    private static float Parabola (float x){
        x = 2f * x - 1f;
        return x * x;
    }
    //Sine 曲線 可以通過相位來控制Shader水的流動,有興趣的可以去實現
    private static float Sine (float x){
        return 0.5f + 0.5f * Mathf.Sin(2 * Mathf.PI * x + Time.timeSinceLevelLoad);
    }

3.創建委託函數

private delegate float FunctionDelegate (float x);
    private static FunctionDelegate[] functionDelegates = {
        Linear,
        Exponential,
        Parabola,
        Sine
    };
    public FunctionOption function;

4.創建點

private void CreatePoints () {
        currentResolution = resolution;
        points = new ParticleSystem.Particle[resolution];
        float increment = 1f / (resolution - 1);
        for (int i = 0; i < resolution; i++) {
            float x = i * increment;
            points[i].position = new Vector3(x, 0f, 0f);
            points[i].color = new Color(x, 0f, 0f);
            points[i].size = 0.1f;
        }
    }

    void Update () {
        if (currentResolution != resolution || points == null) {
            CreatePoints();
        }
        FunctionDelegate f = functionDelegates[(int)function];
        for (int i = 0; i < resolution; i++) {
            Vector3 p = points[i].position;
            p.y = f(p.x);
            points[i].position = p;
            Color c = points[i].startColor;
            c.g = p.y;
            points[i].startColor = c;

        }

        GetComponent<ParticleSystem>().SetParticles(points, points.Length);
    }

5.全部代碼如下 ,並把他放在粒子物體上,不過這個一直是在Update裏面執行,所以性能會比較耗性能,這
主要測試功能用的。

using UnityEngine;

public class Test : MonoBehaviour {

    public enum FunctionOption {
        Linear,
        Exponential,
        Parabola,
        Sine
    }

    private delegate float FunctionDelegate (float x);
    private static FunctionDelegate[] functionDelegates = {
        Linear,
        Exponential,
        Parabola,
        Sine
    };

    public FunctionOption function;

    [Range(10, 100)]
    public int resolution = 10;

    private int currentResolution;
    private ParticleSystem.Particle[] points;

    private void CreatePoints () {
        currentResolution = resolution;
        points = new ParticleSystem.Particle[resolution];
        float increment = 1f / (resolution - 1);
        for (int i = 0; i < resolution; i++) {
            float x = i * increment;
            points[i].position = new Vector3(x, 0f, 0f);
            points[i].color = new Color(x, 0f, 0f);
            points[i].size = 0.1f;
        }
    }

    void Update () {
        if (currentResolution != resolution || points == null) {
            CreatePoints();
        }
        FunctionDelegate f = functionDelegates[(int)function];
        for (int i = 0; i < resolution; i++) {
            Vector3 p = points[i].position;
            p.y = f(p.x);
            points[i].position = p;
            Color c = points[i].startColor;
            c.g = p.y;
            points[i].startColor = c;

        }

        GetComponent<ParticleSystem>().SetParticles(points, points.Length);
    }

    private static float Linear (float x) {
        return x;
    }

    private static float Exponential (float x) {
        return x * x;
    }

    private static float Parabola (float x){
        x = 2f * x - 1f;
        return x * x;
    }

    private static float Sine (float x){
        return 0.5f + 0.5f * Mathf.Sin(2 * Mathf.PI * x + Time.timeSinceLevelLoad);
    }
}
發佈了76 篇原創文章 · 獲贊 23 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章