Change sprite order from being handled on the z axis to sort order on sprite renderer

+ some refactoring
This commit is contained in:
EliyaFishman 2022-12-07 18:41:16 +02:00
parent 5818234018
commit 307a881584
10 changed files with 48 additions and 55 deletions

View file

@ -45,7 +45,7 @@ namespace Bread2Unity
animator.runtimeAnimatorController = controller; animator.runtimeAnimatorController = controller;
} }
private static AnimationClip CreateAnimationClip(BccadPrefab bccadPrefab, Animation animation, private static AnimationClip CreateAnimationClip(BccadPrefab bccadPrefab, BccadAnimation bccadAnimation,
List<Sprite> sprites, List<Sprite> sprites,
IReadOnlyCollection<BccadSprite> spritesAssociatedWithPrefab) IReadOnlyCollection<BccadSprite> spritesAssociatedWithPrefab)
{ {
@ -66,7 +66,7 @@ namespace Bread2Unity
var xTransformCurve = new BccadCurve(); var xTransformCurve = new BccadCurve();
var yTransformCurve = new BccadCurve(); var yTransformCurve = new BccadCurve();
var zTransformCurve = new BccadCurve(); var sortOrderCurve = new BccadCurve();
var rotationCurve = new BccadCurve(); var rotationCurve = new BccadCurve();
@ -85,7 +85,7 @@ namespace Bread2Unity
var currentTime = 0f; var currentTime = 0f;
foreach (var currentStep in animation.Steps) foreach (var currentStep in bccadAnimation.Steps)
{ {
stepRotation.AddKey(currentTime, currentStep.Rotation); stepRotation.AddKey(currentTime, currentStep.Rotation);
stepScaleX.AddKey(currentTime, currentStep.StretchX); stepScaleX.AddKey(currentTime, currentStep.StretchX);
@ -111,11 +111,9 @@ namespace Bread2Unity
SpriteCreator.PixelsPerUnit + sprite.bounds.size.x * 0.5f * width; SpriteCreator.PixelsPerUnit + sprite.bounds.size.x * 0.5f * width;
var y = -(bccadSpritePart.PosY + currentStep.TranslateY - 512f) / SpriteCreator.PixelsPerUnit - var y = -(bccadSpritePart.PosY + currentStep.TranslateY - 512f) / SpriteCreator.PixelsPerUnit -
sprite.bounds.size.y * 0.5f * height; sprite.bounds.size.y * 0.5f * height;
var z = -0.00001f * partIndex;
xTransformCurve.AddKey(currentTime, x); xTransformCurve.AddKey(currentTime, x);
yTransformCurve.AddKey(currentTime, y); yTransformCurve.AddKey(currentTime, y);
zTransformCurve.AddKey(currentTime, z);
scaleXCurve.AddKey(currentTime, width); scaleXCurve.AddKey(currentTime, width);
scaleYCurve.AddKey(currentTime, height); scaleYCurve.AddKey(currentTime, height);
@ -133,6 +131,9 @@ namespace Bread2Unity
flipXCurve.AddKey(currentTime, bccadSpritePart.FlipX ? 1 : 0); flipXCurve.AddKey(currentTime, bccadSpritePart.FlipX ? 1 : 0);
flipYCurve.AddKey(currentTime, bccadSpritePart.FlipY ? 1 : 0); flipYCurve.AddKey(currentTime, bccadSpritePart.FlipY ? 1 : 0);
var sortOrder = partIndex;
sortOrderCurve.AddKey(currentTime, sortOrder);
rotationCurve.AddKey(currentTime, -bccadSpritePart.Rotation); rotationCurve.AddKey(currentTime, -bccadSpritePart.Rotation);
var color = bccadSpritePart.Multicolor * currentStep.Color; var color = bccadSpritePart.Multicolor * currentStep.Color;
@ -146,13 +147,13 @@ namespace Bread2Unity
currentTime += currentStep.Delay / 30f; currentTime += currentStep.Delay / 30f;
} }
if (animation.Steps.Select(step => step.StretchX).Distinct().Count() > 1) if (bccadAnimation.Steps.Select(step => step.StretchX).Distinct().Count() > 1)
animationClip.SetCurve("", typeof(Transform), "localScale.x", stepScaleX); animationClip.SetCurve("", typeof(Transform), "localScale.x", stepScaleX);
if (animation.Steps.Select(step => step.StretchY).Distinct().Count() > 1) if (bccadAnimation.Steps.Select(step => step.StretchY).Distinct().Count() > 1)
animationClip.SetCurve("", typeof(Transform), "localScale.y", stepScaleY); animationClip.SetCurve("", typeof(Transform), "localScale.y", stepScaleY);
if (animation.Steps.Select(step => step.Rotation).Distinct().Count() > 1) if (bccadAnimation.Steps.Select(step => step.Rotation).Distinct().Count() > 1)
animationClip.SetCurve("", typeof(Transform), "localEulerAngles.z", stepRotation); animationClip.SetCurve("", typeof(Transform), "localEulerAngles.z", stepRotation);
if (childIndex == 0) enabledCurve.CopyLastKey(currentTime); if (childIndex == 0) enabledCurve.CopyLastKey(currentTime);
@ -168,33 +169,14 @@ namespace Bread2Unity
sprite.parts.All(part => bccadPrefab.RegionToChild[part.RegionIndex] != child)); sprite.parts.All(part => bccadPrefab.RegionToChild[part.RegionIndex] != child));
if (animateActive) if (animateActive)
animationClip.SetCurve(child.name, typeof(GameObject), "m_IsActive", enabledCurve); animationClip.SetCurve(child.name, typeof(GameObject), "m_IsActive", enabledCurve);
if ((from part in partsOfGameObject select part.FlipX).Distinct().Count() > 1)
animationClip.SetCurve(child.name, typeof(SpriteRenderer), "m_FlipX", flipXCurve);
if ((from part in partsOfGameObject select part.FlipY).Distinct().Count() > 1)
animationClip.SetCurve(child.name, typeof(SpriteRenderer), "m_FlipY", flipYCurve);
if ((from part in partsOfGameObject select part.RegionIndex.Index).Distinct().Count() > 1)
AnimationUtility.SetObjectReferenceCurve(animationClip, spriteBinding, spriteFrames.ToArray());
//Check if there is any need for z animation
var setOfZIndexes = new HashSet<float>();
foreach (var sprite in spritesAssociatedWithPrefab)
for (var i = 0; i < sprite.parts.Count && setOfZIndexes.Count < 2; i++)
{
var part = sprite.parts[i];
if (bccadPrefab.RegionToChild[part.RegionIndex] != child)
continue;
setOfZIndexes.Add(i);
}
if ((from part in partsOfGameObject select part.PosX).Distinct().Count() > 1 || if ((from part in partsOfGameObject select part.PosX).Distinct().Count() > 1 ||
(from part in partsOfGameObject select part.PosY).Distinct().Count() > 1 || (from part in partsOfGameObject select part.PosY).Distinct().Count() > 1 ||
setOfZIndexes.Count > 1 || bccadAnimation.Steps.Select(step => step.TranslateX).Distinct().Count() > 1 ||
animation.Steps.Select(step => step.TranslateX).Distinct().Count() > 1 || bccadAnimation.Steps.Select(step => step.TranslateY).Distinct().Count() > 1)
animation.Steps.Select(step => step.TranslateY).Distinct().Count() > 1)
{ {
animationClip.SetCurve(child.name, typeof(Transform), "localPosition.x", xTransformCurve); animationClip.SetCurve(child.name, typeof(Transform), "localPosition.x", xTransformCurve);
animationClip.SetCurve(child.name, typeof(Transform), "localPosition.y", yTransformCurve); animationClip.SetCurve(child.name, typeof(Transform), "localPosition.y", yTransformCurve);
animationClip.SetCurve(child.name, typeof(Transform), "localPosition.z", zTransformCurve);
} }
if ((from part in partsOfGameObject select part.Rotation).Distinct().Count() > 1) if ((from part in partsOfGameObject select part.Rotation).Distinct().Count() > 1)
@ -207,8 +189,30 @@ namespace Bread2Unity
animationClip.SetCurve(child.name, typeof(Transform), "localScale.y", scaleYCurve); animationClip.SetCurve(child.name, typeof(Transform), "localScale.y", scaleYCurve);
} }
if ((from part in partsOfGameObject select part.FlipX).Distinct().Count() > 1)
animationClip.SetCurve(child.name, typeof(SpriteRenderer), "m_FlipX", flipXCurve);
if ((from part in partsOfGameObject select part.FlipY).Distinct().Count() > 1)
animationClip.SetCurve(child.name, typeof(SpriteRenderer), "m_FlipY", flipYCurve);
if ((from part in partsOfGameObject select part.RegionIndex.Index).Distinct().Count() > 1)
AnimationUtility.SetObjectReferenceCurve(animationClip, spriteBinding, spriteFrames.ToArray());
//Check if there is any need for sort order animation
var setOfSortingOrders = new HashSet<float>();
foreach (var sprite in spritesAssociatedWithPrefab)
for (var i = 0; i < sprite.parts.Count && setOfSortingOrders.Count < 2; i++)
{
var part = sprite.parts[i];
if (bccadPrefab.RegionToChild[part.RegionIndex] != child)
continue;
setOfSortingOrders.Add(i);
}
if (setOfSortingOrders.Count > 1)
{
animationClip.SetCurve(child.name, typeof(SpriteRenderer), "m_SortingOrder", sortOrderCurve);
}
// We check if any of the steps color that have the game object is not white // We check if any of the steps color that have the game object is not white
var colorChanges = animation.Steps var colorChanges = bccadAnimation.Steps
.Where(step => step.BccadSprite.parts.Any(part => partsOfGameObject.Contains(part))) .Where(step => step.BccadSprite.parts.Any(part => partsOfGameObject.Contains(part)))
.Any(step => !step.Color.Equals(Color.white)); .Any(step => !step.Color.Equals(Color.white));
if (colorChanges || partsOfGameObject.Select(part => part.Multicolor).Distinct().Count() > 1) if (colorChanges || partsOfGameObject.Select(part => part.Multicolor).Distinct().Count() > 1)
@ -220,7 +224,7 @@ namespace Bread2Unity
} }
} }
animationClip.name = animation.Name; animationClip.name = bccadAnimation.Name;
return animationClip; return animationClip;
} }

View file

@ -56,6 +56,7 @@ namespace Bread2Unity
spriteRenderer.flipX = spritePart.FlipX; spriteRenderer.flipX = spritePart.FlipX;
spriteRenderer.color = spritePart.Multicolor; spriteRenderer.color = spritePart.Multicolor;
spriteRenderer.flipY = spritePart.FlipY; spriteRenderer.flipY = spritePart.FlipY;
spriteRenderer.sortingOrder = index;
spriteRenderer.enabled = true; spriteRenderer.enabled = true;
gameObjectPart.transform.SetParent(prefab.ParentObject.transform); gameObjectPart.transform.SetParent(prefab.ParentObject.transform);
@ -70,7 +71,7 @@ namespace Bread2Unity
SpriteCreator.PixelsPerUnit + sprite.bounds.size.x * 0.5f * width, SpriteCreator.PixelsPerUnit + sprite.bounds.size.x * 0.5f * width,
-(spritePart.PosY - 512f) / SpriteCreator.PixelsPerUnit - -(spritePart.PosY - 512f) / SpriteCreator.PixelsPerUnit -
sprite.bounds.size.y * 0.5f * height, sprite.bounds.size.y * 0.5f * height,
-0.00001f * index); 0);
var rotation = Quaternion.AngleAxis(spritePart.Rotation, new Vector3(0, 0, -1)); var rotation = Quaternion.AngleAxis(spritePart.Rotation, new Vector3(0, 0, -1));
gameObjectPart.transform.localPosition = position; gameObjectPart.transform.localPosition = position;
gameObjectPart.transform.localRotation = rotation; gameObjectPart.transform.localRotation = rotation;

View file

@ -77,7 +77,7 @@ namespace Bread2Unity
var animationsNum = byteBuffer.ReadInt(); var animationsNum = byteBuffer.ReadInt();
for (var i = 0; i < animationsNum; i++) for (var i = 0; i < animationsNum; i++)
{ {
var anim = new Animation(); var anim = new BccadAnimation();
var nameBuilder = new StringBuilder(); var nameBuilder = new StringBuilder();
var length = Convert.ToInt32(byteBuffer.ReadByte()); var length = Convert.ToInt32(byteBuffer.ReadByte());
for (var j = 0; j < length; j++) nameBuilder.Append(Convert.ToChar(byteBuffer.ReadByte())); for (var j = 0; j < length; j++) nameBuilder.Append(Convert.ToChar(byteBuffer.ReadByte()));

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: ea35991e78a2918439cc249d1e4e293e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -3,7 +3,7 @@ using UnityEngine;
namespace Bread2Unity namespace Bread2Unity
{ {
public class Animation public class BccadAnimation
{ {
public int InterpolationInt = 0; public int InterpolationInt = 0;
public string Name; public string Name;
@ -15,17 +15,16 @@ namespace Bread2Unity
public class AnimationStep public class AnimationStep
{ {
public BccadSprite BccadSprite; public BccadSprite BccadSprite;
public Color Color; //needs to add public Color Color;
public ushort Delay; public ushort Delay;
public float Depth; public float Depth;
public byte Opacity; //needs to add public float Rotation;
public float Rotation; //needs to add public float StretchX;
public float StretchY;
public float StretchX; //needs to add
public float StretchY; //needs to add
public short TranslateX; public short TranslateX;
public short TranslateY; public short TranslateY;

View file

@ -4,7 +4,7 @@ namespace Bread2Unity
{ {
public class DataModel public class DataModel
{ {
public List<Animation> Animations = new List<Animation>(); public List<BccadAnimation> Animations = new List<BccadAnimation>();
public List<Region> Regions = new List<Region>(); public List<Region> Regions = new List<Region>();
public int SheetH; public int SheetH;
public int SheetW; public int SheetW;

View file

@ -4,7 +4,7 @@ namespace Bread2Unity
{ {
public class PrefabData public class PrefabData
{ {
public List<Animation> Animations; public List<BccadAnimation> Animations;
public string Name; public string Name;
public int SpriteIndex; public int SpriteIndex;

View file

@ -21,7 +21,7 @@ Rhythm Heaven animation to Unity animation converter
- [X] add default line for bcaad data - [X] add default line for bcaad data
- [X] change window height and width - [X] change window height and width
- [x] fix imports - [x] fix imports
- [ ] change sprite order with sprite renderer. - [X] change sprite order with sprite renderer.
- [ ] write a normal readme - [ ] write a normal readme