获取Spark AR studio中的像素屏幕尺寸(适用于Facebook)

时间:2019-01-04 17:06:58

标签: javascript facebook augmented-reality spark-ar-studio

我开始使用Spark AR studio,我正在寻找以像素为单位的屏幕尺寸,以比较gesture.location在Tap上获得的坐标。

TouchGestures.onTap().subscribe((gesture) => {
  // ! The location is always specified in the screen coordinates
  Diagnostics.log(`Screen touch in pixel = { x:${gesture.location.x}, y: ${gesture.location.y} }`);

  // ????
});

gesture.location以像素(屏幕坐标)为单位,想将其与屏幕尺寸进行比较,以确定触摸屏幕的哪一侧。

也许使用Camera.focalPlane是个好主意...

更新

我尝试了两种新方法来设置屏幕大小:

const CameraInfo = require('CameraInfo');
Diagnostics.log(CameraInfo.previewSize.height.pinLastValue());

const focalPlane = Scene.root.find('Camera').focalPlane;
Diagnostics.log(focalPlane.height.pinLastValue());

但是两者都返回0

5 个答案:

答案 0 :(得分:1)

从“场景”部分将其拖动到补丁编辑器后,可以通过“设备信息”补丁输出获得屏幕尺寸。 Device Info patch

答案 1 :(得分:1)

这个答案可能有点晚了,但是对于那些寻找可以在脚本中轻松使用值的解决方案的人来说,这可能是一个很好的补充,我遇到了这段代码(不是我的,忘记保存链接): / p>

var screen_height = 0;
Scene.root.find('screenCanvas').bounds.height.monitor({fireOnInitialValue: true}).subscribe(function (height) {
    screen_height = height.newValue;
});
var screen_width = 0;
Scene.root.find('screenCanvas').bounds.width.monitor({fireOnInitialValue: true}).subscribe(function (width) {
    screen_width = width.newValue;
});

这对我来说效果很好,因为我不知道如何将Diagnostics.log与数据一起使用,而不是Diagnostics.watch。

答案 2 :(得分:0)

最后,

在路径编辑器中使用设备信息,并将其通过脚本传递!

首先,在编辑器中添加一个变量“ to script”:

enter image description here

然后,在补丁编辑器中创建它:

enter image description here

您可以使用以下脚本进行抓取:

const Patches = require('Patches');
const screenSize = Patches.getPoint2DValue('screenSize');

我的错误是使用Diagnostic.log()检查我的变量是否运行良好。 改用Diagnostic.watch()

Diagnostic.watch('screenSize.x', screenSize.x);
Diagnostic.watch('screenSize.y', screenSize.y);

答案 3 :(得分:0)

现在,在开放测试版中(如本文所述),您可以将Device从场景侧栏中拖动到补丁编辑器中,以获取一个补丁,该补丁输出屏幕尺寸,屏幕比例,安全区域插入物以及自身对象。 The Device patch

答案 4 :(得分:0)

可以在分别使用CameraInfo.previewSize.widthCameraInfo.previewSize.height的脚本中使用设备大小。例如,如果您想在屏幕上获得代表最小/最大点的2d点,那么就可以解决问题。

const CameraInfo = require('CameraInfo')
const Reactive = require('Reactive')

const min = Reactive.point2d(
  Reactive.val(0),
  Reactive.val(0)
)
const max = Reactive.point2d(
  CameraInfo.previewSize.width,
  CameraInfo.previewSize.height
)

(我想强调的一点是CameraInfo.previewSize.widthCameraInfo.previewSize.heightScalarSignal,而不是数字文字。)

编辑:这是文档的链接:https://sparkar.facebook.com/ar-studio/learn/documentation/reference/classes/camerainfomodule