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
-1
Question by andreahmed · Mar 27, 2015 at 05:22 PM · shadershader programmingshaderlab

calculating

I'm new. try to do some calculations using shaders.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessespike · Mar 27, 2015 at 05:50 PM

I want to count the red pixels in a texture with a shader or do I have to use a computer shader?

I don't think a shader can count the number of pixels in a texture, they don't really work that way. What you can do is write a script to do that for you.

 public class CountRedPixels : MonoBehaviour {
 
     // Texture needs Read/Write enabled from the Advanced Texture import settings
     public Texture2D _Texture;
 
     [Range(0, 1)]
     public float _MinRedColorValue = 0.5f;
 
     // Update is called once per frame
     void Update () {
         PerformRedPixelCount();
     }
     
     void PerformRedPixelCount () {
         int numOfRedPixels = 0;
 
         for (int y = 0; y < _Texture.height; y++)
         {
             for (int x = 0; x < _Texture.width; x++)
             {
                 Color pixelColor = _Texture.GetPixel(x, y);
                 if (pixelColor.r >= _MinRedColorValue)
                 {
                     numOfRedPixels++;
                 }
             }
         }
 
         string debugMessage = string.Format("Found {0} pixels in {1} with a minimun R value of {2} ({3})", 
                                             numOfRedPixels, 
                                             _Texture.name, 
                                             _MinRedColorValue, 
                                             (int)(_MinRedColorValue * 255));
         Debug.Log(debugMessage);
     }
 }
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 Jessespike · Mar 27, 2015 at 08:38 PM 0
Share

why can it not work?

Well it's quite complex and I don't fully understand how the shader pipeline operates. But from what I can grasp, pixel (fragment) shaders don't actually handle textures per-pixel, they operate on texels.

I might be wrong and I hope someone will correct me if I am.

In either case, what exactly are you trying to accomplish and why does it have to be done via shader on gpu?

avatar image Jessespike · Mar 28, 2015 at 09:39 AM 1
Share

can you remove your answer please ? it doesn't answer my question, I need to do it in shader not in cpu.

Other people can contribute answers as well. I answered your question whether it's possible or not and offered a solution. I won't remove it simply because other knowledge seekers may have the same question in the future, and my solution might help them. You can simply ignore my response and accept someone else's answer.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A Cg shader semantic problem 2 Answers

Help with a see-through shader 0 Answers

How to manipulate a shader's alpha channel while using a texture? 1 Answer

Shader - What is float3.xy? 1 Answer

How to Fade out Custom Unity 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