Unity 抛物线,直线,Sine曲线等

来源:互联网 发布:即时通信软件排行 编辑:程序博客网 时间:2024/06/05 10:54

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);    }}
原创粉丝点击