Sep
25
2012

Marching Cubes (Javascript)

Converted this Marching Cubes(three.js/webgl) to Unity Javascript

– Added image#3 (using perlin noise to generate caves)
– Added profiler image, fatal error image, neighbour chunk connection problem..
– Changed default unity arrays to Lists(), much faster now
– Added some new images
– Now digging the ground possible with “mining laser” (its quite slow on higher resolution, because of the mesh collider updates..)

Webplayer:
http://unitycoder.com/upload/demos/MarchingCubesTerrainSculpting_unity/ (Mesh/Terrain Sculpting test..webplayer size 93kb..Oo )

Download source:
coming later.. (Current test version source is available for those who donate any amount – minimum 1usd – pls.)

Image#1 : First test, failed because of 1 typo & some variables were as ints.. hmm, would the int version be much faster?

Image#3: Using 3D Perlin Noise

Image#4: Bigger caves

Image#5: deep profiler (mesh collider update removed from here..it was too slow, now Perlin noise takes most of the time..)


Image#6: After flying inside the caves for a while got this fatal error.. (had forgotten new Mesh(); inside the loop..)

Image#7: Trying to connect neighbour chunks.. Some parts are not connected, some are. *Fixed: Had wrong world pos value for the next chunk

Image#8: Somehow need to fix the stretching textures.. now each face is UV(0,0|0,1|1,1)..

Image#9: Now walls/border holes are capped

Image#10: Now digging is possible (just some random “out of range” errors still..)

Image#11: Now you can sculpt the mesh (add/remove), make caves etc. (Need to add adjustable brush size and somehow save the mesh to editor?)

Image#12: with DX11 Tesselation shader

marching_cubes_cave_tesselated_dx11_shader_unity(click for fullsize)


15 Comments + Add Comment

  • Cool pictures. But UnityScript… seriously, why? You took actual Javascript code and re-wrote it in C#, except it’s got a couple quirks and .js file ending. Ever noticed there’s something “funny” with the “Unity Javascript”?

    • Well..its still nice 🙂

  • Hey this looks great! Ignore the Unityscript-haters. Are you going to be releasing code for this as I’d love to have a play around with it! 🙂

  • Forgot to check the box for notifications.

    D’oh!!

  • this is one of my favourite codes demonstrations I have seen in unit, I hope you plan to release it to the asset store or the wiki, ask the guy from the other website!. It’s actually textured!

    It reminds me of this-
    https://www.youtube.com/watch?v=vI99dVWg4jA
    metaballs

    • Thanks, that is useful!

  • Why after i download toolbar AND donate i still cant find the source? Im looking under sources there is no marching cubes.

    • Donation downloads are by request only. (i’ll send to your email this package then).

      Only those packages with “buy now” button are instant downloads.

  • Hey man, nice post, thank you
    Is it possible to use a DX11 tessellation method to have a nicer, smoother mesh with this or not? I mean, is this a triangle soup or the vertices are connected?

    One more thing, if the triangles are separate, is there a way to make them look smoother with re-calculating the mesh normals somehow?

    Do you think that a divide-and-conquer will work for generating a triangulated mesh rather than a triangle soup?

  • No idea really.. those are invidual triangles, so it doesnt seems to work (atleast with the default tess.phong shader)

    maybe there are solutions:
    https://developer.nvidia.com/sites/default/files/akamai/gamedev/files/gdc12/GDC12_DUDASH_MyTessellationHasCracks.pdf

  • Hello!

    Is there some source code or do you know a working voxel engine in the unity asset store that can be used properly?

    I mean, I want a 3D Array to store the world, similar to Minecraft but to be rendered with slopes (either marching cubes or dual contouring or similar) – so the world can be easily manipulated and stored on disk and i.e. also transferred over network – just a 3D array (could be compressed for transfer) and out of that array the world should build up again.

    Thanks in advance!

    • i have collected a list here, but most of them are not really updated..
      still i would try some of those free ones first. (or see if there are any popular ones in asset store)
      http://unitycoder.com/blog/2012/10/18/voxel-resources-engines-for-unity/

      loading/saving arrays would be easier part of that, not sure if all the engines use full 3D arrays though, more like split into chunks. (but you could save those chunks then)

  • Thanks for that list!

    Meanwhile I got that marching cubes source from Github (but with C# instead of Javascript): https://github.com/unitycoder/VoxelTerrain

    @Curious: Each Marching Cube creates its own vertices (and of course triangles) – so the triangles do NOT share the same vertices at the same position.

    Anyway – I found some code that smoothes the vertex normales, like they where shared between triangles:
    http://schemingdeveloper.com/2014/10/17/better-method-recalculate-normals-unity/

    But then again the landscape is divided in chunks – so the vertex normals do not smoothen the look between those chunks.

    I think, I will have to do some Voxel editor from ground up – to better understand it, so I can optimise the meshes, to share their vertices – then the regular mesh.recalculateNormals (); function from Unity would suffice.

    Then the normals will only need to be interpolated at the edges of the “chunks” (I will call that areas).

    Btw. I found a quite complex tutorial about marching squares (2D version of Marching Cubes) – I mean complex, because in the following pages it will show how to share vertices and even use some kind if “weighted” (at least I think so) Voxels that will allow smoother forms.

    I guess, if you do something like this in 2D, the process can then be transformed to 3D, so Marching Cubes:
    http://catlikecoding.com/unity/tutorials/marching-squares/

    • PS: What I mean about the NormalSolver: It is not the solution, just a workaround to see what it would like with interpolated Vertex Normals – also it is a bit of an overkill, because it uses an angle treshold that is compared with hash values, which is not really needed – just set the value to i.e. 120° or whatever you want. (it is supposed to smoothen a mesh with multiple materials that share the same vertices at its edges – what the unity’s built in method does not do)

      The real solution would be to reuse the vertices for neighbouring cubes, so at one position is just one unique vertex that is shared between the triangles. Only at the edges between chunks (witch are separated meshes) the normals also should be interpolated. I guess that could be done, when the edges in general are calculated.

      So I want to build something from the ground up …

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.