banner



Unity Can I Apply My Camera Settings To Matrix4x4.ortho?

I've plant this article on Gamasutra on a custom photographic camera project to make 3D expect more second and decided to implement it to make some gameplay tests.

I had to make some changes to make information technology piece of work on URP(on unity 2019.3.6f1) but information technology "worked" with the following code:

          using UnityEngine; using System.Collections; using UnityEngine.Rendering;   // This script is meant to exist attached to your primary camera. // If y'all desire to apply it on more than than i camera at a time, it volition require // modifcations due to the Camera.on* delegates in OnEnable()/OnDisable().   [ExecuteInEditMode] public form CustomProjectionTest : MonoBehaviour {     individual void OnEnable(){         RenderPipelineManager.beginFrameRendering += ScenePreCull;         RenderPipelineManager.endFrameRendering += ScenePostRender;         //Photographic camera.onPreCull += ScenePreCull;     }       private void OnDisable(){         RenderPipelineManager.beginFrameRendering -= ScenePreCull;         RenderPipelineManager.endFrameRendering -= ScenePostRender;           GetComponent<Camera>().ResetWorldToCameraMatrix();     }       private void ScenePreCull(ScriptableRenderContext context, Photographic camera[] cam){         // If the photographic camera is the scene view camera, telephone call our OnPreCull() event method for it.         OnPreCull();     }       private void ScenePostRender(ScriptableRenderContext context, Camera[] cam){         // Unity'south gizmos don't like it when you change the worldToCameraMatrix.         // The workaround is to reset it afterward rendering.                  foreach(var camm in cam)             camm.ResetWorldToCameraMatrix();     }       private Camera _cam;     private void Start(){         _cam = GetComponent<Camera>();           // Set up the camera this script is fastened to to use orthographic sorting order.         // Instead of using the euclidean altitude from the camera, objects will be sorted based on their depth into the scene.         _cam.transparencySortMode = TransparencySortMode.Orthographic;     }       public Vector4 up = new Vector4(0, 1, 0, 0);       // This is a Unity callback and is the ideal place to set the worldToCameraMatrix.     private void OnPreCull(){         var cam = !Application.isPlaying? Camera.electric current : _cam;           // Outset calculate the regular worldToCameraMatrix.         // Starting time with transform.worldToLocalMatrix.         var m = cam.transform.worldToLocalMatrix;           // Then, since Unity uses OpenGL's view matrix conventions nosotros have to flip the output z-value.         yard.SetRow(2, -m.GetRow(2));           // Now for the custom project.         // Set the globe'due south upwardly vector to always align with the photographic camera's upward vector.         // Add a pocket-size corporeality of the original upward vector to ensure the matrix will be invertible.         grand.SetColumn(2, 1e-3f*yard.GetColumn(2) - up);           cam.worldToCameraMatrix = thou;     }       public static Matrix4x4 ScreenToWorldMatrix(Camera cam){         // Make a matrix that converts from screen coordinates to clip coordinates.         var rect = cam.pixelRect;         var viewportMatrix = Matrix4x4.Ortho(rect.xMin, rect.xMax, rect.yMin, rect.yMax, -1, 1);           // The photographic camera'southward view-projection matrix converts from world coordinates to clip coordinates.         var vpMatrix = cam.projectionMatrix*cam.worldToCameraMatrix;           // Setting column 2 (z-axis) to identity makes the matrix ignore the z-axis.         // Instead you go the value on the xy plane.         vpMatrix.SetColumn(2, new Vector4(0, 0, 1, 0));           // Going from correct to left:         // convert screen coords to prune coords, so clip coords to world coords.         render vpMatrix.inverse*viewportMatrix;     }       public Vector2 ScreenToWorldPoint(Vector2 point){         render ScreenToWorldMatrix(_cam).MultiplyPoint(point);     }       private void Update(){         if(Input.GetMouseButtonUp(0)){             Debug.Log(ScreenToWorldPoint(Input.mousePosition));         }     } }                  

Well, kinda. For some reason, it simply works in the scene editor, but not in the game. I just take no clue why

With Scene Editor:

With Scene Editor

In Game

In Game

Source: https://gamedev.stackexchange.com/questions/185270/unity-custom-camera-projection-only-working-in-scene-editor

Posted by: lenahancrioul.blogspot.com

0 Response to "Unity Can I Apply My Camera Settings To Matrix4x4.ortho?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel