Babylon.js WebGPU Ocean Demo — 完整踩坑记录

换成军舰后的图片

源码运行后效果

最后代码正常启动

Babylon.js WebGPU Ocean Demo 本地运行踩坑全记录

环境

  • Chrome 145
  • Babylon.js 6.26.0
  • Windows 10

问题一:depth24unorm-stencil8 类型错误

报错:

复制代码
TS2322: Type '"depth24unorm-stencil8"' is not assignable to type 'GPUFeatureName'

原因: 该特性已从 WebGPU 规范移除。

修复: 直接删除这一行。


问题二:requestAdapterInfo is not a function

报错:

复制代码
TypeError: this._adapter.requestAdapterInfo is not a function

原因: Chrome 新版将 requestAdapterInfo() 改为 adapter.info 属性,Babylon.js 6.x 内部还在调用旧 API。

修复:index.ts 最顶部加 polyfill:

typescript

复制代码
if (typeof navigator !== "undefined" && navigator.gpu) {
    const origRequestAdapter = navigator.gpu.requestAdapter.bind(navigator.gpu);
    (navigator.gpu as any).requestAdapter = async (options?: GPURequestAdapterOptions) => {
        const adapter = await origRequestAdapter(options);
        if (adapter && typeof (adapter as any).requestAdapterInfo !== "function") {
            (adapter as any).requestAdapterInfo = async () => {
                return (adapter as any).info ?? {};
            };
        }
        return adapter;
    };
}
```

---

### 问题三:`onResizeObservable` 类型错误

**报错:**
```
TS2339: Property 'onResizeObservable' does not exist on type 'ThinEngine'

原因: onResizeObservableEngine 上,不在 ThinEngine 上。

修复: RTTDebug.ts 两处修改:

typescript

复制代码
// 1. 私有变量类型改为 any
private _engine: any;

// 2. 第178行加可选链
(this._engine as any).onResizeObservable?.add(this._resize.bind(this));
```

---

### 问题四:`WebGPUEngine` 与 `Engine` 类型不兼容

**报错:**
```
TS2740: Type 'WebGPUEngine' is missing the following properties from type 'Engine'

修复: index.ts 里 engine 类型直接用 any

typescript

复制代码
let engine: any;

最终完整 index.ts

typescript

复制代码
import * as BABYLON from "@babylonjs/core";
import { WebGPUEngine } from "@babylonjs/core/Engines/webgpuEngine";
import { Engine } from "@babylonjs/core/Engines/engine";
import { getSceneModuleWithName } from "./createScene";

// 修复 Chrome 新版移除了 requestAdapterInfo
if (typeof navigator !== "undefined" && navigator.gpu) {
    const origRequestAdapter = navigator.gpu.requestAdapter.bind(navigator.gpu);
    (navigator.gpu as any).requestAdapter = async (options?: GPURequestAdapterOptions) => {
        const adapter = await origRequestAdapter(options);
        if (adapter && typeof (adapter as any).requestAdapterInfo !== "function") {
            (adapter as any).requestAdapterInfo = async () => {
                return (adapter as any).info ?? {};
            };
        }
        return adapter;
    };
}

const getModuleToLoad = (): string | undefined => location.search.split("scene=")[1];

export const babylonInit = async (): Promise<void> => {
    const moduleName = getModuleToLoad();
    const createSceneModule = await getSceneModuleWithName(moduleName);

    (window as any).BABYLON = BABYLON;

    await Promise.all(createSceneModule.preTasks || []);
    const canvas = document.getElementById("renderCanvas") as HTMLCanvasElement;

    let engine: any;
    const webgpuSupported = await WebGPUEngine.IsSupportedAsync;

    if (webgpuSupported) {
        const webgpuEngine = new WebGPUEngine(canvas, {
            deviceDescriptor: {
                requiredFeatures: [
                    "depth-clip-control",
                    "depth32float-stencil8",
                    "texture-compression-bc",
                    "texture-compression-etc2",
                    "texture-compression-astc",
                    "indirect-first-instance",
                ],
            },
        });
        await webgpuEngine.initAsync();
        engine = webgpuEngine;
    } else {
        engine = new Engine(canvas, true);
    }

    (window as any).engine = engine;

    const scene = await createSceneModule.createScene(engine, canvas);
    (window as any).scene = scene;

    engine.runRenderLoop(function () {
        scene.render();
    });

    window.addEventListener("resize", function () {
        engine.resize();
    });
};

babylonInit().then(() => {});

package.json 依赖版本

json

复制代码
"dependencies": {
    "@babylonjs/core": "6.26.0",
    "@babylonjs/gui": "6.26.0",
    "@babylonjs/inspector": "6.26.0",
    "@babylonjs/loaders": "6.26.0",
    "@babylonjs/materials": "6.26.0",
    "lil-gui": "0.16.0"
}

注意去掉 ^,防止自动升级到不兼容版本。

修正后源码:https://download.csdn.net/download/zhanglixin999/92736547

相关推荐
微学AI6 小时前
一根针指向所有方向:挂谷猜想对 LLM Agent 技能-记忆架构的启示
开发语言·人工智能·架构·挂谷猜想
豆瓣鸡7 小时前
算法日记 - Day3
java·开发语言·算法
夏殇之殁8 小时前
包中创建自定义列表项。 . 使用自定义列表项进行数据绑定 . 将天气预报数据保存到本地内存表,通过LiveBindings进行显示。 ...
服务器·前端·javascript
韭菜炒鸡肝天8 小时前
VTK开发笔记(一):VTK介绍,Qt..+VSx+VTK.编译
开发语言·笔记·qt
Aaron - Wistron8 小时前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
新中地GIS开发老师9 小时前
WebGIS开发学生作品|低空航天管理与航线规划系统
前端·javascript·webgis·三维gis开发
丙氨酸長鏈9 小时前
Web前端入门第 问:JavaScript 一个简单的 IndexedDB 数据库入门示例
前端·javascript·数据库
Dxy12393102169 小时前
Python项目打包成EXE完整教程(PyInstaller实战避坑)
开发语言·python
05664610 小时前
Python康复训练——常用标准库
开发语言·python·学习
骊城英雄10 小时前
基于C#+avalonia ui实现的跨平台点胶机灌胶监控控制上位机软件
开发语言·ui·c#