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 /
This question was closed Sep 11, 2014 at 06:02 PM by tanoshimi for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by z4444n · Sep 11, 2014 at 05:42 PM · shadertexture2dsampling

Pixel perfect sampling in surface shader

I'm creating procedural texture, with something like:

 int texSize = 20;
 Texture2D tex = new Texture2D (texSize, texSize, TextureFormat.RGBA32, false, false);
 tex.filterMode = FilterMode.Point;
 
 for (int i = 0; i < tex.width; i++)
 {
     for(int j = 0; j < tex.height; j++)
     {
         if(i == 2 && j == 3)
              tex.SetPixel(i, j, new Color(1.0f, 0.0f, 0.0f));
         if(i == 2 && j == 4)
              tex.SetPixel(i, j, new Color(1.0f, 0.0f, 0.0f));
     }
 }
 
 tex.Apply();


then in shader i want to sample pixel from this texture

 Shader "Custom/Terrain" 
 {
 Properties 
 {
     _TexData ("HexData", 2D) = "white" {}
 }
 SubShader 
 {
     Tags { "RenderType"="Opaque" }
     LOD 200
     //Cull off
     
     CGPROGRAM
     #pragma surface surf Lambert finalcolor:mycolor
     #pragma target 3.0
 
     sampler2D _TexData;

     void surf(Input IN, inout SurfaceOutput o) 
     {
         o.Albedo = grass; 
         o.Alpha = 1.0;
     }
   
     void mycolor (Input IN, SurfaceOutput o, inout fixed4 color)
     {
         color.rgb = tex2D(_TexData, float2(    3 / 20.0f, 4 / 20.0f));// returns correctly red
         //or
         color.rgb = tex2D(_TexData, float2(    3 / 20.0f, 5 / 20.0f));// returns white
     }
 
     ENDCG
 } 
 FallBack "Diffuse"
 }

i'm getting right values when sampling 3 / textSize, 4 / textSize, but wrong for 3 / textSize, 5 / textSize.

i'm saving texture to disk for testing and it looks good: alt text

is there a way to sample texture by pixel, or fix my pixel to text coord calculation?

test.png (110 B)
Comment
Add comment · Show 1
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 z4444n · Sep 11, 2014 at 05:00 PM 0
Share

manage to figure this out, problem was that texture wasn't power of 2

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by z4444n · Sep 11, 2014 at 06:02 PM

manage to figure this out, problem was that texture wasn't power of 2

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

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

glTexSubImage3D in unity 0 Answers

Use result from first shader pass in second pass 0 Answers

GetPixel(x,y) within .shader? HLSL get color from texUV coordinates? 0 Answers

GetPixel in shader graph 1 Answer

Shaders: How heavy is tex2D()? 1 Answer


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