This example shows how to use the displacement caster.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include <string>
#include <stdlib.h>
#include <filesystem>
#include <future>
#include "SimplygonLoader.h"
void DisplacementCasting(Simplygon::ISimplygon* sg)
{
Simplygon::spSceneImporter sgSceneImporter = sg->CreateSceneImporter();
sgSceneImporter->SetImportFilePath( "../Assets/Wall/wall.obj" );
if(!sgSceneImporter->RunImport())
throw std::exception("Failed to load Wall/wall.obj.");
Simplygon::spScene sgScene = sgSceneImporter->GetScene();
// Create the reduction processor.
Simplygon::spReductionProcessor sgReductionProcessor = sg->CreateReductionProcessor();
sgReductionProcessor->SetScene( sgScene );
Simplygon::spReductionSettings sgReductionSettings = sgReductionProcessor->GetReductionSettings();
Simplygon::spMappingImageSettings sgMappingImageSettings = sgReductionProcessor->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 );
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 reduction process.
sgReductionProcessor->RunProcessing();
// Setup and run the displacement material casting.
Simplygon::spDisplacementCaster sgDisplacementCaster = sg->CreateDisplacementCaster();
sgDisplacementCaster->SetMappingImage( sgReductionProcessor->GetMappingImage() );
sgDisplacementCaster->SetSourceMaterials( sgScene->GetMaterialTable() );
sgDisplacementCaster->SetSourceTextures( sgScene->GetTextureTable() );
sgDisplacementCaster->SetOutputFilePath( "DisplacementTexture" );
Simplygon::spDisplacementCasterSettings sgDisplacementCasterSettings = sgDisplacementCaster->GetDisplacementCasterSettings();
sgDisplacementCasterSettings->SetMaterialChannel( "Displacement" );
sgDisplacementCasterSettings->SetGenerateScalarDisplacement( true );
sgDisplacementCasterSettings->SetGenerateTangentSpaceDisplacement( false );
sgDisplacementCasterSettings->SetOutputImageFileFormat( Simplygon::EImageOutputFormat::PNG );
sgDisplacementCasterSettings->SetOutputPixelFormat( Simplygon::EPixelFormat::R8G8B8 );
sgDisplacementCaster->RunProcessing();
std::string displacementTextureFilePath = sgDisplacementCaster->GetOutputFilePath();
// Update scene with new casted texture.
Simplygon::spMaterialTable sgMaterialTable = sg->CreateMaterialTable();
Simplygon::spTextureTable sgTextureTable = sg->CreateTextureTable();
Simplygon::spMaterial sgMaterial = sg->CreateMaterial();
Simplygon::spTexture sgDisplacementTexture = sg->CreateTexture();
sgDisplacementTexture->SetName( "Displacement" );
sgDisplacementTexture->SetFilePath( displacementTextureFilePath.c_str() );
sgTextureTable->AddTexture( sgDisplacementTexture );
Simplygon::spShadingTextureNode sgDisplacementTextureShadingNode = sg->CreateShadingTextureNode();
sgDisplacementTextureShadingNode->SetTexCoordLevel( 0 );
sgDisplacementTextureShadingNode->SetTextureName( "Displacement" );
sgMaterial->AddMaterialChannel( "Displacement" );
sgMaterial->SetShadingNetwork( "Displacement", sgDisplacementTextureShadingNode );
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);
}
DisplacementCasting(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 DisplacementCasting(Simplygon.ISimplygon sg)
{
using (Simplygon.spSceneImporter sgSceneImporter = sg.CreateSceneImporter())
{
sgSceneImporter.SetImportFilePath( "../Assets/Wall/wall.obj" );
if(!sgSceneImporter.RunImport())
throw new System.Exception("Failed to load Wall/wall.obj.");
Simplygon.spScene sgScene = sgSceneImporter.GetScene();
// Create the reduction processor.
using (Simplygon.spReductionProcessor sgReductionProcessor = sg.CreateReductionProcessor())
{
sgReductionProcessor.SetScene( sgScene );
using (Simplygon.spReductionSettings sgReductionSettings = sgReductionProcessor.GetReductionSettings())
using (Simplygon.spMappingImageSettings sgMappingImageSettings = sgReductionProcessor.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 );
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 reduction process.
sgReductionProcessor.RunProcessing();
// Setup and run the displacement material casting.
string displacementTextureFilePath;
using (Simplygon.spDisplacementCaster sgDisplacementCaster = sg.CreateDisplacementCaster())
{
sgDisplacementCaster.SetMappingImage( sgReductionProcessor.GetMappingImage() );
sgDisplacementCaster.SetSourceMaterials( sgScene.GetMaterialTable() );
sgDisplacementCaster.SetSourceTextures( sgScene.GetTextureTable() );
sgDisplacementCaster.SetOutputFilePath( "DisplacementTexture" );
using (Simplygon.spDisplacementCasterSettings sgDisplacementCasterSettings = sgDisplacementCaster.GetDisplacementCasterSettings())
{
sgDisplacementCasterSettings.SetMaterialChannel( "Displacement" );
sgDisplacementCasterSettings.SetGenerateScalarDisplacement( true );
sgDisplacementCasterSettings.SetGenerateTangentSpaceDisplacement( false );
sgDisplacementCasterSettings.SetOutputImageFileFormat( Simplygon.EImageOutputFormat.PNG );
sgDisplacementCasterSettings.SetOutputPixelFormat( Simplygon.EPixelFormat.R8G8B8 );
}
sgDisplacementCaster.RunProcessing();
displacementTextureFilePath = sgDisplacementCaster.GetOutputFilePath();
}
// Update scene with new casted texture.
using (Simplygon.spMaterialTable sgMaterialTable = sg.CreateMaterialTable())
using (Simplygon.spTextureTable sgTextureTable = sg.CreateTextureTable())
using (Simplygon.spMaterial sgMaterial = sg.CreateMaterial())
{
using(Simplygon.spTexture sgDisplacementTexture = sg.CreateTexture())
{
sgDisplacementTexture.SetName( "Displacement" );
sgDisplacementTexture.SetFilePath( displacementTextureFilePath );
sgTextureTable.AddTexture( sgDisplacementTexture );
}
using(Simplygon.spShadingTextureNode sgDisplacementTextureShadingNode = sg.CreateShadingTextureNode())
{
sgDisplacementTextureShadingNode.SetTexCoordLevel( 0 );
sgDisplacementTextureShadingNode.SetTextureName( "Displacement" );
sgMaterial.AddMaterialChannel( "Displacement" );
sgMaterial.SetShadingNetwork( "Displacement", sgDisplacementTextureShadingNode );
}
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;
DisplacementCasting(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 DisplacementCasting(sg: Simplygon.ISimplygon):
sgSceneImporter = sg.CreateSceneImporter()
sgSceneImporter.SetImportFilePath( '../Assets/Wall/wall.obj' )
if not sgSceneImporter.RunImport():
raise Exception('Failed to load Wall/wall.obj.')
sgScene = sgSceneImporter.GetScene()
# Create the reduction processor.
sgReductionProcessor = sg.CreateReductionProcessor()
sgReductionProcessor.SetScene( sgScene )
sgReductionSettings = sgReductionProcessor.GetReductionSettings()
sgMappingImageSettings = sgReductionProcessor.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 )
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 reduction process.
sgReductionProcessor.RunProcessing()
# Setup and run the displacement material casting.
sgDisplacementCaster = sg.CreateDisplacementCaster()
sgDisplacementCaster.SetMappingImage( sgReductionProcessor.GetMappingImage() )
sgDisplacementCaster.SetSourceMaterials( sgScene.GetMaterialTable() )
sgDisplacementCaster.SetSourceTextures( sgScene.GetTextureTable() )
sgDisplacementCaster.SetOutputFilePath( 'DisplacementTexture' )
sgDisplacementCasterSettings = sgDisplacementCaster.GetDisplacementCasterSettings()
sgDisplacementCasterSettings.SetMaterialChannel( 'Displacement' )
sgDisplacementCasterSettings.SetGenerateScalarDisplacement( True )
sgDisplacementCasterSettings.SetGenerateTangentSpaceDisplacement( False )
sgDisplacementCasterSettings.SetOutputImageFileFormat( Simplygon.EImageOutputFormat_PNG )
sgDisplacementCasterSettings.SetOutputPixelFormat( Simplygon.EPixelFormat_R8G8B8 )
sgDisplacementCaster.RunProcessing()
displacementTextureFilePath = sgDisplacementCaster.GetOutputFilePath()
# Update scene with new casted texture.
sgMaterialTable = sg.CreateMaterialTable()
sgTextureTable = sg.CreateTextureTable()
sgMaterial = sg.CreateMaterial()
sgDisplacementTexture = sg.CreateTexture()
sgDisplacementTexture.SetName( 'Displacement' )
sgDisplacementTexture.SetFilePath( displacementTextureFilePath )
sgTextureTable.AddTexture( sgDisplacementTexture )
sgDisplacementTextureShadingNode = sg.CreateShadingTextureNode()
sgDisplacementTextureShadingNode.SetTexCoordLevel( 0 )
sgDisplacementTextureShadingNode.SetTextureName( 'Displacement' )
sgMaterial.AddMaterialChannel( 'Displacement' )
sgMaterial.SetShadingNetwork( 'Displacement', sgDisplacementTextureShadingNode )
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())
DisplacementCasting(sg)
sg = None
gc.collect()