Search Unity

Procedurally generated terrain... using prefabs.

Discussion in 'Prefabs' started by Deleted User, May 4, 2020.

  1. Deleted User

    Deleted User

    Guest

    G'day everyone, i have a question about procedurally generated terrain involving perfabs, i was researching how to create an infinite procedurally-generated terrain, made outta cubes (kinda like minecraft) but all the sources that i found use voxels as the way-to-go. I i want to use prefabs, specifically a prefab that contains 6 different planes.

    But the thing is that i'm new to c#, and i'm having a hard time figuring this out.
    So i have a couple of questions:

    How do you make it so when one of the planes that conforms the cube is out of the player sight it stops rendering? (I've tried dynamicOC and "IsVisible" and it didn't quite work.

    How do you merge two prefabs together so they render as one giant mesh, but when you try to destroy one it only destroys the one you're looking at?

    I'm sorry for this kind of nooby questions, but ngl this has me on the ropes.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    If an entire mesh is outside of the field of view of the camera it won't be rendered. This is called frustrum culling and it happens automatically in Unity. You don't need to do anything.
    If you mean hiding meshes that are not visible because they're behind other meshes, then this is called occlusion culling. Unity's solution for this needs baking in the editor so won't work for procedural generated stuff that places meshes at runtime. People doing procedural stuff usually live without it, or make their own solutions.

    Generally, you can't. You can have all your Prefab parts and make a separate combined mesh out of it and have that be rendered while you still keep the parts around but don't render them. Then if you destroy one object, you should destroy the part, and recreate the combined mesh again from the parts, and have the new combined mesh replace the old combined mesh.

    But really, there's no need to then have all the Prefab instances to begin with. The code is roughly the same if you just combine a combined mesh with instructions to use the same Prefab Asset many times at many positions, without having ever instantiated instances of that Prefab Asset.

    A starting point is this API: https://docs.unity3d.com/ScriptReference/Mesh.CombineMeshes.html

    But the problem you're trying to solve is a complex one, and I'd recommend also going through some tutorials on procedural mesh generation. Alternatively, there's stuff on the Asset Store that does what you're talking about already.
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    Alright, thank you so much! This is a complicated thing to do, now that i see the big picture i may wanna try those voxels.