zoukankan      html  css  js  c++  java
  • u3d插值

      在unity3d中经常用线性插值函数Lerp()来在两者之间插值,两者之间可以是两个材质之间、两个向量之间、两个浮点数之间、两个颜色之间,其函数原型如下:

    1.    例子
    2. public class Forest : MonoBehaviour   
    3. {  
    4.    
    5.     public GameObject[] obstacles;     //路障物体数组  
    6.     public float startLength = 50;   //路障在道路上出现的开始位置  
    7.     public float minLength = 100;   //路障距上一个路障的最小距离  
    8.     public float maxLength = 200;   //路障距上一个路障的最大距离  
    9.    
    10.    
    11.     private Transform player;        //游戏主人公-奔跑者的Transform组件  
    12.     private waypoints wayPoints;    //与路面相贴合的路线上的脚本组件  
    13.    
    14.     void Awake()   
    15.     {  
    16.         player = GameObject.FindGameObjectWithTag(Tags.player).transform; //找到游戏主人公-奔跑者并获得它的Transform组件  
    17.         wayPoints = transform.Find("waypoints").GetComponent<waypoints>();  //找到与路面相贴合的路线上的脚本组件  
    18.     }  
    19.    
    20.     // Use this for initialization  
    21.     void Start()  
    22.     {  
    23.         GenerateObstacle();    //当森林道路被创建出来时,就会自动调用此Start()方法,从而调用此GenerateObstacle()方法  
    24.    
    25.     }  
    26.     // 如果主人公跑完了这段道路,则通知GenerateForest类开始运行产生新的道路,并销毁已跑完的这条道路  
    27.     void Update ()   
    28.     {  
    29.         if (player.position.z > transform.position.z+100)   
    30.         {  
    31.             Camera.main.SendMessage("GenerateForest");  
    32.             GameObject.Destroy(this.gameObject);  
    33.         }  
    34.     }  
    35.    
    36.     void GenerateObstacle()  
    37.     {  
    38.         float startZ = transform.position.z - 3000;  //当前道路在场景中的起始Z坐标  
    39.         float endZ = transform.position.z;          //当前道路在场景中的结束Z坐标  
    40.         float z = startZ + startLength;             //将要产生的路障的Z坐标  
    41.         while (true)   
    42.         {  
    43.             z += Random.Range(100, 200);            //每隔100多米的距离产生一个路障  
    44.             if (z > endZ)                           //如果将要产生路障的位置超出了这条道路则退出路障产生循环,否则产生路障  
    45.             {  
    46.                 break;  
    47.             }  
    48.             else   
    49.             {  
    50.                 Vector3 position = GetWayPosByz(z);                    //调用GetWayPosByz()方法计算路障位置坐标  
    51.                 int obsIndex = Random.Range(0, obstacles.Length);      //产生一个从路障数组里取路障的随机序数  
    52.                 GameObject.Instantiate(obstacles[obsIndex], position, Quaternion.identity);//实例化路障  
    53.             }  
    54.         }  
    55.     }  
    56.    
    57.     Vector3 GetWayPosByz(float z)   
    58.     {  
    59.         Transform[] points = wayPoints.points;       //在道路上设置的转折点的集合  
    60.         int index = 0;                               //转折点在集合中的序数号  
    61.         for (int i = 0; i < points.Length-1; i++)   
    62.         { //根据要插入路障的Z值在集合中寻找在哪两个点之间,找到后记下序数号  
    63.             if(z<=points[i].position.z && z>=points[i+1].position.z){  
    64.                 index = i;  
    65.                 break;  
    66.             }  
    67.         }  
    68.         //使用Lerp函数计算出插入路障处的空间坐标值  
    69.         return Vector3.Lerp(points[index + 1].position, points[index].position,(z - points[index + 1].position.z) / (points[index].position.z - points[index +1].position.z));  
    70.     }    
    71. }
  • 相关阅读:
    547. Friend Circles
    399. Evaluate Division
    684. Redundant Connection
    327. Count of Range Sum
    LeetCode 130 被围绕的区域
    LeetCode 696 计数二进制子串
    LeetCode 116 填充每个节点的下一个右侧节点
    LeetCode 101 对称二叉树
    LeetCode 111 二叉树最小深度
    LeetCode 59 螺旋矩阵II
  • 原文地址:https://www.cnblogs.com/fengdaren/p/8805501.html
Copyright © 2011-2022 走看看