Material casting

<< Click to Display Table of Contents >>

Navigation:  Simplygon API 7.1 User guide >

Material casting

The material casting is a post-process of both the reduction and remeshing processing, where the caller application can use the mapping image data to "cast" material and texture data from the original onto the processed geometry. The casting process requires that the processing object has created a mapping image during the main processing.

When creating new maps using material casting, opacity values can be baked down into either diffuse map's alpha value or baked down into a separate opacity map (version 5.2).

IMaterialCaster

All casting objects are derived from the IMaterialCaster class, which implements the basic processing parameters of all casting objects. IMaterialCaster contains settings for the output image format.

When using a caster designed to transfer materials from the original to the output mesh, a reference to the original material table must be set. If the input materials in the material table are defined using shading networks rather than the legacy material system, a texture table containing all textures referenced by the texture nodes in the shading networks must also be created and supplied.

For further information, see the material casting example.

IAmbientOcclusionCaster

The IAmbientOcclusionCaster is used to generate an ambient occlusion map. The occlusion values are generated by ray casting from the original geometries, which will allow preservation of occlusion detail on an otherwise heavily optimized mesh. The quality and noise of the ambient occlusion mesh can be controlled by setting the number of rays per pixel, as well as the falloff and occlusion multiplier for the rays. Note that increasing the multisampling in the mapping image settings can be used to further reduce noise.

Example

The following example shows how to use the IAmbientOcclusionCaster to generate an ambient occlusion map:

 
void GenerateAOMap( spMappingImage mapping_image )
{
   // Cast AO texture data.
   spAmbientOcclusionCaster cast = sg->CreateAmbientOcclusionCaster();      
   // The mapping image from the process.
   cast->SetMappingImage( mapping_image );
   cast->SetRaysPerPixel( 256 );
   cast->SetOutputChannels( 1 ); // 1 channel
   cast->SetOutputChannelBitDepth( 8 ); // 8 bits per channel
   cast->SetOutputFilePath( "ao_out.png" );
   cast->CastMaterials();
}
 

IColorCaster

The IColorCaster is used to cast material color information from the original geometries. The caster can cast ambient, diffuse, specular, emmisive and opacity color data. It is possible to add color data, which is set using the ColorType parameter.

When casting the specular channel, Simplygon will cast the shininess of the input materials into the alpha channel of the output texture if one is available. To get the correct result, the alpha component of the base-color of the destination material must be set to 128 to ensure correct scaling of the shininess factor when the texture is created. For an example of how to bake shininess, see the reduction example.

Simplygon supports textures both with and without sRGB color format. Call SetsRGB to set whether textures are in sRGB format.

 

Example

The following example shows how to use the IColorCaster to cast diffuse color data

 
void CastDiffuseColorData( spMaterial output_material, spMappingImage mapping_image, spMaterialTable source_materials )
{
   // Cast diffuse texture data using a color caster.
   spColorCaster cast = sg->CreateColorCaster();      
   cast->SetColorType( SG_MATERIAL_CHANNEL_DIFFUSE );
   cast->SetSourceMaterials( source_materials );  
   // The mapping image from the process.
   cast->SetMappingImage( mapping_image );
   cast->SetOutputChannels( 3 ); // RGB, 3 channels
   cast->SetOutputChannelBitDepth( 8 ); // 8 bits per channel, 24bpp
   cast->SetOutputFilePath( "diffuse_out.png" );
   cast->CastMaterials();
   // Set the diffuse multiplier for the texture.
   // 1 means it will not differ from original texture,      
   // Example: 0 would ignore a specified color and
   // 2 would make a color twice as pronounced as the others.    
   output_material->SetColor( SG_MATERIAL_CHANNEL_DIFFUSE, 1.0, 1.0, 1.0, 1.0 );
 
   // Set material to point to created texture filename.    
   output_material->SetTexture( SG_MATERIAL_CHANNEL_DIFFUSE, "diffuse_out.png" );
}
 

IDisplacementCaster

The IDisplacementCaster is used to generate a displacement map containing the delta-values between the original and the processed geometries. The caster can be configured to generate either a scalar displacement value, or a displacement vector. The values are divided by a scaling value and clamped into the [-1,1] range, or clamped to length 1 in the case of vectors, before being stored in the map.

 

Example

The following example shows how to use the IDisplacementCaster to generate a displacement map:

 
void GenerateDisplacementMap( spMappingImage mapping_image )
{
   // Cast displacement texture data.
   spDisplacementCaster cast = sg->CreateDisplacementCaster();      
   // The mapping image from the process.
   cast->SetMappingImage( mapping_image );
   cast->SetGenerateScalarDisplacement( true );
   cast->SetDistanceScaling( 0.1 ); // Divide values by 0.1
   cast->SetOutputChannels( 1 ); // 1 channel
   cast->SetOutputChannelBitDepth( 8 ); // 8 bits per channel
   cast->SetOutputFilePath( "displacement_out.png" );
   cast->CastMaterials();
}
 

INormalCaster

The INormalCaster is used to cast normals data from the original geometries. The caster can handle input normal maps, and has two basic settings for output, FlipBackfacingNormals, and GenerateTangentSpaceNormals.

 

Example

The following example shows how to use the INormalCaster to cast diffuse color data

 
void CastNormalData( spMaterial output_material, spMappingImage mapping_image, spMaterialTable source_materials )
{
   // Cast the data using a normal caster.
   spNormalCaster cast = sg->CreateNormalCaster();      
   cast->SetSourceMaterials( source_materials );
   cast->SetMappingImage( mapping_image );      
   // Set output image settings. Use 3 channels
   // (the x, y and z values for the normal).
   cast->SetOutputChannels( 3 );
   cast->SetOutputChannelBitDepth( 8 );      
   cast->SetDilation( 10 );      
   cast->SetOutputFilePath( "normals_out.png" );
   cast->SetFlipBackfacingNormals( false );      
   cast->SetGenerateTangentSpaceNormals( true );  
 
   // Cast the data  
   cast->CastMaterials();
     
   // Set material to point to created texture filename.    
   output_material->SetTexture( SG_MATERIAL_CHANNEL_NORMALS, "normals_out.png" );
}
 

IOpacityCaster

The IOpacityCaster is used to cast opacity values, and can be used when opacity is needed as a separate texture.

Example

The following example shows how to use the IOpacityCaster to generate an opacity map:

 
void GenerateDisplacementMap( spMaterial output_material, spMappingImage mapping_image, spMaterialTable source_materials )
{
    // Cast displacement texture data.
    spOpacityCaster cast = sg->CreateOpacityCaster();       
    cast->SetSourceMaterials( source_materials );
    cast->SetMappingImage( mapping_image );
   
    cast->SetOutputChannels( 1 ); // 1 channel
    cast->SetOutputChannelBitDepth( 8 ); // 8 bits per channel
    cast->SetOutputFilePath( "opacity_out.png" );
    cast->CastMaterials();
       
    // Set material to point to created texture filename.      
    output_material->SetTexture( SG_MATERIAL_CHANNEL_OPACITY, "opacity_out.png" );
}