Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Procedural object placement

Discussion in 'Scripting' started by EBR, Jul 8, 2014.

  1. EBR

    EBR

    Joined:
    Jan 27, 2013
    Posts:
    117
    I have a procedural cave system (webplayer) which uses voxels and some smoothing etc. How would I procedurally go and place items? do you think I would somehow use the voxel data to do this? any suggestions? Thanks. :)
     
  2. Pysassin

    Pysassin

    Joined:
    Dec 27, 2012
    Posts:
    87
    I'm not sure how one generates anything close to this but is there anyway on chunk load you can get the middle of chunk and shoot a ray with a random direction (though always toward the ground) and then use that point to place an item?

    On a complete side note it is pretty fun just to roam around and try to fall through your caves before they fill in. "Played" this demo for like 5-10m just doing that lol. Also your top and bottom textures are flipped. Your standing on top and look up the bottom in this build.
     
  3. EBR

    EBR

    Joined:
    Jan 27, 2013
    Posts:
    117
    Thats an interesting way of doing it. Also the textures aren't flipped its just the names are confusing. The 'Top' should be where the player stands and the 'bottom' is the ceiling. Yeah I agree this is confusing and illogical. But they are placeholders so its fine.
     
  4. NeverConvex

    NeverConvex

    Joined:
    Jun 26, 2013
    Posts:
    88
    I'm (slowly!) working on a similar project (although my current webplayer is somewhat blockier: Claustrophic Caves Webplayer); if the voxelization system you're using is similar to the one I'm using (MarchingCubes / "chunk" based --- which I think must be the case, since I'm not aware of any alternatives to a MarchingCubes-like algorithm, and the 65k tris per mesh requirement imposes chunks whether you want 'em or not), then I'd think you have a lot of options. The first option to occur to me is:

    If you want to procedurally place chests, you could randomly select a chunk, randomly select a position within that chunk, check that the (hopefully primitive) collider on the chest isn't intersecting your preexisting mesh collider, and, if so, place it. Add a RigidBody to force the chest to fall onto the mesh collider. (I think this will work, so long as the chest has a primitive collider?)

    A second obvious option is to try to procedurally identify the voxels that contain the surface, and place objects 'next' to them. I think this is doable, but requires working with the specifics of the MarchingCubes (or MarchingTetraheda, etc) interpolations; you could place a torch on a wall, say, if you identified the neighboring voxels and determined there was enough room for the torch, but this all means figuring out what the linearized surface looks like that your surface-approximation algorithm looks like. This second option is (at least for wall-hanging structures, like torches) a lot simpler if you do a somewhat 'blocker' (in corridor structure, but still not Minecraft-like) design for chunks, like I did, since you could easily set walls to be flat and throw torches on them, only having to identify chunks ahead of time.

    A third option might present itself if your objects don't particularly mind getting covered up (or partially covered up) by other objects; in that case, you could just implement a second pass of your surface-generating algorithm (e.g. MarchingCubes), and use that to generate fire or water or what have you. It might poke out of the surface at weird places, but if that's OK for your purpose...
     
  5. EBR

    EBR

    Joined:
    Jan 27, 2013
    Posts:
    117
    @NeverConvex Thanks for the tips and methods. I will explore them all and choose the best one. I am thinking the second one will be the one I will use but we will see. Thanks for the quick reply :)
     
  6. NeverConvex

    NeverConvex

    Joined:
    Jun 26, 2013
    Posts:
    88
    No problem! The idea's constantly in my head, since I'm working on something similar.

    An optimization idea: are you saving the spots in your chunk array that are empty/not? That makes it pretty easy to single out on the areas that you may need to actually do detailed surface work.
     
  7. EBR

    EBR

    Joined:
    Jan 27, 2013
    Posts:
    117
    I haven's got a working example yet, but I will post when I am done. Also, I can't view your demo/example for some reason... not sure why :(
     
  8. NeverConvex

    NeverConvex

    Joined:
    Jun 26, 2013
    Posts:
    88
    Oh, bummer! What happens when you try to start the webplayer? It's a really large map (but not infinite) that it initially loads, so it takes maybe 30 seconds for it to actually load everything.
     
  9. EBR

    EBR

    Joined:
    Jan 27, 2013
    Posts:
    117
    My browser says the page is unresponsive. I think my computer is too S***ty to run it :(

    also I am detecting possible areas for objects to spawn with the normals of the mesh.
     
  10. NeverConvex

    NeverConvex

    Joined:
    Jun 26, 2013
    Posts:
    88
    Ahh, that might be it. Here's a smaller version, maybe it'll work:

    Claustrophobic Caves (v. small) Webplayer

    I like the normals idea. That should give you a convenient way of identifying where the insides and outsides of the cave system are for object placement.