I’m rendering my webcam to a plane with an unlit shader, however, the texture in the game view looks washed out when compared to the texture asset in the material. See screenshot below for comparison. When I apply the same material with an unlit shader and apply a texture file it doesn’t appear washed out, it’s only when I apply the webcam texture…
Managed to fix it with the code below by raising the texture base power to 2.2, not a shader pro so I’m not sure why this works…?
Shader "Custom/Webcam"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
}
SubShader
{
Tags{ "RenderType" = "Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
// sample the texture
//fixed4 col = tex2D(_MainTex, i.uv);
fixed4 col = pow(tex2D(_MainTex, i.uv), 2.2);
return col;
}
ENDCG
}
}
}
Raising to a power of 2.2 is essentially converting from gamma color space to linear color space.
I’ve not used webcam textures, but my guess is you may have to adjust some setting on the texture to give Unity a hint whether it should be treated as a gamma or linear texture.
I was wondering if it was gamma vs linear. Makes sense since my project color space is set to linear. Thanks!