Search Unity

Mesh To SkinMesh Renderer

Discussion in 'Scripting' started by kilik128, Dec 28, 2016.

  1. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    876
    Any one can help please got Strange result

    when assign mesh.boneweights


    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Collections;
    6. using System.Linq;
    7. using UnityEngine;
    8.  
    9. publicclassMesh_To_SkinRenderer:MonoBehaviour{
    10.  
    11.  
    12. publicMeshFiltermf;
    13. publicMeshRenderermr;
    14.  
    15. publicSkinnedMeshRendererski;
    16. //Usethisfor initialization
    17. voidStart(){
    18.  
    19.  
    20. GameObjectbn=newGameObject();
    21. bn.name="roots";
    22.  
    23.  
    24. mf=gameObject.GetComponent<MeshFilter>();
    25. mr=gameObject.GetComponent<MeshRenderer>();
    26.  
    27. Meshmesh=mf.mesh;
    28.  
    29. GameObjectn=newGameObject();
    30. n.name=name;
    31. n.transform.parent=bn.transform;
    32.  
    33. SkinnedMeshRendererski=n.AddComponent<SkinnedMeshRenderer>();
    34. ski.materials=mr.materials;
    35.  
    36. List<Transform>bon=newList<Transform>();
    37. List<Matrix4x4>bindPose=newList<Matrix4x4>();
    38.  
    39. Vector3p1=newVector3(mr.bounds.center.x,mr.bounds.min.y,mr.bounds.center.z);
    40. Vector3p2=newVector3(mr.bounds.center.x,mr.bounds.center.y,mr.bounds.center.z);
    41. Vector3p3=newVector3(mr.bounds.center.x,mr.bounds.max.y,mr.bounds.center.z);
    42.  
    43.  
    44.  
    45. GameObjectbone_base=newGameObject();
    46. bone_base.name="bone_base";
    47. bone_base.transform.parent=bn.transform;
    48. bon.Add(bone_base.transform);
    49. bon[0].localRotation=Quaternion.identity;
    50. bon[0].localPosition=p1;
    51.  
    52. bindPose.Add(bone_base.transform.transform.worldToLocalMatrix*bn.transform.localToWorldMatrix);
    53.  
    54.  
    55. GameObjectbone_center=newGameObject();
    56. bone_center.name="bone_center";
    57. bone_center.transform.parent=bone_base.transform;
    58. bon.Add(bone_center.transform);
    59. bon[1].localRotation=Quaternion.identity;
    60. bon[1].localPosition=p2;
    61. bindPose.Add(bone_center.transform.transform.worldToLocalMatrix*bn.transform.localToWorldMatrix);
    62.  
    63. GameObjectbone_up=newGameObject();
    64. bone_up.name="bone_up";
    65. bone_up.transform.parent=bone_center.transform;
    66. bon.Add(bone_up.transform);
    67. bon[2].localRotation=Quaternion.identity;
    68. bon[2].localPosition=p3;
    69. bindPose.Add(bone_up.transform.transform.worldToLocalMatrix*bn.transform.localToWorldMatrix);
    70.  
    71.  
    72.  
    73. List<BoneWeight>poids=newList<BoneWeight>();
    74.  
    75. for(inti=0;i<mesh.vertices.Length;i++){
    76.  
    77. BoneWeightbb=newBoneWeight();
    78.  
    79.  
    80. bb.boneIndex0=0;
    81. bb.weight0=1;
    82. poids.Add(bb);
    83.  
    84.  
    85. }
    86.  
    87. ski.bones=bon.ToArray();
    88.  
    89. mesh.boneWeights=poids.ToArray();
    90.  
    91. ski.sharedMesh=mesh;
    92.  
    93. }
    94.  
    95.  
    96. //Updateiscalledonceper frame
    97. voidUpdate(){
    98.  
    99. }
    100.  
    101.  
    102. publicGameObjectproche(List<GameObject>arrayPlayers,Vector3pos){
    103.  
    104. GameObjectcandidatePlayer=null;
    105. floatdistance=1000.0f;
    106.  
    107. foreach(GameObject player inarrayPlayers){
    108.  
    109.  
    110. floatnewdistance=Vector3.Distance(player.transform.position,pos);
    111.  
    112. if(newdistance<distance){
    113.  
    114. distance=newdistance;
    115. candidatePlayer=player;
    116.  
    117. }
    118. }
    119.  
    120.  
    121.  
    122. returncandidatePlayer;
    123.  
    124. }
    125.  
    126. }
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    Your indentation is horribly wrong, please redo that part
     
  3. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    876
    the question is why got this result


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections;
    4. using System.Linq;
    5. using UnityEngine;
    6.  
    7. public class Mesh_To_SkinRenderer : MonoBehaviour {
    8.  
    9.  
    10.     public MeshFilter mf;
    11.     public MeshRenderer mr;
    12.  
    13.     public SkinnedMeshRenderer ski;
    14.     // Use this for initialization
    15.     void Start () {
    16.  
    17.  
    18.         GameObject bn = new GameObject ();
    19.         bn.name = "roots";
    20.  
    21.  
    22.         mf = gameObject.GetComponent<MeshFilter> ();
    23.         mr = gameObject.GetComponent<MeshRenderer> ();
    24.  
    25.         Mesh mesh = mf.mesh;
    26.  
    27.         GameObject n = new GameObject ();
    28.         n.name = name;
    29.         n.transform.parent = bn.transform;
    30.  
    31.         ski =     n.AddComponent<SkinnedMeshRenderer> ();
    32.         ski.materials = mr.materials;
    33.  
    34.         List<Transform>  bon = new List<Transform>();
    35.         List<Matrix4x4>  bindPose = new List<Matrix4x4>();
    36.  
    37.         Vector3 p1  = new Vector3 (mr.bounds.center.x, mr.bounds.min.y, mr.bounds.center.z);
    38.         Vector3 p2  = new Vector3 (mr.bounds.center.x, mr.bounds.center.y, mr.bounds.center.z);
    39.         Vector3 p3  = new Vector3 (mr.bounds.center.x, mr.bounds.max.y, mr.bounds.center.z);
    40.  
    41.  
    42.  
    43.         GameObject bone_base = new GameObject ();
    44.         bone_base.name = "bone_base";
    45.         bone_base.transform.parent = bn.transform;
    46.         bon.Add (bone_base.transform);
    47.         bon [0].localRotation = Quaternion.identity;
    48.         bon [0].localPosition = p1;
    49.  
    50.         bindPose.Add(bone_base.transform.transform.worldToLocalMatrix * ski.transform.localToWorldMatrix );
    51.  
    52.  
    53.         GameObject bone_center = new GameObject ();
    54.         bone_center.name = "bone_center";
    55.         bone_center.transform.parent = bone_base.transform;
    56.         bon.Add (bone_center.transform);
    57.         bon [1].localRotation = Quaternion.identity;
    58.         bon [1].localPosition = p2;
    59.         bindPose.Add(bone_center.transform.transform.worldToLocalMatrix * ski.transform.localToWorldMatrix );
    60.  
    61.         GameObject bone_up = new GameObject ();
    62.         bone_up.name = "bone_up";
    63.         bone_up.transform.parent = bone_center.transform;
    64.         bon.Add (bone_up.transform);
    65.         bon [2].localRotation = Quaternion.identity;
    66.         bon [2].localPosition = p3;
    67.         bindPose.Add(bone_up.transform.transform.worldToLocalMatrix * ski.transform.localToWorldMatrix );
    68.  
    69.  
    70.  
    71.         List<BoneWeight> poids = new List<BoneWeight> ();
    72.  
    73.             for (int i = 0; i < mesh.vertices.Length; i++) {
    74.  
    75.                 BoneWeight bb = new BoneWeight ();
    76.  
    77.  
    78.                 bb.boneIndex0 = 1;
    79.                 bb.weight0 = 1;
    80.                 poids.Add (bb);
    81.  
    82.             }
    83.  
    84.         mesh.bindposes = bindPose.ToArray ();
    85.         mesh.boneWeights = poids.ToArray();
    86.  
    87.         ski.rootBone = bon [0];
    88.         ski.bones = bon.ToArray ();
    89.  
    90.          
    91.         ski.sharedMesh = mesh;
    92.  
    93.  
    94.     }
    95.  
     
  4. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    876
    that's can be fun anyone can help please it's look when assign
    bindpose
     
unityunity