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
0
Question by irrumabote · Dec 20, 2018 at 09:41 PM · shadersgraphics

Get target RenderTexture dimensions in fragment shader used for Graphics.Blit

I'm resizing (scaling up) a texture by doing something like:

 Material testMaterial = new Material(Shader.Find("Unlit/MyTestFragmentShader"));
 Graphics.Blit(_sourceTex, renderTex, testMaterial);

where _sourceTex is a Texture2D and renderTex is a RenderTexture that is larger than _sourceTex by some integer multiple. So if, say, _sourceTex is 10x10, renderTex might be 40x40 for e.g., with each pixel of _sourcePixel ending up as 4x4 pixels in renderTex. I'm doing this in the Editor.

This is doing what I want. However, as a further step I would like, in my fragment shader, to put 1 pixel 'gaps' between what were the original pixels from the source. So for example _sourceTex is 10x10, renderTex is 40x40 but every 4th pixel (in both dimensions) is transparent - effectively each original pixel ends up as a 3x3 'cell' with a 1 pixel transparent border in both x and y.

To do this, in the fragment shader, I need to know how big each 'cell' is in the destination texture, which will be destination.width / source.width. I know source width from _MainTex_TexelSize.z and this works as expected. I thought I could use _ScreenParams.x to get the target width, but _ScreenParams actually has the value the dimensions of Editor game view width, not the target RenderTexture dimensions.

I can just pass in the target RenderTexture dimensions as a property to the shader, and that will solve my problem, but is there any way of getting the actual RenderTexture dimensions in a similar way to _ScreenParams from inside the shader?

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 Pangamini · Feb 27, 2020 at 11:06 AM 0
Share

Hello. Did you by any chance figure this out?

avatar image Namey5 Pangamini · Feb 27, 2020 at 01:16 PM 0
Share

In theory, if you really needed to find the dimensions of the destination RT without passing them in yourself, you could use partial derivatives, i.e.

 //Where zw is the destination's dimensions, and xy are the distance between texels
 float4 texelSize;
 texelSize.xy = abs (float2 (ddx (i.uv.x), ddy (i.uv.y)));
 texelSize.zw = 1.0 / texelSize.xy;

It won't be as precise as the original, and I don't see a reason why you shouldn't be able to just pass in the texture's dimensions, but it should work.

avatar image irrumabote · Feb 27, 2020 at 11:57 AM 0
Share

No, sorry. I'm doing things a bit differently now - basically I just create the target render texture at the size it needs to be, then pass in the 'cell' size and the 'gap' size to the shader as properties, so I don't really need to worry about the target texture width in the shader.

0 Replies

· Add your reply
  • Sort: 

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

120 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

Related Questions

post shader: reconstruct worldspace plane coordinates 1 Answer

materials in editor look good but in run-time ineditor(android) they are pink 0 Answers

Using stencil buffer with OnRenderImage()/Blit 0 Answers

LWRP - Placing markers on terrain and gameobject dynamically 0 Answers

Graphics.CopyTexture and location fo textures 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