You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用A-Frame激活AR时出现黑屏问题求助(Chrome Android)

问题:Chrome Android端A-Frame AR模式黑屏排查

我是一名具备一定编程基础的设计师,希望使用A-Frame实现无标记AR体验——在浏览器中查看Blender制作的动画模型,点击AR按钮切换到AR模式在现实场景中查看模型。参考文档编写代码后,在Chrome Android设备上激活AR时出现黑屏现象,附上代码及截图请求排查。

代码示例

<!DOCTYPE html>
<html>
    <head>
        <script src="https://aframe.io/releases/1.7.0/aframe.min.js"></script>
        <script src="https://unpkg.com/aframe-orbit-controls@1.3.2/dist/aframe-orbit-controls.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/aframe-extras@6.1.1/dist/aframe-extras.min.js"></script>
        <style>
            body, html { margin: 0; overflow: hidden; height: 100%; }
        </style>
    </head>
    <body>
        <a-scene
            webxr="mode: ar; requiredFeatures: hit-test;"
            xr-mode-ui="enabled: true; XRMode: ar;"
            vr-mode-ui="enabled: false"
            ar-hit-test="target:#vahan";
        >
        <a-assets>
        <a-asset-item id="vahanModel" src="/vahan_model.glb"></a-asset-item>
        </a-assets>

        <!-- Model entity with animation mixer -->
        <a-entity
        id="vahan"
        gltf-model="#vahanModel"
        position="0 0 0"
        scale="0.8 0.8 0.8"
        animation-mixer
        ></a-entity>

        <!-- Camera with orbit controls for desktop and non-AR mode -->
        <a-entity 
        camera 
        look-controls="enabled: false"
        orbit-controls="target: 0 0 0; enableZoom: true; enablePan: false; initialPosition: 0 1.6 3; rotateSpeed=2"
        ></a-entity>

        <!-- Basic lighting -->
        <a-entity light="type: ambient; intensity: 0.5"></a-entity>
        <a-entity light="type: directional; intensity: 0.8" position="1 2 1"></a-entity>

        <!-- Sky background -->
        <!-- <a-sky color="#ECECEC"></a-sky> -->
        </a-scene>
    </body>
</html>

故障截图

Chrome Android端激活AR后出现黑屏


排查与修复方案

1. 修正AR模式初始化配置

  • 原代码强制<a-scene>初始进入AR模式,设备未完成权限/环境初始化就触发AR,导致黑屏;同时xr-mode-ui属性名错误,ar-hit-test末尾多了分号。
  • 修复代码:
<a-scene
    webxr="mode: inline; requiredFeatures: hit-test;"
    xr-mode-ui="enabled: true; mode: ar;"
    vr-mode-ui="enabled: false"
    ar-hit-test="target:#vahan"
>

说明:初始用inline模式加载普通3D场景,通过AR按钮切换到AR模式,避免初始化冲突。

2. 解决相机控件冲突

  • 自定义相机的orbit-controls与AR模式下的系统相机冲突,且rotateSpeed=2语法错误(应使用冒号)。
  • 修复方案:分场景控制相机显示,仅在非AR模式启用桌面相机:
<!-- 桌面/非AR模式相机 -->
<a-entity 
    camera 
    look-controls="enabled: false"
    orbit-controls="target: 0 0 0; enableZoom: true; enablePan: false; initialPosition: 0 1.6 3; rotateSpeed: 2"
    visible="false"
    id="desktop-camera"
></a-entity>

<!-- 添加脚本监听模式切换 -->
<script>
  const scene = document.querySelector('a-scene');
  const desktopCam = document.getElementById('desktop-camera');
  // 初始显示桌面相机
  desktopCam.setAttribute('visible', true);
  // 切换到AR时隐藏桌面相机
  scene.addEventListener('enter-vr', evt => {
    desktopCam.setAttribute('visible', evt.detail.sessionMode !== 'immersive-ar');
  });
</script>

3. 优化模型加载与AR定位

  • 模型初始位置可能导致AR渲染异常,且需确保模型路径正确(/vahan_model.glb需匹配服务器实际路径)。
  • 修复:设置模型初始不可见,AR命中后显示:
<a-entity
    id="vahan"
    gltf-model="#vahanModel"
    position="0 0 0"
    scale="0.8 0.8 0.8"
    animation-mixer
    visible="false"
></a-entity>

添加事件监听显示模型:

scene.addEventListener('ar-hit-test-start', () => {
  document.getElementById('vahan').setAttribute('visible', true);
});

4. 确认权限与兼容性

  • 确保Chrome Android版本≥81,设备已授予相机权限;网站需使用HTTPS(本地开发可用localhost)。

内容的提问来源于stack exchange,提问作者Nilay Vaibhav Gomkale

火山引擎 最新活动