LoginSignup
17
18

More than 5 years have passed since last update.

UnityからProcessingへOSCを投げる

Last updated at Posted at 2015-09-03

processingでアプリつくったはいいが、UnityのほうがトラッキングしやすいマーカなんかがあるとUnityからProcessingにOSC投げたくなる。ということでその方法を考えてみる。

Screen Shot 2015-09-01 at 21.35.38.png

Processing使うのは久しぶり。Ver2.1が筆者のPCに入っていた。
ProcessingでOSCをreceiveするにはoscP5をinstallする。

次はUnity。今回使うのはVuforiaです。ARアプリ作る特によく使うのかと。任意の画像をマーカーに容易に設定できるというのは、ARToolkitより優れている点か。

Vuforiaでマーカ登録するとかはこの投稿では割愛。
ひとまず、Unityの任意のシーンにImageTarget objectがあり、Image Target Behaviour scriptにマーカーとなる画像が設定されているとする。
そして、この画像の座標をOSCでProcessing側に投げる方法について。

参考にしたのは以下。
http://learning.codasign.com/index.php?title=Sending_and_Receiving_OSC_Data_Using_Processing

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

void setup() {
  size(400, 400);
  // start oscP5, telling it to listen for incoming messages at port 12000
  oscP5 = new OscP5(this, 12000);

  // set the remote location to be the localhost on port 12000
  myRemoteLocation = new NetAddress("127.0.0.1", 12000);
}

void draw() {
  // do funky stuff here

}

void oscEvent(OscMessage theOscMessage){
  // get the first value as an integer
  int firstValue = theOscMessage.get(0).intValue();

  // get the second value as a float
  //float secondValue = theOscMessage.get(1).floatValue();

  // get the third value as a string
  //String thirdValue = theOscMessage.get(2).stringValue();

  // print out the message
  print("OSC Message Recieved");
  //print(theOscMessage.addrPattern()  + " ");
  //println(firstValue + " " + secondValue + " " + thirdValue);
  println(firstValue);
}

UnityでOSCをsendするにはUnityOSCを使うことにした。
https://github.com/jorgegarcia/UnityOSC

まず任意のfolderにclone
Screen Shot 2015-09-01 at 22.10.54.png

UnityOSC Assetをprojectにimport。
追記
久しぶりに同じことをやろうとしたら何をimportするのか忘れた。
こちらのブログ記事を参考にさせていただいて、構成を思い出した。
githubでcloneするとUnityOSCフォルダ以下に複数フォルダがあるが、取り込む必要があるのはsrcのみである。(testsを入れようとするとエラーがでる。)

任意のオブジェクトにOSCHandlerをaddする。
Screen Shot 2015-09-03 at 18.38.15.png

OSCHandlerのコードをprocessingにOSCを送れるように修正。
void initだけ設定を加える。

    public void Init()
    {
        //Initialize OSC clients (transmitters)
        //Example:      
        CreateClient("myRemoteLocation", IPAddress.Parse("127.0.0.1"), 12000);

        //Initialize OSC servers (listeners)
        //Example:

        //CreateServer("AndroidPhone", 6666);
    }

OSCをsendするメソッドは新規にscriptを追加して処理を書く。
例えばOSCController.csとか。

using UnityEngine;
using System.Collections;

public class OSCController : MonoBehaviour {

    // Use this for initialization
    void Start () {
        // this line triggers the magic
        OSCHandler.Instance.Init ();

        int testInteger = 54321;
        OSCHandler.Instance.SendMessageToClient ("myRemoteLocation", "127.0.0.1:12000", testInteger);

    }

    // Update is called once per frame
    void Update () {
        int testInteger = 54321;
        int random = Random.Range (0, 10000);
        OSCHandler.Instance.SendMessageToClient ("myRemoteLocation", "127.0.0.1:12000", random);

    }
}

Unity & ProcessingをrunするとprocessingにOSCを送れているのがわかる。
Screen Shot 2015-09-03 at 18.48.18.png

17
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
18