lua/3d/demo_3d.lua · 4091 字节 · 下载原文
-- demo_3d.lua —— 第 5 层:**3D 视口**(`ui_3d_*`,独立库 vml3d)
-- demo_3d.lua — Layer 5: **3D viewport** (`ui_3d_*`, the separate vml3d library)
--
-- 这一层画在 2D 画布之上:视口自带相机 / 视场角 / 背景色 / 一盏方向光,
-- 模型从同目录的 `box.glb`(CC0,glTF 2.0)装载;2D 那层只负责清屏。
-- This layer draws on top of the 2D canvas: the viewport owns a camera, field of view,
-- background colour and one directional light; the model comes from `box.glb` (CC0,
-- glTF 2.0) next to this file, while the 2D layer only clears the canvas.
--
-- 跑法 / How to run:
-- 手机 vml run examples/lua/3d/demo_3d.lua
-- 桌面 vmlcli Examples/lua/3d/demo_3d.lua --screen 640x480 --frames /tmp/f3d
--
-- ⚠ 三点实测要求 / Three measured requirements:
-- ① 3D 是**透过** 2D 层显示的 ⇒ 必须 `ui_2d_clear(0)`;不透明底会把 3D 整片盖住。
-- The 3D output shows **through** the 2D layer, so `ui_2d_clear(0)` is mandatory.
-- ② `box.glb` 走**相对路径**——宿主沙箱根就是源文件所在目录。
-- `box.glb` uses a **relative** path: the sandbox root is the source-file directory.
-- ③ ⚠ **float 形参用「位模式整数」传**:本前端把整值浮点(`4.0`)当整数压、把
-- `1.5` 这类压成 **8 字节 double**,而 `ui_3d_*` 是 C 侧接口、从主栈按
-- **4 字节 float** 读形参 ⇒ 直接写浮点字面量会静默错位。这里改传 IEEE-754 的
-- 32 位位模式(4 字节一格,C 侧读到的正是那个 float)。
-- ⚠ **Pass float parameters as bit-pattern integers**: this frontend pushes
-- integral floats as integers and non-integral ones as **8-byte doubles**, while
-- `ui_3d_*` is a C-side interface reading **4-byte floats** from the main stack —
-- float literals silently misalign them. The IEEE-754 32-bit patterns are used
-- instead (one 4-byte slot each).
require("vml_ui")
require("vml3d")
local frames = 20 -- 20 帧 × 40ms ≈ 0.8 秒 / 20 frames x 40ms ~ 0.8s
-- float32 位模式常量(右侧是它们表示的数值)/ float32 bit patterns (values on the right)
local f_0 = 0 -- 0.0
local f_1p5 = 1069547520 -- 1.5
local f_4 = 1082130432 -- 4.0
local f_50 = 1112014848 -- 50.0
local f_0p1 = 1036831949 -- 0.1
local f_100 = 1120403456 -- 100.0
local f_0p4 = 1053609165 -- 0.4
local f_0p9 = 1063675494 -- 0.9
local f_0p6 = 1058642330 -- 0.6
local f_0p3 = 1050253722 -- 0.3
-- ① 开窗:宽高传 0 = 交给宿主用真实可用区
-- ① Open the window: size 0 = the host picks the real usable area
ui_win_open_ex("demo_3d (Lua)", 0, 0, 0, 0)
local w = ui_scr_get_w()
local h = ui_scr_get_h()
-- ② 建视口 + 相机 / 视场 / 背景 / 灯光
-- ② Create the viewport plus camera / FOV / background / light
local vp = ui_3d_viewport_create(0, 0, w, h, 0)
-- 相机:eye (0, 1.5, 4) → at (0, 0, 0)
ui_3d_viewport_set_camera(vp, f_0, f_1p5, f_4, f_0, f_0, f_0)
-- 视场角 50°、近 0.1、远 100 / FOV 50 deg, near 0.1, far 100
ui_3d_viewport_set_fov(vp, f_50, f_0p1, f_100)
-- 背景 0xFF14202F;灯光方向 (0.4, 0.9, 0.6)、白、环境光 0.3
-- Background 0xFF14202F; light direction (0.4, 0.9, 0.6), white, ambient 0.3
ui_3d_viewport_set_bg(vp, 0xFF14202F)
ui_3d_viewport_set_light(vp, f_0p4, f_0p9, f_0p6, 0xFFFFFFFF, f_0p3)
-- ③ 2D 层清成透明,让 3D 透出来 / Clear the 2D layer to transparent so the 3D shows through
ui_2d_clear(0)
-- ④ 装载模型(返回场景句柄)/ Load the model (returns a scene handle)
local scene = ui_3d_gltf_load(vp, "box.glb")
-- ⑤ 有界渲染循环:交帧 + 等一条消息 / Bounded render loop: present + wait for one message
local i = 0
while i < frames do
ui_2d_present()
ui_msg_fetch_wait(40)
i = i + 1
end
-- ⑥ 收尾:释放场景 / 视口,再关窗
-- ⑥ Wrap-up: free the scene/viewport, then close the window
ui_3d_gltf_free(scene)
ui_3d_viewport_free(vp)
ui_win_close()
print("demo_3d done (Lua)")