Example #1
0
        //!
        //! activates animation editing for the currently selected object
        //!
        public void activate()
        {
            // Debug.Log("mainController.getCurrentSelection()", mainController.getCurrentSelection().gameObject);

            isActive = true;

            mainController.ActiveMode = MainController.Mode.animationEditing;
            animationTarget           = mainController.getCurrentSelection().gameObject;

            animationTarget.GetComponent <SceneObject>().setKinematic(true, false);
            animationTarget.layer = 13;     //noPhysics layer

            //no animation available yet for this object -> create animation
            if (doCreateClip && animData.getAnimationClips(animationTarget) == null)
            {
                if (!animationTarget.GetComponent <AnimationSerializer>())
                {
                    animationTarget.AddComponent <AnimationSerializer>();
                }


                //create initial animation translation
                AnimationClip  clip        = new AnimationClip();
                AnimationCurve transXcurve = new AnimationCurve();
                AnimationCurve transYcurve = new AnimationCurve();
                AnimationCurve transZcurve = new AnimationCurve();

                // create keys at current time
                transXcurve.AddKey(new Keyframe(0, animationTarget.transform.position.x, -1, 1));
                transYcurve.AddKey(new Keyframe(0, animationTarget.transform.position.y, -1, 1));
                transZcurve.AddKey(new Keyframe(0, animationTarget.transform.position.z, -1, 1));


                /*
                 * transXcurve.AddKey(new Keyframe(1, animationTarget.transform.position.x + 1, 1, -1));
                 * transYcurve.AddKey(new Keyframe(1, animationTarget.transform.position.y + 1, 1, -1));
                 * transZcurve.AddKey(new Keyframe(1, animationTarget.transform.position.z + 1, 1, -1));
                 */

                // timeline.GetComponent<TimelineScript>().updateFrames(transXcurve,clip);
                // timeLine.updateFrames(transXcurve);

                //add animation to runtime data representation
                animData.addAnimationClip(animationTarget, clip);
                animData.addAnimationCurve(clip, typeof(Transform), "m_LocalPosition.x", transXcurve);
                animData.addAnimationCurve(clip, typeof(Transform), "m_LocalPosition.y", transYcurve);
                animData.addAnimationCurve(clip, typeof(Transform), "m_LocalPosition.z", transZcurve);


                //create initial animation Rotation curve
                AnimationCurve rotXcurve = new AnimationCurve();
                AnimationCurve rotYcurve = new AnimationCurve();
                AnimationCurve rotZcurve = new AnimationCurve();
                AnimationCurve rotWcurve = new AnimationCurve();

                // create keys at current time
                rotXcurve.AddKey(new Keyframe(0, animationTarget.transform.rotation.x, -1, 1));
                rotYcurve.AddKey(new Keyframe(0, animationTarget.transform.rotation.y, -1, 1));
                rotZcurve.AddKey(new Keyframe(0, animationTarget.transform.rotation.z, -1, 1));
                rotWcurve.AddKey(new Keyframe(0, animationTarget.transform.rotation.w, -1, 1));

                /*
                 * rotXcurve.AddKey( new Keyframe( 1, animationTarget.transform.rotation.x, -1, 1 ) );
                 * rotYcurve.AddKey( new Keyframe( 1, animationTarget.transform.rotation.y, -1, 1 ) );
                 * rotZcurve.AddKey( new Keyframe( 1, animationTarget.transform.rotation.z, -1, 1 ) );
                 * rotWcurve.AddKey( new Keyframe( 1, animationTarget.transform.rotation.w, -1, 1 ) );
                 */

                // timeline.GetComponent<TimelineScript>().updateFrames( transXcurve, clip );
                // timeLine.updateFrames( transXcurve );


                //add animation to runtime data representation
                animData.addAnimationCurve(clip, typeof(Transform), "m_LocalRotation.x", rotXcurve);
                animData.addAnimationCurve(clip, typeof(Transform), "m_LocalRotation.y", rotYcurve);
                animData.addAnimationCurve(clip, typeof(Transform), "m_LocalRotation.z", rotZcurve);
                animData.addAnimationCurve(clip, typeof(Transform), "m_LocalRotation.w", rotWcurve);

                animatedObjects.Add(animationTarget.GetComponent <SceneObject>());
            }

            animationTarget.GetComponent <SceneObject>().updateAnimationCurves();


            lineRenderer.SetVertexCount(0);
            lineRenderer.enabled = true;

            updateLine();

            updateTimelineKeys();
        }
        public void setKeyFrame()
        {
            currentAnimationTime = timeLine.CurrentTime;

            if (animationTarget != null)
            {
                AnimationClip clip = initAnimationClip();



                // add animation curves if not there already
                if (animData.getAnimationCurve(clip, animationProperties[0]) == null)
                {
                    foreach (string prop in animationProperties)
                    {
                        // add property curve
                        AnimationCurve _curve = new AnimationCurve();

                        //add curve to runtime data representation
                        animData.addAnimationCurve(clip, typeof(Transform), prop, _curve);
                    }
                }


                for (int n = 0; n < animationProperties.Length; n++)
                {
                    // named property in curve
                    string prop = animationProperties[n];
                    // property delegate
                    PropertyInfo field = animationFields[n];


                    // get or create curve
                    AnimationCurve _curve = animData.getAnimationCurve(clip, prop);
                    if (_curve == null)
                    {
                        _curve = new AnimationCurve();
                    }

                    // add or move keyframe
                    bool     movedSuccessfully = false;
                    int      keyIndex          = -1;
                    Keyframe key = new Keyframe(currentAnimationTime, (float)field.GetValue(animationInstance, null));
                    for (int i = 0; i < _curve.length; i++)
                    {
                        if (Mathf.Abs(_curve.keys[i].time - currentAnimationTime) < 0.04)
                        {
                            _curve.MoveKey(i, key);
                            keyIndex          = i;
                            movedSuccessfully = true;
                        }
                    }
                    if (!movedSuccessfully)
                    {
                        keyIndex          = _curve.AddKey(key);
                        movedSuccessfully = false;
                    }
                    if (_curve.keys.Length > 1)
                    {
                        if (keyIndex == 0)
                        {
                            _curve.SmoothTangents(1, 0);
                        }
                        if (keyIndex == _curve.keys.Length - 1)
                        {
                            _curve.SmoothTangents(_curve.keys.Length - 2, 0);
                        }
                    }
                    _curve.SmoothTangents(keyIndex, 0);

                    // update animation data
                    animData.changeAnimationCurve(clip, typeof(Transform), prop, _curve);
                }

                // TODO: cheesy
                // animationTarget.GetComponent<SceneObject>().setKeyframe();
                animationTarget.GetComponent <SceneObject>().updateAnimationCurves();
                updateTimelineKeys();
                updateLine();
            }
        }