让Steam VR的画面左右翻转, 而且物体不会发生变化。效果如下
网上找到的教程大多数都是这个:unity摄像机实现镜像效果
设置后虽然也镜像了,但是物体的阴影那些会发生变化,此代码不会。初学者我也不懂,貌似是对生成后的图像进行处理的。
Shader:
Shader "Custom/FrontCameraInvertImage" {
Properties{
_MainTex("Base (RGB)", 2D) = "" {}
_AxisX("x or y", Range(0,1)) = 0 //0 for x; 1 for y
}
// Shader code pasted into all further CGPROGRAM blocks
CGINCLUDE
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _MainTex;
half4 _MainTex_ST;
float _AxisX;
v2f vert(appdata_img v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord.xy;
return o;
}
half4 frag(v2f i) : SV_Target
{
if (_AxisX == 0)
i.uv.x = 1 - i.uv.x;
else
i.uv.y = 1 - i.uv.y;
half4 color = tex2D(_MainTex, UnityStereoScreenSpaceUVAdjust(i.uv, _MainTex_ST));
return color;
}
ENDCG
Subshader {
Pass{
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
Fallback off
} // shader
代码
using System.Collections; using System.Collections.Generic; using UnityEngine; ////// 把画面左右镜像 /// public class FrontCameraInvertImage : MonoBehaviour { private bool isReverting = true; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Space)) { isReverting = !isReverting; } } public bool NeedRevert = false; public Shader invertShader; static Material m_Material = null; protected Material material { get { if (m_Material == null) { m_Material = new Material(invertShader); m_Material.hideFlags = HideFlags.DontSave; } return m_Material; } } protected void OnDisable() { if (m_Material) { DestroyImmediate(m_Material); } } // Called by the camera to apply the image effect void OnRenderImage(RenderTexture source, RenderTexture destination) { if (NeedRevert && isReverting) { if (Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown) { material.SetFloat("_AxisX", 0); } else { material.SetFloat("_AxisX", 1); } Graphics.Blit(source, destination, material); } else Graphics.Blit(source, destination); } }
把代码挂在到如图所示的相机中,把shader拉到右侧的invert Shader中,勾上NeedRevert即可,我代码中设置了敲击空格开启或关闭镜像。
shader文件创建:
引用:主要代码来源



