Save mesh resources and point set txt files in unity and realize object mirroring

For an imported object in fbx or obj format, additionally save its mesh resource, or save the STL coordinates of its internal points for research. The code can be mounted on the fbx or obj object. In addition, this code can randomly and continuously change the vertex coordinates of the object to form an animation effect (see the shielded part in the update in the code). The mirroring of objects requires two operations: the inversion of Vertices and the inversion of the rendering order of Triangles. RandoMeshmVertices.cs is as follows:

// RandoMeshmVertices.cs
// 随机修改Mesh顶点坐标

using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

public class RandoMeshmVertices : MonoBehaviour
{
    // Mesh的实例
    MeshFilter meshFilter;

    // 顶点的原始坐标
    Vector3[] originalVertices;

   
    void Start()
    {
        meshFilter = GetComponent<MeshFilter>();
//        string name="test";
//        AssetDatabase.CreateAsset(meshFilter.mesh, "Assets/" + name + ".asset");//mesh可直接保存
        originalVertices = meshFilter.mesh.vertices;
//        originalNornals = meshFilter.mesh.normals;

        Vector3 meshpos = gameObject.transform.GetComponent<MeshFilter>().mesh.bounds.center;
        Debug.Log($"FBX's center is {meshpos}");
        //  根据mesh在fbx模型中的相对坐标(即中心点)获取到绝对世界坐标
        Vector3 mypos = gameObject.transform.TransformPoint(meshpos);
        Debug.Log($"FBX's world coordinate is {mypos}");
        Vector3 meshsize = gameObject.transform.GetComponent<MeshFilter>().mesh.bounds.size;
        Debug.Log($"FBX's size is {meshsize}");       
        string path = Application.dataPath + "stl.txt";
        FileStream fs = File.OpenWrite(path);
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < originalVertices.Length; ++i)
        {
            originalVertices[i].x = -originalVertices[i].x;

            sb.Append(originalVertices[i]);
            //           Debug.Log($"originalVertices are {originalVertices[i]}\n");
            sb.AppendLine();
        }
        byte[] stl = Encoding.UTF8.GetBytes(sb.ToString());
        fs.Write(stl,0,stl.Length);
        fs.Close();
        fs.Dispose();
        //将所有面的顶点渲染顺序颠倒
        int[] backupTriangles = meshFilter.mesh.triangles;
        int[] NewTriangles=new int[backupTriangles.Length];
        for (int i = 0; i < backupTriangles.Length; i++)
        {
            NewTriangles[NewTriangles.Length - i - 1] = backupTriangles[i];
        }
        meshFilter.mesh.vertices = originalVertices;
        meshFilter.mesh.triangles = NewTriangles;
        meshFilter.mesh.RecalculateNormals();

    }

    void Update()
    {
 /*       // 随机修改顶点坐标
        Vector3[] vertices = meshFilter.mesh.vertices;
        for (int i = 0; i < originalVertices.Length; ++i)
        {
            var v = originalVertices[i];
            vertices[i] = v + Random.Range(-1.1F,1.1F) * Vector3.one;
        }
        meshFilter.mesh.vertices = vertices;
        meshFilter.mesh.RecalculateNormals();*/
    }
}

Before mirroring:

Mirror effect after running

Guess you like

Origin blog.csdn.net/weixin_44345862/article/details/127268113