- Home /
normalized Z axis is always positive
Hi all. I have a question about normalize coordinates that you get from the mesh. I'm trying to do a simple Sphere, but for some reason I create only one half and the other not. I have observed that this is due to the fact that the coordinates "vertices[i].z" is always positive and never negative. I would like to ask if you do not know what is wrong. Thank you :)
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Vector3[] verticesN = vertices;
int k = 0;
for (var i = 0; i < vertices.Length; i++)
{
verticesN[i] = vertices[i].normalized * 50;
if (k >= 100)
{
Instantiate(sp_debug, verticesN[i], Quaternion.identity);
k = 0;
}
k++;
}
mesh.vertices = verticesN;
mesh.RecalculateNormals();
(Red sphere on picture are only for my debug. You can see there are only half sphere)
what is the actual half sphere about? If that is the mesh you are using then of course you will only get half a sphere
""vertices[i].z" is always positive and never negative" - that rather depends on the model, doesn't it?
normalizing certainly should not change the sign of anything though you could try doing the normalizing manually by changing line 8 to:
verticesN[i] = vertices[i] * 50/vertices[i].magnitude;
I'd be very surprised if you see any difference!
alternatively you could add a few lines:
if(verticesN[i].z*vertices[i].z < 0){
Debug.Log("normalized vertice has a different sign to normal: " + verticesN[i].z + ", " + vertices[i].z);
}
And see if that prints anything, I would agree with tanoshimi that is is unlikely that the code is wrong and more likely that the mesh is not 'centred' i.e. one side of you cube has 0 z values (the pivot point for rotation of the mesh is not in the centre of the cube but at a side)
I wouldn't involve the normal in that debug log. If your z axis is never negative it might not be negative. You can do those tests:
// use the unaltered vertices for this:
if(vertices[i].z < -0.001f){
Debug.Log("z has a negative value: " + vertices[i].z);
}
You could also accumulate all vertex positions to get the geometric center. If your vertices are actually around 0,0,0 the result should be 0,0,0 or quite close to. If the vector is quite huge, the mesh probably isn't centered on 0,0,0
edit
ps: This line Vector3[] verticesN = vertices;
is pretty pointless. It will make your "verticesN" variable to reference the same array as your vertices variable. So you only have one array and effectively doing:
vertices[i] = vertices[i].normalized * 50;
btw: For debugging it might be a problem to only show every 100. vertex as depending on your mesh topology you might produce a strange pattern which actually doesn't exist.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160226im_/https://answers.unity.com/themes/thub/images/avi.jpg)