구조
엔진이 무엇으로 이루어졌고, 무엇이 무엇을 들고 있고, 한 프레임이 어떤 차례로 도는지 봅니다. 저장소의 ARCHITECTURE.md 를 엔진을 쓰는 사람의 눈높이로 줄인 것이고, 원본 vanilla.js 와 달라진 것을 중심으로 적습니다.
모듈 배치
| 모듈 | 들어 있는 것 |
|---|---|
base |
엔진을 모르는 기반 타입: Vector2, Vector3, Matrix4, Quaternion, Rect, Color, Colors, Pivot, SeededRandom, FiniteStateMachine, ObjectPool, Cooldown, RepeatTimer, Version, format, math, wait |
core |
엔진 본체: Engine, Scene, SceneBase, Graphic, Component, TimeManager, ViewManager, InputManager, AudioManager, 애셋 트레이트(Asset), Tween, Animation, async 실행기(EngineContext, SceneContext, TaskHandle, Promise), Canvas2D, 반사 값과 트레이트 |
core/node |
NodeTree(노드 저장소), WorldNode |
core/component |
Paint, Sprite, Text, RichText, Mask, Outline, SoftShade, ShadowText, ShadowImage |
resource |
ImageAsset, TextAsset, JsonAsset, BlobAsset, BunchAsset, FontAsset, AudioAsset, VisualAsset, AssetLoader, SpriteAtlas |
effect |
ParticleSystem, ShaderSprite, ScreenEffect, TrailRenderer, HeatHazeEffect |
misc |
Camera2D, PathFinder, Collision2D, LocalStorage, Localization, DialogueRunner, DevTools 같은 게임 보조 기능 |
animation |
Timeline, Animator, Action |
graphics3d |
SkinnedModel, SkinnedModelRenderer, HumanSkinRenderer, Material, ShadowMap, RenderTarget, 블룸, AO, SSS, Camera3D, 툰 3D(graphics3d/toon) |
ui |
UI 위젯, 창 쌓기, UIDocument, UINode, UIScene, 오토레이아웃(LayoutSolver 등) |
game |
GameScene, GameSceneBase (로딩 화면, 터치 라우팅, 세이프 에어리어, 페이드, 개발자 도구를 갖춘 게임용 씬) |
web |
WebSocketClient, Pane |
platform |
창, GL 컨텍스트, 입력 이벤트, 프레임 루프, 애셋 바이트 읽기, 소리. 데스크탑(winit, glutin)과 웹(캔버스, WebGL2) |
- 공개 이름은
src/lib.rs가 크레이트 맨 위로 다시 내보냅니다. 그래서use vanilla::{Engine, Scene, SceneBase};처럼 씁니다. 모듈 경로(vanilla::core::scene::SceneAny같은)로도 닿습니다. - 레이어는 원본처럼 아래에서 위로만 의존합니다. (
base←core←resource,effect,misc같은 위층)platform은core밑에서 창, GL, 입력을 대 주고, 엔진 본체는 플랫폼을 모릅니다. - 반사 매크로는
macros/의 따로 된 크레이트(vanilla-engine-macros)이고, 엔진이vanilla::reflect로 다시 내보냅니다. 게임은 엔진 하나만 의존합니다.
설계 원칙 (원본과 다른 것)
- 노드는 핸들로 가리킵니다. 원본은 부모가 자식 객체를 배열로 들고 자식이 부모를 거꾸로 가리킵니다. 러스트 판은 씬의
NodeTree가 모든 노드를 저장소 하나(slotmap)에 담고, 부모와 자식을NodeId로 잇습니다.NodeId는 칸 번호와 세대 번호라서, 지운 노드의 칸을 새 노드가 다시 써도 예전 핸들은None을 돌려줍니다.Rc<RefCell>은 쓰지 않습니다. - 전역 엔진은 어디서나 꺼냅니다. 원본
Engine.getEngine()처럼Engine::get_engine()으로 꺼냅니다.Engine::new가 엔진을 프로그램 끝까지 사는 자리에 두어, 만든 직후부터 씬 훅, 컴포넌트의tick과draw, 콜백, async 작업 안 어디서나 같은&'static Engine을 줍니다. 엔진 함수는 모두&self이고 고치는 칸은 안쪽 가변성이라, 씬 훅의engine인자와 같이 써도 됩니다. 매니저는get_x가Ref,get_x_mut이RefMut을 주고, 같은 매니저를 고치는 중에 또 빌리면 함수 이름을 적은 패닉으로 멈춥니다. - 상속 대신 트레이트와 공통 상태 구조체.
Scene+SceneBase,Component+ComponentBase,Asset+AssetBase꼴입니다. 원본의 노드 상속 넷(Node, ComponentNode, TransformNode, WorldNode)은WorldNode하나로 합쳤고, 원본에서 노드였던 위젯(입력 필드, 스크롤 바, 창 같은 것)과UINode는 러스트 판에서 컴포넌트입니다. - 플랫폼 층을 뗐습니다. 창, GL, 입력, 루프, 애셋 바이트, 소리는
platform아래에만 있습니다. - 성능 기능은 엔진 기본입니다. 묶어 그리기와 화면 밖 거르기를 처음부터 켭니다.
- ECS 로 가지 않습니다. 원본의 씬, 노드, 컴포넌트 꼴을 그대로 두어 vanilla.js 게임을 옮기기 쉽게 합니다.
- 2D 와 3D 는 GL 컨텍스트 하나를 같이 씁니다. 씬의
pre_draw에서 그림자, 3D 장면, 블룸 패스를 그리고Graphic::restore_render_state로 2D 상태를 되살린 뒤 UI 를 그립니다. 2D 그리기 사이에 끼우는 3D 는Graphic::run_external_pass로 감쌉니다.
핸들이 어떻게 도는지는 창 없이도 볼 수 있습니다.
use vanilla::{NodeTree, Paint, Vector2};
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let parent_id = node_tree.create_child(root_id, "parent");
let child_id = node_tree.create_child(parent_id, "child");
node_tree[child_id].set_local_position(Vector2::create(10.0, 20.0));
node_tree[child_id].add_component(Paint::create_with_color("#f3e5ab"));
assert_eq!(node_tree.get_parent_id(child_id), Some(parent_id));
assert_eq!(node_tree.get_child_ids(parent_id), &[child_id]);
node_tree.destroy_node(parent_id);
assert!(node_tree.get_node(parent_id).is_none());
assert!(node_tree.get_node(child_id).is_none());
let new_id = node_tree.create_child(root_id, "new");
assert_ne!(new_id, child_id);
assert!(node_tree.get_node(child_id).is_none());
assert!(node_tree.contains(new_id));소유 구조
EngineRunner
├── Engine
│ ├── EngineConfiguration
│ ├── TimeManager
│ ├── ViewManager
│ ├── InputManager
│ │ ├── GamepadManager
│ │ ├── InputActions
│ │ └── TextInput
│ ├── PointerCursor
│ ├── AudioManager
│ ├── Platform
│ ├── TaskExecutor
│ └── Graphic
│ ├── ShaderProgram
│ ├── ImageTextureCache
│ └── TextStringTextureCache
└── Vec<Box<dyn Scene>>
└── SceneBase
├── NodeTree
│ └── WorldNode
│ ├── NodeTransform
│ └── Vec<Box<dyn Component>>
└── Vec<Tween>- 플랫폼이 엔진 실행자(
EngineRunner)를 들고 프레임마다 부릅니다.Engine::run이 이 실행자를 만들어 플랫폼에 넘깁니다. 게임 코드가 실행자를 직접 다룰 일은 없습니다. Graphic은Engine이 듭니다. 플랫폼이 GL 컨텍스트를 만든 뒤 넘겨주고, 원본getGraphic()처럼engine.get_graphic(),engine.get_graphic_mut()로 씬의load,initialize,tick이나 컴포넌트의tick(context.get_engine())에서 꺼냅니다. 씬의 그리기 훅(draw_on_load,pre_draw,draw,post_draw,draw_gizmos)은&mut Graphic을 인자로 받습니다. 엔진이 그래픽을 잠깐 꺼내 넘기는 것이라 그 안에서는engine.get_graphic()이None이고 인자의graphic을 씁니다. (&Engine과&mut Graphic을 함께 넘기는 러스트 빌림 꼴, 그리기 밖에서 둘을 함께 쓰려면engine.with_graphic(|engine, graphic| ...))- 씬은 엔진이
Box<dyn Scene>으로 소유합니다. 다른 씬을 가리킬 때는 씬 값 대신Engine::load_scene이 돌려준SceneId를 씁니다. 원본의SceneManager는 엔진에 합쳤습니다. - 내리는 중인 씬(unload 에서 맡긴 작업을 기다리는 씬)은 원본처럼 목록에 남아
tick과draw를 받습니다. 목록에서 뺀 뒤에도 그 씬의 async 작업이 남았으면 작업이 끝날 때까지 씬 값을 살려 두고, 다 끝나면destroy를 부르고 버립니다. - 애셋은 원본처럼 중앙 매니저 없이 씬이 멤버로 듭니다. 같은 그림을 여러 번 그려도 GPU 텍스처는
Graphic의 캐시가 하나만 만듭니다. 여럿을 함께 받을 때는AssetLoader가 동시 수만큼 요청하고 진행률을 셉니다. - 소리 장치는
AudioManager가 처음 쓸 때 엽니다.
노드와 컴포넌트
- 노드 만들기:
node_tree.create_child(parent_id, "name")이NodeId를 돌려주고,node_tree[node_id]로 꺼내 설정합니다. 붙이지 않은 노드는create_node로 만들고add_child로 붙입니다. - 지우기:
destroy_node(node_id)는 자손까지 지웁니다.tick이나 터치를 도는 도중에 부르면 비활성으로 두었다가 다 돈 뒤에 지웁니다. - 컴포넌트 붙이기: 보통은
node_tree[node_id].add_component(...)입니다. 붙을 때 자식 노드를 만드는 컴포넌트(UI 위젯 같은 것)는node_tree.add_component(node_id, ...)로 붙입니다. - 컴포넌트는
tick, 터치, 휠에서ComponentContext를 받아 자기 노드와 트랜스폼, 트리 전체, 엔진(입력, 시간, 뷰, 소리)에 닿습니다. 부르는 컴포넌트 하나만 노드에서 잠깐 꺼내 두므로,&mut self를 쥔 채로 같은 노드의 다른 컴포넌트와 트리를 빌릴 수 있습니다. - 씬 코드에서 컴포넌트와 트리를 함께 바꿀 때는
node_tree.with_component_mut::<T, _>(node_id, |component, tree| ...)를 씁니다. 자주 쓰는 일은 트리를 받는*_in_tree함수로 따로 있습니다. - 그리기만 하는 컴포넌트(
Paint,Sprite,Text,Mask)만 붙은 노드는tick에서 건너뜁니다. 그리는 사각형을 알려 주는 컴포넌트만 붙은 노드는 화면 밖이면 그리지 않습니다.
프레임 루프
플랫폼이 창이나 캔버스와 GL 을 만들고 첫 씬을 올린 뒤, 매 프레임 이 차례로 돕니다.
- 바이트가 온 글꼴을 글자 캐시에 올립니다.
Graphic의 상태를 되돌리고 화면을 비웁니다. (뷰포트, 투영, 변환은 단위 행렬)- 시간(
TimeManager)과 입력(InputManager, 게임패드와 입력 동작 포함)을 갱신합니다. - 깨어난 async 작업을 폴링하고, 내리기가 끝난 씬을 정리합니다.
- 씬마다: 불러오는 중이면
draw_on_load만 부르고, 아니면tick, 씬의 터치 훅,pre_draw,draw,post_draw, (기즈모를 켰으면)draw_gizmos차례로 부릅니다. - 정보창(
use_statistics를 켰을 때)과 마우스 커서를 처리하고, 한 프레임짜리 입력 표시를 지웁니다. - 남은 묶음을 그리고, 검증용 화면 저장을 하고, 프레임 번호를 하나 올립니다. (첫 프레임이 1)
- 씬 목록을 바꿉니다. 내릴 씬은
finalize,unload를 부르고, 새로 올린 씬은create와load를 부릅니다.
그 뒤에 플랫폼이 화면을 넘깁니다. (데스크탑은 버퍼 바꾸기, 웹은 콜백이 끝나면 브라우저가) 데스크탑에서 엔진의 질문 창(Engine::alert 같은 것)이 떠 있는 동안은 원본에서 자바스크립트가 멈추듯 씬의 tick 과 async 작업을 건너뜁니다.
씬 불러오기는 원본의 create, 비동기 load, initialize 를 같은 이름의 create, load(맡긴 애셋과 작업을 기다림), initialize 로 옮겼습니다. 원본 async load 의 await 는 load 에서 Engine::spawn_scene_task 로 맡긴 작업이고, SceneBase::track_task 로 씬에 맡기면 엔진이 그 작업이 끝나기를 기다립니다. 맡긴 것이 다 끝난 뒤에도 원본처럼 0.3 초 더 로딩 화면을 보이고 initialize 를 부릅니다. 자세한 차례는 "엔진과 씬 생명 주기" 장에서 봅니다.
그리기
- 모든 2D 그리기는
Graphic::draw_vertices한 곳으로 모입니다. 묶을 수 있으면 정점을 CPU 에서 변환해 모았다가 같은 텍스처가 이어지는 동안 드로우 콜 한 번으로 그립니다. 정점마다 색과 틴트를 실어, 색이 다른 그리기도 같은 묶음에 듭니다. - 블렌드가 바뀔 때, 클립을 걸거나 풀 때, 파티클을 그릴 때, 프레임 끝에 모은 것을 먼저 내보냅니다. GL 을 직접 쓰는 코드는 그 전에
Graphic::flush_batch를 부르거나Graphic::run_external_pass로 감쌉니다. - 블렌드 모드는 원본처럼 26 가지를 모두 그립니다. 13 가지는 고정 블렌딩이고, 나머지 13 가지(Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity)는 그릴 영역 아래를 떠 와 W3C Compositing and Blending 공식대로 셰이더로 섞습니다. (이 모드에서는 묶지 않습니다)
- 클립은 원본처럼 스텐실 기반입니다.
- 화면 효과(
ScreenEffect)는 씬이pre_draw첫머리에서 시작하고post_draw끝에서 마칩니다. 마칠 때 화면을 텍스처로 떠 와 풀스크린 패스 사슬을 겁니다. - 묶어 그리기와 거르기는
Graphic::set_batching_enabled,Graphic::set_culling_enabled로 끕니다.
애셋
ImageAsset,TextAsset,JsonAsset,BlobAsset,FontAsset은load(engine, 경로)로 요청하고SceneBase::track_asset으로 씬에 맡깁니다.load는 await 할 수 있는 손잡이를 돌려줍니다.- 데스크탑은
EngineConfiguration::asset_directory_paths의 폴더 후보에서 그 자리에서 읽고, 웹은asset_base_url과 경로를 이어 fetch 합니다. 게임 코드는 같습니다. - 실패한 애셋도 끝난 것으로 쳐서 씬이 멈추지 않습니다.
- 그림, 소리, 글꼴을 웹은 원본처럼 브라우저가 풀고, 데스크탑은 엔진이 같은 결과가 나오게 풉니다.
- 기본 글꼴은 엔진에 들어 있지 않습니다. 두 플랫폼 모두 시스템 글꼴이 기본이고, 게임 글꼴은
default_font_url,default_font_bytes,FontAsset가운데 하나로 줍니다.
async/await
- 엔진은 외부 런타임 없이 스레드 하나짜리 실행기를 품습니다. 퓨처는
Send가 아니어도 되고, 데스크탑과 웹이 같은 코드로 돕니다. Engine::spawn_task,Engine::spawn_scene_task는 원본 async 함수를 부를 때처럼 맡기는 자리에서 첫 await 까지 돌고, 남은 것은 프레임마다 씬의tick앞에서 깨어난 차례대로 돕니다.- 퓨처는
&mut를 await 너머로 쥘 수 없으므로, 작업은 컨텍스트의with(|scene, engine| ...)클로저 안에서만 씬과 엔진을 빌립니다. - 원본에서 Promise 를 돌려주던 load 들은 애셋이나 씬을 빌리지 않는 손잡이를 돌려줍니다. 그래서 원본
await this.atlas.load(path)가scene_context.with(|scene, engine| scene.atlas.load(engine, path)).await한 줄입니다. - 자세한 것은 "async/await" 장에서 봅니다.
원본과의 대응
원본 src/ 의 .js 200 파일 가운데 182 파일을 옮겼습니다. (같은 이름 176, 다른 파일에 합친 것 6) 나머지 18 파일은 빈 파일, 죽은 코드, 러스트 언어와 표준이 맡는 것(객체 모델 도우미, 컬렉션), 다른 기능이 덮는 것(Camera2D 가 덮는 deprecated/camera.js)이라 일부러 옮기지 않았습니다.
러스트 판에만 있는 것은 원본에서 브라우저가 하던 일입니다. 플랫폼 층, 글자 모양 짓기와 색 이모지, 그림 풀기, 글 입력 줄, 노드 저장소, async 실행기, 브라우저 자리(Canvas2D, 질문 창, 브라우저가 그리는 글의 번역)가 그렇습니다.
두 엔진의 차이
두 엔진은 기능 차이를 두지 않습니다. 남은 차이는 언어나 플랫폼 때문에 정말 맞출 수 없는 것과, 러스트에 맞게 일부러 정한 꼴뿐입니다.
정말 맞출 수 없는 것
- 반사(UI 문서의 반사 저장과 읽기, 개발자 도구 인스펙터의 게터 목록)는
#[vanilla::reflect]를 붙인 타입만 됩니다. 엔진 컴포넌트는 모두 붙였고, 게임 컴포넌트는impl블록과impl Component블록에 붙입니다. 러스트에는 실행 중에 메서드를 훑는 길이 없어서입니다. - 데스크탑 개발자 도구의 메모리 칸은 자바스크립트 힙 대신 시스템 할당자의 힙입니다.
- 데스크탑의 글자 래스터 픽셀과 도형 가장자리 안티에일리어싱이 크롬과 다릅니다. (글자 너비와 도형 안쪽 색은 같습니다)
- 데스크탑 Opus 와 AAC 소리의 표본 끝자리가 크롬과 아주 조금 다릅니다. (길이와 다듬기는 같습니다)
일부러 정한 꼴
| 차이 | 까닭 |
|---|---|
전역 엔진 Engine::get_engine() 은 원본처럼 Engine::new 직후부터 어디서나 꺼냅니다. 씬 함수는 인자로, 컴포넌트는 ComponentContext 로도 받습니다(같은 엔진) |
엔진 함수는 모두 &self, 고치는 칸은 안쪽 가변성(Cell, RefCell)입니다. 매니저는 Ref, RefMut 으로 빌려 줍니다 |
노드는 NodeTree 안에 있고 NodeId 로 가리킵니다 |
부모와 자식을 Rc<RefCell> 로 서로 가리키면 순환 참조와 실행 중 빌림 검사가 생깁니다 |
노드 종류는 WorldNode 하나입니다 |
러스트에 클래스 상속이 없어 노드 꼴을 하나로 두고 기능을 컴포넌트로 붙였습니다 |
상속 대신 트레이트와 품기입니다 (Scene + SceneBase, Component + ComponentBase) |
공통 상태는 구조체로, 덮어쓰기는 트레이트 함수로 둡니다 |
수 꼴을 나눠 씁니다: 로직 수는 원본 number 와 같은 f64, 원본이 Float32Array 에 담는 곳(Matrix4 의 칸, GPU 정점과 유니폼)과 크롬이 float 로 셈하는 곳(Canvas2D 안쪽, 소리, 글리프 래스터, 그림 해독, 색 관리)은 f32 입니다 |
JS 수는 number 하나뿐이고 러스트는 꼴을 고릅니다. 로직을 원본과 같은 배정밀도로 셈해 끝자리까지 같고, f32 자리는 원본과 크롬이 실제로 쓰는 정밀도입니다 |
| GL 을 쓰는 객체는 컨텍스트를 들지 않고 함수마다 gl 을 받습니다 | 컨텍스트는 Graphic 이 들고, 나눠 쥐면 수명과 빌림이 엮입니다 |
트리를 받는 *_in_tree 함수를 따로 둔 곳이 있습니다 |
씬 코드가 컴포넌트를 쥔 동안 같은 트리의 다른 노드를 함께 빌릴 수 없습니다 |
선택 인자 객체 대신 기본값을 가진 옵션 구조체입니다 (RenderTargetOptions, UIConfirmWindowOptions 같은 것) |
러스트에는 이름 붙은 선택 인자가 없습니다 |
콜백은 Box<dyn FnMut> 로 받고, 콜백 게터는 Rc<RefCell> 로 돌려줍니다 |
엔진과 부르는 쪽이 같은 함수를 나눠 쥡니다 |
예외 대신 Result 와 Option 입니다 |
러스트는 예외를 던지지 않습니다 |
| 이름은 러스트 규칙(snake_case, UpperCamelCase, SCREAMING_SNAKE_CASE)입니다 | Rust API Guidelines C-CASE 를 따릅니다 |
async 작업은 컨텍스트의 with 안에서만 씬과 엔진을 만집니다 |
러스트 퓨처는 &mut 를 await 너머로 쥘 수 없습니다 |
옮겨 오는 쪽에서 본 이름 바꾸기와 소유 규칙은 "원본 vanilla.js 에서 옮겨 오기" 장에서 봅니다.
관련 타입
Engine, EngineConfiguration, Scene, SceneBase, SceneId, NodeTree, NodeId, WorldNode, Component, ComponentBase, ComponentContext, Graphic, EngineContext, SceneContext, TaskHandle, Promise
Architecture
What the engine is made of, what holds what, and in what order a frame runs. This is the repository's ARCHITECTURE.md trimmed down for people using the engine, focusing on what differs from the original vanilla.js.
Module Layout
| Module | Contents |
|---|---|
base |
Base types that do not know the engine: Vector2, Vector3, Matrix4, Quaternion, Rect, Color, Colors, Pivot, SeededRandom, FiniteStateMachine, ObjectPool, Cooldown, RepeatTimer, Version, format, math, wait |
core |
The engine core: Engine, Scene, SceneBase, Graphic, Component, TimeManager, ViewManager, InputManager, AudioManager, the asset trait (Asset), Tween, Animation, the async executor (EngineContext, SceneContext, TaskHandle, Promise), Canvas2D, reflection values and traits |
core/node |
NodeTree (node storage), WorldNode |
core/component |
Paint, Sprite, Text, RichText, Mask, Outline, SoftShade, ShadowText, ShadowImage |
resource |
ImageAsset, TextAsset, JsonAsset, BlobAsset, BunchAsset, FontAsset, AudioAsset, VisualAsset, AssetLoader, SpriteAtlas |
effect |
ParticleSystem, ShaderSprite, ScreenEffect, TrailRenderer, HeatHazeEffect |
misc |
Game helpers such as Camera2D, PathFinder, Collision2D, LocalStorage, Localization, DialogueRunner, DevTools |
animation |
Timeline, Animator, Action |
graphics3d |
SkinnedModel, SkinnedModelRenderer, HumanSkinRenderer, Material, ShadowMap, RenderTarget, bloom, AO, SSS, Camera3D, toon 3D (graphics3d/toon) |
ui |
UI widgets, window stacking, UIDocument, UINode, UIScene, auto layout (LayoutSolver and others) |
game |
GameScene, GameSceneBase (a scene for games with a loading screen, touch routing, safe area, fades and developer tools) |
web |
WebSocketClient, Pane |
platform |
Windows, GL context, input events, frame loop, asset byte loading, sound. Desktop (winit, glutin) and web (canvas, WebGL2) |
src/lib.rsre-exports the public names at the crate root, so you writeuse vanilla::{Engine, Scene, SceneBase};. Module paths (such asvanilla::core::scene::SceneAny) also work.- As in the original, layers depend only upward. (
base←core← upper layers such asresource,effect,misc)platformsits undercoreand provides the window, GL and input; the engine core does not know the platform. - The reflection macro lives in a separate crate under
macros/(vanilla-engine-macros) and is re-exported by the engine asvanilla::reflect. A game depends on the engine alone.
Design Principles (What Differs from the Original)
- Nodes are referenced by handles. In the original, a parent holds its child objects in an array and the child points back to the parent. In the Rust port, the scene's
NodeTreeputs every node in one storage (a slotmap) and links parents and children withNodeId. ANodeIdis a slot index plus a generation, so even if a new node reuses a destroyed node's slot, the old handle returnsNone.Rc<RefCell>is not used. - The global engine is taken anywhere. Like the original
Engine.getEngine(), it is taken withEngine::get_engine().Engine::newputs the engine in a place that lives until the program ends, so right after creation it gives the same&'static Engineanywhere: scene hooks, a component'stickanddraw, callbacks, inside async tasks. All engine functions are&selfand the fields they change use interior mutability, so it can be used together with theengineargument of scene hooks. Managers come asReffromget_xandRefMutfromget_x_mut, and borrowing the same manager again while it is being changed stops with a panic that names the function. - Traits and shared state structs instead of inheritance. The forms are
Scene+SceneBase,Component+ComponentBase,Asset+AssetBase. The original's four node classes (Node, ComponentNode, TransformNode, WorldNode) are merged into a singleWorldNode, and widgets that were nodes in the original (input field, scroll bar, window and so on) andUINodeare components in the Rust port. - The platform layer is separate. Windows, GL, input, the loop, asset bytes and sound live only under
platform. - Performance features are engine defaults. Batching and off-screen culling are on from the start.
- It does not move to ECS. The original's scene, node and component forms are kept so vanilla.js games are easy to port.
- 2D and 3D share one GL context. A scene's
pre_drawdraws the shadow, 3D scene and bloom passes, restores the 2D state withGraphic::restore_render_state, and then the UI is drawn. 3D inserted between 2D draws is wrapped inGraphic::run_external_pass.
How handles behave can be seen without a window.
use vanilla::{NodeTree, Paint, Vector2};
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let parent_id = node_tree.create_child(root_id, "parent");
let child_id = node_tree.create_child(parent_id, "child");
node_tree[child_id].set_local_position(Vector2::create(10.0, 20.0));
node_tree[child_id].add_component(Paint::create_with_color("#f3e5ab"));
assert_eq!(node_tree.get_parent_id(child_id), Some(parent_id));
assert_eq!(node_tree.get_child_ids(parent_id), &[child_id]);
node_tree.destroy_node(parent_id);
assert!(node_tree.get_node(parent_id).is_none());
assert!(node_tree.get_node(child_id).is_none());
let new_id = node_tree.create_child(root_id, "new");
assert_ne!(new_id, child_id);
assert!(node_tree.get_node(child_id).is_none());
assert!(node_tree.contains(new_id));Ownership
EngineRunner
├── Engine
│ ├── EngineConfiguration
│ ├── TimeManager
│ ├── ViewManager
│ ├── InputManager
│ │ ├── GamepadManager
│ │ ├── InputActions
│ │ └── TextInput
│ ├── PointerCursor
│ ├── AudioManager
│ ├── Platform
│ ├── TaskExecutor
│ └── Graphic
│ ├── ShaderProgram
│ ├── ImageTextureCache
│ └── TextStringTextureCache
└── Vec<Box<dyn Scene>>
└── SceneBase
├── NodeTree
│ └── WorldNode
│ ├── NodeTransform
│ └── Vec<Box<dyn Component>>
└── Vec<Tween>- The platform holds the engine runner (
EngineRunner) and calls it every frame.Engine::runcreates this runner and hands it to the platform. Game code never handles the runner directly. Graphicis held byEngine. The platform passes it in after creating the GL context, and like the originalgetGraphic()you take it withengine.get_graphic()andengine.get_graphic_mut()in the scene'sload,initializeandtickor in a component'stick(context.get_engine()). The scene's drawing hooks (draw_on_load,pre_draw,draw,post_draw,draw_gizmos) receive&mut Graphicas an argument. The engine takes the graphic out for a moment to pass it, so inside themengine.get_graphic()isNoneand you use thegraphicargument. (This is the Rust borrowing form for passing&Engineand&mut Graphictogether; to use both together outside drawing, callengine.with_graphic(|engine, graphic| ...))- The engine owns scenes as
Box<dyn Scene>. To refer to another scene, use theSceneIdreturned byEngine::load_sceneinstead of the scene value. The original'sSceneManageris merged into the engine. - A scene being unloaded (waiting for the tasks it handed over in unload) stays in the list and receives
tickanddraw, as in the original. If the scene's async tasks remain after it leaves the list, the scene value is kept alive until they finish, thendestroyis called and it is dropped. - As in the original, assets are held as members by the scene, with no central manager. Drawing the same image many times creates only one GPU texture, in
Graphic's cache. To load several together,AssetLoaderrequests them up to a concurrency limit and counts progress. - The sound device is opened by
AudioManageron first use.
Nodes and Components
- Creating nodes:
node_tree.create_child(parent_id, "name")returns aNodeId, andnode_tree[node_id]gives the node to configure. Unattached nodes are made withcreate_nodeand attached withadd_child. - Destroying:
destroy_node(node_id)destroys the descendants too. Called in the middle of atickor touch pass, it deactivates the node and destroys it after the pass. - Attaching components: usually
node_tree[node_id].add_component(...). Components that create child nodes when attached (such as UI widgets) are attached withnode_tree.add_component(node_id, ...). - In
tick, touch and wheel, a component receives aComponentContextthat reaches its own node and transform, the whole tree and the engine (input, time, view, sound). Only the calling component is briefly taken out of its node, so while holding&mut selfit can borrow the other components of the same node and the tree. - To change a component and the tree together from scene code, use
node_tree.with_component_mut::<T, _>(node_id, |component, tree| ...). Common operations have separate*_in_treefunctions that take the tree. - Nodes that carry only draw-only components (
Paint,Sprite,Text,Mask) are skipped intick. Nodes whose components all report the rectangle they draw are not drawn when off screen.
Frame Loop
After the platform creates the window or canvas and GL and loads the first scene, every frame runs in this order.
- Fonts whose bytes have arrived are registered in the glyph cache.
Graphicstate is reset and the screen is cleared. (Viewport, projection, and the transform set to identity)- Time (
TimeManager) and input (InputManager, including gamepads and input actions) are updated. - Woken async tasks are polled, and scenes that finished unloading are cleaned up.
- For each scene: while loading only
draw_on_loadis called; otherwisetick, the scene's touch hooks,pre_draw,draw,post_drawand (if gizmos are on)draw_gizmosare called in order. - The statistics overlay (when
use_statisticsis on) and the mouse cursor are handled, and one-frame input flags are cleared. - The remaining batch is drawn, the verification screen capture is taken, and the frame number goes up by one. (The first frame is 1)
- The scene list is updated. Scenes to unload get
finalizeandunload; newly loaded scenes getcreateandload.
Then the platform presents the screen. (Buffer swap on desktop; on the web the browser does it when the callback returns) On desktop, while one of the engine's dialogs (Engine::alert and others) is open, the scenes' tick and async tasks are skipped, the way JavaScript pauses in the original.
Scene loading maps the original's create, async load, initialize onto the same names create, load (waiting for the handed-over assets and tasks), initialize. The awaits of the original async load are a task handed over with Engine::spawn_scene_task in load; handing it to the scene with SceneBase::track_task makes the engine wait for that task. Even after everything handed over has finished, the loading screen stays for 0.3 more seconds, as in the original, before initialize is called. The detailed order is in the "Engine and Scene Lifecycle" chapter.
Drawing
- All 2D drawing funnels into one place,
Graphic::draw_vertices. When batching is possible, vertices are transformed on the CPU and collected, then drawn in one draw call while the same texture continues. Color and tint ride on each vertex, so draws with different colors go into the same batch. - The collected batch is flushed first when the blend changes, when a clip is set or released, when particles are drawn, and at the end of the frame. Code that uses GL directly calls
Graphic::flush_batchbefore that or wraps itself inGraphic::run_external_pass. - Like the original, all 26 blend modes are drawn. 13 use fixed blending; the other 13 (Overlay, Darken, Lighten, ColorDodge, ColorBurn, HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity) copy the area under the draw region and blend in a shader following the W3C Compositing and Blending formulas. (These modes are not batched)
- Clipping is stencil-based, as in the original.
- Screen effects (
ScreenEffect) are begun by the scene at the start ofpre_drawand ended at the end ofpost_draw. Ending copies the screen into a texture and runs a chain of fullscreen passes. - Batching and culling are turned off with
Graphic::set_batching_enabledandGraphic::set_culling_enabled.
Assets
ImageAsset,TextAsset,JsonAsset,BlobAssetandFontAssetare requested withload(engine, path)and handed to the scene withSceneBase::track_asset.loadreturns a handle that can be awaited.- Desktop reads on the spot from the folder candidates in
EngineConfiguration::asset_directory_paths; the web fetchesasset_base_urljoined with the path. Game code is the same. - Failed assets also count as finished, so the scene does not stall.
- On the web the browser decodes images, sound and fonts as in the original; on desktop the engine decodes them so the results match.
- No default font is built into the engine. Both platforms default to system fonts, and a game font is given with one of
default_font_url,default_font_bytesorFontAsset.
async/await
- The engine contains a single-threaded executor with no external runtime. Futures do not have to be
Send, and desktop and web run the same code. Engine::spawn_taskandEngine::spawn_scene_taskrun up to the first await right where they are called, like calling an original async function, and the rest runs every frame before the scenes'tick, in the order the tasks were woken.- A future cannot hold
&mutacross an await, so a task borrows the scene and engine only inside the context'swith(|scene, engine| ...)closure. - The loads that returned a Promise in the original return handles that do not borrow the asset or scene. So the original
await this.atlas.load(path)is the single linescene_context.with(|scene, engine| scene.atlas.load(engine, path)).await. - Details are in the "async/await" chapter.
Mapping to the Original
Of the 200 .js files in the original src/, 182 were ported. (176 under the same name, 6 merged into other files) The remaining 18 were left out on purpose: empty files, dead code, things the Rust language and standard library cover (object model helpers, collections), and what another feature covers (deprecated/camera.js, covered by Camera2D).
What exists only in the Rust port is the work the browser does in the original: the platform layer, text shaping and color emoji, image decoding, the text input line, node storage, the async executor, and browser stand-ins (Canvas2D, dialogs, translations of text the browser draws).
Differences Between the Two Engines
The two engines keep no feature differences. The remaining differences are only what truly cannot match because of the language or platform, and forms chosen on purpose to fit Rust.
What truly cannot match
- Reflection (reflective saving and loading in UI documents, the getter list in the developer tools inspector) works only for types marked with
#[vanilla::reflect]. All engine components are marked; game components put it on theirimplblock andimpl Componentblock. Rust has no way to scan methods at run time. - The memory field in the desktop developer tools shows the system allocator's heap instead of the JavaScript heap.
- On desktop, glyph raster pixels and shape edge antialiasing differ from Chrome. (Text widths and shape interior colors are the same)
- On desktop, the last digits of Opus and AAC sound samples differ very slightly from Chrome. (Length and trimming are the same)
Forms chosen on purpose
| Difference | Reason |
|---|---|
The global engine Engine::get_engine() is taken anywhere right after Engine::new, like the original. Scene functions also receive it as an argument and components through ComponentContext (the same engine) |
All engine functions are &self and the fields they change use interior mutability (Cell, RefCell). Managers are lent as Ref and RefMut |
Nodes live in NodeTree and are referenced by NodeId |
Pointing parents and children at each other with Rc<RefCell> brings reference cycles and run-time borrow checks |
There is one node type, WorldNode |
Rust has no class inheritance, so the node is one form and features are attached as components |
Traits and embedding instead of inheritance (Scene + SceneBase, Component + ComponentBase) |
Shared state goes in structs, overrides in trait functions |
Number types are split: logic numbers are f64 like the original JS number, and f32 is used where the original stores into a Float32Array (Matrix4 elements, GPU vertices and uniforms) or where Chrome computes in float (inside Canvas2D, audio, glyph rasterization, image decoding, color management) |
JS has only one number type, while Rust picks one. Logic is computed in the same double precision as the original so results match to the last digit, and the f32 places use the precision the original and Chrome actually use |
| Objects that use GL do not hold the context; each function receives gl | The context is held by Graphic, and sharing it would tangle lifetimes and borrows |
Some places have separate *_in_tree functions that take the tree |
While scene code holds a component, it cannot also borrow other nodes in the same tree |
Option structs with defaults instead of optional argument objects (RenderTargetOptions, UIConfirmWindowOptions and others) |
Rust has no named optional arguments |
Callbacks are taken as Box<dyn FnMut>, and callback getters return Rc<RefCell> |
The engine and the caller share the same function |
Result and Option instead of exceptions |
Rust does not throw exceptions |
| Names follow Rust conventions (snake_case, UpperCamelCase, SCREAMING_SNAKE_CASE) | Following Rust API Guidelines C-CASE |
Async tasks touch the scene and engine only inside the context's with |
Rust futures cannot hold &mut across an await |
Renaming and ownership rules from the porting side are in the "Porting from vanilla.js" chapter.
Related Types
Engine, EngineConfiguration, Scene, SceneBase, SceneId, NodeTree, NodeId, WorldNode, Component, ComponentBase, ComponentContext, Graphic, EngineContext, SceneContext, TaskHandle, Promise