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
0
Question by MaT227 · May 10, 2014 at 02:31 PM · c#shadertexturearraymaterial

Texture2D to Texture3D

I would like to know how can I create a Texture3D from a Texture2D.

enter image description here

I've found some good examples : Unity 4 - 3D Textures (Volumes) or Unity - 3D Textures or Color Correction Lookup Texture

 int     dim = tex2D.height;
 Color[] c2D = tex2D.GetPixels();
 Color[] c3D = new Color[c2D.Length];
 for (int x = 0; x < dim; ++x)
 {
     for (int y = 0; y < dim; ++y)
     {
         for (int z = 0; z < dim; ++z)
         {
             int y_ = dim - y - 1;
             c3D[x + (y * dim) + (z * dim * dim)] = c2D[z * dim + x + y_ * dim * dim];
         }
     }
 }

But this only works when you have

 Texture2D.height= Mathf.FloorToInt(Mathf.Sqrt(Texture2D.width))

or if

 Depth = Width = Height


How can I extract the values when the depth is not equal to the width or the height ? It seems simple but I am missing something...

Thank you very much.

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 MaT227 · May 12, 2014 at 01:51 PM

Thanks to @NicoSchertler for his great answer.

You can split the texture as follows:

 //Iterate the result
 for(int z = 0; z < depth; ++z)
     for(int y = 0; y < height; ++y)
         for(int x = 0; x < width; ++x)
             c3D[x + y * width + z * width * height]
               = c2D[x + y * width * depth + z * width]

You can get to this index formula as follows:

  • Advancing by 1 in the x-direction results in an increment by 1 (just the next pixel).

  • Advancing by 1 in the y-direction results in an increment by depth * width (skip 4 images with the according width).

  • Advancing by 1 in the z-direction results in an increment by width (skip one image row).

Or if you prefer the other direction:

 //Iterate the original image
 for(int y = 0; y < height; ++y)
     for(int x = 0; x < width * depth; ++x)
          c3D[(x % width) + y * width + (x / width) * width * height] = c2D[x + y * width * depth];

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
1

Answer by Owen-Reynolds · May 10, 2014 at 03:58 PM

Are you asking how 2D and 3D arrays convert back and forth to linear arrays? Common programming topic, so should be able to find non-Unity references, maybe some pictures.

Imagine your texture is 5 wide, by 12 up and down. Number row one with 0-4, then number row two with 5-9. The last row will be 55-59. The next higher level (z=1) will be the same but start at 60. So each time y increases, add 5 (dim1.) Each time z increases, add 60 (dim1*dim2.)

So, to find the linear position in a "everything laid out in a line" array, use z*60+y*5+y. That's all the code above is doing, I think.

The sample code uses dim for height and width, assuming they will be the same. Replace some dims with width, and others with height. If you get confused, go back to the sketch with numbers, and plug in some sample coords.

Or, maybe even easier way to think of it: suppose the base (width by height) is 90. Each level above that is +90, +180, +270 ... . So, forget x,y,z. Just go through pixels 0,1,2... . To find the pixel below you, just use index%90.

BTW: Does that just copy the colors all the way up? I think if you just use the 2D array, it might automatically do that anyway.

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

21 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

Related Questions

How to render black borders around texture of simple 3D quad in c#? 0 Answers

How to change RGB base of material via scripting (Normal map + base RGB) ? 1 Answer

How to change a value in custom shader through script, C# 1 Answer

Light based texture change on material 0 Answers

How can I activate a shader at specific times? 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