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 heyx3 · Apr 24, 2018 at 04:39 PM · texturesshader programmingsampling

Can I sample a texture in a fragment shader using pixel coordinates instead of UV's?

I'm working on some complex image effects using fragment shaders, and it would be great if I could just access the pixels in _MainTex with int coordinates instead of messing around with texels. I can get the pixel coordinate of each shader invocation using the VPOS semantic, but i have no idea how to read from a texture using these pixel coordinates.

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
4
Best Answer

Answer by Bunny83 · Apr 24, 2018 at 05:09 PM

Well, inside of a shader a texture is an abstract thing. Textures always go through a sampler and depending on your filtering setting you either get the nearest pixel or a bilinear interpolated result.


Unity actually feeds the texel size and the texture dimensions as a float4 which is named just like your sampler followed by _TexelSize. So for the _MainTex sampler you just declare this in your cg code:

 uniform float4 _MainTex_TexelSize;

It will contain the following values:

  • x --> 1f / texWidth

  • y --> 1f / texHeight

  • z --> texWidth

  • w --> texHeight


To samples a specific pixel you should make sure you set the texture filter mode to point and then just do this:

 float2 pCoord;
 
 float2 uv = (pCoord+0.5) * _MainTex_TexelSize.xy;

This assumes pCoord is your desired pixel integer coordinate. The "+0.5" makes sure we sample the center of each pixel. The resulting "uv" can be used in a tex2d call. Keep in mind that _TexelSize only works with 2d textures. 3d textures don't get this set.

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 Tralafgar-Law · Jan 08, 2019 at 02:19 AM 0
Share

Thanks a lot !

avatar image
1

Answer by Glurth · Apr 24, 2018 at 05:00 PM

from the unity shader sample page, note this line in the fragment shader:

 fixed4 col = tex2D(_MainTex, i.uv);

This is the line that extracts the color from the texture. All you need to do is change the i.uv to instead be two coordinates in your VPOS result.

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 heyx3 · Apr 24, 2018 at 05:10 PM 0
Share

The tex2D function works in texel space, which is the exact opposite of what I want.

avatar image Glurth heyx3 · Apr 24, 2018 at 05:13 PM 0
Share

See Bunnies answer for how to scale your vertex appropriately. Indeed, this only answers the titular question.

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

How can I access a texture created through C# code (using Texture2D) in a unity shader as a sampler2D? 1 Answer

Vertex Color Shader 0 Answers

Swapping textures / shader of two gameobjects 1 Answer

Possible for a shader to have two decal slots? 2 Answers

select float2 uv_Maintex to UV map channel in shader. 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