Question about uv, triangles on meshs?

hi,

i am trying to write the mesh of an tube manually, I took some example found in the wiki and improved it, and the meshes works fine.

When I assign diffuse material, it’s fine, I got all texture mapped correctly, but if I try the bumped specular (two textures I assume), the texture mapping went so wrong, most of them are not shown properly. If I take the similar model from Cheetah directly, it’s fine. So I assume the uvs are not written correctly.

Here’s the short version of my code.

		line=new GameObject("Line");
		Vector3[] vertices = new Vector3[n*m];
		Vector2[] uvs = new Vector2[n*m];
		int[] triangles = new int[(n-1)*m*3*2];
		
               // then I went ahead setup vertices, uvs, and triangles

		// attach mesh
    	MeshFilter filter = (MeshFilter)line.AddComponent(typeof(MeshFilter));
    	line.AddComponent(typeof(MeshRenderer));
		filter.mesh.vertices = vertices;
		filter.mesh.triangles = triangles;
		filter.mesh.RecalculateNormals();
		filter.mesh.uv = uvs;

what I’m really puzzled is that why the dimension of the uv vector is same is the dimension of the vertices vector, what if one of the vertices needs two different uv coordinates to make it work, ex. tube, when you go around, the starting and the ending vertex are identical but with different uv coordinates of a plane surface.

should the dimension of the uv vector be same as the dimension of the triangles vector ? so each vertex of a triangle has a known and unique uv coordinates ?

I’m missing some fundamental understanding here :wink:

many thanks in advance. the forum gave me tremendous help so far.
Fang

I went to cheetah, and export a simple triangle sweep along a line, and export the model into an obj file and here it is

# WaveFront *.obj file (generated by Cheetah3D)

mtllib pipe.mtl

g Sweep
v 0.500000 -0.703999 -0.896441
v -0.250000 -0.963024 -0.549446
v -0.250000 -0.271205 -0.033017
v 0.500000 -0.012180 -0.380013
v -0.250000 -0.444974 -1.243437
v -0.250000 0.246845 -0.727009

vt 0.000000 0.000000
vt 0.333333 0.000000
vt 0.333333 1.000000
vt 0.000000 1.000000
vt 0.666667 0.000000
vt 0.666667 1.000000
vt 1.000000 0.000000
vt 1.000000 1.000000

vn 0.500000 -0.518050 0.693992
vn -1.000000 0.000000 -0.000000
vn 0.500000 0.518050 -0.693992

usemtl default
f 4/4/1 3/3/1 2/2/1 1/1/1
f 3/3/2 6/6/2 5/5/2 2/2/2
f 6/6/3 4/8/3 1/7/3 5/5/3

so here I got 6 vertices, but 8 uv coordinates (as I expected), and in the face section (f 4/4/1), everything got referenced :slight_smile:

I just wonder how do I setup the same thing in Unity, thanks.

Fang

ok, I think i figured it out.

I need to set normals to be correct as well. The RecalculateNormals just can’t do the magic :wink: BTW, I got bumped specular working without setting tangents (which the documentation says I need to )

And the obj output from unity reads like this now, (comparing with the one output from Cheetah)

mtllib ./Line_0.mtl
g Line
v 1.192093E-08 0 -0.1
v 0.06123724 0.06123724 0.05000002
v -0.06123726 -0.06123725 0.05
v 2.428438E-08 1.236345E-08 -0.1
v -0.09999999 0.1 -0.1
v -0.03876276 0.1612372 0.05000002
v -0.1612373 0.03876276 0.05
v -0.09999998 0.1 -0.1

vn 1.192093E-07 0 -1
vn 0.6123724 0.6123724 0.5000001
vn -0.6123726 -0.6123725 0.5
vn 2.428438E-07 1.236345E-07 -1
vn 1.192093E-07 0 -1
vn 0.6123724 0.6123724 0.5000001
vn -0.6123726 -0.6123725 0.5
vn 2.428438E-07 1.236345E-07 -1

vt 0 0
vt 0 0.3333333
vt 0 0.6666667
vt 0 1
vt 1 0
vt 1 0.3333333
vt 1 0.6666667
vt 1 1

usemtl vessel-material
usemap vessel-material
f 6/6/6 1/1/1 5/5/5
f 2/2/2 1/1/1 6/6/6
f 7/7/7 2/2/2 6/6/6
f 3/3/3 2/2/2 7/7/7
f 8/8/8 3/3/3 7/7/7
f 4/4/4 3/3/3 8/8/8

so instead of cross-reference, now essentially all vertices/uv/normals’ dimension is raised to 8.

Just curious if this is the correct way? and is this the only way ? thanks.

Fang

The lighting won’t be correct without tangents though. It “works”, but not really.

–Eric

thanks Eric,

I want to write the correct tangents, but I don’t know how :slight_smile:

what language is this? just kidding. ok, tangent is a vector, good, tangent is tangent to the surface ? ok, there’s million vectors on the mesh surface which is along the surface and perpendicular to the normal vector of the surface. Which vector then?

along texture direction, god, texture doesn’t have direction. I thought texture is a virtual plane which doesn’t exist in real model.

Please help me with these fundamental definitions. Thanks.

A look at the mesh combine utility script in Standard Assets shows how tangents are computed.

–Eric

I don’t see the light of figuring this out from mesh combine utility, it’s too simple, and it didn’t calculate, i think it’s sort of transformation there.

here’s an article that explains the tangent, binormal and normal, 3D and Audio APIs and Softwares - Jérôme JOUVIE - http:/jerome.jouvie.free.fr/ .

From what it says, as long as texture coordinates and world coordinates are given, tangents and binormal of the texture surface can be determined right away. And seems this tangent vector is unique once the orientation of the triangle is given.

I wonder why it’s not the case here (since I already set uv and vertices), anyone here has better reference as why and how to use tangents manually ?

thanks.

I used the mesh combine utility as a reference for how to calculate the tangents, and it does work.

			Vector3 p = new Vector3(p4.x, p4.y, p4.z);
			p = transform.MultiplyVector(p).normalized;
			dst[i+offset] = new Vector4(p.x, p.y, p.z, p4.w);

“transform” in this case is not the transform component of the object, but a Matrix4x4 that is calculated earlier and passed to the function.

–Eric

ok, thanks, Eric,

so can you confirm that once uv and vertices are set, the tangents can be computed ? is that right?

If so, I’ll go dig a bit more and see if I can extract this piece out of combine utility.

:slight_smile:

Oops, wait…I’m just copying tangents from already existing meshes and applying them to a new mesh, rather than creating them from scratch. I don’t really have any idea how to do that, sorry. :sweat_smile:

–Eric

it’s alright, thanks for your help, eric.

I’ll go digging into Procedure demo to see if anything’s there :slight_smile: