Skip to the main content.

Did you know?

 

RTI is the world’s largest DDS supplier and Connext is the most trusted software framework for critical systems.

Tiered-Support-Plan-DatasheetTiered Support Plans

Our Professional Services team brings extensive experience to train, problem-solve, mentor, and accelerate customer success.

Learn more

Developers

From downloads to Hello World, we've got you covered. Find all of the tutorials, documentation, peer conversations and inspiration you need to get started using Connext today.

Try the Connectivity Selection Tool ⇢

Resources

RTI provides a broad range of technical and high-level resources designed to assist in understanding industry applications, the RTI Connext product line and its underlying data-centric technology.

Company

RTI is the largest software framework company for autonomous systems. The company’s RTI Connext product enables intelligent architecture by sharing information in real time, making large applications work together as one.

News & Events
Cooperation

MODULE 5:
Gaming Visualizations: Expanding to Multiple Topics

⇠ Back to All Modules

Case-Code-hero

Introduction

This module builds directly on Module 4: Using an Object Manager to Subscribe to Multiple Instances. You will need to complete that module before performing this module. In this module we will expand the capabilities of our visualization to include additional shapes.

Tetrahedrons are not native 3D objects in Unity®. The tetrahedron we used in this project is dynamically created from code found on this blog.

Step 1: Creating the New Prefabs

Since we want to include reading the Circle and Triangle topics, we need to create new prefabs based upon the Cube prefab we already have. Here we create the Sphere and Pyramid prefabs.

  1. Create the Sphere prefab the same way we created the cube prefab.
    1. Right-click in your Hierarchy and choose 3D Object»Sphere.
    2. Click on the Sphere component in the Hierarchy and in the Inspector, click Add Component. Add a RigidBody.
    3. Under Constraints, select X, Y, and Z for Freeze Rotation.
    4. Drag the Sphere from your Hierarchy into your Prefabs folder in your Project.
  2. In this project we will be dynamically creating our Pyramids.
    1. Start by creating an Empty Object in your Hierarchy by right-clicking and choosing Create Empty.
    2. Change the name of the GameObject to Pyramid.
    3. Add the following components to your Object:
      1. Mesh Filter
      2. Mesh Renderer
      3. Rigidbody
      4. Mesh Collider
    4. Set the Material of the Mesh Renderer to Default-Material.
    5. In the Rigidbody, under Constraints, select X, Y, and Z for Freeze Rotation.
    6. In the Mesh Collider, check Convex.
    7. Drag the PyramidMeshGenerator script from your Scripts/Shapes folder into the Pyramid Inspector.
    8. Drag the Pyramid into your Prefabs folder.
  3. Open the PyramidMeshGenerator script and add the following code to your Start function:

    Mesh mesh = new Mesh();

            Vector3 p0 = new Vector3(0, 0, 0);
            Vector3 p1 = new Vector3(1, 0, 0);
            Vector3 p2 = new Vector3(0.5f, 0, Mathf.Sqrt(0.75f));
            Vector3 p3 = new Vector3(0.5f, Mathf.Sqrt(0.75f), Mathf.Sqrt(0.75f) / 3);

            mesh.Clear();

            mesh.vertices = new Vector3[]{
                p0,p1,p2,
                p0,p2,p3,
                p2,p1,p3,
                p0,p3,p1};

            mesh.triangles = new int[]{
                0,1,2,
                3,4,5,
                6,7,8,
                9,10,11};

            Vector2 uv3a = new Vector2(0, 0);
            Vector2 uv1 = new Vector2(0.5f, 0);
            Vector2 uv0 = new Vector2(0.25f, Mathf.Sqrt(0.75f) / 2);
            Vector2 uv2 = new Vector2(0.75f, Mathf.Sqrt(0.75f) / 2);
            Vector2 uv3b = new Vector2(0.5f, Mathf.Sqrt(0.75f));
            Vector2 uv3c = new Vector2(1, 0);

            mesh.uv = new Vector2[]{
                uv0,uv1,uv2,
                uv0,uv2,uv3b,
                uv0,uv1,uv3a,
                uv1,uv2,uv3c};

            mesh.RecalculateNormals();
            mesh.RecalculateBounds();
            mesh.Optimize();

            GetComponent<MeshFilter>().mesh = mesh;
            GetComponent<MeshCollider>().sharedMesh = mesh;

  4. Delete the Update function.
  5. Save the script.
Step 2: Updating the Comms Scripts

The comms scripts are currently only handling the Square reader and writer. Circle and Triangle are already configured in the XML, we only need to add support within DDSHandler and ShapeComms. 

  1. We will start in the DDSHandler, adding additional members and initialization functions within the base class. This is all based on the DDS configuration that is already defined within the XML file.
    1. In DDSHandler, add the properties for the Circle and Triangle Data Readers and Data Writers.

      protected DataReader<DynamicData> DataReaderSphere { get; private set; }
          protected DataReader<DynamicData> DataReaderCube { get; private set; }
          protected DataReader<DynamicData> DataReaderPyramid { get; private set; }
          protected DataWriter<DynamicData> DataWriterSphere { get; private set; }
          protected DataWriter<DynamicData> DataWriterCube { get; private set; }
          protected DataWriter<DynamicData> DataWriterPyramid { get; private set; }

    2. Now add the initialization code for these Data Readers and Data Writers in the DDSHandler Constructor.

      DataReaderSphere = (DataReader<DynamicData>)subscriber.LookupDataReaderByName("SphereReader");
      DataReaderCube = (DataReader<DynamicData>)subscriber.LookupDataReaderByName("CubeReader");
      DataReaderPyramid = (DataReader<DynamicData>)subscriber.LookupDataReaderByName("PyramidReader");

      DataWriterSphere = (DataWriter<DynamicData>)publisher.LookupDataWriterByName("SphereWriter");
      DataWriterCube = (DataWriter<DynamicData>)publisher.LookupDataWriterByName("CubeWriter");
      DataWriterPyramid = (DataWriter<DynamicData>)publisher.LookupDataWriterByName("PyramidWriter");

    3. Save the file.
  2. In the ShapeComms class, we will add additional functions for reading and writing to these new Data Readers and Data Writers. This will be simplified by the fact that we already created generic functions that we will be able to reuse.
    1. Add the following members to the ShapeComms class.

      private DynamicData d_SphereData;
      private DynamicData d_PyramidData;

    2. We also need to initialize these members within the constructor.

      d_SphereData = DataWriterSphere.CreateData();
      d_PyramidData = DataWriterPyramid.CreateData();

    3. Now, add the following Write functions for the Sphere and Pyramid.

      public void WriteSphere(Shape shape, String color)
          {
              SetShapeData(ref d_CubeData, shape, color);

              DataWriterSphere.Write(d_SphereData);
          }

      public void WritePyramid(Shape shape, String color)
          {
              SetShapeData(ref d_CubeData, shape, color);

              DataWriterPyramid.Write(d_PyramidData);
          }

    4. Finally, add the following Get functions for the Sphere and Pyramid.

      public List<Shape> GetPyramids()
          {
              return ParseShapes(DataReaderPyramid.Take());
          }

          public List<Shape> GetSpheres()
          {
              return ParseShapes(DataReaderSphere.Take());
          }

    5. Save the file.
Step 3: Creating New Object Managers

Since these new shapes use different data readers, data writers, and topics, we will also use separate object managers to handle each shape.

  1. The Pyramid Manager and Sphere Manager will be very similar to the Cube Manager. Make the following changes to the two scripts.
    1. Change the inheritance from MonoBehavior to ShapeManager.
    2. Copy the Awake function from the Cube Manager into the scripts.
    3. Delete the Start functions.
    4. Copy the Update function from the Cube Manager and change the called function from GetCubes to GetPyramids for the Pyramid Manager and GetSpheres for the Sphere Manager.
Step 4: Integrating New Object Managers

All that needs to be done at this point is to add the new object manager scripts to the scene and call them from our GameManager.

  1. Setting up the Unity environment is relatively straightforward.
    1. Create two new Empty GameObjects within the Hierarchy and call them PyramidManager and SphereManager.
    2. Drag the appropriate manager scripts onto their corresponding GameObjects.
    3. Drag the appropriate prefabs onto the Shape Prefab box for each script.
    4. Save the scene.
  2. Open the GameManager script and make the following additions.
    1. Add new public GameObjects for the PyramidManager and SphereManager.

      public GameObject m_PyramidManager;
      public GameObject m_SphereManager;

    2. Add Initialization code in the Start function for the new Managers.

      m_PyramidManager.SetActive(true);
      m_PyramidManager.GetComponent<PyramidManager>().Setup(m_DDS);

      m_SphereManager.SetActive(true);
      m_SphereManager.GetComponent<SphereManager>().Setup(m_DDS);

    3. Save the file.
  3. Now connect the Pyramid and Sphere GameObjects to the GameManager.
    1. In Unity click on the GameManager GameObject in the Hierarchy subpanel.
    2. Drag the Pyramid Manager and Sphere Manager GameObjects from the Hierarchy subpanel into their corresponding boxes in the Inspector subpanel.
    3. Save the scene.
Step 5: Testing New Application Features

Here we are using the RTI Shapes Demo shipped with RTI Connext to test our new application.

  1. Launch the RTI Shapes demo from the RTI Launcher.
  2. Publish to the Square, Triangle, and Circle topics in the RTI Shapes demo.
  3. Click OK to use the default settings.
  4. Run the Unity application. You should see shapes moving across the screen.

If a shape does not appear, check the debug log within Unity to see if there are any problems within your application. Common problems include a missing license or XML file, along with misspellings of references within the XML file.

Also make sure that you didn’t make any mistakes in the copying process, for example not changing a reference to a Cube function to a Pyramid or Sphere function.

 

Module 5 Demo

This video covers the steps for Module 5.

 

 

Keep Going! 

Module 6: Publishing a Shape in 3 Dimensions