public SynchronizeObject(Animation animation, AnimationClip clip) { this.animation = animation; gameObject = animation.gameObject; transformPoseSave = new TransformPoseSave(gameObject); blendShapeWeightSave = new BlendShapeWeightSave(gameObject); animationClipValueSave = new AnimationClipValueSave(gameObject, clip); this.clip = clip; }
public SynchronizeObject(Animation animation) { this.animation = animation; gameObject = animation.gameObject; propertyModifications = PrefabUtility.GetPropertyModifications(gameObject); transformPoseSave = new TransformPoseSave(gameObject); blendShapeWeightSave = new BlendShapeWeightSave(gameObject); #region Clip { var saveSettings = gameObject.GetComponent <VeryAnimationSaveSettings>(); if (saveSettings != null && saveSettings.lastSelectAnimationClip != null) { if (ArrayUtility.Contains(AnimationUtility.GetAnimationClips(gameObject), saveSettings.lastSelectAnimationClip)) { clip = saveSettings.lastSelectAnimationClip; } } if (clip == null) { clip = animation.clip; } } #endregion }
private void UpdateBlendShapeSetIcon() { if (!blendShapeSetIconUpdate) { return; } blendShapeSetIconUpdate = false; if (va.blendShapeSetList == null || va.blendShapeSetList.Count <= 0) { return; } TransformPoseSave beforePose = new TransformPoseSave(va.editGameObject); BlendShapeWeightSave beforeBlendShape = new BlendShapeWeightSave(va.editGameObject); va.transformPoseSave.ResetDefaultTransform(); va.blendShapeWeightSave.ResetDefaultWeight(); var gameObject = GameObject.Instantiate <GameObject>(va.editGameObject); gameObject.hideFlags |= HideFlags.HideAndDontSave; gameObject.transform.rotation = Quaternion.identity; Action ForceObjectUpdate = () => { gameObject.SetActive(false); gameObject.SetActive(true); }; int blankLayer; { for (blankLayer = 31; blankLayer > 0; blankLayer--) { if (string.IsNullOrEmpty(LayerMask.LayerToName(blankLayer))) { break; } } if (blankLayer < 0) { blankLayer = 31; } } foreach (var renderer in gameObject.GetComponentsInChildren <Renderer>()) { renderer.gameObject.layer = blankLayer; } var renderers = gameObject.GetComponentsInChildren <SkinnedMeshRenderer>().Where(renderer => renderer.sharedMesh != null && renderer.sharedMesh.blendShapeCount > 0).ToArray(); foreach (var renderer in renderers) { renderer.updateWhenOffscreen = true; } { RenderTexture iconTexture = new RenderTexture(IconTextureSize, IconTextureSize, 16, RenderTextureFormat.ARGB32); iconTexture.hideFlags |= HideFlags.HideAndDontSave; iconTexture.Create(); var cameraObject = new GameObject(); cameraObject.hideFlags |= HideFlags.HideAndDontSave; var camera = cameraObject.AddComponent <Camera>(); camera.targetTexture = iconTexture; camera.clearFlags = CameraClearFlags.Color; camera.backgroundColor = Color.clear; camera.cullingMask = 1 << blankLayer; { Bounds bounds = new Bounds(); foreach (var renderer in renderers) { if (Mathf.Approximately(bounds.size.sqrMagnitude, 0f)) { bounds = renderer.bounds; } else { bounds.Encapsulate(renderer.bounds); } } var transform = camera.transform; var sizeMax = Mathf.Max(bounds.size.x, Mathf.Max(bounds.size.y, bounds.size.z)); switch (blendShapeSetIconCameraMode) { case IconCameraMode.forward: { var rot = Quaternion.AngleAxis(180f, Vector3.up); transform.localRotation = rot; sizeMax = Mathf.Max(bounds.size.x, bounds.size.y); transform.localPosition = new Vector3(bounds.center.x, bounds.center.y, bounds.max.z) - transform.forward; } break; case IconCameraMode.back: { transform.localRotation = Quaternion.identity; sizeMax = Mathf.Max(bounds.size.x, bounds.size.y); transform.localPosition = new Vector3(bounds.center.x, bounds.center.y, bounds.min.z) - transform.forward; } break; case IconCameraMode.up: { var rot = Quaternion.AngleAxis(90f, Vector3.right); transform.localRotation = rot; sizeMax = Mathf.Max(bounds.size.x, bounds.size.z); transform.localPosition = new Vector3(bounds.center.x, bounds.max.y, bounds.center.z) - transform.forward; } break; case IconCameraMode.down: { var rot = Quaternion.AngleAxis(-90f, Vector3.right); transform.localRotation = rot; sizeMax = Mathf.Max(bounds.size.x, bounds.size.z); transform.localPosition = new Vector3(bounds.center.x, bounds.min.y, bounds.center.z) - transform.forward; } break; case IconCameraMode.right: { var rot = Quaternion.AngleAxis(-90f, Vector3.up); transform.localRotation = rot; sizeMax = Mathf.Max(bounds.size.y, bounds.size.z); transform.localPosition = new Vector3(bounds.max.x, bounds.center.y, bounds.center.z) - transform.forward; } break; case IconCameraMode.left: { var rot = Quaternion.AngleAxis(90f, Vector3.up); transform.localRotation = rot; sizeMax = Mathf.Max(bounds.size.y, bounds.size.z); transform.localPosition = new Vector3(bounds.min.x, bounds.center.y, bounds.center.z) - transform.forward; } break; } camera.orthographic = true; camera.orthographicSize = sizeMax * 0.6f; camera.farClipPlane = 1f + sizeMax * 5f; } cameraObject.transform.SetParent(gameObject.transform); gameObject.transform.rotation = va.editGameObject.transform.rotation; foreach (var set in va.blendShapeSetList) { va.blendShapeWeightSave.ResetDefaultWeight(); if (set.poseTemplate.blendShapePaths != null && set.poseTemplate.blendShapeValues != null) { foreach (var renderer in renderers) { var path = AnimationUtility.CalculateTransformPath(renderer.transform, gameObject.transform); var index = EditorCommon.ArrayIndexOf(set.poseTemplate.blendShapePaths, path); if (index < 0) { continue; } for (int i = 0; i < set.poseTemplate.blendShapeValues[index].names.Length; i++) { var sindex = renderer.sharedMesh.GetBlendShapeIndex(set.poseTemplate.blendShapeValues[index].names[i]); if (sindex < 0 || sindex >= renderer.sharedMesh.blendShapeCount) { continue; } renderer.SetBlendShapeWeight(sindex, set.poseTemplate.blendShapeValues[index].weights[i]); } } } ForceObjectUpdate(); camera.Render(); { RenderTexture save = RenderTexture.active; RenderTexture.active = iconTexture; if (set.icon == null) { set.icon = new Texture2D(iconTexture.width, iconTexture.height, TextureFormat.ARGB32, iconTexture.useMipMap); set.icon.hideFlags |= HideFlags.HideAndDontSave; } set.icon.ReadPixels(new Rect(0, 0, iconTexture.width, iconTexture.height), 0, 0); set.icon.Apply(); RenderTexture.active = save; } } GameObject.DestroyImmediate(cameraObject); iconTexture.Release(); RenderTexture.DestroyImmediate(iconTexture); } GameObject.DestroyImmediate(gameObject); beforePose.ResetDefaultTransform(); beforeBlendShape.ResetDefaultWeight(); { va.editGameObject.SetActive(false); va.editGameObject.SetActive(true); } va.SetUpdateResampleAnimation(); }
private void InitController() { gameObject = dg_get_PreviewObject(); if (gameObject == null) { return; } var originalGameObject = uAvatarPreviewSelection.GetPreview(dg_get_animationClipType()); animator = dg_get_Animator(); animation = gameObject.GetComponent <Animation>(); if (originalGameObject != null) { transformPoseSave = new TransformPoseSave(originalGameObject); transformPoseSave.SetRootTransform(originalGameObject.transform.position, originalGameObject.transform.rotation, originalGameObject.transform.lossyScale); transformPoseSave.ChangeTransforms(gameObject); } else { transformPoseSave = new TransformPoseSave(gameObject); } blendShapeWeightSave = new BlendShapeWeightSave(gameObject); var clip = dg_get_m_SourcePreviewMotion(instance); if (clip.legacy || instance == null || !((UnityEngine.Object)animator != (UnityEngine.Object)null)) { if (animation != null) { animation.enabled = false; //If vaw.animation.enabled, it is not updated during execution. bug? } } else { if (m_Controller == null) { m_Controller = new UnityEditor.Animations.AnimatorController(); m_Controller.name = "Avatar Preview AnimatorController"; m_Controller.hideFlags |= HideFlags.HideAndDontSave; uAnimatorController.SetPushUndo(m_Controller, false); m_Controller.AddLayer("preview"); m_StateMachine = m_Controller.layers[0].stateMachine; uAnimatorStateMachine.SetPushUndo(m_StateMachine, false); m_StateMachine.hideFlags |= HideFlags.HideAndDontSave; } if (m_State == null) { m_State = m_StateMachine.AddState("preview"); uAnimatorState.SetPushUndo(m_State, false); m_State.motion = (Motion)clip; m_State.iKOnFeet = dg_get_ShowIKOnFeetButton() && dg_get_IKOnFeet(); m_State.hideFlags |= HideFlags.HideAndDontSave; } UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, this.m_Controller); animator.fireEvents = false; animator.applyRootMotion = EditorPrefs.GetBool(EditorPrefsApplyRootMotion, false); } dg_set_ShowIKOnFeetButton(animator != null && animator.isHuman && clip.isHumanMotion); #if !UNITY_2017_3_OR_NEWER mode2D = EditorPrefs.GetBool(EditorPrefs2D, false); #endif SetTime(uTimeControl.currentTime); ForceUpdate(); }
public SynchronizeObject(Animator animator) { this.animator = animator; gameObject = animator.gameObject; propertyModifications = PrefabUtility.GetPropertyModifications(gameObject); transformPoseSave = new TransformPoseSave(gameObject); blendShapeWeightSave = new BlendShapeWeightSave(gameObject); #region Clip { var saveSettings = gameObject.GetComponent <VeryAnimationSaveSettings>(); if (saveSettings != null && saveSettings.lastSelectAnimationClip != null) { if (ArrayUtility.Contains(AnimationUtility.GetAnimationClips(gameObject), saveSettings.lastSelectAnimationClip)) { clip = saveSettings.lastSelectAnimationClip; } } if (clip == null) { var ac = EditorCommon.GetAnimatorController(animator); if (ac != null && ac.layers.Length > 0) { var state = ac.layers[0].stateMachine.defaultState; if (state != null) { if (state.motion is UnityEditor.Animations.BlendTree) { Action <UnityEditor.Animations.BlendTree> FindBlendTree = null; FindBlendTree = (blendTree) => { if (blendTree.children == null) { return; } var children = blendTree.children; for (int i = 0; i < children.Length; i++) { if (children[i].motion is UnityEditor.Animations.BlendTree) { FindBlendTree(children[i].motion as UnityEditor.Animations.BlendTree); } else { clip = children[i].motion as AnimationClip; } if (clip != null) { break; } } blendTree.children = children; }; FindBlendTree(state.motion as UnityEditor.Animations.BlendTree); } else { clip = state.motion as AnimationClip; } } } if (clip != null) { var owc = animator.runtimeAnimatorController as AnimatorOverrideController; if (owc != null) { clip = owc[clip]; } } } } #endregion animator.enabled = false; //In order to avoid the mysterious behavior where an event is called from "UnityEditor.Handles: DrawCameraImpl", it is invalid except when updating }
private void InitController() { gameObject = dg_get_PreviewObject(); if (gameObject == null) { return; } originalGameObject = uAvatarPreviewSelection.GetPreview(dg_get_animationClipType()); animator = dg_get_Animator(); animation = gameObject.GetComponent <Animation>(); if (originalGameObject != null) { transformPoseSave = new TransformPoseSave(originalGameObject); transformPoseSave.ChangeStartTransform(); transformPoseSave.ChangeTransformReference(gameObject); } else { transformPoseSave = new TransformPoseSave(gameObject); } blendShapeWeightSave = new BlendShapeWeightSave(gameObject); var clip = dg_get_m_SourcePreviewMotion(instance); if (clip.legacy || instance == null || !((UnityEngine.Object)animator != (UnityEngine.Object)null)) { if (animation != null) { animation.enabled = false; //If vaw.animation.enabled, it is not updated during execution. bug? } } else { animator.fireEvents = false; animator.applyRootMotion = EditorPrefs.GetBool(EditorPrefsApplyRootMotion, false); #if UNITY_2019_1_OR_NEWER animator.enabled = true; UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, null); m_PlayableGraph = PlayableGraph.Create("Avatar Preview PlayableGraph"); m_PlayableGraph.SetTimeUpdateMode(DirectorUpdateMode.Manual); m_AnimationClipPlayable = AnimationClipPlayable.Create(m_PlayableGraph, clip); m_AnimationClipPlayable.SetApplyPlayableIK(false); m_AnimationClipPlayable.SetApplyFootIK(isIKOnFeet); if (m_UAnimationClipPlayable == null) { m_UAnimationClipPlayable = new UAnimationClipPlayable(); } m_UAnimationClipPlayable.SetRemoveStartOffset(m_AnimationClipPlayable, true); m_UAnimationClipPlayable.SetOverrideLoopTime(m_AnimationClipPlayable, true); m_UAnimationClipPlayable.SetLoopTime(m_AnimationClipPlayable, true); Playable rootPlayable = m_AnimationClipPlayable; #if VERYANIMATION_ANIMATIONRIGGING m_VARigBuilder = gameObject.GetComponent <VeryAnimationRigBuilder>(); if (m_VARigBuilder != null) { RigBuilder.DestroyImmediate(m_VARigBuilder); m_VARigBuilder = null; } m_RigBuilder = gameObject.GetComponent <RigBuilder>(); if (m_RigBuilder != null) { RigBuilder.DestroyImmediate(m_RigBuilder); m_RigBuilder = null; } if (originalGameObject != null) { var rigBuilder = originalGameObject.GetComponent <RigBuilder>(); if (rigBuilder != null && rigBuilder.isActiveAndEnabled && rigBuilder.enabled) { var layers = new List <GameObject>(); foreach (var layer in rigBuilder.layers) { if (layer.rig == null || !layer.active) { continue; } var originalRig = layer.rig.GetComponent <Rig>(); if (originalRig == null) { continue; } Func <Transform, Transform> GetPreviewTransform = (t) => { if (t == null) { return(null); } var path = AnimationUtility.CalculateTransformPath(t, originalGameObject.transform); return(gameObject.transform.Find(path)); }; var previewT = GetPreviewTransform(originalRig.transform); if (previewT == null) { continue; } var newRig = GameObject.Instantiate <GameObject>(originalRig.gameObject, previewT.parent); newRig.name = previewT.name; GameObject.DestroyImmediate(previewT.gameObject); AnimationRigging.ReplaceConstraintTransformReference(gameObject, newRig.GetComponent <Rig>(), originalGameObject, originalRig); layers.Add(newRig); } if (layers.Count > 0) { m_VARigBuilder = gameObject.AddComponent <VeryAnimationRigBuilder>(); m_RigBuilder = gameObject.GetComponent <RigBuilder>(); foreach (var layer in layers) { var rig = layer.GetComponent <Rig>(); #region RemoveEffector { var effectors = rig.effectors as List <RigEffectorData>; effectors.Clear(); } #endregion #if UNITY_2020_1_OR_NEWER var rigLayer = new RigLayer(rig); //version 0.3.2 #else var rigLayer = new RigBuilder.RigLayer(rig); //version 0.2.5 #endif m_RigBuilder.layers.Add(rigLayer); } } } if (m_VARigBuilder != null && m_RigBuilder != null) { m_VARigBuilder.enabled = m_RigBuilder.enabled = EditorPrefs.GetBool(EditorPrefsARConstraint); if (m_RigBuilder.enabled) { m_VARigBuilder.StartPreview(); m_RigBuilder.StartPreview(); rootPlayable = m_VARigBuilder.BuildPreviewGraph(m_PlayableGraph, rootPlayable); rootPlayable = m_RigBuilder.BuildPreviewGraph(m_PlayableGraph, rootPlayable); } } } #endif if (animator.applyRootMotion) { bool hasRootMotionBone = false; if (animator.isHuman) { hasRootMotionBone = true; } else { UAvatar uAvatar = new UAvatar(); var genericRootMotionBonePath = uAvatar.GetGenericRootMotionBonePath(animator.avatar); hasRootMotionBone = !string.IsNullOrEmpty(genericRootMotionBonePath); } if (hasRootMotionBone) { if (m_UAnimationOffsetPlayable == null) { m_UAnimationOffsetPlayable = new UAnimationOffsetPlayable(); } m_AnimationOffsetPlayable = m_UAnimationOffsetPlayable.Create(m_PlayableGraph, transformPoseSave.startLocalPosition, transformPoseSave.startLocalRotation, 1); m_AnimationOffsetPlayable.SetInputWeight(0, 1f); m_PlayableGraph.Connect(rootPlayable, 0, m_AnimationOffsetPlayable, 0); rootPlayable = m_AnimationOffsetPlayable; } { if (m_UAnimationMotionXToDeltaPlayable == null) { m_UAnimationMotionXToDeltaPlayable = new UAnimationMotionXToDeltaPlayable(); } m_AnimationMotionXToDeltaPlayable = m_UAnimationMotionXToDeltaPlayable.Create(m_PlayableGraph); m_UAnimationMotionXToDeltaPlayable.SetAbsoluteMotion(m_AnimationMotionXToDeltaPlayable, true); m_AnimationMotionXToDeltaPlayable.SetInputWeight(0, 1f); m_PlayableGraph.Connect(rootPlayable, 0, m_AnimationMotionXToDeltaPlayable, 0); rootPlayable = m_AnimationMotionXToDeltaPlayable; } } var playableOutput = AnimationPlayableOutput.Create(m_PlayableGraph, "Animation", animator); playableOutput.SetSourcePlayable(rootPlayable); #else if (m_Controller == null) { m_Controller = new UnityEditor.Animations.AnimatorController(); m_Controller.name = "Avatar Preview AnimatorController"; m_Controller.hideFlags |= HideFlags.HideAndDontSave; uAnimatorController.SetPushUndo(m_Controller, false); m_Controller.AddLayer("preview"); m_StateMachine = m_Controller.layers[0].stateMachine; uAnimatorStateMachine.SetPushUndo(m_StateMachine, false); m_StateMachine.hideFlags |= HideFlags.HideAndDontSave; } if (m_State == null) { m_State = m_StateMachine.AddState("preview"); uAnimatorState.SetPushUndo(m_State, false); m_State.motion = (Motion)clip; m_State.iKOnFeet = isIKOnFeet; m_State.hideFlags |= HideFlags.HideAndDontSave; } UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, m_Controller); #endif } dg_set_ShowIKOnFeetButton(animator != null && animator.isHuman && clip.isHumanMotion); ForceUpdate(); }
public SynchronizeObject(Animator animator, AnimationClip clip) { this.animator = animator; gameObject = animator.gameObject; transformPoseSave = new TransformPoseSave(gameObject); blendShapeWeightSave = new BlendShapeWeightSave(gameObject); animationClipValueSave = new AnimationClipValueSave(gameObject, clip); this.clip = clip; #if UNITY_2018_3_OR_NEWER #region BuildPlayableGraph { m_PlayableGraph = PlayableGraph.Create("SynchronizeObject." + gameObject.name); m_PlayableGraph.SetTimeUpdateMode(DirectorUpdateMode.Manual); m_AnimationClipPlayable = AnimationClipPlayable.Create(m_PlayableGraph, clip); m_AnimationClipPlayable.SetApplyPlayableIK(false); m_AnimationClipPlayable.SetApplyFootIK(false); { UAnimationClipPlayable uAnimationClipPlayable = new UAnimationClipPlayable(); uAnimationClipPlayable.SetRemoveStartOffset(m_AnimationClipPlayable, true); } Playable rootPlayable = m_AnimationClipPlayable; #if VERYANIMATION_ANIMATIONRIGGING m_VARigBuilder = gameObject.GetComponent <VeryAnimationRigBuilder>(); m_RigBuilder = gameObject.GetComponent <RigBuilder>(); if (m_VARigBuilder != null && m_RigBuilder != null) { m_VARigBuilder.StartPreview(); m_RigBuilder.StartPreview(); rootPlayable = m_VARigBuilder.BuildPreviewGraph(m_PlayableGraph, rootPlayable); rootPlayable = m_RigBuilder.BuildPreviewGraph(m_PlayableGraph, rootPlayable); } #endif if (animator.applyRootMotion) { bool hasRootMotionBone = false; if (animator.isHuman) { hasRootMotionBone = true; } else { UAvatar uAvatar = new UAvatar(); var genericRootMotionBonePath = uAvatar.GetGenericRootMotionBonePath(animator.avatar); hasRootMotionBone = !string.IsNullOrEmpty(genericRootMotionBonePath); } if (hasRootMotionBone) { var uAnimationOffsetPlayable = new UAnimationOffsetPlayable(); m_AnimationOffsetPlayable = uAnimationOffsetPlayable.Create(m_PlayableGraph, transformPoseSave.startLocalPosition, transformPoseSave.startLocalRotation, 1); m_AnimationOffsetPlayable.SetInputWeight(0, 1f); m_PlayableGraph.Connect(rootPlayable, 0, m_AnimationOffsetPlayable, 0); rootPlayable = m_AnimationOffsetPlayable; } { var uAnimationMotionXToDeltaPlayable = new UAnimationMotionXToDeltaPlayable(); m_AnimationMotionXToDeltaPlayable = uAnimationMotionXToDeltaPlayable.Create(m_PlayableGraph); uAnimationMotionXToDeltaPlayable.SetAbsoluteMotion(m_AnimationMotionXToDeltaPlayable, true); m_AnimationMotionXToDeltaPlayable.SetInputWeight(0, 1f); m_PlayableGraph.Connect(rootPlayable, 0, m_AnimationMotionXToDeltaPlayable, 0); rootPlayable = m_AnimationMotionXToDeltaPlayable; } } var playableOutput = AnimationPlayableOutput.Create(m_PlayableGraph, "Animation", animator); playableOutput.SetSourcePlayable(rootPlayable); } #endregion #else animator.enabled = false; //In order to avoid the mysterious behavior where an event is called from "UnityEditor.Handles: DrawCameraImpl", it is invalid except when updating #endif #region Save saveFireEvents = animator.fireEvents; animator.fireEvents = false; #endregion }