Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Making a transparent mesh

Discussion in 'Scripting' started by liorplayim, Feb 1, 2015.

  1. liorplayim

    liorplayim

    Joined:
    Dec 15, 2014
    Posts:
    5
    Hi everyone,
    I'm trying to make a scenario in which the user touches the screen and unveil a another image.
    For example, applying a face-cream on the character face. The user needs to drag his finger (or mouse) over the face in order to apply cream on it.

    One solution I got was to make a "cream" image layer on top of the "base object" (face), and play with its mesh colors (so this mechanic could work with both 2d and 3d, as long as using meshes).
    The problem is when i'm trying to "reset" this mesh into transparent when the scene starts. Usually it functions OK, but after I restart unity , it won't always reset.

    This function is called on scene awake (sometimes works, sometimes not):
    Code (CSharp):
    1.     private void ResetMeshColors(MeshFilter tempMesh)
    2.     {
    3.         Color32[] colors = tempMesh.mesh.colors32;
    4.         for (int i = 0; i < colors.Length; i++)
    5.         {
    6.             colors[i] = new Color32(255, 255, 255, 0);
    7.         }
    8.         tempMesh.mesh.colors32 = colors;
    9.         Debug.Log ("Reset the cream to transparent");
    10.      
    11.     }


    Here is the rest of the code.
    It's using a radius and the meshes game objects, and synced to another script that check if the mesh is transparent or not.
    It works great, but it fails when the reset has failed on startup.
    Code (CSharp):
    1.  
    2.     void Update () {
    3.         if (Input.GetButton("Fire1"))
    4.         {
    5.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);                  
    6.             if (Physics.Raycast(ray , out hit))                                                
    7.             {
    8.                 if (hit.collider != null)                                          
    9.                 {
    10.                     MeshFilter targetMeshFilter = hit.collider.GetComponent<MeshFilter>();    
    11.                     if (targetMeshFilter.mesh != null)                                      
    12.                     {
    13.                             PaintMeshVertices(hit.transform, targetMeshFilter.mesh, hit.point, Color.white);                
    14.                     }
    15.                 }
    16.             }
    17.         }
    18.     }
    Code (CSharp):
    1.    
    2.     private void PaintMeshVertices(Transform paintTransform, Mesh paintTarget, Vector3 originPoint, Color colorX)
    3.     {
    4.         Color32[] colors = paintTarget.colors32;                  
    5.         Vector3[] vertices = paintTarget.vertices;                      
    6.      
    7.         bool any = false;
    8.         for (int i = 0; i < vertices.Length; i++)                  
    9.         {
    10.             Vector3 vertexInWorld = paintTransform.TransformPoint(vertices[i]);      
    11.             if (Vector3.Distance(vertexInWorld, originPoint) <= _paintRadius)    
    12.             {
    13.                 colors[i] = colorX;            
    14.                 any = true;                      
    15.             }
    16.         }
    17.      
    18.         // assign the colors (position + values) to the mesh to apply the paint (only if changes (bool "any") were made at all)
    19.         if (any)
    20.         {
    21.             paintTarget.colors32 = colors;
    22.         }
    23.     }
     
    Last edited: Feb 1, 2015
  2. liorplayim

    liorplayim

    Joined:
    Dec 15, 2014
    Posts:
    5
    BTW,
    I will be happy to hear about alternatives for this mechanic (unveil an image/texture by drag, not the whole image at once).
     
  3. liorplayim

    liorplayim

    Joined:
    Dec 15, 2014
    Posts:
    5
    anyone?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    You could paint into texture also, to make it transparent.. (as in here)

    But that vertex paint should work also,
    does that Debug.Log gets called on the ResetMeshColors everytime you start playmode or not..?

    Or do you mean it keeps the mesh colors inside unity editor, after stopping playmode and then wont reset them even if restarted unity?
     
  5. liorplayim

    liorplayim

    Joined:
    Dec 15, 2014
    Posts:
    5
    The debug.log shows "Reset the cream to transparent", so this function runs with no errors, although it's not always actually reset the alpha.

    I have a demo scene with almost those elemtns only (sprite as background a plane mesh with another texture and this script), and it runs alright there. But it's a whole different story with a busy scene. Strange...

    btw, do I need to use [Serializable] on my meshes/textures?