- Home /
How to dynamically reveal another material/texture
My idea is to basically have a circle/area around the player, and as they move through the level, reveal a different material, you can see the effect in the above picture. (The player has moved from the bottom left, filling the area that they've come from)
I've looked around and tried a couple of different methods. The first being a custom shader that masks out material A to reveal B, and then I'm drawing to a render texture and that's being used as the mask. That somewhat works but it doesn't seem to be exporting to Android at all.
Alternatively I have the effect working with getPixel and setPixel, but that seems to be slow. It uses the player's position to update the texture but there's a slight jitter between the player's location and the texture position being updated. (And also if I wanted a bigger texture that seems to slow it down even more).
So I'm just curious if anybody has achieved this kind of effect or if I'm going down the right/wrong route.
Good day.
I don't know the answer yet. I will have to do somethig similar in some months, I will try to find something similar to "fog of war" in other games, I recommend you to investigate about how fog of war is done, and sure you can adapt that to your issue.
Good luck!
You can probably solve this by applying sprite masks every second at the player's position. Every new sprite reveals a little bit more of the underlying texture. You might have to merge shapes after a while, as a lot of small sprites will slow unity down.
Answer by BastianUrbach · Jan 27, 2019 at 05:34 PM
I guess I'm kinda late but my idea would be to use a trail renderer (or a similar script) and a shader that samples the texture with object space coordinates, like this:
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.vertex.xy, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target {
return tex2D(_MainTex, i.uv);
}
I did actually figure out a solution (using particles to cover the floor and deleting the floor as you go over so it looks seamless) but this actually helps me with another problem I had! I needed to figure out how to get a texture to show in object space (like the picture) and this should help, thanks!