Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 nug700 · Mar 03, 2014 at 06:01 AM · voxel

convert from world to voxel when 1 voxel doesn't equal 1 unity unit

I'm making a game which requires using a voxel engine to build structures, except I plan to include more models than just simple cubes, and some of these are quite small. So I need to the voxels to be smaller than 1 unity unit each (3 per unit). The problem is I'm having trouble converting between world space and voxel coordinates due to the unequal sizes (if they were the same size it wouldn't be a problem). This is what I've tried so far:

     public Vector3 GetIndexFromWorld(Vector3 worldPos, Vector3 normals) {
         float half = ((1f / (float)VoxelsPerMeter) / 2f); // VoxelsPerMeter equals 3
         Vector3 localPos = transform.InverseTransformPoint(worldPos);
         Vector3 point = localPos + normals * -half;
         return new Vector3(Mathf.RoundToInt(point.x) * VoxelsPerMeter, Mathf.RoundToInt(point.y) * VoxelsPerMeter, Mathf.RoundToInt(point.z) * VoxelsPerMeter);
     }

 

Thanks in advance for any help.

Comment
Add comment · Show 2
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 Owen-Reynolds · Mar 03, 2014 at 06:10 AM 0
Share

What's InverseTransformPoint for -- Do your objects run diagonally?

avatar image nug700 · Mar 03, 2014 at 06:16 AM 0
Share

I have the InverseTransformPoint to convert from world space to the chunks local space, because the chunk contains the 3D array of voxels (and yea, to give the option of rotating the chunks).

1 Reply

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

Answer by whydoidoit · Mar 03, 2014 at 06:17 AM

Isn't it just:

 var localPos = transform.InverseTransformPoint(worldPos) * VoxelsPerMeter;
 return new Vector3(Mathf.FloorToInt(localPos.x),Mathf.FloorToInt(localPos.y),Mathf.FloorToInt(localPos.z));
 
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 whydoidoit · Mar 03, 2014 at 06:18 AM 0
Share

Though I'd prefer a Tuple for the index.

  Tuple<int,int,int>
avatar image nug700 · Mar 03, 2014 at 06:36 AM 0
Share

Thanks whydoidoit. multiplying the local position by three, replacing "RoundToInt" with "FloorToInt", and removing the * VoxelsPer$$anonymous$$eter from the final vector3 solved it.

avatar image whydoidoit · Mar 03, 2014 at 07:24 AM 0
Share

Will convert this lot to an answer so you can tick it :)

avatar image bigvalthoss · Feb 24, 2021 at 08:51 PM 0
Share

How would you figure out how many voxels per meter? If the object scales, won't that change?? @nug700 @whydoidoit

avatar image Eno-Khaon bigvalthoss · Feb 24, 2021 at 09:40 PM 1
Share

It's not really something to "figure out" so much as it's something to be defined. Essentially, you could think of it as the reciprocal of uniform scale (where that scale would simply need to be an integer). If it's 8 local units per world unit, then you have 0.125 scale (1/8) and, by extension, 8 voxelsPerMeter by this example.

avatar image bigvalthoss Eno-Khaon · Feb 28, 2021 at 04:51 PM 0
Share

Do you think you could check out this question? https://answers.unity.com/questions/1817469/get-voxel-intensities-by-location.html Thank you!

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

24 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

Related Questions

No collision detection with back faces in voxel game. HELP! 1 Answer

Saving 3D color array to file 1 Answer

Let Character Controller walk up actual stairs/blocks 0 Answers

Place block script 1 Answer

How do I calculate this voxel script so I don't get a out of range error? 2 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