cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

How to get camera position on Hololens 2 ?

Lionel_Brly
11-Garnet

How to get camera position on Hololens 2 ?

Hello,

    I'm trying to get postion of my camera (On Hololens 2) with this code:

$scope.setVector= function() {
tml3dRenderer.setupTrackingEventsCommand (function(target,eyepos,eyedir,eyeup) {
$scope.app.params.eyepos = [eyepos[0].toFixed(2),eyepos[1].toFixed(2),eyepos[2].toFixed(2)];
$scope.app.params.eyedir = [eyedir[0].toFixed(2),eyedir[1].toFixed(2),eyedir[2].toFixed(2)];
$scope.app.params.eyeup = [eyeup[0].toFixed(2), eyeup[1].toFixed(2), eyeup[2].toFixed(2)];
})
};

$interval(function(){ $scope.setVector(); },100);

 

Obviously, the three values of "eyepos", "eyedir", "eyeup" are mapped with parametre which are linked to a "3DLabels" Widget to display values within my experience.

In the two tests, I'm using a "Thingmark" to activate my experiences.

And  I'm facing some issue, this code is working fine on "Mobile Project" but on "3D Eyewear"

 

Any ideas ?

3 REPLIES 3

Hi @Lionel_Brly ,

 

so far I remember in the post was metnioned the usage "How to get camera position?"

I have only the HoloLens1 but there is no principle difference from the studio side - so that eyewear project should work on both. I will check if I have such project as demo - so far I remember it was working fine when I tested it on the HoloLens 1 some week ago

What I do not think that is a good idea – is to call the setVector function in an interval. The tracking event has it own callback.

in the article "How can we make a 3D Widget on HoloLens visible in front of me every time ?"  I uploaded  an project which is handle  more complex but it used the requested functionality for tracking of the device postion - so you can check it 

Another info which could be helpful is this one : "https://community.ptc.com/t5/Vuforia-Studio/Finding-distance-from-the-model/m-p/647473#M7497"

In the both mentioned link I attached a project which should demonstrate the functionalit and it implies the requested functionality

In the last post , the more relevant information to your issue is this below:

2020-06-23_17-17-27.jpg

when I open a project it is always there / HoloLens and Mobile/

- the option 'Enable tracking Events' is threre

I checked my Vuforia Studio builder-settings - at the end I added the following options:

...
 "enableDebugLogging": true,
  "publishPreview": true,
  "welcomeDismissed": true,
  "designTimeDropShadows": "HARD",
  "projectsListView": true,
  "annotationsEnabled": true,
  "serverMTG": true,
  "showTrackingEvents": true
}

So belive the option "showTrackingEvents": true has relevance for the mention  display in the 3d container widget UI.  but may be in combination with some of the other options. 

the code :

 

function magnitute(a1,a2,a3)
{

return Math.sqrt( Math.pow(a1, 2) + Math.pow(a2, 2)+ Math.pow(a3, 2));
}

function cross(a1,a2,a3, b1,b2,b3) 
{
 return [ a2 * b3 - a3 * b2, a3 * b1 - a1 * b3, a1 * b2 - a2 * b1 ];
}
function normalizeVector(a1,a2,a3) {
    var mag = magnitute(a1,a2,a3)
    return new Vector(a1/mag, a2/mag, a3/mag);
};

/////////////////////////////////////
$rootScope.$on('modelLoaded', function() { 
 
    
   $scope.setWidgetProp('3DContainer-1','enabletrackingevents',true);
  console.log("now check again the setting of the envronment")
  console.warn($scope.app.view.Home.wdg['3DContainer-1'].enabletrackingevents)
  
  //// end of modelLoaded
});
//=============================================================
 $rootScope.$on('tracking', function( tracker,fargs  ) {
  
  console.warn(fargs.position);
 
   $scope.app.params['eyepos'] ="eyepos=("+fargs.position[0].toFixed(2)+","+fargs.position[1].toFixed(2)+","+fargs.position[2].toFixed(2)+")";
   $scope.app.params['eyedir'] ="eyedir=("+fargs.gaze[0].toFixed(2)+","+fargs.gaze[1].toFixed(2)+","+fargs.gaze[2].toFixed(2)+")";
   $scope.app.params['eyeup' ] ="eyeup =("+fargs.up[0].toFixed(2)+","+ fargs.up[1].toFixed(2)+","+fargs.up[2].toFixed(2)+")";
   var scale=1.3;
   $scope.setWidgetProp('3DImage-1',  'x', ( fargs.position[0]+fargs.gaze[0]*scale));
   $scope.setWidgetProp('3DImage-1',  'y', ( fargs.position[1]+fargs.gaze[1]*scale));
   $scope.setWidgetProp('3DImage-1',  'z', ( fargs.position[2]+fargs.gaze[2]*scale));
  //3DLabel-1 no y
   $scope.setWidgetProp('label-distance-x',  'x', ( fargs.position[0]+fargs.gaze[0]*scale)+0.0);
   $scope.setWidgetProp('label-distance-x',  'y', ( fargs.position[1]+fargs.gaze[1]*scale) -0.08*5);
   $scope.setWidgetProp('label-distance-x',  'z', ( fargs.position[2]+fargs.gaze[2]*scale));
   $scope.setWidgetProp('label-distance-y',  'x', ( fargs.position[0]+fargs.gaze[0]*scale)+0.0);
   $scope.setWidgetProp('label-distance-y',  'y', ( fargs.position[1]+fargs.gaze[1]*scale)-0.08*4);
   $scope.setWidgetProp('label-distance-y',  'z', ( fargs.position[2]+fargs.gaze[2]*scale));
   $scope.setWidgetProp('label-distance-z',  'x', ( fargs.position[0]+fargs.gaze[0]*scale)+0.0);
   $scope.setWidgetProp('label-distance-z',  'y', ( fargs.position[1]+fargs.gaze[1]*scale)-0.08*3);
   $scope.setWidgetProp('label-distance-z',  'z', ( fargs.position[2]+fargs.gaze[2]*scale));

   
   console.info("distance to 0,0,0 World CSYS = :: "+magnitute(fargs.position[0].toFixed(2),fargs.position[1].toFixed(2),fargs.position[2].toFixed(2)) )
  
 $scope.$applyAsync();
});
                            

 

In the parameter eyepos, eyedir and eyeup I created binding to the text property of the lables label-dinstance-x/y/z and are displayed there.

I attached the test project in the mentioned post

View solution in original post

Hello Thanks for your reply,

  This code is running veru well!

Lionel

 

$rootScope.$on('trackingacquired', function() {
$scope.getPosition()
console.log("trackingacquired");
console.log(tml3dRenderer);
console.log("tml3dRenderer logged");
});

$scope.getPosition= function() {
tml3dRenderer.setupTrackingEventsCommand (function(target,eyepos,eyedir,eyeup) {

$scope.app.params.eyepos ="eyepos=("+eyepos[0].toFixed(2)+","+eyepos[1].toFixed(2)+","+eyepos[2].toFixed(2)+")";
$scope.app.params.eyedir ="eyedir=("+eyedir[0].toFixed(2)+","+eyedir[1].toFixed(2)+","+eyedir[2].toFixed(2)+")";
$scope.app.params.eyeup ="eyeup =("+ eyeup[0].toFixed(2)+","+ eyeup[1].toFixed(2)+","+ eyeup[2].toFixed(2)+")";
$scope.$applyAsync();
$scope.view.wdg['3DLabel-1']['text']=$scope.app.params.eyepos; /* eyepos=xx.xx,xx.xx,xx.xx */
$scope.view.wdg['3DLabel-2']['text']=$scope.app.params.eyedir; /* eyedir=xx.xx,xx.xx,xx.xx */
$scope.view.wdg['3DLabel-3']['text']=$scope.app.params.eyeup; /* eyeuo=xx.xx,xx.xx,xx.xx */
})
}

Announcements

Top Tags