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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值