Can anyone explain to me how texture coordinates on models work? My current understanding is that the coordinates are 2d vectors that describe a point on the texture image, where the axes are between 0 and 1, where 0,0 is one corner of the texture image and 1=the width of the image. The problem I am having is that when I apply a texture to an object created at runtime from a .x object file (the texture coordinates are as described) I get a uniform colour over the whole surface. My guesses to why were either a) the coordinates are wrong and only a single pixel from teh image is being applied to the surphace or b) the unity engine is somehow blurring/averaging the colours of the texture and I am just seeing an average of all the texture pixels.
Also, do textures have to be square? Is the program able to work out if they are rgb or bgr and how many bits the colour is, and if not, how do I tell it in a script what the properties of the image are?
First; No, textures are not required to be square…
Second, it sounds like you got the basic understanding correct, regarding UVs, so why it doesn’t work, must be because of bad UVs. What do you mean “Object created at runtime”, and how do you create the UV set?
There is one 2d coordinate per vertex, each is between -1 and 1, these are read in and each assigned to a Vector2[ ] which is then passed to mesh.uv I just noticed there were negative coordinates there which does not fit in with my basic understanding, perhaps this is meant to place a mirror image of the texture.
UVs are usually in the range 0.0 to 1.0, with (0,0) bottom left of the texture and (1,1) as the top right. It’s independent of the texture resolution.
UVs outside this range is tiling of the same UV texture.
if you have UVs going from (0,0) to (3,3) then the texture is tiled 3 times.
Edit. if you want to do a test, use the vertex x,z as the u, v coordinates. (this a simple planar unwrap)
thanks for the test suggestion, that gave me the insight that cracked the problem. I had copied the code from another part of the script and replaced the variable names, I had missed one variable so the texture was getting set to the x and y of the last vertex read for every texCoord, meaning that only one pixel was being textured. Now all I have to do is solve this problem: http://answers.unity3d.com/questions/37154/www-not-loading-from-image-file
and I am home free.