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) // TODO: handle TPackerCW flip mode (probably not swap uv horizontaly)
/*
*/
public class tk2dSpineAttachmentLoader : AttachmentLoader { public class tk2dSpineAttachmentLoader : AttachmentLoader {
/*
*/
private tk2dSpriteCollectionData sprites; private tk2dSpriteCollectionData sprites;
/* public tk2dSpineAttachmentLoader(tk2dSpriteCollectionData sprites) {
*/ if (sprites == null) throw new ArgumentNullException("sprites cannot be null.");
public tk2dSpineAttachmentLoader(tk2dSpriteCollectionData s) { this.sprites = sprites;
if(s == null) throw new ArgumentNullException("sprites cannot be null.");
sprites = s;
} }
public Attachment NewAttachment(Skin skin,AttachmentType type,String name) { public Attachment NewAttachment(Skin skin, AttachmentType type, String name) {
if(type != AttachmentType.region) throw new Exception("Unknown attachment type: " + type); 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; 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]; tk2dSpriteDefinition def = sprites.inst.spriteDefinitions[i];
if(def.name == name) { if (def.name == name) {
attachmentParameters = def; attachmentParameters = def;
break; break;
} }
} }
if(attachmentParameters == null ) throw new Exception("Sprite not found in atlas: " + name + " (" + type + ")"); 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.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 + ")"); if (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW) throw new NotImplementedException("Only 2D Toolkit atlases are supported: " + name + " (" + type + ")");
Texture tex = attachmentParameters.material.mainTexture;
Vector2 minTexCoords = Vector2.one; Vector2 minTexCoords = Vector2.one;
Vector2 maxTexCoords = Vector2.zero; 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]; Vector2 uv = attachmentParameters.uvs[i];
minTexCoords = Vector2.Min(minTexCoords,uv); minTexCoords = Vector2.Min(minTexCoords, uv);
maxTexCoords = Vector2.Max(maxTexCoords,uv); maxTexCoords = Vector2.Max(maxTexCoords, uv);
} }
int width = (int)(Mathf.Abs(maxTexCoords.x - minTexCoords.x) * tex.width); Texture texture = attachmentParameters.material.mainTexture;
int height = (int)(Mathf.Abs(maxTexCoords.y - minTexCoords.y) * tex.height); 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); bool rotated = (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.Tk2d);
if(rotated) { if (rotated) {
float temp = minTexCoords.x; float temp = minTexCoords.x;
minTexCoords.x = maxTexCoords.x; minTexCoords.x = maxTexCoords.x;
maxTexCoords.x = temp; maxTexCoords.x = temp;
@ -65,6 +62,7 @@ public class tk2dSpineAttachmentLoader : AttachmentLoader {
rotated rotated
); );
// TODO - Set attachment.RegionOffsetX/Y. What units does attachmentParameters.untrimmedBoundsData use?!
attachment.RegionWidth = width; attachment.RegionWidth = width;
attachment.RegionHeight = height; attachment.RegionHeight = height;
attachment.RegionOriginalWidth = width; attachment.RegionOriginalWidth = width;

View File

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

View File

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