Esempio n. 1
0
void OgreExporter_c::ExportLevelMaterials(const WadLevel_c &level, const WadFile_c &file)
{
	this->ExportWalls(level, file);

	fs::path materialPath(strMaterialScriptsDirectory);
	materialPath /= fs::path(file.GetFileName()).filename();

	std::string finalFileName = materialPath.string();
	finalFileName += ".material";

	std::ofstream materialFile(finalFileName.c_str());

	BOOST_FOREACH(Name_u name, setExportedTextures)
	{
		materialFile << "material " << name.ToString() << "\n";
		materialFile << "{\n";
			materialFile << "\ttechnique\n";
			materialFile << "\t{\n";
				materialFile << "\t\tpass\n";
				materialFile << "\t\t{\n";
					materialFile << "\t\t\ttexture_unit\n";
					materialFile << "\t\t\t{\n";
						materialFile << "\t\t\t\ttexture " << name.ToString() << ".bmp\n";
					materialFile << "\t\t\t}\n";
				materialFile << "\t\t}\n";
			materialFile << "\t}\n";							
		materialFile << "}\n";
	}
void MaterialCompiler::GenerateCode(MaterialGraph * materialGraph)
{
    // Generate pixel shader code
    String pixelShader;
    String vertexShader;
    
    for (uint32 nodeIndex = 0; nodeIndex < materialGraph->GetNodeCount(); ++nodeIndex)
    {
        MaterialGraphNode * node = materialGraph->GetNode(nodeIndex);
        GenerateCodeForNode(node, vertexShader, pixelShader);
    }
    
    //MaterialGraphNode * rootResultNode = materialGraph->GetNodeByName("material");

    FilePath materialPath(materialGraph->GetMaterialPathname().GetDirectory());
    FilePath vertexShaderPath(materialPath + materialGraph->GetVertexShaderFilename());
    FilePath fragmentShaderPath(materialPath + materialGraph->GetPixelShaderFilename());

    String originalVertexShader = FileSystem::Instance()->ReadFileContents(vertexShaderPath);
    String originalFragmentShader = FileSystem::Instance()->ReadFileContents(fragmentShaderPath);
    
    
    File * resultVsh = File::Create(materialCompiledVshName, File::CREATE | File::WRITE);
    resultVsh->WriteString(finalVertexShaderCode, false); // Save without null terminator
    resultVsh->WriteString(originalVertexShader);
    SafeRelease(resultVsh);
    
    File * resultFsh = File::Create(materialCompiledFshName, File::CREATE | File::WRITE);
    resultFsh->WriteString(finalPixelShaderCode, false); // Save without null terminator
    resultFsh->WriteString(originalFragmentShader);
    //resultFsh->Write(fragmentShaderData->GetPtr(), fragmentShaderData->GetSize());
    SafeRelease(resultFsh);
};
//-----------------------------------------------------------------------------
// Figures out the material path name from the maya shading group
//-----------------------------------------------------------------------------
MString ValveMaya::GetMaterialPath(
	const MObject &shadingGroupObj,
	MObject *pFileObj,
	MObject *pPlace2dTextureObj,
	MObject *pVmtObj,
	bool *pbTransparent,
	MString *pDebugWhy )
{
	MString materialPath( "debug/debugEmpty" );

	const MObject surfaceShaderObj( FindInputNode( shadingGroupObj, "surfaceShader" ) );
	if ( surfaceShaderObj.isNull() )
	{
		if ( pDebugWhy )
		{
			*pDebugWhy = MString( "Can't find surfaceShader node from shadingGroup: No input to " ) +
				MFnDependencyNode( shadingGroupObj ).name() + ".surfaceShader";
		}

		return materialPath;
	}

	if ( MFnDependencyNode( surfaceShaderObj ).typeName() == "vsVmt" )
	{
		MPlug vmtP = MFnDependencyNode( surfaceShaderObj ).findPlug( "vmtPath" );
		if ( !vmtP.isNull() )
		{
			vmtP.getValue( materialPath );
			return materialPath;
		}
	}


	const MObject fileObj( FindInputNodeOfType( surfaceShaderObj, "file", "color" ) );
	if ( fileObj.isNull() )
	{
		if ( pDebugWhy )
		{
			*pDebugWhy = MString( "Can't find file texture node from surfaceShader: No input to " ) +
				MFnDependencyNode( surfaceShaderObj ).name() + ".color";
		}

		return materialPath;
	}

	if ( pFileObj )
	{
		*pFileObj = fileObj;
	}

	if ( pbTransparent )
	{
		const MObject transObj( FindInputNodeOfType( surfaceShaderObj, "file", "transparency" ) );
		if ( fileObj == transObj )
		{
			*pbTransparent = true;
		}
	}

	if ( pPlace2dTextureObj )
	{
		MObject place2dTextureObj( FindInputNodeOfType( fileObj, "place2dTexture", "uvCoord" ) );
		if ( !place2dTextureObj.isNull() )
		{
			const MPlug uvCoordP( MFnDependencyNode( place2dTextureObj ).findPlug( "uvCoord" ) );
			if ( !( uvCoordP.isNull() || uvCoordP.isConnected() || uvCoordP.isLocked() ) )
			{
				*pPlace2dTextureObj = place2dTextureObj;
			}
		}
	}

	// Check to see if a vsVmtToTex node is driving the filename
	const MObject vsVmtToTexObj = FindInputNodeOfType( fileObj, "vsVmtToTex" );
	if ( !vsVmtToTexObj.isNull() )
	{
		if ( pVmtObj )
		{
			*pVmtObj = vsVmtToTexObj;
		}

		const MPlug materialPathP = MFnDependencyNode( vsVmtToTexObj ).findPlug( "materialPath" );
		if ( !materialPathP.isNull() )
		{
			materialPathP.getValue( materialPath );
			if ( materialPath.length() )
			{
				return materialPath;
			}
		}
	}

	// Otherwise do path munging to figure out 
	MString fileTexture;
	int fLen( 0 );

	const MFnDependencyNode fileFn( fileObj );
	const MPlug fileP( fileFn.findPlug( "fileTextureName" ) );
	fileP.getValue( fileTexture );
	fLen = fileTexture.length();
	if ( fLen == 0 )
	{
		if ( pDebugWhy )
		{
			*pDebugWhy = MString( "No texture filename specified on file texture node: " ) +
				MFnDependencyNode( fileObj ).name() + ".fileTextureName is empty";
		}

		return materialPath;
	}

	MStringArray path;
	const uint pLen( SplitPath( StripExtension( fileTexture ), path ) );

	if ( pLen == 0 )
		return fileTexture.asChar();

	materialPath = path[ pLen - 1 ];
	// Make the path into a relative path
	for ( int i = pLen - 2; i >= 0; --i )
	{
		if ( i > 1 && ( !stricmp( path[ i - 1 ].asChar(), "game" ) || !stricmp( path[ i - 1 ].asChar(), "content" ) ) )
			break;

		if ( !stricmp( path[ i ].asChar(), "materials" ) || !stricmp( path[ i ].asChar(), "materialsrc" ) )
			break;

		materialPath = path[ i ] + "/" + materialPath;
	}

	return materialPath;
}