国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(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)遵守國家法律
绩溪县| 铁岭市| 丽江市| 木兰县| 蒙阴县| 来凤县| 七台河市| 柘荣县| 页游| 临武县| 泗水县| 盐城市| 玛纳斯县| 甘洛县| 灵川县| 永德县| 岳阳县| 奎屯市| 乌拉特后旗| 清苑县| 万年县| 克拉玛依市| 宜黄县| 临海市| 宁阳县| 达尔| 子洲县| 嘉禾县| 江山市| 盱眙县| 邢台县| 古交市| 华宁县| 秦皇岛市| 吉首市| 顺义区| 大同市| 枣阳市| 江达县| 磴口县| 航空|