I’m very very very new to shaders, so it may be extremely trivial but I’m kind of struggling a bit to wrap my head around this shader matter.
I have generated a mesh composed of squares, each square has it’s relative UV coords, and I want to mask the entire mesh with another texture, but I don’t know how to precisely scale/offset my mask texture to encapsulate every square.
Here’s the situation and what I want to achieve with images, because I really do not know any better way to explain myself:
This is my shader:
Shader "Custom/Surface Mask" {
Properties{
_MaskText("Texture", 2D) = "white" {}
[NoScaleOffset] _MainText("Texture", 2D) = "white" {}
[NoScaleOffset] _SecText("CoverA", 2D) = "white" {}
}
SubShader {
pass {
CGPROGRAM
#pragma vertex MyVertexProgram
#pragma fragment MyFragmentProgram
#include "UnityCG.cginc"
sampler2D _MaskText, _MainText, _SecText;
float4 _MaskText_ST, _MainText_ST, _SecText_ST;
struct VertexData {
float4 position : POSITION;
float2 uv : TEXCOORD0;
};
struct Interpolators {
float4 localPosition : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uvMask : TEXTCORD1;
};
Interpolators MyVertexProgram(VertexData v) {
Interpolators i;
i.localPosition = UnityObjectToClipPos(v.position);
i.uv = TRANSFORM_TEX(v.uv, _MaskText);
i.uvMask = v.uv;
return i;
}
float4 MyFragmentProgram(Interpolators i) : SV_TARGET{
float4 mask = tex2D(_MaskText, i.uvMask);
float4 color = tex2D(_MainText, i.uv) * mask.r +
tex2D(_SecText, i.uv) * (1 - mask.r);
return color;
}
ENDCG
}
}
}
This is the current material:
Names are a bit confusing… CoverA and Texture have to be tiled to the squares, the mask will basically be used to create patches of grass.
Hope I explained myself well, and I hope you can help! Thanks anyway for reading.
Some other additional and potentially pointless informations:
Additional info The squares making the terrain are generated via script, in the editor not during runtime. I thought of making a big UV when generating the terrain, by picking the most extreme verts as the coordinates for the UV, and then tile the main and secondary texture accordingly, but there has to be a way to do it in the shader, also that is not desirable since I wanted the mask to not repeat itself nor stretch if I then want to change the shape of the terrain, say by adding more squares: It has to adapt to different meshes in short, obviously each mesh will then have it’s own mask.