Guest User

Untitled

a guest
Jun 23rd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. [CustomEditor(typeof(VRCSDK2.VRC_AvatarDescriptor))]
  7. public class AvatarDescriptorEditor : Editor
  8. {
  9. VRCSDK2.VRC_AvatarDescriptor avatarDescriptor;
  10. VRC.Core.PipelineManager pipelineManager;
  11.  
  12. SkinnedMeshRenderer selectedMesh;
  13. List<string> blendShapeNames = null;
  14.  
  15. public override void OnInspectorGUI()
  16. {
  17. if (avatarDescriptor == null)
  18. avatarDescriptor = (VRCSDK2.VRC_AvatarDescriptor)target;
  19.  
  20. if (pipelineManager == null)
  21. {
  22. pipelineManager = avatarDescriptor.GetComponent<VRC.Core.PipelineManager>();
  23. if (pipelineManager == null)
  24. avatarDescriptor.gameObject.AddComponent<VRC.Core.PipelineManager>();
  25. }
  26.  
  27. // DrawDefaultInspector();
  28.  
  29. avatarDescriptor.ViewPosition = EditorGUILayout.Vector3Field("View Position", avatarDescriptor.ViewPosition);
  30. //avatarDescriptor.Name = EditorGUILayout.TextField("Avatar Name", avatarDescriptor.Name);
  31. avatarDescriptor.Animations = (VRCSDK2.VRC_AvatarDescriptor.AnimationSet)EditorGUILayout.EnumPopup("Default Animation Set", avatarDescriptor.Animations);
  32. avatarDescriptor.CustomStandingAnims = (AnimatorOverrideController)EditorGUILayout.ObjectField("Custom Standing Anims", avatarDescriptor.CustomStandingAnims, typeof(AnimatorOverrideController), true, null);
  33. avatarDescriptor.CustomSittingAnims = (AnimatorOverrideController)EditorGUILayout.ObjectField("Custom Sitting Anims", avatarDescriptor.CustomSittingAnims, typeof(AnimatorOverrideController), true, null);
  34. avatarDescriptor.ScaleIPD = EditorGUILayout.Toggle("Scale IPD", avatarDescriptor.ScaleIPD);
  35.  
  36. avatarDescriptor.lipSync = (VRCSDK2.VRC_AvatarDescriptor.LipSyncStyle)EditorGUILayout.EnumPopup("Lip Sync", avatarDescriptor.lipSync);
  37. switch (avatarDescriptor.lipSync)
  38. {
  39. case VRCSDK2.VRC_AvatarDescriptor.LipSyncStyle.Default:
  40. if (GUILayout.Button("Auto Detect!"))
  41. AutoDetectLipSync();
  42. break;
  43.  
  44. case VRCSDK2.VRC_AvatarDescriptor.LipSyncStyle.JawFlapBlendShape:
  45. avatarDescriptor.VisemeSkinnedMesh = (SkinnedMeshRenderer)EditorGUILayout.ObjectField("Face Mesh", avatarDescriptor.VisemeSkinnedMesh, typeof(SkinnedMeshRenderer), true);
  46. if (avatarDescriptor.VisemeSkinnedMesh != null)
  47. {
  48. DetermineBlendShapeNames();
  49.  
  50. int current = -1;
  51. for (int b = 0; b < blendShapeNames.Count; ++b)
  52. if (avatarDescriptor.MouthOpenBlendShapeName == blendShapeNames[b])
  53. current = b;
  54.  
  55. string title = "Jaw Flap Blend Shape";
  56. int next = EditorGUILayout.Popup(title, current, blendShapeNames.ToArray());
  57. if (next >= 0)
  58. avatarDescriptor.MouthOpenBlendShapeName = blendShapeNames[next];
  59. }
  60. break;
  61.  
  62. case VRCSDK2.VRC_AvatarDescriptor.LipSyncStyle.JawFlapBone:
  63. avatarDescriptor.lipSyncJawBone = (Transform)EditorGUILayout.ObjectField("Jaw Bone", avatarDescriptor.lipSyncJawBone, typeof(Transform), true);
  64. break;
  65.  
  66. case VRCSDK2.VRC_AvatarDescriptor.LipSyncStyle.VisemeBlendShape:
  67. avatarDescriptor.VisemeSkinnedMesh = (SkinnedMeshRenderer)EditorGUILayout.ObjectField("Face Mesh", avatarDescriptor.VisemeSkinnedMesh, typeof(SkinnedMeshRenderer), true);
  68. if (avatarDescriptor.VisemeSkinnedMesh != null)
  69. {
  70. DetermineBlendShapeNames();
  71.  
  72. if (avatarDescriptor.VisemeBlendShapes == null || avatarDescriptor.VisemeBlendShapes.Length != (int)VRCSDK2.VRC_AvatarDescriptor.Viseme.Count)
  73. avatarDescriptor.VisemeBlendShapes = new string[(int)VRCSDK2.VRC_AvatarDescriptor.Viseme.Count];
  74. for (int i = 0; i < (int)VRCSDK2.VRC_AvatarDescriptor.Viseme.Count; ++i)
  75. {
  76. int current = -1;
  77. for (int b = 0; b < blendShapeNames.Count; ++b)
  78. if (avatarDescriptor.VisemeBlendShapes[i] == blendShapeNames[b])
  79. current = b;
  80.  
  81. string title = "Viseme: " + ((VRCSDK2.VRC_AvatarDescriptor.Viseme)i).ToString();
  82. int next = EditorGUILayout.Popup(title, current, blendShapeNames.ToArray());
  83. if (next >= 0)
  84. avatarDescriptor.VisemeBlendShapes[i] = blendShapeNames[next];
  85. }
  86. }
  87. break;
  88. }
  89. EditorGUILayout.LabelField("Unity Version", avatarDescriptor.unityVersion);
  90. }
  91.  
  92. void DetermineBlendShapeNames()
  93. {
  94. if (avatarDescriptor.VisemeSkinnedMesh != null &&
  95. avatarDescriptor.VisemeSkinnedMesh != selectedMesh)
  96. {
  97. blendShapeNames = new List<string>();
  98. blendShapeNames.Add("-none-");
  99. selectedMesh = avatarDescriptor.VisemeSkinnedMesh;
  100. for (int i = 0; i < selectedMesh.sharedMesh.blendShapeCount; ++i)
  101. blendShapeNames.Add(selectedMesh.sharedMesh.GetBlendShapeName(i));
  102. }
  103. }
  104.  
  105. void AutoDetectLipSync()
  106. {
  107. var smrs = avatarDescriptor.GetComponentsInChildren<SkinnedMeshRenderer>();
  108. foreach (var smr in smrs)
  109. {
  110. if (smr.sharedMesh.blendShapeCount >= (int)VRCSDK2.VRC_AvatarDescriptor.Viseme.Count)
  111. {
  112. avatarDescriptor.lipSync = VRCSDK2.VRC_AvatarDescriptor.LipSyncStyle.VisemeBlendShape;
  113. avatarDescriptor.VisemeSkinnedMesh = smr;
  114. DetermineBlendShapeNames();
  115.  
  116. if (avatarDescriptor.VisemeBlendShapes == null || avatarDescriptor.VisemeBlendShapes.Length != (int)VRCSDK2.VRC_AvatarDescriptor.Viseme.Count)
  117. avatarDescriptor.VisemeBlendShapes = new string[(int)VRCSDK2.VRC_AvatarDescriptor.Viseme.Count];
  118.  
  119. avatarDescriptor.lipSyncJawBone = null;
  120. int found = 0;
  121.  
  122. for (int i = 0; i < (int)VRCSDK2.VRC_AvatarDescriptor.Viseme.Count; i++)
  123. {
  124. var viseme = (VRCSDK2.VRC_AvatarDescriptor.Viseme)i;
  125. var visemeName = "vrc.v_" + viseme.ToString().ToLower();
  126. if (smr.sharedMesh.GetBlendShapeIndex(visemeName) != -1)
  127. {
  128. avatarDescriptor.VisemeBlendShapes[i] = visemeName;
  129. found++;
  130. }
  131. }
  132. if (found == (int)VRCSDK2.VRC_AvatarDescriptor.Viseme.Count)
  133. {
  134. return;
  135. }
  136. }
  137.  
  138. if(smr.sharedMesh.blendShapeCount > 0)
  139. {
  140. avatarDescriptor.lipSync = VRCSDK2.VRC_AvatarDescriptor.LipSyncStyle.JawFlapBlendShape;
  141. avatarDescriptor.VisemeSkinnedMesh = null;
  142. avatarDescriptor.lipSyncJawBone = null;
  143. return;
  144. }
  145. }
  146.  
  147. if (avatarDescriptor.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.Jaw) != null)
  148. {
  149. avatarDescriptor.lipSync = VRCSDK2.VRC_AvatarDescriptor.LipSyncStyle.JawFlapBone;
  150. avatarDescriptor.lipSyncJawBone = avatarDescriptor.GetComponent<Animator>().GetBoneTransform(HumanBodyBones.Jaw);
  151. avatarDescriptor.VisemeSkinnedMesh = null;
  152. return;
  153. }
  154.  
  155. }
  156. }
Add Comment
Please, Sign In to add comment