Procedural terrain Qs(physics, normals, performance)

I’m making a procedurally (at game init) generated
world, but I have a few problems with the bare terrain at this point.
It takes 2-3 seconds to generate the final height map and polygon terrain. The only code I can think off the top of my head is from the Procedual pack (this modded function)

1 : Faster way of attaching MeshCollider to terrain? It takes 40+ seconds to attach MeshCollider to the terrain. If I do GO.collider.sharedMesh = mesh than it takes ‘only’ 20 onseconds.

2 : Having trouble with the edges of my terrain. I split my terrain into 4 sections (65k polygon limit) however when I do this the normals (smoothing?) won’t follow perfectly into the next terrain.
http://img25.imageshack.us/img25/5329/60255733.jpg
Unless there’s a simpler way, my solution would be to have the borders overlap.

3 : My game is going to be a near top-down. Is there any significant gains to be had if I split the terrain into even smaller chunks?

4 : Much of my game is going to be generated at start - including buildings. Deleting faces off the terrain is a painless procedure (this would happen before collision is done)for things such as basements?

I skip question 1, as I don’t know.

I’m assuming then that you let Unity calculate the normals? The problem is that at the edges, Unity does not have sufficient information about the neighboring triangles to calculate the correct normal. You will need to manually average the normals along the edges to make sure that each pair of connected edge vertices has identical normal values (this also applies to position and uv’s, of course).

‘even smaller’? :stuck_out_tongue:
65k polys is a lot. Your game should definitely gain from much smaller chuncks.

This question is rather vague. Are you asking if it is safe to delete triangles from the surface? It is trivial to remove triangles, however it is very difficult to avoid holes where they don’t belong unless your houses are perfectly aligned with the triangles.

Could anyone go a bit more in depth with this? I understand what you’re saying (giving it the next pixel’s data so it smooths correctly) just not how to do it.

Modded script so the game splits it even further. Chanced it to be 32x32 instead of 255x255. On the plus side this made the full terrain-render (including physics) take 7 seconds.

Are you procedurally generating a 3d mesh that you use as terrain or are you generating a heightmap from which you let Unity create a terrain? I think the latter option would be a better way of handling it (unless you’re running into Unity’s terrain system’s restrictions) since you’d get a Terrain Collider that would be a lot more performant than many many Mesh Colliders. You also would be able to take advantage of the Terrain LOD, trees, detail meshes etc.

I am generating my own terrain map, with it’s own texture maps, and some faces being deleted. The links I posted in the first post are the terrain gen scripts.

Basically, two connected terrain segments join together in a strip of vertices. Along this strip each vertex on segment A has a neighbour vertex in segment B, that is located in the exact same position. Your problem however, is that the A-vertex has a normal that differs from the normal of the B-vertex. To make these normals the same, calculate the average normal and assign it back to the vertices:

Vector3 average = (normalOfVertexA + normalOfVertexB) *.5f;
normalOfVertexA = average;
normalOfVertexB = average;

Note that in corner vertices where 4 segements come together, you need to average 4 normals instead of 2.

Many apologies but I’m terribly new to this.
How exactly would I go about doing this? I’m trying to do as much of this by myself as I can, but I am getting nowhere by myself.

A condensed form of the function :
1:First Loop : Sets up vertices, UVs, and tangent locations.
2:Adds vertices UVs to mesh.
3:Second Loop : Sets up polygons.
4:Adds polygons (triangles) to mesh, Recalculates normals, and adds Tangents to mesh.

I’m very confused as to how I get the neighboring normals if they don’t exist.

As a (horribly inefficient) solution I could generate a dummy mesh that has an extended border, and grab the normals off of it when I generate the true mesh

Bump.

You can either calculate the normals for the whole mesh yourself (and arrange it so the neighbours have the same values) or else use RecalculateNormals and then set the normals yourself just for the vertices along the seam. The exact way you do this will depend on the ordering of the vertices, but you basically need to get the two normals of the points that coincide. You can get the average of the normals simply by adding them, dividing by two and normalising the result. Then, assign the same average normal back to both the vertices. If they are both in the same position and have the same normals then the seam should be perfect.

Yes, I know. But I’m asking how to get the extra vertices to average.
Q: “I split my 256x256 terrain into multiple 64x64 block. My vertices at the edge of each block are not averaged with the next block’s vertices. How do I average teh vertices with the next one over?”
A: “Average the vertice with the next one over”

I am generating each block at a time, as an individual function call. The problem I have is I have no idea how to get the next vertice’s normal if it doesn’t exist yet (because it’s part of the next block, and thus may not exist).

Every square should know if it has neighboring squares. If you create square one, it has no neighbors. Then you create square two right next to it, and it knows it has square one as a neighbor, so it averages it’s own and square ones’s normals. Etc.