Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# BlobAsset Conversion Sample

This sample demonstrates how to convert a information from a GameObject containing a Unity Authoring component to a BlobAsset using the `BlobAssetStore` and `BlobAssetComputationContext` types.
This sample demonstrates how to convert assets referenced from authoring components to a BlobAsset in an efficient and scalable way.
* How to avoid generating blobs that were already generated
* How to share blob generation between multiple components that reference the same or similar source assets
* How to efficiently extract all inputs for blob generation from authoring data and then perform all blob generation in bursted and parallel jobs

It uses `BlobAssetStore` and `BlobAssetComputationContext` to reduce complexity.

## What does it show

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions ECSSamples/Assets/Advanced/BlobAssetSimple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Simple BlobAsset Sample (Not scalable)

This sample demonstrates how to convert assets or authoring component data to an BlobAsset in the simplest possible way.

NOTE: This approach is not scalable in terms of conversion performance. Depending on your scalability needs this may or may not be a good fit.
A more complex but scalable conversion approach is shown in Advanced/BlobAssetScalable

## What does it show

Converts an AnimationCurve to a blob asset. The samples uses it to animate the y position of the transform.

It shows how to create a blob asset during converrsion.

It uses `BlobAssetStore` to manage lifetime of the generated blob assets.
It uses `BlobAssetStore.AddUniqueBlobAsset` to ensue that unique blob assets are automatically shared.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions ECSSamples/Assets/Advanced/BlobAssetSimple/SimpleAnimationBlob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Unity.Entities;
using Unity.Collections;
using Unity.Mathematics;
using UnityEngine;

/// <summary>
/// Very simple animation curve blob data that uses linear interpolation at fixed intervals.
/// Blob data is constructed from a UnityEngine.AnimationCurve
/// </summary>
public struct SimpleAnimationBlob
{
BlobArray<float> Keys;
float InvLength;
float KeyCount;

// When t exceeds the curve time, repeat it
public float CalculateNormalizedTime(float t)
{
float normalizedT = t * InvLength;
return normalizedT - math.floor(normalizedT);
}

public float Evaluate(float t)
{
// Loops time value between 0...1
t = CalculateNormalizedTime(t);

// Find index and interpolation value in the array
float sampleT = t * KeyCount;
var sampleTFloor = math.floor(sampleT);

float interp = sampleT - sampleTFloor;
var index = (int)sampleTFloor;

return math.lerp(Keys[index], Keys[index+1], interp);
}

public static BlobAssetReference<SimpleAnimationBlob> CreateBlob(AnimationCurve curve, Allocator allocator)
{
using (var blob = new BlobBuilder(Allocator.TempJob))
{
ref var anim = ref blob.ConstructRoot<SimpleAnimationBlob>();
int keyCount = 12;

float endTime = curve[curve.length - 1].time;
anim.InvLength = 1.0F / endTime;
anim.KeyCount = keyCount;

var array = blob.Allocate(ref anim.Keys, keyCount + 1);
for (int i = 0; i < keyCount; i++)
{
float t = (float) i / (float)(keyCount - 1) * endTime;
array[i] = curve.Evaluate(t);
}
array[keyCount] = array[keyCount-1];

return blob.CreateBlobAssetReference<SimpleAnimationBlob>(allocator);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Unity.Collections;
using Unity.Entities;
using Unity.Transforms;
using UnityEngine;

public class SimpleBlobAnimationAuthoring : MonoBehaviour, IConvertGameObjectToEntity
{
public AnimationCurve Curve = AnimationCurve.EaseInOut(0, 0, 1, 1);

public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
var blob = SimpleAnimationBlob.CreateBlob(Curve, Allocator.Persistent);

// Add the generated blob asset to the blob asset store.
// if another component generates the exact same blob asset, it will automatically be shared.
// Ownership of the blob asset is passed to the BlobAssetStore,
// it will automatically manage the lifetime of the blob asset.
conversionSystem.BlobAssetStore.AddUniqueBlobAsset(ref blob);

dstManager.AddComponentData(entity, new SimpleBlobAnimation { Anim = blob });
}
}
public struct SimpleBlobAnimation : IComponentData
{
public BlobAssetReference<SimpleAnimationBlob> Anim;
public float T;
}

partial class SimpleBlobAnimationSystem : SystemBase
{
protected override void OnUpdate()
{
var dt = Time.DeltaTime;
Entities.ForEach((ref SimpleBlobAnimation anim, ref Translation translation) =>
{
anim.T += dt;
translation.Value.y = anim.Anim.Value.Evaluate(anim.T);
}).Run();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading