diff --git a/com.unity.render-pipelines.high-definition/Documentation~/TableOfContents.md b/com.unity.render-pipelines.high-definition/Documentation~/TableOfContents.md index 84b1dc76be3..1bf24ed4393 100644 --- a/com.unity.render-pipelines.high-definition/Documentation~/TableOfContents.md +++ b/com.unity.render-pipelines.high-definition/Documentation~/TableOfContents.md @@ -211,6 +211,7 @@ * [Custom Material Inspector](hdrp-custom-material-inspector.md) * [Creating and Editing Lights at Runtime](creating-and-editing-lights-at-runtime.md) * [Creating a Decal Projector at Runtime](creating-a-decal-projector-at-runtime.md) + * [Editing Shader Properties at Runtime](editing-shader-properties-at-runtime.md) * [Adjusting Emissive Intensity at Runtime](adjusting-emissive-intensity-at-runtime.md) * [Editing Frame Settings at Runtime](Frame-Settings-API.md) * [Editing Volumes at Runtime](Volumes-API.md) diff --git a/com.unity.render-pipelines.high-definition/Documentation~/editing-shader-properties-at-runtime.md b/com.unity.render-pipelines.high-definition/Documentation~/editing-shader-properties-at-runtime.md new file mode 100644 index 00000000000..118d0f82ad9 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Documentation~/editing-shader-properties-at-runtime.md @@ -0,0 +1,20 @@ +# Editing shader properties at runtime + +Each combination of shader feature [Keyword](https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html) used in a shader produces a different shader code to compile. This is called a shader variant. Unity tries to reduce the number of shader variants to compile by only considering the sets of keywords used in a material. Until a specific shader feature is used on a material, the corresponding shader variant does not exist. + +As a result, if one tries to edit a material at runtime and change properties that affects which keywords is enabled, Unity is not able to find the shader variant and the rendering of this shader feature fails. +This is because the correct keywords are not enabled in the shader. + +In the editor, enabling the correct keywords allows Unity to compile the corresponding shader variant on the fly. + +When building a player, only the shader variants corresponding to the keywords used on the project materials are compiled. Enabling the correct keywords in a player allows Unity to find the corresponding shader variant but only if it already exists. If not, the rendering of this shader feature fails. + +For example, to assign a Normal Map texture in a blank [Lit](Lit-Shader.md) shader material at runtime in Editor, a specific keyword needs to be [enabled](https://docs.unity3d.com/ScriptReference/Material.EnableKeyword.html) for the normal map shader variant to be compiled on the fly. + +``` +this.GetComponent().material.EnableKeyword("_NORMALMAP"); +``` + +In a player, the keyword also needs to be enabled and the shader variant needs to exist because it cannot be compiled on the fly. + +For High Definition Render Pipeline (HDRP), the list of keywords can be found by right-clicking on the shader itself (in the material inspector header), selecting "Edit Shader" and looking for lines starting with "#pragma shader_feature_local".