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); }}
阅读全文
0 0
- Unity 抛物线,直线,Sine曲线等
- iOS 之绘制直线、曲线、矩形等
- 直线、抛物线、猜猜看!
- VFP在表单上画图(画直线、曲线等)
- 直线与抛物线的交点
- 缓和曲线03三次抛物线
- 缓和曲线05五次抛物线
- Unity抛物线的脚本
- Unity 半圆抛物线运动
- Unity抛物线AddForce计算
- unity 抛物线 效果演示
- unity抛物线的运动
- Unity抛物线动画
- Unity绘制抛物线
- 过抛物线2交点的直线方程
- 求抛物线与直线相交面积
- 多项式轨迹--直线和抛物线轨迹
- Unity 3D 抛物线算法
- ListIterator双向迭代的简单操作
- 大数据学习之路-第02天-Hadoop本地模式配置&伪分布模式配置
- OpenCV: “vector”: 未声明的标识符
- 基于UDP Socket例子
- 在win7中运行hadoop码出现ClassCastException: class com.sun.jersey.core.impl.provider.entity.XMLJAXBElementPr
- Unity 抛物线,直线,Sine曲线等
- SpringBoot的actuator(执行器)常用的访问地址
- Java 对象
- BZOJ 1566: [NOI2009]管道取珠 另类DP
- python常用模块之os模块
- POJ 1899 Farmer Bill's Problem 笔记
- Qt中信号存在重载情况下的选择
- 数据库常见面试题
- Linux的SOCKET编程详解——非常叼