Esempio n. 1
0
    void FaceUpdated(ARFaceAnchor anchorData)
    {
        var isTracked = anchorData.isTracked;

        if (isTracked)
        {
            faceGo.localPosition = UnityARMatrixOps.GetPosition(anchorData.transform);
            faceGo.localRotation = UnityARMatrixOps.GetRotation(anchorData.transform);


            faceVertices  = anchorData.faceGeometry.vertices;
            faceUV        = anchorData.faceGeometry.textureCoordinates;
            faceTriangles = anchorData.faceGeometry.triangleIndices;

            UpdateFaceMesh();

            leftEye.transform.localPosition  = anchorData.leftEyePose.position;
            leftEye.transform.localRotation  = anchorData.leftEyePose.rotation;
            rightEye.transform.localPosition = anchorData.rightEyePose.position;
            rightEye.transform.localRotation = anchorData.rightEyePose.rotation;

            UnityARVideo arVideo = manager.unityARVideo;
            OnFaceRotation(faceGo.localRotation * Quaternion.Inverse(arVideo.transform.rotation));
        }

        lastAnchorData = anchorData;
    }
Esempio n. 2
0
    public void ToggleVideo()
    {
        UnityARVideo arVideo = m_camera.gameObject.GetComponent <UnityARVideo>();
        ARKitWorldTrackingSessionConfiguration _config = new ARKitWorldTrackingSessionConfiguration();

        if (arVideo.enabled && arVideo != null)
        {
            _config.enableAutoFocus       = false;
            _config.planeDetection        = UnityARPlaneDetection.None;
            _config.enableLightEstimation = false;
            _config.getPointCloudData     = false;
            m_session.Pause();
            arVideo.enabled = false;
        }
        else
        {
            _config.planeDetection        = planeDetection;
            _config.alignment             = startAlignment;
            _config.getPointCloudData     = getPointCloud;
            _config.enableLightEstimation = enableLightEstimation;
            _config.enableAutoFocus       = enableAutoFocus;
            m_camera.clearFlags           = CameraClearFlags.SolidColor;
            m_session.RunWithConfig(_config);
            arVideo.enabled = true;
        }
    }
        public static void ARFrameUpdated(UnityARCamera arCamera)
        {
            if (SaveStream != null)
            {
                UnityARVideo video = Camera.main.gameObject.GetComponent <UnityARVideo>();

                // save frame
                SaveStream.BeginFrame(
                    Time.realtimeSinceStartup,
                    Camera.main.gameObject.transform.position,
                    Camera.main.gameObject.transform.rotation);

                // save frame points
                foreach (Vector3f pt in arCamera.pointCloudData)
                {
                    Vector3f screenCoords = Camera.main.WorldToScreenPoint(pt);
                    Colorf   c            = Colorf.White;
                    if (video != null)
                    {
                        c = video.QueryPixel(screenCoords.x, screenCoords.y);
                    }

                    SaveStream.AppendSample(pt, screenCoords.xy, c);
                }
            }
        }          // end ARFrameUpdated
Esempio n. 4
0
 void OnEnable()
 {
     mat = GetComponent <LineRenderer>().material;
     if (video == null)
     {
         video = Camera.main.GetComponent <UnityARVideo>();
     }
 }
        void Start()
        {
            leftBarrel        = LeftCamera.GetComponent <BarrelDistortion>();
            rightBarrel       = RightCamera.GetComponent <BarrelDistortion>();
            centerCullingMask = CenterCamera.cullingMask;
            arVideo           = CenterCamera.GetComponent <UnityARVideo>();


            HoloKitCalibration.LoadDefaultCalibration(this);
            UpdateProjectMatrix();
        }
Esempio n. 6
0
 public void SetCamera(Camera newCamera)
 {
     if (m_camera != null)
     {
         UnityARVideo component = m_camera.gameObject.GetComponent <UnityARVideo>();
         if (component != null)
         {
             savedClearMaterial = component.m_ClearMaterial;
             UnityEngine.Object.Destroy(component);
         }
     }
     SetupNewCamera(newCamera);
 }
Esempio n. 7
0
 public void SetCamera(Camera newCamera)
 {
     if (m_camera != null)
     {
         UnityARVideo oldARVideo = m_camera.gameObject.GetComponent <UnityARVideo>();
         if (oldARVideo != null)
         {
             savedClearMaterial = oldARVideo.m_ClearMaterial;
             Destroy(oldARVideo);
         }
     }
     SetupNewCamera(newCamera);
 }
 private void SetupNewCamera(Camera newCamera)
 {
     m_camera = newCamera;
     if (m_camera != null)
     {
         UnityARVideo unityARVideo = m_camera.gameObject.GetComponent <UnityARVideo> ();
         if (unityARVideo != null)
         {
             savedClearMaterial = unityARVideo.m_ClearMaterial;
             Destroy(unityARVideo);
         }
         unityARVideo = m_camera.gameObject.AddComponent <UnityARVideo> ();
         unityARVideo.m_ClearMaterial = savedClearMaterial;
     }
 }
Esempio n. 9
0
 private void SetupNewCamera(Camera newCamera)
 {
     m_camera = newCamera;
     if (m_camera != null)
     {
         UnityARVideo component = m_camera.gameObject.GetComponent <UnityARVideo>();
         if (component != null)
         {
             savedClearMaterial = component.m_ClearMaterial;
             UnityEngine.Object.Destroy(component);
         }
         component = m_camera.gameObject.AddComponent <UnityARVideo>();
         component.m_ClearMaterial = savedClearMaterial;
     }
 }
Esempio n. 10
0
            private void Start()
            {
#if UNITY_EDITOR
                // Disable AR in editor
                if (Camera.main != null)
                {
                    Camera.main.clearFlags = CameraClearFlags.Color;
                    UnityARVideo arVideo = Camera.main.GetComponent <UnityARVideo>();
                    if (arVideo != null)
                    {
                        arVideo.enabled = false;
                    }
                    Transform parent = Camera.main.transform.parent;
                    if (parent != null)
                    {
                        UnityARCameraManager arCameraManager = parent.GetComponentInChildren <UnityARCameraManager>();
                        if (arCameraManager != null)
                        {
                            arCameraManager.enabled = false;
                        }
                    }
                }
                AR3DOFCameraManager ar3dof = GetComponent <AR3DOFCameraManager>();
                if (ar3dof != null)
                {
                    ar3dof.enabled = false;
                }
#endif
                // Initialize the game object we'll use to
                // represent plane anchors
                if (planePrefab != null)
                {
                    UnityARUtility.InitializePlanePrefab(planePrefab);
                }

                // Initialize the anchor manager
                mUnityARAnchorManager = new UnityARAnchorManager();

                // Start the AR kit session
                ARKitWorldTackingSessionConfiguration sessionConfig = new ARKitWorldTackingSessionConfiguration(
                    UnityARAlignment.UnityARAlignmentCamera,
                    UnityARPlaneDetection.Horizontal
                    );
                sessionConfig.enableLightEstimation = true;
                UnityARSessionRunOption sessionRunOptions = UnityARSessionRunOption.ARSessionRunOptionRemoveExistingAnchors | UnityARSessionRunOption.ARSessionRunOptionResetTracking;
                UnityARSessionNativeInterface.GetARSessionNativeInterface().Pause();                 // Stops current run
                UnityARSessionNativeInterface.GetARSessionNativeInterface().RunWithConfigAndOptions(sessionConfig, sessionRunOptions);
            }
Esempio n. 11
0
    private void ReceiveRemoteScreenUVTex(MessageEventArgs mea)
    {
        if (!_bTexturesInitialized)
        {
            return;
        }
        _remoteScreenUvTex.LoadRawTextureData(CompressionHelper.ByteArrayDecompress(mea.data));
        _remoteScreenUvTex.Apply();
        UnityARVideo arVideo = Camera.main.GetComponent <UnityARVideo>();

        if (arVideo)
        {
            arVideo.SetUVTexure(_remoteScreenUvTex);
        }
        SaveData(_timeCount, CallMethodTypeId.ReceiveRemoteScreenUVTex, mea, false);
    }
        static void per_frame_processing()
        {
            if (SaveStream != null && SaveVideoStream != null)
            {
                // save video frame if necessary
                if (Time.realtimeSinceStartup - last_video_frame > (1.0f / (float)TargetVideoFrameRate))
                {
                    UnityARVideo video = Camera.main.gameObject.GetComponent <UnityARVideo>();

                    Texture2D tex     = video.m_ImageTex;
                    byte[]    pngdata = tex.EncodeToPNG();
                    SaveVideoStream.AppendFrame(new gs.VideoFrame()
                    {
                        realTime = Time.realtimeSinceStartup,
                        width    = tex.width, height = tex.height,
                        rgb      = pngdata
                    });
                    last_video_frame = Time.realtimeSinceStartup;
                }
            }
        }
Esempio n. 13
0
    private void Start()
    {
        if (mainCamera == null)
        {
            mainCamera = Camera.main;
        }

        if (mainDirectionalLight == null)
        {
            mainDirectionalLight = Object.FindObjectOfType <Light>();
        }

        mainCamera.clearFlags    = CameraClearFlags.Depth;
        mainCamera.nearClipPlane = 0.01f;

        if (mainCamera.gameObject.GetComponent <UnityARVideo>() == null)
        {
            UnityARVideo unityARVideo = mainCamera.gameObject.AddComponent <UnityARVideo>();
            unityARVideo.m_ClearMaterial = clearMaterial;
        }

        if (mainCamera.gameObject.GetComponent <UnityARCameraNearFar>() == null)
        {
            mainCamera.gameObject.AddComponent <UnityARCameraNearFar>();
        }

        if (mainCamera.gameObject.GetComponent <UnityARCameraManager>() == null)
        {
            UnityARCameraManager unityARCameraManager = mainCamera.gameObject.AddComponent <UnityARCameraManager>();
            unityARCameraManager.m_camera = mainCamera;
        }

        if (mainDirectionalLight.gameObject.GetComponent <UnityARAmbient>() == null)
        {
            mainDirectionalLight.gameObject.AddComponent <UnityARAmbient>();
        }

        UnityARSessionNativeInterface.ARFrameUpdatedEvent += ARFrameUpdated;
    }
    void splat_to_grid(Vector3[] points)
    {
        Vector3f     camPos = Camera.main.gameObject.transform.position;
        UnityARVideo video  = Camera.main.gameObject.GetComponent <UnityARVideo>();

        for (int pi = 0; pi < points.Length; ++pi)
        {
            Vector3f pt = points[pi];
            if (pt.Distance(camPos) > MaxSampleDistanceM)
            {
                continue;
            }

            Vector3f screenCoords = Camera.main.WorldToScreenPoint(pt);
            Colorf   c            = Colorf.White;
            if (video != null)
            {
                c = video.QueryPixel(screenCoords.x, screenCoords.y);
            }

            grid.AddParticle(points[pi], c);
        }
    }
Esempio n. 15
0
        private static void SetupUnityARVideo(GameObject mainCamera)
        {
            if (mainCamera != null)
            {
                UnityARVideo unityARVideo = mainCamera.AddComponent <UnityARVideo> ();

                string[] guids = AssetDatabase.FindAssets("YUVMaterial");

                if (guids != null && guids.Length > 0)
                {
                    string path = AssetDatabase.GUIDToAssetPath(guids [0]);
                    unityARVideo.m_ClearMaterial = AssetDatabase.LoadAssetAtPath <Material> (path);
                }
                else
                {
                    Debug.LogError("YUVMaterial not found in ARKit plugin.");
                }
            }
            else
            {
                Debug.LogError("MainCamera gameobject not referenced in WRLDARKitSetupHelper.");
            }
        }
Esempio n. 16
0
    void FrameUpdated(UnityARCamera unityArCamera)
    {
        // dispatch
        UnityARVideo arVideo = manager.unityARVideo;
        var          camera  = arVideo.GetComponent <Camera>();

        UpdateCamera(camera);
        arVideo.UpdateFrame(unityArCamera);


#if !UNITY_EDITOR // 非editor下使用强同步逻辑
        // update anchors
        ARFaceAnchor anchor = unityArCamera.faceAnchors.FirstOrDefault(face => face.isTracked);
        if (anchor != null)
        {
            FaceUpdated(anchor);
        }
        else
        {
            FaceRemoved(null);
        }
#endif
    }
Esempio n. 17
0
        public override void SetupCamera(Camera camera)
        {
            UnityARVideo unityARVideo = camera.GetComponent <UnityARVideo>();

            if (unityARVideo == null)
            {
                m_CachedClearMaterial = Resources.Load("YUVMaterial", typeof(Material)) as Material;
            }
            else
            {
                m_CachedClearMaterial = unityARVideo.m_ClearMaterial;
                GameObject.Destroy(unityARVideo);
            }

            unityARVideo = camera.gameObject.AddComponent <UnityARVideo>();
            unityARVideo.m_ClearMaterial = m_CachedClearMaterial;

            if (camera.GetComponent <UnityARCameraNearFar>() == null)
            {
                camera.gameObject.AddComponent <UnityARCameraNearFar>();
            }

            camera.clearFlags = CameraClearFlags.Depth;
        }
    // -- // -- // -- // -- // -- // -- // -- // -- // -- // -- //

    void Start()
    {
        if (!isInterfaceEnabled)
        {
            return;
        }

        if (!yuvMaterial)
        {
            Debug.LogError("ARKit-interface cannot start: YuvMaterial is not set.");
            return;
        }

        // modify the main camera in the scene
        Camera currentCamera = MultiARInterop.GetMainCamera();

        if (!currentCamera)
        {
            GameObject currentCameraObj = new GameObject("Main Camera");
            currentCameraObj.tag = "MainCamera";

            currentCamera = currentCameraObj.AddComponent <Camera>();
        }

        // reset camera position & rotation
        currentCamera.transform.position = Vector3.zero;
        currentCamera.transform.rotation = Quaternion.identity;

        // set camera parameters
        currentCamera.clearFlags    = CameraClearFlags.Depth;
        currentCamera.nearClipPlane = 0.1f;
        currentCamera.farClipPlane  = 30f;

        // reference to the AR main camera
        mainCamera = currentCamera;

        // add camera parent
        if (currentCamera.transform.parent == null)
        {
            GameObject cameraParent = new GameObject("CameraParent");
            currentCamera.transform.SetParent(cameraParent.transform);
            DontDestroyOnLoad(cameraParent);
        }
        else
        {
            DontDestroyOnLoad(currentCamera.transform.root.gameObject);
        }

        // add the needed camera components
        arVideoRenderer = currentCamera.gameObject.AddComponent <UnityARVideo>();
        arVideoRenderer.m_ClearMaterial = yuvMaterial;
        backgroundMat = yuvMaterial;

        currentCamera.gameObject.AddComponent <UnityARCameraNearFar>();

        // modify the directional light
        Light currentLight = MultiARInterop.GetDirectionalLight();

        if (!currentLight)
        {
            GameObject currentLightObj = new GameObject("Directional light");

            currentLight      = currentLightObj.AddComponent <Light>();
            currentLight.type = LightType.Directional;
        }

        // reset light position & rotation
        currentLight.transform.position = Vector3.zero;
        currentLight.transform.rotation = Quaternion.Euler(40f, 40f, 0f);
        DontDestroyOnLoad(currentLight.gameObject);

        // set light parameters
        //currentLight.lightmapBakeType = LightmapBakeType.Mixed;
        currentLight.color = new Color32(255, 254, 244, 255);

        // add the ar-light component
        currentLight.gameObject.AddComponent <MultiARDirectionalLight>();

        // reference to the AR directional light
        //directionalLight = currentLight;

        // create camera manager
        GameObject camManagerObj = new GameObject("ARCameraManager");

        DontDestroyOnLoad(camManagerObj);

        UnityARCameraManager camManager = camManagerObj.AddComponent <UnityARCameraManager>();

        camManager.m_camera = currentCamera;

        camManager.startAlignment = cameraAlignment;
        camManager.planeDetection = UnityARPlaneDetection.HorizontalAndVertical;

        //Debug.Log("arImageDatabase: " + (arImageDatabase != null ? arImageDatabase.resourceGroupName : "-"));
        if (arImageDatabase != null)
        {
            camManager.detectionImages = arImageDatabase;
            Debug.Log("camManager.detectionImages set to: " + arImageDatabase);
        }

        // allow relocalization after session interruption
        UnityARSessionNativeInterface.ARSessionShouldAttemptRelocalization = true;

        // get ar-data
        MultiARInterop.MultiARData arData = arManager ? arManager.GetARData() : null;

        // check for point cloud getter
        if (arManager && arManager.pointCloudPrefab != null)
        {
            arData.pointCloudData      = new Vector3[0];
            arData.pointCloudLength    = 0;
            arData.pointCloudTimestamp = 0.0;
        }

        // create surface renderer
        if (arManager && arData != null)
        {
            arData.surfaceRendererRoot      = new GameObject();
            arData.surfaceRendererRoot.name = "SurfaceRenderer";
            DontDestroyOnLoad(arData.surfaceRendererRoot);
        }

//		// check for tracked plane display
//		if(arManager.displayTrackedSurfaces && trackedPlanePrefab)
//		{
//			UnityARUtility.InitializePlanePrefab(trackedPlanePrefab);
//		}

        // add needed events
        UnityARSessionNativeInterface.ARFrameUpdatedEvent           += ARFrameUpdated;
        UnityARSessionNativeInterface.ARSessionTrackingChangedEvent += ARSessionTrackingChanged;

        UnityARSessionNativeInterface.ARAnchorAddedEvent   += PlaneAnchorAdded;
        UnityARSessionNativeInterface.ARAnchorUpdatedEvent += PlaneAnchorUpdated;
        UnityARSessionNativeInterface.ARAnchorRemovedEvent += PlaneAnchorRemoved;

        UnityARSessionNativeInterface.ARUserAnchorAddedEvent   += UserAnchorAdded;
        UnityARSessionNativeInterface.ARUserAnchorRemovedEvent += UserAnchorRemoved;

        // create ARCoreSession component, if the cloud functionality is used
        GoogleARCore.ARCoreSession arCoreSession = gameObject.GetComponent <GoogleARCore.ARCoreSession>();
        if (arCoreSession == null)
        {
            TextAsset cloudApiKey = Resources.Load("RuntimeSettings/CloudServicesApiKey") as TextAsset;

            if (cloudApiKey != null)
            {
                arCoreSession = gameObject.AddComponent <GoogleARCore.ARCoreSession>();
            }
        }

        // interface is initialized
        isInitialized = true;
    }