用來可視化了解各種矩陣變換的代碼
用來可視化了解各種矩陣變換的代碼。直接創(chuàng)建即可用。代碼注釋完整
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
publicclassCoordinateSystem : MonoBehaviour
{
publicTransform RedPoint;
publicTransform BluePoint;
publicCamera ccamera;
publicTransform transformCamera;
publicTransform transformModel;
publicTransform transformWorld;
publicColor[] cameraSystemColor;
publicColor[] worldSystemColor;
publicColor[] modelSystemColor;
publicvoidOnDrawGizmos()
{
//世界空間坐標(biāo)系
DrawACoordinate(transformWorld,worldSystemColor);
//攝像機(jī)空間坐標(biāo)系
DrawACoordinate(transformCamera,cameraSystemColor);
//模型空間坐標(biāo)系
DrawACoordinate(transformModel,modelSystemColor);
}
privatevoid DrawACoordinate(Transform inT,Color[] DrawColor)
{
Gizmos.color = DrawColor[0];
Gizmos.DrawLine(inT.position,inT.position + inT.forward);
Gizmos.color = DrawColor[1];
Gizmos.DrawLine(inT.position,inT.position + inT.up);
Gizmos.color = DrawColor[2];
Gizmos.DrawLine(inT.position,inT.position + inT.right);
}
publicvoid CreateModelToWorldMatrix()
{
Matrix4x4 mMatrix =Matrix4x4.TRS(transformModel.position,transformModel.rotation, transformModel.lossyScale);
Debug.Log(transformModel.localToWorldMatrix.ToString()+"\n\n"+mMatrix.ToString());
//取模型空間的原點(diǎn),轉(zhuǎn)移到世界坐標(biāo)系中
RedPoint.position = mMatrix *newVector4(0, 0, 0, 1);
}
publicvoid CreateWorldToCameraMatrix()
{
//將世界坐標(biāo)的攝像機(jī)信息構(gòu)建一個(gè)變換矩陣
//需要注意的是,Z方向的軸向是反的,這是因?yàn)?/p>
//camera space matches OpenGLconvention: camera's forward is the negative Z axis.
//This is different from Unity'sconvention, where forward is the positive Z axis.
//OpenGL和unity在Z軸的正方定義是相反的
Matrix4x4 cMatrix =Matrix4x4.TRS(transformCamera.position,transformCamera.rotation,newVector3(1,1,-1));
//計(jì)算這個(gè)攝像機(jī)的逆
cMatrix = Matrix4x4.Inverse(cMatrix);
Debug.Log(ccamera.worldToCameraMatrix.ToString()+ "\n\n" + cMatrix.ToString());
Vector3 cameraPosition = cMatrix *newVector4(RedPoint.position.x,RedPoint.position.y, RedPoint.position.z, 1.0f);
BluePoint.localPosition =newVector3(cameraPosition.x,cameraPosition.y,cameraPosition.z*-1);
Debug.Log("紅點(diǎn)在攝像機(jī)空間的坐標(biāo)為" +BluePoint.localPosition.ToString());
}
publicvoid CreateProjectionMatrix()
{
//Debug.Log(ccamera.projectionMatrix.ToString()+ "\n\n"
Matrix4x4 pMatrix = Matrix4x4.Perspective(ccamera.fieldOfView,ccamera.aspect, ccamera.nearClipPlane, ccamera.farClipPlane);
Vector3 bl = BluePoint.localPosition;
Vector3 screenPosition = pMatrix * newVector4(bl.x, bl.y, bl.z, 1.0f);
screenPosition.Normalize();
Vector2 v2ScreenPosition =newVector2(screenPosition.x*0.5f+0.5f,screenPosition.y*0.5f+0.5f);
Debug.Log(ccamera.projectionMatrix + "\n\n" + pMatrix.ToString());
Debug.Log("紅點(diǎn)在投影后的坐標(biāo)為" + v2ScreenPosition.ToString());
Debug.Log(ccamera.WorldToViewportPoint(BluePoint.position));
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(CoordinateSystem))]
publicclassCoordinateSystemEditor : Editor
{
publicoverridevoid OnInspectorGUI()
{
DrawDefaultInspector();
CoordinateSystem myScript = (CoordinateSystem)target;
if (GUILayout.Button("重置"))
{
myScript.RedPoint.position = Vector3.zero;
myScript.BluePoint.localPosition = Vector3.zero;
}
if (GUILayout.Button("將一個(gè)頂點(diǎn)從本地坐標(biāo)系轉(zhuǎn)換到世界坐標(biāo)系"))
{
myScript.CreateModelToWorldMatrix();
}
if (GUILayout.Button("將一個(gè)頂點(diǎn)從世界坐標(biāo)系轉(zhuǎn)換到視空間坐標(biāo)系"))
{
myScript.CreateWorldToCameraMatrix();
}
if (GUILayout.Button("將一個(gè)視空間坐標(biāo)系的內(nèi)容投影到2D平面上"))
{
myScript.CreateProjectionMatrix();
}
}
}
#endif
更多資源請(qǐng)點(diǎn)擊:https://bycwedu.vipwan.cn/promotion_channels/630597732