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 yoger · Aug 02, 2013 at 09:28 AM · texturealphaplaneclipping

X-raying plane to see underneath

I basically have two planes, which are just a maps of top sight 2d game I make. So the one above is visible all the time, but sometimes I like to xray through it to show whats on the one under it. Top one has material with Alpha blend particles shader so I can actually make alpha holes in it and restore it when I want it to. But is there something that works same way but more smooth and can be easier adjustable if I'd like to assign it to, lets say, rotating game object?

     public Camera camera;
     public GameObject frontPlane;
     public GameObject backPlane;
         
     private Color[] _colorlBackup;
     private Color _color;
     private Texture2D tex;
     
     
     // Use this for initialization
     void Start () {
         _timer = 0;
         _color = new Color(0.0f, 0.0f, 0.0f, 0.0f);
     }
     
     private void GetPixels(){
         
         RaycastHit hit;
         Ray ray = camera.ScreenPointToRay (Input.mousePosition);
         
         if(Physics.Raycast(ray, out hit)){
                 
             //Get texture of hitted collider 
             tex = (Texture2D)hit.transform.gameObject.renderer.sharedMaterial.mainTexture;
             
             
             //Backup texture color array if its empty
             if(_colorlBackup == null){
                 Debug.Log ("Tak");
                 _colorlBackup = tex.GetPixels();
             }
             
             else if(_colorlBackup != null) {
                 Debug.Log ("Drawing...");
                 
                 
                 //Get UV's at the hit.point
                 Vector2 uv;
                 
                 uv.x = (int)(((hit.point.x - hit.collider.bounds.min.x) / hit.collider.bounds.size.x) * tex.width);
                 uv.y = (int)(((hit.point.z - hit.collider.bounds.min.z) / hit.collider.bounds.size.z) * tex.height);
                 
                 StartCoroutine("SetPixelsToChange", uv);
                 
             }
         }
     }
     
     private IEnumerator SetPixelsToChange(Vector2 uv){
         Vector2[] pixelsToChange = new Vector2[15000];
         
         int cnt = 0;
         for( int y = 0; y < 100; y++ ){
             for( int x = 0; x < y; x++){
                 pixelsToChange[cnt] = new Vector2(uv.x + x, uv.y + y);
                 pixelsToChange[cnt+1] = new Vector2(uv.x - x, uv.y + y);
                 cnt += 2;
             }
         }
         if(cnt >= 1000){
             for( int i = 0; i < pixelsToChange.Length; i++)
                 tex.SetPixel ((int)(-pixelsToChange[i].x), (int)(-pixelsToChange[i].y), _color);
             
                 cnt--;
         }
         
         tex.Apply();
         yield return new WaitForSeconds(.05f);
             tex.SetPixels(_colorlBackup);
             tex.Apply();
         StopCoroutine("SetPixelsToChange");
         
     }
     
     
     
     // Update is called once per frame
     void Update () {
         
             GetPixels();
         
     }
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
0
Best Answer

Answer by CHPedersen · Aug 02, 2013 at 09:47 AM

The issue here is that you're doing all the work CPU side. Looping over pixels on the CPU is expensive, especially for larger textures, and the calls to tex.Apply are really expensive, because they have to transfer the texture back to the GPU (so you incur the cost of passing data through the PCI bus).

The only way to do this efficiently is to move all the calculations to the GPU instead to avoid the heavy pixel-looping and the transfer of modified textures. Either use a custom shader for it, or set up a system of existing shaders that yield the same result.

The custom shader option would require you to write Cg code. Do you have ShaderLab/Cg experience? If so, it will be relatively easy. You can calculate on the CPU side how big the X-ray circle is going to be on the plane, then use Material.SetFloat and Material.SetVector to pass the circle's mathematical parameters (radious and center) to the shader. In the shader's fragment program, you would then test each pixel's world position against the circle to see if it's inside or outside the circle. If inside, set its alpha to 0. If outside, render normally.

This would yield precisely the same result without having to do any work on the CPU except for evaluating the circle parameters. It's also possible there is a simpler cut-out based solution involving a white texture with an alpha-0 hole in its center, and a projector setup that projects the hole onto the plane where you want it to be transparent. That might be easier to set up.

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 yoger · Aug 02, 2013 at 09:53 AM 0
Share

As far as I don't have shader experience (just started to learn) the last solution with the projector seem the best option for me. I'll look into it. Thanks alot :]

avatar image CHPedersen · Aug 02, 2013 at 09:59 AM 0
Share

You're welcome. :) In your investigatons, search for topics that have to do with "X-ray", "projector" and "alpha-cutout". I got these links, which seem like they might be of interest to you:

http://unitycoder.com/blog/2012/02/22/x-ray-cutout-shader-with-mouse/

http://forum.unity3d.com/threads/187659-X-ray-shader-problems-need-help

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

15 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

Related Questions

Hiding sections of arbitrary models that lie one side of a plane 0 Answers

Masking a plane object (which has an animated texture offset) 1 Answer

How do I use Material/Texture alpha on a plane? 2 Answers

Texture fade in 2 Answers

Texture Alpha cutoff + Photoshop 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