---
title: "Light Randomization"
canonical: "https://rmanwiki-27.pixar.com/space/RU/592019497/Light%20Randomization"
format: markdown
---
![DemoSign_v01_h264.mov](media://eb93ead8-4729-4ed5-a57e-60b6dc8a4ac6)

![Neon_Random1.jpg](media://8a1b1eac-790c-4713-9004-c34e08c0e29a)

Above is the Solaris scene graph for the randomization of all the lights in our scene. 

 All the magic happens inside *attributewrangle* nodes that control the lighting randomization of each letter in our sign.  I have one wrangle for each of the letters in the scene, as I want to have control over each letter individually.

Let’s take a look at the meshlight Neon tubes first:

---

<span style="color: #4c9aff">**Letters - D - M - O - Neon Tubes**</span>

Create a *attributewrangle *node and under Primitives, we’re selecting the neon tube meshlights we want this node to control. 

Below is the VEX code used to set up the parameters

```
int off = chi("off");
int on = chi("on");
int transition = chi("transition");
int A = off;
int B = off + transition;
int C = off + transition + on;
int D = off + 2 * transition + on;

float val = 0;
int frame = int(@Frame - $FSTART + chi("frameOffset") * @elemnum) % (D + 1);

if(frame < A)
    val = 0;
else if (frame >= A && frame < B)
    val = 1 - (B - frame) / float(B - A);
else if(frame >= B && frame < C)
    val = 1;
else if (frame >= C && frame < D)
    val = (D - frame) / float(D - C);
val *= ch("intensity");

@inputs:defaultFloat = val;
//@inputs:intensity = val;
```


We now have a set of parameters that will allow us to control the randomisation of the prims.

**OFF**

The number of frames that the neons are switched off.

**ON**

The number of frames that the neons are switched on.

**Transition**

The number of frames to transition between OFF and ON states

**FrameOffset**: 

**Intensity**

Sets and overrides the intensity of the lights

![Neon_Random2.jpg](media://d51fa3e2-8f8c-40ad-96e0-65a1d55f1474)


---

<span style="color: #4c9aff">**Letter - E - Globe Bulbs**</span>

This wrangler allows us to control the value of the two primvars we set up in the Globe bulb USD asset.  

Remember, these were called Bulb_COL and Bulb_INT

Here is the code

```
float randomFloat(float min; float  max; string primpath) {
    return fit01(rand(random_shash(primpath), chi("seed")),min, max); 
}

//@inputs:intensity = fit01(rand(random_shash(@primpath), chi("seed")), ch('min'),ch('max')); 
@inputs:defaultFloat = randomFloat(ch('min'),ch('max'), @primpath); 
float randomHue = randomFloat(ch('Hue'), ch('Hue'), @primpath + "hue");
float randomSat = randomFloat(ch('minSat'), ch('maxSat'), @primpath + "sat");
float randomVal = randomFloat(ch('minVal'), ch('maxVal'), @primpath + "val");
v@inputs:defaultColor = hsvtorgb(randomHue,randomSat,randomVal);
```

which creates this set of parameters

![Neon_Random3.jpg](media://e3f9fc17-91bc-4b9d-bb1a-4de97a313347)

**Intensity Min + Max**

The min and max intensity values that will be randomly created for the bulb's intensity (Bulb_INT)

**Bulb Hue**

How much to randomize the hue of each bulb (Bulb_COL)

**Saturation Min + Max **

 The minimum and maximum random saturation values of the bulb color (Bulb_COL)

**Value Min + Max**

The minimum and maximum random values of the bulb color (Bulb_COL)

**Seed**

Allows you to control the speed of the flashing of the bulbs.  

If left to a single frame, the bulbs will not animate. However, using the $FF expression causes the seed to change every frame, thereby animating the bulbs.

**Facing Ratio**

Allows you to control the facing ratio of the bulbs.

---

<span style="color: #4c9aff">**Letter - S - Fairground Bulbs**</span>

This wrangler allows us to control the intensity and color values of the tiny sphere light inside of the glass fairground bulbs.

 

```
float randomFloat(float min; float  max; string primpath) {
    return fit01(rand(random_shash(primpath), chi("seed")),min, max); 
}

//@inputs:intensity = fit01(rand(random_shash(@primpath), chi("seed")), ch('min'),ch('max')); 
@inputs:intensity = randomFloat(ch('min'),ch('max'), @primpath); 
float randomHue = randomFloat(ch('Hue'), ch('Hue'), @primpath + "hue");
float randomSat = randomFloat(ch('minSat'), ch('maxSat'), @primpath + "sat");
float randomVal = randomFloat(ch('minVal'), ch('maxVal'), @primpath + "val");
v@inputs:color = hsvtorgb(randomHue,randomSat,randomVal);
```

Again, creating this set of parameters

![Neon_Random4.jpg](media://baa045e7-468f-4f20-9066-23f1483fd19d)

---

<span style="color: #4c9aff">**Summary**</span>

So … to summarize.

With this set of wrangles, we can randomly flash and control meshlights, the Glow intensity, and color of a PxrSurface and analytic USD lights.  

Feel free to use these code snippets in your scenes where you need to create randomisation of lights and shaders.