From 04504925541b6483b32961e92ec06c197c449308 Mon Sep 17 00:00:00 2001 From: "Ivan \"KaiSD\" Korystin" Date: Sat, 21 Jun 2014 02:00:27 +0400 Subject: [PATCH] Fixed a bug with multiple rotations at the same time. Made sample actions behavior a subclass of the Actor class. Added new subclass for the Actor - MeshActor. This actor let's the actions to modify the mesh (i.e. Skew). Added new actions: ActionSkewBy.cs ActionRandom.cs --- README.md | 24 ++++--- src/ActionsBase/ActionRandom.cs | 69 +++++++++++++++++++ src/ActionsInstant/ActionSetRotation.cs | 10 +-- src/ActionsInterval/ActionRotateBy.cs | 4 +- src/ActionsInterval/ActionRotateTo.cs | 4 +- src/ActionsInterval/ActionSkewBy.cs | 45 ++++++++++++ src/UnityComponents/Actor.cs | 2 +- .../ActorSampleActions.cs} | 13 +++- src/UnityComponents/MeshActor.cs | 54 +++++++++++++++ src/UnityComponents/SkewModifier.cs | 51 -------------- 10 files changed, 198 insertions(+), 78 deletions(-) create mode 100644 src/ActionsBase/ActionRandom.cs create mode 100644 src/ActionsInterval/ActionSkewBy.cs rename src/{Examples/SampleActions.cs => UnityComponents/ActorSampleActions.cs} (68%) create mode 100644 src/UnityComponents/MeshActor.cs delete mode 100644 src/UnityComponents/SkewModifier.cs diff --git a/README.md b/README.md index 251b3b8..448e44d 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Since actions are quite simple, i implemented them myself. ### How to use -Just put the src folder into your assets folder. + 1. Put the src folder into your assets folder. Then attach the Actor.cs script to the object you want. See the examples folder, there's a sample script there. @@ -22,7 +22,7 @@ Base actions - [x] Sequence - [x] Parallel - [x] Repeat and Loop *implemented in one action* -- [ ] Reverse *not yet, but every reversable action has working reverse() method* +- ~[ ] Reverse~ *Every reversable action has a reverse() method. So, no plans for Reverse action right now.* Interval actions - [x] Delay and RandomDelay *implemented in one action* @@ -32,17 +32,18 @@ Interval actions - [x] RotateBy - [x] ScaleTo - [x] ScaleBy -- [x] TintBy *if you want to manipulate alpha, your material must support transparency* -- [x] TintTo *if you want to manipulate alpha, your material must support transparency* -- [x] FadeOut *if you want to manipulate alpha, your material must support transparency* -- [x] FadeIn *if you want to manipulate alpha, your material must support transparency* -- [x] FadeTo *if you want to manipulate alpha, your material must support transparency* -- [x] FadeBy *if you want to manipulate alpha, your material must support transparency* - [x] BezierAbs - [x] BezierRel - [x] JumpTo *uses Bezier action to move the object* - [x] JumpBy *uses Bezier action to move the object* - [x] Blink +*if you want to manipulate alpha with the following actions, your material should support transparency (i.e. use Transparent shader)* +- [x] TintBy +- [x] TintTo +- [x] FadeOut +- [x] FadeIn +- [x] FadeTo +- [x] FadeBy Instant actions - [x] Place *renamed to SetPlace* @@ -51,10 +52,15 @@ Instant actions - [x] Show - [x] ToggleVisibility +There's no plans for 3DGrid actions. You can use the 3D actions instead. + ### Some additional actions +Base actions +- [x] Random *Randomly choises and does one action from the given list.* Interval actions -- [ ] SkewBy +*These two actions requires the using of MeshActor script instead of Actor.* +- [x] SkewBy - [ ] SkewTo Instant actions diff --git a/src/ActionsBase/ActionRandom.cs b/src/ActionsBase/ActionRandom.cs new file mode 100644 index 0000000..d3de7e1 --- /dev/null +++ b/src/ActionsBase/ActionRandom.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +class ActionRandom : ActionInterval +{ + + protected Action[] actions; + protected int index; + + public ActionRandom(Action action1, Action action2) + : base(0) + { + actions = new Action[] {action1, action2}; + } + + public ActionRandom(Action[] actionsList) + : base(0) + { + actions = actionsList; + + } + + public override Action clone() + { + Action[] aList = new Action[actions.Length]; + for (int i = 0; i < actions.Length; i++) + { + aList[i] = actions[i].clone(); + } + return new ActionRandom(aList); + } + + public override Action reverse() + { + Action[] aList = new Action[actions.Length]; + for (int i = 0; i < actions.Length; i++) + { + aList[i] = actions[i].reverse(); + } + return new ActionRandom(aList); + } + + public override void start() + { + base.start(); + index = UnityEngine.Random.Range(0, actions.Length); + actions[index].setActor(target); + actions[index].start(); + } + + public override void step(float dt) + { + dt *= timeScale; + if (actions[index].isRunning()) + actions[index].step(dt); + if (!actions[index].isRunning()) + { + stop(); + dtr = actions[index].dtr; + } + } + + public override void stop() + { + base.stop(); + actions[index].stop(); + } +} \ No newline at end of file diff --git a/src/ActionsInstant/ActionSetRotation.cs b/src/ActionsInstant/ActionSetRotation.cs index 8710eac..85e9f41 100644 --- a/src/ActionsInstant/ActionSetRotation.cs +++ b/src/ActionsInstant/ActionSetRotation.cs @@ -26,13 +26,7 @@ class ActionSetRotation : Action public override void start() { base.start(); - Vector3 path = new Vector3(); - for (int i = 0; i < 3; i++) - { - path[i] = value[i] - transform.rotation.eulerAngles[i]; - } - transform.Rotate(Vector3.up, path.y); - transform.Rotate(Vector3.right, path.x); - transform.Rotate(Vector3.forward, path.z); + transform.rotation = Quaternion.Euler(value); + } } \ No newline at end of file diff --git a/src/ActionsInterval/ActionRotateBy.cs b/src/ActionsInterval/ActionRotateBy.cs index 3883a71..316bb24 100644 --- a/src/ActionsInterval/ActionRotateBy.cs +++ b/src/ActionsInterval/ActionRotateBy.cs @@ -32,8 +32,6 @@ class ActionRotateBy : ActionInterval { float d = dt / duration; Vector3 tgt = delta * d; - transform.Rotate(Vector3.up, tgt.y); - transform.Rotate(Vector3.right, tgt.x); - transform.Rotate(Vector3.forward, tgt.z); + transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + tgt); } } \ No newline at end of file diff --git a/src/ActionsInterval/ActionRotateTo.cs b/src/ActionsInterval/ActionRotateTo.cs index a5c69b2..7713ee4 100644 --- a/src/ActionsInterval/ActionRotateTo.cs +++ b/src/ActionsInterval/ActionRotateTo.cs @@ -42,8 +42,6 @@ class ActionRotateTo : ActionInterval { float d = dt / duration; Vector3 tgt = path * d; - transform.Rotate(Vector3.up, tgt.y); - transform.Rotate(Vector3.right, tgt.x); - transform.Rotate(Vector3.forward, tgt.z); + transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + tgt); } } \ No newline at end of file diff --git a/src/ActionsInterval/ActionSkewBy.cs b/src/ActionsInterval/ActionSkewBy.cs new file mode 100644 index 0000000..8716dec --- /dev/null +++ b/src/ActionsInterval/ActionSkewBy.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +class ActionSkewBy : ActionInterval +{ + protected Vector3 skewAngles1; + protected Vector3 skewAngles2; + protected Mesh mesh; + + public ActionSkewBy(Vector3 tgtAngles1, Vector3 tgtAngles2, float tgtDuration) + : base(tgtDuration) + { + skewAngles1 = tgtAngles1; + skewAngles2 = tgtAngles2; + } + + public override Action clone() + { + return new ActionSkewBy(skewAngles1, skewAngles2, duration); + } + + public override Action reverse() + { + return new ActionSkewBy(-skewAngles1, -skewAngles2, duration); + } + + public override void start() + { + base.start(); + if (!(target is MeshActor)) + { + throw new Exception("You should use MeshActor class instead of Actor, if you want to skew your object."); + } + } + + public override void stepInterval(float dt) + { + float d = dt / duration; + Vector3 tgt = skewAngles1 * d; + ((MeshActor)target).skewAngles1 += tgt; + tgt = skewAngles2 * d; + ((MeshActor)target).skewAngles2 += tgt; + } +} \ No newline at end of file diff --git a/src/UnityComponents/Actor.cs b/src/UnityComponents/Actor.cs index 3692882..0834335 100644 --- a/src/UnityComponents/Actor.cs +++ b/src/UnityComponents/Actor.cs @@ -6,7 +6,7 @@ public class Actor : MonoBehaviour protected Action action; private bool paused = false; - void Update() + protected void Update() { if (paused || action == null) return; diff --git a/src/Examples/SampleActions.cs b/src/UnityComponents/ActorSampleActions.cs similarity index 68% rename from src/Examples/SampleActions.cs rename to src/UnityComponents/ActorSampleActions.cs index 2d59edb..980f728 100644 --- a/src/Examples/SampleActions.cs +++ b/src/UnityComponents/ActorSampleActions.cs @@ -1,7 +1,7 @@ using UnityEngine; using System.Collections; -public class SampleActions : MonoBehaviour +public class ActorSampleActions : MeshActor { public void Start() { @@ -10,7 +10,7 @@ public class SampleActions : MonoBehaviour new ActionFadeIn(2), new ActionParallel(new Action[] { new ActionMoveBy(new Vector3(10, 10, 0), 1), - new ActionRotateBy(new Vector3(90, 0, 0), 1), + new ActionRotateBy(new Vector3(90, 90, 0), 1), new ActionTintBy(new Vector4(-50, 50, -150), 1) }), new ActionScaleBy(new Vector3(2, 2, 1), 1), @@ -18,9 +18,15 @@ public class SampleActions : MonoBehaviour new ActionDelay(1), new ActionBlink(5, 0.1F, 0.4F), new ActionDelay(1), + new ActionSkewBy(Vector3.zero, new Vector3(0, 30, 0), 1), + new ActionDelay(1), new ActionJumpBy(new Vector3(-10, 0, 0), 1, 4, 1), + new ActionSkewBy(Vector3.zero, new Vector3(30, 0, 30), 1), new ActionJumpTo(new Vector3(10, 10, 10), 1, 3, 1), + new ActionRotateBy(new Vector3(90, 0, 0), 1), + new ActionSkewBy(Vector3.zero, new Vector3(-30, 0, -30), 1), new ActionJumpBy(new Vector3(-10, 0, 0), 1, 2, 1), + new ActionSkewBy(Vector3.zero, new Vector3(0, -30, 0), 1), new ActionDelay(0.5F), new ActionBezierRel(new Vector2 (5, 0), new Vector2(5, -10), new Vector2 (0, -10), 2), new ActionScaleTo(new Vector3(2, 2, 2), 1), @@ -28,6 +34,7 @@ public class SampleActions : MonoBehaviour new ActionFadeOut(2), new ActionSetTint(new Vector4(67, 105, 181)) }), 5); - gameObject.SendMessage("AttachAction", seq); + SetTimeScale(2); + this.AttachAction(seq); } } diff --git a/src/UnityComponents/MeshActor.cs b/src/UnityComponents/MeshActor.cs new file mode 100644 index 0000000..173bbe6 --- /dev/null +++ b/src/UnityComponents/MeshActor.cs @@ -0,0 +1,54 @@ +using UnityEngine; +using System.Collections; + +[System.Serializable] +public class MeshActor : Actor +{ + public Vector3 skewAngles1; + public Vector3 skewAngles2; + protected Vector3 angles1prev = Vector3.zero; + protected Vector3 angles2prev = Vector3.zero; + protected const float coeff = Mathf.PI/180; + protected Mesh mesh; + protected Vector3[] initialState; + + void Awake() + { + mesh = gameObject.GetComponent().mesh; + initialState = mesh.vertices; + } + + protected void Update() + { + base.Update(); + if (skewAngles1 != angles1prev || skewAngles2 != angles2prev) + updateMesh(); + } + + protected void updateMesh() + { + Matrix4x4 m = Matrix4x4.zero; + m[0, 0] = 1; + m[1, 1] = 1; + m[2, 2] = 1; + m[3, 3] = 1; + m[0, 1] = Mathf.Tan(skewAngles1.x * coeff); // skewing in xy + m[0, 2] = Mathf.Tan(skewAngles2.x * coeff); // skewing in xz + m[1, 0] = Mathf.Tan(skewAngles1.y * coeff); // skewing in yx + m[1, 2] = Mathf.Tan(skewAngles2.y * coeff); // skewing in yz + m[2, 0] = Mathf.Tan(skewAngles1.z * coeff); // skewing in zx + m[2, 1] = Mathf.Tan(skewAngles2.z * coeff); // skewing in zy + + Vector3[] verts = new Vector3[initialState.Length]; + int i = 0; + while (i < verts.Length) + { + verts[i] = m.MultiplyPoint3x4(initialState[i]); + i++; + } + + mesh.vertices = verts; + angles1prev = skewAngles1; + angles2prev = skewAngles2; + } +} diff --git a/src/UnityComponents/SkewModifier.cs b/src/UnityComponents/SkewModifier.cs deleted file mode 100644 index b2cc83f..0000000 --- a/src/UnityComponents/SkewModifier.cs +++ /dev/null @@ -1,51 +0,0 @@ -using UnityEngine; -using System.Collections; - -[System.Serializable] -public class SkewModifier : MonoBehaviour -{ - public Vector3 angles1; - public Vector3 angles2; - protected Vector3 angles1prev = Vector3.zero; - protected Vector3 angles2prev = Vector3.zero; - protected const float coeff = Mathf.PI/180; - protected Mesh mesh; - - void Awake() - { - mesh = gameObject.GetComponent().mesh; - } - - void Update() - { - if (angles1 != angles1prev || angles2 != angles2prev) - updateMesh(); - } - - protected void updateMesh() - { - Matrix4x4 m = Matrix4x4.zero; - m[0, 0] = 1; - m[1, 1] = 1; - m[2, 2] = 1; - m[3, 3] = 1; - m[0, 1] = Mathf.Tan(angles1.x * coeff) - Mathf.Tan(angles1prev.x * coeff); // skewing in xy - m[0, 2] = Mathf.Tan(angles2.x * coeff) - Mathf.Tan(angles2prev.x * coeff); // skewing in xz - m[1, 0] = Mathf.Tan(angles1.y * coeff) - Mathf.Tan(angles1prev.y * coeff); // skewing in yx - m[1, 2] = Mathf.Tan(angles2.y * coeff) - Mathf.Tan(angles2prev.y * coeff); // skewing in yz - m[2, 0] = Mathf.Tan(angles1.z * coeff) - Mathf.Tan(angles1prev.z * coeff); // skewing in zx - m[2, 1] = Mathf.Tan(angles2.z * coeff) - Mathf.Tan(angles2prev.z * coeff); // skewing in zy - - Vector3[] verts = mesh.vertices; - int i = 0; - while (i < verts.Length) - { - verts[i] = m.MultiplyPoint3x4(verts[i]); - i++; - } - - mesh.vertices = verts; - angles1prev = angles1; - angles2prev = angles2; - } -}