Search Unity

Introducing Voxelform - voxel terrain (Marching Cubes) with real-time deformation.

Discussion in 'Assets and Asset Store' started by Mark-Davis, Jun 24, 2011.

  1. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    Yes. It's been sitting on the back burner for too long. I'm going to revisit it with a 1st gen iPad and an iPhone 4s, and it'll be the next thing I do, ahead of any spring physics liquid simulation. If I have enough time in my schedule, I'm expecting to be able to do a 2.1 release in the next week or two with some cool new features.
     
  2. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    So, about the new features... I have some good news.

    After a few small modifications, the tech behind getting both infinite terrain and much larger terrains to work is finally here. The performance seems good, but there's still some work to do in making sure it's integrated in a way that keeps everything as flexible and general purpose as possible. In order to customize this, you'll be able to simply plug in a custom voxel data source that generates or pages the voxels... or optionally, you can just create larger terrains in memory by storing more voxels and fewer chunks.

    The other new feature, cubical voxels mixed in with organic voxels is working nicely but still has room for improvement. Once both of those features are ready to go, I'll build out some more demos and make a v2.1 release, free of course for anyone who's bought an earlier version.

    And as was mentioned in the previous post, mobile will be revisited next. If it works out, mobile support will probably be the only new feature for v2.2. No time frame on that yet, but it could be a few days to a few weeks of development and testing.
     
  3. winterkewl

    winterkewl

    Joined:
    Oct 28, 2011
    Posts:
    53
    Both of those features are going to be amazing I can't wait to get my hands on it!
     
  4. Ambrosios

    Ambrosios

    Joined:
    Oct 22, 2011
    Posts:
    9
    Same here, infinite terrains would be awesome !

    I bought Voxelform this week and I have little issues about alteration. I want to make different kind of shapes in order to make different tools for my character but it doesn't react like I want.

    Here is what I have :



    It doesn't alter where I want to and it doesn't alter like my sphere shape. Moreover, I have to click like 10 times before obtaining this result. I use the AlterVoxelSphere function (diggingPower is 1.5 and VoxrayPower is 2).

    Do you have a suggestion on what I could change for resolving this problem ?
     
  5. unity_sg

    unity_sg

    Joined:
    Sep 14, 2010
    Posts:
    95

    Infinite terrain and cubical voxels, two very nice features ! I'm curious to try it :)
    The video in the voxel editor seems to be very interesting too !
    Really nice work Mark
     
  6. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    Thanks so much! Here's some code that should point you in the right direction. You'll need to alter it of course to fit your situation. I'm guessing that you current issue has to do with a radius that is too small and a position that is top left. Once you get the size and position correct, playing around with the brush density... maybe it should be volume... should get you where you want to be... if not, please let me know and I'll work with you some more on this. Thanks!

    Code (csharp):
    1. _cursor.transform.localScale = new Vector3(_sphereBrushSize, _sphereBrushSize, _sphereBrushSize) * _terrain._scale * 2f;
    2.  
    3. Vector3 p = (_cursor.transform.position / _terrain._scale) - (_terrain.transform.localPosition / _terrain._scale);
    4.  
    5. p.x += .5f;
    6. p.y += .5f;
    7. p.z += .5f;
    8.  
    9. _terrain.AlterVoxelSphere(p,
    10.     _sphereBrushSize,
    11.     _brushDensity,
    12.     _brushMaterialIndex,
    13.     _eraserEnabled ? VFFabOptions.EnableVolumeSubtraction : VFFabOptions.None);
     
  7. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    @seb7000 - Thanks so much again. Should be a lot of fun. Quick disclaimer..."infinite" isn't technically correct, but the resulting worlds are enormous.
     
  8. winterkewl

    winterkewl

    Joined:
    Oct 28, 2011
    Posts:
    53
    Enormous is good! Quit teasing, when do you think we can try it? :)
     
  9. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    Pretty soon! :) Here's a video demonstration of the "infinite" terrain feature currently under development. What's happening here is that a small terrain is generated and then wrapped "infinitely" in all directions. This is accomplished by using a custom data source that converts x,y,z to x % width, y, z % depth... there's a bit more to it than that, but that's the basic idea. The data source could instead generate fractal based terrain on the fly or load it in from storage, but wrapping was the easiest thing for a proof of concept.

     
  10. winterkewl

    winterkewl

    Joined:
    Oct 28, 2011
    Posts:
    53
    amazing job man! That's really exciting to see!
     
  11. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    Voxelform just made the news today over at PC Gamer Magazine! :)

    They put up a link to the YouTube video I posted late last night demonstrating "infinite" terrain.
    http://www.pcgamer.com/2012/02/09/and-in-other-pc-gaming-news-219/

    "Unity can do voxels now, skip to one minute in to see the deformable terrain."
    - PC Gamer

    That makes two major rags so far; the other mention appeared in a July 1st Edge Magazine interview with Unity Technologies CEO, David Helgason.
     
    Last edited: Feb 9, 2012
  12. winterkewl

    winterkewl

    Joined:
    Oct 28, 2011
    Posts:
    53
    sweet man well deserved! grats. :)
     
  13. theprojectabot

    theprojectabot

    Joined:
    Nov 11, 2011
    Posts:
    38
    awesome! excited to see the data source implementation for study purposes!
     
    Last edited: Feb 10, 2012
  14. unity_sg

    unity_sg

    Joined:
    Sep 14, 2010
    Posts:
    95
    hello mark,do you maybe have a simple example of how to manage the alterVoxel function in the case of a cube that can be rotated?
    Should I manage multiple collision points and each one launch alterVoxel function ?
    Or is there the possibility to manage this simply ?

    I think a example like this could be usefull for the community.
    This could expand the possible use of the alterVoxel function to something other than a mouse click
     
  15. vesuvias

    vesuvias

    Joined:
    Dec 14, 2009
    Posts:
    31
    Excellent,

    I just bought this asset and I am very excited to see what I can do with it. I have a couple of questions that hopefully you can answer that will save me some time.

    Does your marching Cubes implementation mean that cubic regions must have rounded edges? Looking at the algorithm it does seem like this is an absolute side effect. Any ideas on how to get around this if we wanted a hard edge?

    Are you using octrees for data storage or something similar to compress/store the cubic/voxel data? Do you have any polygon reduction implemented (tessilation)? For instance if I have a flat 4x4 vertical cube wall instead of using 16 quads to render do you do any optimization magic to make it render with one quad?

    Have you implemented any sort of lighting scheme built into the shaders? Meaning when under ground will the surface appear pitch black without a local light source? How difficult would it be to for us to add support for some sort of flood-like lighting (where light bleeds in through the cracks on a voxel basis and modifys the shader lightmaps accordingly)?

    That's all for now. I need to start playing with/ modfiying what you have here. Then the fun really begins :).

    Ves
     
  16. ttank7

    ttank7

    Joined:
    Apr 18, 2011
    Posts:
    42
    Love the infinite terrain, I can't wait. This just keeps getting better. :)
     
  17. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    @winterkewl, benjbrown - Thanks!
     
  18. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    There isn't currently... but I agree that it would be helpful... the ultimate way to go of course would be to provide arbitrary mesh voxelization. Then you could use anything as a brush or eraser, and at any scale. I'll implement something like this just as soon as I have the time. :)
     
  19. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    Thanks so much! The Marching Cubes implementation won't currently allow for truly cubical cubes... however, they should be there in v2.1 which contains a tweak that allows for them.

    I haven't used octrees (or other more advanced spacial structures) thus far due to efficiency concerns. However, this might change very soon (in v2.1) due to the requirements of "infinite" terrain mode where most data is generated on the fly and only change data is stored.

    The only polygon reduction currently done is the culling of geometry in inner welds between different materials. I don't currently have any plans to go further than that at the moment, but certainly it could happen, perhaps for the new cubical voxel support.

    I'm definitely planning to implement some type of lighting as you've described it. This may not make v2.1, but it's certainly on the todo list for much desired work items. If you wanted to do this yourself, I'd recommend sending the light in via vertex colors. The shaders could be modified quite easily to deal with that. Currently the easy way to make caves dark is to turn on shadows and crank them up. Then turn ambient lighting down.

    Hope that helps, and thanks again! :)
     
  20. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    Also - a bit of an update on v2.1. Things are progressing nicely with the "infinite" terrain feature. Currently, fractals are being generated continuously and with decent performance. Change data needs more work, but I've started on that. It's certainly taking a lot longer than expected to get this right, and as usual, a bit more time for a general purpose implementation. So what seemed like a few days may actually be a few weeks, but I don't plan on adding any other features to this release other than the cubical voxels. So when those two items are fully ready, it'll ship. Thanks again everyone for all your support! I really appreciate it, and I'll continue to try to make Voxelform the best it can be.
     
  21. unity_sg

    unity_sg

    Joined:
    Sep 14, 2010
    Posts:
    95
    Thanks Mark, that's sure, mesh voxelization could really be a usefull features to expand Voxelform !
    I will wait for this ;)
     
  22. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    More fun with infinite terrain. :)


    Please note this is a preview of v2.1 and not the actual shipping product.
     
  23. winterkewl

    winterkewl

    Joined:
    Oct 28, 2011
    Posts:
    53
    Drool. . . . . Nice work! Really amazing stuff!
     
  24. vesuvias

    vesuvias

    Joined:
    Dec 14, 2009
    Posts:
    31
    Wonderful,

    I am curious as to how you are solving the cubic edge issue. The only way I can think of doing this is flagging the voxel in some way and when geometry is requested it returns a harder edges for the voxel in certain cases. This would of increase polycount, for instance instead if we are getting the tri's for a corner (the case where only one of 1 of 8 voxels are active) instead of a single triangle we would need to render 3 quads to get an accurate hard corner. This is troublesome but even still unless the world was filled with steps I think it would still be performent. It also might create interesting issues the multi-material submeshes lining up. Just let us know what the game implications are (ie as game designers do we need to throttle how often these cubic edges can be created, etc).

    I can only advise strongly for octrees. Saving changes to terrains that can be infinite (or rather ridiculously large) presents the problem of entirely too much data. You simply cannot store that much data in memory or even on disk after a certain point. With these worlds there is a great deal of space that is exactly like its neighbor, octrees take advantage of that. The downside being that accessing data via an octree search is slower than a straight up 3d index (but not that much, its comparable to a hash lookup). But it's also more powerful. An interesting side effect is that if you replaced your 3d index data type with an octree you "could" render the data at a lower voxel resolution on request (think LOD for far out chunks). You would need to design the tree so that is knew how to build a single voxel from child voxels but it would work. In any case, you know what your doing but I just wanted to submit my request anyway :).

    Good to know your thinking about lighting. Keep up the good work, I am excited about your roadmap. I can't wait for v2.1.

    Ves
     
  25. b3y0nd3r

    b3y0nd3r

    Joined:
    Oct 5, 2010
    Posts:
    27
    That 2.1 Video looks brilliant. I'm happy this was my first ever purchased Asset ;)
     
  26. SevenBits

    SevenBits

    Joined:
    Dec 26, 2011
    Posts:
    1,953
    I have a question. What if I want to create a cave for my in-game world, but don't want the player to be able to edit it? Also, is the generated voxel compatible with Unity terrain and is it the same every time you run it, or is it different?

    I can see this tool being used to create caves and other similar things in the game world without needing a mesh for the cave.
     
  27. RichardJaquish

    RichardJaquish

    Joined:
    Sep 30, 2011
    Posts:
    5
    I can only throw in my complete agreement with vesuvias, the biggest addition you could make to this completely awesome technology would be a octree data structure and a complimenting octree LOD structure.

    The main problem with the tech at the moment is its brute force aproach to its geometry. All of the terrain has the same amount of detail, regardless of the position of the viewer. Since we only have a set polygon budget before performance degrades, this results in a smaller number of terrain chunks that can be rendered at once. The results of this can be seen in your video above. As you walk through the caves you can see the skymap off in the distance, but as you approach it, more ground suddenly pops into place.

    The solution to this problem has already been solved in the traditional space of terrain rendering. The object is to render detail as a function of distance in regards to the viewer / camera. This way you can cut detail in the distance, and use those savings to render more terrain chunks.

    I am sure you are well aware of most of this, but just for clarity, here is a decent paper that shows how it works
    http://www.vertexasylum.com/downloads/cdlod/cdlod_latest.pdf

    Since your implementation uses voxels, the sparse voxel octree is a shoe-in for an optimal data structure, in addition to lending itself perfectly to an octree LOD system.

    The benefits are huge! No only would you be able to see further (you could have your entire world encapsulated in one solid terrain piece), but you could add even more detail closer to the viewer, for a much higher level of detail then is currently present.

    It would be awesome to be able to work off of one terrain chunk, initialized through a customizable/programmable algorithm, and subdivide it down through octrees to add specific details! This would also allow easy terrain sculpting at multiple levels.

    Not an easy task, but I would love to see the project head in this direction!
     
  28. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    Hi Vesuvias - I'm going to hold off on answering this until the next version is ready to ship... both problems have been solved and performance is pretty nice, but I want everything to be as good as it can be before making a release. I had initially expected to release this sooner, but given the time required for other projects that I'm involved in at the moment, I'm going to guess that the next release will happen next month at the earliest. Thanks!
     
  29. ttank7

    ttank7

    Joined:
    Apr 18, 2011
    Posts:
    42
    An Octree with LOD mixed with this would be amazing. I currently find that the terrain takes up alot of FPS for rendering so it would be nice if this was implemented. Keep up the great work, the newest video is really nice! :)
     
  30. theprojectabot

    theprojectabot

    Joined:
    Nov 11, 2011
    Posts:
    38
    Mark,
    I have been messing around with the GenerateVoxels method in the attempt to create a spherically generated voxel terrain, however it always is coming out still in the big box size even though I am checking the px, py,pz values so that they match my function for having volume 0 or not 0 depending on their distance from the calculated center of the voxel terrain object. Have any ideas? I feel that perhaps my checking of the px py pz is voxel space not world space? I dont know Ill keep digging but Id love an explanation for this if you got one.
     
  31. runonthespot

    runonthespot

    Joined:
    Sep 29, 2010
    Posts:
    305
    @benjbrown I had no trouble making spheres, although I'd advise not going discretely 0 or 1 if you want a nice smooth sphere. One bug that I had initially was forgetting to cast px/py/pz to float before calculating the distance from the centre point. If that doesn't fix it, post your code and I'll help you debug :)

    Here's a clip of my efforts using voxelform to do spheres:
    http://www.youtube.com/watch?v=anP9yZ1dofo
     
  32. Mau

    Mau

    Joined:
    Jan 30, 2012
    Posts:
    21
    Hey there, this looks absolutely amazing. Old ideas that may have been dismissed due to technical restrictions can now go live again! :D
    One thing that I still haven't seen is foliage support. Is there a way to enrich that voxel terrain with plants, trees, whatever in a suitable manner (like painting them like in the terrain editor)?

    Thanks for this great addition
     
  33. runonthespot

    runonthespot

    Joined:
    Sep 29, 2010
    Posts:
    305
    There's been a lot of discussion: One option if you're using VF to generate static terrain geometry (which is still worthwhile, as it is excellent for caves or otherwise complex terrain with "holes" is to export to OBJ (+maybe reduce polys etc in meshlab or similar) and reimport into Unity, to use as a normal 3d model (albeit, utilitising the triplanar textures).
     
  34. TouchSoft

    TouchSoft

    Joined:
    Oct 18, 2008
    Posts:
    218
    Man.. if this ever gets working on mobile.. it will be GREAT!
     
  35. TouchSoft

    TouchSoft

    Joined:
    Oct 18, 2008
    Posts:
    218
    I just thought of something... can a normal sized terrain be created using Voxelform and then converted into a mesh?

    If I can do that, then I could still use this to create terrains for mobile.
     
  36. theprojectabot

    theprojectabot

    Joined:
    Nov 11, 2011
    Posts:
    38
    nice work @runonthespot... I will post it when I get some time tonight.
     
  37. theprojectabot

    theprojectabot

    Joined:
    Nov 11, 2011
    Posts:
    38
    it does work for mobile.. just ran my demo scene on iOS (slow) but it runs... also you can bake terrain out using the OBJc exporter script... check up through this thread, Mark of Voxelform talks about it.
     
  38. theprojectabot

    theprojectabot

    Joined:
    Nov 11, 2011
    Posts:
    38
    @runonthespot: here is a little code snippet

    makes an aweful looking sphere :) how did you make yours so smooth. I get plataueing...

    ....
    public float VoxelsForSphere(float x, float y, float z, float radius)
    {
    return x*x + y*y + z*z - radius*radius;
    }

    ....




    if(VoxelsForSphere(px-nvx/2,py-nvy/2,pz-nvz/2,radius) <=0)
    {
    v.Volume = v.Volume * (float)Random.Range(0.5f,1.0f);
    if (v.Volume < _isolevel * .75f) v.Volume = 0f;
    //if(v.Volume == 1)
    //v.Volume = v.Volume *.75f;
    _voxels.Write(px,py,pz,v);
    }else{
    v.Volume = 0f;
    _voxels.Write(px,py,pz,v);
    }
     
  39. vesuvias

    vesuvias

    Joined:
    Dec 14, 2009
    Posts:
    31

    Of course you do. You randomize the volume. For a sphere the volume should falloff like a gradient originating from the center.

    You need to set it up so that at the center the volume is 1.0 and at the edges of the sphere you have a volume == IsoLevel. So simply, for each voxel pass in the spheres origin then get the distance from your current voxel to that origin. Divide that distance by the radius and lerp from 0.5 to 1.0 and you should have a perfectly smooth sphere.

    To get the volume for each voxel It will look something like this (note I haven't tested this):

    Code (csharp):
    1.  
    2. float GetVoxelVolumeForSphere(Vector3 VoxelPosition, Vector 3 SphereOrigin, float sphereRadius, float IsoLevel)
    3. {
    4.     float a = ((VoxelPosition - SphereOrigin).sqrMagnitude)/(sphereRadius*sphereRadius);
    5.     float volume = Mathf.Lerp(1.0f,IsoLevel,a);
    6.     return Mathf.Clamp01(volume);
    7. }
    8.  
    Vesuvias

    Edit: I may have inverted that Lerp so it might be Mathf.Lerp(1.0f,IsoLevel,a) instead.
    Edit2: yes definatly inverted the lerp its correct now with Mathf.Lerp(1.0f,IsoLevel,a) the other way would have given a solid block I think
     
    Last edited: Feb 27, 2012
  40. theprojectabot

    theprojectabot

    Joined:
    Nov 11, 2011
    Posts:
    38
    Vesuvias thanks!
     
  41. theprojectabot

    theprojectabot

    Joined:
    Nov 11, 2011
    Posts:
    38
    Vesuviuas, thanks for the reply.

    Im still kind of grasping whats going on here ;)
    So I generate a series of voxels through my nested for loops, the volume of each voxel should be 1 until a voxel boarders on the edge of my radius... which at that point the volume will begin to diminish...

    I have taken your code and im still getting a terraced view of my sphere.
    Code (csharp):
    1.    
    2.     float GetVoxelVolumeForSphere(Vector3 VoxelPosition, Vector3 SphereOrigin, float sphereRadius, float IsoLevel)
    3.     {
    4.    
    5.         float a = ((VoxelPosition - SphereOrigin).sqrMagnitude)/(sphereRadius*sphereRadius);
    6.    
    7.         float volume = Mathf.Lerp(1.0f,IsoLevel,a);
    8.    
    9.         return Mathf.Clamp01(volume);
    10.    
    11.     }
    12.    
    13. ...
    14.  
    15.     public float VoxelsForSphere(float x, float y, float z, float radius)
    16.     {
    17.         return x*x + y*y + z*z - radius*radius;
    18.     }
    19.  
    20. ...
    21.  
    22. public void BBGenerateVoxels(System.Func<Vector3, VFVoxel> noiseFunc, int groundPlaneHeight, ushort groundType)
    23.     {
    24.         int nvx = (_xChunkCount * _numVoxelsPerXAxis) - 1;
    25.         int nvy = (_yChunkCount * _numVoxelsPerYAxis) - 1;
    26.         int nvz = (_zChunkCount * _numVoxelsPerZAxis) - 1;
    27.        
    28.        
    29.         VFImprovedPerlinNoise.Initialize();
    30.         VFSimplexNoise.Initialize();
    31.        
    32.         float radius = nvx/4;
    33.        
    34.         Vector3 sphereOrigin = new Vector3(nvx/2.0f,nvy/2.0f,nvz/2.0f);
    35.        
    36.        
    37.        
    38.         for (int px = 1; px < nvx; ++px)
    39.         {
    40.             for (int py = 1; py < nvy; ++py)
    41.             {
    42.                 for (int pz = 1; pz < nvz; ++pz)
    43.                 {
    44.                     VFVoxel v = noiseFunc(new Vector3(px, py, pz));
    45.                    
    46.                     // Just in case... prevents a crash when materials go out of bounds.
    47.                     // This is mostly here for when you're experimenting with voxel generation functions.
    48.                     // If you feel that this should actually throw an error, feel free to make it do so.
    49.                     if (v.Type >= Materials.Length) v.Type = (ushort)(Materials.Length - 1);
    50.                    
    51.                     // This helps prevent unwanted distortion.
    52.                     // If the condition is set to (v.Volume < _isolevel), then some edges may lose their curved form.
    53.                     // If this line is removed altogether, form may be improved, but unwanted distortion may occur
    54.                     // when using AlterVoxel.
    55.                     //if (v.Volume < _isolevel * .75f) v.Volume = 0f;
    56.                    
    57.                    
    58.                     if(VoxelsForSphere(px-nvx/2,py-nvy/2,pz-nvz/2,radius) <= 0)
    59.                     {
    60.                    
    61.                         v.Volume = GetVoxelVolumeForSphere(new Vector3(px, py, pz), sphereOrigin, radius, _isolevel);
    62.                        
    63.                         _voxels.Write(px,py,pz,v);
    64.                     }
    65.                    
    66.                 }
    67.             }
    68.         }
    69.  
     
    Last edited: Feb 27, 2012
  42. vesuvias

    vesuvias

    Joined:
    Dec 14, 2009
    Posts:
    31
    Hmm not sure, try eliminating the check for if the voxel is in the sphere. Even if a Voxel is outside of the sphere its volume will effect the sphere shape (this is a hard concept to grasp). So just let the voxels that are outside of the sphere still get thier volume from the GetVoxelVolumeForSphere. They will be eliminated by the IsoLevel check since they will have volumes under IsoLevel when the chunk gets its triangles. Comment out the noise function of use the Empty one or it will just muddy up your area.

    Code (csharp):
    1.  
    2.     float GetVoxelVolumeForSphere(Vector3 VoxelPosition, Vector3 SphereOrigin, float sphereRadius, float IsoLevel)
    3.     {
    4.         float a = ((VoxelPosition - SphereOrigin).sqrMagnitude)/(sphereRadius*sphereRadius);
    5.         float volume = Mathf.Lerp(1.0f,IsoLevel,a);
    6.         return Mathf.Clamp01(volume);
    7.     }
    8.     public float VoxelsForSphere(float x, float y, float z, float radius)
    9.  
    10.     {
    11.  
    12.         return x*x + y*y + z*z - radius*radius;
    13.  
    14.     }
    15.  
    16.  public void BBGenerateVoxels(System.Func<Vector3, VFVoxel> noiseFunc, int groundPlaneHeight, ushort groundType)
    17.     {
    18.         int nvx = (_xChunkCount * _numVoxelsPerXAxis) - 1;
    19.         int nvy = (_yChunkCount * _numVoxelsPerYAxis) - 1;
    20.         int nvz = (_zChunkCount * _numVoxelsPerZAxis) - 1;
    21.  
    22.         VFImprovedPerlinNoise.Initialize();
    23.  
    24.         VFSimplexNoise.Initialize();      
    25.         float radius = nvx/4;        
    26.         Vector3 sphereOrigin = new Vector3(nvx/2.0f,nvy/2.0f,nvz/2.0f);            
    27.         for (int px = 1; px < nvx; ++px)
    28.         {
    29.             for (int py = 1; py < nvy; ++py)
    30.             {
    31.                 for (int pz = 1; pz < nvz; ++pz)
    32.                 {
    33.  
    34.                     //VFVoxel v = noiseFunc(new Vector3(px, py, pz));
    35.                     // Just in case... prevents a crash when materials go out of bounds.
    36.                     // This is mostly here for when you're experimenting with voxel generation functions.
    37.                     // If you feel that this should actually throw an error, feel free to make it do so.
    38.                     if (v.Type >= Materials.Length) v.Type = (ushort)(Materials.Length - 1);
    39.  
    40.                     // This helps prevent unwanted distortion.
    41.                     // If the condition is set to (v.Volume < _isolevel), then some edges may lose their curved form.
    42.                     // If this line is removed altogether, form may be improved, but unwanted distortion may occur
    43.                     // when using AlterVoxel.
    44.                     //if (v.Volume < _isolevel * .75f) v.Volume = 0f;
    45.  
    46.                    
    47.                    v.Volume = GetVoxelVolumeForSphere(new Vector3(px, py, pz), sphereOrigin, radius, _isolevel);
    48.                     _voxels.Write(px,py,pz,v);  
    49.                     }                    
    50.                 }
    51.             }
    52.         }
    53.  
     
  43. theprojectabot

    theprojectabot

    Joined:
    Nov 11, 2011
    Posts:
    38

    Yeah with those changes it just comes out as a huge cube.

    aka i got rid of my sphere calcuation and just used your GetVoxelVolumeForSphere and i get cube...

    Code (csharp):
    1.  
    2. public void BBGenerateVoxels(System.Func<Vector3, VFVoxel> noiseFunc, int groundPlaneHeight, ushort groundType)
    3.     {
    4.         int nvx = (_xChunkCount * _numVoxelsPerXAxis) - 1;
    5.         int nvy = (_yChunkCount * _numVoxelsPerYAxis) - 1;
    6.         int nvz = (_zChunkCount * _numVoxelsPerZAxis) - 1;
    7.        
    8.        
    9.         VFImprovedPerlinNoise.Initialize();
    10.         VFSimplexNoise.Initialize();
    11.        
    12.         float radius = nvx/2f;
    13.        
    14.         Vector3 sphereOrigin = new Vector3(0,0,0);
    15.        
    16.        
    17.        
    18.         for (int px = 1; px < nvx; ++px)
    19.         {
    20.             for (int py = 1; py < nvy; ++py)
    21.             {
    22.                 for (int pz = 1; pz < nvz; ++pz)
    23.                 {
    24.                     //VFVoxel v = noiseFunc(new Vector3(px, py, pz));
    25.                     VFVoxel v = new VFVoxel(GetVoxelVolumeForSphere(new Vector3(px, py, pz), sphereOrigin, radius, _isolevel));
    26.                    
    27.                     // Just in case... prevents a crash when materials go out of bounds.
    28.                     // This is mostly here for when you're experimenting with voxel generation functions.
    29.                     // If you feel that this should actually throw an error, feel free to make it do so.
    30.                     if (v.Type >= Materials.Length) v.Type = (ushort)(Materials.Length - 1);
    31.                    
    32.                     // This helps prevent unwanted distortion.
    33.                     // If the condition is set to (v.Volume < _isolevel), then some edges may lose their curved form.
    34.                     // If this line is removed altogether, form may be improved, but unwanted distortion may occur
    35.                     // when using AlterVoxel.
    36.                     //if (v.Volume < _isolevel * .75f) v.Volume = 0f;
    37.                    
    38.                    
    39.                    
    40.                     _voxels.Write(px,py,pz,v);
    41.                 }
    42.             }
    43.         }
    44.        
    45.         // Provides solid ground.
    46.         /*
    47.         if (groundPlaneHeight > 0)
    48.         {
    49.             for (int px = 1; px <= nvx; ++px)
    50.             {
    51.                 for (int pz = 1; pz <= nvz; ++pz)
    52.                 {
    53.                     for (int py = 1; py < groundPlaneHeight; ++py)
    54.                     {
    55.                         _voxels.Write(px, py, pz, new VFVoxel(0.8f, groundType));
    56.                     }
    57.                 }
    58.             }
    59.         }
    60.         */
    61.  
    62.         _voxels.Flush();
    63.        
    64.     }
    65.  
    thanks for your help :)
     
  44. runonthespot

    runonthespot

    Joined:
    Sep 29, 2010
    Posts:
    305
    I think you may be overcomplicating it. I started with the absolute simplest implementation I could do, and then generalized it for spheres anywhere in the space.

    My guess was that density that is basically 1 - Distance between px,py,pz and origin would yield more-or-less a sphere and was pleasantly surprised when it produced a perfect one.

    This code assumes it is the only sphere in the entire volume, but as I said, it can be generalized for a different origin.

    Something like:

    Code (csharp):
    1. public static Voxel Sphere(Vector3 p, float radius)
    2. {
    3.     float dist = Vector3.Distance(new Vector3(radius,radius,radius),p)/radius;
    4.     float density = Mathf.Clamp01((1f-dist));
    5.     return new Voxel(density);         
    6. }
    I don't think this was my final version of this code, since I think this generates a sphere roughly half the volume of the original voxel cube, but don't have access to all my source or Unity from work. See if that works for you- if not, will take a look tonight to see if I can find the latest.

    As Vesuvias noted, you need to let the algorithm provide the density for voxels that are technically "outside" the sphere... i.e. definitely don't cut-off densities above or below a range, as they all matter. When you look at those lovely fractal landscapes, be aware that the underlying voxel density has values for what will ultimately be "air" when marching cubes takes over. Think "anti aliasing" with pixels. This is also why the threshold for the surface is usually lower than "1.0"

    At least, that's my complete layman's understanding of it so far.
     
  45. runonthespot

    runonthespot

    Joined:
    Sep 29, 2010
    Posts:
    305
  46. vesuvias

    vesuvias

    Joined:
    Dec 14, 2009
    Posts:
    31
    I think I know the problem. Its:
    Code (csharp):
    1.  
    2. float volume = Mathf.Lerp(1.0f,IsoLevel,a);
    3.  
    We are basically only setting the value between 1 and IsoLevel so when a is above 1.0 then volume is IsoLevel.. So basically your whole space is a block.

    Code (csharp):
    1.  
    2. float volume = Mathf.Lerp(1.0f,0f,a);
    3.  
    This should fix it.

    Vesuvias

    Spectacular sphere Runonthespot, your implementation is dead on.
     
  47. theprojectabot

    theprojectabot

    Joined:
    Nov 11, 2011
    Posts:
    38
    thanks both of you this is super helpful for the me brain.
     
  48. Mark-Davis

    Mark-Davis

    Joined:
    Jun 21, 2011
    Posts:
    156
    Hi all - back after a few weeks away, and have a new "infinite terrain" preview demo ready to go for anyone who'd like to give it a try. Bear in mind that this is a preview and not the final product by any means. Please be aware that features seen here may or may not make it to the final products. It requires at least a mid range core gamer GPU. Thanks!

    "Infinite Terrain" Demo

    Here are a couple screen shots of this demo in a previous build:









     
    Last edited: Mar 14, 2012
  49. KRGraphics

    KRGraphics

    Joined:
    Jan 5, 2010
    Posts:
    4,466
    HOLY S***!! This would be PERFECT to rebuild my Atlantis level
     
  50. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,502
    Looking mighty fine, I drool with anticipation :)