MapExtractorPlugin/MapExtractorPlugin.cpp

MapExtractorPlugin/MapExtractorPlugin.cpp
//**************************************************************************/
// Copyright (c) 2012 Autodesk, Inc.
// All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
//
//**************************************************************************/
// DESCRIPTION: An example plugin that will set up custom attributes once
// a new Map Extraction node is created.
// CREATED: May 2012
//**************************************************************************/
#include "MapExtractorPlugin.h"
// This line provides some information about the plugin to Mudbox. This can be used to
// specify a function which will be called once all the plugins are loaded.
MB_PLUGIN( "Map Extractor", "Custom settings on Map Extaction", "Autodesk", "http://www.mudbox3d.com", MapExtractorPlugin::Initializer );
IMPLEMENT_CLASS( MapExtractorPlugin, Node, "MapExtractorPlugin" );
abool *s_bEnableMapExtractor;
MapExtractorPlugin *g_cMapExtractorPlugin;
void MapExtractorPlugin::Initializer()
{
bool bEnable = true;
// Create a new boolean preference in the File section o fthe preferences window. This
// preference can be used to turn this plug-in on and off.
s_bEnableMapExtractor = Kernel()->Preferences()->RegisterBoolVariable(NTR("Custom Map Extract Options"), NTR("Files"),
QObject::tr("Customize Map Extraction Options on startup"), QObject::tr("Files"), bEnable);
s_bEnableMapExtractor->SetVisible(true);
g_cMapExtractorPlugin = CreateInstance<MapExtractorPlugin>();
}
// Connect our own constructed scene member event (m_pSceneMembershipEvent) to the source scene member event.
// Once these events are conneced, any time certain nodes are added to the main scene, the OnNodeEvent of this node
// will be called.
MapExtractorPlugin::MapExtractorPlugin() : m_pSceneMembershipEvent(this, "SceneMembershipEvent" )
{
m_pSceneMembershipEvent.Connect( Kernel()->Scene()->SceneMembershipEvent );
}
// This is where the map extraction creation event is caught and its values changed
void MapExtractorPlugin::OnNodeEvent( const Attribute &a, NodeEventType t )
{
if( a == m_pSceneMembershipEvent && t == etEventTriggered &&
m_pSceneMembershipEvent.Value() &&
m_pSceneMembershipEvent->m_pNode.Value() &&
// Check if the node we have is the right type - In this case, it is checking if the node is derived
// from a map extractor class, which is the main class involved in map extraction
m_pSceneMembershipEvent->m_pNode->RuntimeClass()->IsDerivedFrom( mapextractionmodules::MapExtractor::StaticClass() ) &&
m_pSceneMembershipEvent->m_eType == SceneMembershipEventNotifier::eAboutToAdd
)
{
// Catch the event node, set it to a pointer, then downcast it from
// a base 'node' class to the more useful 'MapExtractor' class
mapextractionmodules::MapExtractor *pG = dynamic_cast<mapextractionmodules::MapExtractor *>(m_pSceneMembershipEvent->m_pNode.Value());
// This call uses our MapExtractor class to obtain a pointer to its Extractor class,
// enabling us to use the functions in the MapExtracorAPI
// Each map extractor node has its base values as well as a few Samplers.
// These Samplers are what control the values of each 'tab' in the map
// extractor option box.
// Here, we can select and choose which Sampler we want to have a pointer to
// so that we can enable/alter its values. Comment out or delete any Samplers
// that you do not need or will not use.
mapextraction::NormalSampler nC = eC.NormalSampler();
mapextraction::AmbientOcclusionSampler aoC = eC.AmbientOcclusionSampler();
mapextraction::VectorDisplacementSampler vdC = eC.VectorDisplacementSampler();
mapextraction::DisplacementSampler dC = eC.DisplacementSampler();
mapextraction::PaintLayerSampler plsC = eC.PaintLayerSampler();
mapextraction::SculptLayerSampler slsC = eC.SculptLayerSampler();
// Call any functions to change the initial setup of a map extraction node.
// In this example each Sampler is split into seperate functions so as to distinguish the
// changes easier. To add/remove any function, look in the MapExtractionPlugin
// class in the MapExtractorPlugin.h file.
changeBasicSettings(eC);
changeNormalSettings(nC);
changeAmbientSettings(aoC);
changeDisplacementSettings(dC);
}
}
// Each of the following functions takes a Sampler and changes the initial values for it.
// Each Sampler has various settings that can be changed. Take a look at the MapExtractorAPI.h file
// in the plugins folder for a full list of settings that can be changed for each Sampler.
// Change basic values for the extraction operation
void MapExtractorPlugin::changeBasicSettings( mapextraction::Extractor eC)
{
// Take all geometry in the scene and put them
// into the source/target geometry box of the texture extraction operation
// Get the number of mesh objects in the scene
int meshcount = Kernel()->Scene()->GeometryCount();
// Set how large we want our source/target array to be (sets the
// maximum amount of geometry the box can hold)
eC.SetSourceCount(meshcount);
eC.SetTargetCount(meshcount);
// Create an empty pointer to a Geometry node
Geometry *sG;
// Iterate through every geometry in the scene, adding each one to the source/target box,
// with their respective highest/lowest subdivision level
for(int g = 0; g < meshcount; g++)
{
sG = Kernel()->Scene()->Geometry( g );
eC.SetSource(g, sG->HighestLevel() );
eC.SetTarget(g, sG->LowestLevel() );
}
// Set the 'Smooth Source Models' option to true
// Set the 'Smooth Target Models' option to false
eC.SetTargetSmoothing(false);
// Set the 'Use Creases & Hard Edges' option to false
eC.SetTargetCreases(false);
// Set the antialiasing to 8X
eC.SetAntialiasing(eAntialiasing);
// It is possible to set custom width/hight values for the extraction operation.
// Note, however, that the UI will not update to represent these custom values.
eC.SetMapWidth(512);
eC.SetMapHeight(1024);
// Set 'Method' to Raycasting
eC.SetLocateMethod(eLocateMethod);
// Set 'Choose Samples' to 'Closest to lowres mesh'
eC.SetSampling(eSampling);
// Set 'Search Distance'
// Set 'Test Both Sides' option to true
eC.SetTestBothSides(true);
}
// Change settings in the 'Normal Map' Sampler for the extraction operation
void MapExtractorPlugin::changeNormalSettings( mapextraction::NormalSampler nC)
{
//Enable the normal map Sampler of the map extraction operation
nC.SetEnabled( true );
// Set file name of the normal map that will be extracted
QString fileName = "MyNormalMap";
nC.SetFileName(fileName);
}
// Change settings in the 'Ambient Occlusion Map' Sampler for the extraction operation
void MapExtractorPlugin::changeAmbientSettings( mapextraction::AmbientOcclusionSampler aoC)
{
//Enable the ambient occlusion Sampler of the map extraction operation
aoC.SetEnabled(true);
//Set the quality of the output map
aoC.SetQuality(eQuality);
//Set the resolution of the Shadow Map
aoC.SetShadowMapresolution(eResolution);
// Set file name of the ambient map that will be extracted
QString fileName = "MyAmbientMap";
aoC.SetFileName(fileName);
// Set the 'Shadow Darkness'
aoC.SetShadowDarkness(1.70f);
// Set the 'Shadow Contrast'
aoC.SetShadowContrast(0.50f);
// Set the 'Filter'
aoC.SetFilter(0.01f);
}
// Change settings in the Displacement Sampler of the extraction operation
void MapExtractorPlugin::changeDisplacementSettings( mapextraction::DisplacementSampler dC)
{
//Enable the displacement Sampler of the map extraction operation
dC.SetEnabled(true);
// Set file name of the displacement map that will be extracted
QString fileName = "MyDisplacementMap";
dC.SetFileName(fileName);
}