Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

c# - How can I split a sprite mesh into two seperate sprite meshs?

So I’m using Unity.

I’ll try and explain. The mesh is:

Sprite sprite = m_SpriteRenderer.sprite;

I am trying to store half of its triangles and vertices in four separate variables:

       //First half
       ushort[] triA = triangles.Take(triangles.Length / 2).ToArray();
       //Second Half
       ushort[] triB = triangles.Skip(triangles.Length / 2).ToArray(); 
       
       //First half
       Vector2[] spriteA = spriteVertices.Take(spriteVertices.Length / 2).ToArray();
       //Second Half
       Vector2[] spriteB = spriteVertices.Skip(spriteVertices.Length / 2).ToArray();

Then, I instantiate those variables individually:

        GameObject a = Instantiate(gameObject);
        //Stops infinite loop
        a.GetComponent<SplitCircle>().enabled = false;
 
        /*
         * Error of Invalid triangle index array. 
         * Some indices are referencing out of bounds vertices, 
         * IndexCount: 189, VertexCount: 64
         */
        a.GetComponent<SpriteRenderer>().sprite.OverrideGeometry(spriteA, triA);

For simplicity, I'm only focused on the first half of the mesh. My problem is understanding whether or not what I am doing makes sense. Do I even need to store the triangle? I also don’t understand how my triangle is referencing out of bound vertices.

I hope that clears.

Note: I know I can just create smaller game objects out of the sprite and create the illusion of a split but I wanted something more quality for what I'm doing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...