From 90ba7c69a4aba1d36ac92eb0fc8e7a749de3f1a2 Mon Sep 17 00:00:00 2001 From: Lewis Jordan Date: Fri, 30 Apr 2021 15:48:51 +0100 Subject: [PATCH] Added new scripting examples --- .../Documentation~/TableOfContents.md | 5 +- ...adjusting-emissive-intensity-at-runtime.md | 19 +++++++ .../creating-a-decal-projector-at-runtime.md | 25 +++++++++ .../creating-and-editing-lights-at-runtime.md | 51 +++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 com.unity.render-pipelines.high-definition/Documentation~/adjusting-emissive-intensity-at-runtime.md create mode 100644 com.unity.render-pipelines.high-definition/Documentation~/creating-a-decal-projector-at-runtime.md create mode 100644 com.unity.render-pipelines.high-definition/Documentation~/creating-and-editing-lights-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 b5a12735d1e..dede5169d0f 100644 --- a/com.unity.render-pipelines.high-definition/Documentation~/TableOfContents.md +++ b/com.unity.render-pipelines.high-definition/Documentation~/TableOfContents.md @@ -66,7 +66,6 @@ * [Procedural Sky](Override-Procedural-Sky.md) * [Volumetric Clouds](Override-Volumetric-Clouds.md) * [Visual Environment](Override-Visual-Environment.md) - * [Volumes API](Volumes-API.md) * Render Pipeline Settings * [HDRP Asset](HDRP-Asset.md) * [Frame Settings](Frame-Settings.md) @@ -206,7 +205,11 @@ * [Scripting your own Custom Pass in C#](Custom-Pass-Scripting.md) * [Troubleshooting](Custom-Pass-Troubleshooting.md) * [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) + * [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) * [Render Graph](render-graph.md) * [HDRP Glossary](Glossary.md) * [Known Issues and How To Fix Them](Known-Issues.md) diff --git a/com.unity.render-pipelines.high-definition/Documentation~/adjusting-emissive-intensity-at-runtime.md b/com.unity.render-pipelines.high-definition/Documentation~/adjusting-emissive-intensity-at-runtime.md new file mode 100644 index 00000000000..81bacd9f95b --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Documentation~/adjusting-emissive-intensity-at-runtime.md @@ -0,0 +1,19 @@ +# Adjusting emissive intensity at runtime + +For High Definition Render Pipeline (HDRP) non-Shader Graph shaders, such as the [Lit](Lit-Shader.md), [Unlit](Unlit-Shader.md), and [Decal](Decal-Shader.md) shaders, changing the `_EmissiveIntensity` property does not have any effect at runtime. This is because `_EmissiveIntensity` is not an independent property. The shader only uses `_EmissiveIntensity` to serialize the property in the UI and stores the final result in the `_EmissiveColor` property. To edit the intensity of emissive materials, set the `_EmissiveColor` and multiply it by the intensity you want. + +``` +using UnityEngine; + +public class EditEmissiveIntensityExample : MonoBehaviour +{ + public GameObject m_EmissiveObject; + + void Start() + { + float emissiveIntensity = 10; + Color emissiveColor = Color.green; + m_EmissiveObject.GetComponent().material.SetColor("_EmissiveColor", emissiveColor * emissiveIntensity); + } +} +``` diff --git a/com.unity.render-pipelines.high-definition/Documentation~/creating-a-decal-projector-at-runtime.md b/com.unity.render-pipelines.high-definition/Documentation~/creating-a-decal-projector-at-runtime.md new file mode 100644 index 00000000000..143456d06d9 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Documentation~/creating-a-decal-projector-at-runtime.md @@ -0,0 +1,25 @@ +# Creating a Decal Projector at runtime + +The [Decal Projector](Decal-Projector.md) component enables you to project decals into an environment. When you create a Decal Projector at runtime, the workflow is slightly different to the workflow you usually use when instantiating [Prefabs](https://docs.unity3d.com/Manual/Prefabs.html). + +Instantiating a new DecalProjector from a Prefab does not automatically create a new instance of the DecalProjector's material. If you spawn multiple instances of the DecalProjector and make changes in one of its materials, the changes affect every DecalProjector instance. In the usual Prefab workflow, Unity creates a new instance of the material with the new Prefab instance. To match this usual workflow you must manually create a new material from the Prefab when you instantiate a new DecalProjector instance. For an example of how to do this, see the following code sample. + +``` +using UnityEngine; +using UnityEngine.Rendering.HighDefinition; + +public class CreateDecalProjectorExample : MonoBehaviour +{ + public GameObject m_DecalProjectorPrefab; + + void Start() + { + GameObject m_DecalProjectorObject = Instantiate(m_DecalProjectorPrefab); + DecalProjector m_DecalProjectorComponent = m_DecalProjectorObject.GetComponent(); + + // Creates a new material instance for the DecalProjector. + m_DecalProjectorComponent.material = new Material(m_DecalProjectorComponent.material); + + } +} +``` diff --git a/com.unity.render-pipelines.high-definition/Documentation~/creating-and-editing-lights-at-runtime.md b/com.unity.render-pipelines.high-definition/Documentation~/creating-and-editing-lights-at-runtime.md new file mode 100644 index 00000000000..bfcac6cfc7e --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Documentation~/creating-and-editing-lights-at-runtime.md @@ -0,0 +1,51 @@ +# Creating and editing lights at runtime + +The High Definition Render Pipeline (HDRP) extends Unity's [Light](https://docs.unity3d.com/Manual/class-Light.html) component with additional data and functionality. To do this, it adds the [HDAdditionalLightData](./api/UnityEngine.Rendering.HighDefinition.HDAdditionalLightData.html) component to the GameObject that the Light component is attached to. Because of this, you cannot create and edit Lights at runtime in the usual way. This document explains how to create an HDRP [Light](Light-Component.md) at runtime and how to edit its properties. + +## Creating a new light + +HDRP provides a utility function that adds a Light component to a GameObject, and sets up its dependencies. The function is `AddHDLight` and it takes an [HDLightTypeAndShape](./api/UnityEngine.Rendering.HighDefinition.HDLightTypeAndShape.html) as a parameter which sets the Light's type and shape. + +``` +using UnityEngine; +using UnityEngine.Rendering.HighDefinition; + +public class HDLightCreationExample : MonoBehaviour +{ + public GameObject m_Object; + void Start() + { + // Calls a Utility function to create an HDRP Light on a GameObject. + HDAdditionalLightData lightData = m_Object.AddHDLight(HDLightTypeAndShape.ConeSpot); + + // Sets property values for the Light. + lightData.intensity = 5; + lightData.range = 1.5f; + lightData.SetSpotAngle(60); + } +} + +``` + + +## Editing an existing Light + +HDRP does not use the data stored in the Light component. Instead it stores Light data in another component called `HDAdditionalLightData`. To access a property, use the `HDAdditionalLightData` component, even if the property is visible in the Light component Inspector. The following code sample changes the Light's intensity: + +``` +using UnityEngine; +using UnityEngine.Rendering.HighDefinition; + +public class HDLightEditingExample : MonoBehaviour +{ + public GameObject m_LightObject; + + void Start() + { + HDAdditionalLightData lightData; + lightData = m_LightObject.GetComponent(); + // The Light intensity is stored in HDAdditionalLightData. + lightData.intensity = 40f; + } +} +```