- Home /
vertex[i] TransformPoint command not working
these are dark times at developer studio:I am making generative terrain tiles so I need to calculate the mountains on each tiles relative to their position in world space.
when I try to get the vertices world position with TransformPoint, it's still returning their local position:
Here is the code and debuglog- sampling the 1st vertex of 100 tiles, the tiles are displaced by 200, 400,etc, and their vertex TransformPoint are all the same:
function nzprl ( meshobject : GameObject){//this function takes mesh objects and changes their vertices in XY and Z into mountains
Debug.Log( "object position " +meshobject.transform.position );
var mymeshfilter : Mesh = meshobject.GetComponent(MeshFilter).mesh;
var vtxArray : Vector3[];
var scale = 7;
if (vtxArray == null) vtxArray = mymeshfilter.vertices;
var vertices = new Vector3[vtxArray.Length];
for (var i=0;i<vertices.Length;i++)
{
if ( i== 0 ){Debug.Log( "vertex-position "+vtxArray[i] ); }
vtxArray[i] = transform.TransformPoint(vtxArray[i]);
var vertex = vtxArray[i];
if ( i== 0 ){Debug.Log( "vertex-transformpoint position "+vtxArray[i] ); }
vertex.y += (Mathf.PerlinNoise(vtxArray[i].x,5.456)*Mathf.PerlinNoise(vtxArray[i].z,7.53)-
.5) * scale;
vertices[i] = transform.InverseTransformPoint(vertex);
}
mymeshfilter.vertices = vertices;
mymeshfilter.RecalculateNormals();
mymeshfilter.RecalculateBounds();
DestroyImmediate(meshobject.collider);
meshobject.AddComponent("MeshCollider");
}
this is a really weird bug!using a coroutine, I tried waiting 5 seconds after the mesh has instantiated, to access the vertices, and the transform point is still completely wrong! How can it be wrong!?
Answer by Wolfram · Feb 02, 2013 at 05:29 PM
If I understand your script correctly, your transformed tiles are in meshobject.transform, but you use the TransformPoint/InverseTransformPoint of just transform
, which would be the local coordinates of the object your script is attached to, not the ones of your meshobject.
Thanks, that's a lifesaver, i was just edging along the window sill as you wrote that.
transformpoint isnt always correct for vertices because it doesnt take into account the total size of the parent transform, if the parent transform is devided by 10, the child object vertex would still be scaled by 1... i just tried this with the correct parent and child object transform.transformpoint and the vertex position was not scaled sizewise and was falsely world space.
Your answer
Follow this Question
Related Questions
Transform.TransformPoint 2 Answers
TransformPoint trouble 1 Answer
TransformPoint and InverseTransformPoint: Moving Platform Logic 1 Answer
TransformPoint returns wrong points? 1 Answer
TransformPoint and InverseTransformPoint 2 Answers