Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

custom skinned mesh implementation working not updating properly

Discussion in 'Scripting' started by JetStreamSham, Jun 14, 2019.

  1. JetStreamSham

    JetStreamSham

    Joined:
    Jul 4, 2015
    Posts:
    27
    I'm creating my own scripted implementation of a Skinned Mesh in unity for collision detection. But I've run into some problems whenever I update the vertex positions using the update armature function to match the white model, the blue wireframe model which is my custom implementation does match its white counterpart and is in correctly positioned. Updating the armature multiple times without changing the model's rotation will change the wiresframe's orientation.



    I'm sure I'm doing something wrong with transformation matrix, but I don't know what.

    Code (CSharp):
    1.  
    2.     using System.Collections;
    3.     using System.Collections.Generic;
    4.     using UnityEditor;
    5.     using UnityEngine;
    6.  
    7.  
    8.     public class MeshArmature : MonoBehaviour
    9.     {
    10.  
    11.         public SkinnedMeshRenderer skinnedMesh;
    12.         public Mesh mesh;
    13.         public BoneWeight[] weights;
    14.  
    15.         public Transform[] bones;
    16.         public Transform rootBone;
    17.  
    18.         DynamicMeshCollider meshCollider;
    19.  
    20.         bool manualVolumes;
    21.  
    22.         public Transform player;
    23.  
    24.         // Use this for initialization
    25.         public void Start()
    26.         {
    27.             Setup();
    28.         }
    29.         public void Setup()
    30.         {
    31.             skinnedMesh = GetComponentInChildren<SkinnedMeshRenderer>();
    32.             mesh = new Mesh();
    33.             skinnedMesh.BakeMesh(mesh);
    34.             mesh.name = skinnedMesh.sharedMesh.name;
    35.             bones = skinnedMesh.bones;
    36.             rootBone = skinnedMesh.rootBone;
    37.             weights = skinnedMesh.sharedMesh.boneWeights;
    38.  
    39.  
    40.  
    41.             meshCollider = GetComponent<DynamicMeshCollider>();
    42.             meshCollider.mesh = mesh;
    43.             meshCollider.Setup();
    44.  
    45.  
    46.         }
    47.  
    48.         public void UpdateArmature()
    49.         {
    50.             List<Vector3> verts = new List<Vector3>();
    51.             mesh.GetVertices(verts);
    52.  
    53.             for (int i  = 0; i < mesh.vertices.Length; i++)
    54.             {
    55.                 Vector3 vert = new Vector4(verts[i].x, verts[i].y, verts[i].z);
    56.                 Matrix4x4 trs = Matrix4x4.TRS(bones[weights[i].boneIndex0].position, bones[weights[i].boneIndex0].rotation, bones[weights[i].boneIndex0].localScale);
    57.                 vert =  trs.MultiplyPoint(vert) * weights[i].weight0;
    58.                 verts[i] = vert;
    59.  
    60.             }
    61.  
    62.             meshCollider.mesh.SetVertices(verts);
    63.  
    64.         }
    65.  
    66.     }
    67.  
    68.  
    69. //Render Code For Blue Mesh
    70.  
    71.    public class DMAGizmoDrawer
    72.      {
    73.          [DrawGizmo(GizmoType.Selected | GizmoType.Active)]
    74.          static void DrawGizmo(MeshArmature arm, GizmoType type)
    75.          {
    76.              Gizmos.color = Color.blue;
    77.              if (arm.mesh)
    78.                  Gizmos.DrawWireMesh(arm.mesh, arm.transform.position);
    79.  
    80.          }
    81.      }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    I see there are some errors in console. First of all get rid of errors in console, then check your mesh again. Unity stops frame processing when exception is raised leaving your game in invalid state. You can't assume your scripts working right or wrong if there are any exceptions in console.
     
  3. JetStreamSham

    JetStreamSham

    Joined:
    Jul 4, 2015
    Posts:
    27
    The errors in the console are old and unrelated, caused by the mesh property in the armature being null which it is not in the picture.