版權聲明:
- 本文原創發布於博客園"優夢創客"的博客空間(網址:
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) //每個點都有一個法線,更真實,因為是用三角形去表示曲面,面關照就很顯得這個面是平的,而不是曲線
- 像素光照