Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Mirror : MonoBehaviour
- {
- [SerializeField] private Renderer m_PlaneRenderer;
- [SerializeField] private Camera m_PlayerCamera;
- private Camera m_MirrorCamera;
- private RenderTexture m_MirrorViewTexture;
- private void Awake()
- {
- // Get references
- m_MirrorCamera = GetComponentInChildren<Camera>();
- }
- private void Update()
- {
- // Mirror virtual camera's position with player's camera
- UpdateMirrorCameraTransform();
- // Render virtual camera's view to render texture
- RenderVirtualCamera();
- }
- /// <summary>
- /// Will update the mirror's virtual camera to be mirrored
- /// to the players camera relative to the plane/mirror.
- /// </summary>
- private void UpdateMirrorCameraTransform()
- {
- // Use trick to calculate new position and rotation, see https://www.youtube.com/watch?v=xXUVP6sN-tQ
- // Flip mirror/screen plane
- Vector3 planeScale = m_PlaneRenderer.transform.localScale;
- m_PlaneRenderer.transform.localScale = new Vector3(planeScale.x, planeScale.y * -1, planeScale.z);
- // Set the virtual camera's position and rotaion to the player camera
- m_MirrorCamera.transform.position = m_PlayerCamera.transform.position;
- m_MirrorCamera.transform.rotation = Quaternion.Euler(m_MirrorCamera.transform.rotation.eulerAngles.x, m_PlayerCamera.transform.rotation.eulerAngles.y, m_MirrorCamera.transform.rotation.eulerAngles.z);
- Debug.Log($"Player cam rotation: {m_PlayerCamera.transform.rotation.eulerAngles}");
- Debug.Log($"Mirror cam rotation: {m_MirrorCamera.transform.rotation.eulerAngles}");
- // Flip mirror/screen plane back to where it was before
- Vector3 newPlaneScale = m_PlaneRenderer.transform.localScale;
- m_PlaneRenderer.transform.localScale = new Vector3(newPlaneScale.x, newPlaneScale.y * -1, newPlaneScale.z);
- // Rotate the camera so its not upside down
- Quaternion mirrorRot = m_MirrorCamera.transform.localRotation;
- // m_MirrorCamera.transform.localRotation = Quaternion.Euler(mirrorRot.x - 90, mirrorRot.y, mirrorRot.z);
- }
- private void RenderVirtualCamera()
- {
- // Create mirrored view texture
- CreateMirroredTexture();
- m_MirrorCamera.Render();
- }
- /// <summary>
- /// Creates a new RenderTexture that the mirror's virtual camera
- /// will render to. Then it applies this texture to the Renderer
- /// on the object which will display the mirrored view.
- /// </summary>
- private void CreateMirroredTexture()
- {
- // If it's already created we don't care
- if (m_MirrorViewTexture != null)
- return;
- // Create new render texture with identical width and height as the screen
- m_MirrorViewTexture = new RenderTexture(Screen.width, Screen.height, 0);
- // Render the view from the mirror camera in to the texture
- m_MirrorCamera.targetTexture = m_MirrorViewTexture;
- // Apply the mirrored view texture to the screen/mirror
- m_PlaneRenderer.material.mainTexture = m_MirrorViewTexture;
- }
- }
Add Comment
Please, Sign In to add comment