This example shows how to use the Reduction pipeline with material casting.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include <string>
#include <stdlib.h>
#include <filesystem>
#include <future>
#include "SimplygonLoader.h"
void RunReductionWithMaterialCasting(Simplygon::ISimplygon* sg)
{
Simplygon::spSceneImporter sgSceneImporter = sg->CreateSceneImporter();
sgSceneImporter->SetImportFilePath( "../Assets/SimplygonMan/SimplygonMan.obj" );
if(!sgSceneImporter->RunImport())
throw std::exception("Failed to load SimplygonMan/SimplygonMan.obj.");
Simplygon::spScene sgScene = sgSceneImporter->GetScene();
// Create the reduction pipeline.
Simplygon::spReductionPipeline sgReductionPipeline = sg->CreateReductionPipeline();
Simplygon::spReductionSettings sgReductionSettings = sgReductionPipeline->GetReductionSettings();
Simplygon::spMappingImageSettings sgMappingImageSettings = sgReductionPipeline->GetMappingImageSettings();
// Set reduction target to triangle ratio with a ratio of 50%.
sgReductionSettings->SetReductionTargets( Simplygon::EStopCondition::All, true, false, false, false );
sgReductionSettings->SetReductionTargetTriangleRatio( 0.5f );
// Generates a mapping image which is used after the reduction to cast new materials to the new
// reduced object.
sgMappingImageSettings->SetGenerateMappingImage( true );
sgMappingImageSettings->SetApplyNewMaterialIds( true );
sgMappingImageSettings->SetGenerateTangents( true );
sgMappingImageSettings->SetUseFullRetexturing( true );
sgMappingImageSettings->SetTexCoordGeneratorType( Simplygon::ETexcoordGeneratorType::ChartAggregator );
Simplygon::spChartAggregatorSettings sgChartAggregatorSettings = sgMappingImageSettings->GetChartAggregatorSettings();
// Enable the chart aggregator and reuse UV space.
sgChartAggregatorSettings->SetChartAggregatorMode( Simplygon::EChartAggregatorMode::SurfaceArea );
sgChartAggregatorSettings->SetSeparateOverlappingCharts( false );
Simplygon::spMappingImageOutputMaterialSettings sgOutputMaterialSettings = sgMappingImageSettings->GetOutputMaterialSettings(0);
// Setting the size of the output material for the mapping image. This will be the output size of the
// textures when we do material casting in a later stage.
sgOutputMaterialSettings->SetTextureWidth( 2048 );
sgOutputMaterialSettings->SetTextureHeight( 2048 );
// Add diffuse material caster to pipeline.
Simplygon::spColorCaster sgDiffuseCaster = sg->CreateColorCaster();
Simplygon::spColorCasterSettings sgDiffuseCasterSettings = sgDiffuseCaster->GetColorCasterSettings();
sgDiffuseCasterSettings->SetMaterialChannel( "Diffuse" );
sgDiffuseCasterSettings->SetOutputImageFileFormat( Simplygon::EImageOutputFormat::PNG );
sgReductionPipeline->AddMaterialCaster( sgDiffuseCaster, 0 );
// Add normals material caster to pipeline.
Simplygon::spNormalCaster sgNormalsCaster = sg->CreateNormalCaster();
Simplygon::spNormalCasterSettings sgNormalsCasterSettings = sgNormalsCaster->GetNormalCasterSettings();
sgNormalsCasterSettings->SetMaterialChannel( "Normals" );
sgNormalsCasterSettings->SetGenerateTangentSpaceNormals( true );
sgNormalsCasterSettings->SetOutputImageFileFormat( Simplygon::EImageOutputFormat::PNG );
sgReductionPipeline->AddMaterialCaster( sgNormalsCaster, 0 );
// Start the reduction pipeline.
sgReductionPipeline->RunScene(sgScene, Simplygon::EPipelineRunMode::RunInThisProcess);
// Get the processed scene.
Simplygon::spScene sgProcessedScene = sgReductionPipeline->GetProcessedScene();
Simplygon::spSceneExporter sgSceneExporter = sg->CreateSceneExporter();
sgSceneExporter->SetScene(sgProcessedScene);
sgSceneExporter->SetExportFilePath( "ReductionOutput.fbx" );
if(!sgSceneExporter->RunExport())
throw std::exception("Failed to save ReductionOutput.fbx.");
}
int main()
{
Simplygon::ISimplygon* sg = NULL;
Simplygon::EErrorCodes initval = Simplygon::Initialize( &sg );
if( initval != Simplygon::EErrorCodes::NoError )
{
return int(initval);
}
RunReductionWithMaterialCasting(sg);
Simplygon::Deinitialize(sg);
return 0;
}
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.IO;
using System.Threading.Tasks;
public class Program
{
static void RunReductionWithMaterialCasting(Simplygon.ISimplygon sg)
{
using (Simplygon.spSceneImporter sgSceneImporter = sg.CreateSceneImporter())
{
sgSceneImporter.SetImportFilePath( "../Assets/SimplygonMan/SimplygonMan.obj" );
if(!sgSceneImporter.RunImport())
throw new System.Exception("Failed to load SimplygonMan/SimplygonMan.obj.");
Simplygon.spScene sgScene = sgSceneImporter.GetScene();
// Create the reduction pipeline.
using (Simplygon.spReductionPipeline sgReductionPipeline = sg.CreateReductionPipeline())
{
using (Simplygon.spReductionSettings sgReductionSettings = sgReductionPipeline.GetReductionSettings())
using (Simplygon.spMappingImageSettings sgMappingImageSettings = sgReductionPipeline.GetMappingImageSettings())
{
// Set reduction target to triangle ratio with a ratio of 50%.
sgReductionSettings.SetReductionTargets( Simplygon.EStopCondition.All, true, false, false, false );
sgReductionSettings.SetReductionTargetTriangleRatio( 0.5f );
// Generates a mapping image which is used after the reduction to cast new materials to the new
// reduced object.
sgMappingImageSettings.SetGenerateMappingImage( true );
sgMappingImageSettings.SetApplyNewMaterialIds( true );
sgMappingImageSettings.SetGenerateTangents( true );
sgMappingImageSettings.SetUseFullRetexturing( true );
sgMappingImageSettings.SetTexCoordGeneratorType( Simplygon.ETexcoordGeneratorType.ChartAggregator );
using (Simplygon.spChartAggregatorSettings sgChartAggregatorSettings = sgMappingImageSettings.GetChartAggregatorSettings())
{
// Enable the chart aggregator and reuse UV space.
sgChartAggregatorSettings.SetChartAggregatorMode( Simplygon.EChartAggregatorMode.SurfaceArea );
sgChartAggregatorSettings.SetSeparateOverlappingCharts( false );
}
using (Simplygon.spMappingImageOutputMaterialSettings sgOutputMaterialSettings = sgMappingImageSettings.GetOutputMaterialSettings(0))
{
// Setting the size of the output material for the mapping image. This will be the output size of the
// textures when we do material casting in a later stage.
sgOutputMaterialSettings.SetTextureWidth( 2048 );
sgOutputMaterialSettings.SetTextureHeight( 2048 );
}
}
// Add diffuse material caster to pipeline.
using (Simplygon.spColorCaster sgDiffuseCaster = sg.CreateColorCaster())
{
using (Simplygon.spColorCasterSettings sgDiffuseCasterSettings = sgDiffuseCaster.GetColorCasterSettings())
{
sgDiffuseCasterSettings.SetMaterialChannel( "Diffuse" );
sgDiffuseCasterSettings.SetOutputImageFileFormat( Simplygon.EImageOutputFormat.PNG );
}
sgReductionPipeline.AddMaterialCaster( sgDiffuseCaster, 0 );
}
// Add normals material caster to pipeline.
using (Simplygon.spNormalCaster sgNormalsCaster = sg.CreateNormalCaster())
{
using (Simplygon.spNormalCasterSettings sgNormalsCasterSettings = sgNormalsCaster.GetNormalCasterSettings())
{
sgNormalsCasterSettings.SetMaterialChannel( "Normals" );
sgNormalsCasterSettings.SetGenerateTangentSpaceNormals( true );
sgNormalsCasterSettings.SetOutputImageFileFormat( Simplygon.EImageOutputFormat.PNG );
}
sgReductionPipeline.AddMaterialCaster( sgNormalsCaster, 0 );
}
// Start the reduction pipeline.
sgReductionPipeline.RunScene(sgScene, Simplygon.EPipelineRunMode.RunInThisProcess);
// Get the processed scene.
using (Simplygon.spScene sgProcessedScene = sgReductionPipeline.GetProcessedScene())
{
using (Simplygon.spSceneExporter sgSceneExporter = sg.CreateSceneExporter())
{
sgSceneExporter.SetScene(sgProcessedScene);
sgSceneExporter.SetExportFilePath( "ReductionOutput.fbx" );
if(!sgSceneExporter.RunExport())
throw new System.Exception("Failed to save ReductionOutput.fbx.");
}
}
}
}
}
static int Main(string[] args)
{
using (var sg = Simplygon.Loader.InitSimplygon(out var errorCode, out var errorMessage))
{
if (errorCode != Simplygon.EErrorCodes.NoError)
return (int)errorCode;
RunReductionWithMaterialCasting(sg);
}
return 0;
}
}
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
import math
import os
import sys
import glob
import gc
import threading
from pathlib import Path
from simplygon import simplygon_loader
from simplygon import Simplygon
def RunReductionWithMaterialCasting(sg: Simplygon.ISimplygon):
sgSceneImporter = sg.CreateSceneImporter()
sgSceneImporter.SetImportFilePath( '../Assets/SimplygonMan/SimplygonMan.obj' )
if not sgSceneImporter.RunImport():
raise Exception('Failed to load SimplygonMan/SimplygonMan.obj.')
sgScene = sgSceneImporter.GetScene()
# Create the reduction pipeline.
sgReductionPipeline = sg.CreateReductionPipeline()
sgReductionSettings = sgReductionPipeline.GetReductionSettings()
sgMappingImageSettings = sgReductionPipeline.GetMappingImageSettings()
# Set reduction target to triangle ratio with a ratio of 50%.
sgReductionSettings.SetReductionTargets( Simplygon.EStopCondition_All, True, False, False, False )
sgReductionSettings.SetReductionTargetTriangleRatio( 0.5 )
# Generates a mapping image which is used after the reduction to cast new materials to the new
# reduced object.
sgMappingImageSettings.SetGenerateMappingImage( True )
sgMappingImageSettings.SetApplyNewMaterialIds( True )
sgMappingImageSettings.SetGenerateTangents( True )
sgMappingImageSettings.SetUseFullRetexturing( True )
sgMappingImageSettings.SetTexCoordGeneratorType( Simplygon.ETexcoordGeneratorType_ChartAggregator )
sgChartAggregatorSettings = sgMappingImageSettings.GetChartAggregatorSettings()
# Enable the chart aggregator and reuse UV space.
sgChartAggregatorSettings.SetChartAggregatorMode( Simplygon.EChartAggregatorMode_SurfaceArea )
sgChartAggregatorSettings.SetSeparateOverlappingCharts( False )
sgOutputMaterialSettings = sgMappingImageSettings.GetOutputMaterialSettings(0)
# Setting the size of the output material for the mapping image. This will be the output size of the
# textures when we do material casting in a later stage.
sgOutputMaterialSettings.SetTextureWidth( 2048 )
sgOutputMaterialSettings.SetTextureHeight( 2048 )
# Add diffuse material caster to pipeline.
sgDiffuseCaster = sg.CreateColorCaster()
sgDiffuseCasterSettings = sgDiffuseCaster.GetColorCasterSettings()
sgDiffuseCasterSettings.SetMaterialChannel( "Diffuse" )
sgDiffuseCasterSettings.SetOutputImageFileFormat( Simplygon.EImageOutputFormat_PNG )
sgReductionPipeline.AddMaterialCaster( sgDiffuseCaster, 0 )
# Add normals material caster to pipeline.
sgNormalsCaster = sg.CreateNormalCaster()
sgNormalsCasterSettings = sgNormalsCaster.GetNormalCasterSettings()
sgNormalsCasterSettings.SetMaterialChannel( "Normals" )
sgNormalsCasterSettings.SetGenerateTangentSpaceNormals( True )
sgNormalsCasterSettings.SetOutputImageFileFormat( Simplygon.EImageOutputFormat_PNG )
sgReductionPipeline.AddMaterialCaster( sgNormalsCaster, 0 )
# Start the reduction pipeline.
sgReductionPipeline.RunScene(sgScene, Simplygon.EPipelineRunMode_RunInThisProcess)
# Get the processed scene.
sgProcessedScene = sgReductionPipeline.GetProcessedScene()
sgSceneExporter = sg.CreateSceneExporter()
sgSceneExporter.SetScene(sgProcessedScene)
sgSceneExporter.SetExportFilePath( 'ReductionOutput.fbx' )
if not sgSceneExporter.RunExport():
raise Exception('Failed to save ReductionOutput.fbx.')
if __name__ == '__main__':
sg = simplygon_loader.init_simplygon()
if sg is None:
exit(Simplygon.GetLastInitializationError())
RunReductionWithMaterialCasting(sg)
sg = None
gc.collect()