Skip to content

Instantly share code, notes, and snippets.

@tm8r

tm8r/phong.fx Secret

Created December 17, 2021 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tm8r/363b2b4cdd9329b935921c93a56c55e0 to your computer and use it in GitHub Desktop.
Save tm8r/363b2b4cdd9329b935921c93a56c55e0 to your computer and use it in GitHub Desktop.
dx11 phong example
// -----------Properties--------------
uniform float3 _LightDir0 : Direction
<
string Object = "Light 0";
string UIName = "Light 0 Direction";
string Space = "World";
>;
texture diffuseTexture
<
string name = "";
string UIName = "Diffuse";
string TextureType = "2D";
>;
uniform sampler2D _diffuseMap
<
string UIName = "Diffuse";
> = sampler_state{
Texture = <diffuseTexture>;
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
texture layerTexture
<
string name = "";
string UIName = "Layer";
string TextureType = "2D";
>;
uniform sampler2D layerMap
<
string UIName = "Layer";
> = sampler_state{
Texture = <layerTexture>;
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
// ---------------Math------------------
float4x4 WorldViewProjection : WorldViewProjection < string UIWidget = "None";>;
float4x4 WorldInverseTranspose : WorldInverseTranspose < string UIWidget = "None";>;
float4x4 World : World < string UIWidget = "None";>;
float4x4 ViewInverse : ViewInverse < string UIWidget = "None";>;
//-----------Structs--------------
struct app2vert {
float4 vertex : POSITION;
float2 texCoord : TEXCOORD0;
float4 normal : NORMAL;
};
struct vert2pixel {
float4 pos : POSITION;
float2 uvs : TEXCOORD0;
float3 viewDir : TEXCOORD1;
float4 normal : TEXCOORD2;
};
//-----------Vertex Shader--------------
vert2pixel vShader(app2vert IN)
{
vert2pixel OUT;
OUT.pos = mul(IN.vertex, WorldViewProjection);
OUT.uvs = float2(IN.texCoord.x, 1.0 - IN.texCoord.y);
float3 posWorld = mul(IN.vertex, World).xyz;
float3 _WorldSpaceCameraPos = float3(ViewInverse[0].w, ViewInverse[1].w, ViewInverse[2].w);
OUT.viewDir = normalize(ViewInverse[3].xyz - posWorld);
OUT.normal = mul(IN.normal, WorldInverseTranspose);
return OUT;
}
//-----------Pixel Shader--------------
float4 pShader (vert2pixel IN) : SV_Target
{
float4 outColor;
float4 diffuseColor = pow(tex2D(_diffuseMap, IN.uvs), 2.2);
float3 normalDir = normalize(mul(IN.normal, WorldInverseTranspose).xyz);
float3 lightDir = normalize(-_LightDir0.xyz);
float3 halfDir = normalize(lightDir.xyz + IN.viewDir);
float NoH = saturate(dot(normalDir, halfDir));
float LoH = saturate(dot(lightDir, halfDir));
float NoL = saturate(dot(normalDir, lightDir));
float3 reflectionVector = normalize(2.0 * IN.normal * NoL - lightDir);
float specular = pow(max(0, dot(reflectionVector, IN.viewDir)), 30);
float4 color = diffuseColor * NoL;
outColor = float4(color.xyz + specular, 0);
return outColor;
}
RasterizerState CullBack
{
FillMode = SOLID;
CullMode = Front;
FrontCounterClockWise = FALSE;
MultiSampleEnable = FALSE;
AntiAliasedLineEnable = FALSE;
};
//-----------Techniques--------------
technique11 Main
{
pass P0
{
SetRasterizerState(CullBack);
SetVertexShader(CompileShader(vs_5_0, vShader()));
SetPixelShader(CompileShader(ps_5_0, pShader()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment