Mistakes when declaring _uv variables?

Hi,

in this code, in order to use a distortion map, I have got some errors, would you know how to fix them?

"ERROR: ‘uv_DistortionMap’ : undeclared identifier " , by double clicking on it, it points to CGProgram…

Here is the code :

_DistortionMap("_DistortionMap", 2D) = "black" {}
//...
uniform sampler2D _DistortionMap;

struct a2v
{
	//...
    float2 uv_DistortionMap : TEXCOORD0;
}; 

struct v2f
{
	//...
    float2 uv : TEXCOORD0;
    float2 uv2 : TEXCOORD1;
};


 v2f vert (a2v v)
{
    v2f o;
	float2 DistortUV = (uv_DistortionMap.xy) + DistortSpeed;
	//...
    o.uv = TRANSFORM_TEX (myDistortion, _MainTex);  
    o.uv2 = TRANSFORM_TEX (myDistortion, _Bump);
    
    return o;
}

Would you have any ideas?

Thanks for your help

I’m not really sure what you’re trying to do here.

But to use TRANSFORM_TEX, you feed in the UV and then _TexturePropertyName_ST.

The errir is because you aren’t referencing your struct when you reference uv_DistortionMap.xy.
You need to use **v.**uv_DistortionMap.xy - because you’re passing your a2v struct into the vertex shader as a variable called v.

_DistortionMap("_DistortionMap", 2D) = "black" {}

struct a2v
{
    float2 uv : TEXCOORD0;
}; 

struct v2f

{
    float2 uv : TEXCOORD0;
    float2 uv2 : TEXCOORD1;
};

uniform float4 _MainTex_ST;
uniform float4 _Bump_ST;

v2f vert (a2v v)
{
    v2f o;
    float2 DistortUV = v.uv + DistortSpeed;
    o.uv = TRANSFORM_TEX (DistortUV, _MainTex_ST);  
    o.uv2 = TRANSFORM_TEX (DistortUV, _Bump_ST);
    return o;
}

uniform sampler2D _DistortionMap;

Thanks Farfarer, what if I have more than 1 texture in the shader, how will Unity understand that " float2 uv : TEXCOORD0; " in a2v is the uv from the distortion map and not the one from the main texture for example?

(I would like to use a simple distortion map to create small waves on a toon shader, I have got the toon shader, written as a frag/vert shader, and I would like to add the distortion map to it. So I would like to use only the uv from the distortion map, and apply it on the main texture and the bump image)

It won’t - that’s just pulling the first UV set from the mesh. You can have 2 sets of UVs for each mesh, maximum.

You transform it to match the tiling/offset settings of your texture in the vertex shader, using TRANSFORM_TEX().

If you want a texture to warp the UVs you’ll have to do that in the fragment shader unless you only want it to warp the UV coordinates of each vertex.

OK thanks, but if I have got “main tex” and a “distortion map” declared in the shader, which one would be (and how to separate them) in the a2v struct?
I will have to check more examples, because the waves on a custom mesh is not as simple as I thought! :wink:

There isn’t a way to distinguish them - they use the same UV map as far as the a2v struct is concerned.

However, the tiling/offset values of the textures are available to you in the vertex shader. So you apply them to that UV map in the vertex shader and pass those to the fragment shader - via v2f - for sampling the texture in the fragment shader.

OK thanks a lot for your answers