瀏覽代碼

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
master
父節點
當前提交
0450492554
共有 10 個檔案被更改,包括 198 行新增78 行删除
  1. +15
    -9
      README.md
  2. +69
    -0
      src/ActionsBase/ActionRandom.cs
  3. +2
    -8
      src/ActionsInstant/ActionSetRotation.cs
  4. +1
    -3
      src/ActionsInterval/ActionRotateBy.cs
  5. +1
    -3
      src/ActionsInterval/ActionRotateTo.cs
  6. +45
    -0
      src/ActionsInterval/ActionSkewBy.cs
  7. +1
    -1
      src/UnityComponents/Actor.cs
  8. +10
    -3
      src/UnityComponents/ActorSampleActions.cs
  9. +54
    -0
      src/UnityComponents/MeshActor.cs
  10. +0
    -51
      src/UnityComponents/SkewModifier.cs

+ 15
- 9
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


+ 69
- 0
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();
}
}

+ 2
- 8
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);

}
}

+ 1
- 3
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);
}
}

+ 1
- 3
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);
}
}

+ 45
- 0
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;
}
}

+ 1
- 1
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;


src/Examples/SampleActions.cs → 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);
}
}

+ 54
- 0
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<MeshFilter>().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;
}
}

+ 0
- 51
src/UnityComponents/SkewModifier.cs 查看文件

@@ -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<MeshFilter>().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;
}
}