BabylonNative Documentation
repository·master·Indexed 21 days ago
https://github.com/babylonjs/babylonnativeBabylon Native enables developers to run Babylon.js JavaScript code in native environments including Windows, macOS, iOS, Android, and Linux. The library includes tools for headless rendering via HeadlessScreenshotApp, shader validation with PrecompiledShaderTest, and native graphics integration through the ExternalTexture plugin for passing native API texture objects (such as D3D12 resources) into the Babylon engine.
What's inside BabylonNative
- Babylon Native is a collection of technologies designed to bring Babylon.js capabilities to cross-platform applications outside of a web browser. It enables the same JavaScript code used in Babylon.js web applications to run identically on native platforms including Windows, macOS, iOS, Android, and Linux.
What is Shader Tool
masterShader Tool is a cross-platform console utility used to compile GLSL vertex and fragment shaders for a target graphics API. It utilizes Babylon Native'sShaderCompilerfor compilation andShaderCacheto save the results. It is specifically designed to be integrated into automated build processes, allowing you to bundle multiple shader pairs into a single compiled cache file.Overview of StyleTransferApp
masterStyleTransferApp is a demonstration Windows application that showcases how to integrate Windows Machine Learning APIs with Babylon Native. It applies Neural Style Transfers effects directly to the rendered output of a Babylon Native scene.Use the ExternalTexture plugin to provide native textures
masterTheExternalTextureplugin library allows you to pass native graphics API texture objects (like D3D12 resources) into Babylon Native. This enables BabylonJS to use textures created by your native hosting application. By usingBabylon::Plugins::ExternalTexture, you can generate JavaScript objects that the Babylon engine can consume as textures.Understand Babylon Native Polyfills
masterBabylon Native Polyfills provide implementations for specific JavaScript browser APIs that are required by Babylon.js or commonly used by its developers.
Important Limitations:
- They do not attempt to fulfill the full web specification exactly.
- They do not mirror the exact behavior of web browsers.
- They are in an early stage and are subject to change.
Currently supported polyfills include:
CanvasConsoleWindowXMLHttpRequest
What is AppRuntime and how does it relate to JsRuntime
masterIn Babylon Native,
JsRuntimeis an abstraction that provides an integration point for interacting with a JavaScript engine and its dedicated thread, but it does not provide the implementation itself.AppRuntimeis the canonical implementation that underlies aJsRuntime. It manages the entire lifecycle and infrastructure required for a Babylon Native app, including:- A dedicated thread for the JavaScript engine instance.
- The JavaScript engine instance itself.
- A
JsRuntimeto provide access to these resources.
For most dedicated Babylon Native scenarios, creating an
AppRuntimeis the recommended way to control JavaScript in a manner that is easily consumable by other Babylon Native components.Understand the role of NativeEngine
masterThe
NativeEnginecomponent is the core rendering bridge in Babylon Native. WhileJsRuntimeandAppRuntimeprovide the infrastructure for running web logic on native,NativeEngineis the specific component that interfaces with Babylon.js to power its rendering capabilities.Key characteristics:
- It implements the Babylon.js
Engineabstraction. - It allows Babylon.js code written for the web to run on native platforms without modification.
- It acts as a translation layer between the high-level Babylon.js
Engineand low-level native graphics APIs.
- It implements the Babylon.js
How shader transpilation works in Babylon Native
masterBabylon Native's
NativeEnginebridges the gap between Babylon.js's ESSL shaders (designed for WebGL) and the native shader languages required by the underlyingbgfxgraphics abstraction (HLSL, GLSL, MSL, etc.).To achieve this,
NativeEngineperforms runtime transpilation by converting ESSL source code into platform-specific native shaders. This allows developers to use standard Babylon.js shader code while running on native platforms like DirectX, OpenGL, or Metal.Understand Babylon Native component categories
masterBabylon Native uses a modular, componentized architecture organized into four principle categories. This structure supports lateral dependency management, allowing components to be assembled, excluded, or replaced easily.
Dependencies: Contains external or pre-existing code that may not follow Babylon Native's lateral patterns. It includes "adapter" mechanisms (viaCMakeLists.txt) to allow these external libraries to be consumed as standard CMake targets by other components.Core: Foundational components that expose functionality to the native (C++) layer. They must have no dependencies outside ofCoreandDependencies. An example isJsRuntime.Plugins: Components that expose functionality to the JavaScript layer, often by exposing new types (e.g., viaN-API). They can have many dependencies acrossPlugins,Core, andDependencies. TheNativeEngineis the primary plugin.Polyfills: Components that provide implementations for pre-existing JavaScript capabilities (likeconsole.logor browser features).
Critical Constraint: No Babylon Native C++ component is allowed to depend on a
Polyfill.How NativeEngine handles native graphics via bgfx
masterTo support multiple native platforms,
NativeEngineuses bgfx (along withbimgandbx) as its underlying graphics abstraction.Benefits and Implementation
- Abstraction:
bgfxallowsNativeEngineto write a single translation layer that connects the Babylon.jsEngineabstraction to various native graphics APIs (targeting every major native graphics API). - Shader Transpilation: Because bgfx uses native shader languages,
NativeEngineincludes a specialized mechanism to transpile Babylon.js WebGL shaders into the appropriate languages for the target platform. This is handled internally and does not affect the public-facing API.
- Abstraction:
How NativeEngine integrates with Babylon.js
masterUnlike a WebGL polyfill that attempts to mimic the WebGL API at the lowest level, Babylon Native integrates at the
Enginelevel. This approach minimizes the integration surface area and allows for customizations on both the JavaScript and native sides.The Integration Flow
- Initialization: During startup, the initializer exposes a C++ N-API constructor for the native
NativeEnginetype to JavaScript. - JavaScript Layer: Developers use the
NativeEngineJavaScript type. This type is aware of the native constructor and manages a nativeNativeEngineinstance internally. - Execution: The JavaScript
NativeEngineuses the backing C++ instance to execute all rendering work required by a Babylon.jsEngine.
Because this integration happens at the
Engineabstraction level, code targeting traditional web-based engines can typically run on Babylon Native without any changes.- Initialization: During startup, the initializer exposes a C++ N-API constructor for the native
Implement JsRuntime for different app architectures
masterBabylon Native supports two primary integration patterns via
JsRuntime:- Owning (Canonical): The app is a dedicated Babylon Native app where a component (like
AppRuntime) owns both the JavaScript engine and the thread it runs on. - Piggybacking: Babylon Native is integrated into an existing infrastructure, such as Babylon React Native. In this mode, the
JsRuntimeis implemented to interface with a pre-existing JavaScript engine and thread managed by the host (e.g., React Native's engine), allowing Babylon Native components to function without controlling the host's environment.
- Owning (Canonical): The app is a dedicated Babylon Native app where a component (like