Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by PsyDev · Apr 11, 2017 at 05:47 PM · texturetexture2dsampling

Why does tex3d return a different value than tex2d?

I'm hoping someone can shed some light on how texture sampling works with Texture3D and tex3d. I am seeing different results when sampling from a 2D texture then when sampling from a similar 3D texture.

alt text

As a simple test case, I create a 3D texture in code, and have a shader that maps a slice of that 3D texture to a 2D quad. The texture is 8x8x8, and is gradient (or step) from black to white in the x/u direction. So for a given value of x/u, all values of y/v and z/w are the same.

When sampling in the shader, I map the u and v from the vertex positions as you would with any 2D texture, and set the w to a fixed value. At the same time as I create the 3D texture, I also create a 2D texture, using the same pixel values as the 3D texture for one slice in the z-direction, when z = 0.

Both textures have point filtering and clamp wrap mode.

Why are the colors brighter on the 3D version?

Here is the code to create the textures:

         for(int k = 0; k < texDim; k++)
         {
             for(int j = 0; j < texDim; j++)
             {
                 for(int i = 0; i < texDim; i++)
                 {
                     byte colorByte = (byte)(255 * i / (float)(texDim - 1));
                     pixels[i + (j * texDim) + (k * texDim * texDim)].r = colorByte;
                     pixels[i + (j * texDim) + (k * texDim * texDim)].g = colorByte;
                     pixels[i + (j * texDim) + (k * texDim * texDim)].b = colorByte;
                     pixels[i + (j * texDim) + (k * texDim * texDim)].a = 1;
 
                     if(k == 0)
                     {
                         pixels2d[i + (j * texDim)].r = colorByte;
                         pixels2d[i + (j * texDim)].g = colorByte;
                         pixels2d[i + (j * texDim)].b = colorByte;
                         pixels2d[i + (j * texDim)].a = 1;
                     }
                 }
             }
         }
         
         tex3d.SetPixels32(pixels);
         tex3d.wrapMode = TextureWrapMode.Clamp;
         tex3d.filterMode = FilterMode.Point;
         tex3d.Apply();
 
         tex2d.SetPixels32(pixels2d);
         tex2d.wrapMode = TextureWrapMode.Clamp;
         tex2d.filterMode = FilterMode.Point;
         tex2d.Apply();
 

Here is the 3d shader code:

             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
             };
 
             sampler3D _Tex3d;
             float _Slice;
             
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.uv;
                 return o;
             }
             
             fixed4 frag (v2f i) : SV_Target
             {
                 // sample the texture
                 float3 uvw = float3(i.uv.x, i.uv.y, _Slice);
                 fixed4 col = tex3D(_Tex3d, uvw);
                 return col;
             }
 
 

And 2D shader code:

             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
             };
 
             sampler2D _MainTex;
 
             v2f vert(appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.uv;
                 return o;
             }
 
             fixed4 frag(v2f i) : SV_Target
             {
                 fixed4 col = tex2D(_MainTex, i.uv);
                 return col;
             }
 

tex3dtex2d.png (14.0 kB)
Comment
Add comment · Show 3
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 FortisVenaliter · Apr 11, 2017 at 06:01 PM 0
Share

Can you show the code where you instantiate the textures? They are using the same TextureFormat, right?

avatar image PsyDev FortisVenaliter · Apr 11, 2017 at 06:02 PM 0
Share

Yes, same texture format:

         tex3d = new Texture3D(texDim, texDim, texDim, TextureFormat.ARGB32, false);
         Color32[] pixels = new Color32[texDim * texDim * texDim];
 
         tex2d = new Texture2D(texDim, texDim, TextureFormat.ARGB32, false);
         Color32[] pixels2d = new Color32[texDim * texDim];
 
avatar image FortisVenaliter PsyDev · Apr 11, 2017 at 06:06 PM 0
Share

No idea then... It looks like that code should produce identical results.

2 Replies

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

Answer by Dante_CyberdeckGames · Apr 12, 2017 at 07:43 PM

Texture2D has sRGB gamma correction by default: https://docs.unity3d.com/ScriptReference/Texture2D-ctor.html

Texture3D does not have an option to choose which color space you use. I believe it is always linear.

Try this code for Texture2D, which makes it linear color space instead of sRGB. Then they should be the same.

 tex3d = new Texture3D(texDim, texDim, texDim, TextureFormat.ARGB32, false);
 Color32[] pixels = new Color32[texDim * texDim * texDim];
 
 // The final parameter bool is linear color space (true)
 tex2d = new Texture2D(texDim, texDim, TextureFormat.ARGB32, false, true);
 Color32[] pixels2d = new Color32[texDim * texDim];
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 PsyDev · Apr 12, 2017 at 08:40 PM 0
Share

Exactly, problem solved. Thanks very much for taking the time to reply.

avatar image
0

Answer by mbzdmvp · Apr 13, 2017 at 09:48 AM

I know you mentioned they have they both have point filtering, but I would double check the filterMode property on the textures, it could be changing itself?

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 PsyDev · Apr 21, 2017 at 12:41 PM 0
Share

No, it was linear vs gamma colour space as described in the answer above.

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

84 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

Related Questions

Why is my texture like this? 1 Answer

Encoding metadata in a texture 1 Answer

Update Texture type to GUI during runtime 0 Answers

Splatmap and Corresponding Textures to 1 final Texture 1 Answer

Converting a RenderTexture to a Texture2D for use in a shader 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