# How to select coordinate position within uv texture in a shader - Unity

first of all I'm sorry for asking noob-ish question. This is my fifth day trying to make a shader in Unity.

I'm trying to select an area within my UV image as shown below

and here's how it looks like on the mesh

Basically I'm only selecting the red from the UV and apply it to the mesh. I encountered no problem when trying to select from the RGB, but when i try to use the XYZ for the coordinate position, it turns out that the value of x and r is the same, so is y and g, and z and b.

My question is, how to select by the coordinate position in the UV texture?

Thanks.

here's my code :

    Shader "Custom/ASK RGB" {
Properties {
_tex ("Tex", 2D) = "white" {}
}
Tags { "RenderType"="Opaque" }
LOD 200

CGPROGRAM
#pragma surface surf Lambert

sampler2D _tex;

struct Input {
float2 uv2_tex : TEXCOORD1;
float4 pos : TEXCOORD1;
};

void surf (Input IN, inout SurfaceOutput o) {
// my variable used to store the UV texture mentioned
half4 tex = tex2D (_tex, IN.uv2_tex.xy);

// my attempt to use position which doesn't work
half4 texPos = half4(IN.pos);

half4 tryUV = tex2D(_tex, IN.uv2_tex.xy);

tex.a = 1 - tex.b - tex.r - tex.g;

tryUV.a = tex.r;

o.Albedo = tryUV.rgb * tryUV.a;
}
ENDCG
}
FallBack "Diffuse"
}


.xyz is just the same as .rgb, the idea is that you use whichever makes the most sense in the context you are using it.

I think the values you want are in IN.uv_tex.x and IN.uv_tex.y

• Thanks so much for the fast reply! It works perfectly as I intended! :D – Alf Aug 3 '16 at 10:21