In this tutorial, we are going to use RapidCompact CLI to process and optimize assets with transparent parts and/or materials. In the following we will use the term opaque for all completely non-transparent materials, and transparent for all materials with any kinds of transparency, be it stored in materials descriptions or texture maps. In order to provide the best possible result according to the needs of your application, RapidCompact provides different Flattening Modes, which give the option to do a more selective merge of data (mesh, textures, materials) instead of exclusively merging all content to achieve the best performance possible (e.g., 1 drawcall per asset). We will see that it is still possible to reduce opaque meshes to one single drawcall, and why optimized assets with transparent parts or materials will always have a minimum number of two or more drawcalls, depending on the meshes and materials involved. In section Handling Transparency we go more into detail on why this is the case.
As an example file we will use the FlightHelmet asset which is shown above. You can download the glTF file here.
Let's see what RapidCompact does if we optimize the asset to 25% of the original polygons and use flattening mode "full". This can be done either directly in the command line by changing the "flattening:mode" setting, or by altering the config file under "flattening:mode". On the command line it will look like this:
rpdx -i .\FlightHelmet.gltf -s flattening:mode full -c f:25% -e FlightHelmet-compact-full.glb
The resulting file will have only one drawcall, and all meshes and materials will be merged to use only a single, compact UV atlas. However, as you can see in the resulting model, the transparency of the glasses will not preserved:
The reason why RapidCompact does not preserve the transparency in this "full flattening" case is due to the real-time rendering pipeline not being able to deal with a mix of transparent and non-transparent parts within a single drawcall. Instead, the transparent parts must usually be rendered separately, one-by-one, and sorted back-to-front.
With RapidCompact, we are able to produce a different version that contains multiple meshes and nodes, and that will render properly in a real-time setting. The standard setting for flattening is "auto", which lets RapidCompact decide about the flattening mode - version 3 will simply flatten the asset "byOpacity". Let us simply recreate the last command, but without the --flatten command, to see the effect of the "auto" / "byOpacity" flattening mode in practice:
rpdx -i FlightHelmet.gltf -c f:25% -e FlightHelmet-compact-auto.glb
As you may see when inspecting the resulting file, it will now have the exact same amount of faces and vertices as the previously optimized version, but it now has two drawcalls. Also, the transparency is now preserved and your rendered result should look similar to this version:
There are basically two different ways of storing transparency in the optimized models. Mostly this is dependent on how the input file's transparency is stored. If an input FBX or glTF file contains a transparency value in a material (or an OBJ file with Tr in the .mtl file), RapidCompact also preserves that material and its transparency. In many cases, however, the transparency is stored in an alpha channel of a texture map.
To cover all sorts of possible further usage of optimized models, RapidCompact offers four unique flattening modes. Instead of merging all geometry and material information into single nodes (and hence into single drawcalls), you can specify a mode in order to preserve more of the original materials, meshes and/or hierarchy. The available flattening modes are as follows:
Flattening
Name | Type | Default | Valid Range | Quick Description |
---|---|---|---|---|
flattening:mode | String | auto | {'auto', 'byOpacity', 'byMaterial', 'full', 'none'} | mode to be used for flattening |
Not using any flattening has specific benefits and potential downsides as well, also depending on what mode is chosen for the specific input mesh:
Benefits:
Potential Downsides:
Performance:
We just saw how the usage of different flattening modes can have benefits and downsides. To get a better picture what that means in terms of performance and materials of a 3D model, we will produce four different versions of the example file:
rpdx -i FlightHelmet.gltf -s flattening:mode byOpacity -c f:25% -e FlightHelmet-compact-byOpacity.glb
rpdx -i FlightHelmet.gltf -s flattening:mode byMaterial -c f:25% -e FlightHelmet-compact-byMaterial.glb
rpdx -i FlightHelmet.gltf -s flattening:mode full -c f:25% -e FlightHelmet-compact-full.glb
rpdx -i FlightHelmet.gltf -s flattening:mode none -c f:25% -e FlightHelmet-compact-none.glb
When comparing the results, we can see that each .glb file has a different number of drawcalls and nodes. Also, the result in the bottom left corner lost the transparency because of using the "full" flattening mode (we did already cover that case in the the first section).
The other results are mostly behaving according to the syntax behind the flattening. The version reduced with "byOpacity" produces two drawcalls from two different meshes so that the transparent glasses and the opaque parts can be rendered correctly. In this case the flattening "none" version has almost the same result due to the structure of the input mesh. This might differ depending on the organization of the input mesh and hierarchy.
Overall, we can say that using RapidCompact's standard setting (flattening mode "auto") produces results supporting and preserving transparency without compromising the performance too much. Of course, an input file with a lot of unique transparent parts will have more drawcalls than with the "full" flattening mode, but the consequences regarding performance should be still minor and usually the benefits of preserving transparency should outwheigh the additional drawcalls and nodes.
Hierarchy Preservation:
Let us take the models produced in the previous section and this time have a look at the glTF hierarchy in the different output files in comparison to the original hierarchy:
As you can see, the hierarchies of the generated results give a good overview on how RapidCompact can use a grade of flattening specifically tailored to the needs of each final application. In comparison, the flattening mode "none" will preserve the nodes and naming conventions of the original, whereas the "full" and "byOpactiy" modes will flatten either everything or all opaque nodes, respectively. Like "byOpacity", the "byMaterial" mode will usually produce a different number of nodes than given in the input model. The only difference between these two modes is that "byOpacity" will merge together all nodes that have the same opacity category (transparent or opaque), while "byMaterial" merges together all nodes that have the same material.
Draw Calls vs. Materials or Atlases:
Within this tutorial, we have learned how to use RapidCompact's flattening feature to produce assets with a low number of draw calls, fitting the needs of a certain target application - for applications that require separate nodes (for example, to show, hide or transform them individually during runtime), no flattening is necessary, while others can use flattening by opacity or by material to achieve a lower number of draws, hence boosting the rendering performance. Within the next tutorial, we are going to have a look at atlasing modes, which will allow us to configure how RapidCompact creates texture atlases when creating new, optimized materials.