Search Unity

Uniblocks: cube-based infinite voxel terrain engine

Discussion in 'Assets and Asset Store' started by RawLionWorkshop, Feb 2, 2014.

  1. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    The trigger collider has to be on a separate child GameObject because you can't have two mesh colliders on a GameObject, so the Chunk itself has the regular collider, and for the trigger collider a child object is created. If you have any custom components on the Chunk object, you'll need to find some way for them to interact with the child GameObject instead.

    Custom components on the actual block prefabs should work the same regardless of whether they have solid colliders or trigger colliders, but remember that the components of individual blocks aren't actually active in the scene, they're only accessible if you instantiate a copy of the prefab manually.
     
  2. Riwer

    Riwer

    Joined:
    Jan 19, 2014
    Posts:
    23
    Ok, but uniblocks dont should be give us an option to set at least one prefab as a child objet for the chunk?
    In my case the chunk loses all the custom components and the child object only have the trigger collider :/.

    But ok, with code isn't a pain, but isn't "clean" I think.
     
  3. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    I'm not sure if I understand you correctly, but if you want to add custom components to the trigger collider object, you can do that by adding them to the prefab - it's at Uniblocks/UniblocksObjects/ChunkObjects/chunk trigger collider

    You can also add custom components to the actual chunk prefab (it's in the same folder). The components shouldn't be lost in any way if you attach them to the prefabs.
     
  4. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    The 1.2 update is now live.

    I wasn't able to improve the performance as much as I initially planned, but the loading times should still be significantly faster. The biggest optimization was in the saving process, so that now saving the world should be practically instant in most cases. Loading data and mesh generation should also be somewhat faster.

    In addition, I have added support for multiple materials. You can now select a material for each block using the block editor. This could be used for things like water or other blocks that require a different material or shader. Check the user manual for more details!
     
  5. Riwer

    Riwer

    Joined:
    Jan 19, 2014
    Posts:
    23
    Hi maap! Awesome update. But I have a question.
    I'm using the new default settings and I can appreciate the better loading speed. But "is not constant", I mean:

    After you load the chunks regarding to your position, when you move your character in one direction, there is a time which the chunks are "waiting" to start load. They load fast, but there is enough pause time between loads. (for despawn and spawn chunks)

    Sometimes the "mysterious waiting time" is so high that my character can fall out before generating new chunks.
     
  6. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    Thank you for bringing this up, I think I know what the problem is. The delay itself is normal, it happens after the chunks are loaded and is caused by existing chunks being updated to match the newly spawned ones (otherwise you'd have holes in the blocks at the borders of chunks), but it takes too long because of some errors in the code. I need to test this properly and will release a patch soon, but for now try this quick fix and let me know if there are still any problems:

    replace everything in ChunkManager's UpdateNeighborsForFreshChunks function with the following:
    Code (csharp):
    1.  
    2.      foreach (Chunk chunk in FreshlySpawned) {
    3.        
    4.        chunk.GetNeighbors();
    5.        foreach (var neighbor in chunk.NeighborChunks) {
    6.          if (neighbor != null && neighbor.Fresh == false) {
    7.            neighbor.FlagToUpdate();
    8.            UpdatesThisFrame += ChunkManager.UpdateValue;
    9.            
    10.            while (UpdatesThisFrame > 1.0f) {
    11.              yield return new WaitForEndOfFrame ();
    12.              UpdatesThisFrame -= 1.0f;
    13.            }
    14.          }
    15.        }
    16.      }
    17.      
    18.      
    19.      UnflagFreshChunks(); // NEXT STEP <===== 6
    20.  
     
  7. JaggedGlobe

    JaggedGlobe

    Joined:
    Aug 22, 2014
    Posts:
    5
    Hi. Amazing job on Uniblocks! How would I go about getting something like trees to spawn on the surface of my map?
     
  8. Brendonm17

    Brendonm17

    Joined:
    Dec 9, 2013
    Posts:
    43
    Hello, maap. I'm not sure if this project is still active or not, but i was wondering how hard would it be to add stone beneath the dirt? Or say random clumps of ores in the ground? If it's not to hard, I am interested in purchasing this asset.
     
  9. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    You'd need to create a custom terrain generator or modify the example one. I can give you a general outline of the steps it would take, but the details will depend on the specific needs of your project.

    First, the generator would need to determine where to place trees. You can do this randomly, or procedurally using something like perlin noise, or even some other method depending on your needs. You'd probably need some way of making sure the trees aren't placed too close together, and that they're properly positioned on the surface of the terrain.

    You'd also need a function that sets voxels in the shape of a tree, starting at a given voxel index and setting the voxels relative to the start. This function would be called every time the terrain generator places a tree.

    You could run the tree generation process after the rest of the terrain has already been generated, or you could do it at the same time. For example, in the example terrain generator, grass blocks are only placed on the surface of the terrain, so you could simply do a random check to determine if a tree should be placed on top of a grass block every time one is placed (but only if there are no trees within x blocks already).


    These would require modifications to the terrain generation algorithm, or writing your custom one from scratch. In fact the default Uniblocks terrain generator is just a very basic example script, which should be modified anyway in order to achieve something more satisfactory, so you'd need to know at least the basics of procedural terrain generation. The things you listed could be achieved in many different ways, the example terrain generator script can give you an idea on where to start.
     
  10. Brendonm17

    Brendonm17

    Joined:
    Dec 9, 2013
    Posts:
    43
    Ok, thank you map. I will look into this more, and get back to you.
     
  11. CodeSlacker

    CodeSlacker

    Joined:
    Dec 5, 2014
    Posts:
    3
    Hi maap, I have been looking at the voxel assets at the unity store. I have been working off and on with a 2d project, but cannot get the thing moving (every time I add something, the performance goes splatt!)

    From what I have seen from earlier posts, you have the save-load, multiplayer worked out, a place for where my own procedural terrain generation would plug in, and removal of individual voxels? How about voxel manipulation?

    For example: I blast a radius in the dirt and want to carve it out. Can I pull all the affected voxels out, and replace the ones on the edges of the circle with a dynamically created mesh?
     
  12. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    Voxels can be accessed directly by their voxel index (x,y,z), so you can easily modify any voxel whenever you want. Changing voxels in a circle around a specific point would be easy enough, you'd just need to figure out the correct index offsets for all the voxels affected.

    However, dynamically created meshes are not supported. Custom meshes can be used, but they have to be pre-assigned to specific voxels and cannot be changed later. But if you have a finite set of possible meshes to generate, you could assign each to a specific voxel and place them as needed.
     
  13. Riwer

    Riwer

    Joined:
    Jan 19, 2014
    Posts:
    23
    So, if I have a tree, a giant rock, a castle etc etc as custom meshes(prefabs), I need to asign every mesh to a voxel in the uniblocks voxel database?


    We can't instantiate custom prefabs in a point of a chunk?
     
  14. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    Uniblocks does not handle custom prefabs in any way. You can instantiate prefabs as you like, but you'd have to do it manually, outside of Uniblocks.
    Voxels can have a custom mesh (not prefab!) assigned - for example, the tall grass block and the door block in the demo use a custom mesh. This could also be done with a big mesh like a castle or a tree, but it probably wouldn't be very practical.
     
  15. CodeSlacker

    CodeSlacker

    Joined:
    Dec 5, 2014
    Posts:
    3
    Okay, that does answer my question. I would need to anticipate and create a mesh that would fit certain corner situations, then set the voxel to that index.

    Do you suppose there would ever be a modification that would allow for mesh adjustment? For example, an offset value held in the voxel that could be used to move a point in the mesh? I am sure you have seen some of the programs that utilize this. Sony's "Landmark" is one example.
     
  16. NathanHale1776

    NathanHale1776

    Joined:
    Mar 22, 2014
    Posts:
    46
    We bought this asset, few things: the bug falling through the terrain if you place a block under you should be fixed by you.
    You show trees and water generated in your images on the asset store as well as the video for this asset. This does not actually exist. You can "build" a tree, but they are not rendered as the terrain is. For 75 bucks I feel this should be addressed.
     
  17. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    The trees in the screenshots, along with other things shown like the house, tall grass, doors, etc, are only shown as examples of what you can do with the engine, and they're not actually listed as features anywhere. I'm sorry if it wasn't clear enough, but I couldn't possibly list every feature that ISN'T included, I can only list features that are.
    The documentation also covers every feature in detail, and it can be downloaded before buying the asset. And if anything is unclear you can always just ask me.

    By the way, if scroll up a few posts in this thread (post #159), you can find my post describing a method of adding trees, it shouldn't be too difficult.
     
  18. catalys

    catalys

    Joined:
    Dec 29, 2014
    Posts:
    6
    Is there any option to generate biomes to?
     
  19. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    Biomes aren't included, but you can add them as part of a custom terrain generation algorithm. This requires some knowledge of procedural generation and coding skills.
     
  20. NathanHale1776

    NathanHale1776

    Joined:
    Mar 22, 2014
    Posts:
    46
    maap, HOW using your product can we generate trees? How to generate water? Code examples please.
     
  21. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    I have described how to generate trees in the previous post. If any part of it is unclear, feel free to ask me, but please don't expect me to actually write all the code for you.

    As for water, there are many ways of doing it, depending on the specific needs of your project, but proper voxel water is generally not an easy thing to do. There is no way I could just give you the exact solution you need in one forum post like this. It's just something you'll need to develop on your own, or find another third-party solution.

    Also please note that the features you're asking for are not actually included in Uniblocks, so technically I have no obligation to even help you with them at all. I do try to help as much as I can because I'd like my customers to be satisfied, but there are some limits - I can't simply write code on demand for free.
     
  22. NathanHale1776

    NathanHale1776

    Joined:
    Mar 22, 2014
    Posts:
    46
    Maap, I know you aren't obligated, just asking for a solution based upon your product with code. If I knew on my own, i wouldn't be asking :)
     
  23. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    NathanHale1776 likes this.
  24. Martinx09

    Martinx09

    Joined:
    Sep 27, 2013
    Posts:
    22
    Hello!

    I'm interested in this source, but I have some quick questions,
    would it be easy to add different biomes?
    I saw you can remove cubes with left click, what about placing cubes? Is that already implemented?
    And last question; what happened to the trees? On the asset store's images I see a tree and some grass but on the demo it's just plain land with no trees or object of any kind, there is no tree/object generation?
     
  25. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    The default terrain generator is actually really basic and it doesn't do biomes or trees. Generally, you'll want to modify it or replace it with your own, and this is where you'd add these features. This requires some coding skills and knowledge of procedural generation.

    If you take a look at the post above yours, there's a link to a terrain generator with trees. It's still very basic and would need some modifications for best results, but it can get you started.

    Placing and removing cubes are both implemented (in the demo, press different numbers on your keyboard to select blocks to place), but these can also be very easily modified to suit your needs. Placing, changing and destroying blocks can all be done with a single function call from anywhere in your code.
     
  26. NathanHale1776

    NathanHale1776

    Joined:
    Mar 22, 2014
    Posts:
    46
  27. rosdi

    rosdi

    Joined:
    Sep 15, 2012
    Posts:
    18
    I have bought this, allow me to do a simple 'review' of the product.

    Let me state that I am a seasoned programmer, (but a web programer.. I never done any game or unity3d programming before.) I bought it at $75 and I don't even complain.. there are a lot of things to learn from the code.

    I consider the money spent akin of buying a specialized book for unity and voxel programming. There are tons! of code samples in there on how to do certain things.. (good or otherwise).

    I am right now developing my own voxel engine in unity and some of the codes are inspired from uniblocks... I learned a great deal (and still learning!).. things like face culling, draw calls, uv, vertices, tris.. (these things were alien to me when I first started)... I even converted greedy meshing algorithm into C# because I want to minimize the triangles and faces... (turned out it has its own complexities and the performance gained isn't worth it). But I learn a great deal...

    If you are moaning about the lack of trees, or bug when you destroyed a block directly beneath the player.. and such... you are in the wrong place. No one in their right mind should expect to be able to publish a game by buying uniblocks. I have inspected the code and while it seems to be working good enough for the demo, it is in no way ready for a real game... just consider it as a thick unity book with tons of code samples..
     
    Last edited: Jan 5, 2015
  28. TradG3me

    TradG3me

    Joined:
    Nov 29, 2014
    Posts:
    35
    how i can make differennt levels of terrain, like colbestone underground
     
  29. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    You'll need to modify the terrain generator, and there are many ways to do that. The default generator already does different layers of blocks (for grass on top and dirt under it), so the easiest way would be to extend that to add cobblestone under the dirt.

    If you add this code in the ExampleTerrainGenerator, after the section labeled // dirt pass, you should get cobblestone two blocks below dirt.

    Code (CSharp):
    1. // cobblestone pass
    2. currentHeight = currentHeight + 2;
    3. if (perlin1 > currentHeight) {
    4.   if (perlin1 > perlin2 + currentHeight) {
    5.    chunk.SetVoxelSimple(x,y,z, 3); // set cobblestone
    6.   }
    7. }
     
  30. TradG3me

    TradG3me

    Joined:
    Nov 29, 2014
    Posts:
    35
    how i can add som random trees in the word?…. and water
     
  31. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    I've posted an example terrain generator with trees a few posts above in this thread (post #173).

    Water would be way too difficult to explain here. There are many ways of doing it, but proper voxel water will take a lot of time and effort to do.
     
  32. King Phoenix W.

    King Phoenix W.

    Joined:
    Feb 4, 2014
    Posts:
    6
    How do you manage the Chunk-Streaming? How good is its performance? Do you use Multithreads or something like that?
     
  33. NathanHale1776

    NathanHale1776

    Joined:
    Mar 22, 2014
    Posts:
    46
    Maap, sorry to bother you again, but I noticed via webplayer that cannot save the map etc can you give a suggestion please on a method for that so that voxel data/map data can be saved? tyvm in advance. (or if anyone else knows)
     
  34. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    I think its impossible to save things on the webplayer anyways. Not sure if standalone Uniblocks has this though, as I dont have it
     
  35. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    Chunks are loaded in the background, and there are settings available which let you balance the loading speed versus framerate (faster loading = lower fps while loading). Generally, with a standalone build you can get steady 60 fps with a reasonably fast chunk loading speed, but you will run into problems if you try to load chunks too fast, like when for example flying over the terrain really fast.
    The demo build should give you an idea of what the performance is like, though it's actually an older version, and the current version should be quite a bit faster.
    Currently there is no multithreading.

    As far as I know the webplayer doesn't actually allow you to save anything on the hard drive. I don't know, there might be ways to work around that, but I'm afraid I won't be able to help you here.
     
    eabykov likes this.
  36. eabykov

    eabykov

    Joined:
    Jan 12, 2015
    Posts:
    11
    Could you help me with the generation of terrain?
    I just want to make a map consisting of 3 layers of grass, earth and unbreakable iron, LIMITED 256 * 256 blocks

    I can not deal with this in any way, do not tell?
     
  37. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    The attached script will generate the terrain how you wanted it, it assumes that voxel id 3 is iron. The script is commented and really simple, you should be able to figure out what's going on in it in case you need to customize it.

    To use it you'll need to replace the ExampleTerrainGenerator script with this one on the Chunk prefab (at UniblocksObjects/ChunkObjects/Chunk).

    To make the iron unbreakable with the default control setup, you'll just need to add a line to the DefaultVoxelEvents script. Below the line labeled with // destroy a block with LMB, put the Voxel.DestroyBlock (voxelInfo); inside an if check:

    Code (CSharp):
    1. if (voxelInfo.GetVoxel() != 3) { }
    This again assumes that iron is id 3, which you can of course change if you need to.
     

    Attached Files:

    eabykov likes this.
  38. eabykov

    eabykov

    Joined:
    Jan 12, 2015
    Posts:
    11
    Thanks!
    And may be you also know how to make borders of the map, because for now my "uniblocks dude" falls down at the end of map.
     
  39. Roachie

    Roachie

    Joined:
    Aug 2, 2009
    Posts:
    67
    Is it easy to modify your engine so it's not infinite terrain but an x by x size world?
     
    Last edited: Jan 14, 2015
  40. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    You mean something like a wall at the borders? The easiest way would be to use a regular Unity object (a cube, for example) with a collider, but if you want it as part of the voxel terrain, try adding this to the terrain generator script (after // set bottom layer to iron)
    Code (CSharp):
    1. // make walls at the borders
    2. if ( absoluteX == 128 || absoluteX == -128 || absoluteZ == 128 || absoluteZ == -128 ) {
    3.     if ( currentHeight >= -1 && currentHeight <= 3 ) {
    4.         chunk.SetVoxelSimple (x,y,z, 3); // set iron
    5.     }
    6. }

    Sure. You'd only need one or two if checks at the appropriate place, either in the ChunkManager (don't spawn chunks if their x or z are above/below the limit) or in the terrain generator (don't generate voxels if their absolute position is above/belowthe limit - that's the way I did it in the terrain generator from two posts above).
     
    eabykov likes this.
  41. eabykov

    eabykov

    Joined:
    Jan 12, 2015
    Posts:
    11


    I mean not that, not a wall of cubes)
    Its like invisible barrier, not build for cubes, its like obstacle to the player could not go beyond the card and advanced high above 64 units, just started with your project and I'm a newbie

    And Its not a your scripts but, why the camera is very narrowed? below, in the second screenshot
     

    Attached Files:

    Last edited: Jan 14, 2015
  42. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    For an invisible barrier, you can just create a default Unity cube and disable it's mesh renderer, then scale it and position it where you need it.

    Not sure what's going with that camera, maybe the Field of View on it is set too high?
     
    eabykov likes this.
  43. eabykov

    eabykov

    Joined:
    Jan 12, 2015
    Posts:
    11
    thank you for what you did not answer my previous post, it helped me to understand and learn to do it without any help, but with this I have a problem:
    I do not go for the physics block, How to do when a block was not supporting it fell under the map and it disappeared?
     
  44. theultimatepwn

    theultimatepwn

    Joined:
    Dec 14, 2012
    Posts:
    6
    Hello, everything works great except for one thing. If I walk, in game, for a long period of time (On both editor and standalone) it crashes with "Fatal error in gc - too many heap sections". The first thing I thought is that data is not getting properly released from memory and/or not getting collected by the garbage collector. Any ideas?
     
  45. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    Unfortunately, checking if a block is floating, unattached to anything, is really difficult, I don't actually have any solution for that that I could give you here. I don't even know if kind of thing this was ever actually done by anyone before in a real-time voxel engine.

    But if all you need is just something like sand or gravel in Minecraft, that would be relatively easy. Any time you modify a block, check if there is a block above (Index.y + 1), and if so, make that block fall (and check again if there is another block above it).
    To make a block fall, you could do the following:
    - set the original block that needs to fall to an empty block
    - spawn a dummy GameObject that looks like the original block in it's place
    - let the dummy GameObject fall (using a rigidbody, scripting, or whatever)
    - when it finishes falling, destroy it, and set the voxel at it's new position to the original block


    Well, I've done some research on this, and it seems that garbage collection in Unity can behave in some unexpected ways, like not actually collecting some objects unless they're explicitly destroyed. After some testing I found what could potentially be a memory leak, though since I can't actually reproduce your error I don't know if it's actually the problem - it seems that the meshes that were being generated for new chunks weren't getting released from memory as they should.

    I solved that by manually forcing garbage collection by sticking a Resources.UnloadUnusedAssets(); at the end of ChunkManager's chunk spawning sequence (in the UnflagFreshChunks function), but, again, I don't know if that actually solves your problem. Try it out, and let me know if you're still getting the error. And if you are, please also test it on a clean project with only Uniblocks and no other scripts, to make sure it's not any of your code causing the error, or even better - test it with the official demo build.
     
    eabykov likes this.
  46. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    You have to actually call Object.Destroy() on that, as unity keeps an unmanaged representation of all UnityEngine.Objects

    Specifically, renderer.material, Meshes and Texture2D are common things people forget to destroy
     
  47. Dwill_Productions

    Dwill_Productions

    Joined:
    Jan 18, 2015
    Posts:
    3
    Hey man, I'm getting this engine, but I was just wandering if there is an inventory system ? Sure hope so
     
  48. RawLionWorkshop

    RawLionWorkshop

    Joined:
    Jan 29, 2014
    Posts:
    206
    I see, thanks. It is rather annyoing though, in all the years I've coded in Unity I've never seen this mentioned anywhere unitl I actually started researching memory leak problems. But eh, I guess you never stop learning...
    Anyway, for now it seems the Resources.UnloadUnusedAssets(); solves the problem, though it might be worth looking into the code for any other potential leaks. I'll release a patch for this when I have some more time.

    Nope. It's a terrain engine, not a game engine. All it does is manages voxel data, streams it in chunks, builds cubic voxel meshes, etc. Gameplay features are not included, except for the few basic ones like placing and destroying blocks, which are only included as examples.
     
  49. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    Yeah the Unity docs tend to be really pointed towards in-editor modifying these kind of things, in which case you don't need to destroy them manually because they aren't copied instances. Setting renderer.material or even getting it via code can create a copy (new instance, indicated in the name by (clone)) of the material which you DO have to destroy manually. I agree this isn't always stated as clear as it should be.
     
  50. theultimatepwn

    theultimatepwn

    Joined:
    Dec 14, 2012
    Posts:
    6
    Resources.UnloadUnusedAssets() solved the problem, interestingly enough I later discovered that it does not happen at all in the 64bit build. I can only assume its just a quirk of the garbage collector. Thanks for your help, this truly is a great asset. On a side note, why do so many people expect you to add all their game features for them? On the other hand I was once that 14 year old kid who thought they could sit down and make a game in 2 hours as well. :p