- Home /
Need help with Oren-Nayar lighting model.
Hi Im relatively new to shaders and I was playing around with implementing different lighting models such as Oren-Nayar with help from this website http://www.gamasutra.com/view/feature/131269/implementing_modular_hlsl_with_.php?page=3
I've finished the shader but it doesnt look quite right,it has a lot of artifacts heres a picture.
And heres the code
Shader "LearningShaders/PersonalTests/Oren-Nayar" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Roughness("Roughness",Range(0,1))=.5
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Oren_Nayar
#pragma target 3.0
float _Roughness;
inline float4 LightingOren_Nayar(SurfaceOutput s,half3 lightDir,half3 viewDir,half atten){
float roughness = _Roughness;
float VdotN = dot(viewDir,s.Normal);
float LdotN = dot(lightDir,s.Normal);
float cos_theta_i=LdotN;
float theta_r = acos(VdotN);
float theta_i = acos(cos_theta_i);
float cos_phi_diff = dot(normalize(viewDir-s.Normal*VdotN),normalize(lightDir-s.Normal*LdotN));
float alpha = max(theta_i,theta_r);
float beta = min(theta_i,theta_r);
float sigma2 = roughness*roughness;
float A = 1.0-0.5*sigma2/(sigma2+0.33);
float B =0.45 * sigma2/(sigma2+0.09);
if(cos_phi_diff>=0)
B*=sin(alpha)*tan(beta);
else
B*=0;
float4 col;
col.rgb =s.Albedo * _LightColor0.rgb*(cos_theta_i*(A+B)*atten);
col.a = s.Alpha;
return col;
}
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Thanks in advance;-)
Nice, did you get that code from my blog by any chance?
http://shaderjvo.blogspot.nl/2011/08/van-ouwerkerks-rewrite-of-oren-nayar.html
Yep I did;)Thanks by the way you really helped me understand the model.
Answer by jvo3dc · Jan 26, 2014 at 05:12 AM
The other code is I assume based on my work here. There I remove the sin and tan instructions in the original Qualitative Model in favor of a sqrt instruction. This optimization is mathematically the same as the original, but runs faster on typical GPU's.
The approximation metaleap references to is based on the full model that also includes secondary bounces. I just did a little test and I come to the following lighting model code for Unity:
half4 LightingOrenNayar(SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
//roughness A, Ap and B
half roughness = _Roughness;
half roughness2 = roughness * roughness;
half3 oren_nayar_fraction = roughness2 / (roughness2 + half3(0.33, 0.13, 0.09));
half3 oren_nayar = half3(1, 0, 0) + half3(-0.5, 0.17, 0.45) * oren_nayar_fraction;
//components
half cos_nl = saturate(dot(s.Normal, lightDir));
half cos_nv = saturate(dot(s.Normal, viewDir));
half oren_nayar_s = saturate(dot(lightDir, viewDir)) - cos_nl * cos_nv;
oren_nayar_s /= lerp(max(cos_nl, cos_nv), 1, step(oren_nayar_s, 0));
//composition
half3 result = s.Albedo * cos_nl * (oren_nayar.x + s.Albedo * oren_nayar.y + oren_nayar.z * oren_nayar_s);
result *= _LightColor0.rgb * atten;
return half4(result, s.Alpha);
}
The lerp/step combination should indeed be reversed. I changed that in an earlier post, but it got lost during sending. I've updated the code above.
Answer by JecoGames · Oct 31, 2013 at 01:51 PM
I figured it out myself looked around on the internet and found a better implementation heres a picture(on a more interesting model with some bump) and the code if anyone is interested ;)
Shader "LearningShaders/PersonalTests/Oren-NayarTest2" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_BumpTex("Bump",2D)="bump"{}
_NormalIntensity("Intensity",Range(0,2))=1
_Roughness("Roughness",float)=0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Oren_Nayar
#pragma target 3.0
float _Roughness;
inline float4 LightingOren_Nayar(SurfaceOutput s,half3 lightDir,half3 viewDir,half atten){
//roughness A and B
float roughness = _Roughness;
float roughness2=roughness*roughness;
float2 oren_nayar_fraction = roughness2/(roughness2 + float2(0.33,0.09));
float2 oren_nayar = float2(1, 0) + float2(-0.5, 0.45) * oren_nayar_fraction;
//Theta and phi
float2 cos_theta = saturate(float2(dot(s.Normal,lightDir),dot(s.Normal,viewDir)));
float2 cos_theta2 = cos_theta * cos_theta;
float sin_theta = sqrt((1-cos_theta2.x)*(1-cos_theta2.y));
float3 light_plane = normalize(lightDir - cos_theta.x*s.Normal);
float3 view_plane = normalize(viewDir - cos_theta.y*s.Normal);
float cos_phi = saturate(dot(light_plane, view_plane));
//composition
float diffuse_oren_nayar = cos_phi * sin_theta / max(cos_theta.x, cos_theta.y);
float diffuse = cos_theta.x * (oren_nayar.x + oren_nayar.y * diffuse_oren_nayar);
float4 col;
col.rgb =s.Albedo * _LightColor0.rgb*(diffuse*atten);
col.a = s.Alpha;
return col;
}
sampler2D _MainTex;
sampler2D _BumpTex;
float _NormalIntensity;
struct Input {
float2 uv_MainTex;
float2 uv_BumpTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
half3 normalMap= UnpackNormal(tex2D(_BumpTex,IN.uv_BumpTex));
normalMap = float3(normalMap.x * _NormalIntensity,normalMap.y * _NormalIntensity,normalMap.z);
o.Albedo = c.rgb;
o.Normal = normalMap.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613120840im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Modify Color in Surface Shader After Lighting (Forward Rendering) 0 Answers
Converting Fixed Function Shader to Surface Shader 2 Answers
Why doesn't a material shader render on procedural meshes? 1 Answer
Multipass Surface Shader with _CameraDepthNormalsTexture 0 Answers
Unlit terrain shader 0 Answers