Created
July 30, 2022 00:30
-
-
Save yosun/fb3523f41e10469ad206d84a63e9ccd7 to your computer and use it in GitHub Desktop.
CameraShotToPlane.cs lets you create a hit plane (or debugPoint objects) based on camera viewport corners using CreatePlaneFromFourPoints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class CameraShotToPlane : MonoBehaviour | |
{ | |
public bool debugMode = true; | |
public GameObject goDebugPoint; | |
public Camera cameraToRaycastFrom; | |
public Material mat; | |
private void Start() | |
{ | |
// test | |
RaycastFromDesignatedCamera(); | |
} | |
#if UNITY_EDITOR | |
private void Update() | |
{ | |
if(Input.GetMouseButtonUp(0))RaycastFromDesignatedCamera(debugMode); | |
} | |
#endif | |
// use testmode=true to show only debugPoints | |
public void RaycastFromDesignatedCamera(bool testmode = false) | |
{ | |
RaycastFromCameraCreatePlane(cameraToRaycastFrom, testmode); | |
} | |
public GameObject RaycastFromCameraCreatePlane(Camera cam,bool testmode=false) | |
{ | |
// viewport coordinates are like in math class, bottom left is (0,0) and top right is (1,1) | |
// creating triangles based on bottom left, bottom right, top left, top right | |
Vector3[] hitpoints = GetHitFromViewportPoints(cam, new Vector3[4] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0) }); | |
if (testmode && goDebugPoint != null) | |
{ | |
for (int i = 0; i < hitpoints.Length; i++) | |
{ | |
GameObject g = Instantiate(goDebugPoint, hitpoints[i], Quaternion.identity); | |
g.transform.parent = transform; | |
g.name = hitpoints[i].ToString("F4"); | |
print(hitpoints[i].ToString("F4")); | |
} | |
} | |
if (!testmode) | |
return CreatePlaneFromFourPoints(hitpoints); | |
else return null; | |
} | |
Vector3[] GetHitFromViewportPoints(Camera cam, Vector3[] viewportpoints) | |
{ | |
Vector3[] hitpoints = new Vector3[viewportpoints.Length]; | |
for (int i = 0; i < viewportpoints.Length; i++) | |
{ | |
Ray ray = cam.ViewportPointToRay(viewportpoints[i]); | |
print(viewportpoints[i]); | |
RaycastHit hit; | |
if (Physics.Raycast(ray, out hit)) | |
{ | |
hitpoints[i] = hit.point; | |
} | |
} | |
return hitpoints; | |
} | |
public GameObject CreatePlaneFromFourPoints(Vector3[] v) | |
{ | |
return CreatePlaneFromFourPoints(v[0], v[1], v[2], v[3],mat); | |
} | |
public GameObject CreatePlaneFromFourPoints(Vector3 bl, Vector3 br, Vector3 tl, Vector3 tr, Material mat = null, MeshFilter mf=null,Renderer rend=null) | |
{ | |
Vector3[] vertices = new Vector3[4] { bl, br, tl, tr }; | |
// define or create meshfilter | |
if(mf==null) mf = gameObject.AddComponent<MeshFilter>(); | |
// create mesh | |
Mesh mesh = new Mesh(); | |
mf.mesh = mesh; | |
mesh.vertices = vertices; | |
// create triangles | |
int[] tris = new int[6] { | |
// lower left triangle | |
0, 2, 1, | |
// upper right triangle | |
2, 3, 1 | |
}; | |
mesh.triangles = tris; | |
/*Vector3[] normals = new Vector3[4] | |
{ | |
Vector3.forward, | |
Vector3.forward, | |
Vector3.forward, | |
Vector3.forward | |
}; | |
mesh.normals = normals;*/ | |
Vector2[] uv = new Vector2[4] | |
{ | |
new Vector2(0, 0), | |
new Vector2(1, 0), | |
new Vector2(0, 1), | |
new Vector2(1, 1) | |
}; | |
mesh.uv = uv; | |
mesh.RecalculateBounds(); | |
mesh.RecalculateNormals(); | |
mf.mesh = mesh; | |
// define or create renderer and collider | |
if (rend == null) | |
{ | |
rend = gameObject.AddComponent<MeshRenderer>(); | |
gameObject.AddComponent<BoxCollider>(); | |
} | |
else rend.gameObject.AddComponent<BoxCollider>(); | |
if (mat != null) | |
rend.material = mat; | |
return mf.gameObject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment