- Home /
Flip/Mirror > Camera?
Is there a way to flip the Camera (let's say in X position), so the whole scene is "mirrored" for example to the right side,?
use this scrip
void Start () {
$$anonymous$$atrix4x4 mat = Camera.main.projection$$anonymous$$atrix;
mat *= $$anonymous$$atrix4x4.Scale(new Vector3(-1, 1, 1));
Camera.main.projection$$anonymous$$atrix = mat;
}
// Set it to true so we can watch the flipped Objects
void OnPreRender()
{
GL.invertCulling = true;
}
// Set it to false again because we dont want to affect all other cammeras.
void OnPostRender()
{
GL.invertCulling = false;
}
Answer by Mike 3 · Jun 23, 2010 at 11:34 AM
You can use something like this (Once in start should do it):
Matrix4x4 mat = Camera.main.projectionMatrix;
mat *= Matrix4x4.Scale(new Vector3(-1, 1, 1));
Camera.main.projectionMatrix = mat;
i haven't tried it yet, need to give that to my programmer, and he is at work atm (got no time). i help him as good as i can and i am only a grafic artist with a bit knowledge about how things work.
so we are not talking about a render to texture method?
it really flips the whole view in x position?
Does it work on iphone too?
Should do. Flips the camera horizontally on 2.6, and the same function is there for iphone. It's old style flipping, nothing fancy,should work on any computer
you are my god :D thnx!! is exactly that code the one which works on iphone too? xD
Hello $$anonymous$$ike, we tried the script now. It seems to work, but it flips the normal matrix of all objects. in play mode the camera is facing to the right direction. in game mode i recognize my objects areflipped. but it's like all normals where flipped too. Do you know how to fix that problem?
Doesn't work... normals are also flipped so the mirrored camera renders objects something like inside-out, and terrain surfaces are not rendered at all.
Answer by ApenasVB · Jan 17, 2017 at 03:30 PM
Just use the attached script in your camera, it will flip the image according to the editor selection and fix the culling accordingly.
using UnityEngine;
[RequireComponent(typeof(Camera))]
[ExecuteInEditMode]
public class MirrorFlipCamera : MonoBehaviour {
new Camera camera;
public bool flipHorizontal;
void Awake () {
camera = GetComponent<Camera>();
}
void OnPreCull() {
camera.ResetWorldToCameraMatrix();
camera.ResetProjectionMatrix();
Vector3 scale = new Vector3(flipHorizontal ? -1 : 1, 1, 1);
camera.projectionMatrix = camera.projectionMatrix * Matrix4x4.Scale(scale);
}
void OnPreRender () {
GL.invertCulling = flipHorizontal;
}
void OnPostRender () {
GL.invertCulling = false;
}
}
This one works in non-vr mode, in google cardboard VR mode, it seems not working, Any suggestions? thanks
Answer by StephanK · Jun 23, 2010 at 11:04 AM
If you have render to texture (pro only) you could easily achieve this by rendering a texture and then flip that texture before it's displayed.
That's a solution for Animated Textures or so. What i need is a flip for right handed iphone users. the whole game-view shell be flipped, and the touch-matrix would be moved to the right position too. thing is my game works with on thumb. and the touchfield for it is atm on the left side. if i want to move the touchfield to the right side, i also have to flip the camera in x position, cause the game is strictly layouted on that way.
Answer by _geo__ · Nov 15, 2021 at 04:38 PM
URP and HDRP have deprecated the On..Render events. Here is a version of @ApenasVB s script which will work for URP and HDRP. I also added mirroring for both axis.
using UnityEngine;
using UnityEngine.Rendering;
using UnityEditor;
[RequireComponent(typeof(Camera))]
[ExecuteInEditMode]
public class MirrorFlipCamera : MonoBehaviour
{
Camera _camera;
public Camera Camera
{
get
{
if (_camera == null)
{
_camera = this.GetComponent<Camera>();
}
return _camera;
}
}
[SerializeField]
protected bool flipHorizontal;
public bool FlipHorizontal
{
get => flipHorizontal;
set
{
flipHorizontal = value;
UpdateCameraMatrix();
}
}
[SerializeField]
protected bool flipVertical;
public bool FlipVertical
{
get => flipVertical;
set
{
flipVertical = value;
UpdateCameraMatrix();
}
}
protected float aspectRatio = 0;
void Awake()
{
RenderPipelineManager.beginCameraRendering += beginCameraRendering;
RenderPipelineManager.endCameraRendering += endCameraRendering;
}
void beginCameraRendering(ScriptableRenderContext context, Camera camera)
{
// Flip only if ONE of the two axis is mirrored but not if both are mirrored.
if (this == null || !this.gameObject.activeInHierarchy)
return;
GL.invertCulling = flipHorizontal ^ flipVertical;
// update is aspect ratio changed
if (Mathf.Abs(aspectRatio - Camera.aspect) > 0.01f)
{
aspectRatio = Camera.aspect;
UpdateCameraMatrix();
}
}
void endCameraRendering(ScriptableRenderContext context, Camera camera)
{
if (this == null || !this.gameObject.activeInHierarchy)
return;
GL.invertCulling = false;
}
public void UpdateCameraMatrix()
{
Camera.ResetWorldToCameraMatrix();
Camera.ResetProjectionMatrix();
Vector3 scale = new Vector3(
flipHorizontal ? -1 : 1,
flipVertical ? -1 : 1,
1);
Camera.projectionMatrix = Camera.projectionMatrix * Matrix4x4.Scale(scale);
}
void OnValidate()
{
if (Camera == null)
return;
UpdateCameraMatrix();
#if UNITY_EDITOR
RenderPipelineManager.beginCameraRendering -= beginCameraRendering;
RenderPipelineManager.endCameraRendering -= endCameraRendering;
if (!EditorApplication.isPlayingOrWillChangePlaymode)
{
RenderPipelineManager.beginCameraRendering += beginCameraRendering;
RenderPipelineManager.endCameraRendering += endCameraRendering;
}
#endif
}
}
Since another answer mentioned the Unity wiki (which no longer exists), here is the linked page from the archives: https://web.archive.org/web/20210414060956/https://wiki.unity3d.com/index.php?title=InvertCamera
Answer by devigod · Mar 11, 2011 at 04:10 AM
There is the answer: http://wiki.unity3d.com/index.php?title=InvertCamera
Your answer
Follow this Question
Related Questions
OnRenderImage Camera is flipped upside down 1 Answer
Platformer Flipping 0 Answers
How to rotate the camera on a button press. 2 Answers
Can I make the camera flip upside-down when spacebar is pressed? 3 Answers
Why does my camera automatically flip when trying to follow object around X-axis? 0 Answers