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
2
Question by Santolin · Apr 01, 2014 at 04:26 PM · shadertexturedimensions

Is it possible to access the dimensions of a texture in a shader?

I'm trying to make a screen space shader that renders a texture which keeps always its natural size, regardless of the screen resolution or aspect ratio. That would mean the bigger the resolution the more times the texture would repeat. I would also like it to work for whatever texture, maybe even one provided by the user, so hard coding the actual picture dimensions wouldn't work, although that's what I'm doing right now.

Is there any way to access them? The code so far is like this:

 float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
 screenUV *= float2(_ScreenParams.x/[[Texture.Width]],_ScreenParams.y/[[Texture.Height]]);
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
-2
Best Answer

Answer by Owen-Reynolds · Apr 01, 2014 at 04:45 PM

No, the shader doesn't know or care about the actual pixel size of a texture.

But, Unity code can look up the texture size, and that's all you need. I'm assuming you have a plane covering the background, holding the texture. You can do you're second line in Unity code, one time, to change the background material's tiling:

 Texture2D T1;
 Transform backPlane;
 ...
 float xTile = Screen.width / T1.width, yTile = Screen.height / T1.height;
 Vector2 backTiling = new Vector2( xTile, yTile );
 backPlane.renderer.material.mainTextureOffset = backTiling;

For a test, type into a material's tiling yourself, with some precomputed numbers.

Comment
Add comment · Show 2 · 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 Santolin · Apr 02, 2014 at 05:49 AM 0
Share

Thanks a lot. I think changing the offset and tiling wouldn't work in my case, but your answer set me on the right path.

I added a new property to the shader:

 _TexSize ("Texture Size", Float) = 512

Which I can dynamically set through an external script:

 renderer.material.SetFloat("_TexSize",  T1.width);

Again, thanks.

avatar image AbleArcher · Nov 27, 2018 at 08:41 PM 0
Share

The correct answer is "Yes".

avatar image
60

Answer by AllanSamurai · Dec 11, 2015 at 12:36 PM

yes(tested)

http://docs.unity3d.com/Manual/SL-PropertiesInPrograms.html

look at Special Texture properties:

Texture size

{TextureName}_TexelSize - a float4 property contains texture size information:

 x contains 1.0/width
 y contains 1.0/height
 z contains width
 w contains height

so

For example, if a shader contains texture named _MainTex, the size information will be in a _MainTex_TexelSize vector.

 _MainTex_TexelSize.z //contains width
 _MainTex_TexelSize.w //contains height
Comment
Add comment · Show 7 · 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 jayatubi · Jun 14, 2016 at 05:16 AM 0
Share

This should be the best answer.

avatar image Owen-Reynolds jayatubi · Jun 14, 2016 at 01:39 PM 0
Share

The green thing doesn't mean best answer -- it's just the one the original question asker Accepted. It's understood that people will upvote whichever one they like and looking at the green one is just for fun (here, you can see it relates to specific details of the Q.)

Answers should be sorted by votes, but what can you do.

avatar image ixi150 · Sep 06, 2016 at 10:50 AM 0
Share

Logged in just to upvote this beautiful comment!

avatar image gferrand_UofM · Feb 17, 2017 at 09:46 AM 0
Share

Alas this doesn't seem to be offered for 3D textures.

avatar image TruffelsAndOranges · Aug 09, 2017 at 07:39 PM 0
Share

Can you give a better example of how this works?

Using any members of "$$anonymous$$ainTex_TexelSize" in the surface shader throws errors. Do you need to add the $$anonymous$$ainTex as a texture sampler first? I'm using the Sprites/Diffuse shader, with "[PerRendererData] _$$anonymous$$ainTex("Sprite Texture", 2D) = "white" {}".

avatar image AllanSamurai TruffelsAndOranges · Aug 10, 2017 at 09:42 PM 1
Share

you'll need declared this part "float4 _MainTex_TexelSize;" before use inside surf function:

 //declaration
 float4 _MainTex_TexelSize;
     
 void surf (Input IN, inout SurfaceOutput o)
 {
     fixed4 c = SampleSpriteTexture (IN.uv_MainTex) * IN.color;
     o.Albedo = c.rgb * c.a;

     // now we can use it. Its  just example to test if works
     o.Alpha = c.a * _MainTex_TexelSize.x;
 }
avatar image awsapps · Mar 07 at 10:41 AM 0
Share

This is what I was looking for, but just after decalring the variable all other components in the shader graph are throwing errors (lol)

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

28 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

Related Questions

Transparency and texture atlases, shader calls affecting performance? 2 Answers

Changing Eye Colour (Colour only non-white parts of a texture?) 2 Answers

Change particle tint color via script 1 Answer

Object space normal map warning the texture is not a normal map 1 Answer

My mesh didn’t appear in Unity 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