2.1K Views
January 16, 23
スライド概要
可視化技術や人間計測/空間計測技術を活用した問題解決に関する研究開発。 ARコンテンツ作成勉強会(tryAR)を主催。
Getting Started with WebXR using Babylon.js for HoloLens 2 / Meta Quest
Download sample files https://github.com/TakashiYoshinaga/AR-Fukuoka/ raw/main/Babylonjs_20220727/Samples.zip
Todayʼs Goal https://youtu.be/MyY8gUxv0vA Introduction to the basics of using Babylon.js and the use of hand interaction by MRTK
Duplicate Template Source Code https://glitch.com/~babylon-template GET STARTED
Duplicate Template Source Code Remix Your Own
Duplicate Template Source Code Click on index.html and confirm that the code is displayed index.html
Duplicate Template Source Code Click on index.html and confirm that the code is displayed Editor Preview
Tip: If Preview is Not Displayed PREVIEW
Tip: If Preview is Not Displayed Open preview pane
Exercise Procedure Web browser on PC Creation of 3D space HoloLens/Quest XR Mode Interaction by MRTK Visualize Hands
Check the Template Lesson01
Check the Template Loading Library Description of drawing and interaction (today's main focus) Layout of drawing area
Check the Template Layout of drawing area
Explanation of HTML Description
<!-- Area to be drawn -->
<canvas id="renderCanvas" style="width: 100%; height: 100%;"></canvas>
ID is used when accessing from javascript
Paste the canvas in the window at the full size.
Check the Template Loading Library
テンプレートの確認 Description of drawing and interaction (today's main focus)
Exercise Procedure Web browser on PC Creation of 3D space HoloLens/Quest XR Mode Interaction by MRTK Visualize Hands
Declaration of Variables Add variable here //Initialization of variables after page loading is completed window.onload = function() { } //Initialization of 3D scene function createScene() { } //Initialization of WebXR settings async function initializeXR(){ } //Initialization of 3D objects to be drawn function addObjects(){ }
Declaration of Variables let canvas; //canvas element (drawing area) ← "where" to draw let engine; //Drawing functionality with Babylon.js ← "What" to draw with let scene; //Virtual 3D space ← "What" to draw //Initialization of variables after page loading is completed window.onload = function() { } //Initialization of 3D scene function createScene() { } //Initialization of WebXR settings async function initializeXR(){ } Lesson02
Initialization of Drawing Functions of Babylon.js
let canvas; //canvas element (drawing area) ← "where" to draw
let engine; //Drawing functionality with Babylon.js ← "What" to draw with
let scene; //Virtual 3D space ← "What" to draw
//Initialization of variables after page loading is completed
window.onload = function() {
//Obtain the drawing area (canvas element) from HTML
canvas = document.getElementById("renderCanvas");
//Preparation for drawing with Babylon.js (Arguments: destination, anti-aliasing)
engine = new BABYLON.Engine(canvas, true);
//カメラやライトの設定を⾏ったscene(3D空間)を取得
scene= createScene();
}
//Initialization of 3D scene
function createScene() {
}
Lesson03
Initialization of Scene (3D Space)
let canvas; //canvas element (drawing area) ← "where" to draw
let engine; //Drawing functionality with Babylon.js ← "What" to draw with
let scene; //Virtual 3D space ← "What" to draw
//Initialization of variables after page loading is completed
window.onload = function() {
//Obtain the drawing area (canvas element) from HTML
canvas = document.getElementById("renderCanvas");
//Preparation for drawing with Babylon.js (Arguments: destination, anti-aliasing)
engine = new BABYLON.Engine(canvas, true);
//Create a scene (3D space) with camera and light settings
createScene();
}
// Initialization of 3D scene
function createScene() {
}
Description of the initialization process of a scene (next page)
Lesson04
Initialization of Scene (3D Space)
function createScene() {
//Create 3D space
scene = new BABYLON.Scene(engine);
//Set background color
scene.clearColor = new BABYLON.Color3.Black();
//Set Light (arguments: name, direction of reflection, destination)
let light = new BABYLON.HemisphericLight("light",
new BABYLON.Vector3(1, 1, -0.5) ,scene);
//Create a camera[Details]
let camera = new BABYLON.ArcRotateCamera(
"camera",
//name of camea
-Math.PI/2, Math.PI/2, 3, //alpha,beta,distance form origin
new BABYLON.Vector3(0, 0, 0)); //look at
// Enables camera operation with mouse or keyboard
camera.attachControl(canvas, true);
}
Lesson05
Start Drawing
//Initialization of variables after page loading is completed
window.onload = function() {
//Obtain the drawing area (canvas element) from HTML
canvas = document.getElementById("renderCanvas");
//Preparation for drawing with Babylon.js (Arguments: destination, anti-aliasing)
engine = new BABYLON.Engine(canvas, true);
//Create a scene (3D space) with camera and light settings
createScene();
//Update drawing every frame
engine.runRenderLoop(function () {
if (scene && scene.activeCamera) {
scene.render(); //Drawing!
}
});
}
Lesson06
Operation Check Preview background becomes the specified color (=black)
Adding a Display Object
// Initialization of variables after page loading is completedう
window.onload = function() {
//Obtain the drawing area (canvas element) from HTML
canvas = document.getElementById("renderCanvas");
//Preparation for drawing with Babylon.js (Arguments: destination, anti-aliasing)
engine = new BABYLON.Engine(canvas, true);
//Create a scene (3D space) with camera and light settings
createScene();
//Creating Drawing Objects
addObjects();
//Update drawing every frame
engine.runRenderLoop(function () {
if (scene && scene.activeCamera) {
scene.render();
}
});
Lesson07
}
Adding a Display Object Edit here next
Adding a Display Object
function addObjects(){
}
//Create ground (for visual checking the location of the origin.)
let ground = BABYLON.MeshBuilder.CreateGround(
"ground", {width: 1, height: 1});
//Creation of Box object (30cm)
let box = BABYLON.MeshBuilder.CreateBox(
"box", {width: 0.3, height: 0.3, depth: 0.3});
box.position.x = 0.0;
box.position.y = 1; // 1m above the ground
//Create Material
let boxMaterial = new BABYLON.StandardMaterial("material");
//Set the color of the box (in this case randomly changing) and apply it to the box
boxMaterial.diffuseColor = BABYLON.Color3.Random();
box.material = boxMaterial;
//Create an octahedron
let octa = BABYLON.MeshBuilder.CreatePolyhedron("octa", {type:1,size: 0.15});
octa.position.x=0.5;
octa.position.y=1;
Lesson08
Operation Check Box and octahedron is shown
Adding a Display Object Support for mouse and keyboard operation with one line of camera.attachControl(canvas, true);. Initial Viewpoint Left-click and drag to rotate Scroll back and forward Right-click and drag to move vertical/horizontal
Exercise Procedure Web browser on PC Creation of 3D space HoloLens/Quest XR Mode Interaction by MRTK Visualize Hands
Experience the Content on Quest or HoloLens2 window.onload = function() { /* Omitted */ //Creating Drawing Objects addObjects(); //Initialization of WebXR settings initializeXR(); } //Update drawing every frame engine.runRenderLoop(function () { if (scene && scene.activeCamera) { scene.render(); } }); //Initialization of WebXR settings async function initializeXR(){ let xr = await scene.createDefaultXRExperienceAsync({ /*options*/ }); } Lesson09
Change the URL Before Testing on a Device Click Settings
Change the URL Before Testing on a Device Edit project details Change the name to something easy to remember Save
Check URL of Your XR Content Click Copy Link
Check URL of Your XR Content Check the URL by pasting it into a text editor, etc. https://your-content.glitch.me/
Operation Check HoloLens2 Input the URL Goggle Button Quest Input the URL Goggle Button
Operation Check
How to Exit HoloLens2 Home Button Quest Close Button Exit content via Home window shown by wrist tap (HoloLens2) or Oculus button (Quest)
Exercise Procedure Web browser on PC Creation of 3D space HoloLens/Quest XR Mode Interaction by MRTK Visualize Hands
Manipulate Objects (1)
function addObjects(){
/*Omitted*/
// Set box color randomly
let boxMaterial = new BABYLON.StandardMaterial("material");
boxMaterial.diffuseColor = BABYLON.Color3.Random();
box.material = boxMaterial;
//Create an octahedron
let octa = BABYLON.MeshBuilder.CreatePolyhedron("octa", {type:1,size: 0.15});
octa.position.x=0.5;
octa.position.y=1;
//Add manipulatable attribute to the box object
let sixDofDragBehavior = new BABYLON.SixDofDragBehavior();
sixDofDragBehavior.allowMultiPointer = false;
sixDofDragBehavior.rotateWithMotionController = false;
box.addBehavior(sixDofDragBehavior);
}
Lesson10
Manipulate Objects (1)
Operation Check Reload Reload Always click the Reload button before testing
Operation Check For Quest users, this content is currently only available for controllers
Manipulate Objects (2)
function addObjects(){
/*Omitted*/
let boxMaterial = new BABYLON.StandardMaterial("material");
boxMaterial.diffuseColor = BABYLON.Color3.Random();
box.material = boxMaterial;
//Create an octahedron
let octa = BABYLON.MeshBuilder.CreatePolyhedron("octa", {type:1,size: 0.15});
octa.position.x=0.5;
octa.position.y=1;
//Add manipulatable attribute to the box object
let sixDofDragBehavior = new BABYLON.SixDofDragBehavior();
sixDofDragBehavior.allowMultiPointer=false;
sixDofDragBehavior.rotateWithMotionController=false;
box.addBehavior(sixDofDragBehavior);
}
Code for manipulation with bounding boxes (next page)
Manipulate Objects (2) //Add manipulatable attribute to the box object let sixDofDragBehavior = new BABYLON.SixDofDragBehavior(); sixDofDragBehavior.allowMultiPointer=false; sixDofDragBehavior.rotateWithMotionController=false; box.addBehavior(sixDofDragBehavior); //Manipulation with bounding box var gizmoManager = new BABYLON.GizmoManager(scene); gizmoManager.boundingBoxGizmoEnabled = true; gizmoManager.boundingBoxDragBehavior.allowMultiPointer=false; //Scaling settings (enable scaling, synchronize x-y-z scaling) gizmoManager.gizmos.boundingBoxGizmo.setEnabledScaling(true,true); //Setting the appearance of the Bounding box gizmoManager.gizmos.boundingBoxGizmo.scaleBoxSize=0.04; gizmoManager.gizmos.boundingBoxGizmo.rotationSphereSize=0.05; //⼋⾯体オブジェクトを登録 gizmoManager.attachableMeshes = [octa]; Lesson11
Operation Check on PC
Operation Check on XR Device Reload Reload Always click the Reload button before testing
Operation Check on XR Device For Quest users, this content is currently only available for controllers
Exercise Procedure Web browser on PC Creation of 3D space HoloLens/Quest XR Mode Interaction by MRTK Visualize Hands
Turn On Hand Tracking (For Quest)
//Initialization of WebXR settings
async function initializeXR() {
let xr = await scene.createDefaultXRExperienceAsync({});
// Check if XR mode available or not
if (!xr.baseExperience) { }
else {
//Turn on hand tracking if XR mode available
xr.baseExperience.featuresManager.enableFeature(
BABYLON.WebXRFeatureName.HAND_TRACKING,
"latest",
{xrInput: xr.input}
);
}
}
Lesson12
Operation Check on XR Device
Completed!
References Camera https://doc.babylonjs.com/divingDeeper/cameras/camera_introduction WebXR Experience Helper https://doc.babylonjs.com/divingDeeper/webXR/webXRExperienceHelpers#webxr-defaultexperience-helper MRTK https://doc.babylonjs.com/divingDeeper/gui/mrtk SixDofDragBehaviour https://doc.babylonjs.com/typedoc/classes/BABYLON.SixDofDragBehavior GizmoManager https://doc.babylonjs.com/typedoc/classes/BABYLON.GizmoManager Gizmos https://doc.babylonjs.com/divingDeeper/mesh/gizmo Hand Tracking https://doc.babylonjs.com/divingDeeper/webXR/WebXRSelectedFeatures#hand-tracking