• 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
0
Question by DavidDebnar · Jul 12, 2011 at 05:25 PM · meshproceduralcubeprocedural meshalgorithm

Sphere made of cubes algorithm

Hey guys! I'm looking for a simple (or less simple) algorithm, that will create a sphere made of cubes with some adjustable radius. Something like this for cube, but for sphere.

var prefab : Transform; var oneSide : int = 5; function Start(){ var x : int = 0; var y : int = 0; var z : int = 0; while(x

And I don't wan't to rotate the cubes to the center. :)

  • David

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

4 Replies

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

Answer by flaviusxvii · Jul 12, 2011 at 07:39 PM

The code you have will work, you just need to check to see if transform.position + Vector3(x,y,z) is in the target sphere, an only instantiate if it is.

Comment
Add comment · Show 4 · 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 Chris D · Jul 12, 2011 at 07:56 PM 2
Share

If you want to find out if something is in a sphere, you can find the distance between the point in question and the centre of the sphere. If that is greater than the radius, it is no longer inside the sphere ...

(...if you're using centroids - if you're considering any point on your cube touching to be "in", there's more to do)

avatar image DavidDebnar · Jul 12, 2011 at 08:04 PM 0
Share

I'll try that distance :).

avatar image DavidDebnar · Jul 12, 2011 at 08:26 PM 0
Share

Thanks, worked.

var prefab : Transform; var size : int; var radius : float;

function Start() { var x : int = -size; var y : int = -size; var z : int = -size;

while(x

avatar image runonthespot · Jul 14, 2011 at 03:20 PM 2
Share

faster way of doing your distance check:

 if (Vector3.Sqr$$anonymous$$agnitude(Vector3(x,y,z)) > (radius * radius))
 

because you only need to check the x/y/z Vector relative to 0 (and then place that at your transform).

Because 1) position - position + Vector3(x,y,z) == Vector3(x,y,z) ! 2) Comparing Sqr$$anonymous$$agnitude with a square of the radius is much faster

avatar image
1

Answer by Peter G · Jul 12, 2011 at 06:28 PM

I haven't tried this, but this blog post seems to describe exactly what you want.

http://entitycrisis.blogspot.com/2011/02/uniform-points-on-sphere.html

Comment
Add comment · 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
0

Answer by Chris D · Jul 12, 2011 at 06:03 PM

While I'm thinking of a better (more efficient) option, why not find the (cubic) bounds of the sphere you want to convert, flood it with cubes of the resolution you want, then run a quick collision check to see which of the cubes are held within the sphere?

It seems like this would work but, again, I can't imagine it's the best way to go about it.

Comment
Add comment · Show 6 · 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 DavidDebnar · Jul 12, 2011 at 06:08 PM 0
Share

A little more explanation please :).

avatar image Chris D · Jul 12, 2011 at 06:17 PM 0
Share

So (in 3d) you create your sphere. Then you create a larger box that contains its bounds, kind of like this:

SquareCircle: https://upload.wikimedia.org/wikipedia/commons/0/00/Square-circle.svg [preview of external content removed for GDPR compliance as it was including 3rd party cookies]

and you flood that cube with your smaller cubes (of the size you choose, with the code you already have, assu$$anonymous$$g it works) on a grid.

After that, you run a check to see which of those cubes intersects the sphere. If they don't, destroy them.

I'm not sure there's really one answer to this question...

avatar image DavidDebnar · Jul 12, 2011 at 07:18 PM 0
Share

Well I tried, that is it collides, than not destroy, is not destroy, but it isn't working :(. I suck at mesh thingys.

avatar image Chris D · Jul 12, 2011 at 07:52 PM 0
Share

I don't understand what you're saying. You have code working but it's not destroying anything?

avatar image DavidDebnar · Jul 12, 2011 at 08:20 PM 0
Share

I tried, that if a cube is colliding with that sphere don't destroy and if it isn't than destroy, but it destroyed all cubes, I will try that distance, that was posted below :)

Show more comments
avatar image
0

Answer by BillyBobBeavis · Jun 14, 2021 at 01:40 AM

This solution worked for me. As said above you can make a cubical grid and check distance from a center point. This code makes a sphere of cubes that looks like something from MineCraft, but the idea is the same, just delete all the components from the brick and you have a grid of transforms. If you just want the shell of the sphere add a minimum distance in the distance check. You might want to reduce the number of bricks though. I'm running this on a 1060 GPU and I'm only getting a few frames.

 public GameObject brick;
     public int gridWidth;
     public int gridDepth;
     public int gridHeight;
 
     public List<GameObject> gridElements;
     public GameObject gridCenter;
 
     public Bounds gridBounds;
     
     void Start()
     {
         gridWidth = 240;
         gridDepth = 240;
         gridHeight = 240;
 
         gridCenter = new GameObject();
 
         brick = GameObject.CreatePrimitive(PrimitiveType.Cube);
         brick.transform.localScale = new Vector3(6, 6, 6);
 
         for (int y = 0; y < gridHeight; y = y + 6)
         {
             for (int z = 0; z < gridDepth; z = z + 6)
             {
                 for (int x = 0; x < gridWidth; x = x + 6)
                 {
                     var gridElement = Instantiate(brick, new Vector3(x, y, z), Quaternion.identity);
                     gridElements.Add(gridElement);
                     gridBounds.Encapsulate(gridElement.transform.position);
                 }
             }
         }
 
         Destroy(brick);
 
         gridCenter.transform.position = gridBounds.center;
 
         foreach(GameObject gridElement in gridElements)
         {
             var distance = Vector3.Distance(gridElement.transform.position, gridCenter.transform.position);
             if (distance > 120)
                 Destroy(gridElement);
             else
                 gridElement.transform.parent = gridCenter.transform;
         }
 
         gridCenter.transform.position = new Vector3(0, 0, 0);
     }
 
Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

Simple plane generation 1 Answer

Adding extra materials to a mesh programmatically 0 Answers

C# Proceducal Mesh terrain 2 Answers

Mesh Collider Edge Position. 0 Answers

Procedural sphere 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