From 11f3aab69805aa05fe01bb8a63e3e9b344fef10c Mon Sep 17 00:00:00 2001 From: Remi Chapelain Date: Thu, 16 Sep 2021 11:10:41 +0200 Subject: [PATCH 1/6] add section about editing shader at runtime --- .../Documentation~/TableOfContents.md | 1 + .../editing-shader-properties-at-runtime.md | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 com.unity.render-pipelines.high-definition/Documentation~/editing-shader-properties-at-runtime.md 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..4ab1e7475a0 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Documentation~/editing-shader-properties-at-runtime.md @@ -0,0 +1,11 @@ +# Editing shader properties at runtime + +For High Definition Render Pipeline (HDRP) non-Shader Graph shaders, such as the [Lit](Lit-Shader.md), [Unlit](Unlit-Shader.md) shaders, changing a property at runtime, in some cases, does not have any effect. This is because if the property is not enabled before runtime, the specific shader variant associated with this property is not included in the shader (and is also not included when bulding a player). To include the shader variant, the proper keywords need to be enabled on the material before editing. + +For example, to assign an emissive texture in an already blank emissive [Lit](Lit-Shader.md) shader at runtime, before setting the texture, this specific keyword needs to be [enabled](https://docs.unity3d.com/ScriptReference/Material.EnableKeyword.html) for the emissive color map variant to be included. + +``` +this.GetComponent().material.EnableKeyword("_EMISSIVE_COLOR_MAP"); +``` + +The list of the keywords can be found by right-clicking on the shader itself (in the material inspector header) and select "Edit Shader". From 683ec217a42c7bfeb567be0025e83406f7c68931 Mon Sep 17 00:00:00 2001 From: Remi Chapelain Date: Thu, 16 Sep 2021 11:30:59 +0200 Subject: [PATCH 2/6] remove double space --- .../Documentation~/editing-shader-properties-at-runtime.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 4ab1e7475a0..266f10f7d5b 100644 --- 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 @@ -1,6 +1,6 @@ # Editing shader properties at runtime -For High Definition Render Pipeline (HDRP) non-Shader Graph shaders, such as the [Lit](Lit-Shader.md), [Unlit](Unlit-Shader.md) shaders, changing a property at runtime, in some cases, does not have any effect. This is because if the property is not enabled before runtime, the specific shader variant associated with this property is not included in the shader (and is also not included when bulding a player). To include the shader variant, the proper keywords need to be enabled on the material before editing. +For High Definition Render Pipeline (HDRP) non-Shader Graph shaders, such as the [Lit](Lit-Shader.md), [Unlit](Unlit-Shader.md) shaders, changing a property at runtime, in some cases, does not have any effect. This is because if the property is not enabled before runtime, the specific shader variant associated with this property is not included in the shader (and is also not included when bulding a player). To include the shader variant, the proper keywords need to be enabled on the material before editing. For example, to assign an emissive texture in an already blank emissive [Lit](Lit-Shader.md) shader at runtime, before setting the texture, this specific keyword needs to be [enabled](https://docs.unity3d.com/ScriptReference/Material.EnableKeyword.html) for the emissive color map variant to be included. From 8fdf0a5b8fca47da8ca0e30939dd4934f7859e30 Mon Sep 17 00:00:00 2001 From: Remi Chapelain Date: Fri, 17 Sep 2021 18:06:32 +0200 Subject: [PATCH 3/6] Improved version --- .../editing-shader-properties-at-runtime.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) 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 index 266f10f7d5b..59e63b10f22 100644 --- 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 @@ -1,11 +1,19 @@ # Editing shader properties at runtime -For High Definition Render Pipeline (HDRP) non-Shader Graph shaders, such as the [Lit](Lit-Shader.md), [Unlit](Unlit-Shader.md) shaders, changing a property at runtime, in some cases, does not have any effect. This is because if the property is not enabled before runtime, the specific shader variant associated with this property is not included in the shader (and is also not included when bulding a player). To include the shader variant, the proper keywords need to be enabled on the material before editing. +Each combination of shader feature [Keyword](https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html) used in a shader produce 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 kewords used in a material. Until a specific shader feature is used on a material, the corresponding shader variant does not exist. -For example, to assign an emissive texture in an already blank emissive [Lit](Lit-Shader.md) shader at runtime, before setting the texture, this specific keyword needs to be [enabled](https://docs.unity3d.com/ScriptReference/Material.EnableKeyword.html) for the emissive color map variant to be included. +As a result, if one tries to edit a material at runtime and change properties that affects which keywords is enabled, Unity will not be able to find the shader variant and the rendering of this shader feature will fail. +This is because Unity cannot find the corresponding shader variant since the correct keywords are not enabled in the shader. + +In the editor, enabling the correct keywords will allow 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 will allow Unity to find the corresponding shader variant but only if it already exists. If not, the rendering of this shader feature will fail. + +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("_EMISSIVE_COLOR_MAP"); +this.GetComponent().material.EnableKeyword("_NORMALMAP"); ``` -The list of the keywords can be found by right-clicking on the shader itself (in the material inspector header) and select "Edit Shader". +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". From 43a9cf0359a45e05d4104e51eda038d00544c648 Mon Sep 17 00:00:00 2001 From: Remi Chapelain Date: Fri, 17 Sep 2021 18:10:29 +0200 Subject: [PATCH 4/6] remove future --- .../editing-shader-properties-at-runtime.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 index 59e63b10f22..6d3a2ffa106 100644 --- 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 @@ -1,12 +1,12 @@ # Editing shader properties at runtime -Each combination of shader feature [Keyword](https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html) used in a shader produce 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 kewords used in a material. Until a specific shader feature is used on a material, the corresponding shader variant does not exist. +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 kewords 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 will not be able to find the shader variant and the rendering of this shader feature will fail. +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 Unity cannot find the corresponding shader variant since the correct keywords are not enabled in the shader. -In the editor, enabling the correct keywords will allow 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 will allow Unity to find the corresponding shader variant but only if it already exists. If not, the rendering of this shader feature will fail. +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. From 75b41c81324030b1e68ca81c6e37efc4c25522dd Mon Sep 17 00:00:00 2001 From: Remi Chapelain Date: Fri, 17 Sep 2021 18:13:26 +0200 Subject: [PATCH 5/6] double info --- .../Documentation~/editing-shader-properties-at-runtime.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index 6d3a2ffa106..fd9ba82b262 100644 --- 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 @@ -3,9 +3,10 @@ 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 kewords 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 Unity cannot find the corresponding shader variant since the correct keywords are not enabled in the shader. +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. From c1a9d74ef6e4b641ec885cf5d85ccc13d68b1006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Chapelain?= <57442369+remi-chapelain@users.noreply.github.com> Date: Mon, 27 Sep 2021 14:45:56 +0200 Subject: [PATCH 6/6] Fixed typo --- .../Documentation~/editing-shader-properties-at-runtime.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index fd9ba82b262..118d0f82ad9 100644 --- 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 @@ -1,6 +1,6 @@ # 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 kewords used in a material. Until a specific shader feature is used on a material, the corresponding shader variant does not exist. +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.