Skip to content

Commit

Permalink
Remove 2D transform workflow
Browse files Browse the repository at this point in the history
For now this package only use unity full 3D transforms. This was done because many devs are confused when trying to use this package while theirs project written with use of regular unity transforms.
  • Loading branch information
Antoshidza committed Nov 21, 2023
1 parent abbdd07 commit 36e0ef1
Show file tree
Hide file tree
Showing 33 changed files with 25 additions and 1,352 deletions.
8 changes: 1 addition & 7 deletions Base/Authoring/Modules/BakerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void BakeSpriteBase<TAuthoring>(this Baker<TAuthoring> baker, in S
/// Bakes all passed data to make entity be able to rendered though shader from this package.
/// If you use another shader and you need some another data, please implement your own baking method.
/// </summary>
public static void BakeSpriteRender<TAuthoring>(this Baker<TAuthoring> baker, in Entity entity, TAuthoring authoring, in float4 uvAtlas, in float4 uvTilingAndOffset, in float2 pivot, in float2 scale, bool flipX = false, bool flipY = false, bool add2DTransform = true)
public static void BakeSpriteRender<TAuthoring>(this Baker<TAuthoring> baker, in Entity entity, TAuthoring authoring, in float4 uvAtlas, in float4 uvTilingAndOffset, in float2 pivot, in float2 scale, bool flipX = false, bool flipY = false)
where TAuthoring : Component
{
if (baker == null)
Expand All @@ -46,12 +46,6 @@ public static void BakeSpriteRender<TAuthoring>(this Baker<TAuthoring> baker, in
baker.AddComponent(entity, new Pivot { value = pivot });
baker.AddComponent(entity, new Scale2D { value = scale });
baker.AddComponent(entity, new Flip { Value = new(flipX ? -1 : 0, flipY ? -1 : 0) });

if (add2DTransform)
{
baker.AddComponentObject(entity, new Transform2DRequest { Source = authoring.gameObject });
baker.DependsOn(authoring.transform);
}
}
}
}
2 changes: 1 addition & 1 deletion Base/Authoring/Modules/SpriteSettingsAuthoringModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Bake<TAuthoring>(Baker<TAuthoring> baker, TAuthoring authoring, in f

baker.BakeSpriteRender
(
baker.GetEntity(TransformUsageFlags.None),
baker.GetEntity(TransformUsageFlags.Dynamic),
authoring,
uvAtlas,
GetTilingAndOffsetByDrawMode(),
Expand Down
7 changes: 4 additions & 3 deletions Base/Common/Bounds2D.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Unity.Mathematics;
using Unity.Transforms;

namespace NSprites
{
Expand Down Expand Up @@ -66,10 +67,10 @@ public bool Intersects(in Bounds2D bounds)
min.y <= anotherMax.y && max.y >= anotherMin.y;
}

public static Bounds2D From(in LocalToWorld2D ltw, in Scale2D size, in Pivot pivot)
public static Bounds2D From(in LocalToWorld ltw, in Scale2D size, in Pivot pivot)
{
var rotation = MathHelper.euler(ltw.Rotation).z;
var scale = ltw.Scale * size.value;
var scale = size.value;
var localCenter= -scale * pivot.value + scale * .5f;

var sin = math.sin(rotation);
Expand All @@ -85,7 +86,7 @@ static float2 RotateFloat2(in float sin, in float cos, in float2 v)
=> new(v.x * cos - v.y * sin, v.x * sin + v.y * cos);

var adjustedScale = RotateScale2D(sin, cos, scale);
var position = ltw.Position + RotateFloat2(sin, cos, localCenter);
var position = ltw.Position.xy + RotateFloat2(sin, cos, localCenter);

return new Bounds2D(position, adjustedScale);
}
Expand Down
3 changes: 2 additions & 1 deletion Base/PropertiesManifest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using NSprites;
using Unity.Transforms;

[assembly: InstancedPropertyComponent(typeof(UVTilingAndOffset), "_uvTilingAndOffsetBuffer")]
[assembly: InstancedPropertyComponent(typeof(UVAtlas), "_uvAtlasBuffer")]
[assembly: InstancedPropertyComponent(typeof(LocalToWorld2D), "_positionBuffer")]
[assembly: InstancedPropertyComponent(typeof(LocalToWorld), "_positionBuffer")]
[assembly: InstancedPropertyComponent(typeof(Scale2D), "_heightWidthBuffer")]
[assembly: InstancedPropertyComponent(typeof(Pivot), "_pivotBuffer")]
[assembly: InstancedPropertyComponent(typeof(SortingValue), "_sortingValueBuffer")]
Expand Down
5 changes: 3 additions & 2 deletions Base/Systems/FullScreenSpriteSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

namespace NSprites
{
Expand All @@ -14,11 +15,11 @@ private partial struct RecalculateSpritesJob : IJobEntity
public float2 CameraPosition;
public float2 ScreenSize;

private void Execute(ref Scale2D scale, ref LocalTransform2D transform, ref UVTilingAndOffset uvTilingAndOffset, in NativeSpriteSize nativeSpriteSize)
private void Execute(ref Scale2D scale, ref LocalTransform transform, ref UVTilingAndOffset uvTilingAndOffset, in NativeSpriteSize nativeSpriteSize)
{
scale.value = ScreenSize;
transform.Scale = 1;
transform.Position = CameraPosition;
transform.Position = new float3(CameraPosition.x, CameraPosition.y, 0f);
uvTilingAndOffset.value = new float4(ScreenSize / nativeSpriteSize.Value, CameraPosition / nativeSpriteSize.Value - ScreenSize / nativeSpriteSize.Value / 2f);
}
}
Expand Down
6 changes: 3 additions & 3 deletions Base/Systems/SpriteFrustumCullingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEditor;

namespace NSprites
Expand All @@ -17,7 +18,7 @@ private partial struct DisableCulledJob : IJobEntity
public EntityCommandBuffer.ParallelWriter EntityCommandBuffer;
public Bounds2D CameraBounds2D;

private void Execute(Entity entity, [ChunkIndexInQuery]int chunkIndex, in LocalToWorld2D worldPosition, in Scale2D size, in Pivot pivot)
private void Execute(Entity entity, [ChunkIndexInQuery]int chunkIndex, in LocalToWorld worldPosition, in Scale2D size, in Pivot pivot)
{
var bounds = Bounds2D.From(worldPosition, size, pivot);
if(!CameraBounds2D.Intersects(bounds))
Expand All @@ -32,7 +33,7 @@ private partial struct EnableUnculledJob : IJobEntity
public EntityCommandBuffer.ParallelWriter EntityCommandBuffer;
public Bounds2D CameraBounds2D;

private void Execute(Entity entity, [ChunkIndexInQuery] int chunkIndex, in LocalToWorld2D worldPosition, in Scale2D size, in Pivot pivot)
private void Execute(Entity entity, [ChunkIndexInQuery] int chunkIndex, in LocalToWorld worldPosition, in Scale2D size, in Pivot pivot)
{
var bounds = Bounds2D.From(worldPosition, size, pivot);
if (CameraBounds2D.Intersects(bounds))
Expand Down Expand Up @@ -83,7 +84,6 @@ public void OnUpdate(ref SystemState state)
};
var disableCulledHandle = disableCulledJob.ScheduleParallelByRef(state.Dependency);


var enableUnculledJob = new EnableUnculledJob
{
EntityCommandBuffer = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter(),
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Also before working with this package please read [NSprites documentation](https
| [Sorting](About/Sorting.md) | runtime | Calculate SortingValue depending on 2D position to use in shader |
| Culling | runtime | Disables / Enables rendering based on 2D position relatively to camera |
| [Animation](About/Animation.md) | runtime | Shifts UVs values to simulate sprite animation |
| [2D Transform](About/2DTransform.md) | both | Provides systems / components to simulate 2D transforms |
| Graphics | runtime | Provides shaders / materials / other render assets |

Diagram below illustrates dependencies between parts
Expand Down
19 changes: 10 additions & 9 deletions Sorting/Systems/SpriteSortingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;

[assembly: RegisterGenericJobType(typeof(SpriteSortingSystem.SortArrayJob<int, SpriteSortingSystem.SortingDataComparer>))]

Expand Down Expand Up @@ -59,7 +60,7 @@ internal struct SortingDataComparer : IComparer<int>
private struct GatherSortingDataJob : IJobChunk
{
[ReadOnly] public EntityTypeHandle entityTypeHandle;
[ReadOnly] public ComponentTypeHandle<LocalToWorld2D> worldPosition2D_CTH;
[ReadOnly] public ComponentTypeHandle<LocalToWorld> worldPosition2D_CTH;
[ReadOnly] public ComponentTypeHandle<SortingIndex> sortingIndex_CTH;
[WriteOnly][NativeDisableContainerSafetyRestriction] public NativeArray<SortingData> sortingDataArray;
[WriteOnly][NativeDisableContainerSafetyRestriction] public NativeArray<int> pointers;
Expand All @@ -68,15 +69,15 @@ private struct GatherSortingDataJob : IJobChunk
public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
{
var entityArray = chunk.GetNativeArray(entityTypeHandle);
var worldPosition2DArray = chunk.GetNativeArray(ref worldPosition2D_CTH);
var worldPositionArray = chunk.GetNativeArray(ref worldPosition2D_CTH);
var sortingIndexes = chunk.GetNativeArray(ref sortingIndex_CTH);
var firstEntityIndex = chunkBasedEntityIndices[unfilteredChunkIndex];
for (int entityIndex = 0; entityIndex < entityArray.Length; entityIndex++)
{
var arrayIndex = firstEntityIndex + entityIndex;
sortingDataArray[arrayIndex] = new SortingData
{
position = worldPosition2DArray[entityIndex].Position,
position = worldPositionArray[entityIndex].Position.xy,
sortingIndex = sortingIndexes[entityIndex].value,
id = entityArray[entityIndex].Index
};
Expand Down Expand Up @@ -145,7 +146,7 @@ public void Execute() {}
struct SystemAPIData
{
public EntityTypeHandle entityTypeHandle;
public ComponentTypeHandle<LocalToWorld2D> componentTypeHandleL2W;
public ComponentTypeHandle<LocalToWorld> componentTypeHandleLTW;
public ComponentTypeHandle<SortingIndex> componentTypeHandleSI;
public ComponentTypeHandle<SortingValue> componentTypeHandleSV;
}
Expand All @@ -167,7 +168,7 @@ private JobHandle RegularSort(in EntityQuery sortingQuery, int sortingLayer, ref
var gatherSortingDataJob = new GatherSortingDataJob
{
entityTypeHandle = systemAPIData.entityTypeHandle,
worldPosition2D_CTH = systemAPIData.componentTypeHandleL2W,
worldPosition2D_CTH = systemAPIData.componentTypeHandleLTW,
sortingIndex_CTH = systemAPIData.componentTypeHandleSI,
pointers = dataPointers,
sortingDataArray = sortingDataArray,
Expand Down Expand Up @@ -221,7 +222,7 @@ public void OnCreate(ref SystemState state)
var systemData = new SystemData();
var queryBuilder = new EntityQueryBuilder(Allocator.Temp)
.WithNone<CullSpriteTag>()
.WithAll<LocalToWorld2D>()
.WithAll<LocalToWorld>()
.WithAll<SortingValue>()
.WithAll<SortingIndex>()
.WithAll<SortingLayer>()
Expand All @@ -232,7 +233,7 @@ public void OnCreate(ref SystemState state)
queryBuilder.Reset();
_ = queryBuilder
.WithNone<CullSpriteTag>()
.WithAll<LocalToWorld2D>()
.WithAll<LocalToWorld>()
.WithAll<SortingValue>()
.WithAll<SortingIndex>()
.WithAll<SortingLayer>()
Expand Down Expand Up @@ -270,7 +271,7 @@ public void OnUpdate(ref SystemState state)
systemData.sortingSpritesQuery.SetSharedComponentFilter(sortingLayer);
var systemAPIData = new SystemAPIData {
entityTypeHandle = SystemAPI.GetEntityTypeHandle(),
componentTypeHandleL2W = SystemAPI.GetComponentTypeHandle<LocalToWorld2D>(true),
componentTypeHandleLTW = SystemAPI.GetComponentTypeHandle<LocalToWorld>(true),
componentTypeHandleSI = SystemAPI.GetComponentTypeHandle<SortingIndex>(true),
componentTypeHandleSV = SystemAPI.GetComponentTypeHandle<SortingValue>(false)
};
Expand All @@ -286,7 +287,7 @@ public void OnUpdate(ref SystemState state)
systemData.sortingStaticSpritesQuery.SetSharedComponentFilter(sortingLayer);
var systemAPIData = new SystemAPIData {
entityTypeHandle = SystemAPI.GetEntityTypeHandle(),
componentTypeHandleL2W = SystemAPI.GetComponentTypeHandle<LocalToWorld2D>(true),
componentTypeHandleLTW = SystemAPI.GetComponentTypeHandle<LocalToWorld>(true),
componentTypeHandleSI = SystemAPI.GetComponentTypeHandle<SortingIndex>(true),
componentTypeHandleSV = SystemAPI.GetComponentTypeHandle<SortingValue>(false)
};
Expand Down
3 changes: 0 additions & 3 deletions Transform2D.meta

This file was deleted.

3 changes: 0 additions & 3 deletions Transform2D/Authoring.meta

This file was deleted.

9 changes: 0 additions & 9 deletions Transform2D/Authoring/ExcludeFrom2DConversion.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Transform2D/Authoring/ExcludeFrom2DConversion.cs.meta

This file was deleted.

17 changes: 0 additions & 17 deletions Transform2D/Authoring/Transform2DAuthoring.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Transform2D/Authoring/Transform2DAuthoring.cs.meta

This file was deleted.

97 changes: 0 additions & 97 deletions Transform2D/Authoring/Transform2DConversionSystem.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Transform2D/Authoring/Transform2DConversionSystem.cs.meta

This file was deleted.

11 changes: 0 additions & 11 deletions Transform2D/Authoring/Transform2DRequest.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Transform2D/Authoring/Transform2DRequest.cs.meta

This file was deleted.

3 changes: 0 additions & 3 deletions Transform2D/Data.meta

This file was deleted.

Loading

0 comments on commit 36e0ef1

Please sign in to comment.