Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@douyin-microapp/microapp-ar-three

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@douyin-microapp/microapp-ar-three

降低外部开发者在字节小程序中使用 ThreeJS 对 AI/AR 模型进行渲染的难度。目前支持两种功能:

latest
npmnpm
Version
0.0.5
Version published
Weekly downloads
6
100%
Maintainers
2
Weekly downloads
 
Created
Source

microapp-ar-three

降低外部开发者在字节小程序中使用 ThreeJS 对 AI/AR 模型进行渲染的难度。目前支持两种功能:

  • 根据 AI/AR 算法数据对模型进行抠除(例如试鞋场景下从鞋子模型中抠掉脚腕部分)
  • 根据 AI/AR 算法数据调整模型的 position、scale 和 rotation

详细的使用方法介绍请参考字节跳动小程序开发文档 codelab 中的 microapp-ar-three 教程:https://microapp-codelabs.bytedance.com/

目录

使用方法

  • 通过 npm 安装
npm install @douyin-microapp/microapp-ar-three
  • 导入 ArMixer 和需要使用的 ArProcessor,以 ArFootProcessor 为例
import { ArMixer, ArFootProcessor } from '@douyin-microapp/microapp-ar-three'
  • @douyin-microapp/microapp-ar-three/libs 下的 three.js 是解决了兼容性的 THREE,可以直接在字节小程序中使用,通过调用 getThree() 传入 canvas 来调用:
import { getThree } from '@douyin-microapp/microapp-ar-three/libs/three.js'
function init(canvas) {
  const THREE = getThree(canvas)
}

ArMixer

ArMixer

ArMixer 构造函数,创建一个 ArMixer 对象。ArMixer 根据 arProcessors 的配置混合 cameraTexture、modelTexture 以及 maskTexture,并通过 mixedTexture 属性获取混合结果。

语法

const arMixer = new ArMixer(options)

参数说明

属性名类型默认值必填说明
threetype of THREE-当前使用的 THREE 本身
processorsArProcessor[]-arProcessor 数组
cameraTextureTHREE.Texture-保存了一帧相机画面数据的 THREE.Texture
modelTextureTHREE.Texture-绘制了所有模型的 THREE.Texture
mixedTextureWidthnumber-指定保存混合结果的 mixedTexture 的宽度
mixedTextureHeightnumber-指定保存混合结果的 mixedTexture 的高度

代码示例

const arMixer = new ArMixer({
      three: THREE,
      processors: [arProcessor0,...,arProcessorN],
      cameraTexture: cameraTexture,
      modelTexture: objectTarget.texture,
      mixedTextureWidth: width,
      mixedTextureHeight: height
    })

render

ArMixer 对象调用 render 进行一次混合,将结果渲染到 mixedTexture 上。

语法

arMixer.render(options)

参数说明

属性名类型默认值必填说明
rendererTHREE.Renderer-当前渲染使用的 renderer

代码示例

onFrame(renderer: THREE.WebGLRenderer, data: Image) {
    this.leftMaskTexture.needsUpdate = true;
    this.rightMaskTexture.needsUpdate = true;
    (this.plane.material as any).uniformsNeedUpdate = true;
    this.cameraTexture.needsUpdate = true;

    renderer.setSize(this.width, this.height);
    const savedTarget = renderer.getRenderTarget();
    renderer.setRenderTarget(this.objectTarget);
    renderer.render(this.objectScene, this.objectCamera);

    renderer.setRenderTarget(null);

    this.arMixer.render({
      renderer: renderer
    })

    renderer.render(this.scene, this.camera);
    renderer.setRenderTarget(savedTarget);
}

ArFootProcessor

ArFootProcessor

ArFootProcessor 的构造函数,创建一个 ArFootProcessor 对象。ArFootProcessor 负责根据算法数据更新足部模型信息。ArFootProcessor 的使用示例可参考字节跳动小程序 codelab

语法

const arFootProcessor = new ArFootProcessor(options)

参数说明

options 为 Object 类型,属性如下:

属性名类型默认值必填说明
threetype of THREE-当前使用的 THREE 本身

代码示例

const arFootProcessor = new ArFootProcessor({
  three: THREE,
})

AR Foot 的 demo 可通过下面的链接来获取,运行前需要修改一下模型获取地址,模型的朝向以及尺寸可参考 updateModels 以及 shoe_models 中的示例模型(本地测试的话可以使用 http-server)

updateModels

ArFootPrcesoor 类对象函数,根据算法数据对模型进行位移、旋转和缩放操作。

语法

arFootProcessor.updateModels(options)

参数说明

属性名类型默认值必填说明
algResultFootAlgResult(Amazing 返回的算法数据)-Amazing 算法接口返回的算法数据
modelsObject : {left: THREE.Object3D right: THREE.Object3D}-双脚的模型

模型朝向请参照代码示例中的模型进行调整,bounding box 的尺寸如下(Xcode 下):

Left Shoe

widthheightdepth
7.71717.9988.21

Right Shoe

widthheightdepth
7.72917.858.173

代码示例

onAlgorithmResult(algRes) {
    arFootProcessor.updateModels({
        algResult: algRes,
        models: {
            left: leftShoeModel,
            right: rightShoeModel
        }
    })
}

ArNailProcessor

ArNailProcessor

ArNailProcessor 的构造函数,创建一个 ArNailProcessor 对象,它负责根据算法数据更新指甲模型信息。ArNailProcessor 的示例教程可以参考字节跳动小程序 codelab

语法

const arNailProcessor = new ArNailProcessor(options)

参数说明

options 为 Object 类型,属性如下:

属性名类型默认值必填说明
threetype of THREE-当前使用的 THREE 本身

代码示例

const arNailProcessor = new ArNailProcessor({ three: THREE })

updateModels

ArNailProcessor 类对象函数,根据算法数据改变指甲模型位置,使其跟随相机中的指甲位置。

语法

arNailProcessor.updateModels(options)

参数说明

属性名类型默认值必填说明
algResultNailAlgResult(Amazing 返回的指甲算法数据)-Amazing 算法接口返回的指甲算法数据
modelsTHREE.Group-包含了五个指甲模型的 Group
cameraWidthnumber-相机画面宽度
cameraHeightnumber-相机画面高度

代码示例

function onAlgorithmResult(algorithmResult) {
  arNailProcessor.updateModels({
    algResult: algorithmResult,
    models: nailModelGroup,
    cameraWidth: canvas_width,
    cameraHeight: canvas_height,
  })
}

ArNailUtils

目前的指甲算法数据不支持 3 维模型的移动,因此目前只支持更换指甲模型的纹理,不支持更换指甲模型。ArNailUtils 提供了 getNailModelGroupByColor 和 getNailModelGroupByTexture 两种类方法来返回指甲模型。

getNailModelGroupByColor

getNailModelGroupByColor 接受一个颜色参数,可以是 string,number 或者 THREE.Color 类型,返回一个带有默认高光贴图的模型,类型为 THREE.Group,包含五个指甲的模型。

语法

let nailModelGroup = ArNailUtils.getNailModelGroupByColor(options)

参数说明

options 为 Object 类型,属性如下:

属性名类型默认值必填说明
THREEtype of THREE-当前使用的 THREE 本身
rendererTHREE.Renderer-当前渲染使用的 renderer
colorTHREE.Color | string | number-当前指甲高光贴图的背景颜色

代码示例

let nailModelGroup = ArNailUtils.getNailModelGroupByColor({
  THREE,
  renderer,
  color: 0x00ff00,
})

nailModelGroup = ArNailUtils.getNailModelGroupByColor({
  THREE,
  renderer,
  color: 'green',
})

nailModelGroup = ArNailUtils.getNailModelGroupByColor({
  THREE,
  renderer,
  color: new THREE.Color('red'),
})

getNailModelGroupByTexture

getNailModelGroupByTexture 接受一个 THREE.Texture 类型的纹理对象,返回一个带有默认高光贴图的模型,类型为 THREE.Group,包含五个指甲的模型。

语法

const nailModelGroup = ArNailUtils.getNailModelGroupByTexture(options)

参数说明

options 为 Object 类型,属性如下:

属性名类型默认值必填说明
THREEtype of THREE-当前使用的 THREE 本身
textureTHREE.Texture-当前指甲的高光贴图

代码示例

const nailModelGroup = ArNailUtils.getNailModelGroupByTexture({
  THREE,
  texture: highLightTexture,
})

Tip:

关于指甲纹理,getNailModelGroupByTexture 接口在生成模型时,为算法返回的 8 个坐标点都分配了对应的纹理坐标作为锚点。 实际上,能够显示出来的纹理要比由这 8 个点构成的多边形要小,为了使指甲边缘平滑,接口实现中的做法是计算上图这 8 个点构成的多边形的形心,并且将 8 个点沿着形心到这 8 个点的方向上移动,移动距离是形心到各点距离的 0.2 倍。详细用法可见 codelab。

FAQs

Package last updated on 15 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts