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 /
  • Help Room /
avatar image
0
Question by Tageos · Aug 24, 2015 at 11:56 PM · c#cameracolor

Change color of a pixel to either black or white depending on how grey the pixel is

Hello!

I am making a script that "scans" a black and white picture. When i use a black and white picture (32x32 pixels) i always get a few pixels that are grey. I am trying to make these either white or black depending where on the greyscale they are. This is what i have so far:

 using UnityEngine;
 using System.Collections;
 
 public class CreateBlocks : MonoBehaviour {
 
     public Texture2D heightmap;
     private Color color;
     public int w;
     public Color[] pixels;
     
     void Start () {
     
         pixels = heightmap.GetPixels (0, 0, heightmap.width, heightmap.height);
 
         for (int x = 0; x < heightmap.width; x++){
             for (int y = 0; y < heightmap.height; y++){
                 color = pixels[(y * heightmap.height)+x];
                 GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 obj.transform.position = new Vector3 (x, y, 0);
                 obj.renderer.material.color = color;
             
                 if (color.a > 127.5f)
                 {
                     color = Color.black;
                 }
                 else
                 {
                     color = Color.white;
                    }
             }
          }
       }    
 }

But nothing really happens with the grey pixels. According to the RGBA-pallette the a-value is between 0 and 255. 255 is white and 0 is black so all the pixels that are under 127.5 should become black with my if statement, i dont see why its not working.

Thank you

//Taegos

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 getyour411 · Aug 25, 2015 at 12:36 AM 0
Share

I don't mess with this stuff so I'm just typing what comes to $$anonymous$$d - is ".a" or Alpha the same as "the Color"?

avatar image Tageos · Aug 25, 2015 at 01:51 AM 0
Share

a is one color component. The sprite has 4 componetns: rgba, i want to access a because it works in grayscale since im working in black and white.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by nesis · Aug 25, 2015 at 12:34 AM

Color is a struct. Structs are copy-by-value (like int, float, bool, and string) rather than copy-by-reference (like classes and collections).

This means that you are copying color's value to material.color, and are then changing color's value to black or white (which doesn't affect material.color).

Set color's value first, then copy it to material.color. This will make your script work.

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 Tageos · Aug 25, 2015 at 01:49 AM 0
Share

Thank you! Can you show me with an example? I have tried to do as you say but i cant really get it to work.

avatar image Tageos · Aug 25, 2015 at 02:11 AM 0
Share

Wait i got it now, thank you!

ins$$anonymous$$d of using color in the if statement i used obj.renderer.material.color = Color.black;

like this for example:

 if (0 < color.b && color.b < Color.gray.b)
                 {
                     w = w + 1;
                     obj.renderer.material.color = Color.black;
                 }

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

26 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

Related Questions

Changing Camera Background Color 1 Answer

How do you rotate camera down 45 degrees no matter the direction? 2 Answers

Lerp Not working 1 Answer

c# - error CS0103: The name `hit' does not exist in the current context (cardboard switching) 1 Answer

Clamping when Zoom changes? 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