Material* Material::MaterialFactory(MObject &set) { MObject shaderNode = findShader(set); if (shaderNode == MObject::kNullObj) return NULL; MFnDependencyNode depNode(shaderNode); if (depNode.typeName() == "blinn") { return new Uber(depNode); } else if (depNode.typeName() == "lambert") { return new Plastic(depNode); } else if (depNode.typeName() == "anisotropic") { // need to find a better material } else if (depNode.typeName() == "phong") { return new PhongMat(depNode); } else { return new Uber(depNode); } return NULL; }
bool DX11ViewportRenderer::drawSurface( const MDagPath &dagPath, bool active, bool templated) { bool drewSurface = false; if ( !dagPath.hasFn( MFn::kMesh )) { MMatrix matrix = dagPath.inclusiveMatrix(); MFnDagNode dagNode(dagPath); MBoundingBox box = dagNode.boundingBox(); float color[3] = {0.6f, 0.3f, 0.0f}; if (active) { color[0] = 1.0f; color[1] = 1.0f; color[2] = 1.0f; } else if (templated) { color[0] = 1.0f; color[1] = 0.686f; color[2] = 0.686f; } drawBounds( matrix, box, color); return true; } if ( dagPath.hasFn( MFn::kMesh )) { MMatrix matrix = dagPath.inclusiveMatrix(); MFnDagNode dagNode(dagPath); // Look for any hardware shaders which can draw D3D first. // bool drewWithHwShader = false; { MFnMesh fnMesh(dagPath); MObjectArray sets; MObjectArray comps; unsigned int instanceNum = dagPath.instanceNumber(); if (!fnMesh.getConnectedSetsAndMembers(instanceNum, sets, comps, true)) MGlobal::displayError("ERROR : MFnMesh::getConnectedSetsAndMembers"); for ( unsigned i=0; i<sets.length(); i++ ) { MObject set = sets[i]; MObject comp = comps[i]; MStatus status; MFnSet fnSet( set, &status ); if (status == MS::kFailure) { MGlobal::displayError("ERROR: MFnSet::MFnSet"); continue; } MObject shaderNode = findShader(set); if (shaderNode != MObject::kNullObj) { MPxHardwareShader * hwShader = MPxHardwareShader::getHardwareShaderPtr( shaderNode ); if (hwShader) { const MRenderProfile & profile = hwShader->profile(); if (profile.hasRenderer( MRenderProfile::kMayaD3D)) { // Render a Maya D3D hw shader here.... //printf("Found a D3D hw shader\n"); //drewWithHwShader = true; } } } } } // Get the geometry buffers for this bad boy and render them D3DGeometry* Geometry = m_resourceManager.getGeometry( dagPath, m_pD3DDevice); if( Geometry) { // Transform from object to world space // XMMATRIX objectToWorld = XMMATRIX ( (float)matrix.matrix[0][0], (float)matrix.matrix[0][1], (float)matrix.matrix[0][2], (float)matrix.matrix[0][3], (float)matrix.matrix[1][0], (float)matrix.matrix[1][1], (float)matrix.matrix[1][2], (float)matrix.matrix[1][3], (float)matrix.matrix[2][0], (float)matrix.matrix[2][1], (float)matrix.matrix[2][2], (float)matrix.matrix[2][3], (float)matrix.matrix[3][0], (float)matrix.matrix[3][1], (float)matrix.matrix[3][2], (float)matrix.matrix[3][3] ); FixedFunctionConstants cb; if (!drewWithHwShader) { // Get material properties for shader associated with mesh // // 1. Try to draw with the sample internal programmable shader bool drewGeometryWithShader = false; // 2. Draw with fixed function shader if (!drewGeometryWithShader) { // Set up a default material, just in case there is none. // float diffuse[3]; if (active) { if (templated) { m_pD3DDeviceCtx->RSSetState( m_pWireframeRS ); diffuse[0] = 1.0f; diffuse[1] = 0.686f; diffuse[2] = 0.686f; } else { m_pD3DDeviceCtx->RSSetState( m_pNormalRS ); diffuse[0] = 0.6f; diffuse[1] = 0.6f; diffuse[2] = 0.6f; } } else { if (templated) { m_pD3DDeviceCtx->RSSetState( m_pWireframeRS ); diffuse[0] = 1.0f; diffuse[1] = 0.686f; diffuse[2] = 0.686f; } else { m_pD3DDeviceCtx->RSSetState( m_pNormalRS ); diffuse[0] = 0.5f; diffuse[1] = 0.5f; diffuse[2] = 0.5f; } } // Set constant buffer XMVECTOR det; cb.wvIT = XMMatrixInverse( &det, objectToWorld * m_currentViewMatrix ); cb.wvp = XMMatrixTranspose( objectToWorld * m_currentViewMatrix * m_currentProjectionMatrix ); cb.wv = XMMatrixTranspose( objectToWorld * m_currentViewMatrix ); cb.lightDir = XMFLOAT4( 0.0f, 0.0f, 1.0f, 0.0f ); cb.lightColor = XMFLOAT4( 1.0f, 1.0f, 1.0f, 0.0f ); cb.ambientLight = XMFLOAT4( 0.2f, 0.2f, 0.2f, 0.0f ); cb.diffuseMaterial = XMFLOAT4( diffuse[0], diffuse[1], diffuse[2], 0.0f ); cb.specularColor = XMFLOAT4( 0.2f, 0.2f, 0.2f, 0.0f ); cb.diffuseCoeff = 1.0f; cb.shininess = 16.0f; cb.transparency = 1.0f; m_pD3DDeviceCtx->UpdateSubresource( m_pFixedFunctionConstantBuffer, 0, NULL, &cb, 0, 0 ); // get shader SurfaceEffectItemList::const_iterator it = m_resourceManager.getSurfaceEffectItemList().find( "Maya_fixedFunction" ); if ( it == m_resourceManager.getSurfaceEffectItemList().end() ) return false; const SurfaceEffectItem* sei = it->second; // bind shaders m_pD3DDeviceCtx->VSSetShader( sei->fVertexShader, NULL, 0 ); m_pD3DDeviceCtx->VSSetConstantBuffers( 0, 1, &m_pFixedFunctionConstantBuffer ); m_pD3DDeviceCtx->IASetInputLayout( sei->fInputLayout ); m_pD3DDeviceCtx->PSSetShader( sei->fPixelShader, NULL, 0 ); m_pD3DDeviceCtx->PSSetConstantBuffers( 0, 1, &m_pFixedFunctionConstantBuffer ); Geometry->Render( m_pD3DDeviceCtx ); drewSurface = true; } } // Draw wireframe on top // if ( drewSurface && active ) { bool drawActiveWithBounds = false; if (drawActiveWithBounds) { MBoundingBox box = dagNode.boundingBox(); float color[3] = {1.0f, 1.0f, 1.0f}; drawBounds( matrix, box, color ); } else { cb.lightColor = XMFLOAT4( 0.0f, 0.0f, 0.0f, 0.0f ); cb.ambientLight = XMFLOAT4( 1.0f, 1.0f, 1.0f, 0.0f ); cb.diffuseMaterial = XMFLOAT4( 1.0f, 1.0f, 1.0f, 0.0f ); cb.specularColor = XMFLOAT4( 0.0f, 0.0f, 0.0f, 0.0f ); m_pD3DDeviceCtx->UpdateSubresource( m_pFixedFunctionConstantBuffer, 0, NULL, &cb, 0, 0 ); m_pD3DDeviceCtx->RSSetState( m_pWireframeRS ); Geometry->Render( m_pD3DDeviceCtx ); } } } // If Geometry } return drewSurface; }
MStatus findTexturesPerPolygon::doIt( const MArgList& ) // // Description: // Find the texture files that apply to the color of each polygon of // a selected shape if the shape has its polygons organized into sets. // { // Get the selection and choose the first path on the selection list. // MStatus status; MDagPath path; MObject cmp; MSelectionList slist; MGlobal::getActiveSelectionList(slist); slist.getDagPath(0, path, cmp); // Have to make the path include the shape below it so that // we can determine if the underlying shape node is instanced. // By default, dag paths only include transform nodes. // path.extendToShape(); // If the shape is instanced then we need to determine which // instance this path refers to. // int instanceNum = 0; if (path.isInstanced()) instanceNum = path.instanceNumber(); // Get a list of all sets pertaining to the selected shape and the // members of those sets. // MFnMesh fnMesh(path); MObjectArray sets; MObjectArray comps; if (!fnMesh.getConnectedSetsAndMembers(instanceNum, sets, comps, true)) cerr << "ERROR: MFnMesh::getConnectedSetsAndMembers\n"; // Loop through all the sets. If the set is a polygonal set, find the // shader attached to the and print out the texture file name for the // set along with the polygons in the set. // for ( unsigned i=0; i<sets.length(); i++ ) { MObject set = sets[i]; MObject comp = comps[i]; MFnSet fnSet( set, &status ); if (status == MS::kFailure) { cerr << "ERROR: MFnSet::MFnSet\n"; continue; } // Make sure the set is a polygonal set. If not, continue. MItMeshPolygon piter(path, comp, &status); if ((status == MS::kFailure) || comp.isNull()) continue; // Find the texture that is applied to this set. First, get the // shading node connected to the set. Then, if there is an input // attribute called "color", search upstream from it for a texture // file node. // MObject shaderNode = findShader(set); if (shaderNode == MObject::kNullObj) continue; MPlug colorPlug = MFnDependencyNode(shaderNode).findPlug("color", &status); if (status == MS::kFailure) continue; MItDependencyGraph dgIt(colorPlug, MFn::kFileTexture, MItDependencyGraph::kUpstream, MItDependencyGraph::kBreadthFirst, MItDependencyGraph::kNodeLevel, &status); if (status == MS::kFailure) continue; dgIt.disablePruningOnFilter(); // If no texture file node was found, just continue. // if (dgIt.isDone()) continue; // Print out the texture node name and texture file that it references. // MObject textureNode = dgIt.thisNode(); MPlug filenamePlug = MFnDependencyNode(textureNode).findPlug("fileTextureName"); MString textureName; filenamePlug.getValue(textureName); cerr << "Set: " << fnSet.name() << endl; cerr << "Texture Node Name: " << MFnDependencyNode(textureNode).name() << endl; cerr << "Texture File Name: " << textureName.asChar() << endl; // Print out the set of polygons that are contained in the current set. // for ( ; !piter.isDone(); piter.next() ) cerr << " poly component: " << piter.index() << endl; } return MS::kSuccess; }
MStatus parseShader(MObject &src, SXRShaderData& d) { MStatus status; MFnSet fnSet( src, &status ); if (status == MStatus::kFailure) { status.perror("Unable to lookup shader from set of shaders for object"); return status; } MObject shaderNode = findShader(src,d); if (shaderNode == MObject::kNullObj) return (MStatus::kFailure); MPlug colorPlug = MFnDependencyNode(shaderNode).findPlug("color", &status); if (status == MStatus::kFailure) return (status); MItDependencyGraph dgIt(colorPlug, MFn::kFileTexture, MItDependencyGraph::kUpstream, MItDependencyGraph::kBreadthFirst, MItDependencyGraph::kNodeLevel, &status); if (status == MStatus::kFailure) return (status); dgIt.disablePruningOnFilter(); // If no texture file node was found, just continue. // if (dgIt.isDone()) { // cout << "no textures found for " << colorPlug.name() << "\n"; return (MStatus::kSuccess); } // Print out the texture node name and texture file that it references. // MObject textureNode = dgIt.thisNode(); MPlug filenamePlug = MFnDependencyNode(textureNode).findPlug("fileTextureName"); MString textureName; filenamePlug.getValue(textureName); MStringArray rgFolders; if (strchr (textureName.asChar(), '\\')) { textureName.split ('\\', rgFolders); } else { textureName.split ('/', rgFolders); } d.tex_name = rgFolders[rgFolders.length() -1]; // cout << "Found texture file: '" << filename.asChar() << "'\n"; short index; //double side flag MPlug xrDoubleSidePlug= MFnDependencyNode(shaderNode).findPlug("xrayDoubleSide", &status); if (status == MS::kSuccess) { MFnEnumAttribute enm = xrDoubleSidePlug.attribute(); if ((status == MS::kSuccess)&&(MS::kSuccess==xrDoubleSidePlug.getValue(index))) d.double_side = index; } //engine MPlug xrEnginePlug= MFnDependencyNode(shaderNode).findPlug("xrayEngineShader", &status); if (status == MS::kSuccess) { MFnEnumAttribute enm = xrEnginePlug.attribute(); if ((status == MS::kSuccess)&&(MS::kSuccess==xrEnginePlug.getValue(index))) d.eng_name = enm.fieldName(index); } //compiler MPlug xrCompilerPlug = MFnDependencyNode(shaderNode).findPlug("xrayCompilerShader", &status); if (status == MS::kSuccess) { MFnEnumAttribute enm = xrCompilerPlug.attribute(); if ((status == MS::kSuccess)&&(MS::kSuccess==xrCompilerPlug.getValue(index))) d.comp_name = enm.fieldName(index); } //game material MPlug xrGameMaterialPlug = MFnDependencyNode(shaderNode).findPlug("xrayGameMaterial", &status); if (status == MS::kSuccess) { MFnEnumAttribute enm = xrGameMaterialPlug.attribute(); if ((status == MS::kSuccess)&&(MS::kSuccess==xrGameMaterialPlug.getValue(index))) d.gmat_name = enm.fieldName(index); } return (MStatus::kSuccess); }