您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
72 行
2.4 KiB
72 行
2.4 KiB
using Unity.Collections;
|
|
using Unity.Entities;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
|
|
namespace Unity.MegaCity.Gameplay
|
|
{
|
|
/// <summary>
|
|
/// Create blob asset out of the animation curves used in the player,
|
|
/// the effect is applied later to improve the player rotations.
|
|
/// </summary>
|
|
public struct VehicleVolatilityCurves : IComponentData
|
|
{
|
|
public BlobAssetReference<AnimationCurveBlob> BankVolatilityCurve;
|
|
public BlobAssetReference<AnimationCurveBlob> YawVolatilityCurve;
|
|
public BlobAssetReference<AnimationCurveBlob> PitchVolatilityCurve;
|
|
}
|
|
|
|
public struct AnimationCurveBlob
|
|
{
|
|
private BlobArray<float> Keys;
|
|
private float Length;
|
|
private float KeyCount;
|
|
|
|
// When t exceeds the curve time, repeat it
|
|
public float CalculateNormalizedTime(float t)
|
|
{
|
|
float normalizedT = t * Length;
|
|
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<AnimationCurveBlob> CreateBlob(AnimationCurve curve, Allocator allocator,
|
|
Allocator allocatorForTemp = Allocator.TempJob)
|
|
{
|
|
using (var blob = new BlobBuilder(allocatorForTemp))
|
|
{
|
|
ref var anim = ref blob.ConstructRoot<AnimationCurveBlob>();
|
|
int keyCount = 20;
|
|
|
|
var endTime = curve[curve.length - 1].time;
|
|
anim.Length = 1.0F / endTime;
|
|
anim.KeyCount = keyCount;
|
|
|
|
var array = blob.Allocate(ref anim.Keys, keyCount + 1);
|
|
for (int i = 0; i < keyCount; i++)
|
|
{
|
|
var t = (float) i / (keyCount - 1) * endTime;
|
|
array[i] = curve.Evaluate(t);
|
|
}
|
|
|
|
array[keyCount] = array[keyCount - 1];
|
|
|
|
return blob.CreateBlobAssetReference<AnimationCurveBlob>(allocator);
|
|
}
|
|
}
|
|
}
|
|
}
|