Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by dacudo · Mar 08, 2013 at 12:11 PM · c#implementation

How to implement marching cubes in Unity with C#

Hi

I am wanting to create a game with similar use of marching cubes as this game for my final year project in university. The differences being mine will be a horror game, you cannot change the distance of the 'terrain-editing box' from the player and it will be played from a first-person viewpoint.

So far, I have translated the C Source code of Paul Bourke's tutorial to C#. I'm not sure how to actually use this code to create something like the game I linked to. I think I will need to create a grid and a custom, modifiable mesh. I don't know how to create the grid. I have seen this for how to create a mesh, but I'm not sure how to use marching cubes to form and change this mesh.

I have seen that there are many other questions about marching cubes but none are the same as what I am asking, as far as I am aware. I have seen this question and it has a lot of source code (more further down the page) but I can't get my head around it.

If anyone could just give me a list of the steps I would need to take to move from the Paul Bourke source code to the linked game, that would be great. Any code or tips would also be a great help.

Thank you so much for any help with this,

Daniel

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Mar 08, 2013 at 02:58 PM 1
Share

alucardj, thats not the same kind of marching cubes implementation. The one Paul Bourke presents interpolates the edges to create smooth hulls while the one I posted in that answer is just "$$anonymous$$ecrafty" blocky voxels.

avatar image dacudo · Mar 08, 2013 at 03:00 PM 0
Share

Yes, Statement is correct but thank you for contributing, alucardj.

avatar image AlucardJay · Mar 08, 2013 at 03:12 PM 0
Share

Based on Paul Bourke's, possible point of reference with the information in Statements answer :

  • http://wiki.unity3d.com/index.php?title=$$anonymous$$etaBalls

  • more theory : http://www.exaflop.org/docs/marchcubes/

Edit : no worries, I shall go away now =]

avatar image dacudo · Mar 08, 2013 at 03:19 PM 0
Share

I have seen metaballs before but I am not sure how to adapt it to function like the game I linked to in my question. Thanks again!

avatar image Statement · Mar 08, 2013 at 03:25 PM 1
Share

Haha @alucardj, please stay :)

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Statement · Mar 08, 2013 at 03:08 PM

I am not sure I know what you mean by "a grid". I'll assume you mean a chunk, which means that you kind of slice the space up in boxes. These boxes contains typically one mesh, the voxel datas generated triangles for that particular chunk.

So your input is some scalar field, the voxel input. You probably have a function (or consider making one) to generate triangles for a given box in space. That is like saying, "from 0, 0, 0 to 16, 16, 16, give me all of the triangles for this area". These triangles are what goes into your chunk or "grid" if I understand you correctly. Each chunk could be a game object with a MeshRenderer and MeshFilter on it, and the output of your voxel scalar field to polygon should be assigned to a Mesh. As you can see, you can assign vertices and indices on a new Mesh object and this is how I did it when I wrote Clayful - which also was derived from Paul Bourkes code.

So I guess you'd do this in an outlined form:

  1. Generate triangles for a given chunk

  2. Create a new Mesh object and assign verts, uvs, normals, indices etc

  3. Create a new GameObject to hold this chunk

  4. Add a MeshFilter to the GameObject

  5. Assign the Mesh to the MeshFilter

  6. Add a MeshRenderer to the GameObject

  7. Assign an appropriate Material to the Renderer

This would represent one chunk in a very simple form. To make another chunk, just triangulate another region of space.

I solved texturing using triplanar texturing - this is basically just projecting 3 textures on the mesh, from 3 axes (x, y and z projection) and interpolating between them based on the normal.

To make the model editable, I used a 3d float (or byte) array that represented the scalar field. When you modify (carve/build) you just modify the floats (add/subtract). So your polygonizer should sample that 3d array. This is a naive implementation and I am sure there could be ways to make it faster, better, smaller, smarter and so on, but I was happy to just get it working in my run.

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Mar 08, 2013 at 03:11 PM 0
Share

Unfortunately I rarely save source code for pet projects, otherwise I could have shown the messy code I had.

avatar image dacudo · Mar 08, 2013 at 03:32 PM 0
Share

Thank you for the answer! This is what I meant by grid (quoted from first paragraph of solution here):

The fundamental problem is to form a facet approximation to an isosurface through a scalar field sampled on a rectangular 3D grid. Given one grid cell defined by its vertices and scalar values at each vertex, it is necessary to create planar facets that best represent the isosurface through that grid cell.

However, I think your description of a chunk is the same as this and what I want.

Yes, I do have a function for generating triangles. I have the whole of Paul Bourke's Polygonise source code in C#.

This clayful? If so, you know how to make what I want. I just want a box/sphere object to do the cutting/drawing of the terrain like in this game.

Thanks very much. I will try this and get back to you. I appreciate the help.

$$anonymous$$

avatar image Statement · Mar 08, 2013 at 03:46 PM 0
Share

Yeah okay if you can generate triangles based on a float[,,] scalarField = new scalarField[16, 16, 16]; then all you need to do is to paint into that (add/subtract) and then regenerate the mesh

avatar image dacudo · Mar 08, 2013 at 04:05 PM 0
Share

Would I use this code to generate a chunk of triangles:

 using UnityEngine;
 using System.Collections;
 
 public class example : $$anonymous$$onoBehaviour {
     public Vector3[] newVertices;
     public Vector2[] newUV;
     public int[] newTriangles;
     void Start() {
         $$anonymous$$esh mesh = new $$anonymous$$esh();
         GetComponent<$$anonymous$$eshFilter>().mesh = mesh;
         mesh.vertices = newVertices;
         mesh.uv = newUV;
         mesh.triangles = newTriangles;
     }
 }

Then add the 8 vertices of the cube/chunk. I'm not sure what to use the triangles array for though. Thanks again!

avatar image dacudo · Mar 08, 2013 at 04:15 PM 0
Share

Yeah okay if you can generate triangles based on a float[,,] scalarField = new scalarField[16, 16, 16]; then all you need to do is to paint into that (add/subtract) and then regenerate the mesh

Could you explain this in more detail, please? What steps in your list would that method be in place of?

Show more comments
avatar image
1

Answer by Sammael · Sep 26, 2013 at 07:03 PM

Take a look into this link: Link removed due to phishing site (2017.01.06)

Hope it helps

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chrisbot · Jan 06, 2017 at 08:17 PM 0
Share

the link above is a phishing site. DO NOT CLIC$$anonymous$$ IT!

avatar image Bunny83 Chrisbot · Jan 06, 2017 at 10:22 PM 0
Share

I guess the blog has been hijacked. I remove the link from the answer to avoid people clicking it.

Thanks for the info.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Fading To New Scene Problem? 1 Answer

I know C#, but what Unity methods should I learn? 2 Answers

UDP implementation in Unity, unable to send to two different machines 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges