Integrate Babylon native in existing Swift project

hi ,
I try to integrate babaylon native in existing (swift NOT react native) project , and I could not achieve this for now ,
do I have to add a pod / package ?
how to load a glb file ?
I could not find any demo project.
I’ll be happy with any kind of help …

cc @BabylonNative

Hi @Elhanan_Uzan and welcome to the forum

The BabylonNative playground app for ios is coded with Swift: BabylonNative/Apps/Playground/iOS at master · BabylonJS/BabylonNative · GitHub
You can start adding the bridge in your app.
There is no other way to integrate BN than with the source code. That means adding subprojects to your xcode workspace.

thanks for your reply ,
so I need to integrate the part of the project of Babylon native ,
“You can start adding the bridge in your app.”
you mean like in the demo project ? _bridge = LibNativeBridge()

and how to load glb file and select the view for this ?

Hey @Elhanan_Uzan , how are you doing? Welcome to the forum!

Let me see if I can give you a more detailed explanation.

Babylon Native is just a fancy C++ library that allows you C++ code to performe rendering using javascript scripts using the Babylon.js API. That been said, in order to integrate it with a swift application you will need to do it the same way you would do with any other C++ library.

Since swift can not interoperate with C++ directly, what people usually do is to create an Objective-C bridge that will glue Swift and C++ togehter, since Swift can call Objective-C and Objective-C can call C++. In our example you can see that bridge object declared in " LibNativeBridge.h" and implemented under " LibNativeBridge.mm". This LibNativeBrige is not part of the Babylon Native API and is probably dependent on the specifics of what you application wants to do with Babylon Native, but it can be a starting point for your own implementation.

Our example implementation of the LibNativeBridge shows how to give Babylon Native access to the MTKView object to render to, as well as how to send touch events from swift to Babylon Native using the setTouchDown, setTouchMove and setTouchUp.

After creating you Objective-C bridge, you will also need to make you XCode project link against the Babylon Native libraries and headers. They way our sample project is setup we do everything using CMake, so our Swift project is created using CMake and linking against the Babylon Native library is just a matter of using target_link_libraries. However, if you are not using CMake for you project you will probably have to setup this manually using XCode.

1 Like

thanks for the warm welcome …

many thanks for your detailed and long explanation.

so you gave the light to this project , now I can see who against who ,

but I have more question if you can answer it will be good ,

so I need to select my needs from the library and add with the bridging header all the part that I want from the library, correct ?
and of course add all the files to my project.

1.what is still not clear for me is the end of your explanation , about target_link_libraries ,
If I understand correctly you want to say that Xcode need to know about those library and we need to setup this in Xcode ? if yes do you know how to do that.

  1. I need to use with Babylon native GLB file how I can achieve that ?

  2. I have the setting already done in web with Babylon.js , that suppose to help me (I know that we want to use : ArcRotateCamera,
    CubeTexture,
    FramingBehavior,
    Matrix,
    Scene,
    SpotLight,
    Tools,
    Vector3 )

thanks in advance.

Babylon Native is developed in a way that really favors using CMake for its configuration. Are you guys using CMake or just creating a new XCode project directly? If you are using XCode instead of CMake would it be a blocker for you to use CMake?

we have already existing big project in swift , and the part that we want Babylon is in catalog in product details with images, also already exist , and we want to add also 3d , so not a new project.

In that case, since you already have a XCode project setup, you will need to do things the hard way and manually configure it to use Babylon Native. (BTW, I’m not a XCode expert so, if other users know a better way of doing this please feel free to comment on this post).

Compile Babylon Native from source

The first thing you will need to do is to get the Babylon Native binaries and headers. To do this you will have to compile them using the public github repo:

git clone https://github.com/BabylonJS/BabylonNative.git --recursive

From the BabylonNative folder create a build directory and call cmake to generate the XCode solution:

mkdir build
cd build
cmake .. -G Xcode -DBABYLON_NATIVE_PLUGIN_EXTERNALTEXTURE=Off

OBS: This will generate MacOS project, if you want to generate IOS binaries you need to use:

cmake .. -G Xcode -D BABYLON_NATIVE_PLUGIN_EXTERNALTEXTURE=Off -D CMAKE_TOOLCHAIN_FILE=../Dependencies/ios-cmake/ios.toolchain.cmake -D PLATFORM=OS64COMBINED -D ENABLE_ARC=0 -D DEPLOYMENT_TARGET=12

Use cmake to build the Xcode project:

cmake --build .

Use cmake install to copy all Babylon Native binaries and headers to a install folder:

cmake --install . --prefix="./install"

This should create a install folder with a include and a lib directories.

Configure XCode manually

Now, that you have the libraries and headers in your machine you just have to configure your XCode project to use those. This is going to be a little painful since the design for Babylon Native is that all of that will be taken care for you by cmake.

1- Go to your project Build Settings and add the path to the install/include folder to your Headers Search Path this will make XCode capable of finding the Babylon Native header files.

2- Also add the path to the install/lib folder to your Library Search Path. This will make XCode search in that folder when looking for native library files (.a files).

3- Go to your “General” project tab and add ALL .a files in the install/lib folder to the Frameworks, Libraries and Embedded Content list. This will make your project link against those native libraries.

4- Add JavaScripCore.framework to Frameworks, Libraries and Embedded Content list as well. Babylon Native will use this javascript runtime to execute javascript.

5- Finally, there is one last configuration you must do. NAPI has some preprocessor definitions that are automatically taken care by cmake. However, since we are not using CMake, we must manually define those. Go to your “Build Settings”, under Apple Clang - Preprocessing and add the following macros to the Preprocessor Macros list:

NODE_ADDON_API_DISABLE_NODE_SPECIFIC
NODE_ADDON_API_DISABLE_DEPRECATED

After all these steps you should be able to create a new Objective-C files and header and copy the LibNativeBridge code into them and have it working.

Once again, this is not the recommended way of doing it, since Babylon Native was really intended to be used with CMake.

3 Likes

thank you very much for your answer, I’ll take that from here.


I’m getting this error when trying to use this command -

cmake .. -G Xcode -D BABYLON_NATIVE_PLUGIN_EXTERNALTEXTURE=Off -D CMAKE_TOOLCHAIN_FILE=../Dependencies/ios-cmake/ios.toolchain.cmake -D PLATFORM=OS64COMBINED -D ENABLE_ARC=0 -D DEPLOYMENT_TARGET=12
``
do you have any idea for this

You didn’t run npm install in Apps/Playground folder

1 Like

now I’m getting this error -


thank you

does the file exist? did you build a playground? can you run the playground?

thank you very much for your help !
now we getting this error

cmake --install . --prefix=“./install”
– Install configuration: “Release”
– Up-to-date: …/Desktop/BabylonNative/build/./install/lib/libastc-encoder.a
CMake Error at /usr/local/Cellar/cmake/3.25.1/share/cmake/Modules/CMakeIOSInstallCombined.cmake:229 (message):
destination is not absolute: ./install/lib/libastc-encoder.a
Call Stack (most recent call first):
cmake_install.cmake:99 (ios_install_combined)\

destination is not absolute: ./install/lib/libastc-encoder.a

what it says: use an absolute path for the installation destination.

I follow all the step 1,2,3,4,5
and I’m talking the libnativeBridge from the example project , and copied in my project ,
but I’m getting this error for all those files -

#import <Babylon/Plugins/NativeXr.h>
#import <Babylon/Plugins/NativeOptimizations.h>
#import <Babylon/Polyfills/Window.h>
#import <Babylon/Polyfills/XMLHttpRequest.h>
#import <Babylon/Polyfills/Canvas.h>
#import <Babylon/Polyfills/Console.h>

File not found

thanks in advance for your help !

Are you creating your swift project with xcode? did you add path to the include directories in your project?

yes we create the project with xcode, and we done all the configuration,
make --install . --prefix=“./install” => some plugins don’t have the release-iphoneos folder also after we build the playground and all plugins alone.