Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by JecoGames · Oct 29, 2013 at 11:00 AM · shadersurface shader

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.alt text

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;-)

oren-nayar.png (126.2 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jvo3dc · Dec 17, 2013 at 07:39 PM 0
Share

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

avatar image JecoGames · Jan 26, 2014 at 09:03 AM 0
Share

Yep I did;)Thanks by the way you really helped me understand the model.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 ;) alt text

 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"
 }
 

oren-nayar.png (438.9 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges