34
edits
m (1.5st draft) |
(→Unity: formatting) |
||
(7 intermediate revisions by the same user not shown) | |||
Line 20: | Line 20: | ||
== Processing == | == Processing == | ||
In Processing you need to install a library. Click on ''Tools'' – ''Add Tools'' – Choose ''Libraries'' – Search for oscP5 and install it.<br><br> | In Processing you need to install a library. Click on ''Tools'' – ''Add Tools'' – Choose ''Libraries'' – Search for oscP5 and install it.<br><br> | ||
This is the basic processing script for sending Osc data (it just sets up the connection, actions can be added): <br>< | This is the basic processing script for sending Osc data (it just sets up the connection, actions can be added): <br> | ||
//load libraries | <source lang="javascript"> | ||
import netP5.*; | //load libraries | ||
import oscP5.*; | import netP5.*; | ||
OscP5 oscP5; | import oscP5.*; | ||
NetAddress myRemoteLovation; | |||
//make a connection | OscP5 oscP5; | ||
void setup () { | NetAddress myRemoteLovation; | ||
osP5 = new OscP5 (this, 9000); | |||
myRemoteLocation = new NetAddress("127.0.0.1", 9000); | //make a connection | ||
} | void setup () { | ||
// this is where you can make things happen and send values to Unity | osP5 = new OscP5 (this, 9000); | ||
void draw () { | myRemoteLocation = new NetAddress("127.0.0.1", 9000); | ||
OscMessage myMessage = new OscMessage ("/"); | } | ||
// add a value to the message (optional) | |||
myMessage.add(somevalue); | // this is where you can make things happen and send values to Unity | ||
// add a bool to the message (optional) | void draw () { | ||
myMessage.add(true); | OscMessage myMessage = new OscMessage ("/"); | ||
oscP5.send(myMessage, myRemoteLocation); | // add a value to the message (optional) | ||
} | myMessage.add(somevalue); | ||
// this is where you get feedback in Processing | // add a bool to the message (optional) | ||
void oscEvent (OscMessage theOscMessage) { | myMessage.add(true); | ||
print("### received an osc message."); | oscP5.send(myMessage, myRemoteLocation); | ||
print(" addrpattern: "+theOscMessage.addrPattern()); | } | ||
println(" typetag: "+theOscMessage.typetag()); | |||
println("### received an osc message. with address pattern"+theOscMessage.addrPattern()+" typetag | // this is where you get feedback in Processing | ||
}</ | void oscEvent (OscMessage theOscMessage) { | ||
print("### received an osc message."); | |||
print(" addrpattern: "+theOscMessage.addrPattern()); | |||
println(" typetag: "+theOscMessage.typetag()); | |||
println("### received an osc message. with address | |||
pattern"+theOscMessage.addrPattern()+" typetag | |||
+theOscMessage.typetag()); | |||
} | |||
</source> | |||
== Unity == | == Unity == | ||
With just a little help from Google (or bing if you’re one of those people) you can find one of these Osc Receiver Scripts. You will also need scripts for [https://github.com/heaversm/unity-osc-receiver/tree/master/Assets/Plugins Osc and Udp]. | With just a little help from Google (or bing if you’re one of those people) you can find one of these Osc Receiver Scripts. You will also need scripts for [https://github.com/heaversm/unity-osc-receiver/tree/master/Assets/Plugins Osc and Udp]. | ||
Line 53: | Line 62: | ||
1. Import the scripts to the Assets folder using drag&drop. | 1. Import the scripts to the Assets folder using drag&drop. | ||
If you’re working on a real actual project it might be best to create a folder for all your scripts. <br> | If you’re working on a real actual project it might be best to create a folder for all your scripts. <br> | ||
2. Create an empty game object (right click in the | 2. Create an empty game object (right click in the hierarchy) and rename it, so you know what it is. <br> | ||
3. From the assets you drag the scripts onto the empty game object. <br> | 3. From the assets you drag the scripts onto the empty game object. <br> | ||
4. Do not forget to adjust the IP, and the port in the inspector. <br> | 4. Do not forget to adjust the IP, and the port in the inspector. <br> | ||
5. In the Inspector you see the public variable | [[File:IPandPortintheOSCReceiver.png]]<br> | ||
<br> | 5. In the Inspector you see the public variable Game Receiver within the Osc Receiver Script. Drag the object you want to manipulate from the hierarchy onto the Game Receiver value. By default this value is called “Cube”. <br> | ||
[[File:GameReceiver.png]]<br> | |||
6. If you haven't done it already - Safe! Ctrl+S on Windows, Macs probably do that differently, but you will know that better than me.<br><br> | |||
If all this code is completely new to you and you only understand trainstation – here is an explanation for the whole OSC Receiver Script: | If all this code is completely new to you and you only understand trainstation – here is an explanation for the whole OSC Receiver Script: | ||
<br> | <br> | ||
Public values are adjustable without any coding; just use the inspector for that. <br>< | Public values are adjustable without any coding; just use the inspector for that. <br> | ||
public var RemoteIP : String = ""; | <source lang="javascript"> | ||
public var SendToPort : int = xxxx; | public var RemoteIP : String = ""; | ||
public var ListenerPort : int = yyyy; | public var SendToPort : int = xxxx; | ||
public var controller : Transform; | public var ListenerPort : int = yyyy; | ||
public var gameReceiver = "Cube";</ | public var controller : Transform; | ||
In the Start() function all the necessary scripts and components are connected. This will happen once you press Play. <br>< | public var gameReceiver = "Cube"; | ||
public function Start () | </source> | ||
{ | In the Start() function all the necessary scripts and components are connected. This will happen once you press Play. <br> | ||
var udp : UDPPacketIO = GetComponent("UDPPacketIO"); | <source lang="javascript"> | ||
udp.init(RemoteIP, SendToPort, ListenerPort); | public function Start () | ||
handler = GetComponent("Osc"); | { | ||
handler.init(udp); | var udp : UDPPacketIO = GetComponent("UDPPacketIO"); | ||
handler.SetAllMessageHandler(AllMessageHandler); | udp.init(RemoteIP, SendToPort, ListenerPort); | ||
}</ | handler = GetComponent("Osc"); | ||
The AllMessageHandler() function is where the Osc happens. This function unpacks the Osc signals, so you can address them in other functions. In this function you can command, whenever A happens, you want FunctionB() to be executed. <br>< | handler.init(udp); | ||
public function AllMessageHandler(oscMessage: OscMessage){ | handler.SetAllMessageHandler(AllMessageHandler); | ||
var msgString = Osc.OscMessageToString(oscMessage); | } | ||
var msgAddress = oscMessage.Address; | </source> | ||
var msgValue = oscMessage.Values[0];</ | The AllMessageHandler() function is where the Osc happens. This function unpacks the Osc signals, so you can address them in other functions. In this function you can command, whenever A happens, you want FunctionB() to be executed. <br> | ||
So you typed all that code and there are no errors but that stupid thing still won't do what it should - try Debug.Logs! Just add them everywhere! They can tell you what parts of your script are accessed after you press Play. <br>< | <source lang="javascript"> | ||
Debug.Log("Stakkars meg, som har skrevet 50 Debug.Logs");<br>< | public function AllMessageHandler(oscMessage: OscMessage){ | ||
var msgString = Osc.OscMessageToString(oscMessage); | |||
var msgAddress = oscMessage.Address; | |||
var msgValue = oscMessage.Values[0]; | |||
</source> | |||
So you typed all that code and there are no errors but that stupid thing still won't do what it should - try Debug.Logs! Just add them everywhere! They can tell you what parts of your script are accessed after you press Play. <br> | |||
<source lang="javascript"> | |||
Debug.Log("Stakkars meg, som har skrevet 50 Debug.Logs"); | |||
</source> | |||
The lines will be written in the console section of Unity once they are accessed.<br><br> | |||
[[File:50Debug_Logs.png]] | |||
== Example: Make an object jump on clapping == | == Example: Make an object jump on clapping == | ||
Line 90: | Line 111: | ||
a) Install an additional library for sound. Click on ''Tools'' – ''Add Tools'' – Choose ''Libraries'' – Search for Minim and install it. <br> | a) Install an additional library for sound. Click on ''Tools'' – ''Add Tools'' – Choose ''Libraries'' – Search for Minim and install it. <br> | ||
b) To your imported Libraries add <br> | b) To your imported Libraries add <br> | ||
< | <source lang="javascript"> | ||
import ddf.minim.*; | import ddf.minim.*; | ||
</ | </source> | ||
c) Add Audio variables to your script. This script will also draw a circle depending on the audio volume. <br> | |||
c) Add Audio variables to your script. This script will also draw a circle depending on the audio volume. <br>< | <source lang="javascript"> | ||
float x; | float x; | ||
float y; | float y; | ||
Minim minim; | |||
AudioInput input;</ | Minim minim; | ||
d) In the Setup() function add the values for the circle and create the audiotoolkit <br>< | AudioInput input; | ||
size (320, 240); | </source> | ||
smooth(); | d) In the Setup() function add the values for the circle and create the audiotoolkit <br> | ||
stroke (255, 25); | <source lang="javascript"> | ||
noFill (); | size (320, 240); | ||
// Set start position | smooth(); | ||
x = 0; | stroke (255, 25); | ||
y = 20; | noFill (); | ||
minim = new Minim (this); | |||
input = minim.getLineIn (Minim.STEREO, 512); | // Set start position | ||
background (0);</ | x = 0; | ||
e) The draw() function will be the container of the circle’s size. It will only send the values to Unity if the circle is big enough. <br>< | y = 20; | ||
void draw () { | |||
int i =0; | minim = new Minim (this); | ||
float dim = input.mix.level () * width;<br> | input = minim.getLineIn (Minim.STEREO, 512); | ||
if (dim >= 80){System.out.println("Loud enough: "+dim); | background (0); | ||
OscMessage myMessage = new OscMessage("/"); | </source> | ||
myMessage.add(dim); | e) The draw() function will be the container of the circle’s size. It will only send the values to Unity if the circle is big enough. <br> | ||
myMessage.add(true); | <source lang="javascript"> | ||
oscP5.send(myMessage, myRemoteLocation);}</ | void draw () { | ||
int i =0; | |||
float dim = input.mix.level () * width;<br> | |||
if (dim >= 80){System.out.println("Loud enough: "+dim); | |||
OscMessage myMessage = new OscMessage("/"); | |||
myMessage.add(dim); | |||
myMessage.add(true); | |||
oscP5.send(myMessage, myRemoteLocation);} | |||
</source> | |||
f) When you are ready, press Play. <br> <br> | f) When you are ready, press Play. <br> <br> | ||
'''2. In Unity:''' <br> | '''2. In Unity:''' <br> | ||
a) Create an object in Unity. Drag this object onto the gameReceiver of your Osc Receiver Script. This object is now the gameReceiver and will behave as you define in the Osc Receiver script. <br> | a) Create an object in Unity. Drag this object onto the gameReceiver of your Osc Receiver Script. This object is now the gameReceiver and will behave as you define in the Osc Receiver script. <br> | ||
b) Open up the Osc Receiver Script. Define a new private variable.<br>< | b) Open up the Osc Receiver Script. Define a new private variable. <br><br><source lang="javascript"> | ||
private var moveSpeed : int = 0; </ | private var moveSpeed : int = 0; | ||
c) Create a new public function.<br>< | </source> | ||
public function MoveObject(msgValue) : void | c) Create a new public function.<br> | ||
{ | <source lang="javascript"> | ||
moveSpeed = msgValue; | public function MoveObject(msgValue) : void | ||
}</ | { | ||
d) In the Update() function add <br>< | moveSpeed = msgValue; | ||
var go = GameObject.Find(gameReceiver); | } | ||
go.transform.Translate(0, moveSpeed, 0); </ | </source> | ||
e) In the AllMessageHandler() function add <br>< | d) In the Update() function add <br> | ||
switch (msgAddress){ | <source lang="javascript"> | ||
default: | var go = GameObject.Find(gameReceiver); | ||
MoveObject(msgValue); | go.transform.Translate(0, moveSpeed, 0); | ||
break;}</ | </source> | ||
e) In the AllMessageHandler() function add <br> | |||
<source lang="javascript"> | |||
switch (msgAddress){ | |||
default: | |||
MoveObject(msgValue); | |||
break;} | |||
</source> | |||
f) When you are ready, press Play. | f) When you are ready, press Play. | ||
edits