Implementing a custom lighting function or model gives you more control over the visual style and performance of your application.
For example, a simpler, cartoon style rendering can be significantly faster than realistic physically-based rendering, and can have a unique appealing look.
A good example of a custom lighting implementation is the cockpit scene in the URP 3D Sample project.
Unity provides several ways of implementing custom lighting using HLSL, shaderA program that runs on the GPU. More info
See in Glossary graph custom function nodes, or through URP source code modification.
Custom HLSL shaders provide you with a wide range of features and flexibility to implement custom lighting functions. You have full control over render passes, input and output variables, attributes, and internal shader functions.
Pre-built Shader Graph nodes let you use common shader functions, but provide few options for modifying their behavior. Shader Graph also provides custom function nodes that let you inject custom HLSL code inside a node.
You can find examples of custom function nodes in URP 3D Sample project, and in Shader Graph Feature Examples.
Custom function nodes let you insert your code into the vertex shaderA program that runs on each vertex of a 3D model when the model is being rendered. More info
See in Glossary function or the fragment shader function. These functions correspond to the Vertex and Fragment shader stages in Shader Graph, or to the functions vert(Attributes IN)
and frag(Varyings IN)
in an HLSL shader.
A custom function node does not provide the following functionality:
Change or add attributes in the input and output structures (often defined as struct Attributes
and struct Varyings
in shader code). For example, this means that you can’t add an extra custom option to the Fragment or Vertex shader stages.
Define extra shader passes.
Define extra render state commands such as ColorMask
.
If your shader requires any of the features above, consider writing a ShaderLabUnity’s language for defining the structure of Shader objects. More info
See in Glossary shader.
Techniques for implementing custom lighting vary depending on which types of lights your custom effects require.
To use the main light, use the GetMainLight
shader function. For more information, refer to Use lighting in a custom URP shader.
The Forward and Forward+ rendering paths handle extra directional lights and additional lights differently. The Forward+ rendering pathThe technique that a render pipeline uses to render graphics. Choosing a different rendering path affects how lighting and shading are calculated. Some rendering paths are more suited to different platforms and hardware than others. More info
See in Glossary doesn’t have a limit on real-time lights per GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary, so the GetAdditionalLightsCount
method always returns 0. For information on how to write a rendering loop that uses additional lights in Forward and Forward+ rendering paths, refer to Render additional lights in a shader in URP.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.