I am looking for a way to orient a sceneobject on a tracked plane towards the camera via scripting in SparkAR Studio 98. This sounds trivial, but I am having a very hard time finding the world position of the camera, and then from that generating a rotation that I can apply to the object looking at the camera.
I got it to work WITHIN the tracked plane hierarchy, so that one object would always look at the other, but as the camera is living outside of that hierarchy and coordinate system, it doesn't help me.
I am, as always, struggeling with the reactive nature of the SparkAR API, as well as the fact that things are spread out over several coordinate systems and information is hard to find on the net and quickly outdated. My code looks like this:
const Scene = require('Scene');
const R = require('Reactive');
export const Diagnostics = require('Diagnostics');
(async function () {
Diagnostics.log("Filter activated");
const objectToOrient = await Scene.root.findFirst('object');
const objectParent= await Scene.root.findFirst('objectParent');
const target = await Scene.root.findFirst('target');
const lookAtPt = R.point(
target.worldTransform.x,
target.worldTransform.y,
target.worldTransform.z);
const lookAtTransform = objectParent.transform.lookAt(lookAtPt);
})();
To illustrate the concept for the reader, I have set up this scene:
The "S" appears, as soon as the underlying plane is tracked. It then stays in place, along with the tracked plane. It should then always rotate towards the camera/device/user. Thank you!