Sveske_Juice

mirror

Dec 29th, 2022 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Mirror : MonoBehaviour
  6. {
  7.     [SerializeField] private Renderer m_PlaneRenderer;
  8.     [SerializeField] private Camera m_PlayerCamera;
  9.     private Camera m_MirrorCamera;
  10.     private RenderTexture m_MirrorViewTexture;
  11.  
  12.     private void Awake()
  13.     {
  14.         // Get references
  15.         m_MirrorCamera = GetComponentInChildren<Camera>();
  16.     }
  17.  
  18.     private void Update()
  19.     {
  20.         // Mirror virtual camera's position with player's camera
  21.         UpdateMirrorCameraTransform();
  22.  
  23.         // Render virtual camera's view to render texture
  24.         RenderVirtualCamera();
  25.     }
  26.  
  27.     /// <summary>
  28.     /// Will update the mirror's virtual camera to be mirrored
  29.     /// to the players camera relative to the plane/mirror.
  30.     /// </summary>
  31.     private void UpdateMirrorCameraTransform()
  32.     {
  33.         // Use trick to calculate new position and rotation, see https://www.youtube.com/watch?v=xXUVP6sN-tQ
  34.  
  35.         // Flip mirror/screen plane
  36.         Vector3 planeScale = m_PlaneRenderer.transform.localScale;
  37.         m_PlaneRenderer.transform.localScale = new Vector3(planeScale.x, planeScale.y * -1, planeScale.z);
  38.  
  39.         // Set the virtual camera's position and rotaion to the player camera
  40.         m_MirrorCamera.transform.position = m_PlayerCamera.transform.position;
  41.         m_MirrorCamera.transform.rotation = Quaternion.Euler(m_MirrorCamera.transform.rotation.eulerAngles.x, m_PlayerCamera.transform.rotation.eulerAngles.y, m_MirrorCamera.transform.rotation.eulerAngles.z);
  42.         Debug.Log($"Player cam rotation: {m_PlayerCamera.transform.rotation.eulerAngles}");
  43.         Debug.Log($"Mirror cam rotation: {m_MirrorCamera.transform.rotation.eulerAngles}");
  44.  
  45.         // Flip mirror/screen plane back to where it was before
  46.         Vector3 newPlaneScale = m_PlaneRenderer.transform.localScale;
  47.         m_PlaneRenderer.transform.localScale = new Vector3(newPlaneScale.x, newPlaneScale.y * -1, newPlaneScale.z);
  48.  
  49.         // Rotate the camera so its not upside down
  50.         Quaternion mirrorRot = m_MirrorCamera.transform.localRotation;
  51.         // m_MirrorCamera.transform.localRotation = Quaternion.Euler(mirrorRot.x - 90, mirrorRot.y, mirrorRot.z);
  52.  
  53.  
  54.     }
  55.  
  56.     private void RenderVirtualCamera()
  57.     {
  58.         // Create mirrored view texture
  59.         CreateMirroredTexture();
  60.  
  61.         m_MirrorCamera.Render();
  62.     }
  63.  
  64.     /// <summary>
  65.     /// Creates a new RenderTexture that the mirror's virtual camera
  66.     /// will render to. Then it applies this texture to the Renderer
  67.     /// on the object which will display the mirrored view.
  68.     /// </summary>
  69.     private void CreateMirroredTexture()
  70.     {
  71.         // If it's already created we don't care
  72.         if (m_MirrorViewTexture != null)
  73.             return;
  74.  
  75.         // Create new render texture with identical width and height as the screen
  76.         m_MirrorViewTexture = new RenderTexture(Screen.width, Screen.height, 0);
  77.  
  78.         // Render the view from the mirror camera in to the texture
  79.         m_MirrorCamera.targetTexture = m_MirrorViewTexture;
  80.  
  81.         // Apply the mirrored view texture to the screen/mirror
  82.         m_PlaneRenderer.material.mainTexture = m_MirrorViewTexture;
  83.     }
  84. }
  85.  
Tags: c# unity
Add Comment
Please, Sign In to add comment