BEngine for Unity

Introduction

  BEngine for Unity is a tool which allows to generate meshes and instances procedurally. Setting up Geometry Nodes and putting a blend file into Unity, it allows us to create amazing generations for games.

 

Features:

  • Procedural meshes generation
  • Geometry Nodes support
  • Sverchok Nodes support
  • Unity Terrain support
  • Unity Curve (Spline and Polyline) support
  • Networking between Blender and Unity.
  • Procedural instances generation (be_instance attribute)
  • Unity Materials support (be_material attribute)
  • UVs support (uv_map, uv_map2, uv_map3 uv_map8 attributes)
  • Vertex Colors support (be_color attribute)
  • Custom Normals support (be_normal attribute)
  • Inputs support inside of Unity UI (Tabs, Foldouts, Separators, Labels, Objects, Floats, Integers, Strings, Textures, Collections)

BEngine Installation

Video about installation

BEFORE THE INSTALLATION,  PLEASE MAKE SURE YOUR LOCAL HOST IS NOT BLOCKED BY YOUR FIREWALL. OPEN YOUR CONSOLE AND TEST THIS: ping 127.0.0.10

  1. Install the BEngine for Unity package into your Unity project.
  2. Go to Edit->ProjectSettings->BEngine and set up your Blender Executable path.
  3. Go to BEngine Python Module and download a proper version for you BEngine For Unity. Setup a path of BEngine-PY directory in the BEngine Python Module in Edit->ProjectSettings->BEngine.
  4. Setup a path to a temporary folder in Edit->ProjectSettings->BEngine.

That’s it. BEngine is ready to go!

MACOS Blender Path: for MacOS you will need to specify a full Blender path. It will look like: /Applications/Blender.app/Contents/MacOS/Blender
https://docs.blender.org/manual/en/latest/advanced/command_line/launch/macos.html

!!!IMPORTANT!!! Exclude the BEngine Python Module folder from your project. In other case you can violate the GPL License of Blender.

BEngine Component Setup

  • Create your Geometry Nodes in Blender. Save your blend file in the Assets folder somewhere.
  • Create an empty GameObject and add the BEngine Component.
  • Drag’n’drop your blend file into “Blend File” container.
  • Set up Geometry Nodes Name as in Blender.
  • Press the Update Nodes button. A new json file will be created next to the blend file. It will contain Geometry Nodes Inputs values. Please don’t delete the json file. Also you can tweak UI Inputs now.

Then Tweak UI Inputs and press the Run Nodes button.

Networking

You can use either Networking or run Blender instance every time. Networking is much faster and recommended.

Please check out this
video presentation.

Please set up RunBlenderType parameter as Run Networking.

Run Blender server in the BEngine->RunBlenderServer.

That’s all! Now Networking is working when “Update Nodes” and “Run Nodes” buttons are pressed.

The only thing left is managing Timeout minutes. So that Unity will not work infinitely when Blender’s server has any bugs.

Inputs

BEngine for Unity supports following Inputs:

  • Float
  • Integer
  • String
  • Vector
  • Boolean
  • Object
  • Collection
  • Color
  • Material

  To transfer Geometry Nodes UI into Unity, please check out UI_Example.blend file in the Examples:

Default value is supported by most inputs. For example, it's possible to set up a default value of MyInteger=5, MyFloat=0.5, MyString=”Hello World”.

Also, Float, Integer and Vector inputs support Min and Max values. It’s possible to clamp these values. For example, MyFloat value can have Min=0.5 and Max=3.5.

There are also some special inputs. Label, Separator, Tab, Foldout. To use these inputs just create  String input and set names:

  • TAB
  • LABEL
  • SEPARATOR
  • FOLDOUT

Objects and Meshes

  The default geometry input isn’t used. Instead “Object Info” and “Collection Info” nodes are used to get geometry. In Unity you can define some GameObjects as an Input.

Main Attributes

Normals

  Normals are exported automatically for every mesh from Blender to Unity and there is no need to do any additional steps. But if you want to have custom normals then you can use the “be_normal” facecorner attribute for this.

Custom Normals:

Name: be_normal

Domain: FaceCorner

Type: Vector

UVs

Video Explanation of UVS

  To add some UVs just add uv_map, uv_map2, uv_map3 … uv_map8 attribute, FaceCorner domain. Unity supports up to 8 UV sets.

Name: uv_map, uv_map2, uv_map3 … uv_map8

Domain: FaceCorner

Type: Vector2, Vector

Vertex Colors

  To add Vertex Colors to a mesh just simply add be_color attribute, FaceCorner domain.

Name: be_color

Domain: FaceCorner

Type: Color(Vector4)

Materials

Video Explanation of Materials

  To set up Unity materials simply add a be_material attribute to faces in Geometry Nodes. A type of the material attribute should be Integer. That means an Index of a Unity material will be set.

Name: be_material

Domain: Face

Type: Integer

Instances

Video Explanation of Instances

  Instances can be created with be_instance attribute. But the attribute domain can be any. But it’s better using the Face domain.

Name: be_instance

Domain: Any

Type: Integer

Custom Attributes

BEngine supports custom attributes which can be passed into C# ParseCustomMeshAttribs(), ParseCustomObjectAttribs(), EndCustomAttribs()  functions.

Supported Types:

Float: Float, Boolean, Integer

Vector: Vector2D, Vector3D

Color: Color, ByteColor

Supported Domains:

Point, Edges, Face, FaceCorner

1 Create a C# Script.

2 Add this code. Extend the BEngineComponent and override functions below.

using UnityEngine;

using UnityEngine;

using BEngine;

using BESimpleJSON;

public class MyBEngineComponent : BEngineComponent

{

#if UNITY_EDITOR

        public override Mesh ParseCustomMeshAttribs(Mesh mesh, JSONNode CustomAttribs)

        {

            // Your Code. It works per Generated Mesh.

            UnityEngine.Debug.Log(CustomAttribs);

            return mesh;

        }

        public override void ParseCustomObjectAttribs(GameObject gameObj, JSONNode CustomAttribs)

        {

            // Your Code. It works per Generated Object.

            UnityEngine.Debug.Log(CustomAttribs);

        }

        public override void EndCustomAttribs()

        {

            // Your Code. It works at the end of all generated objects.

            // Clear/Clean your variables.

        }

#endif

}

3 Assign your custom script to an Object.

Setting Up Custom Attributes in Blender

Use bem_* attribute names to create custom mesh attributes. They will be passed in the ParseCustomMeshAttribs() function.

Use beo_* attribute names to create custom object attributes. They will be passed in the ParseCustomObjectAttribs() function.

BEngine Terrain Component

It’s possible to sample Unity Terrain to convert it to a mesh. Just create an empty GameObject and add a BEngineTerrain component. Then put this GameObject to a BEngine mesh input.

BEngine Curve Component

Add BEngineCurveComponent to a GameObject.

Hotkeys:

Shift+N - New Curve

Shift+A - Add a point (Select 1 or 2 points)

Shift+S - Select Points

Shift+X - Delete Selected Points

Spline Types

Closed Curve

“Closed/Open” can switch a type of a curve.

Tilt

Tilt is supported.

Sverchok

BEngine supports Sverchok Node System. Sverchok is a very amazing node system. If you want to add your own Python nodes or any Python/C++ library then this node system is for you.

Proper Installation.

It’s recommended to have these modules to be installed so that more Sverchok nodes to be available. But this is optional.

BEngine Nodes for Sverchok.

Please, download BEngine Python Module and get scripts for Sverchok. https://github.com/mifth/BEngine-Python-Module/tree/main/Sverchok/ScriptNodeLite/BEngineInOut

These scripts can be loaded in Blender with a Text Editor or can be placed into “addons\sverchok\node_scripts\SNLite_templates”.

Variant 1. Through Text Editor:

Variant 2. Or add Files to Sverchok:

Sverchok Input/Output Nodes:

After adding BEngine nodes for Sverchok you need to add a Script Node Lite node.

Input and output nodes look like this.

Inputs:

Outputs:

Examples

Please, try Examples for BEngine.

Additional Tools

Save Mesh Tool

Sometimes you would probably like to save a mesh to use it in different scenes.