BabylonNative Documentation

repository·master·Indexed 21 days ago

https://github.com/babylonjs/babylonnative

Babylon 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.

Tokens
22.8K
Snippets
51
Records
111
Agent score
72%

What's inside BabylonNative

  1. Overview of Babylon Native

    master
    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.
  2. What is Shader Tool

    master
    Shader Tool is a cross-platform console utility used to compile GLSL vertex and fragment shaders for a target graphics API. It utilizes Babylon Native's ShaderCompiler for compilation and ShaderCache to 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.
  3. Use the ExternalTexture plugin to provide native textures

    master
    The ExternalTexture plugin 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 using Babylon::Plugins::ExternalTexture, you can generate JavaScript objects that the Babylon engine can consume as textures.
  4. Understand Babylon Native Polyfills

    master

    Babylon 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:

    • Canvas
    • Console
    • Window
    • XMLHttpRequest
  5. What is AppRuntime and how does it relate to JsRuntime

    master

    In Babylon Native, JsRuntime is 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.

    AppRuntime is the canonical implementation that underlies a JsRuntime. 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 JsRuntime to provide access to these resources.

    For most dedicated Babylon Native scenarios, creating an AppRuntime is the recommended way to control JavaScript in a manner that is easily consumable by other Babylon Native components.

  6. Understand the role of NativeEngine

    master

    The NativeEngine component is the core rendering bridge in Babylon Native. While JsRuntime and AppRuntime provide the infrastructure for running web logic on native, NativeEngine is the specific component that interfaces with Babylon.js to power its rendering capabilities.

    Key characteristics:

    • It implements the Babylon.js Engine abstraction.
    • 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 Engine and low-level native graphics APIs.
  7. How shader transpilation works in Babylon Native

    master

    Babylon Native's NativeEngine bridges the gap between Babylon.js's ESSL shaders (designed for WebGL) and the native shader languages required by the underlying bgfx graphics abstraction (HLSL, GLSL, MSL, etc.).

    To achieve this, NativeEngine performs 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.

  8. Understand Babylon Native component categories

    master

    Babylon 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 (via CMakeLists.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 of Core and Dependencies. An example is JsRuntime.
    • Plugins: Components that expose functionality to the JavaScript layer, often by exposing new types (e.g., via N-API). They can have many dependencies across Plugins, Core, and Dependencies. The NativeEngine is the primary plugin.
    • Polyfills: Components that provide implementations for pre-existing JavaScript capabilities (like console.log or browser features).

    Critical Constraint: No Babylon Native C++ component is allowed to depend on a Polyfill.

  9. How NativeEngine handles native graphics via bgfx

    master

    To support multiple native platforms, NativeEngine uses bgfx (along with bimg and bx) as its underlying graphics abstraction.

    Benefits and Implementation

    • Abstraction: bgfx allows NativeEngine to write a single translation layer that connects the Babylon.js Engine abstraction to various native graphics APIs (targeting every major native graphics API).
    • Shader Transpilation: Because bgfx uses native shader languages, NativeEngine includes 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.
  10. How NativeEngine integrates with Babylon.js

    master

    Unlike a WebGL polyfill that attempts to mimic the WebGL API at the lowest level, Babylon Native integrates at the Engine level. This approach minimizes the integration surface area and allows for customizations on both the JavaScript and native sides.

    The Integration Flow

    1. Initialization: During startup, the initializer exposes a C++ N-API constructor for the native NativeEngine type to JavaScript.
    2. JavaScript Layer: Developers use the NativeEngine JavaScript type. This type is aware of the native constructor and manages a native NativeEngine instance internally.
    3. Execution: The JavaScript NativeEngine uses the backing C++ instance to execute all rendering work required by a Babylon.js Engine.

    Because this integration happens at the Engine abstraction level, code targeting traditional web-based engines can typically run on Babylon Native without any changes.

  11. Implement JsRuntime for different app architectures

    master

    Babylon 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 JsRuntime is 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.