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!
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 ();
}
}