浏览代码
Merge pull request #150 from Unity-Technologies/animation_curve_sampling
Merge pull request #150 from Unity-Technologies/animation_curve_sampling
Added animation curve sampling + sampler deserialization bugfix + native sampling removal/main
GitHub
4 年前
当前提交
d98bf0d6
共有 30 个文件被更改,包括 350 次插入 和 608 次删除
-
11com.unity.perception/CHANGELOG.md
-
8com.unity.perception/Documentation~/Randomization/Index.md
-
34com.unity.perception/Documentation~/Randomization/Parameters.md
-
30com.unity.perception/Editor/Randomization/VisualElements/Sampler/SamplerElement.cs
-
2com.unity.perception/Runtime/GroundTruth/Labelers/Visualization/Materials/OutlineMaterial.mat
-
11com.unity.perception/Runtime/Randomization/Parameters/NumericParameter.cs
-
33com.unity.perception/Runtime/Randomization/Parameters/ParameterTypes/NumericParameters/BooleanParameter.cs
-
111com.unity.perception/Runtime/Randomization/Parameters/ParameterTypes/NumericParameters/ColorParameters/ColorHsvaParameter.cs
-
52com.unity.perception/Runtime/Randomization/Parameters/ParameterTypes/NumericParameters/ColorParameters/ColorRgbParameter.cs
-
11com.unity.perception/Runtime/Randomization/Parameters/ParameterTypes/NumericParameters/FloatParameter.cs
-
31com.unity.perception/Runtime/Randomization/Parameters/ParameterTypes/NumericParameters/IntegerParameter.cs
-
35com.unity.perception/Runtime/Randomization/Parameters/ParameterTypes/NumericParameters/Vector2Parameter.cs
-
38com.unity.perception/Runtime/Randomization/Parameters/ParameterTypes/NumericParameters/Vector3Parameter.cs
-
51com.unity.perception/Runtime/Randomization/Parameters/ParameterTypes/NumericParameters/Vector4Parameter.cs
-
12com.unity.perception/Runtime/Randomization/Samplers/ISampler.cs
-
39com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/ConstantSampler.cs
-
46com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/NormalSampler.cs
-
39com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/UniformSampler.cs
-
66com.unity.perception/Runtime/Randomization/Samplers/SamplerUtility.cs
-
15com.unity.perception/Runtime/Randomization/Scenarios/Scenario.cs
-
19com.unity.perception/Tests/Runtime/Randomization/ParameterTests/StructParameterTests.cs
-
21com.unity.perception/Tests/Runtime/Randomization/SamplerTests/NormalSamplerTests.cs
-
54com.unity.perception/Tests/Runtime/Randomization/SamplerTests/SamplerTestsBase.cs
-
21com.unity.perception/Tests/Runtime/Randomization/SamplerTests/UniformSamplerTests.cs
-
99com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/AnimationCurveSampler.cs
-
11com.unity.perception/Runtime/Randomization/Samplers/SamplerTypes/AnimationCurveSampler.cs.meta
-
10com.unity.perception/Runtime/Randomization/Samplers/SamplerValidationException.cs
-
3com.unity.perception/Runtime/Randomization/Samplers/SamplerValidationException.cs.meta
-
34com.unity.perception/Tests/Runtime/Randomization/SamplerTests/AnimationCurveSamplerTests.cs
-
11com.unity.perception/Tests/Runtime/Randomization/SamplerTests/AnimationCurveSamplerTests.cs.meta
|
|||
using System; |
|||
using System.Runtime.CompilerServices; |
|||
using Unity.Burst; |
|||
using Unity.Collections; |
|||
using Unity.Collections.LowLevel.Unsafe; |
|||
using Unity.Jobs; |
|||
using UnityEngine.Experimental.Perception.Randomization.Scenarios; |
|||
|
|||
namespace UnityEngine.Experimental.Perception.Randomization.Samplers |
|||
{ |
|||
/// <summary>
|
|||
/// Returns random values according to a range and probability distribution denoted by a user provided AnimationCurve.
|
|||
/// The X axis of the AnimationCurve corresponds to the values this sampler will pick from,
|
|||
/// and the Y axis corresponds to the relative probability of the values.
|
|||
/// The relative probabilities (Y axis) do not need to max out at 1, as only the shape of the curve matters.
|
|||
/// The Y values cannot however be negative.
|
|||
/// </summary>
|
|||
[Serializable] |
|||
public class AnimationCurveSampler : ISampler |
|||
{ |
|||
/// <summary>
|
|||
/// The Animation Curve associated with this sampler
|
|||
/// </summary>
|
|||
[Tooltip("Probability distribution curve used for this sampler. The X axis corresponds to the values this sampler will pick from, and the Y axis corresponds to the relative probability of the values. The relative probabilities (Y axis) do not need to max out at 1 as only the shape of the curve matters. The Y values cannot however be negative.")] |
|||
public AnimationCurve distributionCurve; |
|||
|
|||
/// <summary>
|
|||
/// Number of samples used for integrating over the provided AnimationCurve.
|
|||
/// The larger the number of samples, the more accurate the resulting probability distribution will be.
|
|||
/// </summary>
|
|||
[Tooltip("Number of internal samples used for integrating over the provided AnimationCurve. The larger the number of samples, the more accurately the resulting probability distribution will follow the provided AnimationCurve. Increase this if the default value proves insufficient.")] |
|||
public int numOfSamplesForIntegration = 500; |
|||
|
|||
float[] m_IntegratedCurve; |
|||
bool m_Initialized; |
|||
float m_StartTime; |
|||
float m_EndTime; |
|||
float m_Interval; |
|||
|
|||
/// <summary>
|
|||
/// Constructs a default AnimationCurveSampler
|
|||
/// </summary>
|
|||
public AnimationCurveSampler() |
|||
{ |
|||
distributionCurve = new AnimationCurve( |
|||
new Keyframe(0, 0), new Keyframe(0.5f, 1), new Keyframe(1, 0)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Constructs an AnimationCurveSampler with a given animation curve
|
|||
/// </summary>
|
|||
/// <param name="curve">The animation curve to sample from</param>
|
|||
/// <param name="numberOfSamples">Number of samples used for integrating over the provided AnimationCurve</param>
|
|||
public AnimationCurveSampler(AnimationCurve curve, int numberOfSamples=500) |
|||
{ |
|||
distributionCurve = curve; |
|||
numOfSamplesForIntegration = numberOfSamples; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Generates one sample
|
|||
/// </summary>
|
|||
/// <returns>The generated sample</returns>
|
|||
public float Sample() |
|||
{ |
|||
Initialize(); |
|||
var rng = new Unity.Mathematics.Random(ScenarioBase.activeScenario.NextRandomState()); |
|||
return SamplerUtility.AnimationCurveSample( |
|||
m_IntegratedCurve, rng.NextFloat(), m_Interval, m_StartTime, m_EndTime); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Validates that the sampler is configured properly
|
|||
/// </summary>
|
|||
/// <exception cref="SamplerValidationException"></exception>
|
|||
public void Validate() |
|||
{ |
|||
if (distributionCurve.length == 0) |
|||
throw new SamplerValidationException("The distribution curve provided is empty"); |
|||
|
|||
if (numOfSamplesForIntegration < 2) |
|||
throw new SamplerValidationException("Insufficient number of integration samples"); |
|||
} |
|||
|
|||
void Initialize() |
|||
{ |
|||
if (m_Initialized) |
|||
return; |
|||
|
|||
Validate(); |
|||
m_IntegratedCurve = new float[numOfSamplesForIntegration]; |
|||
SamplerUtility.IntegrateCurve(m_IntegratedCurve, distributionCurve); |
|||
m_StartTime = distributionCurve.keys[0].time; |
|||
m_EndTime = distributionCurve.keys[distributionCurve.length - 1].time; |
|||
m_Interval = (m_EndTime - m_StartTime) / (numOfSamplesForIntegration - 1); |
|||
m_Initialized = true; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: d4147226e4d134ac8b0c69c14db5d23b |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
|
|||
namespace UnityEngine.Experimental.Perception.Randomization.Samplers |
|||
{ |
|||
class SamplerValidationException : Exception |
|||
{ |
|||
public SamplerValidationException(string msg) : base(msg) {} |
|||
public SamplerValidationException(string msg, Exception innerException) : base(msg, innerException) {} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 232e97ede3ab4d769096833ee3e8d0e4 |
|||
timeCreated: 1610570590 |
|
|||
using NUnit.Framework; |
|||
using UnityEngine.Experimental.Perception.Randomization.Samplers; |
|||
|
|||
namespace RandomizationTests.SamplerTests |
|||
{ |
|||
[TestFixture] |
|||
public class AnimationCurveSamplerTestsBase : SamplerTestsBase<AnimationCurveSampler> |
|||
{ |
|||
public AnimationCurveSamplerTestsBase() |
|||
{ |
|||
m_BaseSampler = new AnimationCurveSampler(); |
|||
} |
|||
|
|||
[Test] |
|||
public void SamplesInRange() |
|||
{ |
|||
var min = m_Sampler.distributionCurve.keys[0].time; |
|||
var max = m_Sampler.distributionCurve.keys[m_Sampler.distributionCurve.length - 1].time; |
|||
|
|||
var samples = new float[k_TestSampleCount]; |
|||
for (var i = 0; i < samples.Length; i++) |
|||
{ |
|||
samples[i] = m_Sampler.Sample(); |
|||
} |
|||
Assert.AreEqual(samples.Length, k_TestSampleCount); |
|||
|
|||
for (var i = 0; i < samples.Length; i++) |
|||
{ |
|||
Assert.GreaterOrEqual(samples[i], min); |
|||
Assert.LessOrEqual(samples[i], max); |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 15fca3a26ddcf4459be02aea956806bc |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue