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
0
Question by bigvalthoss · Feb 24, 2021 at 08:23 PM · vrvoxeloculusriftvoxels

Get Voxel Intensities by location

I'm wondering if it's possible to get the voxel value at a given position? Here is the code used to fill the voxels:

 for (int k = 0; k < NumberOfFrames; k++) {
             string fname_ = "T" + k.ToString("D2");
             Color[] colors = LoadData(Path.Combine (imageDir, fname_+".raw"));
             _volumeBuffer.Add (new Texture3D (dim [0], dim [1], dim [2], TextureFormat.RGBAHalf, mipmap));
 
             _volumeBuffer[k].SetPixels(colors);
             _volumeBuffer [k].Apply ();
         }
 
         GetComponent<Renderer>().material.SetTexture("_Data", _volumeBuffer[0]);

Does anybody know how to get a value at a given position in space??

Thanks!

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

2 Replies

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

Answer by bigvalthoss · Feb 24, 2021 at 08:58 PM

Took me a little bit, but I've figured it out I think. If there is a flaw in my coding, please let me know :)

 public Vector3Int GetIndexFromWorld(Vector3 worldPos)
     {
         Vector3 deltaBounds = rend.bounds.max - rend.bounds.min;
         Vector3 OffsetPos = worldPos - rend.bounds.min;
         Vector3 normPos = new Vector3(OffsetPos[0] / deltaBounds[0], OffsetPos[1] / deltaBounds[1], OffsetPos[2] / deltaBounds[2]);
 
         Vector3 voxelPositions = new Vector3(normPos[0] * voxelDims[0], normPos[1] * voxelDims[1], normPos[2] * voxelDims[2]);
 
         Vector3Int voxelPos = Vector3Int.FloorToInt(voxelPositions);
         return voxelPos;
     }

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 bigvalthoss · Feb 28, 2021 at 04:53 PM 0
Share

@$$anonymous$$o-Khaon I have a dataset that is 512 x 256 x 512. It can be resized, is there any way you could think to find the voxel index from a world position (if it is inside the object).

@whydoidoit Do you have any input on this question?

avatar image
0

Answer by Eno-Khaon · Feb 28, 2021 at 08:23 PM

Since you're using transform.InverseTransformPoint() to transform a point from world to local space, the most straightforward way to position voxels "physically" is to place them at their absolute positions relative to the GameObject (i.e. (0, 0, 0), (15, 98, 17), etc.) so that their positions can inherently be used as-is when converted to 3-dimensional array indices. (Note that this may also call for Mathf.RoundToInt() or Mathf.FloorToInt() usage, depending on how coordinates are used and whether anything like 0.5 offsets would be applied to positions to "center" the voxels)

If any repositioning or scaling is also applied to the voxels at the construction level (ideally, rotation should be a Transform-only modification), then a given block of voxel data would also need its own scaling factor(s) and position offset data.

 // Reformatting your function as an example
 public Vector3Int GetIndexFromWorld(Vector3 worldPos)
 {
     Vector3 localPos = transform.InverseTransformPoint(worldPos);
     
     // If further scaling needs to be done:
     // Where scaleFactor is voxels-per-unit...
     // Scale is a Vector3
     localPos = Vector3.Scale(localPos, scaleFactor);
     // Scale is a float/int
     localPos *= scaleFactor;
     
     // voxelOffset would be a factor when the voxel-grid doesn't begin
     // at (0, 0, 0) of its containing GameObject/Mesh
     Vector3Int voxelPos = Vector3Int.FloorToInt(localPos + voxelOffset);
     
     // Using FloorToInt() on the assumption that there IS NOT an additional
     // 0.5-voxel offset baked in that's not part of the main offset
     // If there was, RoundToInt() would be the better choice here
     
     return voxelPos;
 }



All in all, the kinds of modifications that need to be applied are really just based on whether (and how) you want to pack additional data in. If you performed *ALL* transformations to the voxel data using the GameObject's Transform data instead, your function would look more like this:

 public Vector3Int GetIndexFromWorld(Vector3 worldPos)
 {
     Vector3 localPos = transform.InverseTransformPoint(worldPos);
     Vector3Int voxelPos = Vector3Int.FloorToInt(localPos);
     return voxelPos;
 }
Comment
Add comment · Show 3 · 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 bigvalthoss · Mar 11, 2021 at 08:40 PM 0
Share

Thank you very much for the answer, I think your way would probably work. however, I am having trouble co$$anonymous$$g up with finding the scaling factor and the offset. Would you be able to help me out with that? dim[0-2] are the dimensions of the dataset. The current one I'm using is 512 x 256 x 512.

Here is how the local scale is defined:

 this.transform.localScale = new Vector3(mhdheader.spacing[0] volScale, mhdheader.spacing[1] volScale dim[1] / dim[0], mhdheader.spacing[2] volScale * dim[2] / dim[0]);


Would I just create my offset by doing:

 Vector3 localPos = transform.InverseTransformPoint(worldPos); 
 Vector3 localScale = gameObject.transform.localScale; 
 Vector3 OffsetPos = localPos + localScale / 2;

And then I need to find what to scale it by, which is causing me a little grief as well...

avatar image Eno-Khaon bigvalthoss · Mar 12, 2021 at 06:46 PM 0
Share

In general, the scaling factor and offset don't really need to be tracked using a dual system. While you could have something like...

 Vector3 scale = transform.localScale * myScale;
 Vector3 offset = localPos + Vector3.Scale(scale, myOffset);

... that's actually not very valuable to have a mishmash of your own script's values *AND* Transform attributes.

However, that's why I offer the suggestion of perfor$$anonymous$$g all transformations using just the Transform data of the containing GameObject. That way, voxel[0,0,0] would be located at the local (0, 0, 0) of the Transform. Then, if you want, say, 5 voxels per unit/meter/etc., you'd just have something like:

 transform.localScale = Vector3.one / 5.0f;



By contrast, if you wanted to ignore the GameObject's Transform data entirely (which a Renderer wouldn't love, in terms of position relative to object origin), then you'd want to place the GameObject at Vector3.zero position, Quaternion.identity rotation, and Vector3.one scale.

That really would defeat the purpose of having Transform data to begin with, though, and would actually be a hindrance (at best).


Basically, if you just store values for your scale, position, and rotation, then apply those to the Transform, that's all you need to do. The rest just depends on how you want to store that data for use.
 // voxel grid position is stored as world position
 transform.position = voxelGridWorldPos;
 // alternate: voxel grid position is stored in small integer form
 // Vector3 voxelGridSize: (512x512x256)
 // Vector3Int voxelGridPos: (i.e. (5, -2, 0))
 transform.position = Vector3.Scale(voxelGridSize * voxelScale, voxelGridPos);
 
 // voxel grid position is stored as Quaternion
 transform.rotation = voxelGridRot;
 // alternate: rotation is stored as XYZ Euler values
 transform.rotation = Quaternion.Euler(vgrX, vgrY, vgrZ);
 
 // voxel grid scale is a single value for uniform scaling
 transform.localScale = Vector3.one * voxelScale;
 // alternate: voxel grid scale is stored directly as a Vector3
 transform.localScale = voxelScaleVector3;
 // alternate: scale is stored as 3 axis scalars
 transform.localScale = new Vector3(scaleX, scaleY, scaleZ);
 // alternate: scale is stored as voxels-per-unit (Vector3 example)
 transform.localScale = new Vector3(1f / vpu.x, 1f / vpu.y, 1f / vpu.z);
avatar image bigvalthoss Eno-Khaon · Mar 12, 2021 at 07:23 PM 0
Share

Would it be at all possible to like call over google meet about this? I don't think I entirely follow... It would help a lot

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

157 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 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

Will my VR project (currently using mouse) run correctly when I return to a PC with a headset? 1 Answer

Oculus "PerEyeCameras" showing blackscreen only when LWRP or URP is enabled 0 Answers

Preventing VR mirroring to main display 0 Answers

Question about slideScrolling Voxels 0 Answers

Is it possible to override the "Cannot set field of view on camera with name 'screenCamera' while VR is enabled." limitation? 1 Answer


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