本文主要是介绍视口坐标转换成Canvas下本地坐标与shader显示在3D物体前,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
shader显示在3D物体前,先转化成视口坐标
public virtual void SetESignPos()
{
float x = 0;
float y = 0;
int v = PlayerPrefsTool.GetSpecialScreen(-1);
if (EScreenCamera != null)
{
Vector3 viewPoint=EScreenCamera.WorldToViewportPoint(CurENeedShowPosition);
Logger.Log($"SetESignPos viewPoint:{viewPoint},{viewPoint.x*Screen.width},{viewPoint.y*Screen.height}");
x = viewPoint.x * Screen.width;
y = viewPoint.y * Screen.height;
}
else
{
Vector2 screenPos = RectTransformUtility.WorldToScreenPoint(mE_f.canvas.worldCamera, CurENeedShowPosition);
x = screenPos.x;
y = screenPos.y;
}
if (v == -1)//正常屏幕
{
y = Screen.height - y;
}
mESignMaterial.SetVector("_LP1", new Vector4(x, y, mESignMaterialVector4.z, mESignMaterialVector4.w));
}
视口坐标转换成Canvas下本地坐标
using UnityEngine;
using UnityEngine.UI;
public class ViewportToCanvasCoordinates : MonoBehaviour
{
public RectTransform canvasRect;
void Update()
{
// 设置一个示例的视口坐标
Vector3 viewportPoint = new Vector3(0.5f, 0.5f, 0);
// 获取主摄像机
Camera mainCamera = Camera.main;
if (mainCamera != null)
{
// 使用主摄像机的方法将视口坐标转换为屏幕坐标
Vector3 screenPoint = mainCamera.ViewportToScreenPoint(viewportPoint);
// 将屏幕坐标转换为 UI 中的 Canvas 坐标
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, mainCamera, out localPoint);
// 打印 UI 中的 Canvas 坐标
Debug.Log("UI Canvas Local Point: " + localPoint);
}
}
}
这篇关于视口坐标转换成Canvas下本地坐标与shader显示在3D物体前的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!