-
要访问世界变换位置,必须使用路径
plane4.worldTransform
.
-
您可以在Spark ar中订阅任何信号的更改。
例如:您可以订阅以下更改
plane4.worldTransform.x
因为这是
ScalarSignal
。但您不能订阅
plane4
或
plan4.worldTransform
因为它们不是信号而是简单地被认为是场景的对象。
您可以在此处了解有关每种Spark ar的更多信息
https://sparkar.facebook.com/ar-studio/learn/developer/reference/scripting/summary
下面是一个用于订阅plane4的世界X坐标的工作示例代码
plane4.worldTransform.x.monitor().subscribe(function (posX) {
Diagnostics.log(posX.newValue);
});
UPD:添加了一个检查许多对象位置的示例
Spark AR引擎的开发人员建议使用尽可能少的信号订阅,因为订阅会极大地影响性能,因此您需要熟练地使用Reactive模块并订阅复杂的复合信号。在这个例子中,我使用了2个对象,而不是9个。此外,我没有使用纯0,但有一些小偏差。
注意代码中的注释。
const Scene = require('Scene');
const Reactive = require('Reactive');
const Diagnostics = require('Diagnostics');
const planes = [];
const objectsCount = 2;
Promise.all([
Scene.root.findFirst('plane0'),
Scene.root.findFirst('plane1'),
]).then(function(result)
{
for(let i = 0; i < objectsCount; ++i)
{
planes.push(result[i]);
}
for(let i = 0; i < objectsCount; ++i)
{
Reactive.and(
Reactive.and(
planes[i].worldTransform.x.le(0.01),
planes[i].worldTransform.x.ge(-0.01)
),
Reactive.and(
planes[i].worldTransform.y.le(0.01),
planes[i].worldTransform.y.ge(-0.01)
)
)
.monitor().subscribe(function(isPosZero)
{
if(isPosZero.newValue)
{
Diagnostics.log("plane" + i.toString() + " pos is zero");
}
});
}
let myOrList = [];
for (let i = 0; i < objectsCount; ++i)
{
myOrList.push(
Reactive.and(
Reactive.and(
planes[i].worldTransform.x.le(0.01),
planes[i].worldTransform.x.ge(-0.01)
),
Reactive.and(
planes[i].worldTransform.y.le(0.01),
planes[i].worldTransform.y.ge(-0.01)
)
)
);
}
Reactive.orList(
myOrList
).monitor().subscribe(function(isPosZero)
{
Diagnostics.log('the position of one of the objects is {0,0}');
});
});