- Home /
How to move alpha mask along the floor plane as the user (camera) moves
I am using ARKit plugin for an augmented reality iOS app.
What I need:
To display a grid-like texture on the floor. The grid needs to be fixed or in other words, it shouldn't move with the camera. There is a necessity that the grid has alpha mask so that it is visible only inside a radius around the user (camera), with opacity falling with the distance from the camera. As the player moves, the alpha mask follows, so that the camera is always at the centre of the visible grid radius.
What I did:
I added a plane into the scene. I did some tweaking around the original code of unlit/transparent shader and found some examples online and I managed to add my alpha mask to the shader. I apply that material on the plane. I made the plane move with the camera such that the camera is always in the centre of the plane and radius. However, what I need it that the plane doesn't move - that the grid actually stays in place, while the alpha mask move such that the camera is always at the centre of the opacity circle.
Question:
How do I approach this problem? Being very new to shaders, is it possible to do it through a shader? Or is there any better approach to this?
Thanks in advance!
Answer by mkusan · May 02, 2018 at 09:30 AM
Solved my problem with a shader:
Shader "Unlit/AlphaMask" {
Properties {
_Radius ("Radius", float) = 10.0
_MainTex ("MainTex (Sprite)", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 100
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float4 color: COLOR;
};
struct v2f {
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
float4 color: COLOR;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Radius;
// vertex function
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex); // tranforms the vertex from object space to the screen
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
o.color = v.color;
float dist = distance(_WorldSpaceCameraPos, mul(unity_ObjectToWorld, v.vertex));
float alpha = smoothstep(0, _Radius, dist);
o.color.a = 1 - alpha;
return o;
}
// fragment function
fixed4 frag (v2f i) : SV_Target
{
fixed4 main = tex2D(_MainTex, i.texcoord);
return fixed4(main.r, main.g, main.b, (main.a*i.color.a));
}
ENDCG
}
}
}