blob: f5a8dcfa6f14d21d07f6c680d1d926837b549580 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
using UnityEngine;
using System.Collections.Generic;
namespace Pathfinding {
using Pathfinding.Util;
using Pathfinding.Drawing;
[HelpURL("https://arongranberg.com/astar/documentation/stable/animationlink.html")]
public class AnimationLink : NodeLink2 {
public string clip;
public float animSpeed = 1;
public bool reverseAnim = true;
public GameObject referenceMesh;
public LinkClip[] sequence;
public string boneRoot = "bn_COG_Root";
[System.Serializable]
public class LinkClip {
public AnimationClip clip;
public Vector3 velocity;
public int loopCount = 1;
public string name {
get {
return clip != null ? clip.name : "";
}
}
}
static Transform SearchRec (Transform tr, string name) {
int childCount = tr.childCount;
for (int i = 0; i < childCount; i++) {
Transform ch = tr.GetChild(i);
if (ch.name == name) return ch;
else {
Transform rec = SearchRec(ch, name);
if (rec != null) return rec;
}
}
return null;
}
public void CalculateOffsets (List<Vector3> trace, out Vector3 endPosition) {
//Vector3 opos = transform.position;
endPosition = transform.position;
if (referenceMesh == null) return;
GameObject ob = GameObject.Instantiate(referenceMesh, transform.position, transform.rotation) as GameObject;
ob.hideFlags = HideFlags.HideAndDontSave;
Transform root = SearchRec(ob.transform, boneRoot);
if (root == null) throw new System.Exception("Could not find root transform");
Animation anim = ob.GetComponent<Animation>();
if (anim == null) anim = ob.AddComponent<Animation>();
for (int i = 0; i < sequence.Length; i++) {
anim.AddClip(sequence[i].clip, sequence[i].clip.name);
}
Vector3 prevOffset = Vector3.zero;
Vector3 position = transform.position;
Vector3 firstOffset = Vector3.zero;
for (int i = 0; i < sequence.Length; i++) {
LinkClip c = sequence[i];
if (c == null) {
endPosition = position;
return;
}
anim[c.clip.name].enabled = true;
anim[c.clip.name].weight = 1;
for (int repeat = 0; repeat < c.loopCount; repeat++) {
anim[c.clip.name].normalizedTime = 0;
anim.Sample();
Vector3 soffset = root.position - transform.position;
if (i > 0) {
position += prevOffset - soffset;
} else {
firstOffset = soffset;
}
for (int t = 0; t <= 20; t++) {
float tf = t/20.0f;
anim[c.clip.name].normalizedTime = tf;
anim.Sample();
Vector3 tmp = position + (root.position-transform.position) + c.velocity*tf*c.clip.length;
trace.Add(tmp);
}
position = position + c.velocity*1*c.clip.length;
anim[c.clip.name].normalizedTime = 1;
anim.Sample();
Vector3 eoffset = root.position - transform.position;
prevOffset = eoffset;
}
anim[c.clip.name].enabled = false;
anim[c.clip.name].weight = 0;
}
position += prevOffset - firstOffset;
GameObject.DestroyImmediate(ob);
endPosition = position;
}
public override void DrawGizmos () {
base.DrawGizmos();
List<Vector3> buffer = Pathfinding.Util.ListPool<Vector3>.Claim();
Vector3 endPosition = Vector3.zero;
CalculateOffsets(buffer, out endPosition);
for (int i = 0; i < buffer.Count-1; i++) {
Draw.Line(buffer[i], buffer[i+1], Color.blue);
}
}
}
}
|