- Home /
Camera horizontal distortion?
Hello!
A while ago I was looking for how to make the camera stretch (distort) horizontally. Or, an alternative is to make the screen resolution wider horizontally and make the image look that way.
But the "Fisheye" effect doesn't work the way I'm looking for And the resolution has an "aspect ratio" that does not allow me to make such an effect
Does anyone know how that effect can be done? Thank you!
Answer by Namey5 · Apr 27, 2020 at 12:50 AM
Cameras use a projection matrix to map things to the screen for rendering. As part of this, things like field of view, clipping planes and aspect ratio are taken into account - separate from resolution. As such, we can override the camera's projection matrix using a custom aspect ratio without actually modifying the screen's aspect ratio;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HorizontalDistortion : MonoBehaviour
{
private Camera cam;
//This is in relation to the rendering aspect ratio, clamped to a minimum to stop errors
[Min(0.01f)]
public float aspectMultiplier = 1f;
//Before the camera culls the scene
private void OnPreCull ()
{
//Get the camera if we don't already have it
if (!cam)
cam = GetComponent<Camera>();
//Override the projection matrix, only adjusting aspect ratio
cam.projectionMatrix = Matrix4x4.Perspective (cam.fieldOfView, cam.aspect / aspectMultiplier, cam.nearClipPlane, cam.farClipPlane);
}
private void OnDisable ()
{
//Reset the projection matrix when we are done
if (cam)
cam.ResetProjectionMatrix ();
}
}
Thanks for posting this answer, but I do have a question about it. Please excuse my ter$$anonymous$$ology, I'm very new to this and I don't know how all this works.
It seems the more the aspect ratio changes from the original, say by making it way 0.3 instead of 1.77 (for fullHD), it will become more and more pixelated in the horizontal direction. This seems pretty logical to me as it stretches the 1:3 image to fit a 16:9 screen and I guess it simply doesn't have the amount of 'information' on the horizontal axis as it normally would. Is there any way to fix that and have the camera actually 'gather' the same amount of information on the horizontal axis as it normally would, regardless the aspect ratio?
Your answer
Follow this Question
Related Questions
How to adjust desired camera size for all aspect ratios without having black edges? 0 Answers
Aspect Ration For 2D Sprites and Canvas 0 Answers
Keep Aspect ratio/Letterbox and maintain pixel perfection 0 Answers
Camera viewport should be square and pixel-perfect, regardless of player aspect ratio. 0 Answers
Phantom camera translation when trying to set screen size 1 Answer