Duplicating a mesh at runtime but removing most of it's triangles

Hey Folks

I’m sorry I’m not a good coder, I’m an animator by trade and I’m trying something here.

I’ve got a high res character mesh and I wanna duplicate it and remove almost all but a certain set of triangles.

I’ve got all of it working with a complete copy of the mesh, but as soon as I try set mesh.triangles to my new small Int Array i get this error.

Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 15, VertexCount: 15606
UnityEngine.Mesh:set_triangles(Int32[ ])

I’ve seen a few tutorials like this one where this lady just sets mesh.triangles as the array with less stuff in it and it removes them just fine, so I though I’d be fine!

This is my horrible, terrible code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class CreatePatches : MonoBehaviour {

    private SkinnedMeshRenderer m_skinnedMeshRenderer = null;
    private SkinnedMeshRenderer m_skinnedMeshRendererPatches = null;
    private Mesh m_SourceMesh = null;
    private GameObject m_PatchesObject;
    private Mesh m_NewMesh;

    // PLACEHOLDER TEST FOR WHEN PASSING IN THE TRAIGNLE INDICES 
    private int[] resultTris = new int[15] {14592, 14593, 14594, 34314, 34315, 34316, 11607, 11608, 11609, 33405, 33406, 33407, 2562, 2563, 2564};

    public Material m_PatchesMaterial;

    void Awake () {

        m_skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer> ();
        m_SourceMesh = m_skinnedMeshRenderer.sharedMesh;

        // CREATE NEW GAME OBJECT AND PARENT
        m_PatchesObject = new GameObject (gameObject.name + "_patches");
        m_PatchesObject.transform.parent = gameObject.transform.parent;

        // get original data
        Vector3[] sourceVerts = m_SourceMesh.vertices;
        int[] sourceTris = m_SourceMesh.triangles;

        // COPY MESH AND SKINNING
        m_NewMesh = new Mesh();
        m_NewMesh.name = (m_SourceMesh.name + "_patches");
        m_NewMesh.vertices = sourceVerts;
        m_NewMesh.bounds = m_SourceMesh.bounds;
        m_NewMesh.uv = m_SourceMesh.uv;
        m_NewMesh.uv2 = m_SourceMesh.uv2;
        m_NewMesh.normals = m_SourceMesh.normals;
        m_NewMesh.colors = m_SourceMesh.colors;
        m_NewMesh.tangents = m_SourceMesh.tangents;
        //m_NewMesh.triangles = sourceTris; // THIS IS FINE
        m_NewMesh.triangles = resultTris; // THIS IS WHERE IT BREAKS
        m_NewMesh.bindposes = m_SourceMesh.bindposes;
        m_NewMesh.boneWeights = m_SourceMesh.boneWeights;

        // copy over blendhapes
        Vector3[] dVertices = new Vector3[m_SourceMesh.vertexCount];
        Vector3[] dNormals = new Vector3[m_SourceMesh.vertexCount];
        Vector3[] dTangents= new Vector3[m_SourceMesh.vertexCount];
        for (int shape = 0; shape < m_SourceMesh.blendShapeCount; shape++) {
            for (int frame = 0; frame < m_SourceMesh.GetBlendShapeFrameCount(shape); frame++) {
                string shapeName = m_SourceMesh.GetBlendShapeName(shape);
                float frameWeight = m_SourceMesh.GetBlendShapeFrameWeight(shape, frame);

                m_SourceMesh.GetBlendShapeFrameVertices(shape, frame, dVertices, dNormals, dTangents);
                m_NewMesh.AddBlendShapeFrame(shapeName, frameWeight, dVertices, dNormals, dTangents);
            }
        }


        // ADDS SKINNED MESH RENDERER COMPONENT AND COPIES OVER THE PROPERTIES
        m_skinnedMeshRendererPatches = m_PatchesObject.AddComponent<SkinnedMeshRenderer> ();

        m_skinnedMeshRendererPatches.sharedMesh = m_NewMesh;
        m_skinnedMeshRendererPatches.rootBone = m_skinnedMeshRenderer.rootBone;
        m_skinnedMeshRendererPatches.bones = m_skinnedMeshRenderer.bones;
        m_skinnedMeshRendererPatches.localBounds = m_skinnedMeshRenderer.localBounds;
        m_skinnedMeshRendererPatches.quality = m_skinnedMeshRenderer.quality;
        m_skinnedMeshRendererPatches.material = m_PatchesMaterial

    }

thanks a lot

There are 15606 verts, but your result array tries to use vertex numbers 34314, 34315, 34316, and others that are out of renge

Thanks, seemed I still hadnt grasped how mesh.triangles and mesh.vertices worked but I do now! cheers!