Giter Club home page Giter Club logo

urp_shadergraphcustomlighting's Introduction

Shader Graph Custom Lighting

Some custom lighting functions/sub-graphs for Shader Graph, Universal Render Pipeline. If anything breaks, let me know by opening an issue!

+ This version is for URP v12+ (Unity 2021.2+) For use with older versions use the v8 branch!

Setup:

  • Install via Package Manager → Add package via git URL :
    • https://github.com/Cyanilux/URP_ShaderGraphCustomLighting.git
  • Alternatively, download and put the folder in your Assets

Includes Sub Graphs for :

Main Light

  • Main Light
    • Outputs : Direction, Colour, CullingMask
  • Main Light Shadows
    • Inputs : WorldPosition, Shadowmask (can leave at 1,1,1,1 if you don't need it)
    • Outputs : ShadowAtten
    • (Now works with all Shadow Cascades settings!)
  • Main Light Cookie- For supporting cookies. Will also enable cookies for additional lights (as they use the same keyword)
    • Inputs : WorldPosition
    • Outputs : Cookie
  • Main Light Layer Test - For supporting Light Layers. Pass your shading calculation from main light through here.
    • Inputs : Shading
    • Output : Out (with layer mask applied)
  • Main Light Diffuse (handles Lambert / NdotL calculation)
    • Inputs : Normal
    • Outputs : Diffuse
  • Main Light Specular Highlights - handles specular highlights based on Phong or BlinnPhong models
    • Inputs : Smoothness, Normal
    • Outputs : Specular

Other

  • Additional Lights - Loops through each additional light, point, spotlights, etc. Handles diffuse, specular and shadows. Supports Forward+ path (in 2022.2+)
    • Also supports cookies if Main Light Cookie node is used (or _LIGHT_COOKIES Boolean Keyword is defined in Blackboard)
    • Also supports light layers if Main Light Layer Test node is used (or _LIGHT_LAYERS Boolean Keyword is defined in Blackboard)
    • For creating custom lighting models, you'll need to copy this function and edit it due to the loop, e.g. swap the LightingLambert and LightingSpecular functions out for custom ones. Also see the AdditionalLightsToon function as an example
    • Inputs : SpecularColour, Smoothness, Normal, Shadowmask
    • Outputs : Diffuse, Specular
  • Sample Shadowmask - attach this to the Shadowmask port on the Main Light Shadows and Additional Lights sub graphs, in order to support Shadowmask baked lighting mode
    • Outputs : Shadowmask
  • Ambient SampleSH - uses per-pixel SampleSH, use add node to apply this. Can alternatively use the built-in Baked GI node)
    • Inputs : Normal
    • Outputs : Ambient
  • Subtractive GI - for supporting Subtractive baked lighting mode. Should connect Main Light Shadows node to first port. Uses MixRealtimeAndBakedGI function from URP ShaderLibrary
    • Inputs : ShadowAtten, Normal, BakedGI
    • Outputs : Out (Vector3)
  • Mix Fog - applies fog to the colour, should be used just before outputting to BaseColor on Master Stack
    • Inputs : Colour
    • Outputs : Out (Vector3, colour with fog applied)

Deprecated

  • Phong Specular and Blinn-Phong Specular subgraphs are now considered deprecated - use Main Light Specular Highlights instead.

Included Examples

  • Toon - Toon/Cel Shading. Main Light (ramp texture) & Additional Lights (number of bands). Also supports Main Light Cookies, BakedGI (including Subtractive & Shadowmask), Fog
  • Shadow Receiver - Transparent material that receives shadows and can set their colour. Can turn off casting via Mesh Renderer

urp_shadergraphcustomlighting's People

Contributors

cyanilux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

urp_shadergraphcustomlighting's Issues

Getting shader include errors with URP 13.1.8

Hey I'm getting these two errors:
Shader error in 'Shader Graphs/ShadowReceiver': Couldn't open include file 'Packages/com.cyanilux.shadergraph-customlighting/CustomLighting.hlsl'. at line 210
Shader error in 'Shader Graphs/ShadowReceiver': Couldn't open include file 'Packages/com.cyanilux.shadergraph-customlighting/CustomLighting.hlsl'. at line 877

Any ideas?

Invalid subscript 'ShadowCoord' compilation error for unlit graph in 2020 LTS/URP 10.8.1

Hi,

I get this error on shader compilation when using a 2020 LTS with custom lighting.

It looks like a relevant fix for this was made on the URP repo. It consists of :
https://github.com/Unity-Technologies/Graphics/pull/3551/files
where the fix was to add a check for defined(VARYINGS_NEED_SHADOW_COORD) in Varyings.hlsl:

#if defined(VARYINGS_NEED_SHADOW_COORD) && defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)

However, the URP version used on 2020 LTS still uses the old test:

#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)

so it requires a different workaround.

This repo has a test starting on line 38:

#ifndef SHADERGRAPH_PREVIEW
	#if VERSION_GREATER_EQUAL(9, 0)
		#include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/ShaderPass.hlsl"
		#if (SHADERPASS != SHADERPASS_FORWARD)
			#undef REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR
		#endif
	#else
		#ifndef SHADERPASS_FORWARD
			#undef REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR
		#endif
	#endif
#endif

which undefines the statement so that Varyings.hlsl does not try to reference shadowCoord.

In my case, this still resulted in errors in the console. The reason was because the custom lighting nodes (and related includes) were only included on shadergraph paths that calculated color. The shadowcaster pass does not calculate color, so it doesn't make the custom lighting include, does not make the undef and so still throws the errors. The fix for me was to wire a custom function to the alpha node so that it makes the include and then also correctly undefs the shadowcaster pass.

(Issue can be closed, I hope the info is useful to others)

Vertex Lit Support

Thank you for you work, it has helped me a lot exploring new ideas and learn about shaders without having to mess much with shader code!

There is a way to set every shader to vertex lit, but I wanted to make only some of them using this technique to have better performance, is there a way to make a vertex lit shader using your resources?

Additional light not working on Android build target

In a clean unity 2021.3.9f (LTS) install using URP 12.1.7, additional lights seem to be broken when setting the build target to Android.
Here's a screen shot with Windows as build target:

image

And here's the exact same scene, just with android as build target:
image

Details:

  • Using the Toon material included in the package.
  • No settings were changed to the default 3D (URP) template
  • Render pipeline set to the URP "HighFidelity" preset (has 8 per pixel lights set for additional light and most settings maxed out)

Thanks for your work - just thought I would report it for anyone battling with this problem. I don't know if this may be related to the light indexing problem described here: https://forum.unity.com/threads/additional-lighting-issue-when-switch-to-android-platform.1191736/
With a unity case (though it is marked as fixed): https://issuetracker.unity3d.com/issues/mobile-urp-all-lights-are-emitting-light-from-one-light-spot-when-the-platform-is-set-to-android-or-ios


Update: The problem doesn't seem to be in the light indexing, but rather in calculating the ToonAttenuation. Tried applying a standard lighting model for the additional lights, which worked as expected.


Anyone else not getting shadows in a fullscreen shader custom function?

The shadows work perfectly in a lit or unlit graph, or when using the subgraph in a fullscreen shader. However, if i make my own custom function that includes CustomLighting.hlsl in a fullscreen shader, MainLightShadows_float outputs 1 for everything, like there are no shadows.

Edit: After looking at other issues, I'm fairly certain this has to do with the boolean and enum keywords, namely _SHADOWS_SOFT and _MAIN_LIGHT

shadows not working

image
the teal is the original shader, the red my implementation (to rule out issues with the project of scene).
I implemented my toon shader, added the ramp and specular wihtout any issues but the shadow map and baked/subtractive GI dont seem effective : I have reproduced the shadergraph in a very similar subgraph below, am I missing something?

image

Shadow shaders break in URP 14.0.9

In the past these shaders worked well in all Unity versions. Right now 2022.3.15LTS breaks the shadow receiving shader.
It only happens with 3 or 4 cascades, so it probably is a keyword issue. It also happens with 2 cascades when conservative enclosing sphere is enabled

Soft shadows now also have enums for the quality tiers

image

VERSION_GREATER_EQUAL doesn't exist

Hi!

First off, thank you so much for this example repo! I've spent way too long searching for a way to make unlit shadows work and only finding outdated posts.

One thing I had to do to make your repo work, was adding one line to CustomLighting.hlsl to fix an error that said that VERSION_GREATER_EQUAL does not exist. The line in question is:

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Version.hlsl"

I'm on my MacBook Pro (Big Sur 11.5, Metal) on URP version 11.0.0. Not sure if this is necessary only for Mac or only for newer versions of URP though.

Cheers!

Baked Toon

Super awesome shader.

I have played around with different custom lighting models for URP for a while. I'm having trouble getting something that bake the light correctly and supports light probes.

I have tried using your toon shader example for baking, but it bakes the standard lighting model. Is it on purpose?

Baked
image

Realtime
image

I'm using 2021.3.10f1

Does not work / Examples are all pink

Is there something to configure befor use?
I imported it form asset store and all the examples were pink. Then i imported it from git with the same result.
Is there something to setup befor i can use this shader?
Is there a manual?

Additional light shadows not working 2020.2f1 and 2021.1.0b3

I pulled this repository into a fresh unity 2020.21f1 project and found that the realtime additional light shadows are not working for the example toon shader with spotlights. I tried the same for 2021.1.0b3 with spotlights and pointlights.
furthermore I tried:

  • enabling the keyword via script as outlined in your article
  • setting the boolean in the graph to true
  • checked if shadows are enabled in the RP asset

I am not sure what I am missing at this point, but please let me know if I overlooked or misunderstood anything

Question about MainLightShadows

Really cool package, I can't thank you enough for this ! I was wondering and fiddling with it and is there a way to actually remove the "self-shadowing" from the MainLightShadows. I've been trying for a good while now to get access to the unity shadow texture and the fact is while I'm compositing my lighting I've got issue that comes from trying to remove the ugly self shadowing that I'm getting with the node. It might not be possible maybe the actual shadow looks like this, but since I'm compositing my own self shadowing with the inverse DOT it kinda conflict with it. The projected shadow are so smooth and nice compared to what the self shadows are. :/ I'm a noob at HLSL could've probably figured it out in shaderlab, but now I'm a bit lost. Thanks in advance for your time!

debug mainlight shadow

Forward+

Adding support for forward+ would be awesome. It flickers now if forward+ is activated. Also would be neat if the point/spot lights listened to the ramp texture instead of creating bands.

Deferred Rendering?

Hello! This repo has been an invaluable resource for setting up custom lighting in URP. Thank you so much for putting this together!

I was wondering if you had any thoughts/leads on how I might extend it to work with deferred rendering now that URP supports it? I was picking through the URP code and the mechanics of reading/writing the GBuffer seem doable, but complicated. Have you looked at it at all, or have any idea where one might get started?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.