Skip to content

Instantly share code, notes, and snippets.

@Tom-Ski
Created April 26, 2014 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tom-Ski/11330686 to your computer and use it in GitHub Desktop.
Save Tom-Ski/11330686 to your computer and use it in GitHub Desktop.
Old Timeline
public FfdTimeline (int frameCount) {
super(frameCount);
frames = new float[frameCount];
frameVertices = new float[frameCount][];
}
public void setSlotIndex (int slotIndex) {
this.slotIndex = slotIndex;
}
public int getSlotIndex () {
return slotIndex;
}
public void setMeshAttachment (MeshAttachment attachment) {
this.meshAttachment = attachment;
}
public MeshAttachment getMeshAttachment () {
return meshAttachment;
}
public float[] getFrames () {
return frames;
}
public float[][] getVertices () {
return frameVertices;
}
/** Sets the time of the specified keyframe. */
public void setFrame (int frameIndex, float time, float[] vertices) {
frames[frameIndex] = time;
frameVertices[frameIndex] = vertices;
}
public void apply (Skeleton skeleton, float lastTime, float time, Array<Event> firedEvents, float alpha) {
Slot slot = skeleton.slots.get(slotIndex);
if (slot.getAttachment() != meshAttachment) return;
FloatArray verticesArray = slot.getAttachmentVertices();
verticesArray.size = 0;
float[] frames = this.frames;
if (time < frames[0]) return; // Time is before first frame.
float[][] frameVertices = this.frameVertices;
int vertexCount = frameVertices[0].length;
verticesArray.ensureCapacity(vertexCount);
verticesArray.size = vertexCount;
float[] vertices = verticesArray.items;
if (time >= frames[frames.length - 1]) { // Time is after last frame.
System.arraycopy(frameVertices[frames.length - 1], 0, vertices, 0, vertexCount);
return;
}
// Interpolate between the previous frame and the current frame.
int frameIndex = binarySearch(frames, time, 1);
float frameTime = frames[frameIndex];
float percent = MathUtils.clamp(1 - (time - frameTime) / (frames[frameIndex - 1] - frameTime), 0, 1);
percent = getCurvePercent(frameIndex - 1, percent);
float[] prevVertices = frameVertices[frameIndex - 1];
float[] nextVertices = frameVertices[frameIndex];
// BOZO - FFD, use alpha for mixing?
for (int i = 0; i < vertexCount; i++) {
float prev = prevVertices[i];
vertices[i] = prev + (nextVertices[i] - prev) * percent;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment