for (int s = 0; s < o_groundMesh.GetSurfaceCount(); s++)
{
Godot.Collections.Array surfaceData = o_groundMesh.SurfaceGetArrays(s);
Vector3[] borderVerts = (Vector3[])surfaceData[0];
for (int o = 0; o < borderVerts.Length; o++)
{
if (borderVerts[o].X < 0)
{
borderVerts[o].X -= halfAdditionX;
}
else if (borderVerts[o].X > 0)
{
borderVerts[o].X += halfAdditionX;
}
}
o_groundMesh.SurfaceSetArrays(s)[0] = borderVerts; //my attempt
}
Question
I want to change the vertices as shown, but the mesh isn’t updating the changes (since is not applied). In Unity, it was a matter of setting the mesh = to another mesh, but since Godot has surfaces am a little confused on how to do this. Reading the documentation doesn’t help either.
// Excuse my ignorance: I don't understand your question (I think you refer to it being a Mesh?)... however, I think it's working now, since the mesh apparently changed, but it doesn't look how it is supposed to look like and I suspect that it might be because is interpreting a single..triangle? Could you help me with this?
var arrMesh = new ArrayMesh();
var arrays = new Godot.Collections.Array();
arrays.Resize((int)Mesh.ArrayType.Max);
for (int s = 0; s < o_groundMesh.GetSurfaceCount(); s++)
{
Godot.Collections.Array surfaceData = o_groundMesh.SurfaceGetArrays(s);
Vector3[] borderVerts = (Vector3[])surfaceData[0];
for (int o = 0; o < borderVerts.Length; o++)
{
if (borderVerts[o].X < 0)
{
borderVerts[o].X -= halfAdditionX;
}
else if (borderVerts[o].X > 0)
{
borderVerts[o].X += halfAdditionX;
}
}
arrays[(int)Mesh.ArrayType.Vertex] = borderVerts;
arrMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, arrays);
}
MeshInstance3D _o_ground = GetNode<MeshInstance3D>("./o_ground");
_o_ground.Mesh = arrMesh;
Thanks for helping me. Am trying to implement that code, but in c# (mostly with the help of chatGPT), since I don’t know GDScript, and am getting an error in the packedVector3Array and Randi. At any case, what you are doing seems to be right. Could you share a minimalistic simplified code (c#) of this? If you can’t, at least erase those lines, since I can more or less read GDScript anyway (also the vertArray line shows an error)
I finally figured out why. It’s because, by default, the size of the plane is smaller. However. then everything goes back to the original problem, since my ground is a 3D mesh and your entire code doesn’t work for that. Is there any way to work around this?
var arr_mesh = new ArrayMesh(); // I finally found the way:
for (int s = 0; s < o_groundMesh.GetSurfaceCount(); s++) {
Godot.Collections.Array meshArrays = o_groundMesh.SurfaceGetArrays(s);
Vector3[] vertArray = (Vector3[]) meshArrays[(int) Mesh.ArrayType.Vertex];
for (int t = 0; t < vertArray.Length; t++) {
if (vertArray[t].X < 0) {
vertArray[t].X -=
orig_halfAdditionX; // Add or subtract from original size, otherwise
// it only works for a frame because o_groundMesh
// is constant from _Ready()
} else if (vertArray[t].X > 0) {
vertArray[t].X += orig_halfAdditionX;
}
}
meshArrays[(int) Mesh.ArrayType.Vertex] = vertArray;
arr_mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, meshArrays);
}
GetNode<MeshInstance3D>("./o_ground").Mesh = arr_mesh;