- Home /
Mirror Reflection via Render Texture distorting at scales other than 1 , 1
Hey friends. I've run into an issue with attempting to make a fully reflective mirror in unity. We are developing a few perspective based puzzles that require mirrors, and I am attempting to create the mirrors using a camera and a render texture.
I currently have my camera's (on the mirrors) moving relative to the player position, so that part is working.
The issue I'm having is that when the mirror is set to any x or y scale other than 1, the image becomes distorted at various angles. Below is a video showing the issue in action.
https://imgur.com/gallery/2d8UGKu
The first mirror has a scale of 1 , 1. The second a scale of 6 , 6. And the last one a scale of 6 , 1.
Here is the code I am using to both create the render texture, and position the mirror cameras. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Mirror : MonoBehaviour
{
//Our mirror and player cameras
Camera mirrorCam;
Camera playerCam;
//The mesh renderer for our mirror
MeshRenderer mirrorFace;
//The output texture from our camera
RenderTexture viewTexture;
//The starting center height of our mirror camera, relative to it's mirror parent
float camHeight;
void Awake() {
mirrorFace = GetComponent<MeshRenderer>();
mirrorCam = GetComponentInChildren<Camera>();
camHeight = mirrorCam.transform.position.y;
playerCam = Camera.main;
}
void FixedUpdate() {
if (viewTexture != null) {
viewTexture.Release();
}
//Set the size of the texture equal to our desired resolution (1024) multiplied by the size of our mirror
viewTexture = new RenderTexture(1024 * (int)Mathf.Abs(transform.localScale.x), 1024 * (int)Mathf.Abs(transform.localScale.y), 0);
//Render the view from the mirror cam to the view texture
mirrorCam.targetTexture = viewTexture;
//Set the mirrors texture to the render texture
mirrorFace.material.SetTexture("_MainTex", viewTexture);
//Create an offset so the camera is correctly positioned
Vector3 offset = playerCam.transform.position - transform.position;
//move the camera relative to the mirror and our player
mirrorCam.transform.position = transform.position - offset ;
//Force the mirror cameras y height to stay the same (centered on the mirror)
mirrorCam.transform.position = new Vector3(mirrorCam.transform.position.x, camHeight, mirrorCam.transform.position.z);
}
}