Exemplo n.º 1
0
    public float GetCursorPosition(OJKeyframe kf1, OJKeyframe kf2)
    {
        float min    = kf1.time;
        float cursor = currentTime;
        float max    = kf2.time;

        return((cursor - min) / (max - min));
    }
Exemplo n.º 2
0
    private void DrawHandles(OJSequence sequence, OJKeyframe kf)
    {
        if (kf.curve.before != Vector3.zero)
        {
            Handles.DrawLine(sequence.transform.position + kf.position, sequence.transform.position + kf.position + kf.curve.before);
        }

        if (kf.curve.after != Vector3.zero)
        {
            Handles.DrawLine(sequence.transform.position + kf.position, sequence.transform.position + kf.position + kf.curve.after);
        }
    }
Exemplo n.º 3
0
    public void OnSceneGUI()
    {
        OJSequence sequence = (OJSequence)target;

        if (currentEditorKeyframe >= sequence.keyframes.Count)
        {
            currentEditorKeyframe = 0;
        }

        if (sequence.keyframes.Count > 1)
        {
            for (int k = 1; k < sequence.keyframes.Count; k++)
            {
                Handles.color = new Color(1f, 1f, 1f, 0.5f);

                OJKeyframe kf1 = sequence.keyframes [k - 1];
                OJKeyframe kf2 = sequence.keyframes [k];

                for (float t = 0.05f; t <= 1.05f; t += 0.05f)
                {
                    Vector3 p1 = OJSequence.CalculateBezierPoint(t - 0.05f, sequence.transform.position + kf1.position, sequence.transform.position + kf1.position + kf1.curve.after, sequence.transform.position + kf2.position + kf2.curve.before, sequence.transform.position + kf2.position);
                    Vector3 p2 = OJSequence.CalculateBezierPoint(t, sequence.transform.position + kf1.position, sequence.transform.position + kf1.position + kf1.curve.after, sequence.transform.position + kf2.position + kf2.curve.before, sequence.transform.position + kf2.position);

                    Handles.DrawLine(p1, p2);
                }
            }

            OJKeyframe kf = sequence.keyframes [currentEditorKeyframe];

            Handles.color = new Color(0f, 1f, 1f, 0.5f);
            DrawHandles(sequence, kf);

            Vector3 before = kf.curve.before;
            kf.curve.before = Handles.PositionHandle(sequence.transform.position + kf.position + kf.curve.before, Quaternion.Euler(Vector3.zero)) - kf.position - sequence.transform.position;

            Vector3 after = kf.curve.after;
            kf.curve.after = Handles.PositionHandle(sequence.transform.position + kf.position + kf.curve.after, Quaternion.Euler(Vector3.zero)) - kf.position - sequence.transform.position;

            if (kf.curve.symmetrical)
            {
                if (before != kf.curve.before)
                {
                    kf.MirrorCurveAfter();
                }
                else if (after != kf.curve.after)
                {
                    kf.MirrorCurveBefore();
                }
            }
        }
    }
Exemplo n.º 4
0
    public void SetCamera(OJKeyframe kf)
    {
        cam.fieldOfView = kf.fov;

        fadeTex.SetPixels(new Color[] {
            new Color(0, 0, 0, 1 - kf.brightness),
            new Color(0, 0, 0, 1 - kf.brightness),
            new Color(0, 0, 0, 1 - kf.brightness),
            new Color(0, 0, 0, 1 - kf.brightness)
        });
        fadeTex.Apply();

        cam.transform.position      = transform.position + kf.position;
        cam.transform.localRotation = Quaternion.Euler(kf.rotation);
    }
Exemplo n.º 5
0
    public int AddKeyframe(float time)
    {
        int i = 0;

        // Check if keyframe exists at the given time
        for (i = 0; i < keyframes.Count; i++)
        {
            if (keyframes [i].time == time)
            {
                return(i);
            }
        }

        // Create new keyframe
        OJKeyframe   kf      = new OJKeyframe();
        KeyframePair closest = FindClosestKeyframes();

        if (closest.kf1 != null && closest.kf2 != null)
        {
            float cursor = GetCursorPosition(closest.kf1, closest.kf2);
            LerpKeyframe(kf, closest.kf1, closest.kf2, cursor);
        }
        else if (closest.kf1 != null)
        {
            //kf = closest.kf1;
        }
        else if (closest.kf2 != null)
        {
            //kf = closest.kf2;
        }

        kf.time = time;

        keyframes.Add(kf);

        // Return the correct index
        for (i = 0; i < keyframes.Count; i++)
        {
            if (keyframes [i].time == time)
            {
                return(i);
            }
        }

        return(0);
    }
Exemplo n.º 6
0
    public KeyframePair FindClosestKeyframes()
    {
        OJKeyframe kf1 = null;
        OJKeyframe kf2 = null;

        for (int i = 0; i < keyframes.Count; i++)
        {
            OJKeyframe kf = keyframes [i];

            if (kf.time == currentTime || (kf.time < currentTime && (kf1 == null || Mathf.Abs(kf.time - currentTime) < Mathf.Abs(kf1.time - currentTime))))
            {
                kf1 = kf;
            }
            else if (kf.time > currentTime && (kf2 == null || Mathf.Abs(kf.time - currentTime) < Mathf.Abs(kf2.time - currentTime)))
            {
                kf2 = kf;
            }
        }

        return(new KeyframePair(kf1, kf2));
    }
Exemplo n.º 7
0
    public void LerpCamera(OJKeyframe kf1, OJKeyframe kf2, float t)
    {
        cam.fieldOfView        = Mathf.Lerp(kf1.fov, kf2.fov, t);
        cam.transform.position = CalculateBezierPoint(t, transform.position + kf1.position, transform.position + kf1.position + kf1.curve.after, transform.position + kf2.position + kf2.curve.before, transform.position + kf2.position);

        if (rotateAlongCurve)
        {
            cam.transform.LookAt(CalculateBezierPoint(t + 0.05f, transform.position + kf1.position, transform.position + kf1.position + kf1.curve.after, transform.position + kf2.position + kf2.curve.before, transform.position + kf2.position));
        }
        else
        {
            cam.transform.localRotation = Quaternion.Lerp(Quaternion.Euler(kf1.rotation), Quaternion.Euler(kf2.rotation), t);
        }

        float alpha = 1 - (Mathf.Lerp(kf1.brightness, kf2.brightness, t));

        fadeTex.SetPixels(new Color[] {
            new Color(0, 0, 0, alpha),
            new Color(0, 0, 0, alpha),
            new Color(0, 0, 0, alpha),
            new Color(0, 0, 0, alpha)
        });
        fadeTex.Apply();
    }
Exemplo n.º 8
0
    public override void OnInspectorGUI()
    {
        OJSequence sequence    = (OJSequence)target;
        float      padding     = 10;
        float      currentTime = sequence.playing ? currentEditorTime : sequence.currentTime;

        sequence.cam          = (Camera)EditorGUILayout.ObjectField("Camera", sequence.cam, typeof(Camera), true);
        sequence.eventHandler = (GameObject)EditorGUILayout.ObjectField("Event handler", sequence.eventHandler, typeof(GameObject), true);

        if (!sequence.cam)
        {
            EditorGUILayout.LabelField("Please link a camera first");
            return;
        }

        EditorGUILayout.Space();

        // Playback controls
        EditorGUILayout.BeginHorizontal();

        if (sequence.playing)
        {
            if (GUILayout.Button("STOP"))
            {
                sequence.Stop();
            }
        }
        else
        {
            if (GUILayout.Button("PLAY"))
            {
                sequence.Play();
            }
        }

        if (GUILayout.Button("RESET"))
        {
            currentEditorTime = 0;
            currentTime       = 0;
            sequence.Reset();
        }

        EditorGUILayout.EndHorizontal();

        sequence.autoPlay         = EditorGUILayout.Toggle("Autoplay", sequence.autoPlay);
        sequence.rotateAlongCurve = EditorGUILayout.Toggle("Rotate along curve", sequence.rotateAlongCurve);

        EditorGUILayout.Space();

        timelineScale = EditorGUILayout.Slider("Scale", timelineScale + 80, 100, 500) - 80;

        // Timeline
        EditorGUILayout.BeginHorizontal();

        GUILayout.Box("", GUILayout.Height(50), GUILayout.ExpandWidth(true));

        Rect rect = GUILayoutUtility.GetLastRect();

        if (!sequence.playing && GUILayout.Button("+", GUILayout.Width(32), GUILayout.ExpandHeight(true)))
        {
            currentEditorKeyframe = sequence.AddKeyframe(currentTime);
            return;
        }

        EditorGUILayout.EndHorizontal();

        GUI.BeginScrollView(rect, new Vector2(currentTime * timelineScale, 0), new Rect(0, 0, padding * 2 + sequence.length * timelineScale, 20), GUIStyle.none, GUIStyle.none);

        for (int s = 0; s <= sequence.length; s++)
        {
            GUI.Label(new Rect(padding - 5 + s * timelineScale, 0, 20, 14), s.ToString());
        }

        GUI.Box(new Rect(padding, 34, sequence.length * timelineScale, 2), "");

        GUI.color = Color.red;

        GUI.Box(new Rect(padding + currentTime * timelineScale, 20, 2, 30), "");

        for (int i = 0; i < sequence.keyframes.Count; i++)
        {
            OJKeyframe kf = sequence.keyframes [i];

            GUI.color = i == currentEditorKeyframe ? Color.green : Color.white;

            if (GUI.Button(new Rect(padding - 5f + kf.time * timelineScale, 25.0f, 10f, 20f), ""))
            {
                currentEditorKeyframe = i;
                return;
            }
        }

        GUI.color = Color.white;

        GUI.EndScrollView();

        if (!sequence.playing)
        {
            // Scrobble
            EditorGUILayout.Space();

            EditorGUILayout.BeginHorizontal();
            currentTime = EditorGUILayout.Slider(currentTime, 0, sequence.length);
            EditorGUILayout.LabelField("/", GUILayout.Width(10));
            sequence.length = EditorGUILayout.FloatField(sequence.length, GUILayout.Width(50));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Space();

            if (currentEditorKeyframe > -1 && currentEditorKeyframe < sequence.keyframes.Count)
            {
                OJKeyframe kf = sequence.keyframes [currentEditorKeyframe];
                EditorGUILayout.LabelField("# " + (currentEditorKeyframe + 1) + " / " + sequence.keyframes.Count);

                kf.stop = EditorGUILayout.Toggle("Stop", kf.stop);
                kf.time = EditorGUILayout.Slider("Time", kf.time, 0, sequence.length);

                EditorGUILayout.Space();

                // Event
                EditorGUILayout.LabelField("Event", EditorStyles.boldLabel);
                kf.evt.message  = EditorGUILayout.TextField("Message", kf.evt.message);
                kf.evt.argument = EditorGUILayout.TextField("Argument", kf.evt.argument);

                EditorGUILayout.Space();

                // Transform
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Transform", EditorStyles.boldLabel);
                if (GUILayout.Button("Copy from scene", GUILayout.Width(120)))
                {
                    kf.position = sequence.cam.transform.position - sequence.transform.position;
                    kf.rotation = sequence.cam.transform.localEulerAngles;
                }
                EditorGUILayout.EndHorizontal();

                kf.position = EditorGUILayout.Vector3Field("Position", kf.position);
                kf.rotation = EditorGUILayout.Vector3Field("Rotation", kf.rotation);

                // Curve
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Curve", EditorStyles.boldLabel);
                kf.curve.symmetrical = EditorGUILayout.Toggle("Symmetrical", kf.curve.symmetrical);
                kf.curve.before      = EditorGUILayout.Vector3Field("Before", kf.curve.before);
                kf.curve.after       = EditorGUILayout.Vector3Field("After", kf.curve.after);

                EditorGUILayout.Space();

                // Properties
                EditorGUILayout.LabelField("Properties", EditorStyles.boldLabel);
                kf.fov        = (int)EditorGUILayout.Slider("FOV", kf.fov, 1, 179);
                kf.brightness = EditorGUILayout.Slider("Brightness", kf.brightness, 0, 1);

                // Actions
                EditorGUILayout.Space();

                if (GUILayout.Button("Remove"))
                {
                    sequence.RemoveKeyframe(currentEditorKeyframe);
                }
            }
        }

        // Make sure the list of keyframes is sorted by time
        if (!sequence.playing && GUI.changed)
        {
            sequence.SetTime(currentTime);
        }

        if (!sequence.playing)
        {
            currentEditorTime = currentTime;
        }
    }
Exemplo n.º 9
0
 public KeyframePair(OJKeyframe kf1, OJKeyframe kf2)
 {
     this.kf1 = kf1;
     this.kf2 = kf2;
 }
Exemplo n.º 10
0
 public void LerpKeyframe(OJKeyframe kf, OJKeyframe kf1, OJKeyframe kf2, float percent)
 {
     kf.position = Vector3.Lerp(kf1.position, kf2.position, percent);
 }