- Home /
Modifying vertices values in a mesh
I've been trying to apply brushes as heightmap on a sphere but there's some weird stuff going on that I can't find an answer to.
What is happening is that when I apply the heightmap on a vertex, it seems like it's not affecting the good vertex. I'm checking if from the center in the direction of the vertex there's a collision with a brush and if so, I'll add a vector3 in the same direction as the raycast used but as you can see in the image below, it affects the vertex on the left side instead of on the top of the sphere.
The code is straightforward but I can't seem to understand what's happening and why.
Also, if the brush is on x axis, it appears fine but otherwise, the z and y are invert.
The code I used is:
void Start () {
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Vector3[] sphereVertices = new Vector3[mesh.vertexCount];
for(int i=0; i<vertices.Length; ++i){
Vector3 vertex = vertices[i].normalized * 3;
RaycastHit info;
if(Physics.Raycast(Vector3.zero, vertex, out info)){
Vector3 addHeight = vertex.normalized;
addHeight *= GetGrayscale(info.collider.transform.renderer.material.GetTexture("_MainTex") as Texture2D,info.textureCoord);
vertex += addHeight;
}
sphereVertices[i] = vertex;
}
mesh.vertices = sphereVertices;
mesh.RecalculateNormals();
}
Answer by robertbu · Jun 01, 2013 at 05:57 PM
I don't know if this is the issue without more study, but your vertices are in local space and your brush is in world space. You may need to do a Transform.TransformPoint(). If you sphere is not at the origin, you also have to take that into account:
RaycastHit info;
Vector3 v3 = transform.TransformPoint(vertex) - transform.position;
if(Physics.Raycast(Vector3.zero, v3, out info)){
Great! It's working :) Thanks!
If you can create an answer, I can mark it as the solution
I've converted the comment into an answer.
btw. $$anonymous$$eep in $$anonymous$$d that your collider will stay the same and will not reflect the new shape unless:
You use a $$anonymous$$eshCollider ins$$anonymous$$d of a SphereCollider
You update the mesh of the collider as well
Using the .mesh property will duplicate the mesh. You might want to use shared$$anonymous$$esh ins$$anonymous$$d.
ps: The Collider class has a dedicated Raycast function which will only test for this collider. This is quite useful for such cases.