【问题标题】:How to add aframe a webcam background and shadows on this background?如何在此背景上添加网络摄像头背景和阴影?
【发布时间】:2020-11-25 10:09:55
【问题描述】:

我想让用户网络摄像头作为简单的背景(== 没有跟踪、标记...) 使用以下代码,我得到了它,但网络摄像头背景上没有阴影。 我想我不应该使用 aframe-ar.js,我可以看到这个示例显示了映射在立方体上的网络摄像头: webcam as aframe texture

但是如何将网络摄像头作为简单的背景?然后如何让这个背景上的阴影可见?

<html>
  <head>
    <script src="https://aframe.io/releases/0.8.2/aframe.min.js"></script><!-- Master file for aframe (== a-scene) -->
    <script src="https://raw.githack.com/AR-js-org/AR.js/master/aframe/build/aframe-ar.js"></script><!-- display webcam -->    
    <script src="../js/play-all-model-animations.js"></script><!-- play animation -->
    <script src="../js/aframe-orbit-controls.min.js"></script><!-- orbit cam target -->
    <script src="../js/aframe-extras.js"></script><!-- pour cub-env-map -->
  </head>
  <body>
<a-scene vr-mode-ui="enabled: false" embedded>
     <a-assets>
     <a-asset-item id="toy" src="../assets/toyDrummerSolo-03.glb"></a-asset-item>
     </a-assets>
     <a-entity gltf-model="../assets/toyDrummerSolo-03.glb" play-all-model-animations="" shadow="" id="gltf">
     </a-entity>
     <a-entity camera look-controls  orbit-controls="target: 0 1 0; minDistance: 0.5; maxDistance: 60; initialPosition: 0 5 5"></a-entity>
     <a-entity id="keylight" light="type:point;intensity:2;castShadow:true;shadowCameraFar:100;shadowBias:-0.001;shadowMapHeight:2048;shadowMapWidth:2048;" position="2.40512 2.77634 2.73113"></a-entity>
     <a-entity id="ambient" light="type:hemisphere;color:#e5fdff;groundColor:#ffffff;intensity:0.5" visible=""></a-entity>
</a-scene>
<!-- no shadow on the webcam background -->
</body>
</html>

【问题讨论】:

    标签: background webcam aframe shadow


    【解决方案1】:

    这里有几个问题,不知道哪一个解决了,所以让我解决一下标题:

    1.非框架背景

    如果没有定义 &lt;a-sky&gt; 或背景 &lt;a-scene background=""&gt; - 场景下方的任何元素都将可见。 Like here:

    <img src="background-image">
    <a-scene>
      <a-box></a-box>
    </a-scene>
    

    1a。网络摄像头背景

    几乎和上面一样,但是作为一个 img 你需要一个视频元素:

    <video></video>
    // after the page is loaded:
    // navigator.mediaDevices.getUserMedia({video: true})
    //                       .then(stream => {
    //                               video.srcObject = stream;
    //                               video.play();
    //                       })
    

    调整大小以使场景与视频相匹配可能会很棘手,但这不是问题。

    2。背景阴影

    由于相机背景没有物理表面来接收阴影 - 我们必须模仿它们。例如,阴影可以投射到不可见的&lt;a-plane&gt; 元素上。 Three.js 对此有一个特殊的材料:ShadowMaterial。一个简化的示例可能如下所示:

    <video>
    <a-scene>
      <a-box shadow>
      <a-plane rotation="-90 0 0" apply-shadowmaterial></a-plane>
    </a-scene>
    

    使用简单的自定义组件:

    AFRAME.registerComponent("apply-shadowmaterial", {
      init: function() {
        // grab the mesh
        let mesh = this.el.getObject3D("mesh");
        // keep the reference to the old material
        let tmp = mesh.material;
        // assign the new material
        mesh.material = new THREE.ShadowMaterial({ opacity: 0.6 });
        mesh.receiveShadow = true;
        // free memory
        tmp.dispose();
      }
    });
    

    检查一下here(草率地调整大小,但我认为这在这里不重要:))

    【讨论】:

    • 关于网络摄像头背景是否可以将视频“封装”在 中,因为我希望在加载页面时视频和 a-scene 一起开始?
    • @ÉricLoillieux 我不确定 ar.js 是如何做到的,我认为它不会将 &lt;video&gt; 封装在 &lt;a-scene&gt; 中。在用户允许使用相机之前,视频不会出现,因此您可以在场景准备好时初始化相机,并添加一个加载屏幕,直到流未准备好。
    • 好的,非常感谢您的帮助。我无法编写/阅读/理解任何 javascript,也许我应该学习...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2012-05-11
    • 1970-01-01
    • 2015-06-18
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多