0
\$\begingroup\$

I was trying to clone a rigged mesh via script and change its orientation as the characters face wasn't facing at forward direction. So I created a Editor Script for that:

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

[CustomEditor(typeof(MeshPositionChanger))]
public class MeshPositionChangerEditorScript : Editor
{
private Mesh _initMesh;
public override void OnInspectorGUI()
{
    base.OnInspectorGUI();
    MeshPositionChanger temp = target as MeshPositionChanger;
    EditorGUILayout.Space(30);
    if (GUILayout.Button("CreateDuplicate")) { CreateDuplicate(temp); };
    EditorGUILayout.Space(30);
    if (GUILayout.Button("Revert")) { Revert(temp); };


}

private void CreateDuplicate(MeshPositionChanger temp)
{
    if (_initMesh == null)
    {
        _initMesh = temp.SkinnedMesh.sharedMesh;
    }
    Mesh mesh = new Mesh();
    SkinnedMeshRenderer skm = temp.SkinnedMesh;
    Mesh referenceMesh = skm.sharedMesh;
    mesh.vertices = new Vector3[referenceMesh.vertices.Length];
    for (int i = 0; i < mesh.vertices.Length; i++)
    {
        mesh.vertices[i] = referenceMesh.vertices[i];
    }
    mesh.normals = new Vector3[referenceMesh.normals.Length];
    for (int i = 0; i < mesh.normals.Length; i++)
    {
        mesh.normals[i] = referenceMesh.normals[i];
    }
    mesh.boneWeights = new BoneWeight[referenceMesh.boneWeights.Length];
    for(int i = 0; i < mesh.boneWeights.Length; i++)
    {
        mesh.boneWeights[i] = referenceMesh.boneWeights[i];
    }
    mesh.triangles = new int[referenceMesh.triangles.Length];
    for(int i = 0; i < mesh.triangles.Length; i++)
    {
        mesh.triangles[i] = referenceMesh.triangles[i];
    }
    mesh.bounds = referenceMesh.bounds;
    mesh.tangents = new Vector4[referenceMesh.tangents.Length];
    for(int i = 0; i < mesh.tangents.Length; i++)
    {
        mesh.tangents[i] = referenceMesh.tangents[i];
    }
    mesh.uv = new Vector2[referenceMesh.uv.Length];
    for(int i = 0; i < mesh.uv.Length; i++)
    {
        mesh.uv[i] = referenceMesh.uv[i];
    }
    mesh.bindposes = new Matrix4x4[referenceMesh.bindposes.Length];
    for(int i = 0; i < mesh.bindposes.Length; i++)
    {
        mesh.bindposes[i] = referenceMesh.bindposes[i];
    }
    mesh.colors = new Color[referenceMesh.colors.Length];
    for(int i = 0; i < mesh.colors.Length; i++)
    {
        mesh.colors[i] = referenceMesh.colors[i];
    }
   
    
    mesh.name = referenceMesh.name + "newCreated";
    RotateMesh(temp.RotationFlip, mesh);
    temp.SkinnedMesh.sharedMesh = mesh;
}

private void RotateMesh(Vector3 rotation, Mesh mesh)
{
    Quaternion quatrot = Quaternion.AngleAxis(rotation.x, Vector3.right);
    quatrot *= Quaternion.AngleAxis(rotation.y, Vector3.up);
    quatrot *= Quaternion.AngleAxis(rotation.z, Vector3.forward);
    for(int i = 0; i < mesh.vertices.Length; i++)
    {
        mesh.vertices[i] = quatrot*mesh.vertices[i];
    }
    for (int i = 0; i < mesh.normals.Length; i++)
    {
        mesh.normals[i] = quatrot * mesh.normals[i];
    }
    for (int i = 0; i < mesh.tangents.Length; i++)
    {
        mesh.tangents[i] = quatrot * mesh.tangents[i];
    }

}

private void Revert(MeshPositionChanger temp)
{
    temp.SkinnedMesh.sharedMesh = _initMesh;
}
}

and another one:

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

public class MeshPositionChanger : MonoBehaviour
{
    public Vector3 RotationFlip;
    public SkinnedMeshRenderer SkinnedMesh;

}

As I click the create button it clones the mesh but the mesh isn't rendered, can anyone help me out on what I did wrong here. I thought the problem was due to rotation and didn't call the rotationmesh function,but that didn't help either.

\$\endgroup\$
2
  • \$\begingroup\$ Did you call UploadMeshData to make your new data available to the GPU? \$\endgroup\$
    – DMGregory
    May 26 at 10:24
  • \$\begingroup\$ I didn't call that before, Though it seems to not work even when uploadmeshdata is called. \$\endgroup\$
    – Bipin467
    May 26 at 10:39

0

You must log in to answer this question.

Browse other questions tagged .