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 motorcycleboy · Nov 27, 2019 at 10:42 PM · shaderlightingmaterials

Surface Shader PBR

Hello,

I was wondering if anyone could provide a working example of the standard surface shader setup to use PBR materials?

I have figured out a few things so far, for example, to use a normal map I need to unpack it:

o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_mainTex));

But for some reason I can't get the metallic texture map to work...

Is there anything wrong with the following line of code?

o.Metallic = tex2D (_MettalicMap, IN.uv_MainTex).r;

Thanks in advance!

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

Answer by xibanya · Dec 24, 2019 at 09:59 AM

These are all the inputs of SurfaceOutputStandard

 struct SurfaceOutputStandard
 {
     fixed3 Albedo;      // base (diffuse or specular) color
     fixed3 Normal;      // tangent space normal, if written
     half3 Emission;
     half Metallic;      // 0=non-metal, 1=metal
     half Smoothness;    // 0=rough, 1=smooth
     half Occlusion;     // occlusion (default 1)
     fixed Alpha;        // alpha for transparencies
 };

Generally, the metallic gloss map will have the metallic value in the red channel and the smoothness value in the alpha channel, so you can do something like

 half4 glossmap = tex2D(_MetallicGlossMap, IN.uv_MainTex);
 o.Metallic = glossmap.r * _Metallic;
 o.Smoothness = glossmap.a * _Metallic;

o.Occlusion controls how much the object is affected by ambient light. If you have an occlusion map, generally the value is derived from the green channel like this:

 o.Occlusion = (1 - _OcclusionStrength) + tex2D(_OcclusionMap, IN.uv_MainTex).g * _OcclusionStrength;

a note on o.Alpha -- if your shader's RenderType is opaque, no matter what you write to this, it won't matter unless you put keepalpha into the parameters of the directive in which you declare the surface shader. For example:

#pragma Surface surf Standard keepalpha

oh, and since you requested a working example,

 Shader "Xibanya/Standard/SimpleStandard"
 {
     Properties
     {
         _Color ("Color", Color) = (1,1,1,1)
         _MainTex ("Albedo (RGB)", 2D) = "white" {}
         _BumpMap("Normal", 2D) = "bump" {}
         _BumpScale("Normal Strength", Float) = 1
         _MetallicGlossMap("Gloss Map", 2D) = "white" {}
         [Gamma]_Metallic("Metallic", Range(0, 1)) = 0.5
         _Glossiness("Smoothness", Range(0 ,1)) = 0.5
         _OcclusionMap("AO", 2D) = "white" {}
         _OcclusionStrength("Occlusion", Range(0, 1)) = 1
         [HDR]_EmissionColor("Emission Color", Color) = (0,0,0,1)
         _EmissionMap("Emission Tex", 2D) = "white" {}
     }
     SubShader
     {
         Tags { "RenderType" = "Opaque" }
         LOD 200
         CGPROGRAM
         #pragma surface surf Standard
         #pragma target 3.0
 
         sampler2D   _MainTex;
         sampler2D   _BumpMap;
         sampler2D   _MetallicGlossMap;
         sampler2D   _EmissionMap;
         sampler2D   _OcclusionMap;
 
         struct Input
         {
             float2 uv_MainTex;
         };
 
         half4       _Color;
         float       _BumpScale;
         half3       _EmissionColor;
         half        _Glossiness;
         half        _Metallic;
         half        _OcclusionStrength;
 
         void surf (Input IN, inout SurfaceOutputStandard o)
         {
             o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb * _Color.rgb;
             o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale);
             half4 glossmap = tex2D(_MetallicGlossMap, IN.uv_MainTex);
             o.Metallic = glossmap.r * _Metallic;
             o.Smoothness = glossmap.a * _Glossiness;
             o.Emission = _EmissionColor * tex2D(_EmissionMap, IN.uv_MainTex);
             o.Occlusion = (1 - _OcclusionStrength) + 
                 tex2D(_OcclusionMap, IN.uv_MainTex).g * 
                 _OcclusionStrength;
             o.Alpha = 1;
         }
         ENDCG
     }
     FallBack "Standard"
 }

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

214 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 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

unity2d light system doesnt work 2 Answers

How to edit a shader to make it show sprites it's on without lighting? 0 Answers

Unity 3D Vs 3DS Max Default Models Colors Variance 0 Answers

HDRP double sided light problem 0 Answers

a 'bar' or neon like light. 3 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