This example shows how to use the vertex color caster.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include <string>
#include <stdlib.h>
#include <filesystem>
#include <future>
#include "SimplygonLoader.h"
void VertexColorCasting(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 aggregation processor.
Simplygon::spAggregationProcessor sgAggregationProcessor = sg->CreateAggregationProcessor();
sgAggregationProcessor->SetScene( sgScene );
Simplygon::spAggregationSettings sgAggregationSettings = sgAggregationProcessor->GetAggregationSettings();
Simplygon::spMappingImageSettings sgMappingImageSettings = sgAggregationProcessor->GetMappingImageSettings();
// Merge all geometries into a single geometry.
sgAggregationSettings->SetMergeGeometries( true );
// Generates a mapping image which is used after the aggregation to cast new materials to the new
// aggregated object.
sgMappingImageSettings->SetGenerateMappingImage( true );
sgMappingImageSettings->SetApplyNewMaterialIds( true );
sgMappingImageSettings->SetGenerateTangents( true );
sgMappingImageSettings->SetUseFullRetexturing( true );
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 );
// Start the aggregation process.
sgAggregationProcessor->RunProcessing();
// Setup and run the vertex color material casting.
Simplygon::spVertexColorCaster sgDiffuseCaster = sg->CreateVertexColorCaster();
sgDiffuseCaster->SetMappingImage( sgAggregationProcessor->GetMappingImage() );
sgDiffuseCaster->SetSourceMaterials( sgScene->GetMaterialTable() );
sgDiffuseCaster->SetSourceTextures( sgScene->GetTextureTable() );
sgDiffuseCaster->SetScene( sgScene );
Simplygon::spVertexColorCasterSettings sgDiffuseCasterSettings = sgDiffuseCaster->GetVertexColorCasterSettings();
sgDiffuseCasterSettings->SetMaterialChannel( "Diffuse" );
sgDiffuseCasterSettings->SetOutputColorName( "DiffuseAsVertexColor" );
sgDiffuseCaster->RunProcessing();
// Update scene with new casted vertex colors.
Simplygon::spMaterialTable sgMaterialTable = sg->CreateMaterialTable();
Simplygon::spTextureTable sgTextureTable = sg->CreateTextureTable();
Simplygon::spMaterial sgMaterial = sg->CreateMaterial();
Simplygon::spShadingColorNode sgDiffuseAsVertexColorColorShadingNode = sg->CreateShadingColorNode();
sgDiffuseAsVertexColorColorShadingNode->SetColor( 1.0f, 1.0f, 1.0f, 1.0f );
sgMaterial->AddMaterialChannel( "DiffuseAsVertexColor" );
sgMaterial->SetShadingNetwork( "DiffuseAsVertexColor", sgDiffuseAsVertexColorColorShadingNode );
sgMaterialTable->AddMaterial( sgMaterial );
sgScene->GetTextureTable()->Clear();
sgScene->GetMaterialTable()->Clear();
sgScene->GetTextureTable()->Copy(sgTextureTable);
sgScene->GetMaterialTable()->Copy(sgMaterialTable);
Simplygon::spSceneExporter sgSceneExporter = sg->CreateSceneExporter();
sgSceneExporter->SetScene(sgScene);
sgSceneExporter->SetExportFilePath( "Output.fbx" );
if(!sgSceneExporter->RunExport())
throw std::exception("Failed to save Output.fbx.");
}
int main()
{
Simplygon::ISimplygon* sg = NULL;
Simplygon::EErrorCodes initval = Simplygon::Initialize( &sg );
if( initval != Simplygon::EErrorCodes::NoError )
{
return int(initval);
}
VertexColorCasting(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 VertexColorCasting(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 aggregation processor.
using (Simplygon.spAggregationProcessor sgAggregationProcessor = sg.CreateAggregationProcessor())
{
sgAggregationProcessor.SetScene( sgScene );
using (Simplygon.spAggregationSettings sgAggregationSettings = sgAggregationProcessor.GetAggregationSettings())
using (Simplygon.spMappingImageSettings sgMappingImageSettings = sgAggregationProcessor.GetMappingImageSettings())
{
// Merge all geometries into a single geometry.
sgAggregationSettings.SetMergeGeometries( true );
// Generates a mapping image which is used after the aggregation to cast new materials to the new
// aggregated object.
sgMappingImageSettings.SetGenerateMappingImage( true );
sgMappingImageSettings.SetApplyNewMaterialIds( true );
sgMappingImageSettings.SetGenerateTangents( true );
sgMappingImageSettings.SetUseFullRetexturing( true );
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 );
}
}
// Start the aggregation process.
sgAggregationProcessor.RunProcessing();
// Setup and run the vertex color material casting.
using (Simplygon.spVertexColorCaster sgDiffuseCaster = sg.CreateVertexColorCaster())
{
sgDiffuseCaster.SetMappingImage( sgAggregationProcessor.GetMappingImage() );
sgDiffuseCaster.SetSourceMaterials( sgScene.GetMaterialTable() );
sgDiffuseCaster.SetSourceTextures( sgScene.GetTextureTable() );
sgDiffuseCaster.SetScene( sgScene );
using (Simplygon.spVertexColorCasterSettings sgDiffuseCasterSettings = sgDiffuseCaster.GetVertexColorCasterSettings())
{
sgDiffuseCasterSettings.SetMaterialChannel( "Diffuse" );
sgDiffuseCasterSettings.SetOutputColorName( "DiffuseAsVertexColor" );
}
sgDiffuseCaster.RunProcessing();
}
// Update scene with new casted vertex colors.
using (Simplygon.spMaterialTable sgMaterialTable = sg.CreateMaterialTable())
using (Simplygon.spTextureTable sgTextureTable = sg.CreateTextureTable())
using (Simplygon.spMaterial sgMaterial = sg.CreateMaterial())
{
using(Simplygon.spShadingColorNode sgDiffuseAsVertexColorColorShadingNode = sg.CreateShadingColorNode())
{
sgDiffuseAsVertexColorColorShadingNode.SetColor( 1.0f, 1.0f, 1.0f, 1.0f );
sgMaterial.AddMaterialChannel( "DiffuseAsVertexColor" );
sgMaterial.SetShadingNetwork( "DiffuseAsVertexColor", sgDiffuseAsVertexColorColorShadingNode );
}
sgMaterialTable.AddMaterial( sgMaterial );
sgScene.GetTextureTable().Clear();
sgScene.GetMaterialTable().Clear();
sgScene.GetTextureTable().Copy(sgTextureTable);
sgScene.GetMaterialTable().Copy(sgMaterialTable);
}
}
using (Simplygon.spSceneExporter sgSceneExporter = sg.CreateSceneExporter())
{
sgSceneExporter.SetScene(sgScene);
sgSceneExporter.SetExportFilePath( "Output.fbx" );
if(!sgSceneExporter.RunExport())
throw new System.Exception("Failed to save Output.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;
VertexColorCasting(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 VertexColorCasting(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 aggregation processor.
sgAggregationProcessor = sg.CreateAggregationProcessor()
sgAggregationProcessor.SetScene( sgScene )
sgAggregationSettings = sgAggregationProcessor.GetAggregationSettings()
sgMappingImageSettings = sgAggregationProcessor.GetMappingImageSettings()
# Merge all geometries into a single geometry.
sgAggregationSettings.SetMergeGeometries( True )
# Generates a mapping image which is used after the aggregation to cast new materials to the new
# aggregated object.
sgMappingImageSettings.SetGenerateMappingImage( True )
sgMappingImageSettings.SetApplyNewMaterialIds( True )
sgMappingImageSettings.SetGenerateTangents( True )
sgMappingImageSettings.SetUseFullRetexturing( True )
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 )
# Start the aggregation process.
sgAggregationProcessor.RunProcessing()
# Setup and run the vertex color material casting.
sgDiffuseCaster = sg.CreateVertexColorCaster()
sgDiffuseCaster.SetMappingImage( sgAggregationProcessor.GetMappingImage() )
sgDiffuseCaster.SetSourceMaterials( sgScene.GetMaterialTable() )
sgDiffuseCaster.SetSourceTextures( sgScene.GetTextureTable() )
sgDiffuseCaster.SetScene( sgScene )
sgDiffuseCasterSettings = sgDiffuseCaster.GetVertexColorCasterSettings()
sgDiffuseCasterSettings.SetMaterialChannel( 'Diffuse' )
sgDiffuseCasterSettings.SetOutputColorName( "DiffuseAsVertexColor" )
sgDiffuseCaster.RunProcessing()
# Update scene with new casted vertex colors.
sgMaterialTable = sg.CreateMaterialTable()
sgTextureTable = sg.CreateTextureTable()
sgMaterial = sg.CreateMaterial()
sgDiffuseAsVertexColorColorShadingNode = sg.CreateShadingColorNode()
sgDiffuseAsVertexColorColorShadingNode.SetColor( 1.0, 1.0, 1.0, 1.0 )
sgMaterial.AddMaterialChannel( 'DiffuseAsVertexColor' )
sgMaterial.SetShadingNetwork( 'DiffuseAsVertexColor', sgDiffuseAsVertexColorColorShadingNode )
sgMaterialTable.AddMaterial( sgMaterial )
sgScene.GetTextureTable().Clear()
sgScene.GetMaterialTable().Clear()
sgScene.GetTextureTable().Copy(sgTextureTable)
sgScene.GetMaterialTable().Copy(sgMaterialTable)
sgSceneExporter = sg.CreateSceneExporter()
sgSceneExporter.SetScene(sgScene)
sgSceneExporter.SetExportFilePath( 'Output.fbx' )
if not sgSceneExporter.RunExport():
raise Exception('Failed to save Output.fbx.')
if __name__ == '__main__':
sg = simplygon_loader.init_simplygon()
if sg is None:
exit(Simplygon.GetLastInitializationError())
VertexColorCasting(sg)
sg = None
gc.collect()