← 都来码 / Dolaima← 例程索引dart/3d/demo_3d.dart 原文

dart/3d/demo_3d.dart · 3727 字节 · 下载原文

// demo_3d.dart —— **3D 视口 + GLB 模型最小示例**(Dart)
// demo_3d.dart — **minimal 3D viewport + GLB model demo** (Dart)
//
// 流程:开窗 → 取画布尺寸 → 建 3D 视口 → 设相机 / 视场 / 背景 / 一盏平行光
//       → 清屏 → 装载同目录的 box.glb → 有界呈现 20 帧 → 释放视口并关窗。
// Flow: open window → query the canvas size → create a 3D viewport → camera / FOV / background / one light
//       → clear → load box.glb from this directory → present 20 frames → free the viewport and close.
//
// box.glb 与 `Examples/c/3d/models/BoxVertexColors.glb` 是同一份文件(Khronos,CC0)。
// box.glb is the same file as `Examples/c/3d/models/BoxVertexColors.glb` (Khronos, CC0).
// 它必须和本文件放在同一目录:桌面 vmlcli 的文件沙箱根是源文件所在目录。
// It must sit next to this file: the desktop vmlcli file sandbox is rooted at the source file's directory.
//
// 声明来自两行 import:`Lib/dart/vml_ui.dart` 与 `Lib/dart/vml3d.dart`
// (GenLib 生成;本前端会真的把声明文件读进源码,见 `DartCompiler.ExpandImports`)。
// Declarations come from the two imports: `Lib/dart/vml_ui.dart` and `Lib/dart/vml3d.dart`
// (generated by GenLib; this frontend really reads the declaration files into the source,
// see `DartCompiler.ExpandImports`).
//
// 跑法:手机 `vml run examples/dart/3d/demo_3d.dart`;
//       桌面 `dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/dart/3d/demo_3d.dart --frames /tmp/fr`
// How to run: on the phone `vml run examples/dart/3d/demo_3d.dart`;
//       on the desktop `dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/dart/3d/demo_3d.dart --frames /tmp/fr`
// ⚠ 看画面请用 `--frames <目录>`,不要用 `--frame`:`ui_win_close()` 之后宿主场景已被置空。
// ⚠ Use `--frames <dir>`, not `--frame`: `ui_win_close()` empties the host scene.

import 'vml_ui.dart';
import 'vml3d.dart';

void main() {
  // ── 1. 开窗(宽高传 0 ⇒ 宿主用真实可用区)──
  // ── 1. Open the window (size 0 ⇒ the host uses the real available area) ──
  ui_win_open_ex("demo_3d", 0, 0, 0, 0);

  // ── 2. 开窗之后再问尺寸,那时才是真实画布 ──
  // ── 2. Query the size after the open, when it is the real canvas ──
  var w = ui_scr_get_w();
  var h = ui_scr_get_h();
  if (w <= 0) { w = 640; }
  if (h <= 0) { h = 640; }

  // ── 3. 建 3D 视口(失败只关窗返回)──
  // ── 3. Create the 3D viewport (on failure just close and return) ──
  var vp = ui_3d_viewport_create(0, 0, w, h, 0);
  if (vp <= 0) { ui_win_close(); return; }

  // ── 4. 相机 / 视场 / 背景 / 一盏平行光 ──
  // ── 4. Camera / FOV / background / one directional light ──
  ui_3d_viewport_set_camera(vp, 0, 1069547520, 1082130432, 0, 0, 0);
  ui_3d_viewport_set_fov(vp, 1112014848, 1036831949, 1120403456);
  ui_3d_viewport_set_bg(vp, -15458257);                 // 0xFF14202F
  ui_3d_viewport_set_light(vp, 1053609165, 1063675494, 1058642330, -1, 1050253722); // argb -1 = 0xFFFFFFFF

  // ── 5. 清屏并装载模型 ──
  // ── 5. Clear the canvas and load the model ──
  ui_2d_clear(0);
  ui_3d_gltf_load(vp, "box.glb");

  // ── 6. 有界呈现:20 帧 ──
  // ── 6. Bounded presentation: 20 frames ──
  var i = 0;
  while (i < 20) {
    ui_2d_present();
    ui_msg_fetch_wait(40);
    i = i + 1;
  }

  // ── 7. 收尾:先释放视口,再关窗 ──
  // ── 7. Clean up: free the viewport first, then close the window ──
  ui_3d_viewport_free(vp);
  ui_win_close();
}