More efficient draw order when bind pose order is keyed.

This commit is contained in:
NathanSweet 2013-09-24 11:10:10 +02:00
parent 90877fe14c
commit 2b86668f4e
6 changed files with 66 additions and 61 deletions

View File

@ -513,7 +513,8 @@ namespace Spine {
drawOrders = new int[frameCount][];
}
/** Sets the time and value of the specified keyframe. */
/** Sets the time and value of the specified keyframe.
* @param drawOrder May be null to use bind pose draw order. */
public void setFrame (int frameIndex, float time, int[] drawOrder) {
frames[frameIndex] = time;
drawOrders[frameIndex] = drawOrder;
@ -532,8 +533,13 @@ namespace Spine {
List<Slot> drawOrder = skeleton.drawOrder;
List<Slot> slots = skeleton.slots;
int[] drawOrderToSetupIndex = drawOrders[frameIndex];
for (int i = 0, n = drawOrderToSetupIndex.Length; i < n; i++)
drawOrder[i] = slots[drawOrderToSetupIndex[i]];
if (drawOrderToSetupIndex == null) {
drawOrder.Clear();
drawOrder.AddRange(slots);
} else {
for (int i = 0, n = drawOrderToSetupIndex.Length; i < n; i++)
drawOrder[i] = slots[drawOrderToSetupIndex[i]];
}
}
}
}

View File

@ -29,8 +29,8 @@
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
*****************************************************************************/
using System;
using System.IO;
using System.Collections.Generic;
@ -372,27 +372,30 @@ namespace Spine {
int slotCount = skeletonData.slots.Count;
int frameIndex = 0;
foreach (Dictionary<String, Object> drawOrderMap in values) {
int[] drawOrder = new int[slotCount];
for (int i = slotCount - 1; i >= 0; i--)
drawOrder[i] = -1;
List<Object> offsets = (List<Object>)drawOrderMap["offsets"];
int[] unchanged = new int[slotCount - offsets.Count];
int originalIndex = 0, unchangedIndex = 0;
foreach (Dictionary<String, Object> offsetMap in offsets) {
int slotIndex = skeletonData.FindSlotIndex((String)offsetMap["slot"]);
if (slotIndex == -1) throw new Exception("Slot not found: " + offsetMap["slot"]);
// Collect unchanged items.
while (originalIndex != slotIndex)
int[] drawOrder = null;
if (drawOrderMap.ContainsKey("offsets")) {
drawOrder = new int[slotCount];
for (int i = slotCount - 1; i >= 0; i--)
drawOrder[i] = -1;
List<Object> offsets = (List<Object>)drawOrderMap["offsets"];
int[] unchanged = new int[slotCount - offsets.Count];
int originalIndex = 0, unchangedIndex = 0;
foreach (Dictionary<String, Object> offsetMap in offsets) {
int slotIndex = skeletonData.FindSlotIndex((String)offsetMap["slot"]);
if (slotIndex == -1) throw new Exception("Slot not found: " + offsetMap["slot"]);
// Collect unchanged items.
while (originalIndex != slotIndex)
unchanged[unchangedIndex++] = originalIndex++;
// Set changed items.
drawOrder[originalIndex + (int)(float)offsetMap["offset"]] = originalIndex++;
}
// Collect remaining unchanged items.
while (originalIndex < slotCount)
unchanged[unchangedIndex++] = originalIndex++;
// Set changed items.
drawOrder[originalIndex + (int)(float)offsetMap["offset"]] = originalIndex++;
// Fill in unchanged items.
for (int i = slotCount - 1; i >= 0; i--)
if (drawOrder[i] == -1) drawOrder[i] = unchanged[--unchangedIndex];
}
// Collect remaining unchanged items.
while (originalIndex < slotCount)
unchanged[unchangedIndex++] = originalIndex++;
// Fill in unchanged items.
for (int i = slotCount - 1; i >= 0; i--)
if (drawOrder[i] == -1) drawOrder[i] = unchanged[--unchangedIndex];
timeline.setFrame(frameIndex++, (float)drawOrderMap["time"], drawOrder);
}
timelines.Add(timeline);
@ -415,4 +418,4 @@ namespace Spine {
}
}
}
}
}

View File

@ -594,7 +594,8 @@ public class Animation {
return drawOrders;
}
/** Sets the time of the specified keyframe. */
/** Sets the time of the specified keyframe.
* @param drawOrder May be null to use bind pose draw order. */
public void setFrame (int frameIndex, float time, int[] drawOrder) {
frames[frameIndex] = time;
drawOrders[frameIndex] = drawOrder;
@ -613,8 +614,12 @@ public class Animation {
Array<Slot> drawOrder = skeleton.drawOrder;
Array<Slot> slots = skeleton.slots;
int[] drawOrderToSetupIndex = drawOrders[frameIndex];
for (int i = 0, n = drawOrderToSetupIndex.length; i < n; i++)
drawOrder.set(i, slots.get(drawOrderToSetupIndex[i]));
if (drawOrderToSetupIndex == null)
System.arraycopy(slots.items, 0, drawOrder.items, 0, slots.size);
else {
for (int i = 0, n = drawOrderToSetupIndex.length; i < n; i++)
drawOrder.set(i, slots.get(drawOrderToSetupIndex[i]));
}
}
}
}

View File

@ -320,26 +320,30 @@ public class SkeletonJson {
int slotCount = skeletonData.slots.size;
int frameIndex = 0;
for (JsonValue drawOrderMap = drawOrdersMap.child; drawOrderMap != null; drawOrderMap = drawOrderMap.next()) {
int[] drawOrder = new int[slotCount];
for (int i = slotCount - 1; i >= 0; i--)
drawOrder[i] = -1;
int[] unchanged = new int[slotCount - drawOrderMap.get("offsets").size];
int originalIndex = 0, unchangedIndex = 0;
for (JsonValue offsetMap = drawOrderMap.getChild("offsets"); offsetMap != null; offsetMap = offsetMap.next()) {
int slotIndex = skeletonData.findSlotIndex(offsetMap.getString("slot"));
if (slotIndex == -1) throw new SerializationException("Slot not found: " + offsetMap.getString("slot"));
// Collect unchanged items.
while (originalIndex != slotIndex)
int[] drawOrder = null;
JsonValue offsets = drawOrderMap.get("offsets");
if (offsets != null) {
drawOrder = new int[slotCount];
for (int i = slotCount - 1; i >= 0; i--)
drawOrder[i] = -1;
int[] unchanged = new int[slotCount - offsets.size];
int originalIndex = 0, unchangedIndex = 0;
for (JsonValue offsetMap = offsets.child; offsetMap != null; offsetMap = offsetMap.next()) {
int slotIndex = skeletonData.findSlotIndex(offsetMap.getString("slot"));
if (slotIndex == -1) throw new SerializationException("Slot not found: " + offsetMap.getString("slot"));
// Collect unchanged items.
while (originalIndex != slotIndex)
unchanged[unchangedIndex++] = originalIndex++;
// Set changed items.
drawOrder[originalIndex + offsetMap.getInt("offset")] = originalIndex++;
}
// Collect remaining unchanged items.
while (originalIndex < slotCount)
unchanged[unchangedIndex++] = originalIndex++;
// Set changed items.
drawOrder[originalIndex + offsetMap.getInt("offset")] = originalIndex++;
// Fill in unchanged items.
for (int i = slotCount - 1; i >= 0; i--)
if (drawOrder[i] == -1) drawOrder[i] = unchanged[--unchangedIndex];
}
// Collect remaining unchanged items.
while (originalIndex < slotCount)
unchanged[unchangedIndex++] = originalIndex++;
// Fill in unchanged items.
for (int i = slotCount - 1; i >= 0; i--)
if (drawOrder[i] == -1) drawOrder[i] = unchanged[--unchangedIndex];
timeline.setFrame(frameIndex++, drawOrderMap.getFloat("time"), drawOrder);
}
timelines.add(timeline);

View File

@ -300,17 +300,7 @@
{ "slot": "eyes", "offset": 3 }
]
},
{
"time": 2.6206,
"offsets": [
{ "slot": "head", "offset": -12 },
{ "slot": "eyes", "offset": -12 }
]
},
{
"time": 3.5862,
"offsets": []
}
{ "time": 2 }
]
},
"jump": {

View File

@ -307,10 +307,7 @@
{ "slot": "eyes", "offset": -12 }
]
},
{
"time": 3.5862,
"offsets": []
}
{ "time": 3.5862 }
]
},
"jump": {