Skip to content

Instantly share code, notes, and snippets.

@makihiro
makihiro / TestARSpawn.cs
Last active January 14, 2021 04:11
ARTest-TestARSpawn.cs
using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.SceneManagement;
using Assert = UnityEngine.Assertions.Assert;
// Test AR behavior with Unity Test Framework
namespace Tests
{
@makihiro
makihiro / Reproducer.cs
Last active January 14, 2021 04:10
ARTest-Reproducer.cs
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.XR.ARFoundation;
using WebSocketSharp;
using WebSocketSharp.Server;
using System.IO;
// Replay AR data and camera images
namespace ARKitStream
@makihiro
makihiro / Recorder.cs
Last active January 14, 2021 04:04
ARTest-Recorder.cs
using System;
using System.IO;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using Unity.Collections.LowLevel.Unsafe;
using ARKitStream.Internal;
// Save AR Related data and camera images
//L155-157
function onSessionEnded(event) {
xrButton.setSession(null);
}
function addARObjectAt(matrix) {
let newFlower = arObject.clone();
newFlower.visible = true;
newFlower.matrix = matrix;
scene.addNode(newFlower); //sceneに仮想オブジェクトnewFlowerを追加
flowers.push(newFlower);
if (flowers.length > MAX_FLOWERS) {
let oldFlower = flowers.shift();
scene.removeNode(oldFlower);
}
//L198-211,onXRFrame内
if (xrHitTestSource && pose) {
let hitTestResults = frame.getHitTestResults(xrHitTestSource);
if (hitTestResults.length > 0) {
//hit testを行い、hit testの結果が得られたら、バーチャルオブジェクト(reticle)を可視にセットする処理。表示位置はpose情報から取得
let pose = hitTestResults[0].getPose(xrRefSpace);
reticle.visible = true;
reticle.matrix = pose.transform.matrix;
}
}
//L135-146,onSessionStarted内
session.requestReferenceSpace('viewer').then((refSpace) => {
xrViewerSpace = refSpace;
session.requestHitTestSource({ space: xrViewerSpace }).then((hitTestSource) => {
xrHitTestSource = hitTestSource; //hit testの要求
});
});
session.requestReferenceSpace('local').then((refSpace) => {
xrRefSpace = refSpace;
session.requestAnimationFrame(onXRFrame); //Render loop処理のコール
//L107-113
function onRequestSession() {
return navigator.xr.requestSession('immersive-ar', {requiredFeatures: ['local', 'hit-test']})
.then((session) => {
// XRセッションを開始する処理
// requiredFeaturesでhit-testを要求
xrButton.setSession(session);
onSessionStarted(session); //onSessionStartedのコール
});
}
//L99-103
if (navigator.xr) {
navigator.xr.isSessionSupported('immersive-ar')
.then((supported) => {
// XRデバイスが有効でimmersive-arがサポートされていれば、XRボタンを有効にする処理
xrButton.enabled = supported;
});
//L99-103
if (navigator.xr) {
navigator.xr.isSessionSupported('immersive-ar')
.then((supported) => {
// XRデバイスが有効でimmersive-arがサポートされていれば、XRボタンを有効にする処理
xrButton.enabled = supported;
});