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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by NeatWolf · Jul 05, 2014 at 12:45 PM · colortrianglespolygonaverage

How do I get the average color among 3 points in a texture?

I'm building an application that polygonize a picture. I still have to work on the actual geometry but in the meanwhile:

Let's suppose I've got 3 coordinates of a texture. How do I find the average color inside that triangle? Thank you.

(Possibly for Unity Free)

Comment
Add comment · Show 2
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 tanoshimi · Jul 05, 2014 at 01:10 PM 0
Share

In RGB or HSV colour space?

avatar image NeatWolf · Jul 05, 2014 at 01:37 PM 0
Share

RGB... or the easiest way.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by sschaem · Jul 07, 2014 at 07:01 AM

If its offline (simplest and shortest code):

  • Make a loop that covers the bounding box of the triangle

  • Check each point if its inside the triangle, if yes accumulate and keep that count.

  • When the loop end divide the accumulated RGB value by the number of points. Done.

Pseudo code:

 x0 = min(p0.x, p1.x, p2.x); x1 = max(p0.x, p1.x, p2.x);
 y0 = min(p0.y, p1.y, p2.y); y1 = max(p0.y, p1.y, p2.y);
 color.rgb = 0; count = 0;
 for(y=y0; y<y1; y++)
  for(x=x0; x<x1; x++)
   if(IsInside(float2(x, y), p0, p1, p2)) { color.rgb += texture.GetPixel(x,y); count += 1; }
 
 if(count>1) color.rgb /= count;

See this if you need the barycentric logic for IsInside(): http://www.blackpawn.com/texts/pointinpoly/

If this is done at runtime and you need optimal performance will need to write a simple triangle rasterizer and use that to accumulate value.

note: one important thing. If the texture hold gamma compressed values you 'cant' accumulate the value without first decompressing them. They should be actually sRGB encoded, so use the opengl sRGB function to decompress to linear before accumulating. Then after you do the average, you need to compress back to sRGB. Follow the formula in this link to decode the texture RGB color data. https://www.opengl.org/registry/specs/EXT/texture_sRGB.txt

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
avatar image
0

Answer by legion_44 · Jul 05, 2014 at 02:21 PM

Using Texture2D you can use GetPixel to get pixel color from specified position. Then You just add all color values (r,g,b) and divide them by their count. Example, you have 3 pixels:

 Color c1;
 Color c2;
 Color c3;
 Texture2D tex;
 
 void Awake()
 {
  c1 = tex.GetPixel(0,0);
  c2 = tex.GetPixel(0,1);
  c3 = tex.GetPixel(1,1);
 
  float Raverage = c1.r + c2.r + c3.r;
  float Gaverage = c1.g + c2.g + c3.g;
  float Baverage = c1.b + c2.b + c3.b;
  Color average = new Color(Raverage / 3, Gaverage / 3, Baverage / 3);
 }

Hope that helps,
Paul

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 NeatWolf · Jul 05, 2014 at 04:09 PM 0
Share

Thanks, Sure, this gives the average among 3 pixels, I was looking for the average of ALL pixels inside a triangle with defined vertexes.

avatar image Eric5h5 · Jul 05, 2014 at 05:43 PM 1
Share

Just so you know, it's way simpler just to do average = (c1 + c2 + c3) / 3;.

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

24 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

Related Questions

Getting triangles of a single gameobject (one prefab instance, not all) 1 Answer

Changing two different objects renderer colour 1 Answer

How do I get the color of a certain mesh vertex? 0 Answers

Merge mesh triangles into polygons: need help with vertex ordering 1 Answer

Overlapping Polygon Triangulation 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