Add SimplygonSelectionSet component to GameObject - Scripting
Several Simplygon features require some form of selection set to function properly, for example for selective processing, assignment of clipping geometries, cameras /visibility volumes for visibility culling, bone locks etc. As Unity does not have any official support for selection sets we've added an extension in the form of a SimplygonSelectionSet component.
The SimplygonSelectionSet component can be added to most scene nodes, such as transformations, meshes, bones and cameras. Upon export (to Simplygon) these components will be identified and related nodes placed in selection sets accordingly. A set has two properties, the first is the name of the selection set the node should be part of, the second is include children, which when specified will add all children of the node to the set.
To add a selection set component to a GameObject, simply call AddComponent for the desired GameObjects. AddComponent will return a reference to the new component, which can be used to modify the properties of the selection set, for example the Name of the set and the Include Children option.
The example script below will register a menu item (Simplygon -> SimpleReductionWithSelectionSet) so that we easily can access the script from Unity. The script will add a visibility cube to the scene and add it to a selection set called 'VisibilitySelectionSet'. We'll then use this selection set in the VisibilitySettings for the reduction pipeline as a visibility geometry input.
using Simplygon;
using Simplygon.Unity.EditorPlugin;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class SimpleReductionWithSelectionSet
{
[MenuItem("Simplygon/SimpleReductionWithSelectionSet")]
static void EntryPoint()
{
if (Selection.gameObjects.Length > 0)
{
using (ISimplygon simplygon = global::Simplygon.Loader.InitSimplygon
(out EErrorCodes simplygonErrorCode, out string simplygonErrorMessage))
{
// if Simplygon handle is valid, loop all selected objects
// and call dummy function.
if (simplygonErrorCode == Simplygon.EErrorCodes.NoError)
{
// Create a visibility selection set and at its gameobjects to selection.
var selectedGameObjects = Selection.gameObjects.ToList();
GameObject visibilityCube = GameObject.Find("VisibilityCube");
if (visibilityCube == null)
{
visibilityCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
visibilityCube.name = "VisibilityCube";
Simplygon.Unity.EditorPlugin.SimplygonSelectionSet visibilitySet = visibilityCube.AddComponent<Simplygon.Unity.EditorPlugin.SimplygonSelectionSet>();
visibilitySet.SelectionSetName = "VisibilitySelectionSet";
}
selectedGameObjects.Add(visibilityCube);
// Create a reduction pipeline.
using var simplygonReductionPipeline = simplygon.CreateReductionPipeline();
// Set triangle ratio to 25% of original.
using var simplygonReductionSettings = simplygonReductionPipeline.GetReductionSettings();
simplygonReductionSettings.SetReductionTargetTriangleRatio(0.25f);
// Enabled visibility and use the 'VisibilitySelectionSet' selection set.
using var simplygonVisibilitySettings = simplygonReductionPipeline.GetVisibilitySettings();
simplygonVisibilitySettings.SetUseVisibilityWeightsInReducer(true);
simplygonVisibilitySettings.SetCameraSelectionSetName("VisibilitySelectionSet");
// Run Simplygon processing.
var simplygonProcessing = new SimplygonProcessing();
simplygonProcessing.Run(simplygonReductionPipeline, selectedGameObjects, EPipelineRunMode.RunInThisProcess, true);
}
// if invalid handle, output error message to the Unity console
else
{
Debug.LogError("Simplygon initializing failed!");
}
}
}
}
}
Let's give this script a try. Create a new Unity project, import an asset, select the object(s) and run the script.
If everything goes as expected you should now see an optimized asset where more triangles are kept on the side facing the visibility cube.
For more information, please see Visibility