Search Unity

export ShaderFX shader to Unity?

Discussion in 'Shaders' started by FeastSC2, Oct 20, 2018.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Is there a way to export a ShaderFX shader created in Maya to Unity?

    I know that this is possible in other game engines but since Unity uses ShaderLab does it work with Unity?
     
  2. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Export ShaderFX as HLSL and paste code here.
    I will help with translation to Shaderlab.
     
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Waw that'd be really cool,

    Ultimately I would like to have the shader in that Amplify Shader form so that I can make adjustments to it for my game, so if you can build it in Amplify Shader it's even better for me, but a hlsl form would be a big help already.

    Here's the hlsl code, it's the dota 2 shader:


    Code (CSharp):
    1. // ----------------------------------------- Header ------------------------------------------
    2. #ifndef SFX_HLSL_5
    3.     #define SFX_HLSL_5
    4. #endif
    5. #ifndef _MAYA_
    6.     #define _MAYA_
    7. #endif
    8.  
    9.  
    10. #ifndef half
    11.     #define half float
    12.     #define half2 float2
    13.     #define half3 float3
    14.     #define half4 float4
    15. #endif
    16.  
    17.  
    18.  
    19. // ----------------------------------- Per Frame --------------------------------------
    20. cbuffer UpdatePerFrame : register(b0)
    21. {
    22.     float4x4 viewI : ViewInverse < string UIWidget = "None"; >;
    23.  
    24.     bool MayaHwFogEnabled : HardwareFogEnabled < string UIWidget = "None"; > = false;
    25.     int MayaHwFogMode : HardwareFogMode < string UIWidget = "None"; > = 0;
    26.     float MayaHwFogStart : HardwareFogStart < string UIWidget = "None"; > = 0.0;
    27.     float MayaHwFogEnd : HardwareFogEnd < string UIWidget = "None"; > = 100.0;
    28.     float MayaHwFogDensity : HardwareFogDensity < string UIWidget = "None"; > = 0.1;
    29.     float4 MayaHwFogColor : HardwareFogColor < string UIWidget = "None"; > = { 0.5, 0.5, 0.5, 1.0 };
    30.  
    31.  
    32.     float4x4 viewPrj : ViewProjection < string UIWidget = "None"; >;
    33.  
    34.     float4x4 view : View < string UIWidget = "None"; >;
    35.  
    36. };
    37.  
    38. // --------------------------------------- Per Object -----------------------------------------
    39. cbuffer UpdatePerObject : register(b1)
    40. {
    41. int ClampDynamicLights
    42.     <
    43.         float UIMin = 0;
    44.         float UISoftMin = 0;
    45.         float UIMax = 99;
    46.         float UISoftMax = 99;
    47.         float UIStep = 1;
    48.         string UIName = "ClampDynamicLights";
    49.         string UIWidget = "Slider";
    50. > = 99;
    51.  
    52.     float4x4 world : World < string UIWidget = "None"; >;
    53.  
    54.     float4x4 wvp : WorldViewProjection < string UIWidget = "None"; >;
    55.  
    56. };
    57.  
    58. // --------------------------------------- Attributes -----------------------------------------
    59. cbuffer UpdateAttributes : register(b2)
    60. {
    61. float3 specularColor
    62.     <
    63.         string UIName = "specularColor";
    64.         string UIWidget = "ColorPicker";
    65. > = {0.854306,0.954687,1.000000};
    66.  
    67. float specularExponent
    68.     <
    69.         float UIMin = 0.000000;
    70.         float UISoftMin = 0.000000;
    71.         float UIMax = 99.000000;
    72.         float UISoftMax = 99.000000;
    73.         float UIStep = 0.010000;
    74.         string UIName = "specularExponent";
    75.         string UIWidget = "Slider";
    76. > = 23.000000;
    77.  
    78. float specularScale
    79.     <
    80.         float UIMin = 0.000000;
    81.         float UISoftMin = 0.000000;
    82.         float UIMax = 99.000000;
    83.         float UISoftMax = 99.000000;
    84.         float UIStep = 0.010000;
    85.         string UIName = "specularScale";
    86.         string UIWidget = "Slider";
    87. > = 42.000000;
    88.  
    89. float3 rimLightColor
    90.     <
    91.         string UIName = "rimLightColor";
    92.         string UIWidget = "ColorPicker";
    93. > = {0.735357,0.906332,0.954687};
    94.  
    95. float rimLightScale
    96.     <
    97.         float UIMin = 0.000000;
    98.         float UISoftMin = 0.000000;
    99.         float UIMax = 99.000000;
    100.         float UISoftMax = 99.000000;
    101.         float UIStep = 0.010000;
    102.         string UIName = "rimLightScale";
    103.         string UIWidget = "Slider";
    104. > = 10.000000;
    105.  
    106. float cubeMapScalar
    107.     <
    108.         float UIMin = 0.000000;
    109.         float UISoftMin = 0.000000;
    110.         float UIMax = 99.000000;
    111.         float UISoftMax = 99.000000;
    112.         float UIStep = 0.010000;
    113.         string UIName = "cubeMapScalar";
    114.         string UIWidget = "Slider";
    115. > = 0.000000;
    116.  
    117. };
    118.  
    119. // ---------------------------------------- Textures -----------------------------------------
    120. Texture2D fresnelWarpColor
    121. <
    122.     string ResourceName = "__death_prophet_base_fresnelWarpColor.tga";
    123.     string UIName = "fresnelWarpColor";
    124.     string ResourceType = "2D";
    125.     string UIWidget = "FilePicker";
    126. >;
    127.  
    128. Texture2D fresnelWarpRim
    129. <
    130.     string ResourceName = "__death_prophet_base_fresnelWarpRim.tga";
    131.     string UIName = "fresnelWarpRim";
    132.     string ResourceType = "2D";
    133.     string UIWidget = "FilePicker";
    134. >;
    135.  
    136. Texture2D fresnelWarpSpec
    137. <
    138.     string ResourceName = "__death_prophet_base_fresnelWarpSpec.tga";
    139.     string UIName = "fresnelWarpSpec";
    140.     string ResourceType = "2D";
    141.     string UIWidget = "FilePicker";
    142. >;
    143.  
    144. Texture2D cubeMap
    145. <
    146.     string ResourceName = "__death_prophet_base_cubeMap.tga";
    147.     string UIName = "cubeMap";
    148.     string ResourceType = "2D";
    149.     string UIWidget = "FilePicker";
    150. >;
    151.  
    152. SamplerState MMMLCCCSampler
    153. {
    154.     Filter = MIN_MAG_MIP_LINEAR;
    155.     AddressU = CLAMP;
    156.     AddressV = CLAMP;
    157.     AddressW = CLAMP;
    158. };
    159.  
    160. Texture2D color
    161. <
    162.     string ResourceName = "__death_prophet_base_color.tga";
    163.     string UIName = "color";
    164.     string ResourceType = "2D";
    165.     string UIWidget = "FilePicker";
    166. >;
    167.  
    168. SamplerState MMMLWWWSampler
    169. {
    170.     Filter = MIN_MAG_MIP_LINEAR;
    171.     AddressU = WRAP;
    172.     AddressV = WRAP;
    173.     AddressW = WRAP;
    174. };
    175.  
    176. Texture2D normal
    177. <
    178.     string ResourceName = "__death_prophet_base_normal.tga";
    179.     string UIName = "normal";
    180.     string ResourceType = "2D";
    181.     string UIWidget = "FilePicker";
    182. >;
    183.  
    184. Texture2D specularMask
    185. <
    186.     string ResourceName = "__death_prophet_base_specularMask.tga";
    187.     string UIName = "specularMask";
    188.     string ResourceType = "2D";
    189.     string UIWidget = "FilePicker";
    190. >;
    191.  
    192. Texture2D rimMask
    193. <
    194.     string ResourceName = "__death_prophet_base_rimMask.tga";
    195.     string UIName = "rimMask";
    196.     string ResourceType = "2D";
    197.     string UIWidget = "FilePicker";
    198. >;
    199.  
    200. Texture2D selfIllumMask
    201. <
    202.     string ResourceName = "__death_prophet_base_selfIllumMask.tga";
    203.     string UIName = "selfIllumMask";
    204.     string ResourceType = "2D";
    205.     string UIWidget = "FilePicker";
    206. >;
    207.  
    208. Texture2D translucency
    209. <
    210.     string ResourceName = "__death_prophet_base_translucency.tga";
    211.     string UIName = "translucency";
    212.     string ResourceType = "2D";
    213.     string UIWidget = "FilePicker";
    214. >;
    215.  
    216. Texture2D metalnessMask
    217. <
    218.     string ResourceName = "__death_prophet_base_metalnessMask.tga";
    219.     string UIName = "metalnessMask";
    220.     string ResourceType = "2D";
    221.     string UIWidget = "FilePicker";
    222. >;
    223.  
    224. Texture2D TransDepthTexture : transpdepthtexture
    225. <
    226.     string ResourceName = "";
    227.     string UIName = "TransDepthTexture";
    228.     string ResourceType = "2D";
    229.     string UIWidget = "None";
    230. >;
    231.  
    232. Texture2D OpaqueDepthTexture : opaquedepthtexture
    233. <
    234.     string ResourceName = "";
    235.     string UIName = "OpaqueDepthTexture";
    236.     string ResourceType = "2D";
    237.     string UIWidget = "None";
    238. >;
    239.  
    240. SamplerState MMMPBBW_255_255_255_255_Sampler
    241. {
    242.     Filter = MIN_MAG_MIP_POINT;
    243.     AddressU = BORDER;
    244.     AddressV = BORDER;
    245.     AddressW = WRAP;
    246.     BorderColor = float4(1.000000,1.000000,1.000000,1.000000);
    247. };
    248.  
    249.  
    250. // ---------------------------------------- Functions -----------------------------------------
    251.  
    252. // You may add new struct outputs, function inputs and adjust code in the function.
    253. // Function name and output struct should match 'Function Name' attribute on node.
    254. // Available preprocessor definitions: SFX_HLSL_3, SFX_HLSL_5, SFX_GLSL_1_2, SFX_GLSL_4, SFX_CGFX_3, SFX_OSGFX, _MAYA_, _3DSMAX_, SFX_SWATCH
    255. // You can use SFX_TEXTURE[n] or SFX_SAMPLER[n] to refer to texture inputs so you do not have to hardcode their names
    256.  
    257. float3 my_lerp( float3 a, float3 b, float w )
    258. {
    259.     return a + w * ( b - a ) ;
    260. };
    261.  
    262. float my_saturate( float x )
    263. {
    264.     return (x < 0.0) ? 0.0 : (1.0 < x) ? 1.0 : x;
    265. };
    266.  
    267. float3 calcReflectionVectorUnnormalized( float3 N, float3 L ){
    268.     return ( 2.0 * ( dot ( N, L ) ) * N ) - ( dot( N, N ) * L );
    269. };
    270.  
    271. float2 calcCubeMapUV( float3 N )
    272. {
    273.     float nx_abs = abs( N.r );
    274.     float ny_abs = abs( N.g );
    275.     float nz_abs = abs( N.b );
    276.     float cubeU = 0;
    277.     float cubeV = 0;
    278.  
    279.     if ( nx_abs > ny_abs && nx_abs > nz_abs  )
    280.     {
    281.         if ( N.r > 0.0 )
    282.         {
    283.             cubeU = ( ( ( -1.0 * N.b ) / nx_abs ) + 1.0 ) / 2.0;
    284.             cubeV = ( ( ( -1.0 * N.g ) / nx_abs ) + 1.0 ) / 2.0;
    285.         }
    286.         else
    287.         {
    288.             cubeU = ( ( ( 1.0 * N.b ) / nx_abs ) + 1.0 ) / 2.0;
    289.             cubeV = ( ( ( -1.0 * N.g ) / nx_abs ) + 1.0 ) / 2.0;
    290.         }
    291.     }
    292.     else if ( ny_abs > nx_abs && ny_abs > nz_abs )
    293.     {
    294.         if ( N.g > 0.0 )
    295.         {
    296.             cubeU = ( ( ( 1.0 * N.r ) / ny_abs ) + 1.0 ) / 2.0;
    297.             cubeV = ( ( ( 1.0 * N.b ) / ny_abs ) + 1.0 ) / 2.0;
    298.         }
    299.         else
    300.         {
    301.             cubeU = ( ( ( 1.0 * N.r ) / ny_abs ) + 1.0 ) / 2.0;
    302.             cubeV = ( ( ( -1.0 * N.b ) / ny_abs ) + 1.0 ) / 2.0;
    303.         }
    304.  
    305.     }
    306.     else
    307.     {
    308.         if ( N.b > 0.0 )
    309.         {
    310.             cubeU = ( ( ( 1.0 * N.r ) / nz_abs ) + 1.0 ) / 2.0;
    311.             cubeV = ( ( ( -1.0 * N.g ) / nz_abs ) + 1.0 ) / 2.0;
    312.         }
    313.         else
    314.         {
    315.             cubeU = ( ( ( -1.0 * N.r ) / nz_abs ) + 1.0 ) / 2.0;
    316.             cubeV = ( ( ( -1.0 * N.g ) / nz_abs ) + 1.0 ) / 2.0;
    317.         }
    318.     }
    319.  
    320.     return float2( cubeU, cubeV );
    321. };
    322.  
    323. struct Dota2HeroOutput
    324. {
    325.     float4 color;
    326. };
    327.  
    328. Dota2HeroOutput Dota2HeroFunc(
    329.         float3 color, float3 normal,
    330.         float3 specularMask, float3 specularColor, float specularExponent, float specularScale,
    331.         float3 rimMask, float3 rimLightColor, float rimLightScale,
    332.         float3 selfIllumMask, float cubeMapScale, float3 translucency, float3 metalnessMask,
    333.         float3 Nn, float3 Tn, float3 Bn, float3 L  )
    334. {
    335.     Dota2HeroOutput OUT;
    336.  
    337.     //
    338.     // compute the diffuse element
    339.     //
    340.     normal.g = 1.0 - normal.g;
    341.     normal = normal * 2 - 1.0;
    342.  
    343.     float3 N = normalize( (normal.x * Tn) + (normal.y * Bn) + (normal.z * Nn) );
    344.  
    345.     float N_dot_L = 0;
    346.     #ifdef SFX_HLSL_5
    347.         N_dot_L = saturate( dot( N, L ) );
    348.     #endif
    349.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    350.         N_dot_L = my_saturate( dot( N, L ) );
    351.     #endif
    352.  
    353.     float halfLambert = ( N_dot_L * 0.5 ) + 0.5;
    354.     float3 diffuse = float3( halfLambert, halfLambert, halfLambert );
    355.  
    356.     //
    357.     // compute the fresnel terms
    358.     //
    359.     float L_dot_N = 0;
    360.     #ifdef SFX_HLSL_5
    361.         L_dot_N = saturate( dot( L, Nn ) );
    362.     #endif
    363.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    364.         L_dot_N = my_saturate( dot( L, Nn ) );
    365.     #endif
    366.  
    367.     float fwRim = 0;
    368.     float fwColor = 0;
    369.     float fwSpec = 0;
    370.  
    371.     #ifdef SFX_HLSL_5
    372.         fwColor = fresnelWarpColor.Sample( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).r;
    373.         fwRim = fresnelWarpRim.Sample( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).g;
    374.         fwSpec = fresnelWarpSpec.Sample( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).b;
    375.     #endif
    376.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    377.         fwColor = tex2D( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).x;
    378.         fwRim = tex2D( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).y;
    379.         fwSpec = tex2D( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).z;
    380.     #endif
    381.  
    382.     /**
    383.      * TODO: NON-HLSL FUNCTIONS GO HERE
    384.      */
    385.  
    386.     //
    387.     // compute the specular element
    388.     //
    389.     float3 R = reflect( L, N );
    390.  
    391.     float R_dot_L = 0;
    392.     #ifdef SFX_HLSL_5
    393.         R_dot_L = saturate( dot( L, -R.rgb ) );
    394.     #endif
    395.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    396.         R_dot_L = my_saturate( dot( L, -R.rgb ) );
    397.     #endif
    398.  
    399.     float specularIntensity = 0;
    400.     #ifdef SFX_HLSL_5
    401.         specularIntensity = saturate( N_dot_L ) * pow( max( 0.001, R_dot_L ), specularExponent );
    402.     #endif
    403.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    404.         specularIntensity = my_saturate( N_dot_L ) * pow( max( 0.001, R_dot_L ), specularExponent );
    405.     #endif
    406.  
    407.     float3 spec = float3( specularIntensity, specularIntensity, specularIntensity );
    408.     spec *= specularScale;
    409.    
    410.     //
    411.     // cubemap
    412.     //
    413.     float3 RU = calcReflectionVectorUnnormalized( L, N );
    414.     float2 cubeUV = calcCubeMapUV( RU );
    415.     float3 vCubemap = float3( 0, 0, 0 );
    416.     #ifdef SFX_HLSL_5
    417.         vCubemap = cubeMap.Sample( MMMLCCCSampler, cubeUV ).r ;
    418.     #endif
    419.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    420.         vCubemap = tex2D( MMMLCCCSampler, cubeUV ).r ;
    421.     #endif
    422.     spec += ( vCubemap.rgb * ( 1.0 - metalnessMask.r ) );
    423.     spec *= specularMask;
    424.    
    425.     float3 specTint = float3( 1, 1, 1 );
    426.     #ifdef SFX_HLSL_5
    427.         specTint = lerp( color, specularColor, specularMask );
    428.     #endif
    429.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    430.         specTint = my_lerp( color, specularColor, specularMask );
    431.     #endif
    432.     spec *= specTint;
    433.     spec *= fwSpec;
    434.  
    435.     //
    436.     // accumulate the final color
    437.     //
    438.     float3 accum = ( color * diffuse ) + spec;
    439.  
    440.     //
    441.     // metalness
    442.     //
    443.     float3 metalness = spec;
    444.     #ifdef SFX_HLSL_5
    445.         accum = lerp( accum, metalness, metalnessMask.r );
    446.     #endif
    447.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    448.         accum = my_lerp( accum, metalness, metalnessMask.r );
    449.     #endif
    450.  
    451.     //
    452.     // add rim light
    453.     //
    454.     float3 rimLight = rimLightColor * rimLightScale * rimMask;
    455.     rimLight *= max(0, dot( Nn, float3( 0, 1, 0 ) ) );
    456.     rimLight *= fwRim;
    457.     accum += rimLight;
    458.  
    459.     #ifdef SFX_HLSL_5
    460.         accum = lerp( accum, color, selfIllumMask.r );
    461.     #endif
    462.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    463.         accum = my_lerp( accum, color, selfIllumMask.r );
    464.     #endif
    465.  
    466.     OUT.color = float4( accum.r, accum.g, accum.b, translucency.r );
    467.     return OUT;
    468. };
    469.  
    470.  
    471.  
    472. float4 sampleTransDepthTex(float2 UV)
    473. {
    474.     float4 col = float4(0,0,0,0);
    475.  
    476.     #if defined(SFX_CGFX_3) || defined(SFX_HLSL_3)
    477.         col = tex2D( MMMPBBW_255_255_255_255_Sampler, UV );
    478.     #endif
    479.     #ifdef SFX_HLSL_5
    480.         #if defined(SFX_SWATCH) || defined(_3DSMAX_)
    481.             col = TransDepthTexture.Sample( MMMPBBW_255_255_255_255_Sampler, UV );
    482.         #else
    483.             col = TransDepthTexture.Sample( MMMPBBW_255_255_255_255_Sampler, UV );
    484.         #endif
    485.     #endif
    486.     #ifdef SFX_GLSL_4
    487.         col = texture( TransDepthTexture, UV );
    488.     #endif
    489.     #ifdef SFX_OGSFX
    490.         col = texture( MMMPBBW_255_255_255_255_Sampler, UV );
    491.     #endif
    492.     #ifdef SFX_GLSL_1_2
    493.         col = texture2D( TransDepthTexture, UV );
    494.     #endif
    495.  
    496.     return col;
    497. }
    498.  
    499. float4 sampleOpaqueDepthTex(float2 UV)
    500. {
    501.     float4 col = float4(0,0,0,0);
    502.  
    503.     #if defined(SFX_CGFX_3) || defined(SFX_HLSL_3)
    504.         col = tex2D( MMMPBBW_255_255_255_255_Sampler, UV );
    505.     #endif
    506.     #ifdef SFX_HLSL_5
    507.         #if defined(SFX_SWATCH) || defined(_3DSMAX_)
    508.             col = OpaqueDepthTexture.Sample( MMMPBBW_255_255_255_255_Sampler, UV );
    509.         #else
    510.             col = OpaqueDepthTexture.Sample( MMMPBBW_255_255_255_255_Sampler, UV );
    511.         #endif
    512.     #endif
    513.     #ifdef SFX_GLSL_4
    514.         col = texture( OpaqueDepthTexture, UV );
    515.     #endif
    516.     #ifdef SFX_OGSFX
    517.         col = texture( MMMPBBW_255_255_255_255_Sampler, UV );
    518.     #endif
    519.     #ifdef SFX_GLSL_1_2
    520.         col = texture2D( OpaqueDepthTexture, UV );
    521.     #endif
    522.  
    523.     return col;
    524. }
    525.  
    526. struct DepthPeelOutput
    527. {
    528.     float4 LinearDepth;
    529.     float Peel;
    530. };
    531.  
    532. DepthPeelOutput DepthPeelFunc( float3 worldPos, float4x4 view, float4x4 viewPrj )
    533. {
    534.     DepthPeelOutput OUT;
    535.  
    536.     #ifdef SFX_CGFX_3
    537.         float currZ = abs( mul( view, float4(worldPos, 1.0f) ).z );
    538.         float4 Pndc  = mul( viewPrj, float4(worldPos, 1.0f) );
    539.         float2 UV = Pndc.xy / Pndc.w * float2(0.5f, 0.5f) + 0.5f;
    540.  
    541.         float prevZ = sampleTransDepthTex(UV).r;
    542.         float opaqZ = sampleOpaqueDepthTex(UV).r;
    543.         float bias = 0.00002f;
    544.         if (currZ < prevZ * (1.0f + bias) || currZ > opaqZ * (1.0f - bias))
    545.         {
    546.             discard;
    547.         }
    548.  
    549.         float ld = abs( mul( view, float4(worldPos, 1.0f) ).z );
    550.         OUT.LinearDepth = float4(ld, ld, ld, ld);
    551.     #else
    552.     #if defined(SFX_GLSL_1_2) || defined(SFX_GLSL_4) || defined(SFX_OGSFX)
    553.         float currZ = abs( ( view * float4(worldPos, 1.0f) ).z );
    554.         float4 Pndc  = viewPrj * float4(worldPos, 1.0f);
    555.         float2 UV = Pndc.xy / Pndc.w * float2(0.5f, 0.5f) + 0.5f;
    556.  
    557.         float prevZ = sampleTransDepthTex(UV).r;
    558.         float opaqZ = sampleOpaqueDepthTex(UV).r;
    559.         float bias = 0.00002f;
    560.         if (currZ < prevZ * (1.0f + bias) || currZ > opaqZ * (1.0f - bias))
    561.         {
    562.             discard;
    563.         }
    564.  
    565.         float ld = abs( ( view * float4(worldPos, 1.0f) ).z );
    566.         OUT.LinearDepth = float4(ld, ld, ld, ld);
    567.     #else
    568.         float currZ = abs( mul( float4(worldPos, 1.0f), view ).z );
    569.         float4 Pndc  = mul( float4(worldPos, 1.0f), viewPrj );
    570.         float2 UV = Pndc.xy / Pndc.w * float2(0.5f, -0.5f) + 0.5f;
    571.  
    572.         float prevZ = sampleTransDepthTex(UV).r;
    573.         float opaqZ = sampleOpaqueDepthTex(UV).r;
    574.         float bias = 0.00002f;
    575.         if (currZ < prevZ * (1.0f + bias) || currZ > opaqZ * (1.0f - bias))
    576.         {
    577.             discard;
    578.         }
    579.  
    580.         float ld = abs( mul( float4(worldPos, 1.0f), view ).z );
    581.         OUT.LinearDepth = float4(ld, ld, ld, ld);
    582. #endif
    583.     #endif
    584.  
    585.     OUT.Peel = 1.0f;
    586.  
    587.     return OUT;
    588. }
    589.  
    590.  
    591.  
    592. // -------------------------------------- ShaderVertex --------------------------------------
    593. struct APPDATA
    594. {
    595.     float3 Position : POSITION;
    596.     float2 map1 : TEXCOORD0;
    597.     float3 Normal : NORMAL;
    598.     float3 Tangent : TANGENT;
    599.     float3 BiNormal : BINORMAL;
    600. };
    601.  
    602. struct SHADERDATA
    603. {
    604.     float4 Position : SV_Position;
    605.     float4 map1 : TEXCOORD0;
    606.     float4 Normal : NORMAL;
    607.     float4 Tangent : TANGENT;
    608.     float4 BiNormal : BINORMAL;
    609.     float4 WorldPosition : TEXCOORD1;
    610.     half3 FogFactor : TEXCOORD2;
    611. };
    612.  
    613. SHADERDATA ShaderVertex(APPDATA IN)
    614. {
    615.     SHADERDATA OUT;
    616.  
    617.     OUT.Position = float4(IN.Position, 1);
    618.     float4 OutUVs = float4(IN.map1.x, IN.map1.y, 0.000000, 0.000000);
    619.     OUT.map1 = OutUVs;
    620.     float3 MulOp = mul(IN.Normal, ((float3x3)world));
    621.     float3 NormalN = normalize(MulOp);
    622.     float4 WorldNormal = float4(NormalN.x, NormalN.y, NormalN.z, 1.000000);
    623.     OUT.Normal = WorldNormal;
    624.     float3 MulOp431 = mul(IN.Tangent, ((float3x3)world));
    625.     float3 TangentNorm = normalize(MulOp431);
    626.     float4 WorldTangent = float4(TangentNorm.x, TangentNorm.y, TangentNorm.z, 1.000000);
    627.     OUT.Tangent = WorldTangent;
    628.     float3 MulOp407 = mul(IN.BiNormal, ((float3x3)world));
    629.     float3 BiNormalNorm = normalize(MulOp407);
    630.     float4 WorldBiNormal = float4(BiNormalNorm.x, BiNormalNorm.y, BiNormalNorm.z, 1.000000);
    631.     OUT.BiNormal = WorldBiNormal;
    632.     float4 WorldPos = mul(OUT.Position, world);
    633.     OUT.WorldPosition = WorldPos;
    634.     OUT.WorldPosition = (mul(float4(IN.Position,1), world));
    635.     float4 _HPosition = mul( float4(OUT.WorldPosition.xyz, 1), viewPrj );
    636.     float fogFactor = 0.0;
    637.     if (MayaHwFogMode == 0) {
    638.                 fogFactor = saturate((MayaHwFogEnd - _HPosition.z) / (MayaHwFogEnd - MayaHwFogStart));
    639.     }
    640.     else if (MayaHwFogMode == 1) {
    641.                 fogFactor = 1.0 / (exp(_HPosition.z * MayaHwFogDensity));
    642.     }
    643.     else if (MayaHwFogMode == 2) {
    644.                 fogFactor = 1.0 / (exp(pow(_HPosition.z * MayaHwFogDensity, 2)));
    645.     }
    646.     OUT.FogFactor = float3(fogFactor, fogFactor, fogFactor);
    647.  
    648.     float4 WVSpace = mul(OUT.Position, wvp);
    649.     OUT.Position = WVSpace;
    650.  
    651.     return OUT;
    652. }
    653.  
    654. // -------------------------------------- ShaderPixel --------------------------------------
    655. struct PIXELDATA
    656. {
    657.     float4 Color : SV_Target;
    658. };
    659.  
    660. PIXELDATA ShaderPixel(SHADERDATA IN)
    661. {
    662.     PIXELDATA OUT;
    663.  
    664.     float4 LightLoopTotal11 = float4(0,0,0,0);
    665.     for (int ActiveLightIndex = 0; ActiveLightIndex < 3; ++ActiveLightIndex)
    666.     {
    667.         if (ActiveLightIndex >= ClampDynamicLights) {continue;}
    668.         LightLoopTotal11 += float4(0, 0, 0, 0);
    669.     }
    670.     float4 Sampler = color.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    671.     float4 Sampler378 = normal.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    672.     float4 Sampler463 = specularMask.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    673.     float3 PowOp = pow(Sampler463.xyz, float3(2.200000,2.200000,2.200000));
    674.     float4 Sampler491 = rimMask.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    675.     float3 PowOp506 = pow(Sampler491.xyz, float3(2.200000,2.200000,2.200000));
    676.     float4 Sampler520 = selfIllumMask.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    677.     float3 PowOp535 = pow(Sampler520.xyz, float3(2.200000,2.200000,2.200000));
    678.     float4 Sampler580 = translucency.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    679.     float3 PowOp595 = pow(Sampler580.xyz, float3(2.200000,2.200000,2.200000));
    680.     float4 Sampler607 = metalnessMask.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    681.     float3 PowOp622 = pow(Sampler607.xyz, float3(2.200000,2.200000,2.200000));
    682.     float3 NormOp = normalize(IN.Normal.xyz);
    683.     float3 NormOp439 = normalize(IN.Tangent.xyz);
    684.     float3 NormOp415 = normalize(IN.BiNormal.xyz);
    685.     float3 CameraPosition = viewI[3].xyz;
    686.     float3 CamVec = (CameraPosition - IN.WorldPosition.xyz);
    687.     float3 CamVecNorm = normalize(CamVec);
    688.     Dota2HeroOutput CustomCode = Dota2HeroFunc(Sampler.xyz, Sampler378.xyz, PowOp, specularColor.xyz, specularExponent, specularScale, PowOp506, rimLightColor.xyz, rimLightScale, PowOp535, cubeMapScalar, PowOp595, PowOp622, NormOp, NormOp439, NormOp415, CamVecNorm);
    689.     float4 LightLoopAndAfterLoop = (LightLoopTotal11 + CustomCode.color);
    690.     float SatOp = saturate(LightLoopAndAfterLoop.w);
    691.     float4 VectorConstruct = float4(LightLoopAndAfterLoop.xyz.x, LightLoopAndAfterLoop.xyz.y, LightLoopAndAfterLoop.xyz.z, SatOp);
    692.     if (MayaHwFogEnabled) {
    693.         float fogFactor = (1.0 - IN.FogFactor.x) * MayaHwFogColor.a;
    694.         VectorConstruct.rgb    = lerp(VectorConstruct.rgb, MayaHwFogColor.rgb, fogFactor);
    695.     }
    696.  
    697.     OUT.Color = VectorConstruct;
    698.  
    699.     return OUT;
    700. }
    701.  
    702. // -------------------------------------- ShaderPixelP1 --------------------------------------
    703. struct PIXELDATAP1
    704. {
    705.     float4 Color0 : SV_Target0;
    706.     float4 Color1 : SV_Target1;
    707. };
    708.  
    709. PIXELDATAP1 ShaderPixelP1(SHADERDATA IN)
    710. {
    711.     PIXELDATAP1 OUT;
    712.  
    713.     DepthPeelOutput DepthPeel = DepthPeelFunc(IN.WorldPosition.xyz, view, viewPrj);
    714.     OUT.Color0 = ShaderPixel(IN).Color;
    715.     OUT.Color1 = DepthPeel.LinearDepth;
    716.  
    717.     return OUT;
    718. }
    719.  
    720. // -------------------------------------- ShaderPixelP2 --------------------------------------
    721. struct PIXELDATAP2
    722. {
    723.     float4 Color0 : SV_Target0;
    724.     float4 Color1 : SV_Target1;
    725. };
    726.  
    727. PIXELDATAP2 ShaderPixelP2(SHADERDATA IN)
    728. {
    729.     PIXELDATAP2 OUT;
    730.  
    731.     DepthPeelOutput DepthPeel = DepthPeelFunc(IN.WorldPosition.xyz, view, viewPrj);
    732.     OUT.Color0 = ShaderPixel(IN).Color;
    733.     OUT.Color1 = (DepthPeel.Peel * (OUT.Color0.w > 0.001f ? float4(1.0f, 1.0f, 1.0f, 1.0f) : float4(0.0f, 0.0f, 0.0f, 0.0f)));
    734.  
    735.     return OUT;
    736. }
    737.  
    738. // -------------------------------------- ShaderPixelP3 --------------------------------------
    739. struct PIXELDATAP3
    740. {
    741.     float4 Color0 : SV_Target0;
    742.     float4 Color1 : SV_Target1;
    743. };
    744.  
    745. PIXELDATAP3 ShaderPixelP3(SHADERDATA IN)
    746. {
    747.     PIXELDATAP3 OUT;
    748.  
    749.     OUT.Color0 = ShaderPixel(IN).Color;
    750.     OUT.Color1 = (OUT.Color0.w > 0.001f ? float4(1.0f, 1.0f, 1.0f, 1.0f) : float4(0.0f, 0.0f, 0.0f, 0.0f));
    751.  
    752.     return OUT;
    753. }
    754.  
    755. // -------------------------------------- technique T0 ---------------------------------------
    756. technique11 T0
    757. <
    758.     bool overridesDrawState = false;
    759.     int isTransparent = 0;
    760. >
    761. {
    762.     pass P0
    763.     <
    764.         string drawContext = "colorPass";
    765.     >
    766.     {
    767.         SetVertexShader(CompileShader(vs_5_0, ShaderVertex()));
    768.         SetPixelShader(CompileShader(ps_5_0, ShaderPixel()));
    769.         SetHullShader(NULL);
    770.         SetDomainShader(NULL);
    771.         SetGeometryShader(NULL);
    772.     }
    773.  
    774.     pass P1
    775.     <
    776.         string drawContext = "transparentPeel";
    777.     >
    778.     {
    779.         SetVertexShader(CompileShader(vs_5_0, ShaderVertex()));
    780.         SetPixelShader(CompileShader(ps_5_0, ShaderPixelP1()));
    781.         SetHullShader(NULL);
    782.         SetDomainShader(NULL);
    783.         SetGeometryShader(NULL);
    784.     }
    785.  
    786.     pass P2
    787.     <
    788.         string drawContext = "transparentPeelAndAvg";
    789.     >
    790.     {
    791.         SetVertexShader(CompileShader(vs_5_0, ShaderVertex()));
    792.         SetPixelShader(CompileShader(ps_5_0, ShaderPixelP2()));
    793.         SetHullShader(NULL);
    794.         SetDomainShader(NULL);
    795.         SetGeometryShader(NULL);
    796.     }
    797.  
    798.     pass P3
    799.     <
    800.         string drawContext = "transparentWeightedAvg";
    801.     >
    802.     {
    803.         SetVertexShader(CompileShader(vs_5_0, ShaderVertex()));
    804.         SetPixelShader(CompileShader(ps_5_0, ShaderPixelP3()));
    805.         SetHullShader(NULL);
    806.         SetDomainShader(NULL);
    807.         SetGeometryShader(NULL);
    808.     }
    809.  
    810. }
    811.  
    812.  
     
  4. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Work in progress (shader currently doesn't work, I have to fix texture samplers and insert proper matrices):
    Code (CSharp):
    1. Shader "Dota"
    2. {
    3.     Properties
    4.     {
    5.         fresnelWarpColor ("Fresnel Warp Color", 2D) = "white" {}
    6.         fresnelWarpRim ("Fresnel Warp Rim", 2D) = "white" {}
    7.         fresnelWarpSpec ("Fresnel Warp Spec", 2D) = "white" {}
    8.         cubeMap ("Cube Map", 2D) = "white" {}
    9.         //color ("Color Map", 2D) = "white" {}
    10.         normal ("Normal Map", 2D) = "white" {}
    11.         specularMask ("Specular Mask", 2D) = "white" {}
    12.         rimMask ("Rim Mask", 2D) = "white" {}
    13.         selfIllumMask ("selfIllumMask", 2D) = "white" {}
    14.         translucency ("translucency", 2D) = "white" {}
    15.         metalnessMask ("metalnessMask", 2D) = "white" {}
    16.         TransDepthTexture ("TransDepthTexture", 2D) = "white" {}
    17.         OpaqueDepthTexture ("OpaqueDepthTexture", 2D) = "white" {}        
    18.     }
    19.     SubShader
    20.     {
    21.         CGINCLUDE
    22.        
    23.         // ----------------------------------------- Header ------------------------------------------
    24.         #ifndef SFX_HLSL_5
    25.             #define SFX_HLSL_5
    26.         #endif
    27.         #ifndef _MAYA_
    28.             #define _MAYA_
    29.         #endif
    30.        
    31.        
    32.         #ifndef half
    33.             #define half float
    34.             #define half2 float2
    35.             #define half3 float3
    36.             #define half4 float4
    37.         #endif  
    38.  
    39.         Texture2D fresnelWarpColor, fresnelWarpRim, fresnelWarpSpec, cubeMap, color, normal, specularMask, rimMask, selfIllumMask;
    40.         Texture2D translucency, metalnessMask, TransDepthTexture, OpaqueDepthTexture;
    41.  
    42.         int ClampDynamicLights = 99;
    43.         float4x4 world;
    44.         float4x4 wvp;
    45.         float4x4 viewI;
    46.         float4x4 viewPrj;
    47.         float4x4 view;
    48.        
    49.     bool MayaHwFogEnabled  = false;
    50.     int MayaHwFogMode  = 0;
    51.     float MayaHwFogStart  = 0.0;
    52.     float MayaHwFogEnd  = 100.0;
    53.     float MayaHwFogDensity  = 0.1;
    54.     float4 MayaHwFogColor  =  float4(0.5, 0.5, 0.5, 1.0 );
    55.    
    56. float3 specularColor = float3(0.854306,0.954687,1.000000);
    57. float specularExponent = 23.000000;
    58. float specularScale = 42.000000;
    59. float3 rimLightColor = float3(0.735357,0.906332,0.954687);
    60. float rimLightScale = 10.000000;
    61. float cubeMapScalar = 0.000000;  
    62.    
    63. /*    SamplerState MMMLCCCSampler
    64. {
    65.     Filter = MIN_MAG_MIP_LINEAR;
    66.     AddressU = CLAMP;
    67.     AddressV = CLAMP;
    68.     AddressW = CLAMP;
    69. };
    70. SamplerState MMMLWWWSampler
    71. {
    72.     Filter = MIN_MAG_MIP_LINEAR;
    73.     AddressU = WRAP;
    74.     AddressV = WRAP;
    75.     AddressW = WRAP;
    76. };
    77. SamplerState MMMPBBW_255_255_255_255_Sampler
    78. {
    79.     Filter = MIN_MAG_MIP_POINT;
    80.     AddressU = BORDER;
    81.     AddressV = BORDER;
    82.     AddressW = WRAP;
    83.     BorderColor = float4(1.000000,1.000000,1.000000,1.000000);
    84. };    */
    85.  
    86. sampler2D MMMLCCCSampler,MMMLWWWSampler,MMMPBBW_255_255_255_255_Sampler;
    87.        
    88.        
    89.        
    90. float3 my_lerp( float3 a, float3 b, float w )
    91. {
    92.     return a + w * ( b - a ) ;
    93. };
    94. float my_saturate( float x )
    95. {
    96.     return (x < 0.0) ? 0.0 : (1.0 < x) ? 1.0 : x;
    97. };
    98. float3 calcReflectionVectorUnnormalized( float3 N, float3 L ){
    99.     return ( 2.0 * ( dot ( N, L ) ) * N ) - ( dot( N, N ) * L );
    100. };
    101. float2 calcCubeMapUV( float3 N )
    102. {
    103.     float nx_abs = abs( N.r );
    104.     float ny_abs = abs( N.g );
    105.     float nz_abs = abs( N.b );
    106.     float cubeU = 0;
    107.     float cubeV = 0;
    108.     if ( nx_abs > ny_abs && nx_abs > nz_abs  )
    109.     {
    110.         if ( N.r > 0.0 )
    111.         {
    112.             cubeU = ( ( ( -1.0 * N.b ) / nx_abs ) + 1.0 ) / 2.0;
    113.             cubeV = ( ( ( -1.0 * N.g ) / nx_abs ) + 1.0 ) / 2.0;
    114.         }
    115.         else
    116.         {
    117.             cubeU = ( ( ( 1.0 * N.b ) / nx_abs ) + 1.0 ) / 2.0;
    118.             cubeV = ( ( ( -1.0 * N.g ) / nx_abs ) + 1.0 ) / 2.0;
    119.         }
    120.     }
    121.     else if ( ny_abs > nx_abs && ny_abs > nz_abs )
    122.     {
    123.         if ( N.g > 0.0 )
    124.         {
    125.             cubeU = ( ( ( 1.0 * N.r ) / ny_abs ) + 1.0 ) / 2.0;
    126.             cubeV = ( ( ( 1.0 * N.b ) / ny_abs ) + 1.0 ) / 2.0;
    127.         }
    128.         else
    129.         {
    130.             cubeU = ( ( ( 1.0 * N.r ) / ny_abs ) + 1.0 ) / 2.0;
    131.             cubeV = ( ( ( -1.0 * N.b ) / ny_abs ) + 1.0 ) / 2.0;
    132.         }
    133.     }
    134.     else
    135.     {
    136.         if ( N.b > 0.0 )
    137.         {
    138.             cubeU = ( ( ( 1.0 * N.r ) / nz_abs ) + 1.0 ) / 2.0;
    139.             cubeV = ( ( ( -1.0 * N.g ) / nz_abs ) + 1.0 ) / 2.0;
    140.         }
    141.         else
    142.         {
    143.             cubeU = ( ( ( -1.0 * N.r ) / nz_abs ) + 1.0 ) / 2.0;
    144.             cubeV = ( ( ( -1.0 * N.g ) / nz_abs ) + 1.0 ) / 2.0;
    145.         }
    146.     }
    147.     return float2( cubeU, cubeV );
    148. };
    149. struct Dota2HeroOutput
    150. {
    151.     float4 color;
    152. };
    153. Dota2HeroOutput Dota2HeroFunc(
    154.         float3 color, float3 normal,
    155.         float3 specularMask, float3 specularColor, float specularExponent, float specularScale,
    156.         float3 rimMask, float3 rimLightColor, float rimLightScale,
    157.         float3 selfIllumMask, float cubeMapScale, float3 translucency, float3 metalnessMask,
    158.         float3 Nn, float3 Tn, float3 Bn, float3 L  )
    159. {
    160.     Dota2HeroOutput OUT;
    161.     //
    162.     // compute the diffuse element
    163.     //
    164.     normal.g = 1.0 - normal.g;
    165.     normal = normal * 2 - 1.0;
    166.     float3 N = normalize( (normal.x * Tn) + (normal.y * Bn) + (normal.z * Nn) );
    167.     float N_dot_L = 0;
    168.     #ifdef SFX_HLSL_5
    169.         N_dot_L = saturate( dot( N, L ) );
    170.     #endif
    171.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    172.         N_dot_L = my_saturate( dot( N, L ) );
    173.     #endif
    174.     float halfLambert = ( N_dot_L * 0.5 ) + 0.5;
    175.     float3 diffuse = float3( halfLambert, halfLambert, halfLambert );
    176.     //
    177.     // compute the fresnel terms
    178.     //
    179.     float L_dot_N = 0;
    180.     #ifdef SFX_HLSL_5
    181.         L_dot_N = saturate( dot( L, Nn ) );
    182.     #endif
    183.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    184.         L_dot_N = my_saturate( dot( L, Nn ) );
    185.     #endif
    186.     float fwRim = 0;
    187.     float fwColor = 0;
    188.     float fwSpec = 0;
    189.     #ifdef SFX_HLSL_5
    190.         fwColor = fresnelWarpColor.Sample( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).r;
    191.         fwRim = fresnelWarpRim.Sample( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).g;
    192.         fwSpec = fresnelWarpSpec.Sample( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).b;
    193.     #endif
    194.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    195.         fwColor = tex2D( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).x;
    196.         fwRim = tex2D( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).y;
    197.         fwSpec = tex2D( MMMLCCCSampler, float2( L_dot_N, 0.5 ) ).z;
    198.     #endif
    199.     /**
    200.      * TODO: NON-HLSL FUNCTIONS GO HERE
    201.      */
    202.     //
    203.     // compute the specular element
    204.     //
    205.     float3 R = reflect( L, N );
    206.     float R_dot_L = 0;
    207.     #ifdef SFX_HLSL_5
    208.         R_dot_L = saturate( dot( L, -R.rgb ) );
    209.     #endif
    210.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    211.         R_dot_L = my_saturate( dot( L, -R.rgb ) );
    212.     #endif
    213.     float specularIntensity = 0;
    214.     #ifdef SFX_HLSL_5
    215.         specularIntensity = saturate( N_dot_L ) * pow( max( 0.001, R_dot_L ), specularExponent );
    216.     #endif
    217.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    218.         specularIntensity = my_saturate( N_dot_L ) * pow( max( 0.001, R_dot_L ), specularExponent );
    219.     #endif
    220.     float3 spec = float3( specularIntensity, specularIntensity, specularIntensity );
    221.     spec *= specularScale;
    222.  
    223.     //
    224.     // cubemap
    225.     //
    226.     float3 RU = calcReflectionVectorUnnormalized( L, N );
    227.     float2 cubeUV = calcCubeMapUV( RU );
    228.     float3 vCubemap = float3( 0, 0, 0 );
    229.     #ifdef SFX_HLSL_5
    230.         vCubemap = cubeMap.Sample( MMMLCCCSampler, cubeUV ).r ;
    231.     #endif
    232.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    233.         vCubemap = tex2D( MMMLCCCSampler, cubeUV ).r ;
    234.     #endif
    235.     spec += ( vCubemap.rgb * ( 1.0 - metalnessMask.r ) );
    236.     spec *= specularMask;
    237.  
    238.     float3 specTint = float3( 1, 1, 1 );
    239.     #ifdef SFX_HLSL_5
    240.         specTint = lerp( color, specularColor, specularMask );
    241.     #endif
    242.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    243.         specTint = my_lerp( color, specularColor, specularMask );
    244.     #endif
    245.     spec *= specTint;
    246.     spec *= fwSpec;
    247.     //
    248.     // accumulate the final color
    249.     //
    250.     float3 accum = ( color * diffuse ) + spec;
    251.     //
    252.     // metalness
    253.     //
    254.     float3 metalness = spec;
    255.     #ifdef SFX_HLSL_5
    256.         accum = lerp( accum, metalness, metalnessMask.r );
    257.     #endif
    258.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    259.         accum = my_lerp( accum, metalness, metalnessMask.r );
    260.     #endif
    261.     //
    262.     // add rim light
    263.     //
    264.     float3 rimLight = rimLightColor * rimLightScale * rimMask;
    265.     rimLight *= max(0, dot( Nn, float3( 0, 1, 0 ) ) );
    266.     rimLight *= fwRim;
    267.     accum += rimLight;
    268.     #ifdef SFX_HLSL_5
    269.         accum = lerp( accum, color, selfIllumMask.r );
    270.     #endif
    271.     #if defined( SFX_CGFX_3 ) || defined( SFX_HLSL_3 )
    272.         accum = my_lerp( accum, color, selfIllumMask.r );
    273.     #endif
    274.     OUT.color = float4( accum.r, accum.g, accum.b, translucency.r );
    275.     return OUT;
    276. };
    277. float4 sampleTransDepthTex(float2 UV)
    278. {
    279.     float4 col = float4(0,0,0,0);
    280.     #if defined(SFX_CGFX_3) || defined(SFX_HLSL_3)
    281.         col = tex2D( MMMPBBW_255_255_255_255_Sampler, UV );
    282.     #endif
    283.     #ifdef SFX_HLSL_5
    284.         #if defined(SFX_SWATCH) || defined(_3DSMAX_)
    285.             col = TransDepthTexture.Sample( MMMPBBW_255_255_255_255_Sampler, UV );
    286.         #else
    287.             col = TransDepthTexture.Sample( MMMPBBW_255_255_255_255_Sampler, UV );
    288.         #endif
    289.     #endif
    290.     #ifdef SFX_GLSL_4
    291.         col = texture( TransDepthTexture, UV );
    292.     #endif
    293.     #ifdef SFX_OGSFX
    294.         col = texture( MMMPBBW_255_255_255_255_Sampler, UV );
    295.     #endif
    296.     #ifdef SFX_GLSL_1_2
    297.         col = texture2D( TransDepthTexture, UV );
    298.     #endif
    299.     return col;
    300. }
    301. float4 sampleOpaqueDepthTex(float2 UV)
    302. {
    303.     float4 col = float4(0,0,0,0);
    304.     #if defined(SFX_CGFX_3) || defined(SFX_HLSL_3)
    305.         col = tex2D( MMMPBBW_255_255_255_255_Sampler, UV );
    306.     #endif
    307.     #ifdef SFX_HLSL_5
    308.         #if defined(SFX_SWATCH) || defined(_3DSMAX_)
    309.             col = OpaqueDepthTexture.Sample( MMMPBBW_255_255_255_255_Sampler, UV );
    310.         #else
    311.             col = OpaqueDepthTexture.Sample( MMMPBBW_255_255_255_255_Sampler, UV );
    312.         #endif
    313.     #endif
    314.     #ifdef SFX_GLSL_4
    315.         col = texture( OpaqueDepthTexture, UV );
    316.     #endif
    317.     #ifdef SFX_OGSFX
    318.         col = texture( MMMPBBW_255_255_255_255_Sampler, UV );
    319.     #endif
    320.     #ifdef SFX_GLSL_1_2
    321.         col = texture2D( OpaqueDepthTexture, UV );
    322.     #endif
    323.     return col;
    324. }
    325. struct DepthPeelOutput
    326. {
    327.     float4 LinearDepth;
    328.     float Peel;
    329. };
    330. DepthPeelOutput DepthPeelFunc( float3 worldPos, float4x4 view, float4x4 viewPrj )
    331. {
    332.     DepthPeelOutput OUT;
    333.     #ifdef SFX_CGFX_3
    334.         float currZ = abs( mul( view, float4(worldPos, 1.0f) ).z );
    335.         float4 Pndc  = mul( viewPrj, float4(worldPos, 1.0f) );
    336.         float2 UV = Pndc.xy / Pndc.w * float2(0.5f, 0.5f) + 0.5f;
    337.         float prevZ = sampleTransDepthTex(UV).r;
    338.         float opaqZ = sampleOpaqueDepthTex(UV).r;
    339.         float bias = 0.00002f;
    340.         if (currZ < prevZ * (1.0f + bias) || currZ > opaqZ * (1.0f - bias))
    341.         {
    342.             discard;
    343.         }
    344.         float ld = abs( mul( view, float4(worldPos, 1.0f) ).z );
    345.         OUT.LinearDepth = float4(ld, ld, ld, ld);
    346.     #else
    347.     #if defined(SFX_GLSL_1_2) || defined(SFX_GLSL_4) || defined(SFX_OGSFX)
    348.         float currZ = abs( ( view * float4(worldPos, 1.0f) ).z );
    349.         float4 Pndc  = viewPrj * float4(worldPos, 1.0f);
    350.         float2 UV = Pndc.xy / Pndc.w * float2(0.5f, 0.5f) + 0.5f;
    351.         float prevZ = sampleTransDepthTex(UV).r;
    352.         float opaqZ = sampleOpaqueDepthTex(UV).r;
    353.         float bias = 0.00002f;
    354.         if (currZ < prevZ * (1.0f + bias) || currZ > opaqZ * (1.0f - bias))
    355.         {
    356.             discard;
    357.         }
    358.         float ld = abs( ( view * float4(worldPos, 1.0f) ).z );
    359.         OUT.LinearDepth = float4(ld, ld, ld, ld);
    360.     #else
    361.         float currZ = abs( mul( float4(worldPos, 1.0f), view ).z );
    362.         float4 Pndc  = mul( float4(worldPos, 1.0f), viewPrj );
    363.         float2 UV = Pndc.xy / Pndc.w * float2(0.5f, -0.5f) + 0.5f;
    364.         float prevZ = sampleTransDepthTex(UV).r;
    365.         float opaqZ = sampleOpaqueDepthTex(UV).r;
    366.         float bias = 0.00002f;
    367.         if (currZ < prevZ * (1.0f + bias) || currZ > opaqZ * (1.0f - bias))
    368.         {
    369.             discard;
    370.         }
    371.         float ld = abs( mul( float4(worldPos, 1.0f), view ).z );
    372.         OUT.LinearDepth = float4(ld, ld, ld, ld);
    373. #endif
    374.     #endif
    375.     OUT.Peel = 1.0f;
    376.     return OUT;
    377. }      
    378.  
    379.  
    380. struct APPDATA
    381. {
    382.     float3 Position : POSITION;
    383.     float2 map1 : TEXCOORD0;
    384.     float3 Normal : NORMAL;
    385.     float3 Tangent : TANGENT;
    386.     float3 BiNormal : TEXCOORD3;
    387. };
    388. struct SHADERDATA
    389. {
    390.     float4 Position : SV_Position;
    391.     float4 map1 : TEXCOORD0;
    392.     float4 Normal : NORMAL;
    393.     float4 Tangent : TANGENT;
    394.     float4 BiNormal : TEXCOORD3;
    395.     float4 WorldPosition : TEXCOORD1;
    396.     half3 FogFactor : TEXCOORD2;
    397. };
    398. SHADERDATA ShaderVertex(APPDATA IN)
    399. {
    400.     SHADERDATA OUT;
    401.     OUT.Position = float4(IN.Position, 1);
    402.     float4 OutUVs = float4(IN.map1.x, IN.map1.y, 0.000000, 0.000000);
    403.     OUT.map1 = OutUVs;
    404.     float3 MulOp = mul(IN.Normal, ((float3x3)world));
    405.     float3 NormalN = normalize(MulOp);
    406.     float4 WorldNormal = float4(NormalN.x, NormalN.y, NormalN.z, 1.000000);
    407.     OUT.Normal = WorldNormal;
    408.     float3 MulOp431 = mul(IN.Tangent, ((float3x3)world));
    409.     float3 TangentNorm = normalize(MulOp431);
    410.     float4 WorldTangent = float4(TangentNorm.x, TangentNorm.y, TangentNorm.z, 1.000000);
    411.     OUT.Tangent = WorldTangent;
    412.     float3 MulOp407 = mul(IN.BiNormal, ((float3x3)world));
    413.     float3 BiNormalNorm = normalize(MulOp407);
    414.     float4 WorldBiNormal = float4(BiNormalNorm.x, BiNormalNorm.y, BiNormalNorm.z, 1.000000);
    415.     OUT.BiNormal = WorldBiNormal;
    416.     float4 WorldPos = mul(OUT.Position, world);
    417.     OUT.WorldPosition = WorldPos;
    418.     OUT.WorldPosition = (mul(float4(IN.Position,1), world));
    419.     float4 _HPosition = mul( float4(OUT.WorldPosition.xyz, 1), viewPrj );
    420.     float fogFactor = 0.0;
    421.     if (MayaHwFogMode == 0) {
    422.                 fogFactor = saturate((MayaHwFogEnd - _HPosition.z) / (MayaHwFogEnd - MayaHwFogStart));
    423.     }
    424.     else if (MayaHwFogMode == 1) {
    425.                 fogFactor = 1.0 / (exp(_HPosition.z * MayaHwFogDensity));
    426.     }
    427.     else if (MayaHwFogMode == 2) {
    428.                 fogFactor = 1.0 / (exp(pow(_HPosition.z * MayaHwFogDensity, 2)));
    429.     }
    430.     OUT.FogFactor = float3(fogFactor, fogFactor, fogFactor);
    431.     float4 WVSpace = mul(OUT.Position, wvp);
    432.     OUT.Position = WVSpace;
    433.     return OUT;
    434. }
    435.         ENDCG
    436.         Pass
    437.         {
    438.             Name "colorPass"
    439.             CGPROGRAM
    440.             #pragma vertex ShaderVertex
    441.             #pragma fragment ShaderPixel
    442.             #pragma target 5.0
    443.            
    444. struct PIXELDATA
    445. {
    446.     float4 Color : SV_Target;
    447. };
    448. PIXELDATA ShaderPixel(SHADERDATA IN)
    449. {
    450.     PIXELDATA OUT;
    451.     float4 LightLoopTotal11 = float4(0,0,0,0);
    452.     for (int ActiveLightIndex = 0; ActiveLightIndex < 3; ++ActiveLightIndex)
    453.     {
    454.         if (ActiveLightIndex >= ClampDynamicLights) {continue;}
    455.         LightLoopTotal11 += float4(0, 0, 0, 0);
    456.     }
    457.     float4 Sampler = color.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    458.     float4 Sampler378 = normal.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    459.     float4 Sampler463 = specularMask.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    460.     float3 PowOp = pow(Sampler463.xyz, float3(2.200000,2.200000,2.200000));
    461.     float4 Sampler491 = rimMask.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    462.     float3 PowOp506 = pow(Sampler491.xyz, float3(2.200000,2.200000,2.200000));
    463.     float4 Sampler520 = selfIllumMask.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    464.     float3 PowOp535 = pow(Sampler520.xyz, float3(2.200000,2.200000,2.200000));
    465.     float4 Sampler580 = translucency.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    466.     float3 PowOp595 = pow(Sampler580.xyz, float3(2.200000,2.200000,2.200000));
    467.     float4 Sampler607 = metalnessMask.Sample(MMMLWWWSampler, float2(IN.map1.xy.x, 1-IN.map1.xy.y));
    468.     float3 PowOp622 = pow(Sampler607.xyz, float3(2.200000,2.200000,2.200000));
    469.     float3 NormOp = normalize(IN.Normal.xyz);
    470.     float3 NormOp439 = normalize(IN.Tangent.xyz);
    471.     float3 NormOp415 = normalize(IN.BiNormal.xyz);
    472.     float3 CameraPosition = viewI[3].xyz;
    473.     float3 CamVec = (CameraPosition - IN.WorldPosition.xyz);
    474.     float3 CamVecNorm = normalize(CamVec);
    475.     Dota2HeroOutput CustomCode = Dota2HeroFunc(Sampler.xyz, Sampler378.xyz, PowOp, specularColor.xyz, specularExponent, specularScale, PowOp506, rimLightColor.xyz, rimLightScale, PowOp535, cubeMapScalar, PowOp595, PowOp622, NormOp, NormOp439, NormOp415, CamVecNorm);
    476.     float4 LightLoopAndAfterLoop = (LightLoopTotal11 + CustomCode.color);
    477.     float SatOp = saturate(LightLoopAndAfterLoop.w);
    478.     float4 VectorConstruct = float4(LightLoopAndAfterLoop.xyz.x, LightLoopAndAfterLoop.xyz.y, LightLoopAndAfterLoop.xyz.z, SatOp);
    479.     if (MayaHwFogEnabled) {
    480.         float fogFactor = (1.0 - IN.FogFactor.x) * MayaHwFogColor.a;
    481.         VectorConstruct.rgb    = lerp(VectorConstruct.rgb, MayaHwFogColor.rgb, fogFactor);
    482.     }
    483.     OUT.Color = VectorConstruct;
    484.     return OUT;
    485. }
    486.             ENDCG
    487.         }
    488.         Pass
    489.         {
    490.             Name "transparentPeel"
    491.             CGPROGRAM
    492.             #pragma vertex ShaderVertex
    493.             #pragma fragment ShaderPixel1
    494.             #pragma target 5.0
    495.            
    496. struct PIXELDATAP1
    497. {
    498.     float4 Color0 : SV_Target0;
    499.     float4 Color1 : SV_Target1;
    500. };
    501. PIXELDATAP1 ShaderPixelP1(SHADERDATA IN)
    502. {
    503.     PIXELDATAP1 OUT;
    504.     DepthPeelOutput DepthPeel = DepthPeelFunc(IN.WorldPosition.xyz, view, viewPrj);
    505.     OUT.Color0 = ShaderPixel(IN).Color;
    506.     OUT.Color1 = DepthPeel.LinearDepth;
    507.     return OUT;
    508. }
    509.             ENDCG
    510.         }
    511.         Pass
    512.         {
    513.             Name "transparentPeelAndAvg"
    514.             CGPROGRAM
    515.             #pragma vertex ShaderVertex
    516.             #pragma fragment ShaderPixel2
    517.             #pragma target 5.0
    518.            
    519. struct PIXELDATAP2
    520. {
    521.     float4 Color0 : SV_Target0;
    522.     float4 Color1 : SV_Target1;
    523. };
    524. PIXELDATAP2 ShaderPixelP2(SHADERDATA IN)
    525. {
    526.     PIXELDATAP2 OUT;
    527.     DepthPeelOutput DepthPeel = DepthPeelFunc(IN.WorldPosition.xyz, view, viewPrj);
    528.     OUT.Color0 = ShaderPixel(IN).Color;
    529.     OUT.Color1 = (DepthPeel.Peel * (OUT.Color0.w > 0.001f ? float4(1.0f, 1.0f, 1.0f, 1.0f) : float4(0.0f, 0.0f, 0.0f, 0.0f)));
    530.     return OUT;
    531. }
    532.             ENDCG
    533.         }
    534.         Pass
    535.         {
    536.             Name "transparentWeightedAvg"      
    537.             CGPROGRAM
    538.             #pragma vertex ShaderVertex
    539.             #pragma fragment ShaderPixel3
    540.             #pragma target 5.0
    541.            
    542. struct PIXELDATAP3
    543. {
    544.     float4 Color0 : SV_Target0;
    545.     float4 Color1 : SV_Target1;
    546. };
    547. PIXELDATAP3 ShaderPixelP3(SHADERDATA IN)
    548. {
    549.     PIXELDATAP3 OUT;
    550.     OUT.Color0 = ShaderPixel(IN).Color;
    551.     OUT.Color1 = (OUT.Color0.w > 0.001f ? float4(1.0f, 1.0f, 1.0f, 1.0f) : float4(0.0f, 0.0f, 0.0f, 0.0f));
    552.     return OUT;
    553. }
    554.             ENDCG
    555.         }      
    556.     }
    557. }
     
    FeastSC2 likes this.