如何在Unity中画抛物线
2016-06-03 11:13
483 查看
最近在开发HTC VIVE VR新品的时候想模仿The Lab中的移动模式,模仿这个功能首先就是要实现抛物线的效果,在网上找了一下发现有很多大家的解决方案,还有一堆关于抛物线的算法,发现不太好用,所以花了点时间自己实现了一个,共享出来欢迎大家来喷。
using UnityEngine; using System.Collections; using System.Collections.Generic; [ExecuteInEditMode] public class Parabola : MonoBehaviour { //重力 [Range(0,1)] public float gravity = 0.13f; //最大长度 public float maxLength = 50; //两点之间的距离 const float length = 0.2f; //点集合 List<Vector3> m_List = new List<Vector3>(); Material m_LineMat; public void OnRenderObject() { CreateLineMaterial(); m_LineMat.SetPass(0); Vector3 position = transform.position; Vector3 forward = transform.rotation * Vector3.forward * length; Vector3 newPos = position; Vector3 lastPos = newPos; m_List.Add(newPos); int i = 0, iMax = 0; float dis = 0; while (dis < maxLength) { i++; newPos = lastPos + forward + Vector3.up * i * -gravity * 0.1f; dis += Vector3.Distance(lastPos, newPos); m_List.Add(newPos); lastPos = newPos; } GL.Begin(GL.LINES); i = 0; iMax = m_List.Count; for (i = 0; i < iMax; i++) { GL.Vertex(m_List[i]); } GL.End(); m_List.Clear(); } void CreateLineMaterial() { if (!m_LineMat) { var shader = Shader.Find("Hidden/Internal-Colored"); m_LineMat = new Material(shader); m_LineMat.hideFlags = HideFlags.HideAndDontSave; m_LineMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); m_LineMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); m_LineMat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off); m_LineMat.SetInt("_ZWrite", 0); } } }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例