unity - Draw Camera Perspective Line 카메라 라인 그리기











1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using UnityEngine;
 
public class DrawCameraLine : MonoBehaviour
{
    public Transform pos1;
    public Transform pos2;
 
    private void FixedUpdate()
    {
        DrawPlane(pos1.position, pos2.position);
    }
 
    void DrawPlane(Vector3 position, Vector3 normal)
    {
        Vector3 screenPos1 = Camera.main.WorldToScreenPoint(position);
        Vector3 screenPos2 = Camera.main.WorldToScreenPoint(normal);
 
        Vector3 posNear1 = new Vector3(screenPos1.x, screenPos1.y, Camera.main.nearClipPlane);
        Vector3 posNear2 = new Vector3(screenPos2.x, screenPos2.y, Camera.main.nearClipPlane);
        Vector3 posFar1 = new Vector3(screenPos1.x, screenPos1.y, Camera.main.farClipPlane);
        Vector3 posFar2 = new Vector3(screenPos2.x, screenPos2.y, Camera.main.farClipPlane);
 
        Debug.Log("posNear1 : " + posNear1);
        Debug.Log("posNear2 : " + posNear2);
 
        Vector3 posN1 = Camera.main.ScreenToWorldPoint(posNear1);
        Vector3 posN2 = Camera.main.ScreenToWorldPoint(posNear2);
        Vector3 posF1 = Camera.main.ScreenToWorldPoint(posFar1);
        Vector3 posF2 = Camera.main.ScreenToWorldPoint(posFar2);
 
        Vector3 corner0 = posN1;
        Vector3 corner1 = posN2;
        Vector3 corner2 = posF1;
        Vector3 corner3 = posF2 ;
        
        Debug.DrawLine(corner0, corner2, Color.green);
        Debug.DrawLine(corner1, corner3, Color.green);
        Debug.DrawLine(corner0, corner1, Color.green);
        Debug.DrawLine(corner2, corner3, Color.green);
    }
}
cs


댓글