@babylonjs-toolkit/next
v8.40.1
Published
Babylon Toolkit Runtime Library (ES6)
Downloads
725
Maintainers
Readme
ESNext Edition
The Babylon Toolkit is a runtime library extension for BabylonJS development that provides modern programming mechanics with a familiar Unity like scripting model to fast track native web game development.
Classic Edition (UMD) Browser Library (CDN) Demo React Project (TSX) Next.js React Project (TSX)
Basic Installation
npm install @babylonjs-toolkit/nextDefault Installation (ES6)
npm install @babylonjs/core @babylonjs/gui @babylonjs/loaders @babylonjs/materials @babylonjs/havok @babylonjs-toolkit/next- Default Module Import Libraries
import { Engine, Scene } from "@babylonjs/core";
import { HavokPlugin } from "@babylonjs/core/Physics/v2/Plugins/havokPlugin";
import HavokPhysics from "@babylonjs/havok";
import { SceneManager, ScriptComponent } from "@babylonjs-toolkit/next";- Granular File Level Import Libraries
import { Engine } from "@babylonjs/core/Engines/engine";
import { Scene } from "@babylonjs/core/scene";
import { HavokPlugin } from "@babylonjs/core/Physics/v2/Plugins/havokPlugin";
import HavokPhysics from "@babylonjs/havok";
import { SceneManager } from "@babylonjs-toolkit/next/scenemanager";
import { ScriptComponent } from "@babylonjs-toolkit/next/scenemanager";
import { LocalMessageBus } from "@babylonjs-toolkit/next/localmessagebus";
import { CharacterController } from "@babylonjs-toolkit/next/charactercontroller";- Legacy Global Namespace Import Libraries
import * as BABYLON from "@babylonjs/core/Legacy/legacy";
import { HavokPlugin } from "@babylonjs/core/Physics/v2/Plugins/havokPlugin";
import HavokPhysics from "@babylonjs/havok";
import * as TOOLKIT from "@babylonjs-toolkit/next";
TOOLKIT.SceneManager.AutoStripNamespacePrefix = false;Navigation Mesh And Pathfinding (Navigation)
Import Navigation from the babylon-toolkit library:
import { Navigation } from '@babylonjs-toolkit/next';Vite Configuration (ES6)
The Vite bundle services behave differently in devmode than production. To preserve some required classes during devmode, these exclude and include settings are strongly recommended in your vite.config.js settings file.
optimizeDeps: {
exclude: mode === 'development' ? [
"@babylonjs/havok",
"@babylonjs/core",
"@babylonjs/loaders",
"@babylonjs/loaders/glTF",
] : ["@babylonjs/havok"],
include: mode === 'development' ? [
"@babylonjs/gui",
"@babylonjs/materials",
"@babylonjs/inspector",
"@babylonjs-toolkit/dlc",
"@babylonjs-toolkit/next"
] : [],
},🌳 Tree Shaking
The Babylon Toolkit ES6 library is optimized for maximum tree-shaking with 39 separate module files containing 114 total declarations. The build system intelligently groups related classes to handle circular dependencies while maintaining optimal bundle sizes.
Key Benefits:
- ✅ Smart dependency grouping - Related classes are bundled together
- ✅ Zero unused code - Only imported classes are included
- ✅ Circular dependency handling - Complex relationships are managed automatically
- ✅ Multiple import styles - Choose the approach that fits your needs
🚀 Import Methods
Method 1: Main Index Import (Recommended)
import { SceneManager, ScriptComponent, InputController } from "@babylonjs-toolkit/next";- Pros: Simple, clean, easy to refactor
- Cons: Bundler must analyze index.js
- Use Case: Most applications, rapid development
Method 2: File-Level Import (Maximum Control)
import { SceneManager } from "@babylonjs-toolkit/next/scenemanager";
import { LocalMessageBus } from "@babylonjs-toolkit/next/localmessagebus";- Pros: Explicit dependencies, maximum bundler hints
- Cons: More verbose, requires knowledge of file structure
- Use Case: Library authors, performance-critical applications
📦 Module Groups
The toolkit is organized into logical groups based on dependency analysis. The main entry point uses file level imports automatically
Core Group (scenemanager.ts)
The largest group containing the main runtime and interconnected components:
Classes:
SceneManager- Main toolkit framework managerScriptComponent- Base class for all script componentsPreloadAssetsManager- Asset preloading managementGlobalMessageBus- Global messaging systemRequestHeader- HTTP request utilitiesCubeTextureLoader- Cube texture loading utilitiesMetadataParser- Scene metadata parsingUtilities- General utility functionsInputController- User input managementWindowManager- Window and screen managementAnimationState- Animation state machine componentBlendTreeValue- Animation blend tree valuesBlendTreeUtils- Animation blend tree utilitiesBlendTreeSystem- Animation blend tree systemMachineState- State machine implementationTransitionCheck- Animation transition logicAnimationMixer- Animation mixing utilitiesBlendingWeights- Animation blending weightsAudioSource- Audio playback componentHavokRaycastVehicle- Havok physics vehicleHavokWheelInfo- Vehicle wheel informationHavokVehicleUtilities- Vehicle utility functionsRaycastVehicle- Generic raycast vehicleRigidbodyPhysics- Physics rigidbody componentPhyscisContainerData- Physics container data
Interfaces:
IAssetPreloader- Asset preloading interfaceIWindowMessage- Window messaging interfaceIRuntimeOptions- Runtime configurationIUnityTransform- Unity transform compatibilityIUnityCurve- Unity animation curve compatibilityIUnityMaterial- Unity material compatibilityIUnityTexture- Unity texture compatibilityIUnityCubemap- Unity cubemap compatibilityIUnityAudioClip- Unity audio clip compatibilityIUnityVideoClip- Unity video clip compatibilityIUnityFontAsset- Unity font asset compatibilityIUnityTextAsset- Unity text asset compatibilityIUnityDefaultAsset- Unity default asset compatibilityIUnityVector2- Unity Vector2 compatibilityIUnityVector3- Unity Vector3 compatibilityIUnityVector4- Unity Vector4 compatibilityIUnityColor- Unity Color compatibilityUserInputPress- User input press interfaceKeymapState- Input keymap stateIAnimatorEvent- Animation event interfaceIAvatarMask- Animation avatar maskIAnimationLayer- Animation layer interfaceIBehaviour- Animation behavior interfaceITransition- Animation transition interfaceICondition- Animation condition interfaceIBlendTree- Animation blend tree interfaceIBlendTreeChild- Blend tree child interfaceIPhysicsShapeCastResult- Physics shape cast resultIPhysicsShapeCastQuery- Physics shape cast query
Types:
UserInputAction- User input action callback type
Enums:
System- System type enumerationSearchType- Search functionality typesPlayerNumber- Player identificationRenderQuality- Graphics quality settingsGamepadType- Controller typesUserInputAxis- Input axis enumerationUserInputKey- Input key enumerationMaterialAlphaMode- Material alpha blending modesDragDirection- Touch drag directionsPinchZoomState- Pinch zoom statesMotionType- Animation motion typesConditionMode- Animation condition modesInterruptionSource- Animation interruption sourcesBlendTreeType- Animation blend tree typesAnimatorParameterType- Animation parameter types
Standalone Component Groups
The remaining components are organized into standalone modules for optimal tree-shaking:
Animation & Media Components
ShurikenParticles- Particle system componentWebVideoPlayer- Video playback component
Character Controllers
CharacterController- Physics-based character controllerSimpleCharacterController- Basic character movementRecastCharacterController- Navigation mesh character controllerNavigationAgent- Navigation mesh agentUniversalCharacterController- Universal character controller type
Terrain & Environment
TerrainGenerator- Procedural terrain generationUniversalTerrainMaterial- Advanced terrain materialUniversalTerrainMaterialPlugin- Terrain material plugin
Material & Rendering Systems
CustomShaderMaterial- Custom shader material systemCustomShaderMaterialPlugin- Custom shader material pluginCustomLoadingScreen- Custom loading screen implementationLinesMeshRenderer- Line rendering utilities
Input & Control Enums
Handedness- Hand preference enumerationPlayerControl- Player control typesXbox360Trigger- Xbox controller trigger enumerationMovementType- Character movement typesMouseButtonMode- Mouse button interaction modesTouchMouseButton- Touch to mouse button mappingUserInputPointer- Input pointer types
Physics & Collision Systems
CollisionContact- Collision contact typesCollisionState- Collision state enumerationCollisionFlags- Physics collision flagsCollisionFilters- Collision filtering systemIntersectionPrecision- Ray intersection precision
Animation & UI Types
BlendTreePosition- Animation blend tree positioningIAnimationCurve- Animation curve interfaceIAnimationKeyframe- Animation keyframe interfaceTouchJoystickHandler- Touch joystick input handler
Data Structures & Results
RaycastHitResult- Ray casting result dataTriggerVolume- Physics trigger volumeRoomErrorMessage- Network room error messagingPrefabObjectPool- Object pooling systemLocalMessageBus- Local messaging systemEntityController- Entity management controller
🎯 Best Practices
1. Start Small
Begin with minimal imports and add components as needed:
// Start with this
import { SceneManager } from "@babylonjs-toolkit/next";
// Add components incrementally
import { SceneManager, InputController } from "@babylonjs-toolkit/next";2. Group Related Imports
Import related functionality together:
// Good - related physics components
import {
CharacterController,
RigidbodyPhysics,
CollisionState
} from "@babylonjs-toolkit/next";3. Use File-Level Imports For Maximum Control
When bundle size is critical:
// Maximum tree-shaking for production
import { SceneManager } from "@babylonjs-toolkit/next/scenemanager";
// Character Controllers
import { CharacterController } from "@babylonjs-toolkit/next/scenemanager";
import { SimpleCharacterController } from "@babylonjs-toolkit/next/scenemanager";
import { RecastCharacterController } from "@babylonjs-toolkit/next/scenemanager";
// Animation & Media Components
import { ShurikenParticles } from "@babylonjs-toolkit/next/shurikenparticles";
import { WebVideoPlayer } from "@babylonjs-toolkit/next/webvideoplayer";
// Terrain & Environment
import { TerrainGenerator } from "@babylonjs-toolkit/next/terraingenerator";
import { UniversalTerrainMaterial } from "@babylonjs-toolkit/next/universalterrainmaterial";
// Material Systems
import { CustomShaderMaterial } from "@babylonjs-toolkit/next/customshadermaterial";
import { CustomShaderMaterialPlugin } from "@babylonjs-toolkit/next/customshadermaterialplugin";
// Utility Classes
import { CustomLoadingScreen } from "@babylonjs-toolkit/next/customloadingscreen";
import { PrefabObjectPool } from "@babylonjs-toolkit/next/prefabobjectpool";
import { LocalMessageBus } from "@babylonjs-toolkit/next/localmessagebus";
import { LinesMeshRenderer } from "@babylonjs-toolkit/next/linesmeshrenderer";
// Enums & Types (Smallest Imports)
import { Handedness } from "@babylonjs-toolkit/next/handedness";
import { PlayerControl } from "@babylonjs-toolkit/next/playercontrol";
import { MovementType } from "@babylonjs-toolkit/next/movementtype";
import { CollisionContact } from "@babylonjs-toolkit/next/collisioncontact";
import { BlendTreePosition } from "@babylonjs-toolkit/next/blendtreeposition";4. Analyze Your Bundle
Use your bundler's analysis tools to verify tree-shaking:
# Vite bundle analysis
npm run build -- --mode production
# Webpack bundle analyzer
npm install --save-dev webpack-bundle-analyzer