Skip to content

Commit b450133

Browse files
committed
Added support to metallic and AO map in lit shader. Also normal scale. Updated scenes.
1 parent 78d6874 commit b450133

File tree

9 files changed

+74
-92
lines changed

9 files changed

+74
-92
lines changed

Assets/_ExampleScenes/51_LitPhysicallyBased/CustomLit.shader

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
// splitting now as I've not implemented custom shader editor yet and
1313
// this will make it look nices in the UI
1414
_Metallic("Metallic", Range(0, 1)) = 1.0
15+
[NoScaleOffset]_MetallicSmoothnessMap("MetalicMap", 2D) = "white" {}
1516
_AmbientOcclusion("AmbientOcclusion", Range(0, 1)) = 1.0
17+
[NoScaleOffset]_AmbientOcclusionMap("AmbientOcclusionMap", 2D) = "white" {}
1618
_Reflectance("Reflectance for dieletrics", Range(0.0, 1.0)) = 0.5
1719
_Smoothness("Smoothness", Range(0.0, 1.0)) = 0.5
1820

1921
[Toggle(_NORMALMAP)] _EnableNormalMap("Enable Normal Map", Float) = 0.0
2022
[Normal][NoScaleOffset]_NormalMap("Normal Map", 2D) = "bump" {}
23+
_NormalMapScale("Normal Map Scale", Float) = 1.0
2124

2225
[Header(Emission)]
2326
[HDR]_Emission("Emission Color", Color) = (0,0,0,1)
@@ -43,6 +46,7 @@
4346
half _Reflectance;
4447
half _Smoothness;
4548
half4 _Emission;
49+
half _NormalMapScale;
4650
CBUFFER_END
4751
ENDHLSL
4852

@@ -74,26 +78,29 @@
7478
// Textures are declared in global scope
7579
TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap);
7680
TEXTURE2D(_NormalMap); SAMPLER(sampler_NormalMap);
77-
TEXTURE2D(_MaskMap);
81+
TEXTURE2D(_MetallicSmoothnessMap);
82+
TEXTURE2D(_AmbientOcclusionMap);
7883

7984
void SurfaceFunction(Varyings IN, out SurfaceData surfaceData)
8085
{
8186
surfaceData = (SurfaceData)0;
8287
float2 uv = TRANSFORM_TEX(IN.uv, _BaseMap);
8388

8489
half3 baseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv) * _BaseColor;
85-
half metallic = _Metallic;
86-
90+
half4 metallicSmoothness = SAMPLE_TEXTURE2D(_MetallicSmoothnessMap, sampler_BaseMap, uv);
91+
half metallic = _Metallic * metallicSmoothness.r;
8792
// diffuse color is black for metals and baseColor for dieletrics
8893
surfaceData.diffuse = ComputeDiffuseColor(baseColor.rgb, metallic);
8994

95+
// f0 is reflectance at normal incidence. we store f0 in baseColor for metals.
96+
// for dieletrics f0 is monochromatic and stored in reflectance value.
9097
// Remap reflectance to range [0, 1] - 0.5 maps to 4%, 1.0 maps to 16% (gemstone)
91-
// https://google.github.io/filament/Filament.html#materialsystem/parameterization/standardparameters
98+
// https://google.github.io/filament/Filament.html#materialsystem/parameterization/standardparameters
9299
surfaceData.reflectance = ComputeFresnel0(baseColor.rgb, metallic, _Reflectance * _Reflectance * 0.16);
93-
surfaceData.ao = _AmbientOcclusion;
94-
surfaceData.perceptualRoughness = 1.0 - _Smoothness;
100+
surfaceData.ao = SAMPLE_TEXTURE2D(_AmbientOcclusionMap, sampler_BaseMap, uv).g * _AmbientOcclusion;
101+
surfaceData.perceptualRoughness = 1.0 - (_Smoothness * metallicSmoothness.a);
95102
#ifdef _NORMALMAP
96-
surfaceData.normalWS = GetPerPixelNormal(TEXTURE2D_ARGS(_NormalMap, sampler_NormalMap), uv, IN.normalWS, IN.tangentWS);
103+
surfaceData.normalWS = GetPerPixelNormalScaled(TEXTURE2D_ARGS(_NormalMap, sampler_NormalMap), uv, IN.normalWS, IN.tangentWS, _NormalMapScale);
97104
#else
98105
surfaceData.normalWS = normalize(IN.normalWS);
99106
#endif

Assets/_ExampleScenes/51_LitPhysicallyBased/dieletricReflectance0.mat

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ Material:
1919
m_SavedProperties:
2020
serializedVersion: 3
2121
m_TexEnvs:
22+
- _AmbientOcclusionMap:
23+
m_Texture: {fileID: 0}
24+
m_Scale: {x: 1, y: 1}
25+
m_Offset: {x: 0, y: 0}
2226
- _BaseMap:
2327
m_Texture: {fileID: 0}
2428
m_Scale: {x: 10, y: 10}
@@ -27,6 +31,10 @@ Material:
2731
m_Texture: {fileID: 0}
2832
m_Scale: {x: 1, y: 1}
2933
m_Offset: {x: 0, y: 0}
34+
- _MetallicSmoothnessMap:
35+
m_Texture: {fileID: 0}
36+
m_Scale: {x: 1, y: 1}
37+
m_Offset: {x: 0, y: 0}
3038
- _NormalMap:
3139
m_Texture: {fileID: 0}
3240
m_Scale: {x: 0, y: 0}
@@ -37,12 +45,12 @@ Material:
3745
- _EnableMaskMap: 0
3846
- _EnableNormalMap: 0
3947
- _Metallic: 0
48+
- _NormalMapScale: 1
4049
- _Reflectance: 0
4150
- _Smoothness: 1
4251
m_Colors:
4352
- _BaseColor: {r: 0.25, g: 0.25, b: 0.75, a: 1}
4453
- _Emission: {r: 0, g: 0, b: 0, a: 1}
45-
m_BuildTextureStacks: []
4654
--- !u!114 &7017825111196047562
4755
MonoBehaviour:
4856
m_ObjectHideFlags: 11

Assets/_ExampleScenes/52_ClearCoat/CarPaint_DarkRedLit.mat

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Material:
2222
m_PrefabAsset: {fileID: 0}
2323
m_Name: CarPaint_DarkRedLit
2424
m_Shader: {fileID: 4800000, guid: 50211223aa9fe48bcb8e1dfe5cd83cc7, type: 3}
25-
m_ShaderKeywords:
25+
m_ShaderKeywords: _NORMALMAP
2626
m_LightmapFlags: 4
2727
m_EnableInstancingVariants: 0
2828
m_DoubleSidedGI: 0
@@ -44,6 +44,10 @@ Material:
4444
m_Texture: {fileID: 2800000, guid: ab941052b65bab5469d7376db72d99bd, type: 3}
4545
m_Scale: {x: 1, y: 1}
4646
m_Offset: {x: 0, y: 0}
47+
- _AmbientOcclusionMap:
48+
m_Texture: {fileID: 0}
49+
m_Scale: {x: 1, y: 1}
50+
m_Offset: {x: 0, y: 0}
4751
- _BaseMap:
4852
m_Texture: {fileID: 2800000, guid: d36c7fd843399dc48b8582c0821022c3, type: 3}
4953
m_Scale: {x: 20, y: 20}
@@ -68,10 +72,14 @@ Material:
6872
m_Texture: {fileID: 0}
6973
m_Scale: {x: 1, y: 1}
7074
m_Offset: {x: 0, y: 0}
71-
- _NormalMap:
75+
- _MetallicSmoothnessMap:
7276
m_Texture: {fileID: 0}
7377
m_Scale: {x: 1, y: 1}
7478
m_Offset: {x: 0, y: 0}
79+
- _NormalMap:
80+
m_Texture: {fileID: 2800000, guid: ab941052b65bab5469d7376db72d99bd, type: 3}
81+
m_Scale: {x: 1, y: 1}
82+
m_Offset: {x: 0, y: 0}
7583
- _OcclusionMap:
7684
m_Texture: {fileID: 0}
7785
m_Scale: {x: 1, y: 1}
@@ -100,12 +108,13 @@ Material:
100108
- _Cutoff: 0.5
101109
- _DstBlend: 0
102110
- _EnableClearCoat: 1
103-
- _EnableNormalMap: 0
111+
- _EnableNormalMap: 1
104112
- _EnvironmentReflections: 1
105113
- _GlossMapScale: 0
106114
- _Glossiness: 0
107115
- _GlossyReflections: 0
108116
- _Metallic: 0.777
117+
- _NormalMapScale: 0.03
109118
- _OcclusionStrength: 1
110119
- _QueueOffset: 0
111120
- _ReceiveShadows: 1

Assets/_ExampleScenes/52_ClearCoat/CarbonFiberLit.mat

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ Material:
88
m_PrefabInstance: {fileID: 0}
99
m_PrefabAsset: {fileID: 0}
1010
m_Name: CarbonFiberLit
11-
m_Shader: {fileID: 4800000, guid: 933532a4fcc9baf4fa0491de14d08ed7, type: 3}
11+
m_Shader: {fileID: 4800000, guid: 50211223aa9fe48bcb8e1dfe5cd83cc7, type: 3}
1212
m_ShaderKeywords: _ENVIRONMENTREFLECTIONS_OFF _METALLICSPECGLOSSMAP _NORMALMAP
1313
_OCCLUSIONMAP
1414
m_LightmapFlags: 4
1515
m_EnableInstancingVariants: 0
1616
m_DoubleSidedGI: 0
17-
m_CustomRenderQueue: 2050
18-
stringTagMap:
19-
RenderType: Opaque
17+
m_CustomRenderQueue: -1
18+
stringTagMap: {}
2019
disabledShaderPasses: []
2120
m_SavedProperties:
2221
serializedVersion: 3
@@ -33,9 +32,13 @@ Material:
3332
m_Texture: {fileID: 2800000, guid: 97a5e65a076f50d42963a284a1bc592c, type: 3}
3433
m_Scale: {x: 1, y: 1}
3534
m_Offset: {x: 0, y: 0}
35+
- _AmbientOcclusionMap:
36+
m_Texture: {fileID: 2800000, guid: 8157dd09913f891428066feaa94ccf12, type: 3}
37+
m_Scale: {x: 1, y: 1}
38+
m_Offset: {x: 0, y: 0}
3639
- _BaseMap:
3740
m_Texture: {fileID: 2800000, guid: 26d0b250f59e0f841ba5e17c1d3825ef, type: 3}
38-
m_Scale: {x: 15, y: 15}
41+
m_Scale: {x: 10, y: 10}
3942
m_Offset: {x: 0, y: 0}
4043
- _BumpMap:
4144
m_Texture: {fileID: 2800000, guid: 067474a1fe20636438146556fb01a657, type: 3}
@@ -57,6 +60,14 @@ Material:
5760
m_Texture: {fileID: 2800000, guid: f7d1bc0cc81c7e84e969040c53742d0a, type: 3}
5861
m_Scale: {x: 1, y: 1}
5962
m_Offset: {x: 0, y: 0}
63+
- _MetallicSmoothnessMap:
64+
m_Texture: {fileID: 2800000, guid: f7d1bc0cc81c7e84e969040c53742d0a, type: 3}
65+
m_Scale: {x: 1, y: 1}
66+
m_Offset: {x: 0, y: 0}
67+
- _NormalMap:
68+
m_Texture: {fileID: 2800000, guid: 067474a1fe20636438146556fb01a657, type: 3}
69+
m_Scale: {x: 1, y: 1}
70+
m_Offset: {x: 0, y: 0}
6071
- _OcclusionMap:
6172
m_Texture: {fileID: 2800000, guid: 8157dd09913f891428066feaa94ccf12, type: 3}
6273
m_Scale: {x: 1, y: 1}
@@ -75,6 +86,7 @@ Material:
7586
- Vector1_D4371E4A: 1
7687
- Vector1_D9C0A08D: 1
7788
- _AlphaClip: 0
89+
- _AmbientOcclusion: 1
7890
- _Blend: 0
7991
- _BumpScale: 1
8092
- _ClearCoat: 0.863
@@ -84,14 +96,17 @@ Material:
8496
- _Cutoff: 0.5
8597
- _DstBlend: 0
8698
- _EnableClearCoat: 1
99+
- _EnableNormalMap: 1
87100
- _EnvironmentReflections: 0
88101
- _GlossMapScale: 0
89102
- _Glossiness: 0
90103
- _GlossyReflections: 0
91-
- _Metallic: 0.262
104+
- _Metallic: 0
105+
- _NormalMapScale: 1
92106
- _OcclusionStrength: 1
93107
- _QueueOffset: 0
94108
- _ReceiveShadows: 1
109+
- _Reflectance: 0.5
95110
- _Smoothness: 0
96111
- _SmoothnessTextureChannel: 0
97112
- _SpecularHighlights: 1
@@ -103,8 +118,9 @@ Material:
103118
- Color_B11A0B8E: {r: 1, g: 1, b: 1, a: 0}
104119
- Vector2_161E53D1: {r: 20, g: 20, b: 0, a: 0}
105120
- Vector2_BD63EE59: {r: 0, g: 0, b: 0, a: 0}
106-
- _BaseColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
121+
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
107122
- _Color: {r: 0.5, g: 0.5, b: 0.5, a: 1}
123+
- _Emission: {r: 0, g: 0, b: 0, a: 1}
108124
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
109125
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
110126
--- !u!114 &8637944387010510288

Assets/_ExampleScenes/52_ClearCoat/CarbonFilterCoat.mat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Material:
2929
m_Offset: {x: 0, y: 0}
3030
- _BaseMap:
3131
m_Texture: {fileID: 2800000, guid: 26d0b250f59e0f841ba5e17c1d3825ef, type: 3}
32-
m_Scale: {x: 15, y: 15}
32+
m_Scale: {x: 10, y: 10}
3333
m_Offset: {x: 0, y: 0}
3434
- _MaskMap:
3535
m_Texture: {fileID: 0}
@@ -63,7 +63,7 @@ Material:
6363
- _Reflectance: 0.5
6464
- _Smoothness: 0
6565
m_Colors:
66-
- _BaseColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
66+
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
6767
- _Emission: {r: 0, g: 0, b: 0, a: 1}
6868
--- !u!114 &7017825111196047562
6969
MonoBehaviour:

Assets/_ExampleScenes/52_ClearCoat/FloatingMahoganyCoat_BronzeLit.mat

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Material:
99
m_PrefabAsset: {fileID: 0}
1010
m_Name: FloatingMahoganyCoat_BronzeLit
1111
m_Shader: {fileID: 4800000, guid: 50211223aa9fe48bcb8e1dfe5cd83cc7, type: 3}
12-
m_ShaderKeywords: _CLEARCOAT _OCCLUSIONMAP
12+
m_ShaderKeywords: _CLEARCOAT _NORMALMAP _OCCLUSIONMAP
1313
m_LightmapFlags: 4
1414
m_EnableInstancingVariants: 0
1515
m_DoubleSidedGI: 0
@@ -31,6 +31,10 @@ Material:
3131
m_Texture: {fileID: 2800000, guid: b6ab2245abdaa4a42ac45889aa9861a9, type: 3}
3232
m_Scale: {x: 1, y: 1}
3333
m_Offset: {x: 0, y: 0}
34+
- _AmbientOcclusionMap:
35+
m_Texture: {fileID: 2800000, guid: a70f1ac2e70003444917f97bdf5b3123, type: 3}
36+
m_Scale: {x: 1, y: 1}
37+
m_Offset: {x: 0, y: 0}
3438
- _BaseMap:
3539
m_Texture: {fileID: 2800000, guid: 6ce3faf7453e55d4988f09d34f954be6, type: 3}
3640
m_Scale: {x: 5, y: 5}
@@ -55,10 +59,14 @@ Material:
5559
m_Texture: {fileID: 2800000, guid: 140e74c1ce570fc4798f10081855ccc9, type: 3}
5660
m_Scale: {x: 1, y: 1}
5761
m_Offset: {x: 0, y: 0}
58-
- _NormalMap:
62+
- _MetallicSmoothnessMap:
5963
m_Texture: {fileID: 0}
6064
m_Scale: {x: 1, y: 1}
6165
m_Offset: {x: 0, y: 0}
66+
- _NormalMap:
67+
m_Texture: {fileID: 2800000, guid: 179dee3bd04bba945995dd57bb9c550b, type: 3}
68+
m_Scale: {x: 1, y: 1}
69+
m_Offset: {x: 0, y: 0}
6270
- _OcclusionMap:
6371
m_Texture: {fileID: 2800000, guid: a70f1ac2e70003444917f97bdf5b3123, type: 3}
6472
m_Scale: {x: 1, y: 1}
@@ -86,17 +94,18 @@ Material:
8694
- _Cutoff: 0.5
8795
- _DstBlend: 0
8896
- _EnableClearCoat: 1
89-
- _EnableNormalMap: 0
97+
- _EnableNormalMap: 1
9098
- _EnvironmentReflections: 1
9199
- _GlossMapScale: 0
92100
- _Glossiness: 0
93101
- _GlossyReflections: 0
94102
- _Metallic: 0
103+
- _NormalMapScale: 0.13
95104
- _OcclusionStrength: 1
96105
- _QueueOffset: 0
97106
- _ReceiveShadows: 1
98107
- _Reflectance: 0.5
99-
- _Smoothness: 1
108+
- _Smoothness: 0
100109
- _SmoothnessTextureChannel: 0
101110
- _SpecularHighlights: 1
102111
- _SrcBlend: 1

Assets/_ExampleScenes/52_ClearCoat/lit.mat

Lines changed: 0 additions & 59 deletions
This file was deleted.

Assets/_ExampleScenes/52_ClearCoat/lit.mat.meta

Lines changed: 0 additions & 8 deletions
This file was deleted.

Assets/_ExampleScenes/CustomShading.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ half3 EnvironmentBRDF(half3 f0, half roughness, half NdotV)
134134
{
135135
// 0.089 perceptual roughness is the min value we can represent in fp16
136136
// to avoid denorm/division by zero as we need to do 1 / (pow(perceptualRoughness, 4)) in GGX
137-
half perceptualRoughness = max(1.0 - surfaceData.perceptualRoughness, 0.089);
137+
half perceptualRoughness = max(surfaceData.perceptualRoughness, 0.089);
138138
half roughness = PerceptualRoughnessToRoughness(perceptualRoughness);
139139

140140
half3 environmentReflection = lightingData.environmentReflections;

0 commit comments

Comments
 (0)