Giter Site home page Giter Site logo

stephenguanqi / unity-arkit-plugin Goto Github PK

View Code? Open in Web Editor NEW
83.0 14.0 21.0 69.71 MB

Modified plugin source and Add custom ARKit projects implement on Unity

License: GNU General Public License v3.0

C# 74.14% ShaderLab 4.81% Objective-C 4.72% Objective-C++ 16.33%
arkit unity objective-c augmented-reality arframe cvpixelbuffer scenekit ios

unity-arkit-plugin's Introduction

Unity-ARKit-Plugin

License: GPL v3

Modified plugin source and add custom ARKit projects implemented by Unity

Introduction

The unity-arkit-plugin enables using all the functionality of the ARKit SDK simply within your Unity projects for iOS. The plugin exposes ARKit SDK's world tracking capabilities, rendering the camera video input, plane detection and update, point cloud extraction, light estimation, and hit testing API to Unity developers for their AR projects. For more information, you can go to the official repo for downloading.

This repository has two extensions for the official plugin. First it modifies the existing UnityARKitScene project to change it to a zombie walk AR application. The app support hitTest function, changing the zombie's euler angle and pos, as well as scaling the zombie up and down with walking animation. Second the repository has make changes to the plugin's source code in the NativeInterface folder, in order to get the captured image CVPixelBuffer from the ARFrame object, as well as the camera pos, timestamp, lightestimation, etc. and saved those info into the native document directory of the app container asyncronously. And the UnityARRecorder app has realize this function.

Environment Requirements:

  • Xcode 9 that contains ARKit framework
  • iOS 11
  • iOS devices that support A9 or better processor for ARWorldTrackingSessionConfiguration (iPhone6s or higher)
  • Unity v5.6.1p1+ with iOS plugin

Usage

  1. $ git clone https://github.com/StephenGuanqi/Unity-ARKit-Plugin.git
  2. Create a new empty project in your Unity, and drag the unity-arkit-plugin.unitypackage into your project.
  3. You could see multiple unity projects whose names start with UnithyAR
  4. Try any projects and made modifications you like, and build with the iOS plugin.
  5. Open the XCode projects, add the CoreImage framework and finish building.

Extended Projects

ZombieWalk

gif

The project enables cloudpoint and plane detection, and perform hitTest against existing plane to change the zombie's transform. It also changes the zombie's euler angle to face towards the position of the camera in world coordinates.

UnityARRecorder

gif

The ARSessionNative.mm in the plugin source code export various functions to C# layer to let users configure the ARSession using C#, and in turn the objective-C++ class take the passed in paramters and conduct processing like parsing the UnityARKitWorldTrackingConfiguration and set it to the ARSession, or transforming left-hand coordinates(Unity) to right-hand coordinaets(ARKit).

The core in ARSessionNative.mm is that it provides callback method for ARFrame update and ARAnchor update. This callback is intended to be implemented by user in C#, and when the ARFrame and ARAnchor id updated, the corresponding delegate method in objective-c like (void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame and (void)session:(ARSession *)session didAddAnchors:(NSArray<ARAnchor*>*)anchors will call the callback function and passed in the ARFrame and ARAnchor parameter.

However, the official plugin provides many interfaces for the native ARKit API, except for method to get CVPixelBuffer in ARFrame. The ARSessionNative.mm only gives user the opportunity to manipulate the ARCamera, not the ARFrame, therefore it's hard for us to get the captured image and conduct our own research.

This repository has modified the ARSessionNative.mm and UnityARSessionNativeInterface, meanwhile added ARFrameHandler class to get and convert the CVPixelBuffer to UIImage, save it to jpg file, and save the camera pos and other info into json file every time the frame is updated.

The sample json file is as follows:

{
  "27255.603090583.jpg" : {
    "lightEstimate" : 944.16241455078125,
    "imageResolution" : {
      "width" : 1280,
      "height" : 720
    },
    "timeStamp" : "27255.603090583",
    "cameraPos" : {
      "x" : -0.0040707914158701897,
      "y" : 0.0039433315396308899,
      "z" : 0.0042630583047866821
    },
    "cameraTransform" : [
      [
        0.83647698163986206,
        -0.21551112830638885,
        0.50384646654129028,
        -0.0040707914158701897
      ],
      [
        0.084991201758384705,
        0.95931828022003174,
        0.26923003792762756,
        0.0039433315396308899
      ],
      [
        -0.54137128591537476,
        -0.18238219618797302,
        0.82076418399810791,
        0.0042630583047866821
      ],
      [
        0,
        0,
        0,
        1
      ]
    ],
    "cameraIntrinsics" : [
      [
        1088.72119140625,
        0,
        639.5
      ],
      [
        0,
        1088.72119140625,
        359.5
      ],
      [
        0,
        0,
        1
      ]
    ],
    "imageName" : "27255.603090583.jpg",
    "cameraEulerAngle" : {
      "x" : -0.27259346842765808,
      "y" : 0.55055916309356689,
      "z" : 0.088364705443382263
    }
  }
}

You can find more sample json and image data in the sampleAppContainer.xcappdata.

Original Official Projects

ParticlePainter

gif The app paint the gameobject only when the distance between the current camera transform and the previous camera transform is larger than the threshold.

ARBallz

gif The app put balls on the coordinates derived by the hitTest result, when a plane has been detected.

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Thanks

Augmented Reality Tutorial: APPLE ARkit is AMAZING!!!

License

This custom Unity-ARKit-Plugin is under GNU GPL v3.0 License. SEE LICENSEfor details.

unity-arkit-plugin's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unity-arkit-plugin's Issues

How do you move an object in Arkit with script?

changing transform doesn't work. Do you have any idea?

Move my gameObject up from existing position in AR world but none of it works.

I'm trying to do transform.DOMove(....) to move the gameObject up AR World. I also tried changing the localposition

But none of that works. Object ie. prefab stays at same position.

How do we move the object that's instantiated in AR World?

I have commented the code in which I'm also directly modifying the transform but doesn't work either.

DOTween is free Tweening package to Tween an Object

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.iOS;
using DG.Tweening;
using UnityEngine.UI;

public class TestHitControl : MonoBehaviour {

	public GameObject obj;
	private UnityARSessionNativeInterface m_session;

	// Use this for initialization
	void Start () {
		DOTween.Init(false, true, LogBehaviour.ErrorsOnly);
		m_session = UnityARSessionNativeInterface.GetARSessionNativeInterface ();
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.touchCount > 0) {
			if (Input.GetTouch (0).phase == TouchPhase.Began) {
				createGameObject ();
			}
		}
	}

	void createGameObject(){
		Vector3 screenPos = Camera.main.ScreenToViewportPoint (Input.GetTouch (0).position);
		ARPoint point = new ARPoint{ x= screenPos.x,y=screenPos.y };

		ARHitTestResultType[] resultTypes = {
			ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, 
			// if you want to use infinite planes use this:
			//ARHitTestResultType.ARHitTestResultTypeExistingPlane,
			ARHitTestResultType.ARHitTestResultTypeHorizontalPlane, 
			ARHitTestResultType.ARHitTestResultTypeFeaturePoint
		}; 

		foreach (ARHitTestResultType resultType in resultTypes)
		{
			List<ARHitTestResult> hitResults = m_session.HitTest (point, resultType);
			if (hitResults.Count > 0) {
				GameObject currentObj = GameObject.Instantiate (obj);
				
				currentObj.transform.position = UnityARMatrixOps.GetPosition (hitResults [0].worldTransform);
				currentObj.transform.rotation = UnityARMatrixOps.GetRotation (hitResults [0].worldTransform);
				currentObj.transform.LookAt (new Vector3(Camera.main.transform.position.x, currentObj.transform.position.y, Camera.main.transform.position.z));
				
				myText.text = currentObj.transform.position.ToString(); 

				StartCoroutine(SlideUp(currentObj)); 

				//new WaitForSeconds(10f);
				//obj.transform.DOMove(currentObj.transform.position + (Vector3.up * 100), 10); 
			}
		}
	}

	IEnumerator SlideUp(GameObject currentObj) {
		yield return new WaitForSeconds(1f);
		obj.transform.DOMove(currentObj.transform.position + (Vector3.up * 100), 10);
	}
}

feature request: Move multiple elements independently

I'd love a feature to create multiple objects and be able to move them independently of each other.

example:
I have two UI buttons, one for a cube and one for a sphere.
I can click the cube button and create a cube at the position of the plane I touched. I can then touch another position to move the cube to this new position.
I can then do the same for the sphere after clicking it's corresponding button.

Would something like that be possible?

Build Errors after importing plugin

Hi there,

I'm getting the following errors after installing the plugin:

Assets/UnityARKitPlugin/ARKitRemote/SerializableObjects.cs(146,10): error CS0246: The type or namespace name `UnityVideoParams' could not be found. Are you missing an assembly reference?
Assets/UnityARKitPlugin/ARKitRemote/SerializableObjects.cs(147,10): error CS0246: The type or namespace name `UnityARLightEstimate' could not be found. Are you missing an assembly reference?
Assets/UnityARKitPlugin/ARKitRemote/SerializableObjects.cs(288,70): error CS0246: The type or namespace name `ARKitWorldTrackingSessionConfiguration' could not be found. Are you missing an assembly reference?

Any idea what's wrong?

I'm using Unity 2017.1.1p4 and the latest Unity-ARKit-Plugin (official)

CVbuffer access in Unity

Hi,
Can I get the CVbuffer data directly inside Unity, like the IntPtr of the Yplane and CbCr?
It will be very usefull to feed another plugin with the YUV buffer for processing.
Your plugin can do that?
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.