Skip to content

Instantly share code, notes, and snippets.

@nyatla
Created May 11, 2013 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyatla/5559375 to your computer and use it in GitHub Desktop.
Save nyatla/5559375 to your computer and use it in GitHub Desktop.
It is a sample to handle two webcamera at the same time the on NyARToolkit Unity3D. Object hirarchy left +CameraL +Directionallight +MarkerObjectL +Cube +PlaneL right +CameraR +Directionallight +MarkerObjectR +Cube +PlaneR
using UnityEngine;
using System;
using System.Collections;
using jp.nyatla.nyartoolkit.cs.markersystem;
using jp.nyatla.nyartoolkit.cs.core;
using NyARUnityUtils;
using System.IO;
/// <summary>
/// AR camera behaviour.
/// This sample shows simpleLite demo.
/// 1.Connect webcam to your computer.
/// 2.Start sample program
/// 3.Take a "HIRO" marker on capture image
///
/// </summary>
public class LeftBehaviour : MonoBehaviour
{
private NyARUnityMarkerSystem _ms;
private NyARUnityWebCam _ss;
private int mid;//marker id
private GameObject _bg_panel;
void Awake()
{
//setup unity webcam
WebCamDevice[] devices= WebCamTexture.devices;
if (devices.Length<=0){
Debug.LogError("No Webcam.");
return;
}
WebCamTexture w=new WebCamTexture(devices[1].name,320,240,15);
//Make WebcamTexture wrapped Sensor.
this._ss=NyARUnityWebCam.createInstance(w);
//Make configulation by Sensor size.
NyARMarkerSystemConfig config = new NyARMarkerSystemConfig(this._ss.width,this._ss.height);
this._ms=new NyARUnityMarkerSystem(config);
mid=this._ms.addARMarker("./Assets/Data/patt.hiro",16,25,80);
//This line loads a marker from texture
//mid=this._ms.addARMarker((Texture2D)(Resources.Load("MarkerHiro", typeof(Texture2D))),16,25,80);
//setup background
this._bg_panel=GameObject.Find("PlaneL");
this._bg_panel.renderer.material.mainTexture=w;
this._ms.setARBackgroundTransform(this._bg_panel.transform);
//setup camera projection
this._ms.setARCameraProjection(this.camera);
//sepalate view space
GameObject root=GameObject.Find("left");
root.transform.position=new Vector3(0,0,0);
return;
}
// Use this for initialization
void Start ()
{
//start sensor
this._ss.start();
}
// Update is called once per frame
void Update ()
{
//Update SensourSystem
this._ss.update();
//Update marker system by ss
this._ms.update(this._ss);
//update Gameobject transform
if(this._ms.isExistMarker(mid)){
this._ms.setMarkerTransform(mid,GameObject.Find("MarkerObjectL").transform);
Debug.Log(c+":"+this._ms.getConfidence(mid));
}else{
Debug.Log(c+":not found");
// hide Game object
GameObject.Find("MarkerObjectL").transform.localPosition=new Vector3(0,0,-100);
}
c++;
}
static int c=0;
}
using UnityEngine;
using System;
using System.Collections;
using jp.nyatla.nyartoolkit.cs.markersystem;
using jp.nyatla.nyartoolkit.cs.core;
using NyARUnityUtils;
using System.IO;
/// <summary>
/// AR camera behaviour.
/// This sample shows simpleLite demo.
/// 1.Connect webcam to your computer.
/// 2.Start sample program
/// 3.Take a "HIRO" marker on capture image
///
/// </summary>
public class RightBehaviour : MonoBehaviour
{
private NyARUnityMarkerSystem _ms;
private NyARUnityWebCam _ss;
private int mid;//marker id
private GameObject _bg_panel;
void Awake()
{
//setup unity webcam
WebCamDevice[] devices= WebCamTexture.devices;
if (devices.Length<=0){
Debug.LogError("No Webcam.");
return;
}
WebCamTexture w=new WebCamTexture(devices[0].name,320,240,15);
//Make WebcamTexture wrapped Sensor.
this._ss=NyARUnityWebCam.createInstance(w);
//Make configulation by Sensor size.
NyARMarkerSystemConfig config = new NyARMarkerSystemConfig(this._ss.width,this._ss.height);
this._ms=new NyARUnityMarkerSystem(config);
mid=this._ms.addARMarker("./Assets/Data/patt.hiro",16,25,80);
//This line loads a marker from texture
//mid=this._ms.addARMarker((Texture2D)(Resources.Load("MarkerHiro", typeof(Texture2D))),16,25,80);
//setup background
this._bg_panel=GameObject.Find("PlaneR");
this._bg_panel.renderer.material.mainTexture=w;
this._ms.setARBackgroundTransform(this._bg_panel.transform);
//setup camera projection
this._ms.setARCameraProjection(this.camera);
//sepalate view space
GameObject root=GameObject.Find("right");
root.transform.position=new Vector3(10000,0,0);
return;
}
// Use this for initialization
void Start ()
{
//start sensor
this._ss.start();
}
// Update is called once per frame
void Update ()
{
//Update SensourSystem
this._ss.update();
//Update marker system by ss
this._ms.update(this._ss);
//update Gameobject transform
if(this._ms.isExistMarker(mid)){
this._ms.setMarkerTransform(mid,GameObject.Find("MarkerObjectR").transform);
Debug.Log(c+":"+this._ms.getConfidence(mid));
}else{
Debug.Log(c+":not found");
// hide Game object
GameObject.Find("MarkerObjectR").transform.localPosition=new Vector3(0,0,-100);
}
c++;
}
static int c=0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment