Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- public class MeshDataPreserver : MonoBehaviour
- {
- public GameObject originalSkinnedMeshObject;
- public GameObject mergedMeshObject;
- [ContextMenu("Transfer Mesh Data")]
- void TransferMeshData()
- {
- if (originalSkinnedMeshObject == null || mergedMeshObject == null)
- {
- Debug.LogError("Please assign both the original and merged mesh objects.");
- return;
- }
- SkinnedMeshRenderer originalSMR = originalSkinnedMeshObject.GetComponent<SkinnedMeshRenderer>();
- MeshFilter mergedMF = mergedMeshObject.GetComponent<MeshFilter>();
- if (originalSMR == null || mergedMF == null)
- {
- Debug.LogError("Original object must have SkinnedMeshRenderer and merged object must have MeshFilter.");
- return;
- }
- Mesh originalMesh = originalSMR.sharedMesh;
- Mesh mergedMesh = mergedMF.sharedMesh;
- // Create a new mesh to store the updated data
- Mesh newMesh = new Mesh();
- newMesh.name = "UpdatedMergedMesh";
- // Copy vertex data
- newMesh.vertices = mergedMesh.vertices;
- newMesh.normals = mergedMesh.normals;
- newMesh.uv = mergedMesh.uv;
- newMesh.tangents = mergedMesh.tangents;
- newMesh.colors = mergedMesh.colors;
- newMesh.triangles = mergedMesh.triangles;
- // Transfer bone weights
- if (originalMesh.boneWeights.Length > 0)
- {
- BoneWeight[] newBoneWeights = new BoneWeight[mergedMesh.vertexCount];
- for (int i = 0; i < mergedMesh.vertexCount; i++)
- {
- newBoneWeights[i] = originalMesh.boneWeights[i % originalMesh.vertexCount];
- }
- newMesh.boneWeights = newBoneWeights;
- }
- // Transfer bind poses
- if (originalMesh.bindposes.Length > 0)
- {
- newMesh.bindposes = originalMesh.bindposes;
- }
- // Transfer blend shapes
- if (originalMesh.blendShapeCount > 0)
- {
- for (int i = 0; i < originalMesh.blendShapeCount; i++)
- {
- string shapeName = originalMesh.GetBlendShapeName(i);
- int frameCount = originalMesh.GetBlendShapeFrameCount(i);
- for (int j = 0; j < frameCount; j++)
- {
- Vector3[] deltaVertices = new Vector3[mergedMesh.vertexCount];
- Vector3[] deltaNormals = new Vector3[mergedMesh.vertexCount];
- Vector3[] deltaTangents = new Vector3[mergedMesh.vertexCount];
- Vector3[] originalDeltaVertices = new Vector3[originalMesh.vertexCount];
- Vector3[] originalDeltaNormals = new Vector3[originalMesh.vertexCount];
- Vector3[] originalDeltaTangents = new Vector3[originalMesh.vertexCount];
- originalMesh.GetBlendShapeFrameVertices(i, j, originalDeltaVertices, originalDeltaNormals, originalDeltaTangents);
- for (int k = 0; k < mergedMesh.vertexCount; k++)
- {
- int originalIndex = k % originalMesh.vertexCount;
- deltaVertices[k] = originalDeltaVertices[originalIndex];
- deltaNormals[k] = originalDeltaNormals[originalIndex];
- deltaTangents[k] = originalDeltaTangents[originalIndex];
- }
- float weight = originalMesh.GetBlendShapeFrameWeight(i, j);
- newMesh.AddBlendShapeFrame(shapeName, weight, deltaVertices, deltaNormals, deltaTangents);
- }
- }
- }
- // Add SkinnedMeshRenderer if it doesn't exist, otherwise update the existing one
- SkinnedMeshRenderer newSMR = mergedMeshObject.GetComponent<SkinnedMeshRenderer>();
- if (newSMR == null)
- {
- newSMR = mergedMeshObject.AddComponent<SkinnedMeshRenderer>();
- }
- // Set the new mesh and bone data
- newSMR.sharedMesh = newMesh;
- newSMR.bones = originalSMR.bones;
- newSMR.rootBone = originalSMR.rootBone;
- // Keep the MeshFilter, but update its shared mesh
- mergedMF.sharedMesh = newMesh;
- // Save the new mesh as an asset
- AssetDatabase.CreateAsset(newMesh, "Assets/UpdatedMergedMesh.asset");
- AssetDatabase.SaveAssets();
- Debug.Log("Mesh data transferred and SkinnedMeshRenderer updated on merged object.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement