Hide menu
Integration with Unity
Warning
This page describes functionality that is part of Unity Plugin add-on, which is licensed separately from the base CAD Exchanger SDK.

Overview

One possible custom scenario is to use CAD Exchanger together with the Unity game engine. CAD Exchanger supports integration with Unity game engine for the purpose of loading models in formats not supported by Unity itself.

Example of model imported with CAD Exchanger add-on

Conversion of models to Unity objects

CadExUnity is a CAD Exchanger SDK module that allows you to convert ModelData_Model to Unity's GameObjects.

Note
Since Unity scripts are implemented in C#, the CadExUnity module is only available as a .NET library.

ModelData_Model is mapped to Unity objects as follows:

  • Product structure is mapped to the hierarchy of GameObjects, implemented via parent-child relationships.
  • The geometry is stored in Unity.Mesh.
  • Appearance attributes are converted to Unity.Material.
Note
Since Unity does not support B-Rep geometry, for all models that do not contain a polygonal representation, the BRep-representation will be converted to polygons.

To perform the conversion one must use Unity_ObjectFactory.Create() which takes the ModelData_Model and an optional name and returns GameObject.

The following example demonstrates a typical workflow:

using cadex;
using UnityEngine;
...
STEP_Reader aReader = new STEP_Reader();
aReader.ReadFile(new Base_UTF16String("MyModel.stp"));
//convert STEP to MDM
ModelData_Model aLoadedModel = new ModelData_Model();
aReader.Transfer(aLoadedModel);
//convert MDM to GameObject
Unity_ObjectFactory aFactory = new Unity_ObjectFactory();
GameObject aTarget = aFactory.Create (aLoadedModel, "MyModel.stp");
...

Building

Unity add-on works with the following shaders:

  • "Standard" (standard Unity Shader).
  • "VertexColor" for displaying color-per-vertex painted parts.
  • "PMIShader" for correct displaying PMI.

To build your project successfully, the mentioned shaders must be added to your .shadervariants file.

Note
You can use the ready made shadervariants file residing here:
${install_dir}/examples/csharp/Unity/Assets/Shaders/CadExUnityShaderVariants.shadervariants.
To do this add this file to your project and specify it in the Build settings->Player Settings->Graphics->Preloaded Shaders.

If your project uses Universal Rendering Pipeline (URP) or High Definition Rendering Pipeline (HDRP), it is necessary to manually set "Lit" shader from your rendering pipeline package as default conversion shader using Unity_ObjectFactoryParameters.SetShader().

Examples

Refer to Unity Base Viewer Example, Unity PMI Viewer Example, Unity Edit Mode Import Example, Unity Edit Mode Export Example

Reads STEP files.
Definition: STEP_Reader.hxx:28
Defines classes, types, and global functions related to CAD Exchanger.
Definition: A3DSTestLib.hxx:22