2
Copyright (c) 2008-2009 NetAllied Systems GmbH
4
This file is part of COLLADAMaya.
6
Portions of the code are:
7
Copyright (c) 2005-2007 Feeling Software Inc.
8
Copyright (c) 2005-2007 Sony Computer Entertainment America
9
Copyright (c) 2004-2005 Alias Systems Corp.
11
Licensed under the MIT Open Source License,
12
for details please see LICENSE file or the website
13
http://www.opensource.org/licenses/mit-license.php
16
#include "COLLADAMayaStableHeaders.h"
17
#include "COLLADAMayaSceneGraph.h"
18
#include "COLLADAMayaEffectExporter.h"
19
#include "COLLADAMayaLightExporter.h"
20
#include "COLLADAMayaExportOptions.h"
21
#include "COLLADAMayaAnimationExporter.h"
23
#include <maya/MFnLight.h>
24
#include <maya/MFnNonAmbientLight.h>
25
#include <maya/MFnAmbientLight.h>
26
#include <maya/MFnSpotLight.h>
32
//---------------------------------------------------------------
33
LightExporter::LightExporter (
34
COLLADASW::StreamWriter* streamWriter,
35
DocumentExporter* documentExporter )
36
: COLLADASW::LibraryLights ( streamWriter )
37
, mDocumentExporter ( documentExporter )
41
//---------------------------------------------------------------
42
void LightExporter::exportLights ( )
44
if ( !ExportOptions::exportLights() ) return;
46
// Get the list with the transform nodes.
47
SceneGraph* sceneGraph = mDocumentExporter->getSceneGraph();
48
SceneElementsList* exportNodesTree = sceneGraph->getExportNodesTree();
50
// Export all/selected DAG nodes
51
size_t length = exportNodesTree->size();
52
for ( uint i = 0; i < length; ++i )
54
SceneElement* sceneElement = ( *exportNodesTree ) [i];
55
exportLights ( sceneElement );
61
//---------------------------------------------------------------
62
void LightExporter::exportLights ( SceneElement* sceneElement )
64
// If we have a external reference, we don't need to export the data here.
65
if ( !sceneElement->getIsLocal() ) return;
66
if ( !sceneElement->getIsExportNode () ) return;
68
// Check if it is a light.
69
SceneElement::Type sceneElementType = sceneElement->getType();
70
if ( sceneElementType == SceneElement::LIGHT )
72
// Get the current dag path
73
MDagPath dagPath = sceneElement->getPath();
75
// Check if the current scene element isn't already exported.
76
SceneGraph* sceneGraph = mDocumentExporter->getSceneGraph();
77
if ( sceneGraph->findExportedElement ( dagPath ) ) return;
79
// Check if the current element is an instance.
80
// We don't need to export instances, because we export the original instanced element.
81
bool isInstance = ( dagPath.isInstanced() && dagPath.instanceNumber() > 0 );
83
// If the original instanced element isn't already exported, we have to export it now.
86
// Get the original instanced element.
87
MDagPath instancedPath;
88
dagPath.getPath ( instancedPath, 0 );
90
// Check if the original instanced element is already exported.
91
SceneGraph* sceneGraph = mDocumentExporter->getSceneGraph();
92
SceneElement* exportedElement = sceneGraph->findExportedElement ( instancedPath );
93
if ( exportedElement == 0 )
95
// Export the original instanced element and push it in the exported scene graph.
96
if ( exportLight ( instancedPath ) )
98
SceneElement* instancedSceneElement = sceneGraph->findElement ( instancedPath );
99
SceneGraph* sceneGraph = mDocumentExporter->getSceneGraph();
100
sceneGraph->addExportedElement( instancedSceneElement );
106
// Export the element and push it in the exported scene graph.
107
if ( exportLight ( dagPath ) )
109
SceneGraph* sceneGraph = mDocumentExporter->getSceneGraph();
110
sceneGraph->addExportedElement( sceneElement );
116
// Recursive call for all the child elements
117
for ( uint i=0; i<sceneElement->getChildCount(); ++i )
119
SceneElement* childElement = sceneElement->getChild ( i );
120
exportLights ( childElement );
124
// ------------------------------------
125
bool LightExporter::exportLight ( const MDagPath& dagPath )
127
if ( !ExportOptions::exportLights() ) return false;
129
MObject lightNode = dagPath.node();
131
// Retrieve the Maya light object
133
MFnLight lightFn(lightNode, &status); CHECK_STAT(status);
134
if (status != MStatus::kSuccess) return false;
136
// Get the maya light id.
137
String mayaLightId = mDocumentExporter->dagPathToColladaId ( dagPath );
139
// Generate a COLLADA id for the new object
140
String colladaLightId;
142
// Check if there is an extra attribute "colladaId" and use this as export id.
143
MString attributeValue;
144
DagHelper::getPlugValue ( lightNode, COLLADA_ID_ATTRIBUTE_NAME, attributeValue );
145
if ( attributeValue != EMPTY_CSTRING )
147
// Generate a valid collada name, if necessary.
148
colladaLightId = mDocumentExporter->mayaNameToColladaName ( attributeValue, false );
152
// Generate a COLLADA id for the new object
153
colladaLightId = mDocumentExporter->dagPathToColladaId ( dagPath );
155
// Make the id unique and store it in a map.
156
colladaLightId = mLightIdList.addId ( colladaLightId );
157
mMayaIdColladaIdMap [ mayaLightId ] = colladaLightId;
159
// Get a pointer to the stream writer.
160
COLLADASW::StreamWriter* streamWriter = mDocumentExporter->getStreamWriter();
163
String lightName = mDocumentExporter->dagPathToColladaName ( dagPath );
165
// Figure out the type of light and create it
166
COLLADASW::Light* light = NULL;
167
MFn::Type type = lightNode.apiType();
170
case MFn::kAmbientLight:
171
light = new COLLADASW::AmbientLight( streamWriter, colladaLightId, lightName );
173
case MFn::kDirectionalLight:
174
light = new COLLADASW::DirectionalLight( streamWriter, colladaLightId, lightName );
176
case MFn::kSpotLight:
177
light = new COLLADASW::SpotLight( streamWriter, colladaLightId, lightName );
179
case MFn::kPointLight: // Intentional pass-through
181
light = new COLLADASW::PointLight( streamWriter, colladaLightId, lightName );
185
// Export the original maya name.
186
light->addExtraTechniqueParameter ( PROFILE_MAYA, PARAMETER_MAYA_ID, mayaLightId );
188
// Get a pointer to the animation exporter.
189
AnimationExporter* anim = mDocumentExporter->getAnimationExporter();
190
bool animated = false;
192
// Color/Intensity are the common attributes of all lights
193
MColor mayaColor = lightFn.color ( &status ); CHECK_STAT(status);
194
COLLADASW::Color lightColor ( mayaColor.r, mayaColor.g, mayaColor.b, mayaColor.a );
195
animated = anim->addNodeAnimation ( lightNode, ATTR_COLOR, kColour, RGBA_PARAMETERS );
196
light->setColor( lightColor, animated );
198
float intensity = lightFn.intensity ( &status ); CHECK_STAT(status);
199
animated = anim->addNodeAnimation ( lightNode, ATTR_INTENSITY, kSingle );
200
light->setIntensity( intensity, animated );
202
// Add the type specific attributes
203
if (lightNode.hasFn(MFn::kNonAmbientLight))
205
// Needed Point and Spot light types parameters: Attenuation and Attenuation_Scale
206
// Attenuation in COLLADA is equal to Decay in Maya.
207
MFnNonAmbientLight naLightFn(lightNode);
208
int decayRate = naLightFn.decayRate(&status); CHECK_STAT(status);
209
decayRate = std::min ( decayRate, 2 );
210
decayRate = std::max ( decayRate, 0 );
212
light->setConstantAttenuation ( ( decayRate == 0 ) ? 1.0f : 0.0f);
213
light->setLinearAttenuation ( ( decayRate == 1 ) ? 1.0f : 0.0f);
214
light->setQuadraticAttenuation ( ( decayRate == 2 ) ? 1.0f : 0.0f);
216
else if (lightNode.hasFn(MFn::kAmbientLight))
218
MFnAmbientLight ambientLightFn ( lightNode );
219
float ambientShade = ambientLightFn.ambientShade();
220
String paramSid = EMPTY_STRING;
221
animated = anim->addNodeAnimation ( lightNode, ATTR_AMBIENT_SHADE, kSingle );
222
if ( animated ) paramSid = ATTR_AMBIENT_SHADE;
223
light->addExtraTechniqueParameter ( PROFILE_MAYA, MAYA_AMBIENTSHADE_LIGHT_PARAMETER, ambientShade, paramSid );
226
if (lightNode.hasFn(MFn::kSpotLight))
228
// Put in the needed spot light type attributes : Falloff, Falloff_Scale and Angle
229
MFnSpotLight spotFn(lightNode);
231
float fallOffAngle = COLLADABU::Math::Utils::radToDegF ( (float)spotFn.coneAngle( &status ) ); CHECK_STAT(status);
232
animated = anim->addNodeAnimation ( lightNode, ATTR_CONE_ANGLE, ( SampleType ) ( kSingle | kAngle ) );
233
light->setFallOffAngle ( fallOffAngle, animated );
235
light->setFallOffExponent ( 1.0f );
237
float penumbraValue = COLLADABU::Math::Utils::radToDegF ( (float)spotFn.penumbraAngle( &status ) ); CHECK_STAT(status);
238
animated = anim->addNodeAnimation ( lightNode, ATTR_PENUMBRA_ANGLE, ( SampleType ) ( kSingle | kAngle ) );
240
// FCDLightTools::LoadPenumbra(light, penumbraValue, colladaLight->GetOuterAngle().GetAnimated());
243
// animated = anim->addNodeAnimation ( lightNode, ATTR_DROP_OFF, kSingle );
244
// light->setDropOff ( (float) spotFn.dropOff ( &status ), animated ); CHECK_MSTATUS(status);
253
// ------------------------------------
254
const String LightExporter::findColladaLightId ( const String& mayaLightId )
256
const StringToStringMap::const_iterator it = mMayaIdColladaIdMap.find ( mayaLightId );
257
if ( it != mMayaIdColladaIdMap.end () )
b'\\ No newline at end of file'