Advertisement
evelynshilosky

MeshDataPreserver

Nov 19th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.56 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5.  
  6. public class MeshDataPreserver : MonoBehaviour
  7. {
  8.     public GameObject originalSkinnedMeshObject;
  9.     public GameObject mergedMeshObject;
  10.  
  11.     [ContextMenu("Transfer Mesh Data")]
  12.     void TransferMeshData()
  13.     {
  14.         if (originalSkinnedMeshObject == null || mergedMeshObject == null)
  15.         {
  16.             Debug.LogError("Please assign both the original and merged mesh objects.");
  17.             return;
  18.         }
  19.  
  20.         SkinnedMeshRenderer originalSMR = originalSkinnedMeshObject.GetComponent<SkinnedMeshRenderer>();
  21.         MeshFilter mergedMF = mergedMeshObject.GetComponent<MeshFilter>();
  22.  
  23.         if (originalSMR == null || mergedMF == null)
  24.         {
  25.             Debug.LogError("Original object must have SkinnedMeshRenderer and merged object must have MeshFilter.");
  26.             return;
  27.         }
  28.  
  29.         Mesh originalMesh = originalSMR.sharedMesh;
  30.         Mesh mergedMesh = mergedMF.sharedMesh;
  31.  
  32.         // Create a new mesh to store the updated data
  33.         Mesh newMesh = new Mesh();
  34.         newMesh.name = "UpdatedMergedMesh";
  35.  
  36.         // Copy vertex data
  37.         newMesh.vertices = mergedMesh.vertices;
  38.         newMesh.normals = mergedMesh.normals;
  39.         newMesh.uv = mergedMesh.uv;
  40.         newMesh.tangents = mergedMesh.tangents;
  41.         newMesh.colors = mergedMesh.colors;
  42.         newMesh.triangles = mergedMesh.triangles;
  43.  
  44.         // Transfer bone weights
  45.         if (originalMesh.boneWeights.Length > 0)
  46.         {
  47.             BoneWeight[] newBoneWeights = new BoneWeight[mergedMesh.vertexCount];
  48.             for (int i = 0; i < mergedMesh.vertexCount; i++)
  49.             {
  50.                 newBoneWeights[i] = originalMesh.boneWeights[i % originalMesh.vertexCount];
  51.             }
  52.             newMesh.boneWeights = newBoneWeights;
  53.         }
  54.  
  55.         // Transfer bind poses
  56.         if (originalMesh.bindposes.Length > 0)
  57.         {
  58.             newMesh.bindposes = originalMesh.bindposes;
  59.         }
  60.  
  61.         // Transfer blend shapes
  62.         if (originalMesh.blendShapeCount > 0)
  63.         {
  64.             for (int i = 0; i < originalMesh.blendShapeCount; i++)
  65.             {
  66.                 string shapeName = originalMesh.GetBlendShapeName(i);
  67.                 int frameCount = originalMesh.GetBlendShapeFrameCount(i);
  68.  
  69.                 for (int j = 0; j < frameCount; j++)
  70.                 {
  71.                     Vector3[] deltaVertices = new Vector3[mergedMesh.vertexCount];
  72.                     Vector3[] deltaNormals = new Vector3[mergedMesh.vertexCount];
  73.                     Vector3[] deltaTangents = new Vector3[mergedMesh.vertexCount];
  74.  
  75.                     Vector3[] originalDeltaVertices = new Vector3[originalMesh.vertexCount];
  76.                     Vector3[] originalDeltaNormals = new Vector3[originalMesh.vertexCount];
  77.                     Vector3[] originalDeltaTangents = new Vector3[originalMesh.vertexCount];
  78.  
  79.                     originalMesh.GetBlendShapeFrameVertices(i, j, originalDeltaVertices, originalDeltaNormals, originalDeltaTangents);
  80.  
  81.                     for (int k = 0; k < mergedMesh.vertexCount; k++)
  82.                     {
  83.                         int originalIndex = k % originalMesh.vertexCount;
  84.                         deltaVertices[k] = originalDeltaVertices[originalIndex];
  85.                         deltaNormals[k] = originalDeltaNormals[originalIndex];
  86.                         deltaTangents[k] = originalDeltaTangents[originalIndex];
  87.                     }
  88.  
  89.                     float weight = originalMesh.GetBlendShapeFrameWeight(i, j);
  90.                     newMesh.AddBlendShapeFrame(shapeName, weight, deltaVertices, deltaNormals, deltaTangents);
  91.                 }
  92.             }
  93.         }
  94.  
  95.         // Add SkinnedMeshRenderer if it doesn't exist, otherwise update the existing one
  96.         SkinnedMeshRenderer newSMR = mergedMeshObject.GetComponent<SkinnedMeshRenderer>();
  97.         if (newSMR == null)
  98.         {
  99.             newSMR = mergedMeshObject.AddComponent<SkinnedMeshRenderer>();
  100.         }
  101.  
  102.         // Set the new mesh and bone data
  103.         newSMR.sharedMesh = newMesh;
  104.         newSMR.bones = originalSMR.bones;
  105.         newSMR.rootBone = originalSMR.rootBone;
  106.  
  107.         // Keep the MeshFilter, but update its shared mesh
  108.         mergedMF.sharedMesh = newMesh;
  109.  
  110.         // Save the new mesh as an asset
  111.         AssetDatabase.CreateAsset(newMesh, "Assets/UpdatedMergedMesh.asset");
  112.         AssetDatabase.SaveAssets();
  113.  
  114.         Debug.Log("Mesh data transferred and SkinnedMeshRenderer updated on merged object.");
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement