- Home /
Linear Texture Masking
Alright so instead of giving some vague description of what I am trying to do I will just explain the scenario.
Making a top-down shooter styled game with Tanks (2D). The treads have proven annoyingly difficult to animate as Unity would have me make a full setup for each individual texture sheet (which I have dozens of). I was wondering if it would be possible to simply make it a repeating masked texture so that the tread object is just "moving over" an repeating pattern of the tread texture.
If there is an easier solution to this that doesn't involve masking that works too.
Answer by Namey5 · Jul 04, 2017 at 10:36 AM
This is actually fairly simple to do in a custom shader. Not knowing the specifics of how you want it to look, something like the following should give you an overview of how it works and if you think the effect is useful.
Shader "Custom/Mask" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_Scale ("Texture Scale", Float) = 2.0
_MainTex ("Albedo (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float3 worldPos;
};
half _Glossiness;
half _Metallic;
half _Scale;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.worldPos.xz / _Scale) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
It basically works by using the per-pixel world position as a texture coordinate rather than the mesh's UVs. If you want the texture to rotate with the object, you can use local space rather than world space.
Your answer

Follow this Question
Related Questions
How to drag and stretch a 2D sprite 0 Answers
Scale down spritesheets without breaking anything else 0 Answers
Is it possible to use sprite as a light? 0 Answers
Sprites Mask with Custom Range not working after build 2 Answers
2D Games with Unity? 2 Answers