### Run developer setup script Source: https://github.com/babylonjs/babylonreactnative/blob/master/README.md From a fresh checkout, run the developer setup script. This installs build tools and dependencies, downloads the pinned Babylon Native commit, compiles the TypeScript module, and on macOS generates the iOS Xcode project. ```powershell cd Package node setup_dev.js ``` -------------------------------- ### Install packaging tools in Package directory Source: https://github.com/babylonjs/babylonreactnative/blob/master/README.md Run from a fresh checkout to install the packaging tools. Uses npm ci in the Package folder. ```powershell cd Package npm ci ``` -------------------------------- ### useEngine hook with scene setup in useEffect Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Creates an engine asynchronously via useEngine. The engine is initially undefined, so scene initialization should occur in a useEffect triggered by the engine state change. The engine is disposed when the owning component unmounts; dispose scenes in the effect cleanup. ```tsx import React, { useEffect } from 'react'; import { useEngine } from '@babylonjs/react-native'; import { Scene } from '@babylonjs/core'; const MyComponent = () => { const engine = useEngine(); useEffect(() => { if (engine) { const scene = new Scene(engine); // Setup the scene! return () => scene.dispose(); } }, [engine]); return null; } ``` -------------------------------- ### Install Linux build tools (Ubuntu) Source: https://github.com/babylonjs/babylonreactnative/blob/master/README.md Required for Android development on Ubuntu. Installs ninja-build, OpenJDK 17, and CMake. ```sh sudo apt-get install ninja-build openjdk-17-jdk cmake ``` -------------------------------- ### Prepare Windows-specific Playground manifests Source: https://github.com/babylonjs/babylonreactnative/blob/master/README.md Run from repository root before developer setup for UWP. Copies Windows package manifests and runs setup and generation scripts. These copies change tracked files locally. ```powershell Copy-Item Apps\Playground\package-windows.json Apps\Playground\package.json -Force Copy-Item Apps\Playground\package-lock-windows.json Apps\Playground\package-lock.json -Force cd Package node setup_dev.js cd ..\Apps\Playground node generate-windows-test-app.mjs ``` -------------------------------- ### EngineView with camera from scene Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Renders a Babylon scene's camera in a React Native view. Assign a camera to the EngineView via the camera prop. The example creates a default camera, light, and a box, then sets the active camera. Dispose the scene in the effect cleanup. ```tsx import React, { useEffect, useState } from 'react'; import { useEngine, EngineView } from '@babylonjs/react-native'; import { Scene, Camera, MeshBuilder } from '@babylonjs/core'; const MyComponent = () => { const engine = useEngine(); const [camera, setCamera] = useState(); useEffect(() => { if (engine) { const scene = new Scene(engine); scene.createDefaultCamera(true); scene.createDefaultLight(true); MeshBuilder.CreateBox('box', {}, scene); setCamera(scene.activeCamera!); return () => scene.dispose(); } }, [engine]); return ( <> ); } ``` -------------------------------- ### Set iOS Podfile platform Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Set the Podfile platform to at least 16.0 to match the module's minimum deployment target. Run pod install from the app's ios directory after npm install. ```ruby platform :ios, '16.0' ``` -------------------------------- ### Install iOS CocoaPods dependencies Source: https://github.com/babylonjs/babylonreactnative/blob/master/README.md On macOS, install the CocoaPods dependencies separately before launching the iOS app. Then open the generated workspace in Xcode. ```powershell cd ..\Apps\Playground\ios pod install ``` -------------------------------- ### Regenerate native project Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Regenerate the native project on macOS from the app root if project generation was skipped or settings changed after installation. Unset BABYLON_NO_CMAKE_POSTINSTALL first if enabled, then rerun pod install. ```sh node node_modules/@babylonjs/react-native/postinstall.js ``` -------------------------------- ### Install module dependencies with BABYLON_NO_CMAKE_POSTINSTALL Source: https://github.com/babylonjs/babylonreactnative/blob/master/README.md Installs module dependencies while skipping iOS project generation until native sources are assembled. Sets the environment variable BABYLON_NO_CMAKE_POSTINSTALL to 1, then removes it after npm ci. In macOS/Linux shells, use export and unset instead of PowerShell commands. ```powershell cd ..\Modules\@babylonjs\react-native $env:BABYLON_NO_CMAKE_POSTINSTALL = "1" npm ci Remove-Item Env:\BABYLON_NO_CMAKE_POSTINSTALL ``` -------------------------------- ### Install @babylonjs/react-native with peer dependencies Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Install the latest stable release (2.0.8) along with required peer dependencies. Use the React version required by your React Native app; React Native 0.81 requires Node.js 20.19.4 or later. For glTF support, also install @babylonjs/loaders at the same version as @babylonjs/core. ```sh npm install @babylonjs/react-native@2.0.8 @babylonjs/core@^9.0.0 react-native-permissions@^5.4.1 ``` -------------------------------- ### Disable post install CMake generation Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Set this environment variable before running npm install to disable post install CMake generation. ```sh export BABYLON_NO_CMAKE_POSTINSTALL=1 ``` -------------------------------- ### Use system CMake Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Since 2.0.3, set this variable before running npm install to use a system CMake instead of the cmake-runtime 3.31.0 dependency. ```sh export BABYLON_USE_SYSTEM_CMAKE=1 ``` -------------------------------- ### Add ReactNativeBabylon.xcodeproj to workspace Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Add a reference to the Babylon React Native project in the app's .xcworkspace/contents.xcworkspacedata file. The path assumes the workspace is in the app's ios directory. ```xml ``` -------------------------------- ### useEngine Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md A custom React hook that manages the lifecycle of a Babylon engine instance asynchronously. It returns undefined initially and disposes the engine when the owning component unmounts. ```APIDOC ## useEngine ### Description A custom React hook that manages the lifecycle of a Babylon engine instance in the context of an owning React component. It creates an engine instance asynchronously, returns undefined initially, and disposes the engine when the component unmounts. ### Usage Typically scene initialization code should exist in a `useEffect` triggered by an `engine` state change. ### Request Example ```tsx import React, { useEffect } from 'react'; import { useEngine } from '@babylonjs/react-native'; import { Scene } from '@babylonjs/core'; const MyComponent = () => { const engine = useEngine(); useEffect(() => { if (engine) { const scene = new Scene(engine); // Setup the scene! return () => scene.dispose(); } }, [engine]); return null; } ``` ``` -------------------------------- ### Disable NativeCamera plugin Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Set this environment variable before generating/building the native project to disable the NativeCamera plugin (enabled by default). Keep the variable set when running pod install and native builds. ```sh export BABYLON_NATIVE_PLUGIN_NATIVECAMERA=0 ``` -------------------------------- ### Disable NativeXR plugin Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Set this environment variable before generating/building the native project to disable the NativeXR plugin (enabled by default). Keep the variable set when running pod install and native builds. ```sh export BABYLON_NATIVE_PLUGIN_NATIVEXR=0 ``` -------------------------------- ### EngineView Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md A custom React Native view that presents a camera from a Babylon scene. It accepts a camera prop and optional configuration flags for transparency, anti-aliasing, and frame rate display. ```APIDOC ## EngineView ### Description A custom React Native view that presents a `camera` from a Babylon `scene`. A `camera` is assigned to the `EngineView`. It supports transparency, anti-aliasing, and an FPS overlay. ### Properties - **camera** (Camera) - Required - The camera to present. - **isTransparent** (boolean) - Optional - Defines whether the background of the scene should be transparent or not. - **antiAliasing** (number) - Optional - Set to 0 or 1 (disabled; default is 0), or to 2, 4, 8, or 16 (MSAA sample count). - **displayFrameRate** (boolean) - Optional - Controls the FPS overlay (enabled by default in development builds). - **onInitialized** (callback) - Optional - On Android and iOS, receives an `EngineViewCallbacks` object with `takeSnapshot(): Promise` returning a base64-encoded JPEG. Not implemented on Windows. - **androidView** (string) - Optional - Android-specific property to set the type of view used for rendering (TextureView, SurfaceView, SurfaceViewZTopMost, SurfaceViewZMediaOverlay). ### Request Example ```tsx import React, { useEffect, useState } from 'react'; import { useEngine, EngineView } from '@babylonjs/react-native'; import { Scene, Camera, MeshBuilder } from '@babylonjs/core'; const MyComponent = () => { const engine = useEngine(); const [camera, setCamera] = useState(); useEffect(() => { if (engine) { const scene = new Scene(engine); scene.createDefaultCamera(true); scene.createDefaultLight(true); MeshBuilder.CreateBox('box', {}, scene); setCamera(scene.activeCamera!); return () => scene.dispose(); } }, [engine]); return ( <> ); } ``` ### Additional Examples ```tsx ``` ```tsx ``` ``` -------------------------------- ### Override iOS deployment target Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Since 2.0.4, set this variable before running npm install to override the iOS deployment target used by the generated ReactNativeBabylon.xcodeproj (defaults to 16.0). The previous iOS 12 default caused Xcode 26's Clang to crash while compiling DeviceImpl_iOS.mm. ```sh export BABYLON_IOS_DEPLOYMENT_TARGET=16.0 ``` -------------------------------- ### EngineView with antiAliasing set to 4 Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Configures anti-aliasing on EngineView. Set antiAliasing to 0 or 1 to disable (default is 0), or to 2, 4, 8, or 16 for MSAA sample count. ```tsx ``` -------------------------------- ### EngineView with isTransparent flag Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Sets the isTransparent boolean flag on EngineView to control whether the scene background is transparent. Only one EngineView can be active at a time. ```tsx ``` -------------------------------- ### Override ARCore version in build.gradle Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/README.md Set the ARCore version used by the module. Defaults to 1.22.0; since 2.0.4 consuming apps can override it in their root android/build.gradle. The Playground uses 1.56.0. ```groovy ext.arcoreVersion = '1.56.0' ``` -------------------------------- ### Configure shell with Android SDK and JDK paths Source: https://github.com/babylonjs/babylonreactnative/blob/master/README.md Sets ANDROID_HOME, PATH, and JAVA_HOME for Android development. Adjust JAVA_HOME for your distribution and architecture. ```sh export ANDROID_HOME="$HOME/Android/Sdk" export PATH="$PATH:$ANDROID_HOME/emulator:$ANDROID_HOME/platform-tools" export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 ``` -------------------------------- ### Build assembled package and create tarball Source: https://github.com/babylonjs/babylonreactnative/blob/master/README.md Downloads and assembles the Babylon Native source tree, copies package files, compiles TypeScript, validates contents, and runs npm pack. The resulting .tgz is placed in Package\Assembled. ```powershell cd ..\..\..\Package npx gulp buildAssembled cd Assembled npm pack ``` -------------------------------- ### Launch Android Playground app Source: https://github.com/babylonjs/babylonreactnative/blob/master/README.md From the Package directory, launch the Android Playground app using react-native-test-app. ```powershell cd ..\Apps\Playground npx react-native run-android ``` -------------------------------- ### Configure local.properties for Android SDK and NDK Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/README.md Required local.properties file content for Android SDK and NDK paths. Replace {username} with your actual username. ```properties ndk.dir=/Users/{username}/Library/Android/sdk/ndk-bundle sdk.dir=/Users/{username}/Library/Android/sdk ``` -------------------------------- ### Clone the BabylonReactNative repository Source: https://github.com/babylonjs/babylonreactnative/blob/master/README.md Step 1 for all development environments and targets. Use a git-enabled terminal to clone the repo and change into the directory. ```powershell git clone https://github.com/BabylonJS/BabylonReactNative cd BabylonReactNative ``` -------------------------------- ### Set Android environment variables in ~/.zshrc Source: https://github.com/babylonjs/babylonreactnative/blob/master/README.md Add these exports to ~/.zshrc for Android development on macOS. Uses zsh, not PowerShell. Ensure CMake and Ninja are also on PATH. ```sh export ANDROID_HOME="$HOME/Library/Android/sdk" export PATH="$PATH:$ANDROID_HOME/emulator:$ANDROID_HOME/platform-tools" export JAVA_HOME=$(/usr/libexec/java_home -v 17) ``` -------------------------------- ### Enable Android 16kb native library alignment and compile definitions Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Conditionally adds linker option for 16kb page size when NDK version is less than 27, and sets compile definitions for native XR and camera plugins. ```CMake # Enable Android 16kb native library alignment if(CMAKE_ANDROID_NDK_VERSION VERSION_LESS "27") target_link_options(${PACKAGE_NAME} PRIVATE "-Wl,-z,max-page-size=16384") endif() target_compile_definitions(${PACKAGE_NAME} PRIVATE BABYLON_NATIVE_PLUGIN_NATIVEXR=${BABYLON_NATIVE_PLUGIN_NATIVEXR}) target_compile_definitions(${PACKAGE_NAME} PRIVATE BABYLON_NATIVE_PLUGIN_NATIVECAMERA=${BABYLON_NATIVE_PLUGIN_NATIVECAMERA}) ``` -------------------------------- ### Configure include directories for PACKAGE_NAME Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Adds private include directories for React Native headers and Babylon Native component targets, including callinvoker, jsi, and various JNI paths, plus directories from Graphics, NativeEngine, and other targets. ```cmake target_include_directories( ${PACKAGE_NAME} PRIVATE "${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker" "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi" "${NODE_MODULES_DIR}/react-native/ReactCommon" "${NODE_MODULES_DIR}/react-native/ReactCommon/react/nativemodule/core" "${NODE_MODULES_DIR}/react-native/ReactCommon/runtimeexecutor" "${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni" "${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../shared" PRIVATE $ PRIVATE $ PRIVATE $ PRIVATE $ PRIVATE $ PRIVATE $ PRIVATE $ PRIVATE $ PRIVATE $ PRIVATE $ PRIVATE $ PRIVATE $ PRIVATE $ ${libfbjni_include_DIRS} ) ``` -------------------------------- ### Link libraries for PACKAGE_NAME Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Links COMMON_LIBS and Babylon Native component libraries (AndroidExtensions, Graphics, NativeEngine, etc.) plus GLESv3 and EGL to the package target. ```cmake target_link_libraries(${PACKAGE_NAME} ${COMMON_LIBS} AndroidExtensions Graphics NativeEngine Canvas XMLHttpRequest NativeInput NativeCapture NativeOptimizations NativeTracing Window NativeXr ShaderCache ShaderCompiler NativeCamera -lGLESv3 -lEGL ) ``` -------------------------------- ### Set TURBOMODULES_LIB based on React Native version Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Selects the turbomodulejsijni library. For RN 0.76+ does nothing (library not available), for RN 0.71+ uses ReactAndroid::turbomodulejsijni, otherwise finds turbomodulejsijni via find_library. ```cmake unset(TURBOMODULES_LIB CACHE) if(${REACT_NATIVE_VERSION} GREATER_EQUAL 76) # RN 0.76 doesn't have turbomodulejsijni # DO NOTHING, we'll not link these libraries elseif(${REACT_NATIVE_VERSION} GREATER_EQUAL 71) # RN 0.71 distributes prebuilt binaries. set (TURBOMODULES_LIB "ReactAndroid::turbomodulejsijni") else() find_library( TURBOMODULES_LIB turbomodulejsijni PATHS ${LIBRN_DIR} NO_CMAKE_FIND_ROOT_PATH ) endif() message("-- TURBO : " ${TURBOMODULES_LIB}) ``` -------------------------------- ### Enable RTTI and disable warnings for Babylon Native targets Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Enables RTTI for the shared Babylon Native directory and disables warnings for various NativeXr, NativeCamera, and other targets. Includes deprecated stl warnings for NativeOptimizations, NativeTracing, JsRuntime, and Scheduling. ```cmake # React Native's JSI headers use dynamic_cast, including from templates # instantiated by BabylonNative targets. enable_babylon_native_rtti("${BABYLON_REACT_NATIVE_SHARED_DIR}") if (TARGET NativeXr) disable_warnings(NativeXr) endif() if (TARGET NativeCamera) disable_warnings(NativeCamera) endif() disable_warnings(XMLHttpRequest) disable_warnings(Graphics) disable_warnings(NativeEngine) disable_warnings(NativeInput) disable_warnings(NativeCapture) disable_warnings(NativeOptimizations) # deprecated stl disable_warnings(NativeTracing) # deprecated stl disable_warnings(JsRuntime) # deprecated stl disable_warnings(Scheduling) # deprecated stl disable_warnings(Canvas) disable_warnings(Window) ``` -------------------------------- ### Add shared library target with BabylonNativeInterop.cpp Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Creates a shared library target named PACKAGE_NAME from BabylonNativeInterop.cpp and BABYLON_SHARED sources, then disables warnings for it due to deprecated STL usage. ```cmake add_library( ${PACKAGE_NAME} SHARED "${CMAKE_CURRENT_SOURCE_DIR}/src/main/cpp/BabylonNativeInterop.cpp" ${BABYLON_SHARED} ) disable_warnings(${PACKAGE_NAME}) # deprecated stl ``` -------------------------------- ### Set REACTNATIVEJNI_LIB based on React Native version Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Selects the reactnativejni library. For RN 0.76+ does nothing (library not available), for RN 0.71+ uses ReactAndroid::reactnativejni, otherwise finds reactnativejni via find_library. ```cmake unset(REACTNATIVEJNI_LIB CACHE) if(${REACT_NATIVE_VERSION} GREATER_EQUAL 76) # RN 0.76 doesn't have reactnativejni # DO NOTHING, we'll not link these libraries elseif(${REACT_NATIVE_VERSION} GREATER_EQUAL 71) # RN 0.71 distributes prebuilt binaries. set (REACTNATIVEJNI_LIB "ReactAndroid::reactnativejni") else() find_library( REACTNATIVEJNI_LIB reactnativejni PATHS ${LIBRN_DIR} NO_CMAKE_FIND_ROOT_PATH ) endif() message("-- REACTNATIVEJNI : " ${REACTNATIVEJNI_LIB}) ``` -------------------------------- ### Set FBJNI_LIBRARY based on React Native version Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Selects the FBJNI library. For RN 0.71+ uses fbjni::fbjni, otherwise finds fbjni via find_library with paths from libfbjni_link_DIRS. ```cmake unset(FBJNI_LIBRARY CACHE) if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71) # RN 0.71 distributes prebuilt binaries. set (FBJNI_LIBRARY fbjni::fbjni) else() find_library( FBJNI_LIBRARY fbjni PATHS ${libfbjni_link_DIRS} NO_CMAKE_FIND_ROOT_PATH ) endif() message("-- FBJNI : " ${FBJNI_LIBRARY}) ``` -------------------------------- ### CMake configuration for ReactNativeBabylon Android build Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Configures the CMake build for the ReactNativeBabylon Android module. Sets C++20 standard, locates fbjni and JSI libraries based on React Native version, and includes Babylon Native shared directory. Handles RN version-specific JSI linking and enables RTTI for Babylon Native targets. ```cmake cmake_minimum_required(VERSION 3.13.2) project(ReactNativeBabylon) set (CMAKE_VERBOSE_MAKEFILE ON) set (CMAKE_CXX_STANDARD 20) set (PACKAGE_NAME "ReactNativeBabylon") # Clear some variables unset(LIBRN_DIR CACHE) unset(libfbjni_link_DIRS CACHE) unset(libfbjni_include_DIRS CACHE) set(build_DIR ${CMAKE_SOURCE_DIR}/build) set(input_SRC src/main/cpp/BabylonNativeInterop.cpp) file(GLOB libfbjni_link_DIRS "${build_DIR}/fbjni*.aar/jni/${ANDROID_ABI}") file(GLOB libfbjni_include_DIRS "${build_DIR}/fbjni-*-headers.jar/") message("-- ABI : " ${ANDROID_ABI}) message("-- BUILD : " ${build_DIR}) message("-- LIBRN : " ${LIBRN_DIR}) link_directories(../libs/android/${ANDROID_ABI}/) # Configure Babylon Native to use JSI set(NAPI_JAVASCRIPT_ENGINE "JSI" CACHE STRING "The JavaScript engine to power N-API") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/src/") if(${REACT_NATIVE_VERSION} LESS 66) file( TO_CMAKE_PATH "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp" INCLUDE_JSI_CPP ) endif() find_library( LOG_LIB log ) message("-- LOG : " ${LOG_LIB}) if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71) # We need to find packages since from RN 0.71 binaries are prebuilt find_package(fbjni REQUIRED CONFIG) find_package(ReactAndroid REQUIRED CONFIG) endif() unset(JSI_LIB CACHE) if(${REACT_NATIVE_VERSION} LESS 66) # JSI lib didn't exist on RN 0.65 and before. Simply omit it. set (JSI_LIB "") elseif(${REACT_NATIVE_VERSION} GREATER_EQUAL 71) # RN 0.71 distributes prebuilt binaries. set (JSI_LIB ReactAndroid::jsi) else() # RN 0.66 distributes libjsi.so, can be used instead of compiling jsi.cpp manually. find_library( JSI_LIB jsi PATHS ${LIBRN_DIR} NO_CMAKE_FIND_ROOT_PATH ) endif() message("-- JSI : " ${JSI_LIB}) if(${REACT_NATIVE_VERSION} GREATER_EQUAL 71) file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/jsi_bridge.cpp" "") add_library(jsi STATIC "${CMAKE_CURRENT_BINARY_DIR}/jsi_bridge.cpp") target_link_libraries(jsi PUBLIC ReactAndroid::jsi) endif() set(BABYLON_NATIVE_BUILD_APPS OFF CACHE BOOL "") set(BABYLON_REACT_NATIVE_SHARED_DIR "${CMAKE_CURRENT_LIST_DIR}/../shared") set(BABYLON_NATIVE_PLUGIN_NATIVEENGINE_WEBP OFF CACHE BOOL "") add_subdirectory(${BABYLON_REACT_NATIVE_SHARED_DIR} ${CMAKE_CURRENT_BINARY_DIR}/shared) function(enable_babylon_native_rtti directory) get_property(targets DIRECTORY "${directory}" PROPERTY BUILDSYSTEM_TARGETS) foreach(target IN LISTS targets) get_target_property(target_type ${target} TYPE) get_target_property(imported ${target} IMPORTED) if(NOT imported AND target_type MATCHES "^(STATIC_LIBRARY|SHARED_LIBRARY|MODULE_LIBRARY|OBJECT_LIBRARY|EXECUTABLE)$") target_compile_options(${target} PRIVATE $<$:-frtti>) endif() endforeach() get_property(subdirectories DIRECTORY "${directory}" PROPERTY SUBDIRECTORIES) foreach(subdirectory IN LISTS subdirectories) enable_babylon_native_rtti("${subdirectory}") endforeach() endfunction() ``` -------------------------------- ### Glob shared files and set REACT_LIB based on React Native version Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Globs shared source files and selects the React library based on REACT_NATIVE_VERSION. For RN 0.76+ uses ReactAndroid::reactnative, for RN 0.71+ uses ReactAndroid::react_nativemodule_core, otherwise finds react_nativemodule_core via find_library. ```cmake file(GLOB BABYLON_SHARED "${CMAKE_CURRENT_SOURCE_DIR}/../shared/*") unset(REACT_LIB CACHE) if(${REACT_NATIVE_VERSION} GREATER_EQUAL 76) # RN 0.76 packs react_nativemodule_core into ReactAndroid::reactnative set (REACT_LIB ReactAndroid::reactnative) elseif(${REACT_NATIVE_VERSION} GREATER_EQUAL 71) # RN 0.71 distributes prebuilt binaries. set (REACT_LIB ReactAndroid::react_nativemodule_core) else() find_library( REACT_LIB react_nativemodule_core PATHS ${LIBRN_DIR} NO_CMAKE_FIND_ROOT_PATH ) endif() message("-- REACT : " ${REACT_LIB}) ``` -------------------------------- ### Set RUNTIMEEXECUTOR_LIB based on React Native version Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Selects the runtimeexecutor library. For RN 0.76+ does nothing (library not available), for RN 0.71+ uses ReactAndroid::runtimeexecutor, otherwise finds runtimeexecutor via find_library. ```cmake unset(RUNTIMEEXECUTOR_LIB CACHE) if(${REACT_NATIVE_VERSION} GREATER_EQUAL 76) # RN 0.76 doesn't have runtimeexecutor # DO NOTHING, we'll not link these libraries elseif(${REACT_NATIVE_VERSION} GREATER_EQUAL 71) # RN 0.71 distributes prebuilt binaries. set (RUNTIMEEXECUTOR_LIB "ReactAndroid::runtimeexecutor") else() find_library( RUNTIMEEXECUTOR_LIB runtimeexecutor PATHS ${LIBRN_DIR} NO_CMAKE_FIND_ROOT_PATH ) endif() message("-- RUNTIMEEXECUTOR : " ${RUNTIMEEXECUTOR_LIB}) ``` -------------------------------- ### Add REACT_NATIVE_VERSION definition Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Adds a compile definition for REACT_NATIVE_VERSION so the code can conditionally compile based on the React Native version. ```cmake add_definitions(-DREACT_NATIVE_VERSION=${REACT_NATIVE_VERSION}) ``` -------------------------------- ### Set RN_SERIALIZABLE_STATE and C++ flags for React Native 0.81+ Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt For React Native 80 and above, includes react-native-flags.cmake and calls target_compile_reactnative_options. For older versions, appends manual C++ flags including -fexceptions, -frtti, and various warning flags. ```cmake # Set RN_SERIALIZABLE_STATE and C++ flags for React Native 0.81+ if(${REACT_NATIVE_VERSION} GREATER_EQUAL 80) include("${REACTNATIVE_DIR}/ReactCommon/cmake-utils/react-native-flags.cmake") target_compile_reactnative_options(${PACKAGE_NAME} PRIVATE) else() string(APPEND CMAKE_CXX_FLAGS " -fexceptions -frtti -std=c++${CMAKE_CXX_STANDARD} -Wall -Werror -Wunused-function -Wunused-private-field -Woverloaded-virtual -Wreorder-ctor -Wdelete-non-abstract-non-virtual-dtor -Wmismatched-tags -Wunused-variable -Wpessimizing-move -Wswitch -Wdeprecated-declarations -Werror=deprecated-this-capture") endif() ``` -------------------------------- ### Set COMMON_LIBS based on React Native version Source: https://github.com/babylonjs/babylonreactnative/blob/master/Modules/@babylonjs/react-native/android/CMakeLists.txt Defines the COMMON_LIBS variable with different library sets depending on whether REACT_NATIVE_VERSION is 76 or greater. For versions below 76, additional React Native JNI and runtime executor libraries are included. ```cmake # Link if(${REACT_NATIVE_VERSION} GREATER_EQUAL 76) set(COMMON_LIBS ${LOG_LIB} ${REACT_LIB} ${FBJNI_LIBRARY} ${JSI_LIB} -ljnigraphics -lGLESv2 -lEGL -landroid ) else() set(COMMON_LIBS ${LOG_LIB} ${FBJNI_LIBRARY} ${REACT_LIB} ${JSI_LIB} ${REACTNATIVEJNI_LIB} ${RUNTIMEEXECUTOR_LIB} ${TURBOMODULES_LIB} -ljnigraphics -lGLESv2 -lEGL -landroid ) endif() ```