Unity基础之:UnityAPI的学习

版权声明:

  • 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客"
  • 您可以自由转载,但必须加入完整的版权声明!

Quaternion

  • 表示旋转

  • 矩阵 //9个浮点数,数据占用量大,且除了表示旋转外,还表示缩放((0,0),(1,1),(2,2)点表示x,y,z的三个缩放) //显卡使用

  • 欧拉角 rotation,绕x,y,z轴的旋转量 //给定朝向的表示不惟一,通过限定yaw,roll在+-180度,pitch在+-90度可以解决该问题 (pitch,绕右向量旋转,点头;yaw,绕上向量旋转,摇头;roll,绕前方向,转头;常用于飞行模拟) //万向锁 如:一个平行于x轴的向量绕y轴旋转-90度,会平行于z轴,此时所有绕z轴的旋转都不再起作用

  • 轴角对 //用旋转轴,旋转角度的对偶表示旋转 //旋转角无唯一

  • 单位四元数:[1,0] 1是标量,0是零向量

  • 四元数的模,4个数的平方和开根

  • identity :单位四元数

  • eulerAngles //返回四元数对应的欧拉角,是一个属性

  • W,x,y,z w表示转化后的旋转值,x,y,z表示转化后的旋转轴

  • LookRotation //给定一个方向向量,返回表示该方向旋转的四元数

  • public static Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up);

  • forward:The direction to look in. upwards:The vector that defines in which direction up is.//定义上方向是什么,一般不用改这个

public class ExampleClass : MonoBehaviour
{
    public Transform target;

    void Update()
    {
        Vector3 relativePos = target.position - transform.position;  //向量代表大小和方向,用target和transtom的向量相减,得到的向量从transform指向target

        // the second argument, upwards, defaults to Vector3.up
        Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);  //该LookRotaion函数,不改变物体positon的位置,只改变Rotation实现朝向
        transform.rotation = rotation;
    }
}
  • Angle //计算两个四元数的角度差

  • public static float Angle(Quaternion a, Quaternion b); //用两个物体的transform的rotation属性计算两个四元数的角度差,和positon没有关系,纯粹就是两个物体rotation属性之间的差值,旋转差值

  • Euler //根据欧拉角构造四元数

  • public static Quaternion Euler(float x, float y, float z);

  • Slerp //角度旋转差值 //不要用Lerp,因为Lerp是线性差值,旋转中一般不用线性相关

  • public static Quaternion Slerp(Quaternion a, Quaternion b, float t); //和position没有关系,只和物体本身的旋转角度有关系

  • SlerpUnclamped //可以过冲的角度旋转差值

  • RotateTowards //旋转指定角度差值,maxDegreesDelta表示一次最大旋转角度

  • public static Quaternion RotateTowards(Quaternion from, Quaternion to, float maxDegreesDelta)

  • FromToRotation //计算从一个方向转到另一个方向需要旋转的四元数 //通常使用它来旋转变换

  • public static Quaternion FromToRotation(Vector3 fromDirection, Vector3 toDirection);

public class Example : MonoBehaviour
{
    void Update()
    {
        // Sets the rotation so that the transform's y-axis goes along the z-axis
        transform.rotation = Quaternion.FromToRotation(Vector3.up, transform.forward);   //一个物体的前方向是它的Z轴,第一次执行时up和forward之间差90度,此时物体的rotaiton为90(绕x轴旋转90度);第二次up和forword之间就差了180,此时物体的rotation为180,(在原始位置绕x轴旋转90度,看起来相对前一个状态旋转了又旋转了90度)
    }
}
  • AngleAxis //用轴角对,构建一个四元数

  • public static Quaternion AngleAxis(float angle, Vector3 axis);

  • ToAngleAxis //把该四元数转换为轴角对

  • public void ToAngleAxis(out float angle, out Vector3 axis);

  • Inverse //反向旋转, //或者从一个旋转坐标系转换转换到另一个旋转坐标系(一般不单独使用旋转坐标系,具体方法例:在原始坐标系中选择个比如45度, Quaternion.Inverse(target.rotation)得到一个反向选择的四元数inv,用inv * gameobject.position就得到了该gameobjec在该选择之后的坐标空间中的位置)

  • public static Quaternion Inverse(Quaternion rotation);

  • 例: transform.rotation = Quaternion.Inverse(target.rotation);

    • // 乘法 //连续旋转,右乘以一个四元数(从左到右依次旋转,不具有交换律) //旋转一个向量 右乘一个向量
  • 思考题,如何对一个向量进行连续旋转q,w,e(四元数)

  • (e(w(q*v)))//记得左乘,四元数与向量相乘的结果为一个向量

平面与射线 Plan & Ray //平面常用于判断其和一个点和射线的关系

  • 平面可以由一个法线n与平面上的任意一个向量p0表示

  • 平面可以由4个值a,b,c,d确定,(a,b,c)表示法线向量n(一个单位向量),d表示常量d(表示从原点到平面最短的距离)

  • 平面上的构造方式:1:法线向量n,平面个上的一点p0,可以计算出d值,并构造出平面 2:平面上的三个点

  • 平面的正面为法线方向

  • 构造:

  • 点法式:public Plane(Vector3 inNormal, Vector3 inPoint);

  • 法线截距式:public Plane(Vector3 inNormal, float d);

  • 三点式:public Plane(Vector3 a, Vector3 b, Vector3 c);

  • 属性:

  • 到原点的距离 distance

  • 翻转之后的平面 flipped

  • 取得平面的法线 normal

  • public method:

  • 与点的距离: GetDistanceToPoint

  • 点在平面正面还是背面:GetSide

  • 点在平面上的投影:CloestPointOnPlane

  • 判断两个点是否在同一个面上:SameSide

  • 与射线的关系 Raycast

  • public bool Raycast(Ray ray, out float enter);

  • 返回是否相交;若相交,则enter参数返回射线发出后到平面的距离;若平行,enter = 0;若不相交,则enter参数返回负值(沿射线反方向延长到平面的距离)

  • 翻转平面 Flip

  • 平移平面 Translate

  • 平面的用途:表示平滑的表面(墙,地板);表示游戏边界(球场的边界,边线)

Ray

  • 要有原点,和一个发射方向

  • 属性:

  • 方向 : direction

  • 原点:origin

  • 构造:

  • 指定一个原点,一个方向

  • public Ray(Vector3 origin, Vector3 direction);

  • public methods:

  • 取得沿射线方向延长任意长度后的点:

  • public Vector3 GetPoint(float distance);

Transform

  • 节点、层级管理
  • 旋转
  • 平移
  • 缩放
  • 坐标变换

    // Moves all transform children 10 units upwards!
    void Start()
    {
        foreach (Transform child in transform)  //遍历所有transform的子节点  //实现了IEnumerator的接口
        {
            child.position += Vector3.up * 10.0f;
        }
    }
  • 属性:

  • childCout 子节点的数量

  • eulerAngles 欧拉角 //在世界坐标系中

  • localEulerAngles //相对于父节点的选择量

  • rotation

  • localRotation

  • position

  • localPosition

  • lossyScale //相对于世界空间的全局缩放

  • localScale //局部缩放,相对于父节点

  • parent 父节点

  • root //不是代表这个场景的根节点,代表这个分支的节点 //所以一般会创建一个GameRoot的节点放入所有的GameObject

  • hasChanged 节点时候发生了修改

  • forward 表示物体的前方向 //相对于该物体本身而言,不同于世界坐标系

  • up 表示物体的上方向

  • right 表示物体的左方向

  • localToWorldMatrix 局部到世界 //一般不用矩阵转换,用封装好的Api: TransromPoint/Direction/Vector

  • worldToLocalMatrix 世界到局部 //一般不用矩阵转换,用封装好的Api: InverseTransformPoint/Direction/Vector

  • public methods:

  • DetachChildren //断开所有子节点 //常用于删除父节点,而不影响子节点的情况下 //此时所有子节点被放到场景最上层 //若想放到自己的父节点上需要遍历,并这种child.parent = this.parent;

  • Find //以当前节点为根,查找子节点 //或者以整个场景为根,查找子节点

  • GetChild //获取子节点

  • GetSiblingIndex //返回当前节点在其父节点下的index

  • IsChildOf //判断是否是谁的Child

  • SetAsFirstSibling //设置为同级节点的第一个

  • SetAdLastSibling //设置为同级节点的最后一个

  • SetSiblingIndex //设置为同级节点的第几个

  • SetParent //设置一个节点的父节点

  • public void SetParent(Transform parent, bool worldPositionStays); //worldPositionStays 表示是否保持其世界坐标不变,若为false表示其相对于其父节点的坐标偏移不变

  • LookAt //物体的position不变,改变rotation,使物体的forward方向看向某一个物体

  • public void LookAt(Transform target, Vector3 worldUp = Vector3.up);

  • Rotate // 指定一个欧拉角,以及坐标系,按照该欧拉角进行旋转

  • public void Rotate(Vector3 eulers, Space relativeTo = Space.Self);

  • public void Rotate(float xAngle, float yAngle, float zAngle, Space relativeTo = Space.Self);

  • public void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self);

  • RotateAround //以某个点为中心,以经过该点的轴为选择轴,旋转一定的角度 //均为世界坐标系的点和旋转轴

  • public void RotateAround(Vector3 point, Vector3 axis, float angle);
    void Update()
    {
    //以20度/秒的速度旋转世界原点周围的物体。
    transform.RotateAround(Vector3.zero,Vector3.up,20 * Time.deltaTime);
    }

  • transform.RotateAround(target.position, vector3.Up, 180 * Time.deltaTime); //绕着某个物体,以世界坐标系y轴为旋转轴转动

  • transform.RotateAround(target.positon, target.Up, 180*Time.deltaTime); //始终绕着某个物体的Up坐标转动

  • SetPositonAndRotation //设置Transform组件在世界空间位置和旋转

  • public void SetPositionAndRotation(Vector3 position, Quaternion rotation);

  • Translate //平移

  • public void Translate(Vector3 translation, Space relativeTo = Space.Self); //默认以自身坐标系为参考

    void Update()
    {
        // Move the object forward along its z axis 1 unit/second.
        transform.Translate(Vector3.forward * Time.deltaTime);

        // Move the object upward in world space 1 unit/second.
        transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    }
  • 一个方便的组件:CharacterController 角色控制器,受Collider影响,不受力的影响(Rigidbody) //简单的控制角色,提供move,simpleMove方法移动角色

  • TransformPoint //把一个点从该局部坐标系,转换为世界坐标系 //注意,返回的位置受比例(缩放)影响

  • public Vector3 TransformPoint(Vector3 position);

  • 例子:比如说我想把一个物体放到另一个物体的左边两个单位的位置:

  • this.position = target.transform.TransformPoint(vector3.right * 2);

  • InverseTransformPoint //把一个点从世界坐标系,转换为该局部坐标系 //注意,返回的位置受比例影响

  • public Vector3 InverseTransformPoint(Vector3 position);

  • 例子:判断物体是否在摄像机的前方:

  • Camera.main.transform.InverseTransformPoint(this.position).z > 0f; 若为ture表示在摄像机的前方,在摄像机坐标系下z轴大于0

  • TransformDirection // direction从当地空间转变为世界空间。

  • InverseTransformDirection // direction从世界空间转变为当地空间。

  • TransformVector //vector从当地空间转变为世界空间。

  • InverseTransformDirection //vector从世界空间转变为当地空间。

Mesh //绘制一个顶点着色的,支持光照和贴图的三角形

  • 需要一个网格模型,Mesh Filter表示形体

  • Mesh Renderer负责把三角形经过灯光和贴图处理后的东西画出来

  • 绘制一个三角形: //所有模型都有一个个三角形拼凑而成

   void Start()
    {
        mesh = GetComponent<MeshFilter>().mesh;

        //Mesh = Vertices(pos + color + uv) + Indices(那三个顶点构成一个三角形)        //顶点+索引

        mesh.vertices = new[] {   //三角形的三个点
            new Vector3(-5, -5, 0),
            new Vector3(0, 5, 0),
            new Vector3(5, -5, 0)
        };

        mesh.triangles = new[] {  //哪三个点按照什么方式组成三角形
            0, 1 , 2
        };


        mesh.colors = new Color[] {  //顶点着色
                new Color(1, 0, 0, 1),
                new Color(0, 1, 0, 1),
                new Color(0, 0, 1, 1),
        };

        mesh.uv = new[] {     //顶点贴图,uv值
            new Vector2(0, 1),
            new Vector2(1,1),
            new Vector2(0.5f, 0.5f),
        };
    }
  • uv坐标: // 贴图坐标,指定要贴图的顶点的uv坐标 //在Dx下U方向向右,v方向向下 //openGL下U方向向右,v方向向上

  • 画一个三棱锥:

  void Start()
    {
        mesh = GetComponent<MeshFilter>().mesh;

        //Mesh = Vertices(pos + color + uv) + Indices(那三个顶点构成一个三角形)        //顶点+索引

        mesh.vertices = new[] {
            new Vector3(0, 5, 0),
            new Vector3(-5, 0, 5),
            new Vector3(0, 0, -5),
            new Vector3(5, 0, 5),
        };

        mesh.triangles = new[] {   //每个三角形都有一个平面,只有法线方向的平面才能被看到,每个三角形遵循左手定则,
            0, 2,1,   //第一个面法线向右上,左手顺时针选择,0,2,1代表表示该三角形及其法线
            0,3,2,
            0,1,3,
            1,2,3,
        };


        mesh.colors = new Color[] {
                new Color(1, 0, 0, 1),
                new Color(0, 1, 0, 1),
                new Color(0, 0, 1, 1),
                new Color(1,1,0,1),
        };

        mesh.uv = new[] {       //0点uv(0.5f,0);  1点uv(0,1);2点的uv可以把132看成一个新的三角形,3的uv为(1,1),那么此时按照等边三角形的规律2就应为(0.5,2);
            new Vector2( 0.5f, 0),
            new Vector2(0,1),
            new Vector2(0.5f, 2),
            new Vector2(1, 1),
        };
    }
  • 指定顶点法线:
        mesh.normals = new[] {
            Vector3.up,
            Vector3.left,
            Vector3.back,
            Vector3.right
        };
  • 做一个波纹的效果://用光照实现
    void Update()
    {
        mesh.normals = new[] {
            Vector3.up,
            new Vector3(Mathf.Cos(Time.time), 0, Mathf.Sin(Time.time)),
            Vector3.back,
            Vector3.right
        };
    }
  • 面光照(FLAT_SHADING) //unity默认为面光照,只计算一个面的法线,然后按照法线进行光线的渲染 //节约性能
  • 顶点光照(GOUROUD_SHADING) //每个点都有一个法线,更真实,因为是用三角形去表示曲面,面关照就很显得这个面是平的,而不是曲线
  • 像素光照
posted @ 2019-08-08 09:23  优梦创客  阅读(2164)  评论(0编辑  收藏  举报