Beispiel #1
0
    /// <summary>
    /// Set by the AnimationCompiler allowing the object use information from the source B3D file
    /// </summary>
    public void SetAnimator(StinkyFile.Blitz3D.Animator animator)
    {
        BaseAnimator = animator;
        if (animator == null)
        {
            return;
        }
        var child = transform.Find(animator.Objects[0].Name, true);

        UnityAnimator = child?.GetComponent <Animator>();
        SequenceNames.Clear();
        foreach (var seq in animator.Sequences)
        {
            if (string.IsNullOrWhiteSpace(seq.Name) || SequenceNames.Keys.Contains(seq.Name))
            {
                Debug.LogWarning($"A sequence by the name of {seq.Name}, ID: {seq.ID} has error(s). The name is either blank or " +
                                 $"a duplicate and therefore was ignored from sequence database construction. Fix errors in Krabby Quest Tools and reload the level.");
                continue;
            }
            else
            {
                SequenceNames.Add(seq.Name, seq.ID);
            }
        }
    }
        private void LoadObject(GameObject target, string path)
        {
            string      objName      = target.name;
            BlitzObject b3dObject    = B3DLoader.LoadedObjects.FirstOrDefault(x => x.Name == objName);
            bool        pathAppended = false;

            if (b3dObject != null && !flag_NotCompile.Contains(b3dObject.Name))
            {
                if (b3dObject.HasAnimator)
                {
                    currentAnimator = b3dObject.Animator;
                    animators.Add(currentAnimator);
                    try
                    {
                        //attempt to load sequences from file
                        var animDir = Path.Combine(WorkspaceDirectory, "Animations");
                        currentAnimator.LoadSequencesFromPath(animDir, b3dObject.Name, B3DName);
                    }
                    catch
                    {
                    }
                    SequenceStates.Clear();
                    currentIndex         = 0;
                    currentUnityAnimator = createController(objName, out creatingController);
                    creatingController   = !creatingController;
                    _currentAnimator     = target.AddComponent <UnityEngine.Animator>();
                    _currentAnimator.runtimeAnimatorController = currentUnityAnimator as RuntimeAnimatorController;
                    parentObjectName = objName;
                    path             = "";
                }

                /*if (b3dObject is MeshModel && objName == TargetMesh.name)
                 * {
                 *  currentBlitzMesh = b3dObject as MeshModel;
                 *  var currentBoneWeights = TargetMesh.boneWeights;
                 *  BoneWeight1[] skinBones = new BoneWeight1[currentBoneWeights.Length];
                 *  foreach (var bone in currentBlitzMesh.Bones)
                 *  {
                 *
                 *  }
                 * }*/
                if (currentAnimator != null)
                {
                    var currentB3DObj = currentAnimator.Objects.FirstOrDefault(x => x.Name == objName);
                    if (currentB3DObj != null)
                    {
                        if (creatingController)
                        {
                            float weight = 1;
                            if (currentB3DObj is StinkyFile.Blitz3D.Prim.Pivot)
                            {
                                weight = (currentB3DObj as StinkyFile.Blitz3D.Prim.Pivot).Weight;
                            }
                            makeLayerForObject(currentB3DObj.Name, weight);
                        }
                        currentIndex++;
                        int animIndex = currentAnimator.Objects.IndexOf(currentB3DObj);
                        var anim      = currentAnimator.Animations.ElementAt(animIndex);
                        foreach (var sequence in currentAnimator.Sequences)
                        {
                            var clip          = createClip(sequence, anim.keys[sequence.ID], target.transform, path, $"Seq{sequence.ID} {currentB3DObj.Name}", out bool clipExisted);
                            var rootLayer     = currentUnityAnimator.layers[0];
                            var animatorState = rootLayer.stateMachine.states[3 + sequence.ID];
                            currentUnityAnimator.SetStateEffectiveMotion(animatorState.state, clip, currentIndex);
                        }
                    }
                }
            }
            for (int i = 0; i < target.transform.childCount; i++)
            {
                var child = target.transform.GetChild(i);
                var rpath = path + (path != "" ? "/" : "") + $"{child.gameObject.name}";
                LoadObject(child.gameObject, rpath);
            }
        }