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

Hide a part of a mesh

Discussion in 'Scripting' started by Acatist, Mar 13, 2020.

  1. Acatist

    Acatist

    Joined:
    Jun 7, 2016
    Posts:
    45
    Hi everyone. I have been reading a lot about this topic in other Unity forums, I am trying to make a clothing equipment system, if I combine two parts of clothing (t-shirt and pants, for example) from two different sets, these overlap and it is seen that the pants go through the mesh of the shirt. In these articles, I have read that you can hide the mesh by setting the alpha channel of the desired vertex color to zero. And in the attached image, it is seen working (however, he did not leave any script to guide me). I tried to reproduce it and follow the steps , but could not get it to work in any way. So my question is: Does anyone know that they can fail? (I'll leave the example script I tried to use)
    Also, does anyone know of some other method that "cuts the mesh" in the same precise way or more? Since I only want to hide what is necessary so that the clothes do not overlap. (Note: I have already tried using UMA2 and I do not plan to use it)

    The link to the other forum: https://answers.unity.com/questions/344487/hiding-triangles-on-a-mesh-via-shader-script.html
    HideMesh.jpg
    MyScript
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TestCutMesh : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private MeshFilter bigPMesh;
    9.     [SerializeField]
    10.     private Mesh rawBigPMesh;
    11.     [SerializeField]
    12.     private Color[] vertexColors;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         rawBigPMesh = bigPMesh.mesh;
    18.         vertexColors = new Color[rawBigPMesh.vertexCount];
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         rawBigPMesh.colors = vertexColors;
    25.     }
    26. }
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    I'm not sure if Vertex alpha is such a good way to do it, since vertex colors are not generally supported by Unity's default shaders. I think it might be possible if you wrote a custom shader for that, though. Marking which vertexes that you'd want to update would be tedious. By the way, Are you implementing this for desktop or mobile?

    Another way to accomplish this would be to use an alpha-mask shader. Then you could create a mask texture that has parts of the main texture that you want to hide. You wouldn't have to update individual vertices with this.

    Another way is to separate any geometry that you may want to hide into separate submeshes. Then you could simply enable/disable these at will. This would probably be the easiest way, because you'd be doing most of the work in your 3D modelling app, rather than coding.
     
    Last edited: Mar 13, 2020
  3. Acatist

    Acatist

    Joined:
    Jun 7, 2016
    Posts:
    45
    True, but the problem is that the clothes I have I bought from different authors, I would have to make mask maps for each set of clothes, since they have textures with different distributions, and I would have to make an infinity of mask maps for each possibility.