MaterialNodePointLight::MaterialNodePointLight(MaterialCompiler* Compiler)
: MaterialNode( Compiler )
{
mAmbient .BindSlot( L"Ambient", this, MVT_Float3, "float3(0.0f,0.0f,0.0f)" );
mDiffuse .BindSlot( L"Diffuse", this, MVT_Float3, "float3(0.0f,0.0f,0.0f)" );
mNormal .BindSlot( L"Normal", this, MVT_Float3, "Parameters.VertexNormal" );
mSpecular .BindSlot( L"Specular", this, MVT_Float3, "float3(0.0f,0.0f,0.0f)");
mSpecularPow.BindSlot( L"SpecularPow", this, MVT_Float, "16.0f");
mOpacity .BindSlot( L"Opacity", this, MVT_Float, "1.0f");
}
typedef std::vector<void*> FormatParameterArray;
void MaterialNodePointLight::Compile( )
{
dword CodeGenLength = 0;
FormatParameterArray tFormatParameterArray;
// 编译代码,并构建格式化数组
for ( SlotList::iterator it = mSlotList.begin();
it != mSlotList.end();
++it )
{
MaterialSlot* Slot = *it;
Slot->Compile();
CodeGenLength += StringHelperA::Length( Slot->GetResultCode() );
CodeGenLength += StringHelperA::Length( Slot->GetCaculateCode() );
tFormatParameterArray.push_back( (void*)Slot->GetCaculateCode() );
tFormatParameterArray.push_back( (void*)Slot->GetResultCode() );
}
// 读取模板文件
FileStream ShaderFile;
if ( !ShaderFile.Open(L"shader/MaterialTemplatePixelShader.usf", FAM_Read ) )
{
Assert( false );
}
dword ShaderFileSize = ShaderFile.GetSize();
AString ShaderTemplateCode;
ShaderTemplateCode.resize( ShaderFileSize + 1);
ShaderFile.ReadBuffer( (void*)ShaderTemplateCode.data(), ShaderFileSize * sizeof( char ) );
ShaderTemplateCode[ShaderFileSize] = 0;
mCompiler->WriteFinalCode( ShaderFileSize + CodeGenLength , ShaderTemplateCode.c_str(), &tFormatParameterArray[0] );
}