最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

用來可視化了解各種矩陣變換的代碼

2021-12-09 18:21 作者:unity小能手  | 我要投稿

用來可視化了解各種矩陣變換的代碼。直接創(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


用來可視化了解各種矩陣變換的代碼的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
苍山县| 井研县| 淮安市| 永昌县| 高唐县| 益阳市| 织金县| 大方县| 永善县| 阿拉善盟| 营口市| 乡宁县| 南阳市| 郑州市| 成武县| 红桥区| 枝江市| 乌拉特中旗| 伊金霍洛旗| 从化市| 潜山县| 砚山县| 巢湖市| 英吉沙县| 化州市| 柳河县| 南部县| 浦江县| 阆中市| 察隅县| 西丰县| 始兴县| 都匀市| 望奎县| 五台县| 亳州市| 陆丰市| 麻阳| 南阳市| 浠水县| 改则县|