Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Seyed_Morteza_Kamaly · Aug 19, 2017 at 05:02 AM · unity 5shaderlighting

How can I add metalic and occlusion to my lighting model?

This is my custom lighting model I just want adding Occlusion and Metalic to it.I added them but don't work!!

 Shader "Skin/PreIntegratedSkinShaderSM2" {
         Properties {
             _Color ("Main Color", Color) = (1,1,1,1)
             _MainTex ("Diffuse Map(RGB)", 2D) = "white" {}
             _BumpMap ("Normal Map", 2D) = "bump" {}
             _Metalic("Metalic",Range(0,1)) = 0
             _Occlusion("Occlusion",2D) = "white"{}
     //        _SpecGlosDepthMap ("Specular (R) Glosiness(G) Depth (B)", 2D) = "white" {}
     //        _Bumpiness ("Bumpiness", Range(0,1)) = 0.9
     //        _SpecIntensity ("Specular Intensity", Range(0,100)) = 1.0
     //        _SpecRoughness ("Specular Roughness", Range(0.3,1)) = 0.7
     //        _LookupDiffuseSpec ("Lookup Map: Diffuse Falloff(RGB) Specular(A)", 2D) = "gray" {}
     //        _ScatteringOffset ("Scattering Boost", Range(0,1)) = 0.0
     //        _ScatteringPower ("Scattering Power", Range(0,2)) = 1.0
         }
         SubShader {
             Tags { "RenderType"="Opaque" }
             LOD 400
             
             CGPROGRAM
                 #pragma surface surf Skin nodirlightmap nolightmap  exclude_path:prepass
                 #pragma exclude_renderers flash
                 #pragma target 3.0
                 #pragma glsl
                 #include "PreIntegratedSkinShaderCommon.cginc"
             ENDCG
         }
         
         Fallback "VertexLit"
     }

this is my PreIntegratedSkinShaderCommon.cginc:

 #include "UnityCG.cginc"
             
 uniform sampler2D _MainTex;
 uniform sampler2D _BumpMap;
 uniform sampler2D _SpecGlosDepthMap;
 sampler2D _Occlusion;
 float _Metalic;
 
 uniform sampler2D _LookupDiffuseSpec;
 
 uniform float _ScatteringOffset;
 uniform float _ScatteringPower;
             
 #ifdef ENABLE_RIMS
 uniform float _BackRimStrength;
 uniform float _BackRimWidth;
 uniform float _FrontRimStrength;
 uniform float _FrontRimWidth;
 #endif
 
 uniform float _Bumpiness;
 
 uniform float _BumpinessDR;
 uniform float _BumpinessDG;
 uniform float _BumpinessDB;
 
 uniform float _SpecIntensity;
 uniform float _SpecRoughness;
 
 uniform fixed4 _Color;
 
 #ifdef ENABLE_TRANSLUCENCY
 uniform float _TranslucencyOffset;
 uniform float _TranslucencyPower;
 uniform float _TranslucencyRadius;
 #endif
 
 struct MySurfaceOutput {
     half3 Albedo;
     half Occlusion; //Don't work!!!!
 
     half3 Normal;
     #ifdef ENABLE_SEPARATE_DIFFUSE_NORMALS
     half3 NormalBlue;
     half3 NormalGreen;
     half3 NormalRed;
     #endif
     half3 Emission;
     half Specular;
     half Gloss;
     half Alpha;
     half Metalic; //Don't work!!!!
     half Scattering;
     #ifdef ENABLE_TRANSLUCENCY
     half3 Translucency;
     #endif
     #ifdef ENABLE_RIMS
     half BackRimWidth;
     half FrontRimWidth;
     #endif
 };     
 
 struct Input {
     float2 uv_MainTex;
 };
 
 inline fixed4 LightingSkin(MySurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten) {
     float NdotL = dot(s.Normal, lightDir); // light ramp
     #if defined(ENABLE_TRANSLUCENCY) || defined(ENABLE_RIMS)
     float NdotE = dot(s.Normal, viewDir); // faloff/rim
     #endif
     float3 h = lightDir + viewDir; // Unnormalized half-way vector  
     float3 H = normalize(h);
     float NdotH = dot(s.Normal, H);
     float EdotH = dot(viewDir, H);
     
     #ifdef ENABLE_SEPARATE_DIFFUSE_NORMALS
     half3 diffNdotL = 0.5 + 0.5 * half3(
         dot(s.NormalRed, lightDir),
         dot(s.NormalGreen, lightDir),
         dot(s.NormalBlue, lightDir));
     #else
     half diffNdotL = 0.5 + 0.5 * NdotL;
     #endif
         
     #ifdef DIRECTIONAL
         diffNdotL *= atten;
     #endif
     
     #ifdef ENABLE_SEPARATE_DIFFUSE_NORMALS
     half3 diff = 2.0 * half3(
         tex2D(_LookupDiffuseSpec, half2(diffNdotL.r, s.Scattering)).r,
         tex2D(_LookupDiffuseSpec, half2(diffNdotL.g, s.Scattering)).g,
         tex2D(_LookupDiffuseSpec, half2(diffNdotL.b, s.Scattering)).b
     );                
     #else
     half3 diff = 2.0 * tex2D(_LookupDiffuseSpec, half2(diffNdotL, s.Scattering)).rgb;
     #endif
             
     
     #ifndef DIRECTIONAL
         diff *= atten;
     #endif
     
     // specular
     float PH = pow( 2.0*tex2D(_LookupDiffuseSpec,float2(NdotH,s.Specular)).a, 10.0 );  
 
     float exponential = pow(1.0 - EdotH, 5.0);
     float fresnelReflectance = exponential + 0.028 * (1.0 - exponential);  
 
     float frSpec = max( PH * fresnelReflectance / dot( h, h ), 0 );  
     float specLevel = saturate(NdotL * s.Gloss * frSpec); // BRDF * dot(N,L) * rho_s
     // NB: specLevel saturated for correct rendering with HDR
 
     #ifdef ENABLE_TRANSLUCENCY
     half3 translucency = s.Translucency * saturate((1-NdotL)*dot(s.Normal, (viewDir-lightDir) * _TranslucencyRadius ));
     #endif
     
     half4 c = _LightColor0.rgba;
 
     #ifdef ENABLE_RIMS
     half frim = (pow(saturate((1-NdotE)*(NdotL)) * s.FrontRimWidth, 2)) * _FrontRimStrength;
     half brim = (pow(saturate((1-NdotE)*(1-NdotL) * s.BackRimWidth), 20)) * _BackRimStrength;
     #endif
     
     c.rgb *= 
         s.Albedo * (
             diff
             #ifdef ENABLE_TRANSLUCENCY
             + translucency * atten
             #endif
             #ifdef ENABLE_RIMS
             + (frim * atten).xxx
             #endif
         )
         + (specLevel * atten).xxx
         #ifdef ENABLE_RIMS
         + brim.xxx
         #endif
         ;
     
     return c;
 }
 
 void surf (Input IN, inout MySurfaceOutput o) {
     float2 uv = IN.uv_MainTex;
     
     #ifdef ENABLE_SEPARATE_DIFFUSE_NORMALS
     float3 normalHigh = UnpackNormal(tex2D(_BumpMap, uv));
     float3 normalLow = UnpackNormal(tex2Dbias(_BumpMap, float4(uv, 0, 3)));
     o.Occlusion = tex2D(_Occlusion,IN.uv_MainTex);
     o.Metalic = _Metalic;
     o.Normal = normalize(lerp(normalLow, normalHigh, _Bumpiness));
     o.NormalRed = normalize(lerp(normalLow, normalHigh, _BumpinessDR));
     o.NormalGreen = normalize(lerp(normalLow, normalHigh, _BumpinessDG));
     o.NormalBlue = normalize(lerp(normalLow, normalHigh, _BumpinessDB));
     #else
     #ifndef NOGLSL
     o.Normal = UnpackNormal(tex2Dbias(_BumpMap, float4(uv, 0, (1-_Bumpiness)*3)));
     #else
     // no GLSL support (most likely Intel igp)
     // fallback to smoothing to fully perpendicular normal instead of low res mipmap
     // this could result in differences if normal contains larger features, not just
     // small bumpy ones.
     // TODO could geet rid of normalize op by pecifying packed normal for various platforms (or is there a define i didn't find?)
 //    o.Normal = normalize(lerp(half3(0,0,1),UnpackNormal(tex2D(_BumpMap, uv)), _Bumpiness));
     // FIXME should normalize it, but there are no more instructions left!
     o.Normal = (lerp(half3(0,0,1),UnpackNormal(tex2D(_BumpMap, uv)), _Bumpiness));
     #endif
     #endif
     
     half3 specGlosDepth = tex2D(_SpecGlosDepthMap, uv).rgb;
     
     half depth = specGlosDepth.b;
     o.Scattering = saturate((depth + _ScatteringOffset) * _ScatteringPower);
                     
     half3 c = tex2D(_MainTex, uv).rgb;
 
     o.Albedo = c.rgb * _Color.rgb;
     o.Alpha = 1; // no transparency
 
     o.Specular = specGlosDepth.g * _SpecRoughness;
     o.Gloss = specGlosDepth.r * _SpecIntensity;
     
     #ifdef ENABLE_RIMS
     half rimSpread = 1 - depth*0.8;
     o.BackRimWidth = _BackRimWidth * rimSpread;
     o.FrontRimWidth = _FrontRimWidth * rimSpread;
     #endif
 
     #ifdef ENABLE_TRANSLUCENCY
     // Calculate the scale of the translucency effect.
     depth = 1-depth;
     depth = saturate((depth + _TranslucencyOffset) * 1);
     
     float scale = 8.25 * depth / _TranslucencyRadius;
     float d = scale * depth;
     half translucencyStrength = (1-depth) * _TranslucencyPower;
     
        
     // Could use a lookup map for this, but it's actually faster to compute on my GTX460
     // half3 translucencyProfile = tex2D(_LookupTranslucency, half2(d, 0)).rgb;
     
     float dd = -d * d;
     half3 translucencyProfile =
                      float3(0.233, 0.455, 0.649) * exp(dd / 0.0064) +
                      float3(0.1,   0.336, 0.344) * exp(dd / 0.0484) +
                      float3(0.118, 0.198, 0.0)   * exp(dd / 0.187)  +
                      float3(0.113, 0.007, 0.007) * exp(dd / 0.567)  +
                      float3(0.358, 0.004, 0.0)   * exp(dd / 1.99)   +
                      float3(0.078, 0.0,   0.0)   * exp(dd / 7.41);
                      
     o.Translucency = translucencyStrength * translucencyProfile;
     #endif
     o.Emission = 0;
 }





Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

180 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access all the lights in a scene in custom shader? 0 Answers

Shader? Light magnifying wall?? 1 Answer

How to edit this shader to use diffuse lighting (2D) 0 Answers

Reflections not working on IOS 2 Answers

How to use the Unity 5 Transparent Shader? 1 Answer


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