Skip to content

brianzinn/react-babylonjs

Repository files navigation

react-babylonjs

'react-babylonjs' integrates the Babylon.js real time 3D engine with React

react-babylonjs lets you build your scene and components using a familiar declarative syntax with the benefits of reusable components and hooks. The Babylon.js API is mostly covered declaratively thanks to code generation and even custom props allow you to declaratively add shadows, physics, 3D models, attach 2D/3D UI to meshes, etc.

NOTE: for the time being React 19 works with newest and @latest versions. For older React versions, you will need to use 3.2.2 (ie: yarn add [email protected]). Hope next version will support both. I tried to stay away from a major bump due to the ephemeral nature of this change. Thanks for your patience.

install

npm i react-babylonjs
yarn add react-babylonjs

Fully supports hooks. Full support for TypeScript with auto-completion on elements and compile time checks. Context API and hooks provide easy access to Scene/Engine/Canvas.

NPM version NPM downloads

No third party dependencies outside of React + babylon.js

https://brianzinn.github.io/react-babylonjs/

Examples

~50 sample web pages with viewable source code on: github pages examples.

Example Projects

React Native

No online examples for native, but you can integrate using EngineCanvasContext.Provider:

import React, { useState } from 'react';
import { View } from 'react-native';

import { EngineView, useEngine } from '@babylonjs/react-native';
import { Camera } from '@babylonjs/core';
import { EngineCanvasContext, Scene } from 'react-babylonjs';

const EngineScreen: FunctionComponent<ViewProps> = (props: ViewProps) => {

  const engine = useEngine();
  const [camera, setCamera] = useState<Camera>();

  return (
    <View style={props.style}>
      <EngineCanvasContext.Provider value={{ engine, canvas: null }}>
        {engine &&
          <Scene>
            <arcRotateCamera
              name="camera1"
              onCreated={camera => setCamera(camera)}
            />
            <hemisphericLight name="light1" />
            { /* rest of declarative scene/components here */ }
          </Scene>
        }
      </EngineCanvasContext.Provider>
      <EngineView camera={camera} displayFrameRate={true} />
    </View>
  );
};

Contributors

Thanks also to all the people who have contributed with issues/questions/discussions. All the great ideas and requests are why this project evolved beyond an experiment.

Made with ♥