* **Action**: In the UI snippet for `MyLightRandomzier`, set the minimum and maximum for range to 0.5 and 3. This range of intensities is arbitrary but will give us a typically nice lighting without excessive darkness or burnt-out highlights. The `MyLightRandomizer` class extends `Randomizer`, which is the base class for all Randomizers that can be added to a Scenario. This base class provides a plethora of useful functions and properties that can help catalyze the process of creating new Randomziers. The `OnIterationStart()` function is used for telling the Randomizer what actions to perform at the start of each Iteration of the Scenario. As seen in the code block, at the start of each Iteration, this class queries the `tagManager` object for all objects that carry the `MyLightRandomizerTag` component. Then, for each object inside the queried list, it first tries to get the `Light` component, and if this component exists, the code sets its intensity to a new random float sampled from `lightIntensityParamter`. Note that the `if (light)` line is not a requirement if you only add the `MyLightRandomizerTag` component to objects that have a `Light` component; however, it is good practice to guard against possible mistakes by always making sure a component exists and is not null before try to use it. * **Action**: Open `MyLightRandomizerTag.cs` and replace its contents with the code below: ``` using UnityEngine; using UnityEngine.Experimental.Perception.Randomization.Randomizers; [AddComponentMenu("Perception/RandomizerTags/MyLightRandomizerTag")] public class MyLightRandomizerTag : RandomizerTag { } ``` Yes, a RandomizerTag can be this simple if you just need it for helping Randomizers query for target objects. 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. * **Action**: Run the simulation again and inspect how `Directional Light` now switches between different intensities. You can pause the simulation and then use the step button (to the right of the pause button) to move the simulation one frame forward and clearly see the varying light intensity Let's now add more variation to our light by randomizing its color as well. * **Action**: Back inside `MyLightRandomizer.cs`, 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 `MyLightRandomizer`, you will notice that `Color Parameter` is added. This Parameter includes four separate randomized values for `Red`, `Green`, `Blue` and `Alpha`. Note that the meaningful range for all of these values is 0-1 (and not 0-255). You can see that the sampling range for red, green, and blue is currently also set to 0-1, which means the parameter covers a full range of colors. A color with (0,0,0) RGB components essentially emits no light. So let's increase the minimum a bit to avoid such a scenario.. * **Action**: Increase the minimum value for red, green, and blue components to 0.4 (this is an arbitrary number that typically produces good-looking results). Each value should also already have a unique `Seed` specified. This is the seed which the sampler will use to produce a random value from the specifed distribution. If two random parameters have the same seed, range, and distribution, they will always have the same value. In the case of this color, this would lead to the red, green, and blue components having equal values, and thus the produced color always being a shade of grey. As such, in order to get varied colors and not just grey, we need to make sure the seed values are different for our red, green, and blue components. * **Action**: In the UI snippet for `MyLightRandomizer`, make sure the red, green, and blue components have different `Seed` values. Set the distribution and value for Alpha to `Constant` and 1, as we do not want to randomize the alpha component of the color. The UI for `My Light Randomizer` should now look like this:
* **Action**: Run the simulation for a few frames to observe the lighting color changing on each iteration. ### Step 2: Bundle Data and Logic Inside Randomization Tags You may sometimes need to bundle certain randomization-related data or logic within an object that are inherent to the object itself. For instance, you may have multiple lights in the Scene but would like each of them to have their own unique range of intensities. It would be quite tedious to add a new Parameter to your light Randomizer for each of your lights. Furthermore, this would make your light Randomizer excessively tailored to one use-case, limiting the Randomizer's reusability. There are also cases were you may need to include certain logic within your object in order to make the Randomizer code more reusable and easy to maintain. For instance, you may want to build an office chair Prefab to use in various simulations. This chair is likely to support a range of customizations for its various parts (back angle, seat angle, seat height, etc.). Instead of directly mapping a Rotation Parameter from a Randomizer to the rotation of the back angle object within the chair, it might be more convenient to have the chair expose the range of possible angles in the form of a simple float between 0 and 1. With this approach, the Randomizer would only need to sample a float Parameter and assign it to the chair. The chair would in turn have a script attached that knows how to map this single float to a certain plausible back angle. You could even map this float to a more complex state of the chair. Your Randomizer would still only need one float Parameter. Let's try this approach with our `Directional Light` object. We will create a duplicate of this light and then have the two lights use different ranges of intensity while both using the exact same float Parameter from `MyLightRandomizer.cs`. * **Action**: Right-click on `Directional Light` in the Scene _**Hierarchy**_, and select _**Duplicate**_. The new light will automatically be named `Directional Light (1)`. * **Action**: Change the Y rotation of `Directional Light (1)` to 60, as shown below:
* **Action**: Change the Y rotation of `Directional Light` to -60. This makes the two lights illuminate the scene from opposing sides, each having a 30 degree angle with the background and foreground planes. * **Action**: Open `MyLightRandomizerTag.cs` and modify it to match the code below: ``` using UnityEngine; using UnityEngine.Experimental.Perception.Randomization.Randomizers; [AddComponentMenu("Perception/RandomizerTags/MyLightRandomizerTag")] public class MyLightRandomizerTag : RandomizerTag { public float minIntensity; public float maxIntensity; public void SetIntensity(float rawIntensity) { var light = gameObject.GetComponent