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
0
Question by Charles_Gams · Mar 19, 2014 at 12:04 AM · shaderlighting-model

Implement roughness parameter for cook torrance

Hi guys I've implemented a vertex lit cook torrance which works fine but I want to control the roughness or metalness of the surface with a roughness parameter rather than set values.

Heres my Shader code :

    Shader "Custom/13_CookTorrance_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 
 
      #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 float3 _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;                 
                 
         float3 HalfV = normalize(lightDirection + viewDirection);
         float NdotH =max(0.0,dot(input.normal,HalfV));
         
         float3 RoughnessParams = {0.5,0.5,0.5};
         
          
         float Alpha = acos(NdotH);
         float C = RoughnessParams.x;
         float m = RoughnessParams.y;
         float3 D = C*exp(-(pow(Alpha/m,2.0)));
         
         
         float NdotV = dot(normalDirection,viewDirection);
         float VdotH = dot(HalfV,viewDirection);
         float NdotL = dot(lightDirection,normalDirection);
         
         float3 G1 = 2*NdotH * NdotV /NdotH;
         float3 G2 = 2*NdotH * NdotL /NdotH;
         float3 G = min(1.0,max(0.0,min(G1,G2)));
         
         float R0 = RoughnessParams.z;
         float3 F = R0 +(1.0-R0) * pow(1.0 - NdotL,5.0);
         
         output.col = float4(_BaseColour * F *D*G/(NdotL *NdotV),1.0);
                      
       
 
         
        
         output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
         return output;
         
         
         
         
         
         
 
     
      }
 
      float4 frag(vertexOutput input) : COLOR
      {
         return input.col;
      }
 
      ENDCG
   }
 
 }
 
 }




alt text

An image of how it looks

Any Suggestions

cook_torrance.jpg (299.7 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Charles_Gams · Mar 19, 2014 at 03:02 AM

I figured out the problem myself something small aswell , before when i replaced the roughnessparams variables values with my Roughness parameter it didn't work because I had my Roughness parameter as a float3 but when I changed that to a float it worked fine.

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

20 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

Related Questions

Blend from lighting model to another 0 Answers

How to force the compilation of a shader in Unity? 5 Answers

Reflective sphere for HDRI 0 Answers

Multiple lighting models in one shader with mask 0 Answers

Multiple lighting models in one 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