# Simple Aggregation pipeline

This example shows how to create a new AggregationPipeline and how to set basic parameters for Aggregation as well as material baking.

// create a Aggregation Pipeline object
$aggregationPipeline = `SimplygonPipeline -c "AggregationPipeline"`;

// set DefaultTangentCalculatorType to OrthonormalRightHanded (0)
$bResult = `SimplygonPipeline -ss "GlobalSettings/DefaultTangentCalculatorType" -v 0 $aggregationPipeline`;
$defaultTangentCalculatorType = `SimplygonPipeline -gs "GlobalSettings/DefaultTangentCalculatorType" $aggregationPipeline`;
print ("DefaultTangentCalculatorType: " + $defaultTangentCalculatorType + "\n");

// merge geometries
$bResult = `SimplygonPipeline -ss "AggregationProcessor/AggregationSettings/MergeGeometries" -v true $aggregationPipeline`;
$mergeGeometries = `SimplygonPipeline -gs "AggregationProcessor/AggregationSettings/MergeGeometries" $aggregationPipeline`;
print ("MergeGeometries: " + $mergeGeometries + "\n");

// more settings can be set for the Aggregation pipeline,
// see Pipeline documentation and/or inspect the generated file
// that is saved out by this script!

// set bMaterialBake to true to enable material baking,
// if material baking is enabled Simplygon will generate
// a new material (with textures) shared by all the optimized meshes.
$bMaterialBake = true;
if($bMaterialBake == true)
{
    // enable material baking
    // mapping image is required for material baking
    $bResult = `SimplygonPipeline -ss "AggregationProcessor/MappingImageSettings/GenerateMappingImage" -v true $aggregationPipeline`;
    $generateMappingImage = `SimplygonPipeline -gs "AggregationProcessor/MappingImageSettings/GenerateMappingImage" $aggregationPipeline`;
    print ("GenerateMappingImage: " + $generateMappingImage + "\n");

    // in this case we want to generate texture coordinates (UVs)
    $bResult = `SimplygonPipeline -ss "AggregationProcessor/MappingImageSettings/GenerateTexCoords" -v true $aggregationPipeline`;
    $generateTexCoords = `SimplygonPipeline -gs "AggregationProcessor/MappingImageSettings/GenerateTexCoords" $aggregationPipeline`;
    print ("GenerateTexCoords: " + $generateTexCoords + "\n");

    // the name of the resulting texture coordinate field
    $bResult = `SimplygonPipeline -ss "AggregationProcessor/MappingImageSettings/TexCoordName" -v "MaterialLOD" $aggregationPipeline`;
    $texCoordLevel = `SimplygonPipeline -gs "AggregationProcessor/MappingImageSettings/TexCoordName" $aggregationPipeline`;
    print ("TexCoordName: " + $texCoordLevel + "\n");

    // width of the baked textures
    $bResult = `SimplygonPipeline -ss "AggregationProcessor/MappingImageSettings/Output0/TextureWidth" -v 512 $aggregationPipeline`;
    $textureWidth = `SimplygonPipeline -gs "AggregationProcessor/MappingImageSettings/Output0/TextureWidth" $aggregationPipeline`;
    print ("TextureWidth: " + $textureWidth + "\n");

    // height of the baked textures
    $bResult = `SimplygonPipeline -ss "AggregationProcessor/MappingImageSettings/Output0/TextureHeight" -v 512 $aggregationPipeline`;
    $textureHeight = `SimplygonPipeline -gs "AggregationProcessor/MappingImageSettings/Output0/TextureHeight" $aggregationPipeline`;
    print ("TextureHeight: " + $textureHeight + "\n");

    // add material casters (ambientColor, color, specularColor and normalCamera)
    $bCasterAdded = `SimplygonPipeline -amc "ColorCaster" $aggregationPipeline`;
    $bResult = `SimplygonPipeline -ss "MaterialCaster/0/ColorCasterSettings/MaterialChannel" -v "ambientColor" $aggregationPipeline`;
    $colorCaster0 = `SimplygonPipeline -gs "MaterialCaster/0/ColorCasterSettings/MaterialChannel" $aggregationPipeline`;
    print ("ColorCaster0: " + $colorCaster0 + "\n");

    $bCasterAdded = `SimplygonPipeline -amc "ColorCaster" $aggregationPipeline`;
    $bResult = `SimplygonPipeline -ss "MaterialCaster/1/ColorCasterSettings/MaterialChannel" -v "color" $aggregationPipeline`;
    $colorCaster1 = `SimplygonPipeline -gs "MaterialCaster/1/ColorCasterSettings/MaterialChannel" $aggregationPipeline`;
    print ("ColorCaster1: " + $colorCaster1 + "\n");

    $bCasterAdded = `SimplygonPipeline -amc "ColorCaster" $aggregationPipeline`;
    $bResult = `SimplygonPipeline -ss "MaterialCaster/2/ColorCasterSettings/MaterialChannel" -v "specularColor" $aggregationPipeline`;
    $colorCaster2 = `SimplygonPipeline -gs "MaterialCaster/2/ColorCasterSettings/MaterialChannel" $aggregationPipeline`;
    print ("ColorCaster2: " + $colorCaster2 + "\n");

    // note: normal caster for normals!
    $bCasterAdded = `SimplygonPipeline -amc "NormalCaster" $aggregationPipeline`;
    $bResult = `SimplygonPipeline -ss "MaterialCaster/3/NormalCasterSettings/MaterialChannel" -v "normalCamera" $aggregationPipeline`;
    $normalCaster3 = `SimplygonPipeline -gs "MaterialCaster/3/NormalCasterSettings/MaterialChannel" $aggregationPipeline`;
    print ("NormalCaster3: " + $normalCaster3 + "\n");

    // set the correct tangent space type,
    // in this example we use tangent space normals
    $bResult = `SimplygonPipeline -ss "MaterialCaster/3/NormalCasterSettings/GenerateTangentSpaceNormals" -v true $aggregationPipeline`;
    $generateTangentSpaceNormals = `SimplygonPipeline -gs "MaterialCaster/3/NormalCasterSettings/GenerateTangentSpaceNormals" $aggregationPipeline`;
    print ("GenerateTangentSpaceNormals: " + $generateTangentSpaceNormals + "\n");
}

if($bMaterialBake)
{
    $bResult = `SimplygonPipeline -s "D:/Pipelines/Aggregation_With_Baking.json" $aggregationPipeline`;
}
else
{
    $bResult = `SimplygonPipeline -s "D:/Pipelines/Aggregation.json" $aggregationPipeline`;
}

// clear all pipelines
SimplygonPipeline -cl;

# Next steps

Get to know how to use the Aggregation Pipeline: