VoxelSkinnedAnimationObject.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. namespace VoxelImporter
  7. {
  8. [AddComponentMenu("Voxel Importer/Voxel Skinned Animation Object")]
  9. [RequireComponent(typeof(SkinnedMeshRenderer))]
  10. public class VoxelSkinnedAnimationObject : VoxelBase
  11. {
  12. #if !UNITY_EDITOR
  13. void Awake()
  14. {
  15. Destroy(this);
  16. }
  17. #else
  18. public override bool EditorInitialize()
  19. {
  20. var result = base.EditorInitialize();
  21. //ver1.021 -> ver1.0.3
  22. if (material != null)
  23. {
  24. materials = new List<Material>();
  25. materials.Add(material);
  26. materialData = new List<MaterialData>();
  27. materialData.Add(new MaterialData());
  28. materialIndexes = new List<int>();
  29. materialIndexes.Add(0);
  30. material = null;
  31. result = true;
  32. }
  33. //ver1.0.6 -> ver1.0.6.p1
  34. {
  35. var meshFilter = GetComponent<MeshFilter>();
  36. if (meshFilter != null && meshFilter.sharedMesh == mesh)
  37. {
  38. DestroyImmediate(meshFilter);
  39. }
  40. }
  41. return result;
  42. }
  43. public Mesh mesh;
  44. [SerializeField]
  45. protected Material material; //ver1.021 old
  46. public List<Material> materials; //ver1.0.3 new
  47. public Texture2D atlasTexture;
  48. public enum RigAnimationType
  49. {
  50. None = -1,
  51. Legacy,
  52. Generic,
  53. Humanoid,
  54. }
  55. public RigAnimationType rigAnimationType = RigAnimationType.None;
  56. public Avatar avatar;
  57. public bool updateAnimatorAvatar = true;
  58. public enum HumanoidBone
  59. {
  60. Hips,
  61. Spine,
  62. Chest,
  63. LeftShoulder,
  64. LeftUpperArm,
  65. LeftLowerArm,
  66. LeftHand,
  67. RightShoulder,
  68. RightUpperArm,
  69. RightLowerArm,
  70. RightHand,
  71. LeftUpperLeg,
  72. LeftLowerLeg,
  73. LeftFoot,
  74. LeftToes,
  75. RightUpperLeg,
  76. RightLowerLeg,
  77. RightFoot,
  78. RightToes,
  79. Neck,
  80. Head,
  81. LeftEye,
  82. RightEye,
  83. Jaw,
  84. LeftThumbProximal,
  85. LeftThumbIntermediate,
  86. LeftThumbDistal,
  87. LeftIndexProximal,
  88. LeftIndexIntermediate,
  89. LeftIndexDistal,
  90. LeftMiddleProximal,
  91. LeftMiddleIntermediate,
  92. LeftMiddleDistal,
  93. LeftRingProximal,
  94. LeftRingIntermediate,
  95. LeftRingDistal,
  96. LeftLittleProximal,
  97. LeftLittleIntermediate,
  98. LeftLittleDistal,
  99. RightThumbProximal,
  100. RightThumbIntermediate,
  101. RightThumbDistal,
  102. RightIndexProximal,
  103. RightIndexIntermediate,
  104. RightIndexDistal,
  105. RightMiddleProximal,
  106. RightMiddleIntermediate,
  107. RightMiddleDistal,
  108. RightRingProximal,
  109. RightRingIntermediate,
  110. RightRingDistal,
  111. RightLittleProximal,
  112. RightLittleIntermediate,
  113. RightLittleDistal,
  114. UpperChest,
  115. Total
  116. }
  117. public static readonly HumanoidBone[] HumanTraitBoneNameTable =
  118. {
  119. HumanoidBone.Hips,
  120. HumanoidBone.LeftUpperLeg,
  121. HumanoidBone.RightUpperLeg,
  122. HumanoidBone.LeftLowerLeg,
  123. HumanoidBone.RightLowerLeg,
  124. HumanoidBone.LeftFoot,
  125. HumanoidBone.RightFoot,
  126. HumanoidBone.Spine,
  127. HumanoidBone.Chest,
  128. HumanoidBone.Neck,
  129. HumanoidBone.Head,
  130. HumanoidBone.LeftShoulder,
  131. HumanoidBone.RightShoulder,
  132. HumanoidBone.LeftUpperArm,
  133. HumanoidBone.RightUpperArm,
  134. HumanoidBone.LeftLowerArm,
  135. HumanoidBone.RightLowerArm,
  136. HumanoidBone.LeftHand,
  137. HumanoidBone.RightHand,
  138. HumanoidBone.LeftToes,
  139. HumanoidBone.RightToes,
  140. HumanoidBone.LeftEye,
  141. HumanoidBone.RightEye,
  142. HumanoidBone.Jaw,
  143. HumanoidBone.LeftThumbProximal,
  144. HumanoidBone.LeftThumbIntermediate,
  145. HumanoidBone.LeftThumbDistal,
  146. HumanoidBone.LeftIndexProximal,
  147. HumanoidBone.LeftIndexIntermediate,
  148. HumanoidBone.LeftIndexDistal,
  149. HumanoidBone.LeftMiddleProximal,
  150. HumanoidBone.LeftMiddleIntermediate,
  151. HumanoidBone.LeftMiddleDistal,
  152. HumanoidBone.LeftRingProximal,
  153. HumanoidBone.LeftRingIntermediate,
  154. HumanoidBone.LeftRingDistal,
  155. HumanoidBone.LeftLittleProximal,
  156. HumanoidBone.LeftLittleIntermediate,
  157. HumanoidBone.LeftLittleDistal,
  158. HumanoidBone.RightThumbProximal,
  159. HumanoidBone.RightThumbIntermediate,
  160. HumanoidBone.RightThumbDistal,
  161. HumanoidBone.RightIndexProximal,
  162. HumanoidBone.RightIndexIntermediate,
  163. HumanoidBone.RightIndexDistal,
  164. HumanoidBone.RightMiddleProximal,
  165. HumanoidBone.RightMiddleIntermediate,
  166. HumanoidBone.RightMiddleDistal,
  167. HumanoidBone.RightRingProximal,
  168. HumanoidBone.RightRingIntermediate,
  169. HumanoidBone.RightRingDistal,
  170. HumanoidBone.RightLittleProximal,
  171. HumanoidBone.RightLittleIntermediate,
  172. HumanoidBone.RightLittleDistal,
  173. HumanoidBone.UpperChest,
  174. };
  175. [Serializable]
  176. public struct VoxelImporterHumanDescription
  177. {
  178. public VoxelImporterHumanDescription(bool reset = true)
  179. {
  180. firstAutomapDone = false;
  181. bones = null;
  182. upperArmTwist = 0.5f;
  183. lowerArmTwist = 0.5f;
  184. upperLegTwist = 0.5f;
  185. lowerLegTwist = 0.5f;
  186. armStretch = 0.05f;
  187. legStretch = 0.05f;
  188. feetSpacing = 0f;
  189. hasTranslationDoF = false;
  190. ResetMapping();
  191. }
  192. public VoxelImporterHumanDescription(ref VoxelImporterHumanDescription src)
  193. {
  194. firstAutomapDone = src.firstAutomapDone;
  195. bones = new VoxelSkinnedAnimationObjectBone[src.bones.Length];
  196. src.bones.CopyTo(bones, 0);
  197. upperArmTwist = src.upperArmTwist;
  198. lowerArmTwist = src.lowerArmTwist;
  199. upperLegTwist = src.upperLegTwist;
  200. lowerLegTwist = src.lowerLegTwist;
  201. armStretch = src.armStretch;
  202. legStretch = src.legStretch;
  203. feetSpacing = src.feetSpacing;
  204. hasTranslationDoF = src.hasTranslationDoF;
  205. }
  206. public bool IsChanged(ref VoxelImporterHumanDescription src)
  207. {
  208. if (firstAutomapDone != src.firstAutomapDone) return true;
  209. if (bones == null && src.bones != null) return true;
  210. if (bones != null && src.bones == null) return true;
  211. if (bones != null && src.bones != null)
  212. {
  213. if (bones.Length != src.bones.Length) return true;
  214. for (int i = 0; i < bones.Length; i++)
  215. {
  216. if (bones[i] != src.bones[i]) return true;
  217. }
  218. }
  219. if(upperArmTwist != src.upperArmTwist) return true;
  220. if (lowerArmTwist != src.lowerArmTwist) return true;
  221. if (upperLegTwist != src.upperLegTwist) return true;
  222. if (lowerLegTwist != src.lowerLegTwist) return true;
  223. if (armStretch != src.armStretch) return true;
  224. if (legStretch != src.legStretch) return true;
  225. if (feetSpacing != src.feetSpacing) return true;
  226. if (hasTranslationDoF != src.hasTranslationDoF) return true;
  227. return false;
  228. }
  229. public void ResetMapping()
  230. {
  231. bones = new VoxelSkinnedAnimationObjectBone[(int)HumanoidBone.Total];
  232. }
  233. public void ResetAdditionalSettings()
  234. {
  235. upperArmTwist = 0.5f;
  236. lowerArmTwist = 0.5f;
  237. upperLegTwist = 0.5f;
  238. lowerLegTwist = 0.5f;
  239. armStretch = 0.05f;
  240. legStretch = 0.05f;
  241. feetSpacing = 0f;
  242. hasTranslationDoF = false;
  243. }
  244. //Mapping
  245. public bool firstAutomapDone;
  246. public VoxelSkinnedAnimationObjectBone[] bones;
  247. //Pre-Muscle Settings
  248. //Additional Settings
  249. public float upperArmTwist;
  250. public float lowerArmTwist;
  251. public float upperLegTwist;
  252. public float lowerLegTwist;
  253. public float armStretch;
  254. public float legStretch;
  255. public float feetSpacing;
  256. public bool hasTranslationDoF;
  257. }
  258. public VoxelImporterHumanDescription humanDescription = new VoxelImporterHumanDescription(true);
  259. public bool skinnedMeshBoundsUpdate = true;
  260. public Vector3 skinnedMeshBoundsUpdateScale = new Vector3(1.5f, 1.5f, 1.5f);
  261. #region BoneWeight
  262. public VoxelSkinnedAnimationObjectBone[] bones;
  263. public Transform rootBone { get { return (bones != null && bones.Length > 0 && bones[0] != null) ? bones[0].transform : null; } }
  264. public Matrix4x4[] bindposes;
  265. #endregion
  266. #region Editor
  267. public enum Edit_Mode
  268. {
  269. None,
  270. BoneAnimation,
  271. BonePosition,
  272. BoneWeight,
  273. }
  274. public Edit_Mode editMode;
  275. public Edit_Mode editLastMode;
  276. public enum Edit_VoxelMode
  277. {
  278. Voxel,
  279. Vertex,
  280. }
  281. public Edit_VoxelMode edit_voxelMode;
  282. public enum Edit_VoxelWeightMode
  283. {
  284. Voxel,
  285. Fill,
  286. Rect,
  287. }
  288. public Edit_VoxelWeightMode edit_voxelWeightMode;
  289. public enum Edit_VertexWeightMode
  290. {
  291. Brush,
  292. Rect,
  293. }
  294. public Edit_VertexWeightMode edit_vertexWeightMode;
  295. public enum Edit_BlendMode
  296. {
  297. Replace,
  298. Add,
  299. Subtract,
  300. }
  301. public Edit_BlendMode edit_blendMode;
  302. public float edit_weight = 1f;
  303. public bool edit_autoNormalize = true;
  304. public float edit_brushRadius = 10f;
  305. public AnimationCurve edit_brushCurve = AnimationCurve.Linear(0f, 1f, 1f, 1f);
  306. public enum Edit_WeightPreviewMode
  307. {
  308. Opaque,
  309. Transparent,
  310. }
  311. public Edit_WeightPreviewMode edit_WeightPreviewMode;
  312. public enum Edit_MirrorSetMode
  313. {
  314. None,
  315. Positive,
  316. Negative,
  317. }
  318. public Edit_MirrorSetMode[] edit_mirrorPosition = new Edit_MirrorSetMode[3] { Edit_MirrorSetMode.Negative, Edit_MirrorSetMode.Positive, Edit_MirrorSetMode.Positive };
  319. public Edit_MirrorSetMode[] edit_mirrorRotation = new Edit_MirrorSetMode[3] { Edit_MirrorSetMode.Negative, Edit_MirrorSetMode.Positive, Edit_MirrorSetMode.Negative };
  320. public Edit_MirrorSetMode[] edit_mirrorScale = new Edit_MirrorSetMode[3] { Edit_MirrorSetMode.Positive, Edit_MirrorSetMode.Positive, Edit_MirrorSetMode.Positive };
  321. public bool edit_animationFoldout = true;
  322. #endregion
  323. #region Asset
  324. public override bool IsUseAssetObject(UnityEngine.Object obj)
  325. {
  326. if (mesh == obj) return true;
  327. if (materials != null)
  328. {
  329. for (int i = 0; i < materials.Count; i++)
  330. {
  331. if (materials[i] == obj) return true;
  332. }
  333. }
  334. if (atlasTexture == obj) return true;
  335. if (avatar == obj) return true;
  336. return false;
  337. }
  338. #endregion
  339. #region Undo
  340. public class RefreshCheckerSkinnedAnimation : RefreshChecker
  341. {
  342. public RefreshCheckerSkinnedAnimation(VoxelSkinnedAnimationObject voxelObject) : base(voxelObject)
  343. {
  344. controllerSkinnedAnimation = voxelObject;
  345. }
  346. public VoxelSkinnedAnimationObject controllerSkinnedAnimation;
  347. public VoxelSkinnedAnimationObjectBone[] bones;
  348. public Avatar avatar;
  349. public override void Save()
  350. {
  351. base.Save();
  352. if (controllerSkinnedAnimation.bones != null)
  353. {
  354. bones = new VoxelSkinnedAnimationObjectBone[controllerSkinnedAnimation.bones.Length];
  355. controllerSkinnedAnimation.bones.CopyTo(bones, 0);
  356. }
  357. else
  358. {
  359. bones = null;
  360. }
  361. avatar = controllerSkinnedAnimation.avatar;
  362. }
  363. public override bool Check()
  364. {
  365. if (base.Check())
  366. return true;
  367. if (bones == null && controllerSkinnedAnimation.bones == null) return false;
  368. if (bones == null || controllerSkinnedAnimation.bones == null) return true;
  369. if (bones.Length != controllerSkinnedAnimation.bones.Length) return true;
  370. for (int i = 0; i < bones.Length; i++)
  371. {
  372. if (bones[i] != controllerSkinnedAnimation.bones[i]) return true;
  373. }
  374. if (avatar != controllerSkinnedAnimation.avatar) return true;
  375. return false;
  376. }
  377. }
  378. #endregion
  379. #endif
  380. }
  381. }