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

swift/3d/demo_3d.swift · 1794 字节 · 下载原文

// demo_3d.swift —— 第 5 层:3D 视口(`ui_3d_*`,独立库 vml3d)
// demo_3d.swift -- Layer 5: 3D viewport (`ui_3d_*`, the separate vml3d library)
//
// 开窗 → 建视口(相机/视场/背景/灯光)→ 从同目录 `box.glb`(CC0)载入 → 20 帧 → 关窗。
// Open window -> viewport (camera/FOV/background/light) -> load `box.glb` (CC0) next
// to this file -> 20 frames -> close.
//
// 跑法 / How to run:
//   手机  vml run examples/swift/3d/demo_3d.swift
//   桌面  vmlcli Examples/swift/3d/demo_3d.swift --frames /tmp/f3d
//
// ⚠ 声明来自 `Lib/swift/vml3d.swift`(GenLib 的 `-u` 产出,DocTable:只有注释、无函数体)。
//    声明文件里**不能带函数体**:带体会生成同名空壳把库实现顶掉(E215),3D 视口恒返回 0。
// ⚠ Declarations come from Lib/swift/vml3d.swift (GenLib `-u`, DocTable: comments only,
//    no function bodies). A body in the declaration file shadows the library (E215) and
//    makes the viewport always return 0.
// ⚠ 3D 是透过 2D 层显示的 ⇒ 必须先 `ui_2d_clear(0)`。
// ⚠ 3D shows **through** the 2D layer, so `ui_2d_clear(0)` comes first.

#include <vml_ui.swift>
#include <vml3d.swift>

ui_win_open_ex("demo_3d (Swift)", 0, 0, 0, 0)
var w = ui_scr_get_w()
var h = ui_scr_get_h()
var vp = ui_3d_viewport_create(0, 0, w, h, 0)
if vp > 0 {
    ui_3d_viewport_set_camera(vp, 0.0, 1.5, 4.0, 0.0, 0.0, 0.0)
    ui_3d_viewport_set_fov(vp, 50.0, 0.1, 100.0)
    ui_3d_viewport_set_bg(vp, -15458257)              // 0xFF14202F
    ui_3d_viewport_set_light(vp, 0.4, 0.9, 0.6, -1, 0.3)
    ui_2d_clear(0)
    ui_3d_gltf_load(vp, "box.glb")
    var i = 0
    while i < 20 {
        ui_2d_present()
        ui_msg_fetch_wait(40)
        i = i + 1
    }
}
ui_win_close()