- Home /
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!
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"
}
Your answer
Follow this Question
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