Spark AR中如何获取Deformation对象并通过JS修改其属性?
How to Access and Modify Morph (Deformation) Objects in Spark AR with JavaScript
I've run into this exact issue before when working with Spark AR's face distortion packs! The key mistake here is assuming the faceDistortionPack is a child of the FaceMesh or accessible via a direct deformation property—instead, morph objects live in the FaceMesh's morphers array.
Spark AR stores all morph targets applied to a FaceMesh in the morphers collection. Your facemesh_distortion FaceMesh does have the faceDistortionPack attached to it, but you need to pull it from that array instead of using find() directly on the FaceMesh.
Corrected Code Example
Here's how to properly retrieve the morph object and modify properties like nose_z:
const Scene = require('Scene'); const Diagnostics = require('Diagnostics'); // Wait for the FaceMesh to load fully to avoid null references (async function () { const faceMesh = await Scene.root.findFirst('facemesh_distortion'); // Locate the faceDistortionPack in the morphers array by name const distortionPack = faceMesh.morphers.find(morph => morph.name === 'faceDistortionPack'); if (distortionPack) { Diagnostics.log('Successfully found distortion pack: ' + distortionPack.name); // Set a static value for the nose_z property (typical range: 0 to 1) distortionPack.setValue('nose_z', 0.7); // For dynamic control (e.g., binding to a UI slider), use this instead: // const slider = await Scene.root.findFirst('distortionSlider'); // distortionPack.inputs.set('nose_z', slider.value); } else { Diagnostics.log('Failed to locate faceDistortionPack morph target!'); } })();
Why Your Original Code Failed
faceMesh.deformationreturns null because this property is designed for mesh deformation modifiers, not pre-built morph target packs like the face distortion assets.faceMesh.find('faceDistortionPack')throws an error becausefind()searches for direct child scene objects, and morphers aren't stored as children—they're a separate, attached collection on the FaceMesh itself.
Key Notes
- Always use
async/awaitor promises when accessing scene objects to ensure they're fully loaded before interaction. - Most morph target property values range from 0 (no distortion) to 1 (full effect), but you can test values outside this range for extreme distortions if needed.
内容的提问来源于stack exchange,提问作者tv1st
相关产品推荐
相关产品推荐

