webrtc - 如何将网络摄像头源用作 A-Frame 纹理?
问题描述
我想将网络摄像头流作为纹理附加到aframe中的实体,这可能吗?我该怎么做?
我想要的效果的一个例子包括:
- 将我的网络摄像头信息投射到 vr 内的电视上
- VR中的“人脸计时”
- 出于调试目的在 VR 中看到自己
解决方案
https://media.giphy.com/media/cJjZg8kXSUopNzZP4V/giphy.gif
添加资产
第一步是将视频添加为资产:
<a-assets>
<video id="webcam" playsinline></video>
</a-assets>
请注意 playinline 指令,它阻止页面进入全屏模式,尤其是在移动设备上。这只是我想添加的一个小细节,因为尽管我们的应用程序将全屏运行,但我希望应用程序决定这一点,而不是一些随机的视频元素!
创建流
接下来,我们使用以下命令创建流:
<!-- Start the webcam stream and attach it to the video element -->
<script>
// You can also set which camera to use (front/back/etc)
navigator.mediaDevices.getUserMedia({audio: false, video: true})
.then(stream => {
let $video = document.querySelector('video')
$video.srcObject = stream
$video.onloadedmetadata = () => {
$video.play()
}
})
</script>
应用纹理
最后,我们将流作为材料应用到任何实体上:material="src: #webcam"
工作演示
<script src="https://aframe.io/releases/0.8.2/aframe.min.js"></script>
<!-- Create an empty video tag to hold our webcam stream -->
<a-assets>
<video id="webcam" playsinline></video>
</a-assets>
<!-- Creates -->
<a-scene background="color: #ECECEC">
<a-box position="-1 0.5 -3" rotation="0 45 0" shadow material="src: #webcam"></a-box>
<a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E" shadow></a-sphere>
<a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D" shadow></a-cylinder>
<a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4" shadow></a-plane>
</a-scene>
<!-- Start the webcam stream and attach it to the video element -->
<script>
// You can also set which camera to use (front/back/etc)
// @SEE https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
navigator.mediaDevices.getUserMedia({audio: false, video: true})
.then(stream => {
let $video = document.querySelector('video')
$video.srcObject = stream
$video.onloadedmetadata = () => {
$video.play()
}
})
</script>
如果 Code Runner 不起作用,您也可以在此处使用它:https ://glitch.com/~webcam-as-aframe-texture
推荐阅读
- python - 如何在 Python 程序中添加暂停模式
- r - 保存/加载可旋转配置
- python - 如何将亲笔签名的好处与训练循环结合起来?
- javascript - 使用自定义按钮将行数据从 DataTable 加载到 localStorage 时出错
- java - 使用 Gradle 5.4.1 签署 maven jar 导致无法设置未知属性“keyId”
- java - 为什么 NavigableSet JavaDoc 说明了实现细节?
- java - 使用android在聊天中指示消息状态
- android - Glide 不会加载所有图像(本地文件)(OutOfMemory)
- automated-tests - 在 cucumber/testCafe 中实现 requestHooks
- javascript - 有没有办法使用 aws-sdk 获取已删除 s3 对象的先前版本?