How to darken the camera feed from ARkit without darkening the AR objects
Using the ARkit plugin I'm trying to apply a darkening filter/effect on the camera feed but not on the AR objects. I see the material for the camera feed is called YUVmaterial but I don't know shader code, and I don't see it being applyied anywhere physically/spacially in the scene.
Any suggestions?
Thanks
Answer by hammadb · Jun 12, 2018 at 02:49 AM
Here is a version of the YUV AR shader that will dim the screen via the dimFactor property.
Shader "Unlit/ARCameraShaderDim"
{
Properties
{
_dimFactor ("DimFactor", float) = 1
_textureY ("TextureY", 2D) = "white" {}
_textureCbCr ("TextureCbCr", 2D) = "black" {}
}
SubShader
{
Cull Off
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4x4 _DisplayTransform;
struct Vertex
{
float4 position : POSITION;
float2 texcoord : TEXCOORD0;
};
struct TexCoordInOut
{
float4 position : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
TexCoordInOut vert (Vertex vertex)
{
TexCoordInOut o;
o.position = UnityObjectToClipPos(vertex.position);
float texX = vertex.texcoord.x;
float texY = vertex.texcoord.y;
o.texcoord.x = (_DisplayTransform[0].x * texX + _DisplayTransform[1].x * (texY) + _DisplayTransform[2].x);
o.texcoord.y = (_DisplayTransform[0].y * texX + _DisplayTransform[1].y * (texY) + (_DisplayTransform[2].y));
return o;
}
// samplers
sampler2D _textureY;
sampler2D _textureCbCr;
float _dimFactor;
fixed4 frag (TexCoordInOut i) : SV_Target
{
// sample the texture
float2 texcoord = i.texcoord;
float y = tex2D(_textureY, texcoord).r;
float4 ycbcr = float4(y, tex2D(_textureCbCr, texcoord).rg, 1.0);
const float4x4 ycbcrToRGBTransform = float4x4(
float4(1.0, +0.0000, +1.4020, -0.7010),
float4(1.0, -0.3441, -0.7141, +0.5291),
float4(1.0, +1.7720, +0.0000, -0.8860),
float4(0.0, +0.0000, +0.0000, +1.0000)
);
fixed4 rgba = mul(ycbcrToRGBTransform, ycbcr);
return rgba * fixed4(_dimFactor, _dimFactor, _dimFactor, 1);
}
ENDCG
}
}
}
Hello @hammadb This solutions seems to do the trick, however, the camera feed seems to be rotated 90 degrees for me. It looks stretched and as Landscape when in portrait and so on. Any idea why?
Your answer
Follow this Question
Related Questions
Leaves disappear in game mode 0 Answers
Grey/Dark line between two cubes 1 Answer
Unable to Launch Game:Shaders Problem 0 Answers
How to bring UI element infront of other objects? 0 Answers
Problem with follow-up camera with curved world shader 1 Answer