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
0
Question by Astroni · Feb 14, 2013 at 06:10 PM · c#raycastmouseclickpixelalpha-channel

Get transparency of one pixel

Hi. I have C# function like this:

 void Update () {
     
     if (!Input.GetMouseButtonDown(0))
     {
         return;
     }
     
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
      RaycastHit[] hits;
             hits = Physics.RaycastAll(ray);

         for (int i=0; i < hits.Length; i++)
         {
             Debug.Log(hits[i].transform);
         }
 }

The function catchs mouse click position and lists which of object have we clicked. It's works fine, but now I want to add checking, whether the texture PNG is transparency in clicked point. Simply - when we click in invisible part of picture, function avoids this object and notices just another objects which was clicked.

I have no idea how to check one exact point properties (whats more - from raycast coordinates) and how to find the alpha channel. Could somebody prompt me something?

Thanx!

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

Answer by robertbu · Feb 14, 2013 at 08:44 PM

What you are asking for is not not normal in a 3D environment, and difficult to do. Shaders often use multiple textures. Sometimes transparency is in the texture, sometimes it is done with a mask, sometimes it is determined by the blending of multiple textures. Sometimes a texture is really an atlas of textures. In addition, an object may have a transparent area that the ray first hits, but would hit a non-transparent area as it pass through the back of the mesh. And in the end, the collider that is reporting through your Raycast and the mesh/texture are different components.

If your object/mesh is a plane, and if you are using a simple shader, and if your material offsets are set to their defaults, you might be able to do it by:

  • Translate the RaycastHit.point to local coordinates of the object

  • Translate the coordinates to a UV system (0 to 1) based on the object size.

  • Use Texture2D.GetPixel() or Texture2D.GetPixelBilinear()

  • Examine the alpha value of the color returned

As a limited alternative, note that you can make changes to the collider. You can change the sizes. You can add multiple colliders of different types to a game object. You can put colliders on empty child objects. I don't know your application, but you can create a tighter bounding for your image by manipulating the collider.

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 Astroni · Feb 19, 2013 at 02:45 PM

I'm sorry, that so late, but I eventually resolve the problem. You've right, robertbu, it was possible for so easy situation as mine - my game is quite simply, I use one texture (set in MeshRender), the transparency is wrote in picture and in Shader (Transparent/Diffuse) and every object is a plane (cause actually it is 2D game :p ).

 using UnityEngine;
 using System.Collections;
 
 public class RayCasting : MonoBehaviour {
         
     void Update () {
         
         if (!Input.GetMouseButtonDown(0))
         {
             return;
         }
         
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit[] hits = Physics.RaycastAll(ray);
         
         Vector3 v;
         bool[] transparent = new bool[hits.Length];
         
         for (int i=0; i < hits.Length; i++)
         {
             Texture2D pic = hits[i].transform.gameObject.renderer.material.mainTexture as Texture2D;
             // - Translate the RaycastHit.point to local coordinates of the object
             v = hits[i].transform.worldToLocalMatrix.MultiplyPoint(hits[i].point);
             
             // - Translate the coordinates to a UV system (0 to 1) based on the object size.
             float xPic = 10 - (v.x + 5);
             float yPic = v.y + 5;
             
             // - Use Texture2D.GetPixel() or Texture2D.GetPixelBilinear()
             // - Examine the alpha value of the color returned
             if(pic.GetPixel((int)((xPic/10)*pic.width),(int)((yPic/10)*pic.height)).a).a < 0.5)
                 transparent[i] = true;    //pixel is transparent
                 Debug.Log(hits[i].transform);
         }
         
         int res = 0;
         if(hits.Length != 0)
             res = exam(hits, transparent);

         //if front checking function found something
         //we remove it
         if(res != hits.Length)
             Destroy(hits[res].transform.gameObject);
     }

     // function which check, what is on front
     int exam(RaycastHit[] hits, bool[] t)
     {
         float tmp = 100;
         int resRay = hits.Length;
         
         for(int i = 0; i < hits.Length; i++)
         {
             if((hits[i].transform.position.z < tmp) && !t[i])
             {
                 tmp = hits[i].transform.position.z;
                 resRay = i;
             }
         }
         return resRay;
     }
 }

Funny, but the hardest was computing the coordinates from "LocalMatrix" to pixels of texture - they was set in completely different way. Maybe wiser would be to think about this UV system, but eventually it work :) So thanks, robertbu, you told me it's possible, so I give you plus.

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 robertbu · Feb 19, 2013 at 04:30 PM 0
Share

It is great you got it working. I use the word may in my answer because it worked in my head, but I'd never written the code. Sometimes when taking an idea to code, there are gotcha's along the way.

avatar image Astroni · Feb 20, 2013 at 09:43 AM 0
Share

Somebody said: "It would be almost impossible!" And somebody replied: "Loving the 'almost'." :)

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mouse Click + Raycast + Colliders 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Chek multiple raycasts? 2 Answers

Hit enemy life with raycast 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