簡體   English   中英

獲取 Spark AR Studio 中的像素屏幕尺寸(適用於 Facebook)

[英]Get the pixel screen size in Spark AR studio (for Facebook)

我開始與Spark AR 工作室合作,我正在尋找以像素為單位的屏幕尺寸,以比較通過gesture.location上的gesture.location獲得的坐標。

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

這個答案可能有點晚了,但對於尋找可以在腳本中輕松使用值的解決方案的人來說,這可能是一個很好的補充,我遇到了這個代碼(不是我的,忘記保存鏈接):

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。

將屏幕大小從場景部分拖到補丁編輯器后,可通過設備信息補丁輸出獲得屏幕尺寸。 設備信息補丁

最后,

在補丁編輯器中使用設備信息並將它們傳遞給腳本工作!

首先,在編輯器中添加一個變量“to script”:

在此處輸入圖片說明

然后,在補丁編輯器中創建它:

在此處輸入圖片說明

你可以用這個腳本抓住它:

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);

現在在公開測試版中(截至本文),您可以將 Device 從場景側邊欄拖到補丁編輯器中,以獲得一個輸出屏幕大小、屏幕比例和安全區域插入以及自對象的補丁。 設備補丁

設備大小可以分別使用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 s,而不是數字文字。)

編輯:這是文檔的鏈接: https : //sparkar.facebook.com/ar-studio/learn/documentation/reference/classes/camerainfomodule

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM