Skip to content

Commit 937e9c1

Browse files
slunitysebastienlagarde
authored andcommitted
[10.x.x] Axf: add normal gradients and all mapping modes (uv, planar, triplanar) #583
1 parent 038e322 commit 937e9c1

File tree

16 files changed

+918
-143
lines changed

16 files changed

+918
-143
lines changed
Lines changed: 2 additions & 2 deletions
Loading
Loading
Lines changed: 3 additions & 0 deletions
Loading

com.unity.render-pipelines.high-definition/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
- Added a function (HDRenderPipeline.ResetRTHandleReferenceSize) to reset the reference size of RTHandle systems.
1111
- Added support for AxF measurements importing into texture resources tilings.
1212
- Added Layer parameter on Area Light to modify Layer of generated Emissive Mesh
13+
- Added support for multiple mapping modes in AxF.
1314

1415
### Fixed
1516
- Fixed issue with reflection probes in realtime time mode with OnEnable baking having wrong lighting with sky set to dynamic (case 1238047).

com.unity.render-pipelines.high-definition/Documentation~/AxF-Shader.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ Note: The AxF Importer imports every Texture as half float, linear, sRGB gamut (
5858

5959
| **Property** | **Description** |
6060
| --------------------- | ------------------------------------------------------------ |
61-
| **Main Tiling & Offset** | Sets the tiling rate (xy) and offsets (zw) for every Texture in the **Surface Inputs** section. HDRP uses these values to tile the Textures along the xy-axes of the Material’s surface, in the object's tangent space. Each texture property can also specify additional tiling and offset values that are applied on top of these main values (Texture property-specific tiling rates are multiplied and offsets are added to the main values set here) |
61+
| **Mapping Mode** | Controls the texture mapping mode of the material for all textures.<br/>&#8226; **UV0..UV3**: Like in Lit, uses a UV set from UV0 to UV3 vertex attributes. Note that UV1 is used for baked lightmaps in Unity, so it isn't recommended to use this set.<br/>&#8226; **PlanarXY,YZ,ZX**: Uses planar mapping along the specified plane.<br/>&#8226; **Triplanar**: Uses triplanar mapping.<br/>&#8226; **Planar Space**: When a planar or triplanar mapping mode is selected, you can select whether the coordinates used are world space or object space using the "planar space" option set to (respectively) "world" or "local". |
62+
| **Main Tiling & Offset** | Sets the tiling rate (xy) and offsets (zw) for every Texture in the **Surface Inputs** section. HDRP uses these values to tile the Textures along the xy-axes of the Material’s surface, in the object's tangent space. Each texture property can also specify additional tiling and offset values that are applied on top of these main values (Texture property-specific tiling rates are multiplied and offsets are added to the main values set here). These additional tiling and offsets appear next to each texture property on the same line. |
6263
| **BRDF Type** | Controls the main AxF Material representation.<br/>&#8226; **SVBRDF**: For information on the properties Unity makes visible when you select this option, see [BRDF Type - SVBRDF](#SVBRDF).<br/>&#8226;**CAR_PAINT**: For information on the properties Unity makes visible when you select this option, see [BRDF Type - CAR_PAINT](#CAR_PAINT). |
6364

6465
<a name="SVBRDF"></a>

com.unity.render-pipelines.high-definition/Editor/Material/AxF/AxFGUI.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ internal enum AxfBrdfType
1515
BTF,
1616
}
1717

18+
internal enum AxFMappingMode
19+
{
20+
UV0,
21+
UV1,
22+
UV2,
23+
UV3,
24+
PlanarXY,
25+
PlanarYZ,
26+
PlanarZX,
27+
Triplanar,
28+
}
29+
1830
/// <summary>
1931
/// GUI for HDRP AxF materials
2032
/// </summary>
@@ -50,6 +62,34 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
5062
const string kEnableGeometricSpecularAA = "_EnableGeometricSpecularAA";
5163
const string kSpecularOcclusionMode = "_SpecularOcclusionMode"; // match AdvancedOptionsUIBlock.kSpecularOcclusionMode : TODO move both to HDStringConstants.
5264

65+
const string kMappingMode = "_MappingMode";
66+
const string kMappingMask = "_MappingMask";
67+
const string kPlanarSpace = "_PlanarSpace";
68+
69+
static public Vector4 AxFMappingModeToMask(AxFMappingMode mappingMode)
70+
{
71+
Vector4 mask = Vector4.zero;
72+
if (mappingMode <= AxFMappingMode.UV3)
73+
{
74+
float X,Y,Z,W;
75+
X = (mappingMode == AxFMappingMode.UV0) ? 1.0f : 0.0f;
76+
Y = (mappingMode == AxFMappingMode.UV1) ? 1.0f : 0.0f;
77+
Z = (mappingMode == AxFMappingMode.UV2) ? 1.0f : 0.0f;
78+
W = (mappingMode == AxFMappingMode.UV3) ? 1.0f : 0.0f;
79+
mask = new Vector4(X, Y, Z, W);
80+
}
81+
else if (mappingMode < AxFMappingMode.Triplanar)
82+
{
83+
float X,Y,Z,W;
84+
X = (mappingMode == AxFMappingMode.PlanarYZ) ? 1.0f : 0.0f;
85+
Y = (mappingMode == AxFMappingMode.PlanarZX) ? 1.0f : 0.0f;
86+
Z = (mappingMode == AxFMappingMode.PlanarXY) ? 1.0f : 0.0f;
87+
W = 0.0f;
88+
mask = new Vector4(X, Y, Z, W);
89+
}
90+
return mask;
91+
}
92+
5393
// All Setup Keyword functions must be static. It allow to create script to automatically update the shaders with a script if code change
5494
static public void SetupMaterialKeywordsAndPass(Material material)
5595
{
@@ -62,6 +102,33 @@ static public void SetupMaterialKeywordsAndPass(Material material)
62102
CoreUtils.SetKeyword(material, "_AXF_BRDF_TYPE_CAR_PAINT", BRDFType == AxfBrdfType.CAR_PAINT);
63103
CoreUtils.SetKeyword(material, "_AXF_BRDF_TYPE_BTF", BRDFType == AxfBrdfType.BTF);
64104

105+
106+
// Mapping Modes:
107+
AxFMappingMode mappingMode = (AxFMappingMode)material.GetFloat(kMappingMode);
108+
109+
// Make sure the mask is synched:
110+
material.SetVector(kMappingMask, AxFMappingModeToMask(mappingMode));
111+
112+
bool mappingIsPlanar = (mappingMode >= AxFMappingMode.PlanarXY) && (mappingMode < AxFMappingMode.Triplanar);
113+
bool planarIsLocal = (material.GetFloat(kPlanarSpace) > 0.0f);
114+
115+
CoreUtils.SetKeyword(material, "_MAPPING_PLANAR", mappingIsPlanar);
116+
CoreUtils.SetKeyword(material, "_MAPPING_TRIPLANAR", mappingMode == AxFMappingMode.Triplanar);
117+
118+
if (mappingIsPlanar || mappingMode == AxFMappingMode.Triplanar)
119+
{
120+
CoreUtils.SetKeyword(material, "_PLANAR_LOCAL", planarIsLocal);
121+
}
122+
123+
// Note: for ShaderPass defines for vertmesh/varyingmesh setup, we still use the same
124+
// defines _REQUIRE_UV2 and _REQUIRE_UV3, and thus if eg _REQUIRE_UV3 is defined, _REQUIRE_UV2 will
125+
// be assumed to be needed. But here in the AxFData sampling code, we use these to indicate precisely
126+
// the single set used (if not using planar/triplanar) only and thus add _REQUIRE_UV1.
127+
// Extra UVs might be transfered but we only need and support a single set at a time for the whole material.
128+
CoreUtils.SetKeyword(material, "_REQUIRE_UV1", mappingMode == AxFMappingMode.UV1);
129+
CoreUtils.SetKeyword(material, "_REQUIRE_UV2", mappingMode == AxFMappingMode.UV2);
130+
CoreUtils.SetKeyword(material, "_REQUIRE_UV3", mappingMode == AxFMappingMode.UV3);
131+
65132
// Keywords for opt-out of decals and SSR:
66133
bool decalsEnabled = material.HasProperty(kEnableDecals) && material.GetFloat(kEnableDecals) > 0.0f;
67134
CoreUtils.SetKeyword(material, "_DISABLE_DECALS", decalsEnabled == false);

com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/AxfSurfaceInputsUIBlock.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class Styles
2020
{
2121
public const string header = "Surface Inputs";
2222

23+
public static GUIContent mappingModeText = new GUIContent("Mapping Mode");
24+
public static GUIContent planarSpaceText = new GUIContent("Planar Space");
25+
26+
public static GUIContent materialTilingOffsetText = new GUIContent("Main Tiling & Offset");
2327
/////////////////////////////////////////////////////////////////////////////////////////////////
2428
// SVBRDF Parameters
2529
public static GUIContent diffuseColorMapText = new GUIContent("Diffuse Color");
@@ -110,10 +114,20 @@ enum SvbrdfFresnelVariant
110114
SCHLICK, // Schlick's Approximation (1994)
111115
}
112116
static readonly string[] SvbrdfFresnelVariantNames = Enum.GetNames(typeof(SvbrdfFresnelVariant));
117+
static readonly string[] MappingModeNames = Enum.GetNames(typeof(AxFMappingMode));
113118

114119
/////////////////////////////////////////////////////////////////////////////////////////////////
115120
// Generic Parameters
116-
121+
122+
static string m_MappingModeText = "_MappingMode";
123+
MaterialProperty m_MappingMode = null;
124+
125+
static string m_MappingMaskText = "_MappingMask";
126+
MaterialProperty m_MappingMask = null;
127+
128+
static string m_PlanarSpaceText = "_PlanarSpace";
129+
MaterialProperty m_PlanarSpace = null;
130+
117131
MaterialProperty m_MaterialTilingOffset = null;
118132
MaterialProperty m_DiffuseColorMapST = null;
119133
MaterialProperty m_SpecularColorMapST = null;
@@ -234,6 +248,10 @@ public AxfSurfaceInputsUIBlock(Expandable expandableBit)
234248

235249
public override void LoadMaterialProperties()
236250
{
251+
m_MappingMode = FindProperty(m_MappingModeText);
252+
m_MappingMask = FindProperty(m_MappingMaskText);
253+
m_PlanarSpace = FindProperty(m_PlanarSpaceText);
254+
237255
m_MaterialTilingOffset = FindProperty(m_MaterialTilingOffsetText);
238256

239257
m_DiffuseColorMapST = FindProperty(m_DiffuseColorMapText + tilingOffsetPropNameSuffix);
@@ -344,7 +362,27 @@ public static void ExtractFlags(uint flags,
344362

345363
void DrawAxfSurfaceOptionsGUI()
346364
{
347-
materialEditor.ShaderProperty(m_MaterialTilingOffset, "Main Tiling & Offset");
365+
//materialEditor.ShaderProperty(m_MappingMode, Styles.mappingModeText);
366+
EditorGUI.BeginChangeCheck();
367+
float val = EditorGUILayout.Popup(Styles.mappingModeText, (int)m_MappingMode.floatValue, MappingModeNames);
368+
if (EditorGUI.EndChangeCheck())
369+
{
370+
Material material = materialEditor.target as Material;
371+
Undo.RecordObject(material, "Change Mapping Mode");
372+
m_MappingMode.floatValue = val;
373+
}
374+
375+
AxFMappingMode mappingMode = (AxFMappingMode)m_MappingMode.floatValue;
376+
m_MappingMask.vectorValue = AxFGUI.AxFMappingModeToMask(mappingMode);
377+
378+
if (mappingMode >= AxFMappingMode.PlanarXY)
379+
{
380+
++EditorGUI.indentLevel;
381+
materialEditor.ShaderProperty(m_PlanarSpace, Styles.planarSpaceText);
382+
--EditorGUI.indentLevel;
383+
}
384+
385+
materialEditor.ShaderProperty(m_MaterialTilingOffset, Styles.materialTilingOffsetText);
348386

349387
AxfBrdfType AxF_BRDFType = (AxfBrdfType)m_AxF_BRDFType.floatValue;
350388
AxF_BRDFType = (AxfBrdfType)EditorGUILayout.Popup("BRDF Type", (int)AxF_BRDFType, AxfBrdfTypeNames);

com.unity.render-pipelines.high-definition/Runtime/Material/AxF/AxF.cs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,35 @@ public struct SurfaceData
8080
public float anisotropyAngle;
8181

8282
// Car Paint Variables
83-
[SurfaceDataAttributes("Flakes UV")]
84-
public Vector2 flakesUV;
85-
86-
[SurfaceDataAttributes("Flakes Mip")]
87-
public float flakesMipLevel;
88-
83+
[SurfaceDataAttributes("Flakes UV (or PlanarZY)")]
84+
public Vector2 flakesUVZY;
85+
[SurfaceDataAttributes("Flakes PlanarXZ")]
86+
public Vector2 flakesUVXZ;
87+
[SurfaceDataAttributes("Flakes PlanarXY")]
88+
public Vector2 flakesUVXY;
89+
90+
[SurfaceDataAttributes("Flakes Mip (and for PlanarZY)")]
91+
public float flakesMipLevelZY;
92+
[SurfaceDataAttributes("Flakes Mip for PlanarXZ")]
93+
public float flakesMipLevelXZ;
94+
[SurfaceDataAttributes("Flakes Mip for PlanarXY")]
95+
public float flakesMipLevelXY;
96+
[SurfaceDataAttributes("Flakes Triplanar Weights")]
97+
public Vector3 flakesTriplanarWeights;
98+
99+
// if non null, we will prefer gradients (to be used statically only!)
100+
[SurfaceDataAttributes("Flakes ddx (and for PlanarZY)")]
101+
public Vector2 flakesDdxZY;
102+
[SurfaceDataAttributes("Flakes ddy (and for PlanarZY)")]
103+
public Vector2 flakesDdyZY;
104+
[SurfaceDataAttributes("Flakes ddx for PlanarXZ")]
105+
public Vector2 flakesDdxXZ;
106+
[SurfaceDataAttributes("Flakes ddy for PlanarXZ")]
107+
public Vector2 flakesDdyXZ;
108+
[SurfaceDataAttributes("Flakes ddx for PlanarXY")]
109+
public Vector2 flakesDdxXY;
110+
[SurfaceDataAttributes("Flakes ddy for PlanarXY")]
111+
public Vector2 flakesDdyXY;
89112
// BTF Variables
90113

91114
// Clearcoat
@@ -127,11 +150,19 @@ public struct BSDFData
127150
public float height_mm;
128151

129152
// Car Paint Variables
130-
[SurfaceDataAttributes("")]
131-
public Vector2 flakesUV;
132-
133-
[SurfaceDataAttributes("Flakes Mip")]
134-
public float flakesMipLevel;
153+
public Vector2 flakesUVZY;
154+
public Vector2 flakesUVXZ;
155+
public Vector2 flakesUVXY;
156+
public float flakesMipLevelZY;
157+
public float flakesMipLevelXZ;
158+
public float flakesMipLevelXY;
159+
public Vector3 flakesTriplanarWeights;
160+
public Vector2 flakesDdxZY; // if non null, we will prefer gradients (to be used statically only!)
161+
public Vector2 flakesDdyZY;
162+
public Vector2 flakesDdxXZ;
163+
public Vector2 flakesDdyXZ;
164+
public Vector2 flakesDdxXY;
165+
public Vector2 flakesDdyXY;
135166

136167
// BTF Variables
137168

0 commit comments

Comments
 (0)