camera.cameraToWorldMatrix

作者:追风剑情 发布于:2023-4-26 15:02 分类:Unity3d

cameraToWorldMatrix

示例:摄像机空间转世界空间

using UnityEngine;

public class Example : MonoBehaviour
{
    public float distance = -1.0F;
    void OnDrawGizmosSelected()
    {
        //获取摄像机空间转世界空间的转换矩阵
        Matrix4x4 m = Camera.main.cameraToWorldMatrix;
        //摄像机前面distance位置
        Vector3 cameraSpacePos = new Vector3(0, 0, distance);
        //摄像机空间转世界空间
        Vector3 worldSpacePos = m.MultiplyPoint(cameraSpacePos);
        //在worldSpacePos位置绘制一个黄色球(仅Scene窗口中可见)
        Gizmos.color = Color.yellow;
        Gizmos.DrawSphere(worldSpacePos, 0.2F);
    }
}

11111.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号