00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "mayaShader.h"
00020 #include "maya_funcs.h"
00021 #include "config_maya.h"
00022 #include "string_utils.h"
00023 #include "pnmImageHeader.h"
00024 #include "pset.h"
00025
00026 #include "pre_maya_include.h"
00027 #include <maya/MFnDependencyNode.h>
00028 #include <maya/MFnLambertShader.h>
00029 #include <maya/MPlug.h>
00030 #include <maya/MPlugArray.h>
00031 #include <maya/MColor.h>
00032 #include <maya/MObject.h>
00033 #include <maya/MStatus.h>
00034 #include "post_maya_include.h"
00035
00036
00037
00038
00039
00040
00041
00042 MayaShader::
00043 MayaShader(MObject engine) {
00044 MFnDependencyNode engine_fn(engine);
00045
00046 set_name(engine_fn.name().asChar());
00047
00048 if (maya_cat.is_debug()) {
00049 maya_cat.debug()
00050 << "Reading shading engine " << get_name() << "\n";
00051 }
00052
00053 bool found_shader = false;
00054 MPlug shader_plug = engine_fn.findPlug("surfaceShader");
00055 if (!shader_plug.isNull()) {
00056 MPlugArray shader_pa;
00057 shader_plug.connectedTo(shader_pa, true, false);
00058
00059 for (size_t i = 0; i < shader_pa.length() && !found_shader; i++) {
00060 MObject shader = shader_pa[0].node();
00061 found_shader = read_surface_shader(shader);
00062 }
00063 }
00064 }
00065
00066
00067
00068
00069
00070
00071 MayaShader::
00072 ~MayaShader() {
00073 }
00074
00075
00076
00077
00078
00079
00080 void MayaShader::
00081 output(ostream &out) const {
00082 out << "Shader " << get_name();
00083 }
00084
00085
00086
00087
00088
00089
00090 void MayaShader::
00091 write(ostream &out) const {
00092 out << "Shader " << get_name() << "\n"
00093 << " color:\n";
00094 _color.write(out);
00095 out << " transparency:\n";
00096 _transparency.write(out);
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 Colorf MayaShader::
00113 get_rgba() const {
00114 Colorf rgba(1.0f, 1.0f, 1.0f, 1.0f);
00115
00116 if (_color._has_flat_color) {
00117 rgba[0] = (float)_color._flat_color[0];
00118 rgba[1] = (float)_color._flat_color[1];
00119 rgba[2] = (float)_color._flat_color[2];
00120 }
00121
00122 if (_transparency._has_flat_color) {
00123
00124
00125
00126 double trans =
00127 _transparency._flat_color[0] * lumin_red +
00128 _transparency._flat_color[1] * lumin_grn +
00129 _transparency._flat_color[2] * lumin_blu;
00130 rgba[3] = 1.0f - (float)trans;
00131 }
00132
00133 return rgba;
00134 }
00135
00136
00137
00138
00139
00140
00141
00142 bool MayaShader::
00143 read_surface_shader(MObject shader) {
00144 MStatus status;
00145 MFnDependencyNode shader_fn(shader);
00146
00147 if (maya_cat.is_spam()) {
00148 maya_cat.spam()
00149 << " Reading surface shader " << shader_fn.name() << "\n";
00150 }
00151
00152
00153
00154
00155
00156 MPlug color_plug = shader_fn.findPlug("color");
00157 if (color_plug.isNull()) {
00158
00159
00160
00161 color_plug = shader_fn.findPlug("outColor");
00162 }
00163
00164 if (!color_plug.isNull()) {
00165 MPlugArray color_pa;
00166 color_plug.connectedTo(color_pa, true, false);
00167
00168 for (size_t i = 0; i < color_pa.length(); i++) {
00169 _color.read_surface_color(color_pa[0].node());
00170 }
00171 }
00172
00173
00174 MPlug trans_plug = shader_fn.findPlug("transparency");
00175 if (trans_plug.isNull()) {
00176 trans_plug = shader_fn.findPlug("outTransparency");
00177 }
00178
00179 if (!trans_plug.isNull()) {
00180 MPlugArray trans_pa;
00181 trans_plug.connectedTo(trans_pa, true, false);
00182
00183 for (size_t i = 0; i < trans_pa.length(); i++) {
00184 _transparency.read_surface_color(trans_pa[0].node());
00185 }
00186 }
00187
00188
00189
00190 if (shader.hasFn(MFn::kLambert)) {
00191 MFnLambertShader lambert_fn(shader);
00192 MColor color = lambert_fn.color(&status);
00193 if (status) {
00194
00195
00196 _color._has_flat_color = true;
00197 _color._flat_color.set(color.r, color.g, color.b, color.a);
00198 _transparency._flat_color.set(0.0, 0.0, 0.0, 0.0);
00199
00200
00201 color = lambert_fn.transparency(&status);
00202 if (status) {
00203 _transparency._has_flat_color = true;
00204 _transparency._flat_color.set(color.r, color.g, color.b, color.a);
00205 }
00206 }
00207 }
00208
00209 if (!_color._has_flat_color && !_color._has_texture) {
00210 if (maya_cat.is_spam()) {
00211 maya_cat.spam()
00212 << " Color definition not found.\n";
00213 }
00214 }
00215 return true;
00216 }