Stripped folder paths from atlas region names. Formatting.

This commit is contained in:
NathanSweet 2013-05-25 17:41:52 +02:00
parent cef7e46e5b
commit 9d4aff8da7
3 changed files with 42 additions and 79 deletions

View File

@ -4,52 +4,49 @@ using Spine;
// TODO: handle TPackerCW flip mode (probably not swap uv horizontaly)
/*
*/
public class tk2dSpineAttachmentLoader : AttachmentLoader {
/*
*/
private tk2dSpriteCollectionData sprites;
/*
*/
public tk2dSpineAttachmentLoader(tk2dSpriteCollectionData s) {
if(s == null) throw new ArgumentNullException("sprites cannot be null.");
sprites = s;
public tk2dSpineAttachmentLoader(tk2dSpriteCollectionData sprites) {
if (sprites == null) throw new ArgumentNullException("sprites cannot be null.");
this.sprites = sprites;
}
public Attachment NewAttachment(Skin skin,AttachmentType type,String name) {
if(type != AttachmentType.region) throw new Exception("Unknown attachment type: " + type);
public Attachment NewAttachment(Skin skin, AttachmentType type, String name) {
if (type != AttachmentType.region) throw new Exception("Unknown attachment type: " + type);
// Strip folder names.
int index = name.LastIndexOfAny(new char[] {'/', '\\'});
if (index != -1) name = name.Substring(index + 1);
tk2dSpriteDefinition attachmentParameters = null;
for(int i = 0; i < sprites.inst.spriteDefinitions.Length; ++i) {
for (int i = 0; i < sprites.inst.spriteDefinitions.Length; ++i) {
tk2dSpriteDefinition def = sprites.inst.spriteDefinitions[i];
if(def.name == name) {
if (def.name == name) {
attachmentParameters = def;
break;
}
}
if(attachmentParameters == null ) throw new Exception("Sprite not found in atlas: " + name + " (" + type + ")");
if(attachmentParameters.complexGeometry) throw new NotImplementedException("Complex geometry is not supported: " + name + " (" + type + ")");
if(attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW) throw new NotImplementedException("Only 2d toolkit atlases are supported: " + name + " (" + type + ")");
Texture tex = attachmentParameters.material.mainTexture;
if (attachmentParameters == null) throw new Exception("Sprite not found in atlas: " + name + " (" + type + ")");
if (attachmentParameters.complexGeometry) throw new NotImplementedException("Complex geometry is not supported: " + name + " (" + type + ")");
if (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW) throw new NotImplementedException("Only 2D Toolkit atlases are supported: " + name + " (" + type + ")");
Vector2 minTexCoords = Vector2.one;
Vector2 maxTexCoords = Vector2.zero;
for(int i = 0; i < attachmentParameters.uvs.Length; ++i) {
for (int i = 0; i < attachmentParameters.uvs.Length; ++i) {
Vector2 uv = attachmentParameters.uvs[i];
minTexCoords = Vector2.Min(minTexCoords,uv);
maxTexCoords = Vector2.Max(maxTexCoords,uv);
minTexCoords = Vector2.Min(minTexCoords, uv);
maxTexCoords = Vector2.Max(maxTexCoords, uv);
}
int width = (int)(Mathf.Abs(maxTexCoords.x - minTexCoords.x) * tex.width);
int height = (int)(Mathf.Abs(maxTexCoords.y - minTexCoords.y) * tex.height);
Texture texture = attachmentParameters.material.mainTexture;
int width = (int)(Mathf.Abs(maxTexCoords.x - minTexCoords.x) * texture.width);
int height = (int)(Mathf.Abs(maxTexCoords.y - minTexCoords.y) * texture.height);
bool rotated = (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.Tk2d);
if(rotated) {
if (rotated) {
float temp = minTexCoords.x;
minTexCoords.x = maxTexCoords.x;
maxTexCoords.x = temp;
@ -64,7 +61,8 @@ public class tk2dSpineAttachmentLoader : AttachmentLoader {
minTexCoords.y,
rotated
);
// TODO - Set attachment.RegionOffsetX/Y. What units does attachmentParameters.untrimmedBoundsData use?!
attachment.RegionWidth = width;
attachment.RegionHeight = height;
attachment.RegionOriginalWidth = width;

View File

@ -5,15 +5,10 @@ using Spine;
// TODO: split skeleton and animation components
// TODO: add events in animation component
/*
*/
[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionForceBuild {
/*
*/
public tk2dSpineSkeletonDataAsset skeletonDataAsset;
public Skeleton skeleton;
@ -22,8 +17,6 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
public float animationSpeed = 1;
public Spine.AnimationState state;
/*
*/
private Mesh mesh;
private Vector3[] vertices;
private Color[] colors;
@ -32,18 +25,14 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
private int cachedQuadCount;
private float[] vertexPositions;
/*
*/
void Awake() {
vertexPositions = new float[8];
}
/*
*/
void Update() {
void Update () {
SkeletonData skeletonData = (skeletonDataAsset != null) ? skeletonDataAsset.GetSkeletonData() : null;
if(skeletonData == null) {
if (skeletonData == null) {
Clear();
return;
}
@ -56,8 +45,6 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
UpdateMesh();
}
/*
*/
private void Clear() {
GetComponent<MeshFilter>().mesh = null;
DestroyImmediate(mesh);
@ -69,8 +56,6 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
state = null;
}
/*
*/
private void Initialize() {
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
@ -81,18 +66,16 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData());
}
/*
*/
private void UpdateMesh() {
int quadIndex = 0;
int drawCount = skeleton.DrawOrder.Count;
Color currentColor = new Color();
for(int i = 0; i < drawCount; i++) {
for (int i = 0; i < drawCount; i++) {
Slot slot = skeleton.DrawOrder[i];
Attachment attachment = slot.Attachment;
if(attachment is RegionAttachment) {
if (attachment is RegionAttachment) {
RegionAttachment regionAttachment = attachment as RegionAttachment;
regionAttachment.ComputeVertices(slot.Bone,vertexPositions);
@ -143,8 +126,8 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
if (skeletonDataAsset.normalGenerationMode == tk2dSpriteCollection.NormalGenerationMode.NormalsAndTangents) {
Vector4[] tangents = new Vector4[mesh.normals.Length];
for (int t = 0; t < tangents.Length; ++t) {
tangents[t] = new Vector4(1, 0, 0, 1);
for (int i = 0; i < tangents.Length; i++) {
tangents[i] = new Vector4(1, 0, 0, 1);
}
mesh.tangents = tangents;
}
@ -153,18 +136,16 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
renderer.sharedMaterial = skeletonDataAsset.spritesData.inst.materials[0];
}
/*
*/
private void UpdateCache() {
int quadCount = 0;
int drawCount = skeleton.DrawOrder.Count;
for(int i = 0; i < drawCount; i++) {
for (int i = 0; i < drawCount; i++) {
Attachment attachment = skeleton.DrawOrder[i].Attachment;
if(attachment is RegionAttachment) quadCount++;
if (attachment is RegionAttachment) quadCount++;
}
if(quadCount == cachedQuadCount) return;
if (quadCount == cachedQuadCount) return;
cachedQuadCount = quadCount;
vertices = new Vector3[quadCount * 4];
@ -173,17 +154,12 @@ public class tk2dSpineSkeleton : MonoBehaviour, tk2dRuntime.ISpriteCollectionFor
triangles = new int[quadCount * 6];
}
/*
*/
private void UpdateSkeleton() {
skeleton.Update(Time.deltaTime * animationSpeed);
skeleton.UpdateWorldTransform();
}
/*
*/
private void UpdateAnimation() {
// Check if we need to stop current animation
if(state.Animation != null && animationName == null) {
state.ClearAnimation();

View File

@ -3,12 +3,7 @@ using System.IO;
using UnityEngine;
using Spine;
/*
*/
public class tk2dSpineSkeletonDataAsset : ScriptableObject {
/*
*/
public tk2dSpriteCollectionData spritesData;
public tk2dSpriteCollection.NormalGenerationMode normalGenerationMode = tk2dSpriteCollection.NormalGenerationMode.None;
@ -20,36 +15,30 @@ public class tk2dSpineSkeletonDataAsset : ScriptableObject {
public string[] toAnimation;
public float[] duration;
/*
*/
private SkeletonData skeletonData;
private AnimationStateData stateData;
/*
*/
public SkeletonData GetSkeletonData() {
if(skeletonData != null) return skeletonData;
if (skeletonData != null) return skeletonData;
MakeSkeletonAndAnimationData();
return skeletonData;
}
public AnimationStateData GetAnimationStateData () {
if(stateData != null) return stateData;
if (stateData != null) return stateData;
MakeSkeletonAndAnimationData();
return stateData;
}
/*
*/
private void MakeSkeletonAndAnimationData() {
if(spritesData == null) {
if (spritesData == null) {
Debug.LogWarning("Sprite collection not set for skeleton data asset: " + name,this);
return;
}
if(skeletonJSON == null) {
if (skeletonJSON == null) {
Debug.LogWarning("Skeleton JSON file not set for skeleton data asset: " + name,this);
return;
}
@ -65,9 +54,9 @@ public class tk2dSpineSkeletonDataAsset : ScriptableObject {
}
stateData = new AnimationStateData(skeletonData);
for(int i = 0, n = fromAnimation.Length; i < n; i++) {
if(fromAnimation[i].Length == 0 || toAnimation[i].Length == 0) continue;
stateData.SetMix(fromAnimation[i],toAnimation[i],duration[i]);
for (int i = 0, n = fromAnimation.Length; i < n; i++) {
if (fromAnimation[i].Length == 0 || toAnimation[i].Length == 0) continue;
stateData.SetMix(fromAnimation[i], toAnimation[i], duration[i]);
}
}