• 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
1
Question by SonicScrew12 · Feb 10, 2018 at 07:53 PM · proceduralvoxelsmarching cubes

Confining Marching Cubes to the Radius of a Sphere

Hello,

I'm making asteroids for my game. I've decided to do so procedurally using 3D Perlin noise and a Marching Cubes algorithm.

I've run into a problem whilst trying to confine the marching cubes to a sphere. Normally, marching cubes gives you a cube-shaped grid. However, I've modified it so that it cuts off anything that's outside of a radius of a sphere like so:

Marching Cubes Inside of a Sphere

I do this by adding this code to my marching cubes algorithm:

    void FillData(float xOrigin, float yOrigin, float zOrigin) {
     for (int x = 0; x < size; x++) {
         for (int y = 0; y < size; y++) {
             for (int z = 0; z < size; z++) {
                 // If the current point is outside the radius, set it to -1
                 if (Vector3.Distance(new Vector3(x, y, z), new Vector3(xOrigin + size/2, yOrigin + size/2, zOrigin + size/2)) > (size / 2)) {
                     data[x, y, z] = -1;
                     continue;
                 }
                                    
                 // more code here...
             }
         }
     }

xOrigin, yOrigin, and zOrigin are the Terrain GameObject's transform.position. If the distance between the current point and the outer edge of the sphere is larger than the sphere's radius, cut those points off (set them equal to a negative density).
~

This works, up to a certain point. When I move the Terrain GameObject past the length of the sphere's radius (AKA size / 2), the asteroid starts to disappear, like this:

Marching Cubes Sphere Failure

How do I prevent this from happening?

capture2.png (313.2 kB)
capture3.png (374.3 kB)
Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Feb 11, 2018 at 12:12 PM

Your xOrigin, yOrigin and zOrigin should not have any influence on the radius distance since it's a local coordinate vector. Your Vector3(x,y,z) is always a vector from the "world origin" up to (size,size,size).


What you actually want is a local space vector that doesn't go from (0,0,0) up to (size,size,size) but a vector that should have the range (-size/2f, -size/2f, -size/2f) up to (size/2f, size/2f, size/2f). So you just subtract half the size of each component. The resulting vector will be a relative vector from the center of your cube volume. The length of this vector should be compared against your desired radius. Since we just want to compare length we can use the squared length since it's easier to calculate:

float radius = size 0.5f; float sqrRadius = radius radius;

 for (int x = 0; x < size; x++) {
     for (int y = 0; y < size; y++) {
         for (int z = 0; z < size; z++) {
             var v = new Vector3(x-radius, y-radius, z-radius);
             if (v.sqrMagnitude > sqrRadius)
             {
                 data[x, y, z] = -1;
                 continue;
             }
             // [ .. ]
Comment
Add comment · Show 1 · 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 SonicScrew12 · Feb 11, 2018 at 02:42 PM 0
Share

OH $$anonymous$$Y STARS I should've known it was relative to the object! Thank you so much!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

81 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Graphics.DrawProceduralIndirect isnt rendering anything 0 Answers

Voxel Terrain , Trees , Destruction - help please 0 Answers

How to implement sandbox system on a marching cubes voxel terrain? 1 Answer

How to remove small & thin triangles from a marching cubes voxel engine? 2 Answers

Deform 2D Mesh Made With Marching Squares 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges