Created
March 11, 2023 21:35
-
-
Save SabinT/7744fb65928f32e65a6d919fc1712a8e to your computer and use it in GitHub Desktop.
CameraQuad.cs
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
private void CalculateCorners() | |
{ | |
float aspectRatio = Screen.width / (float)Screen.height; | |
if (this.Camera == null) | |
{ | |
this.Camera = Camera.main; | |
} | |
// IMPORTANT: Do not use Screen.width here, use Camera.pixelWidth!!! | |
// Debug.Log("Scr " + Screen.width); | |
// Debug.Log("Cam " + this.Camera.pixelWidth); | |
var d = this.DistanceFromCamera; | |
var w = this.Camera.pixelWidth; | |
var h = this.Camera.pixelHeight; | |
Vector3 res = new Vector3(w, h, 1); | |
var p = this.MapperPrefs; | |
// Find the corners of the screen in pixel-space | |
p.bottomLeft.screenPos = new Vector3(0, 0, d) + Vector3.Scale(p.bottomLeft.screenOffset, res); | |
p.topLeft.screenPos = new Vector3(0, h, d) + Vector3.Scale(p.topLeft.screenOffset, res); | |
p.topRight.screenPos = new Vector3(w, h, d) + Vector3.Scale(p.topRight.screenOffset, res); | |
p.bottomRight.screenPos = new Vector3(w, 0, d) + Vector3.Scale(p.bottomRight.screenOffset, res); | |
p.bottomLeft.worldPos = this.Camera.ScreenToWorldPoint(p.bottomLeft.screenPos); | |
p.topLeft.worldPos = this.Camera.ScreenToWorldPoint(p.topLeft.screenPos); | |
p.topRight.worldPos = this.Camera.ScreenToWorldPoint(p.topRight.screenPos); | |
p.bottomRight.worldPos = this.Camera.ScreenToWorldPoint(p.bottomRight.screenPos); | |
// Displace without changing screen position (along ray from camera) | |
Vector3 origin = this.Camera.transform.position; | |
p.topLeft.worldPos = this.Displace(origin, p.topLeft.worldPos, p.topLeft.depthDisplace); | |
p.topRight.worldPos = this.Displace(origin, p.topRight.worldPos, p.topRight.depthDisplace); | |
p.bottomLeft.worldPos = this.Displace(origin, p.bottomLeft.worldPos, p.bottomLeft.depthDisplace); | |
p.bottomRight.worldPos = this.Displace(origin, p.bottomRight.worldPos, p.bottomRight.depthDisplace); | |
// Convert to local space - not necessary if the quad mesh is always rendered at the origin with default rotation | |
p.bottomLeft.localPos = this.transform.InverseTransformPoint(p.bottomLeft.worldPos); | |
p.topLeft.localPos = this.transform.InverseTransformPoint(p.topLeft.worldPos); | |
p.topRight.localPos = this.transform.InverseTransformPoint(p.topRight.worldPos); | |
p.bottomRight.localPos = this.transform.InverseTransformPoint(p.bottomRight.worldPos); | |
this.normalLocal = Vector3.Cross( | |
p.topRight.localPos - p.topLeft.localPos, | |
p.bottomLeft.localPos - p.topLeft.localPos).normalized; | |
// Calculate normals for each corner | |
p.bottomLeft.normal = Vector3.Cross( | |
p.topLeft.localPos - p.bottomLeft.localPos, | |
p.bottomRight.localPos - p.bottomLeft.localPos).normalized; | |
p.topLeft.normal = Vector3.Cross( | |
p.topRight.localPos - p.topLeft.localPos, | |
p.bottomLeft.localPos - p.topLeft.localPos).normalized; | |
p.topRight.normal = Vector3.Cross( | |
p.bottomRight.localPos - p.topRight.localPos, | |
p.topLeft.localPos - p.topRight.localPos).normalized; | |
p.bottomRight.normal = Vector3.Cross( | |
p.bottomLeft.localPos - p.bottomRight.localPos, | |
p.topRight.localPos - p.bottomRight.localPos).normalized; | |
} | |
private Vector3 Displace(Vector3 origin, Vector3 position, float displacement) | |
{ | |
Vector3 dir = position - origin; | |
return position + dir.normalized * displacement; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment