Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

PicaVoxel

Discussion in 'Assets and Asset Store' started by GarethIW, Feb 19, 2015.

  1. UnbreakableOne

    UnbreakableOne

    Joined:
    Nov 16, 2013
    Posts:
    168
    Hi,

    How can I fill an empty Volume from code? As your SHMUP sample, I've made one Volume in the editor, deleted all it's voxels and passed it to my script:


    Code (csharp):
    1.  
    2.        void Start ()
    3.        {
    4.             //v = gameObject.AddComponent<Volume>();
    5.             //v.AddFrame(0);
    6.          
    7.             //v.MeshingMode = MeshingMode.Culled; // Apparently it's better for pcg
    8.  
    9.             //v.Frames[0].Voxels = new Voxel[100];
    10.  
    11.             //volume.Frames[0].Voxels = new Voxel[X_TILES * Y_TILES * Z_TILES];
    12.             volume.Frames[0].Voxels = new Voxel[volume.XSize * volume.YSize * volume.ZSize];
    13.  
    14.             for (int x = 0; x < volume.XSize; x++)
    15.             {
    16.                 for (int y = 0; y < volume.YSize; y++)
    17.                 {
    18.                     for (int z = 0; z < volume.ZSize; z++)
    19.                     {
    20.                         volume.Frames[0].Voxels[x + y + z] = new Voxel()
    21.                         {
    22.                             State = VoxelState.Active,
    23.                             Color = Color.green,
    24.                             Value = 1
    25.                         };
    26.                     }
    27.                 }
    28.                 //volume.UpdateAllChunks();
    29.             }
    30.  
    31.  
    32.  
    33.             volume.UpdateAllChunks();
    34.  
    35.          
    36.        }
    37.  
    But when I run this, only a couple of voxels are generated in the Volume and if I put break points to examine, it crashes and Unity says a mesh can not have more than 65k vertices. I think it has something to do with chunks (as they are a mean to overcome this, IIRC) but can't find out how.
     
  2. GarethIW

    GarethIW

    Joined:
    Jun 24, 2014
    Posts:
    46
    Hi, I believe you may have emailed me about this over the weekend and I replied to it (apologies if not - I received an email with this exact same question).

    First, either have a PicaVoxel volume already in the scene to generate into, or create a prefab from a Volume which you would then instantiate.

    Once you have a volume ready, you can then make it the correct size and add your voxels to it. Here is all the code you need:

    https://gist.github.com/GarethIW/2740ee670dc7085aa926fbd6f354dfd9
     
  3. UnbreakableOne

    UnbreakableOne

    Joined:
    Nov 16, 2013
    Posts:
    168
    Thanks. Reading the gist.

    1) In the edit mode of a volume, if I remove all voxels from a mesh, is there anyway to add one?

    2) Also in the edit mode of a volume, does new voxels you want to draw/add should be attached to a previous one? Like can you put one in the sky?

    3) How can I detect collision on my voxels?

    4) I find a couple methods that you call when you change or create a volume namely "CreateChunks", UpdateAllChunks, UpdateAllChunksNextFrame, SaveForSerialize, among others. What are their differences and where should we call each of them?

    5) Should I be concerned with chunks when making volumes procedurally?

    6) In the SHMUP sample, why did you create an array of volumes? Performance? Why not a big volume instead of a 2D array of small volumes? I thought point of voxels is to overcome doing these.

    7) Why traversing the inner array of a volume as [x + y + z] won't work and I have to do it as you do: "x + voxelVolume.XSize * (y + voxelVolume.YSize * z)" ? I suspect it's for inside mechanism of chunks but am interested to know why.

    (I have a couple more questions actually but I think asking them in "chunks" is better!)
     
  4. GarethIW

    GarethIW

    Joined:
    Jun 24, 2014
    Posts:
    46
    1) In the edit mode of a volume, if I remove all voxels from a mesh, is there anyway to add one?
    2) Also in the edit mode of a volume, does new voxels you want to draw/add should be attached to a previous one? Like can you put one in the sky?

    Answering these two at the same time. The PicaVoxel editing tools were based on MagicaVoxel's tools. When I first started PicaVoxel, Magica was very rudimentary but it quickly became an incredibly powerful voxel art creation tool that I could never hope to keep up with in terms of functionality. PicaVoxel has always been more about what you do with the voxels than it is about creating them, so now I always recommend that users create their models using Magica or Qubicle and import them with PicaVoxel. You can then use the PV editing tools for last-minute touches or game-specific edits.

    To answer the questions: 1) No, but you can create a Base-Only volume which only has the "bottom" layer of voxels filled. 2) Yes, you add voxels by attaching them to existing ones as opposed to painting directly into a grid.

    3) How can I detect collision on my voxels?

    This is a big topic. Firstly, there is nothing special about objects created by PicaVoxel - they are standard Unity GameObjects. That means you can use box / sphere colliders to detect collisions with and between entire objects. You can use MeshColliders (which PicaVoxel can attach for you if you choose), although you need to watch out for performance especially if you are altering the voxels at runtime.

    What I imagine you're wondering though is how you can do per-voxel collision. I include a CollisionDetector script to help with this. You can see an example of using it in the Invaders demo (look at the Bullet script). You can do it manually. There's an example of that in the RPG demo on the RPGCharacterController script.

    Collision detection would be entirely dependent on the game you are making though. It is often best to use a mixture of Unity physics and per-voxel collision (raycasting/collision events for gaemobject collision, then a per-voxel check on the resulting volume).

    4) I find a couple methods that you call when you change or create a volume namely "CreateChunks", UpdateAllChunks, UpdateAllChunksNextFrame, SaveForSerialize, among others. What are their differences and where should we call each of them?

    • CreateChunks(): Splits a volume up into chunks and creates the hierarchy of gameobjects needed to contain the volume. This should only be called if you are changing the size of a volume at runtime.
    • UpdateChunks(): Updates *only* chunks that have been changed since the last time this method was called (usually every frame).
    • UpdateAllChunks(): Immediately generate meshes for all chunks on a volume. Should only be called at runtime if you have added/removed/recolored voxels across the entire volume.
    • UpdateAllChunksNextFrame(): The same as above, but threaded. Will rebuild all of the chunks of a volume over subsequent frames.
    • SaveForSerialize(): Not really to do with generation; this serializes the voxel data into a byte array so Unity can store it. Not used at runtime.

    5) Should I be concerned with chunks when making volumes procedurally?

    No. PicaVoxel takes care of chunking for you.

    6) In the SHMUP sample, why did you create an array of volumes? Performance? Why not a big volume instead of a 2D array of small volumes? I thought point of voxels is to overcome doing these.

    Because the Shmup sample is essentially a tile engine as well as a procedural generation demo. The playfield is split up into tiles (one volume per tile) like a 2D tile engine, and when the tiles disappear off of the left of the playfield, they are moved to the right (all off-screen), and then the landscape is generated into those tiles by copying from a source volume (which acts as a tilesheet of sorts), and also by procedurally generating the cliffs at the top and bottom of the screen.

    Performance-wise, it is faster to generate smaller meshes. So breaking up the entire playfield into tiled volumes also helps in that way.

    7) Why traversing the inner array of a volume as [x + y + z] won't work and I have to do it as you do: "x + voxelVolume.XSize * (y + voxelVolume.YSize * z)" ? I suspect it's for inside mechanism of chunks but am interested to know why.

    A 3D array would normally be indexed as [x,y,z]. While syntactically nice, 3D arrays are (performace-wise) slower to access than 1D arrays. So we take the 3D array and lay it out into a 1D array. The algorithm above is how you address the 3D space contained in the 1D array. Simple.
     
  5. Icezman

    Icezman

    Joined:
    Mar 31, 2017
    Posts:
    7
    Hello!
    I've recently bought the PicaVoxel pack!
    Really great and i'm eagerly getting to know how it works!
    1 issue though! When playing around and when it collides or brakes blocks, almost always it suddenly turns off the lights? Is that only in the editor that it happens and is it normal.?
     
  6. Atomhax

    Atomhax

    Joined:
    Dec 31, 2014
    Posts:
    2
    Old thread but I'm really loving the Demos you included in this asset. I purchased this a few years ago on a whim and recently redownloaded it.
     
  7. GarethIW

    GarethIW

    Joined:
    Jun 24, 2014
    Posts:
    46
    After five fantastic years on the Asset Store and with great sadness, I've decided to deprecate PicaVoxel.

    PicaVoxel always included full source code, so if you've purchased any of the iterations of PicaVoxel over the years, you will still be able to download the asset and update/improve it yourself!

    Due to life commitments, I can no longer keep up with the cadence of updates on the Unity Editor. I recently discovered that my support for the new Prefab workflow (in 2019.1) isn't quite where it needs to be and probably needs a fair bit of work to get it there. I can no longer spare the kind of time needed. And quite frankly, I haven't used the new prefab workflow enough to even understand how it should work!

    There are also a lot of changes coming to unity (SRP, DOTS, ECS, completely new editor) that mean that PicaVoxel needs to become something else entirely really.

    It's been an absolute blast helping you folks make awesome games with PicaVoxel.

    I'm also considering making PicaVoxel available to all as open-source on a Github repo. I won't have the time to manage this myself, but if you're interested in managing an open-source version of PicaVoxel going forward please let me know (support@picavoxel.com)
     
    Last edited: Feb 11, 2020
  8. StevenPicard

    StevenPicard

    Joined:
    Mar 7, 2016
    Posts:
    668
    Making the source code available on GitHub would be great. I'm sure many people could make great use of it.
     
unityunity