浏览代码

Update Phase2.md

/main
GitHub 4 年前
当前提交
712970d4
共有 1 个文件被更改,包括 48 次插入15 次删除
  1. 63
      Tutorial/Phase2.md

63
Tutorial/Phase2.md


[Serializable]
public class LightRandomizer : Randomizer
{
public FloatParameter lightIntensityParameter;
protected override void OnIterationStart()
{
var taggedObjects = tagManager.Query<LightRandomizerTag>();
public FloatParameter lightIntensityParameter;
foreach (var taggedObject in taggedObjects)
{
var light = taggedObject.GetComponent<Light>();
if (light)
{
light.intensity = lightIntensityParameter.Sample();
}
}
}
protected override void OnIterationStart()
{
var taggedObjects = tagManager.Query<LightRandomizerTag>();
foreach (var taggedObject in taggedObjects)
{
var light = taggedObject.GetComponent<Light>();
if (light)
{
light.intensity = lightIntensityParameter.Sample();
}
}
}
}
```

* **Action**: Add `LightRandomizer` to the list of Randomizers in `SimulationScenario`.
You will notice the the Randomizer's UI snippet contains one Parameter named `Light Intensity Parameter`. This is the same Parameter we added in the code block above. Here, you can set the sampling distribution (`Value`), `Seed`, and `Range` for this float Parameter.
You will notice the the Randomizer's UI snippet contains one Parameter named `Light Intensity Parameter`. This is the same Parameter we added in the code block above. Here, you can set the sampling distribution (`Value`), `Seed`, and `Range` for this float Parameter:
<p align="center">
<img src="Images/light_rand_1.png" width="420"/>
</p>
* **Action**: In the UI snippet for `LightRandomzier`, set range minimum and maximum to 0.5 and 3.

Note that the `if (light)` is not a requirement if you make sure to only add the `LightRandomizerTag` component to objects that have a `Light` component; however, it is good practice to guard against possible mistakes by always make sure a component exists and is not null before using it.
* **Action**: Open `LightRandomizerTag.cs` and replace its contents with the code below:
```
using UnityEngine.Experimental.Perception.Randomization.Randomizers;
public class LightRandomizerTag : RandomizerTag
{
}
```
Yes, the Randomizer tags can be this simple if you just need to attach objects to Randomizers. Later, you will learn how to add code here to encapsulate more data and logic within the randomized objects.
* **Action**: Select `Directional Light` in the Scene's _**Hierarchy**_, and in the _**Inspector**_ tab, add a `Light Randomizer Tag` component.
Let's now add more variation to our light by randomizing its color as well.
* **Action**: Define a new `ColorRgbParameter`:
`public ColorRgbParameter lightColorParameter`
* **Action**: Inside the code block that intensity was previously applied, add code for sampling color from the above Parameter and applying it:
```
if (light)
{
light.intensity = lightIntensityParameter.Sample();
light.color = lightColorParameter.Sample();
}
```
If you now check the UI snippet for `LightRandomizer`, you will notice that `Color Parameter` is now added. This Parameter includes four separate values for `Red`, `Green`, `Blue` and `Alpha`. The range for these is 0 to 1, which is already set and does not need to be modified. However, in order to get varied colors and not just different shades of grey, we need to set different seeds for each value.
正在加载...
取消
保存