unity3D:游戏分解之曲线
一提到曲线,很多新手就头疼了,包括我。查了很多资料,终于有个大概的了解。想深入了解曲线原理的,推荐一个链接http://www.cnblogs.com/jay-dong/archive/2012/09/26/2704188.html
之前写了一篇博文《unity3D:游戏分解之角色移动和相机跟随》,里面用到了曲线插值,这里算是对上篇博文的一个补充
先看一下曲线的效果

在使用NGUI的过程中,发现iTween.cs里面有两个很有用的方法,一个是输入指定路点数组,一个就是曲线的插值算法。今天我们主要就用到这两个方法来实现曲线效果。
public static Vector3[] PathControlPointGenerator(Vector3[] path)
{
Vector3[] suppliedPath;
Vector3[] vector3s; //create and store path points:
suppliedPath = path; //populate calculate path;
int offset = ;
vector3s = new Vector3[suppliedPath.Length + offset];
Array.Copy(suppliedPath, , vector3s, , suppliedPath.Length); //populate start and end control points:
//vector3s[0] = vector3s[1] - vector3s[2];
vector3s[] = vector3s[] + (vector3s[] - vector3s[]);
vector3s[vector3s.Length - ] = vector3s[vector3s.Length - ] + (vector3s[vector3s.Length - ] - vector3s[vector3s.Length - ]); //is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
if (vector3s[] == vector3s[vector3s.Length - ])
{
Vector3[] tmpLoopSpline = new Vector3[vector3s.Length];
Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);
tmpLoopSpline[] = tmpLoopSpline[tmpLoopSpline.Length - ];
tmpLoopSpline[tmpLoopSpline.Length - ] = tmpLoopSpline[];
vector3s = new Vector3[tmpLoopSpline.Length];
Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);
} return (vector3s);
} //andeeee from the Unity forum's steller Catmull-Rom class ( http://forum.unity3d.com/viewtopic.php?p=218400#218400 ):
public static Vector3 Interp(Vector3[] pts, float t)
{
int numSections = pts.Length - ;
int currPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - );
float u = t * (float)numSections - (float)currPt; if(currPt == )
{
int dsd = ;
} Vector3 a = pts[currPt];
Vector3 b = pts[currPt + ];
Vector3 c = pts[currPt + ];
Vector3 d = pts[currPt + ]; return .5f * (
(-a + 3f * b - 3f * c + d) * (u * u * u)
+ (2f * a - 5f * b + 4f * c - d) * (u * u)
+ (-a + c) * u
+ 2f * b
);
}
直接上完整代码,把这个脚本放到相机上,然后在场景中拖几个物件作为路点,就可以实现上面的效果
using System;
using System.Collections.Generic;
using UnityEngine; namespace Fish.Study.Curve
{
/// <summary>
/// 曲线测试
/// </summary>
public class CurveTest : MonoBehaviour
{
//路点
public GameObject[] GameObjectList;
//各路点的坐标
public List<Vector3> TransDataList = new List<Vector3>(); void Start()
{
} //Gizmos
void OnDrawGizmos()
{
//1个点是不能画出曲线的,2个点实际上是直线
if (GameObjectList.Length <= ) return; TransDataList.Clear();
for (int i = ; i < GameObjectList.Length; ++i)
{
TransDataList.Add(GameObjectList[i].transform.position);
} if (TransDataList != null && TransDataList.Count > )
{
DrawPathHelper(TransDataList.ToArray(), Color.red);
}
} public Vector3[] GetCurveData()
{
if (TransDataList != null && TransDataList.Count > )
{
var v3 = (TransDataList.ToArray());
Vector3[] vector3s = PathControlPointGenerator(v3);
return vector3s;
} return null;
} //NGUI iTween.cs中的方法,输入路径点
public static Vector3[] PathControlPointGenerator(Vector3[] path)
{
Vector3[] suppliedPath;
Vector3[] vector3s; //create and store path points:
suppliedPath = path; //populate calculate path;
int offset = ;
vector3s = new Vector3[suppliedPath.Length + offset];
Array.Copy(suppliedPath, , vector3s, , suppliedPath.Length); //populate start and end control points:
vector3s[] = vector3s[] + (vector3s[] - vector3s[]);
vector3s[vector3s.Length - ] = vector3s[vector3s.Length - ] + (vector3s[vector3s.Length - ] - vector3s[vector3s.Length - ]); //is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
if (vector3s[] == vector3s[vector3s.Length - ])
{
Vector3[] tmpLoopSpline = new Vector3[vector3s.Length];
Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);
tmpLoopSpline[] = tmpLoopSpline[tmpLoopSpline.Length - ];
tmpLoopSpline[tmpLoopSpline.Length - ] = tmpLoopSpline[];
vector3s = new Vector3[tmpLoopSpline.Length];
Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);
} return (vector3s);
} //曲线插值函数
public static Vector3 Interp(Vector3[] pts, float t)
{
int numSections = pts.Length - ;
int currPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - );
float u = t * (float)numSections - (float)currPt; Vector3 a = pts[currPt];
Vector3 b = pts[currPt + ];
Vector3 c = pts[currPt + ];
Vector3 d = pts[currPt + ]; return .5f * (
(-a + 3f * b - 3f * c + d) * (u * u * u)
+ (2f * a - 5f * b + 4f * c - d) * (u * u)
+ (-a + c) * u
+ 2f * b
);
} //画曲线
private void DrawPathHelper(Vector3[] path, Color color)
{
Vector3[] vector3s = PathControlPointGenerator(path); //Line Draw:
Vector3 prevPt = Interp(vector3s, );
int SmoothAmount = path.Length * ;
for (int i = ; i <= SmoothAmount; i++)
{
float pm = (float)i / SmoothAmount;
Vector3 currPt = Interp(vector3s, pm); Gizmos.color = color;
Gizmos.DrawSphere(currPt, 0.2f);
prevPt = currPt;
}
}
}
}
unity3D:游戏分解之曲线的更多相关文章
- Unity3d游戏中自定义贝塞尔曲线编辑器[转]
关于贝塞尔曲线曲线我们再前面的文章提到过<Unity 教程之-在Unity3d中使用贝塞尔曲线>,那么本篇文章我们来深入学习下,并自定义实现贝塞尔曲线编辑器,贝塞尔曲线是最基本的曲线,一般 ...
- Unity3D游戏开发初探—2.初步了解3D模型基础
一.什么是3D模型? 1.1 3D模型概述 简而言之,3D模型就是三维的.立体的模型,D是英文Dimensions的缩写. 3D模型也可以说是用3Ds MAX建造的立体模型,包括各种建筑.人物.植被. ...
- Unity3D游戏在iOS上因为trampolines闪退的原因与解决办法
http://7dot9.com/?p=444 http://whydoidoit.com/2012/08/20/unity-serializer-mono-and-trampolines/ 确定具体 ...
- unity3d 游戏插件 溶解特效插件 - Dissolve Shader
unity3d 游戏插件 溶解特效插件 - Dissolve Shader 链接: https://pan.baidu.com/s/1hr7w39U 密码: 3ed2
- 将Unity3D游戏移植到Android平台上
将Unity3D游戏移植到Android平台是一件很容易的事情,只需要在File->Build Settings中选择Android平台,然后点击Switch Platform并Build出ap ...
- 从一点儿不会开始——Unity3D游戏开发学习(一)
一些废话 我是一个windows phone.windows 8的忠实粉丝,也是一个开发者,开发数个windows phone应用和两个windows 8应用.对开发游戏一直抱有强烈兴趣和愿望,但奈何 ...
- unity3d游戏无法部署到windows phone8手机上的解决方法
今天搞了个unity3d游戏,准备部署到自己的lumia 920上,数据线连接正常,操作正常,但是“build”以后,始终无法部署到手机上,也没有在选择的目录下生产任何相关文件.(你的系统必须是win ...
- Unity3D游戏UI开发经验谈
原地址:http://news.9ria.com/2013/0629/27679.html 在Unity专场上,108km创始人梁伟国发表了<Unity3D游戏UI开发经验谈>主题演讲.他 ...
- Unity3D游戏开发之连续滚动背景
Unity3D游戏开发之连续滚动背景 原文 http://blog.csdn.net/qinyuanpei/article/details/22983421 在诸如天天跑酷等2D游戏中,因为游戏须要 ...
随机推荐
- php 启动过程 - reqeust RINIT 过程
php 启动过程 - reqeust RINIT 过程 概述 apache 接收到请求之后, 交给 php 处理 php 模块在接收到请求后, 会对请求进行初始化, 及 RINIT 过程 调用触发 a ...
- 搜索结果Refinement 行为总结之 multi-selection refinement
几乎所有的购物网站的搜索结果页面都会提供refinement (filtering) 给用户去过滤产品,以便能更快找到自己想要的产品.(做的都是国外的项目,不太清楚这个功能地道的中文名是什么.所以就暂 ...
- spring-mvc @Controller 200-不生效
复杂的故事简单说,复杂的问题简单做.问题记录. 现象 新增加一个Controller,但在js中调用时报请求200,无请求反馈,重启服务多次,问题依旧. controller 分析 从问题现象分析:2 ...
- 利用Apache POI 实现简单的Excel表格导出
1.利用POI API实现简单的Excel表格导出 首先假设一个学生实体类: package com.sun.poi.domain; import java.io.Serializable; impo ...
- Linux的kobject机制
作者:Younger Liu 本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 未本地化版本许可协议进行许可. sysfs文件系统下的每个目录对应于一个kobj,kset是kobj的封装,内 ...
- MySQL性能优化之参数配置
1.目的: 通过根据服务器目前状况,修改Mysql的系统参数,达到合理利用服务器现有资源,最大合理的提高MySQL性能. 2.服务器参数: 32G内存.4个CPU,每个CPU 8核. 3.MySQL目 ...
- ubuntu实用命令--软件管理
近期重新拿起linux的书看了下,整理了一下linux的命令. ubuntu预装了APT和dpkg ,“APT”是 “Advanced Package Tool”的简写,“dpkg ”是“Debian ...
- Java中this关键字的作用
转载: 原文地址:http://www.cnblogs.com/lzq198754/p/5767024.html 一.this关键字主要有三个应用: (1)this调用本类中的属性,也就是类中的成员变 ...
- mysql5.6源码自动安装脚本
将脚本与源码安装包放在同一目录下,执行脚本即可(执行脚本会使用yum安装依赖包) 安装完成之后,既可以使用mysql -uroot -p登录 脚本内容如下: [root@mysql src]# c ...
- Js调用exe程序方法(通过URL Protocol实现网页调用本地应用程序)
1.使用记事本(或其他文本编辑器)创建一个protocal.reg文件,并写入以下内容 Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROO ...