Interface Lab: Week 3 (Particles!)

August 11, 2020

This week we were introduced to particle systems in Unity. According to the materials, particle systems are were a lot of the unity magic happens when it comes to environmental / immersive interactives.

Christian’s videos were extremely helpful for getting started with particles systems and connecting them to a physical interface. This was the first week I actually started to explore the Unity scripting docs, which is a good sign because it means I’m starting to understand what I need to look for.

For the physical side of things, this was the first time I was able to wire up two analog sensors to unity. I have a potentiometer and an LDR wired up for this interaction. I still have some ways to go before I feel comfortable improvising on the breadboard though.

The interaction I managed to wire up has the potentiometer controlling the emission rate of the particle system and the LDR controlling the color of the particles. Somewhere along the line I somehow converted my particles to cubes, I have no idea how I did that and no idea how to change it back.

Arduino Script

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  int ldrValue = analogRead(A1);
  // print out the value you read:
  Serial.print(ldrValue);
  Serial.print(",");
  Serial.println(sensorValue);
  delay(100);        // delay in between reads for stability
}

Unity Script

using UnityEngine;
using System.Collections;


/**
 * When creating your message listeners you need to implement these two methods:
 *  - OnMessageArrived
 *  - OnConnectionEvent
 */
public class ParticleSystemKnobs : MonoBehaviour
{



	public float ldr = 0;
	public float pot = 0;

	public ParticleSystem particles;
    // Invoked when a line of data is received from the serial device.
    void OnMessageArrived(string msg)
    {

    	string[] msgArr;

        Debug.Log("Message arrived: " + msg);

        msgArr= msg.Split(',');

        ldr = float.Parse(msgArr[0]);
        pot = float.Parse(msgArr[1]);

        Debug.Log("LDR::" + ldr);
        Debug.Log("POT::" + pot);

        // let's tie the pot to emission rate
        var emission = particles.emission;
		var rate = emission.rate;

		if(pot < 20) {
			rate.constantMax = 1f;

		} else if (pot < 100) {
			rate.constantMax = 3f;
		}
		else if (pot < 300) {
			rate.constantMax = 5f;
		}
		else if (pot < 600) {
			rate.constantMax = 7f;
		}
		else if (pot < 1000) {
			rate.constantMax = 10f;
		}
		else {
			rate.constantMax = 20f;
		}

		emission.rate = rate;

		// tie the color to the LDR
        var col = particles.colorOverLifetime;
        col.enabled = true;
        Gradient grad = new Gradient();

        if(ldr < 100) {
        	grad.SetKeys( new GradientColorKey[] { new GradientColorKey(Color.cyan, 0.0f), new GradientColorKey(Color.magenta, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f), new GradientAlphaKey(0.0f, 1.0f) } );

        }
        else {
        	grad.SetKeys( new GradientColorKey[] { new GradientColorKey(Color.red, 0.0f), new GradientColorKey(Color.blue, 1.0f) }, new GradientAlphaKey[] { new GradientAlphaKey(1.0f, 0.0f), new GradientAlphaKey(0.0f, 1.0f) } );

        }

        

        col.color = grad;

    }

    // Invoked when a connect/disconnect event occurs. The parameter 'success'
    // will be 'true' upon connection, and 'false' upon disconnection or
    // failure to connect.
    void OnConnectionEvent(bool success)
    {
        if (success)
            Debug.Log("Particles: Connection established");
        else
            Debug.Log("Nope!");
    }
}