three.js - AFrame:如何将相机渲染到纹理

标签 three.js aframe webvr

我正在尝试使用第二台摄像机来显示 AFrame 场景的“从天空” View 。我已经学会了如何使用 2D Canvas 进行渲染,遵循 this example :

但我想知道这是否可以在不使用外部 <div> 的情况下完成,比如直接渲染到某些 Assets ,或者可能直接渲染到纹理......

我当前的代码是:

<html>
  <head>
    <script src="//aframe.io/releases/0.8.2/aframe.min.js"></script>

    <script>
// Original code:
//   https://wirewhiz.com/how-to-use-a-cameras-output-as-a-texture-in-aframe/
//
AFRAME.registerComponent('view',{
    'schema': {
       canvas: {
            type: 'string',
            default: ''
       },
       // desired FPS
       fps: {
            type: 'number',
            default: 90.0
       }
    },
    'init': function() {
        var targetEl = document.querySelector(this.data.canvas);
        this.counter = 0;
        this.renderer = new THREE.WebGLRenderer( { antialias: true } );
        this.renderer.setPixelRatio( window.devicePixelRatio );
        this.renderer.setSize( targetEl.offsetWidth, targetEl.offsetHeight );
        // creates spectator canvas
        targetEl.appendChild(this.renderer.domElement);
        this.renderer.domElement.id = "canvas";
        this.renderer.domElement.crossorigin="anonymous"
        this.renderer.domElement.height=300;
        this.renderer.domElement.width=400;
        this.el.removeAttribute('look-controls');
        this.el.removeAttribute('wasd-controls');
        console.log(this.renderer.domElement);
        console.log(document.querySelector('a-scene'))
    },
    'tick': function(time, timeDelta) {
        var loopFPS = 1000.0 / timeDelta;
        var hmdIsXFasterThanDesiredFPS = loopFPS / this.data.fps;
        var renderEveryNthFrame = Math.round(hmdIsXFasterThanDesiredFPS);
        if(this.counter % renderEveryNthFrame === 0){
            this.render(timeDelta);
            }
        this.counter += 1;
    },
    'render': function(){
        this.renderer.render( this.el.sceneEl.object3D , this.el.object3DMap.camera );
    }
});
    </script>
  <body>
    <a-scene physics="debug: true">

      <a-plane static-body position="0 0 -4" rotation="-90 0 0" width="30" height="40"
               color="yellow"></a-plane>

      <a-box color="red" position="0 2 0" depth="8" width="8"></a-box>

      <a-entity id="secondaryCamera" position="0 40 0" rotation="-90 0 0">
        <a-camera view="canvas:#spectatorDiv;" active="false">
        </a-camera>
      </a-entity>

      <a-entity position="0 0 10" look-controls>
        <a-entity camera position="0 1.6 0" wasd-controls>
          <a-entity geometry="primitive:plane; width:.2; height:.2" material="src:#canvas; opacity: .6"
                    position="0.2 -0.3 -0.7" rotation="0 -10 0"></a-entity>
          <a-cylinder radius="2" color="green"></a-box>
        </a-entity>
      </a-entity>

    </a-scene>

    <div style="height:300px; width:400px;" id='spectatorDiv'></div>

  </body>
</html>

最佳答案

查看 screenshot component implementation 。渲染到渲染目标,然后对像素数据执行任何您想要的操作(将其用作 WebGL 内的纹理,复制到 Canvas ...)

关于three.js - AFrame:如何将相机渲染到纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53905525/

相关文章:

iOS:使用 Three.js 开发的 HTML5

javascript - 使用自定义间隔将 Three.Js 场景渲染为动画 GIF 文件

Three.js,在网格之间共享ShaderMaterial但具有不同的统一集

javascript - 为什么 Aframe 的依赖属性不起作用?

dom - 在 AFrame 中动态添加新实体

javascript - THREE.js 自定义 VR/纸板效果

javascript - A 帧动画框在特定区域随机移动

ios - iOS/Safari 上的 HLS 视频流

javascript - WebVR 不适用于最新版本的 Firefox

javascript - A型框架运动机身相机高度问题