在火花环境中获取变形对象

时间:2019-05-30 08:48:57

标签: javascript spark-ar-studio

我试图通过JS获取变形对象以更改其属性,但是我什至无法通过任何Spark模块获取它。

Spark AR的样本项目具有面部变形变形。 https://developers.facebook.com/docs/ar-studio/tutorials-and-samples/samples/face-distortion/ 您甚至可以在教程中看到,有一个附加的变形对象,称为faceDistortionPack。该对象位于资产中,我尝试通过脚本中的其他方式来获取它,但是无法实现。我想写一些js逻辑来操纵变形。

const Scene = require('Scene');
const Diagnostics = require('Diagnostics');

const faceMesh = Scene.root.find('facemesh_distortion');

Diagnostics.log(faceMesh); // FaceMesh: https://sparkar.facebook.com/ar-studio/learn/documentation/reference/classes/scenemodule.facemesh
Diagnostics.log(faceMesh.deformation); // null
Diagnostics.log(faceMesh.find('faceDistortionPack')); // Exception...
// ....

我想获取'faceDistortionPack'对象以访问其属性,例如'nose_z',因此我可以通过JS进行更改。

2 个答案:

答案 0 :(得分:1)

虽然这是一个很老的问题,但我想我还是会回答,如果有人对此感到挣扎并遇到这个线程。

首先:有大量有用的提示、教程、片段等,称为Spark AR Community。在那里,您可以找到一个 GitBook,其中包含官方脚本对象引用的替代、更好的索引和更好的工作版本。如果您在官方参考中迷失了或者它经常发生的不起作用,我建议使用它。在那里您可以看到,上一个答案中提到的 BlendShapesMesh 是 deprecated as of Spark AR v85+,因此对任何人都没有帮助。 ()

所以,如果我理解正确的话,您想要实现的是访问 faceMesh 的 Blendshapes 并通过脚本更改它们的值。你需要做的是:

  1. 按照本教程中的说明进行操作:https://sparkar.facebook.com/ar-studio/learn/tutorials/face-distortion-retouching/
  2. 应用 Blendshapes 后,稍微调整一下 Blendshapes,以便了解发生了什么。
  3. 向您的资产添加脚本。您将要通过脚本访问 faceMesh-Object 的 Blendshapes 的权重属性。这可以通过使用 Mesh-Class 的 getBlendshapes()-Method 来完成。 这是一个代码示例:
//Require Modules
const Scene = require('Scene');
const Diagnostics = require('Diagnostics');
const Reactive = require('Reactive');

//Enable async/await [part 1]
(async function () {

  //Load Assets as Promise

const loadAssets = await Promise.all(
  [
      Scene.root.findFirst("faceMesh0"),  //Put name of your facemesh here, default is faceMesh0
  ]
).then(function(assets){
    const faceMesh = assets[0]; //Assign the facemesh from the assets-Array of the promise to a variable you can work with better
    const faceMeshShapes = faceMesh.getBlendShapes(); //Access all Blendshapes of the faceMesh-Object and store them in an Array

您现在拥有的是一个名为 faceMeshShapes 的变量,它是一个包含 Blenshapes 数组的对象。您可以使用 Diagnostics.log(faceMeshShapes) 控制台记录它,并看到其中有一个名为“_value”的数组,其中填充了所有 Blendshapes 作为对象,具有 weight-Property,它指定了 Blendshape 的权重scalarValue。您可以通过使用 scalarValue-Class 的 pinLastValue()-Method 转换它来控制台记录此值,您可以通过将其绑定到自定义值来分配不同的值,您可以使用 val()-Method 将其转换为 scalarValue反应式模块。 举个例子:

Diagnostics.log(faceMeshShapes._value[0].weight.pinLastValue()); //console log the Value of the weight property's scalarValue of the Blendshape that's stored at the index 0 of the _value-Array
faceMeshShapes._value[0].weight = Reactive.val(0.5) //set the weight to 0.5
Diagnostics.log(faceMeshShapes._value[0].weight.pinLastValue()); //console log the changed value of the weight property

所以基本上,这就是您可以访问每个 Blendshape 的重量的方法。索引应根据它们在 spar AR studio 中列出的顺序,从第一个 BlendShape 的 0 开始。现在你可以用这个值做很多事情,比如将它绑定到一个从 0 到 1 的动画,或者使用面部跟踪模块绑定到张开嘴等等。

最后但并非最不重要的一点,不要忘记任何分号并关闭所有括号。

  });

})(); // Enable async/await [part 2]

P.S.:有时通过控制台记录对象所有属性的列表,它确实有助于了解正在发生的事情以及如何访问内容。特别是因为 spark AR 文档在这方面非常薄弱(并且总体上非常薄弱)。您可以使用 MDN 中的以下函数来执行此操作。这没什么特别的,但它可以完成工作并且已经救了我几次:)

function listAllProperties(o) {
  var objectToInspect;
  var result = [];

  for(objectToInspect = o; objectToInspect !== null;
          objectToInspect = Object.getPrototypeOf(objectToInspect)) {
        result = result.concat(
          Object.getOwnPropertyNames(objectToInspect)
        );
    }
  return result;
}

答案 1 :(得分:0)

您发布的链接不再有效。

我想以下链接现在对于其他关注此链接的人来说更相关。我制作了一系列blendShapes动画,这些链接很有用。基本上循环遍历每种blendShape的权重。

https://sparkar.facebook.com/ar-studio/learn/documentation/reference/classes/scenemodule.blendshapesmesh

https://sparkar.facebook.com/ar-studio/learn/documentation/reference/classes/scenemodule.blendshape