我正在使用渲染纹理制作侧面贴图,它工作正常,我已经完成了这些步骤。
- 一个主摄像机渲染场景。
- NGUI 相机呈现 GUI。 该
- GUI 包含渲染纹理(小地图),该纹理使用 GUI 相机在纹理上渲染场景。
但是现在我想进一步改进地图,并希望它变得棘手。我想要渲染纹理单击点的世界空间位置。有什么方法可以从渲染纹理的特定点击点获取世界空间位置?
如果我单击渲染纹理中的某个位置,如何在 3D 世界空间中获得点。
例如:我在渲染纹理上单击了一个汽车对象。现在,如何在3D世界空间中突出显示或获取它。如何将 2D 渲染纹理单击位置转换为世界空间位置!
这假设了一些事情。
首先,我使用EventSystems来获取鼠标点击。这不是必需的,它可以很容易地更改(尽管^^由您更改)。要正确设置它,您需要在场景中有一个EventSystem
(如果您有 UI,您可能已经有一个)。
其次,您需要将PhysicsRaycaster
组件连接到主摄像机(玩家透视的摄像机)。
最后,在包含渲染纹理渲染器的 GameObject 上(我使用了简单的四边形),您可以应用以下脚本并分配相应的摄像机。
using UnityEngine;
using UnityEngine.EventSystems;
//i chose box collider because its cheap
[RequireComponent(typeof(BoxCollider))]
public class RenderTextureRaycaster : MonoBehaviour, IPointerDownHandler {
//assign in inspector
public Camera portalExit;
BoxCollider portal;
Vector3 portalExitSize;
void Start() {
portal = GetComponent<BoxCollider>();
//this is the target camera resolution, idk if there is another way to get it.
portalExitSize = new Vector3(portalExit.targetTexture.width, portalExit.targetTexture.height, 0);
}
public void OnPointerDown(PointerEventData eventData) {
//the click in world space
Vector3 worldClick = eventData.pointerCurrentRaycast.worldPosition;
//transformed into local space
Vector3 localClick = transform.InverseTransformPoint(worldClick);
//since the origin of the collider is in its center, we need to offset it by half its size to get it realtive to bottom left
Vector3 textureClick = localClick + portal.size / 2;
//now we scale it up by the actual texture size which equals to the "camera resoution"
Vector3 rayOriginInCameraSpace = Vector3.Scale(textureClick, portalExitSize);
//with this knowledge we can creata a ray.
Ray portaledRay = portalExit.ScreenPointToRay(rayOriginInCameraSpace );
RaycastHit raycastHit;
//and cast it.
if (Physics.Raycast(portaledRay, out raycastHit)) {
Debug.DrawLine(portaledRay.origin, raycastHit.point, Color.blue, 4);
}
else {
Debug.DrawRay(portaledRay.origin, portaledRay.direction * 100, Color.red, 4);
}
}
}
编辑:上面可能有点冗长,如果您喜欢一个衬里,您可以轻松减少它,它只是为了展示它是如何工作的。 另外,请仅将其视为概念证明,未经彻底测试。