Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Keep UVs the same when removing tris

Discussion in 'Scripting' started by zrrz, Aug 10, 2016.

  1. zrrz

    zrrz

    Joined:
    Nov 14, 2012
    Posts:
    40
    I'm trying to remove the body from a model and leave only the arms for a FPS. This works fine except the UVs get messed up. Any ideas?

    Here's the code:

    Code (CSharp):
    1.     public static void StripArmsQuick(GameObject target) {
    2.         if (target == null) {
    3.             Debug.Log("No GameObject selected");
    4.         }
    5.  
    6. //        SetTPose(target);
    7.  
    8.  
    9.         Vector3 size = new Vector3(0.45f, 5f, 0.45f);
    10.  
    11.         Bounds bounds = new Bounds(Vector3.up, size);
    12.         SkinnedMeshRenderer[] skinnedMeshRenderers = target.GetComponentsInChildren<SkinnedMeshRenderer>();
    13.         foreach (SkinnedMeshRenderer skinnedMeshRenderer in skinnedMeshRenderers) {
    14.             List<int> triangleList = new List<int>();
    15.             List<Vector3> vertList = new List<Vector3>();
    16.             List<Vector2> uvList = new List<Vector2>();
    17.             List<Vector3> normalsList = new List<Vector3>();
    18.  
    19.             Mesh mesh = new Mesh();
    20.             mesh.vertices = skinnedMeshRenderer.sharedMesh.vertices;
    21.             mesh.normals = skinnedMeshRenderer.sharedMesh.normals;
    22.             mesh.uv = skinnedMeshRenderer.sharedMesh.uv;
    23.             mesh.boneWeights = skinnedMeshRenderer.sharedMesh.boneWeights;
    24.             mesh.bindposes = skinnedMeshRenderer.sharedMesh.bindposes;
    25.  
    26.             Vector3[] vertices = mesh.vertices;
    27.  
    28.             for (int i = 0; i < vertices.Length; i++) {
    29.                 vertList.Add (vertices[i]);
    30.                 uvList.Add (mesh.uv[i]);
    31.                 normalsList.Add (mesh.normals[i]);
    32.             }
    33.  
    34.             int[] triangles = mesh.triangles;
    35.             for (int i = 0; i < triangles.Length; i += 3) {
    36.                 if (bounds.Contains(vertices[triangles[i]]) && bounds.Contains(vertices[triangles[i + 1]]) && bounds.Contains(vertices[triangles[i + 2]])) {
    37.                     //In selection
    38.                 }
    39.                 else {
    40.                     triangleList.Add(triangles[i]);
    41.                     triangleList.Add(triangles[i + 1]);
    42.                     triangleList.Add(triangles[i + 2]);
    43.                 }
    44.             }
    45.  
    46.             mesh.triangles = triangleList.ToArray();
    47.  
    48.             mesh.RecalculateNormals();
    49.             mesh.RecalculateBounds();
    50.  
    51.             skinnedMeshRenderer.sharedMesh = mesh;
    52.         }
    53.     }
     
  2. zrrz

    zrrz

    Joined:
    Nov 14, 2012
    Posts:
    40
    I gave up and did it a different way!

    Code (CSharp):
    1.     public static void HideBody(GameObject target) {
    2.         Shader cutoutShader = Shader.Find("Legacy Shaders/Transparent/Cutout/Bumped Diffuse");
    3.         if (cutoutShader == null)
    4.             Debug.LogError("Cutout shader not found");
    5.  
    6.         Vector3 size = new Vector3(0.45f, 6f, 0.45f);
    7.  
    8.         Bounds bounds = new Bounds(Vector3.up, size);
    9.         SkinnedMeshRenderer[] skinnedMeshRenderers = target.GetComponentsInChildren<SkinnedMeshRenderer>();
    10.         foreach (SkinnedMeshRenderer skinnedMeshRenderer in skinnedMeshRenderers) {
    11.             Material[] mats = skinnedMeshRenderer.materials;
    12.  
    13.             foreach (Material mat in mats)
    14.             {
    15.                 if (mat.shader != cutoutShader)
    16.                 {
    17.                     Texture diffuse = mat.GetTexture("_DiffuseMap");
    18.                     Texture normal = mat.GetTexture("_NormalsMap");
    19.  
    20.                     mat.shader = cutoutShader;
    21.                     mat.mainTexture = diffuse;
    22.                     mat.SetTexture("_BumpMap", normal);
    23.                     mat.SetFloat("_Cutoff", 1f);
    24.                 }
    25.                 Texture2D texture = Instantiate(mat.mainTexture) as Texture2D;
    26.                 int width = 10, height = 10;
    27.                 Color32[] newColors = new Color32[width*height];
    28.                 for (int i = 0; i < newColors.Length; i++)
    29.                 {
    30.                     newColors[i] = new Color32(0, 255, 0, 0);
    31.                 }
    32.                 texture.SetPixels32(0, 0, width, height, newColors);
    33.                 texture.Apply();
    34.                 mat.mainTexture = texture;
    35.             }
    36.  
    37.             Mesh mesh = skinnedMeshRenderer.sharedMesh;
    38.             Vector3[] vertices = mesh.vertices;
    39.  
    40.             int[] triangles = mesh.triangles;
    41.             Vector2[] uvs = mesh.uv;
    42.             for (int i = 0; i < triangles.Length; i += 3) {
    43.                 if (bounds.Contains(vertices[triangles[i]]) && bounds.Contains(vertices[triangles[i + 1]]) && bounds.Contains(vertices[triangles[i + 2]])) {
    44.                     uvs[triangles[i]] = new Vector2(0f, 0f);
    45.                     uvs[triangles[i+1]] = new Vector2(0.001f, 0f);
    46.                     uvs[triangles[i+2]] = new Vector2(0f, 0.001f);
    47.                 }
    48.                 else {
    49.                     //In selection
    50.                 }
    51.             }
    52.             mesh.uv = uvs;
    53.             skinnedMeshRenderer.sharedMesh = mesh;
    54.         }
    55.     }