在 arcore 中的 3d 对象上使用模板蒙版时出现问题

Problem using stencil mask on 3d object in arcore

我正在使用 hello ar java 演示在 3d 对象上使用模板蒙版,但是我 运行 遇到了一些意外行为。我的模板遮罩正确地遮挡了平面渲染器,但 3d 对象 (andy) 似乎没有预期的反应。相反,他似乎被翻转了,如图所示。我不确定如何解决这个问题。附件是执行模板掩码的代码片段

模板图像在平面缓冲区上正确工作但在 3d 模型上失败

  GLES20.glClear ( GLES20.GL_STENCIL_BUFFER_BIT );
  GLES20.glEnable(GLES20.GL_STENCIL_TEST);
  GLES20.glColorMask(false, false, false, false);
  GLES20.glDepthMask(false);
  GLES20.glStencilFunc(GLES20.GL_NEVER, 1, 0xFF);
  GLES20.glStencilOp(GLES20.GL_REPLACE, GLES20.GL_KEEP, GLES20.GL_KEEP);
  GLES20.glStencilMask(0xFF);
  GLES20.glClear(GLES20.GL_STENCIL_BUFFER_BIT);

  // controls how pixels are rendered in the stencil mask
  quadDrawer.draw();

  GLES20.glColorMask(true, true, true, true);
  GLES20.glDepthMask(true);
  GLES20.glStencilMask(0xFF);
  GLES20.glStencilFunc(GLES20.GL_EQUAL, 0, 0xFF);

  // Visualize planes.
  // reacts correctly to the stencil mask
  planeRenderer.drawPlanes(
      session.getAllTrackables(Plane.class), camera.getDisplayOrientedPose(), projmtx);

  // Visualize anchors created by touch.
  float scaleFactor = 1.0f;

  for (ColoredAnchor coloredAnchor : anchors) {
    if (coloredAnchor.anchor.getTrackingState() != TrackingState.TRACKING) {
      continue;
    }
    // Get the current pose of an Anchor in world space. The Anchor pose is updated
    // during calls to session.update() as ARCore refines its estimate of the world.
    coloredAnchor.anchor.getPose().toMatrix(anchorMatrix, 0);

    // Update and draw the model and its shadow.
    // does not react correctly to the 
    virtualObject.updateModelMatrix(anchorMatrix, scaleFactor);
    virtualObjectShadow.updateModelMatrix(anchorMatrix, scaleFactor);
    virtualObject.draw(viewmtx, projmtx, colorCorrectionRgba, coloredAnchor.color);
    virtualObjectShadow.draw(viewmtx, projmtx, colorCorrectionRgba, coloredAnchor.color);
  }

  GLES20.glDisable(GLES20.GL_STENCIL_TEST);

忘记更新我为此找到的"solution"。我没有在 3d 对象上应用模板蒙版,而是再次渲染背景,但将其传递通过模板蒙版。这意味着背景将覆盖 3d 对象,从而实现所需的 "masking" 效果。