Match unity and substance designer

Hi there! When working inside Substance Designer, there is live model preview. It uses some cgfx shaders like blinn and e.t.c, but those never match shaders we have in Unity. While I can make diffuse and normal look the same, it fails on speculars. Unity just uses some different angle or something? It shines in front, while in SD somehow from bottom. And lighting setup is the same, identical. Is that possible to figure out what is wrong with specular angle, and match cgfx shader to fit unity’s bumped specular shader?

Like, that is the part of cgfx shader Substance Designer uses. There are some specular calculations involved, is there some way to find how Unity does it, how it calculates angle, and compare?

Here is Unity’s Blinn-Phong lighting function, from Lighting.cginc:

And this seems to be the blinn shader used in the Substance Designer’ 3D View (part of it - I did not past the whole code in here):

/*********************************************************************NVMH3****
$Revision$

Copyright NVIDIA Corporation 2007
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY
LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.


To learn more about shading, shaders, and to bounce ideas off other shader
    authors and users, visit the NVIDIA Shader Library Forums at:

    http://developer.nvidia.com/forums/

******************************************************************************/

[...]

//////// CONNECTOR DATA STRUCTURES ///////////

/* data from application vertex buffer */
struct appdata {
    float3 Position	: POSITION;
    float4 UV		: TEXCOORD0;
    float4 Normal	: NORMAL;
    float4 Tangent	: TANGENT0;
    float4 Binormal	: BINORMAL0;
};

/* data passed from vertex shader to pixel shader */
struct vertexOutput {
    float4 HPosition	: POSITION;
    float2 UV		: TEXCOORD0;
    // The following values are passed in "World" coordinates since
    //   it tends to be the most flexible and easy for handling
    //   reflections, sky lighting, and other "global" effects.
    float3 LightVec	: TEXCOORD1;
    float3 WorldNormal	: TEXCOORD2;
    float3 WorldTangent	: TEXCOORD3;
    float3 WorldBinormal : TEXCOORD4;
    float3 WorldView	: TEXCOORD5;
#ifdef USE_SHARED_SHADOW
    // This optional value expresses the current location in "light"
    //   coordinates for use with shadow mapping.
    float4 LProj	: LPROJ_COORD;
#endif /* USE_SHARED_SHADOW */
};
 
///////// VERTEX SHADING /////////////////////

/*********** Generic Vertex Shader ******/

vertexOutput std_VS(appdata IN) {
    vertexOutput OUT = (vertexOutput)0;
    OUT.WorldNormal = mul(WorldITXf,IN.Normal).xyz;
    OUT.WorldTangent = mul(WorldITXf,IN.Tangent).xyz;
    OUT.WorldBinormal = mul(WorldITXf,IN.Binormal).xyz;
    float4 Po = float4(IN.Position.xyz,1);
    float3 Pw = mul(WorldXf,Po).xyz;
    OUT.LightVec = (Lamp0Pos - Pw);
#ifdef FLIP_TEXTURE_Y
    OUT.UV = float2(IN.UV.x,(1.0-IN.UV.y));
#else /* !FLIP_TEXTURE_Y */
    OUT.UV = IN.UV.xy;
#endif /* !FLIP_TEXTURE_Y */
#ifdef USE_SHARED_SHADOW
    float4 Pl = mul(ShadowViewProjXf,Pw);  // "P" in light coords
    float4x4 BiasXf = make_bias_mat(ShadBias);
    OUT.LProj = mul(BiasXf,Pl);		// bias to make texcoord
#endif /* USE_SHARED_SHADOW */
    OUT.WorldView = normalize(float3(ViewIXf[0].w,ViewIXf[1].w,ViewIXf[2].w) - Pw);
    OUT.HPosition = mul(WvpXf,Po);
    return OUT;
}

///////// PIXEL SHADING //////////////////////

// Utility function for blinn shading

void blinn_shading(vertexOutput IN,
		    float3 LightColor,
		    float3 Nn,
		    float3 Ln,
		    float3 Vn,
		    out float3 DiffuseContrib,
		    out float3 SpecularContrib)
{
    float3 Hn = normalize(Vn + Ln);
    float hdn = dot(Hn,Nn);
    float3 R = reflect(-Ln,Nn);
    float rdv = dot(R,Vn);
    rdv = max(rdv,0.001);
    float ldn=dot(Ln,Nn);
    ldn = max(ldn,0.0);
    float ndv = dot(Nn,Vn);
    float hdv = dot(Hn,Vn);
    float eSq = Eccentricity*Eccentricity;
    float distrib = eSq / (rdv * rdv * (eSq - 1.0) + 1.0);
    distrib = distrib * distrib;
    float Gb = 2.0 * hdn * ndv / hdv;
    float Gc = 2.0 * hdn * ldn / hdv;
    float Ga = min(1.0,min(Gb,Gc));
    float fresnelHack = 1.0 - pow(ndv,5.0);
    hdn = distrib * Ga * fresnelHack / ndv;
    DiffuseContrib = ldn * LightColor;
    SpecularContrib = hdn * Ks * LightColor;
}

float4 std_PS(vertexOutput IN) : COLOR {

//return float4(IN.UV, 1, 1);

	//return float4(tex2D(Diffuse,IN.UV).rgb,0.f);
	//return float4(Lamp0Pos, 1.f);

    float3 diffContrib;
    float3 specContrib;
    float3 Ln = normalize(IN.LightVec);
    float3 Vn = normalize(IN.WorldView);
    float3 Nn = normalize(IN.WorldNormal);
    float3 Tn = normalize(IN.WorldTangent);
    float3 Bn = normalize(IN.WorldBinormal);
    float3 bump = Bump * (tex2D(Normal,IN.UV).rgb - float3(0.5,0.5,0.5));
#ifdef FLIP_NORMAL_Y
	bump.y = -bump.y;
#endif /* ifdef FLIP_NORMAL_Y */
    Nn = Nn + bump.x*Tn + bump.y*Bn;
    Nn = normalize(Nn);
	blinn_shading(IN,Lamp0Color,Nn,Ln,Vn,diffContrib,specContrib);
	diffContrib = saturate(diffContrib);
	specContrib = saturate(specContrib);
    float3 diffuseColor = tex2D(Diffuse,IN.UV).rgb;
#ifdef USE_SHARED_SHADOW
    float shadowed = tex2Dproj(DepthShadSampler,IN.LProj).x;
	float faded = 1.0-(ShadDens*(1.0-shadowed));
	diffContrib *= faded;
	specContrib *= shadowed;
#endif /* USE_SHARED_SHADOW */
    float3 result = specContrib+(diffuseColor*(diffContrib+AmbiColor));
    float3 R = reflect(Vn,Nn);
    float3 reflColor = Kr * texCUBE(Environment,R.xyz).bgr;
    result += reflColor; //diffuseColor*reflColor; //update
    return float4(result,1);
}

///// TECHNIQUES /////////////////////////////

technique Main <
	string Script = "Pass=p0;";
> {
    pass p0 <
	string Script = "Draw=geometry;";
    > {
        VertexProgram = compile arbvp1 std_VS();
		DepthTestEnable = true;
		DepthMask = true;
		CullFaceEnable = true;
		BlendEnable = false;
		DepthFunc = LEqual;
        FragmentProgram = compile arbfp1 std_PS();
    }
}

/////////////////////////////////////// eof //

You can switch/load shaders in 3D View with the help of the Shader menu. So you should be able to code an equivalent to Unity’s blinn-phong shading model.
The code above can be found in C:\Program Files\Allegorithmic\Substance\Designer\3.x\resources\view3d\shaders.

Thank you! I’ll try to reproduce Unity shader correctly using all the references

Heya,

Did you manage to do this?