Hi I’ve implemented Wards Anisotropic Highlight based on an example found in the book :Advance lighting and Materials with Shaders By Kelly Dempski.
The code was written in HLSL and I simply made in work ( or was meant to work) in away for unity. Everything compiles no errors but I the highlights are not even visible or are not affected by any parameters what so ever .
Heres how it looks:
The code I typed up is :
Shader “Custom/12_Ward_A_VertexLit” {
Properties {
_BaseColour (“Base (RGB)”, Color) = (1,1,1,1)
_Subsurface (“SubSurface” , Range(0.0,1.0)) = 0.5
_Metallic (“Metallic”,Range(0.0,1.0)) = 0.5
_Specular (“Specular”,Range(0.0,1.0)) = 0.5
_SpecularTint (“Specular Tint”, Color) = (1,1,1,1)
_Roughness (“Roughness”, Range(0.0,1.0)) = 0.5
_Anisotropic (“Anisotropic”, Range(0.0,1.0))=0.5
_Sheen (“Sheen” ,Range(0.0,1.0)) = 0.5
_SheenTint (“Sheen Tint”, Color) = (1,1,1,1)
_Clearcoat (“Clearcoat” ,Range(0.0,1.0)) = 0.5
_ClearcoatGloss (“Clearcoat Gloss”,Range(0.0,1.0)) = 0.5
}
SubShader {
Pass {
Tags { “LightMode” = “Opaque” }
// pass for ambient light and first light source
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#include “UnityCG.cginc”
uniform float4 _LightColor0;
// color of light source (from “Lighting.cginc”)
// User-specified properties
uniform float4 _BaseColour;
uniform float4 _SpecularTint;
uniform float _Specular;
uniform float2 _Roughness;
struct vertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 col : COLOR;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
float4x4 modelMatrix = _Object2World;
float4x4 modelMatrixInverse = _World2Object;
// multiplication with unity_Scale.w is unnecessary
// because we normalize transformed vectors
float3 normalDirection = normalize(float3(
mul(float4(input.normal, 0.0), modelMatrixInverse)));
float3 viewDirection = normalize(float3(
float4(_WorldSpaceCameraPos, 1.0)
- mul(modelMatrix, input.vertex)));
float3 lightDirection;
lightDirection = normalize(float3(_WorldSpaceLightPos0));
float attenuation;
if (0.0 == _WorldSpaceLightPos0.w) // directional light?
{
attenuation = 1.0; // no attenuation
lightDirection =
normalize(float3(_WorldSpaceLightPos0));
}
else // point or spot light
{
float3 vertexToLightSource = float3(_WorldSpaceLightPos0
- mul(modelMatrix, input.vertex));
float distance = length(vertexToLightSource);
attenuation = 1.0 / distance; // linear attenuation
lightDirection = normalize(vertexToLightSource);
}
float3 ambientLighting =
float3(UNITY_LIGHTMODEL_AMBIENT) * float3(_BaseColour);
float3 diffuseReflection =
attenuation * float3(_LightColor0) * float3(_BaseColour)
- max(0.0, dot(normalDirection, lightDirection));
float3 specularReflection;
float3 HalfV = normalize(lightDirection + viewDirection);
float NdotH =max(0.0,dot(input.normal,HalfV));
float CosTheta = dot(input.normal,lightDirection);
float CosDelta = dot(input.normal,viewDirection);
float4 FirstTerm = 1.0/ sqrt(CosTheta *CosDelta);
float4 SecondTerm = 1.0/(12.56 *_Roughness.x * _Roughness.y);
float3 Direction = (0.0,0.0,1.0);
float3 X = normalize(cross(input.normal,Direction));
float3 Y = normalize(cross(input.normal,X));
float HdotX = dot(HalfV,X);
float HdotY = dot(HalfV,Y);
float HdotN = dot(HalfV,input.normal);
float LdotN = dot(lightDirection,viewDirection);
float A = -2.0 *(pow((HdotX / _Roughness.x),2)+ pow((HdotY / _Roughness.y),2));
float B = 1.0 + HdotN;
float4 ThirdTerm = exp(A/B);
float4 Irradiance = max(0.0,CosTheta);
if (dot(normalDirection, lightDirection) < 0.0)
// light source on the wrong side?
{
specularReflection = float3(0.0, 0.0, 0.0);
// no specular reflection
}
else // light source on the right side
{
specularReflection = attenuation * float3(_LightColor0)
- float3(_SpecularTint)*float3(FirstTerm)float3(SecondTerm) * float3(ThirdTerm) pow(max(0.0, dot(
reflect(-lightDirection, normalDirection),
NdotH)), _Specular);
}
output.col = float4(ambientLighting + diffuseReflection
- specularReflection,1.0) * Irradiance;
output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
return output;
}
float4 frag(vertexOutput input) : COLOR
{
return input.col;
}
ENDCG
}
}
}
You can copy and paste it and have more of a feel for what i mean .
But any suggestions?
