ShaderGenerator vertexShader ,
ShaderGenerator pixelShader ,
ShaderGenerator surfaceInputs ,
ShaderGraphRequirements graphRequiements ,
ShaderGraphRequirements pixelRequirements ,
ShaderGraphRequirements surfaceRequirements ,
ShaderGraphRequirements vertexDescriptionRequirements ,
ShaderGraphRequirements vertexRequirements ,
CoordinateSpace preferredCoordinateSpace )
{
if ( preferredCoordinateSpace = = CoordinateSpace . Tangent )
// *generate needed interpolators
// *generate output from the vertex shader that writes into these interpolators
// *generate the pixel shader code that declares needed variables in the local scope
var graphModelRequirements = graphRequi ements. Union ( modelRequiements ) ;
var combinedRequirements = vertexDescription Requirements . Union ( graphModelRequirements ) ;
var graphModelRequirements = pixelRequir ements. Union ( modelRequiements ) ;
var combinedRequirements = vertexRequirements . Union ( graphModelRequirements ) ;
int interpolatorIndex = interpolatorStartIndex ;
var name = preferredCoordinateSpace . ToVariableName ( InterpolatorType . Normal ) ;
vertexDescriptionInputs . AppendLine ( "float3 {0} = {1};" , name , ConvertBetweenSpace ( "v.normal" , CoordinateSpace . Object , preferredCoordinateSpace , InputType . Normal ) ) ;
if ( vertexDescription Requirements . requiresNormal > 0 | | vertexDescription Requirements . requiresBitangent > 0 )
if ( vertexRequirements . requiresNormal > 0 | | vertexRequirements . requiresBitangent > 0 )
{
interpolators . AddShaderChunk ( string . Format ( "float3 {0} : TEXCOORD{1};" , name , interpolatorIndex ) , false ) ;
{
interpolators . AddShaderChunk ( string . Format ( "float3 {0} : TEXCOORD{1};" , name , interpolatorIndex ) , false ) ;
pixelShader . AddShaderChunk ( string . Format ( "float3 {0} = normalize(IN.{0});" , name ) , false ) ;
interpolatorIndex + + ;
}
pixelShader . AddShaderChunk ( string . Format ( "float3 {0} = normalize(IN.{0});" , name ) , false ) ;
interpolatorIndex + + ;
}
}
if ( combinedRequirements . requiresTangent > 0 | | combinedRequirements . requiresBitangent > 0 )
if ( vertexDescriptionRequirements . requiresTangent > 0 | | vertexDescriptionRequirements . requiresBitangent > 0 )
if ( vertexRequirements . requiresTangent > 0 | | vertexRequirements . requiresBitangent > 0 )
{
interpolators . AddShaderChunk ( string . Format ( "float3 {0} : TEXCOORD{1};" , name , interpolatorIndex ) , false ) ;
{
interpolators . AddShaderChunk ( string . Format ( "float3 {0} : TEXCOORD{1};" , name , interpolatorIndex ) , false ) ;
pixelShader . AddShaderChunk ( string . Format ( "float3 {0} = IN.{0};" , name ) , false ) ;
interpolatorIndex + + ;
}
pixelShader . AddShaderChunk ( string . Format ( "float3 {0} = IN.{0};" , name ) , false ) ;
interpolatorIndex + + ;
}
}
if ( combinedRequirements . requiresBitangent > 0 )
preferredCoordinateSpace . ToVariableName ( InterpolatorType . Normal ) ,
preferredCoordinateSpace . ToVariableName ( InterpolatorType . Tangent ) ,
"v.tangent.w" ) ;
if ( vertexDescription Requirements . requiresBitangent > 0 )
if ( vertexRequirements . requiresBitangent > 0 )
{
interpolators . AddShaderChunk ( string . Format ( "float3 {0} : TEXCOORD{1};" , name , interpolatorIndex ) , false ) ;
{
interpolators . AddShaderChunk ( string . Format ( "float3 {0} : TEXCOORD{1};" , name , interpolatorIndex ) , false ) ;
pixelShader . AddShaderChunk ( string . Format ( "float3 {0} = IN.{0};" , name ) , false ) ;
interpolatorIndex + + ;
}
pixelShader . AddShaderChunk ( string . Format ( "float3 {0} = IN.{0};" , name ) , false ) ;
interpolatorIndex + + ;
}
}
if ( combinedRequirements . requiresViewDir > 0 )
var preferredSpaceViewDir = ConvertBetweenSpace ( worldSpaceViewDir , CoordinateSpace . World , preferredCoordinateSpace , InputType . Vector ) ;
if ( vertexDescription Requirements . requiresViewDir > 0 )
if ( vertexRequirements . requiresViewDir > 0 )
{
interpolators . AddShaderChunk ( string . Format ( "float3 {0} : TEXCOORD{1};" , name , interpolatorIndex ) , false ) ;
{
interpolators . AddShaderChunk ( string . Format ( "float3 {0} : TEXCOORD{1};" , name , interpolatorIndex ) , false ) ;
pixelShader . AddShaderChunk ( string . Format ( "float3 {0} = normalize(IN.{0});" , name ) , false ) ;
interpolatorIndex + + ;
}
pixelShader . AddShaderChunk ( string . Format ( "float3 {0} = normalize(IN.{0});" , name ) , false ) ;
interpolatorIndex + + ;
}
}
if ( combinedRequirements . requiresPosition > 0 )
if ( vertexDescriptionRequirements . requiresPosition > 0 )
if ( vertexRequirements . requiresPosition > 0 )
{
interpolators . AddShaderChunk ( string . Format ( "float3 {0} : TEXCOORD{1};" , name , interpolatorIndex ) , false ) ;
{
interpolators . AddShaderChunk ( string . Format ( "float3 {0} : TEXCOORD{1};" , name , interpolatorIndex ) , false ) ;
pixelShader . AddShaderChunk ( string . Format ( "float3 {0} = IN.{0};" , name ) , false ) ;
interpolatorIndex + + ;
}
pixelShader . AddShaderChunk ( string . Format ( "float3 {0} = IN.{0};" , name ) , false ) ;
interpolatorIndex + + ;
}
}
if ( combinedRequirements . NeedsTangentSpace ( ) )
var normal = preferredCoordinateSpace . ToVariableName ( InterpolatorType . Normal ) ;
if ( vertexDescription Requirements . NeedsTangentSpace ( ) )
if ( vertexRequirements . NeedsTangentSpace ( ) )
pixelShader . AddShaderChunk ( string . Format ( "float3x3 tangentSpaceTransform = float3x3({0},{1},{2});" ,
pixelShader . AddShaderChunk ( string . Format ( "float3x3 tangentSpaceTransform = float3x3({0},{1},{2});" ,
tangent , bitangent , normal ) , false ) ;
}
GenerateSpaceTranslations ( vertexDescriptionRequirements . requiresNormal , InterpolatorType . Normal , preferredCoordinateSpace ,
GenerateSpaceTranslations ( vertexRequirements . requiresNormal , InterpolatorType . Normal , preferredCoordinateSpace ,
GenerateSpaceTranslations ( vertexDescriptionRequirements . requiresTangent , InterpolatorType . Tangent , preferredCoordinateSpace ,
GenerateSpaceTranslations ( vertexRequirements . requiresTangent , InterpolatorType . Tangent , preferredCoordinateSpace ,
GenerateSpaceTranslations ( vertexDescriptionRequirements . requiresBitangent , InterpolatorType . BiTangent , preferredCoordinateSpace ,
GenerateSpaceTranslations ( vertexRequirements . requiresBitangent , InterpolatorType . BiTangent , preferredCoordinateSpace ,
GenerateSpaceTranslations ( vertexDescriptionRequirements . requiresViewDir , InterpolatorType . ViewDirection , preferredCoordinateSpace ,
GenerateSpaceTranslations ( vertexRequirements . requiresViewDir , InterpolatorType . ViewDirection , preferredCoordinateSpace ,
GenerateSpaceTranslations ( vertexDescriptionRequirements . requiresPosition , InterpolatorType . Position , preferredCoordinateSpace ,
GenerateSpaceTranslations ( vertexRequirements . requiresPosition , InterpolatorType . Position , preferredCoordinateSpace ,
InputType . Position , sg , Dimension . Three ) ;
vertexDescriptionInputs . AppendLines ( sg . GetShaderString ( 0 ) ) ;
}
GenerateSpaceTranslations ( graphModelRequirements . requiresPosition , InterpolatorType . Position , preferredCoordinateSpace ,
InputType . Position , pixelShader , Dimension . Three ) ;
if ( vertexDescription Requirements . requiresVertexColor )
if ( vertexRequirements . requiresVertexColor )
vertexDescriptionInputs . AppendLine ( "vdi.{0} = v.color;" , ShaderGeneratorNames . VertexColor ) ;
if ( graphModelRequirements . requiresVertexColor )
{
if ( combinedRequirements . requiresScreenPosition )
{
var screenPosition = "ComputeScreenPos(mul(GetWorldToHClipMatrix(), mul(GetObjectToWorldMatrix(), v.vertex.xyz)), _ProjectionParams.x)" ;
if ( vertexDescription Requirements . requiresScreenPosition )
if ( vertexRequirements . requiresScreenPosition )
{
vertexDescriptionInputs . AppendLine ( "float4 {0} = {1};" , ShaderGeneratorNames . ScreenPosition , screenPosition ) ;
}
}
}
foreach ( var channel in vertexDescription Requirements . requiresMeshUVs . Distinct ( ) )
foreach ( var channel in vertexRequirements . requiresMeshUVs . Distinct ( ) )
{
vertexDescriptionInputs . AppendLine ( "vdi.{0} = v.texcoord{1};" , channel . GetUVName ( ) , ( int ) channel ) ;
}
// additional requirements have come from the lighting model
// and are not needed in the shader graph
{
var replaceString = "surfaceInput.{0} = {0};" ;
GenerateSpaceTranslationSurfaceInputs ( graphRequi ements. requiresNormal , InterpolatorType . Normal , surfaceInputs , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( graphRequi ements. requiresTangent , InterpolatorType . Tangent , surfaceInputs , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( graphRequi ements. requiresBitangent , InterpolatorType . BiTangent , surfaceInputs , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( graphRequi ements. requiresViewDir , InterpolatorType . ViewDirection , surfaceInputs , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( graphRequi ements. requiresPosition , InterpolatorType . Position , surfaceInputs , replaceString ) ;
var replaceString = "surfaceInput.{0} = {0};" ;
GenerateSpaceTranslationSurfaceInputs ( surfaceRequir ements. requiresNormal , InterpolatorType . Normal , surfaceInputs , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( surfaceRequir ements. requiresTangent , InterpolatorType . Tangent , surfaceInputs , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( surfaceRequir ements. requiresBitangent , InterpolatorType . BiTangent , surfaceInputs , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( surfaceRequir ements. requiresViewDir , InterpolatorType . ViewDirection , surfaceInputs , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( surfaceRequir ements. requiresPosition , InterpolatorType . Position , surfaceInputs , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( vertexDescription Requirements . requiresNormal , InterpolatorType . Normal , sg , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( vertexDescription Requirements . requiresTangent , InterpolatorType . Tangent , sg , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( vertexDescription Requirements . requiresBitangent , InterpolatorType . BiTangent , sg , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( vertexDescription Requirements . requiresViewDir , InterpolatorType . ViewDirection , sg , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( vertexDescription Requirements . requiresPosition , InterpolatorType . Position , sg , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( vertexRequirements . requiresNormal , InterpolatorType . Normal , sg , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( vertexRequirements . requiresTangent , InterpolatorType . Tangent , sg , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( vertexRequirements . requiresBitangent , InterpolatorType . BiTangent , sg , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( vertexRequirements . requiresViewDir , InterpolatorType . ViewDirection , sg , replaceString ) ;
GenerateSpaceTranslationSurfaceInputs ( vertexRequirements . requiresPosition , InterpolatorType . Position , sg , replaceString ) ;
if ( graphRequi ements. requiresVertexColor )
if ( pixelRequir ements. requiresVertexColor )
if ( graphRequi ements. requiresScreenPosition )
if ( pixelRequir ements. requiresScreenPosition )
foreach ( var channel in graphRequi ements. requiresMeshUVs . Distinct ( ) )
foreach ( var channel in pixelRequir ements. requiresMeshUVs . Distinct ( ) )
surfaceInputs . AddShaderChunk ( string . Format ( "surfaceInput.{0} = {0};" , channel . GetUVName ( ) ) , false ) ;
}
vertexShader ,
pixelShader ,
surfaceInputs ,
shaderGraphRequirements ,
shaderGraphRequirements ,
ShaderGraphRequirements . none ,
ShaderGraphRequirements . none ,