Пример #1
0
    public void UpdateAnimation()
    {
        if (IsLoaded && shAnim != null && animCuts != null && animCuts.Length > 0 && channelObjects != null && subObjects != null)
        {
            if (currentFrame >= shAnim.num_frames)
            {
                currentFrame %= shAnim.num_frames;
            }
            UpdateAnimationCut();
            // First pass: reset TRS for all sub objects
            for (int i = 0; i < channelParents[currentAnimCut].Length; i++)
            {
                channelParents[currentAnimCut][i] = false;
            }
            ROMAnimation  anim = ROMStruct.Loader.romAnims[animCuts[currentAnimCut].index];
            uint          currentRelativeFrame = currentFrame - anim.a3d.this_start_onlyFrames;
            AnimOnlyFrame of = anim.onlyFrames[currentRelativeFrame];
            // Create hierarchy for this frame
            for (int i = of.start_hierarchies_for_frame;
                 i < of.start_hierarchies_for_frame + of.num_hierarchies_for_frame; i++)
            {
                AnimHierarchy h = anim.hierarchies[i];

                if (!channelIDDictionary[currentAnimCut].ContainsKey(h.childChannelID) || !channelIDDictionary[currentAnimCut].ContainsKey(h.parentChannelID))
                {
                    continue;
                }
                List <int> ch_child_list  = GetChannelByID(h.childChannelID);
                List <int> ch_parent_list = GetChannelByID(h.parentChannelID);
                foreach (int ch_child in ch_child_list)
                {
                    foreach (int ch_parent in ch_parent_list)
                    {
                        channelObjects[currentAnimCut][ch_child].transform.SetParent(channelObjects[currentAnimCut][ch_parent].transform);
                        channelParents[currentAnimCut][ch_child] = true;
                    }
                }

                //channelObjects[ch_child].transform.SetParent(channelObjects[ch_parent].transform);
            }

            // Final pass
            int currentKFIndex = 0;
            for (int i = 0; i < anim.channels.Length; i++)
            {
                AnimChannel ch = anim.channels[i];

                // Select keyframe
                AnimKeyframe kf = null;
                int          selectedKFindex = 0;
                for (int k = 0; k < ch.num_keyframes; k++)
                {
                    if (anim.keyframes[currentKFIndex + k].frame <= currentFrame)
                    {
                        kf = anim.keyframes[currentKFIndex + k];
                        selectedKFindex = k;
                    }
                    else
                    {
                        break;
                    }
                }
                AnimVector     pos            = anim.vectors[kf.positionVector];
                AnimQuaternion qua            = anim.quaternions[kf.quaternion];
                AnimVector     scl            = anim.vectors[kf.scaleVector];
                AnimNumOfNTTO  numOfNTTO      = anim.numOfNTTO[(i * anim.a3d.num_numNTTO) + of.numOfNTTO];
                AnimNTTO       ntto           = null;
                int            poNum          = -1;
                GameObject     physicalObject = null;
                if (numOfNTTO.numOfNTTO != 0xFFFF)
                {
                    poNum          = numOfNTTO.numOfNTTO;
                    ntto           = anim.ntto[poNum];
                    physicalObject = subObjects[currentAnimCut][i][poNum];
                }
                Vector3      vector        = pos.vector;
                Quaternion   quaternion    = qua.quaternion;
                Vector3      scale         = scl.vector;
                int          framesSinceKF = (int)currentFrame - (int)kf.frame;
                AnimKeyframe nextKF        = null;
                int          framesDifference;
                float        interpolation;
                if (selectedKFindex == ch.num_keyframes - 1)
                {
                    nextKF           = anim.keyframes[currentKFIndex];
                    framesDifference = anim.a3d.total_num_onlyFrames - 1 + (int)nextKF.frame - (int)kf.frame;
                    if (framesDifference == 0)
                    {
                        interpolation = 0;
                    }
                    else
                    {
                        //interpolation = (float)(nextKF.interpolationFactor * (framesSinceKF / (float)framesDifference) + 1.0 * nextKF.interpolationFactor);
                        interpolation = framesSinceKF / (float)framesDifference;
                    }
                }
                else
                {
                    nextKF           = anim.keyframes[currentKFIndex + selectedKFindex + 1];
                    framesDifference = (int)nextKF.frame - (int)kf.frame;
                    //interpolation = (float)(nextKF.interpolationFactor * (framesSinceKF / (float)framesDifference) + 1.0 * nextKF.interpolationFactor);
                    interpolation = framesSinceKF / (float)framesDifference;
                }

                currentKFIndex += ch.num_keyframes;


                //print(interpolation);
                //print(a3d.vectors.Length + " - " + nextKF.positionVector);
                //print(perso.p3dData.family.animBank);
                AnimVector     pos2 = anim.vectors[nextKF.positionVector];
                AnimQuaternion qua2 = anim.quaternions[nextKF.quaternion];
                AnimVector     scl2 = anim.vectors[nextKF.scaleVector];
                vector     = Vector3.Lerp(pos.vector, pos2.vector, interpolation);
                quaternion = Quaternion.Lerp(qua.quaternion, qua2.quaternion, interpolation);
                scale      = Vector3.Lerp(scl.vector, scl2.vector, interpolation);
                //float positionMultiplier = Mathf.Lerp(kf.positionMultiplier, nextKF.positionMultiplier, interpolation);

                if (poNum != currentActivePO[currentAnimCut][i])
                {
                    if (currentActivePO[currentAnimCut][i] == -2 && fullMorphPOs[currentAnimCut] != null && fullMorphPOs[currentAnimCut][i] != null)
                    {
                        foreach (GameObject morphPO in fullMorphPOs[currentAnimCut][i].Values)
                        {
                            if (morphPO.activeSelf)
                            {
                                morphPO.SetActive(false);
                            }
                        }
                    }
                    if (currentActivePO[currentAnimCut][i] >= 0 && subObjects[currentAnimCut][i][currentActivePO[currentAnimCut][i]] != null)
                    {
                        subObjects[currentAnimCut][i][currentActivePO[currentAnimCut][i]].SetActive(false);
                    }
                    currentActivePO[currentAnimCut][i] = poNum;
                    if (physicalObject != null)
                    {
                        physicalObject.SetActive(true);
                    }
                }
                if (!channelParents[currentAnimCut][i])
                {
                    channelObjects[currentAnimCut][i].transform.SetParent(transform);
                }
                channelObjects[currentAnimCut][i].transform.localPosition = vector;                // * positionMultiplier;
                channelObjects[currentAnimCut][i].transform.localRotation = quaternion;
                channelObjects[currentAnimCut][i].transform.localScale    = scale;

                if (physicalObject != null &&
                    anim.a3d.num_morphData > 0 &&
                    morphDataArray[currentAnimCut] != null &&
                    i < morphDataArray[currentAnimCut].GetLength(0) &&
                    currentRelativeFrame < morphDataArray[currentAnimCut].GetLength(1))
                {
                    AnimMorphData   morphData = morphDataArray[currentAnimCut][i, currentRelativeFrame];
                    GeometricObject ogPO      = perso.p3dData.Value.objectsTable.Value.data.Value.entries[anim.ntto[poNum].object_index].obj.Value.visual.Value;
                    if (morphData != null && morphData.morphProgress != 0 && morphData.morphProgress != 100)
                    {
                        //print("morphing " + physicalObject.name);
                        GeometricObject morphToPO = perso.p3dData.Value.objectsTable.Value.data.Value.entries[morphData.objectIndexTo].obj.Value.visual.Value;
                        ogPO.MorphVertices(physicalObject, morphToPO, morphData.morphProgress / 100f);
                    }
                    else if (morphData != null && morphData.morphProgress == 100)
                    {
                        physicalObject.SetActive(false);
                        GameObject c = fullMorphPOs[currentAnimCut][i][morphData.objectIndexTo];
                        c.transform.localScale    = objectIndexScales.ContainsKey(morphData.objectIndexTo) ? objectIndexScales[morphData.objectIndexTo] : Vector3.one;
                        c.transform.localPosition = Vector3.zero;
                        c.transform.localRotation = Quaternion.identity;
                        c.SetActive(true);
                        currentActivePO[currentAnimCut][i] = -2;
                    }
                    else
                    {
                        ogPO.ResetMorph(physicalObject);
                    }
                }
            }
        }
    }