Fast Subsurface scattering shader in Unity URP for mobile

Поділитися
Вставка
  • Опубліковано 2 жов 2024

КОМЕНТАРІ • 78

  • @EnriqueVenturaGames
    @EnriqueVenturaGames  2 роки тому +17

    For those using HDRP, since GetMainLight() is not available, you could store the relevant parameters of your light on a class that is then passed on to the shader:
    public class ShaderGlobals : MonoBehaviour
    {
    [SerializeField] Light mainLight;

    void Start()
    {
    Shader.SetGlobalVector("_MainLightDirection", mainLight.transform.forward);
    Shader.SetGlobalFloat("_MainLightAttenuation", mainLight.range);
    Shader.SetGlobalColor("_MainLightColor", mainLight.color);
    }
    }

    • @codeer4805
      @codeer4805 Рік тому +2

      how do you implement this? in the same Custom Function node? in a C# script? do you then call this from the node? how? (I sadly know nothing about coding a shader)

    • @xairo8134
      @xairo8134 6 місяців тому

      yeah a follow up on this would be really useful

  • @burakefe-dev
    @burakefe-dev 2 роки тому +40

    Thanks for sharing!
    There is the function for rushers and typo check:
    #ifdef SHADERGRAPH_PREVIEW
    MainLightDirection = float3(0.5,0.5,0);
    MainLightIntensity = 1.0f;
    #else
    MainLightDirection = GetMainLight().direction;
    MainLightIntensity = length(GetMainLight().color);
    #endif

  • @JonathanGilmer
    @JonathanGilmer 2 роки тому +4

    Thank you so much Enrique! I've been looking for a Sub Surface Scattering shader tutorial for Unity for a while now. All of the other ones I found were using Amplify, but I only have Shader Graph. So thank you again for the awesome tutorial!

  • @nadyawijaya5299
    @nadyawijaya5299 2 роки тому +3

    Hi I need help... it doesn't really work for me either. Wondering if its because im using 3d Core.
    here's the error:
    'Shader error in 'Shader Graphs/Head_ShaderGraph': 'GetMainLightDirection_float': output parameter 'MainLightDirection' not completely initialized at line 890 (on d3d11)'

  • @sagielevy
    @sagielevy 2 роки тому +5

    Thanks so much for this tutorial! One note I have is about the computation of light intensity with the "length" function. It should be "Luminance" instead, which is a dot product of a weighted average of the different light channels based on how bright they are in the human perception (i.e. blue and green are not equally "bright" even if they both have the same value)

  • @TheMadMuffin
    @TheMadMuffin 2 роки тому +5

    I followed this tutorial to the letter but the end result looks completely wrong when viewed from behind. The effect doesnt show at all when you look at it from the side the light is coming from, and from behind the light shows up 'behind' extremely dense portions. Can you share the shader file you made so I can see why mine doesn't work in comparison? To see where I am going wrong?

    • @paulbunyangonewild7596
      @paulbunyangonewild7596 2 роки тому

      Well I imagine you could multiply the effect by a black and white texture that represents the density of the area that's being seen.

  • @taguetrash
    @taguetrash Рік тому +2

    For anyone who tried to forego making a custom function and used the new builtin "Main Light Direction" node instead: It's backwards! You need to invert its direction to match the behaviour of this code!

  • @versio9
    @versio9 2 роки тому +2

    Has anyone tried using this shader for VR mobile dev (Quest 2, Pico)? I wonder how well the performance is for these platforms

  • @giorgosdev9738
    @giorgosdev9738 2 місяці тому

    * NOTE: for anyone encountering a problem that says "incorrect number of arguments to numeric-type constructor at line...", you need to write float3 instead of float for the vector that represents the directional light. So the final code looks like this :
    #ifdef SHADERGRAPH_PREVIEW
    MainLightDirection = float3(0.5, 0.5, 0);
    MainLightIntensity = 1.0f;
    #else
    MainLightDirection = GetMainLight().direction;
    MainLightIntensity = length(GetMainLight().color);
    #endif

  • @gorantrpkov446
    @gorantrpkov446 Рік тому +3

    Hey Enrique, I tried to replicate your process. Thank you for explaining the inner working and logic behind this effect, was very useful and straight forward. I find the effect looks more convincing or more like the HDRP subsurface scattering if I plug the SSS output in the master stack's emission channel rather adding it on top of the albedo. One thing that I find is not quite working for me is the product with the view angle, so that the effect appears when we view the model opposite of the light direction. I don't think this is right. The light should be scattering through the medium dependent on the light intensity and proximity no matter what angle it is viewed from. In real life you wouldn't lose the transmission or forward scattering propagation if you were to look at a human ear from the side. I tried to play around and fix this, but it's beyond my understanding, I just get weird scatter colors if I bypass the dot product. Could you let me know how to make the setup not a view dependent phenomenon? Thanks! I'm also kinda thinking about the blurring in detail textures you get with the HDRP SSS, which I suspect is a sort of a post process applied in screen space. Do you think if I was to apply the SSS effect, it would be enough to inversely lower the strength of the normal map, or some sort of a screen space blur would be the way to go? Would be cool if you have the time to go a bit more in depth with these ... if you have the time for it! Cheers!

    • @EnriqueVenturaGames
      @EnriqueVenturaGames  Рік тому +2

      Hi! Yeah, using the scattering as emissive instead of albedo sounds about right, it tends to be a bit brighter so the final effect is more aesthetically pleasing.
      However, view direction does actually matter! :D If you want to calculate scattering or reflectance accurately, you use a BSDF/BRDF (Bidirectional scattering/reflectance distribution function), which actually takes into account viewer angle to the source light. (Since light won't scatter equally on all directions, different viewers will see a different amount of light that went through the volume).
      For your other question, haven't done this myself but I would start with some tweaks to the normal map, since going from screen to object space is always a cumbersome process.

    • @gorantrpkov446
      @gorantrpkov446 Рік тому +1

      @@EnriqueVenturaGames Thanks for the clarification! I wouldn't argue that specular or reflection rays are dependent on viewing angle, refraction likewise 100%. But I'd like to push back on the scattering because it's a very diffuse phenomenon. Let's say we look at a 1cm thick membrane face on and shine a spot light on the back side of it. We expect to see a strong transmission that roughly and diffusely corresponds to the penumbra of the spotlight. If I change my viewing angle to more of a grazing angle (side on, but lets say 80 degrees instead formerly zero) I wouldn't expect this transmission to decrease one bit (assuming there's no specular layer on top to interfere) and this is I think because the SSS effect is so diffuse it pretty much acts like diffuse lighting in regards to the viewing angle. Again, sorry if I'm being stubborn but this is my intuition on the case

    • @valk1521
      @valk1521 11 місяців тому

      If anyone wants to get a result like Enrique is asking for where the subsurface scattering is not based on the camera view do this: take the main light direction, negate it, then plug it into where the view direction is plugged into. This will permanently apply the full effect as though you where looking at the object right behind the sun. I'm going to experiment with combining this result with a view based result as well to get an even more evolved effect.

    • @valk1521
      @valk1521 11 місяців тому

      I have just realised though that this also applies the sub surface scattering to areas in direct sunlight. You will need to mask the areas with direct sunlight out.

  • @mr.hashundredsofprivatepla3711
    @mr.hashundredsofprivatepla3711 6 місяців тому

    Hey Enrique, do you think this would be applicable in something like Godot?

  • @minedantaken1684
    @minedantaken1684 2 роки тому +1

    When you started with "hola amigos" I was like "shit, should've payed more attention during my spanish classes!"

  • @Владислав-н8е1ч
    @Владислав-н8е1ч Рік тому +2

    Nice voice, good explanation 💯

  • @dkxtro2562
    @dkxtro2562 2 роки тому +2

    You explained this extremely well! I was looking through Nvidia GPU gems articles and other online reference and it barely made sense.
    You absolutely nailed this! Too bad I'm not using on Unity but on SOURCE HAHAHHAA

  • @tufanaydin6340
    @tufanaydin6340 14 днів тому

    I loved your voice and telling

  • @gargantua3622
    @gargantua3622 4 місяці тому

    Hey, how much this shader can effect optimization? Im going to use this on tree leafs on my project. Should i check for gpu intancing in materials ? What would you suggest sir?

    • @EnriqueVenturaGames
      @EnriqueVenturaGames  2 місяці тому +1

      The best thing about this method is how performant it is. Depending on the visual style of your project there could be other possible approaches, but in most cases this should work fine. GPU instancing probably can help with performance, unless your leaves are dynamic objects (in which case I think that material instancing disables batching)

  • @priamgivord469
    @priamgivord469 Рік тому +1

    Hi Enrique, I think I am getting there, nice explanation, where can I get sample normal + ao textures, you accelerated the video at the end, all the best! P

    • @EnriqueVenturaGames
      @EnriqueVenturaGames  Рік тому

      I generated those using substance painter, but you can bake them in blender and other 3D editing tools

  • @itselliexoxx
    @itselliexoxx Рік тому

    Im new to unity and using the 2019 version (for VRchat) I cant seem to get the graph editor up, its not appearing under the assets tab

  • @garethlynch2823
    @garethlynch2823 2 роки тому +1

    What version of Unity are you using? I'm getting some weird results and I think perhaps it's the version I'm using

    • @EnriqueVenturaGames
      @EnriqueVenturaGames  2 роки тому

      I think I was using Unity 2021; sorry I cannot be more specific, this was done using my PC at the studio and it is no longer installed there

  • @disturbiossociais418
    @disturbiossociais418 Рік тому

    Great shader

  • @BillBlakesley
    @BillBlakesley 2 роки тому +1

    This tutorial is excellent. Can’t wait to try building it.

  • @Kirmm
    @Kirmm Рік тому

    I just don't understand why it's not integrated into unity by default.

  • @ralercats491
    @ralercats491 2 роки тому +2

    Thanks for this awesome tutorial!
    And where can I get this dragon model and thickness texture?

    • @EnriqueVenturaGames
      @EnriqueVenturaGames  2 роки тому +1

      Thanks! The Stanford Dragon (and the bunny) are available in a few plces (for example, clara.io/library?query=stanford&gameCheck=true). The thickness texture was baked by me using Substance Painter 3D, but like I said in the video, you can use a baked inverted AO map to get a similar result

  • @tatepayton7368
    @tatepayton7368 2 роки тому +1

    Doesn't work. I followed the tutorial to the letter and am only getting a pink texture as if there is no texture applied

    • @EnriqueVenturaGames
      @EnriqueVenturaGames  2 роки тому

      Can you post any other details? It is hard to tell where the problem is without any other info, sorry

    • @lijinlong9951
      @lijinlong9951 Рік тому

      @@EnriqueVenturaGames I am having this issue too. like my surface inputs I try put red colour for the base, the mesh is still pink color

  • @nathanthomas3844
    @nathanthomas3844 2 роки тому +1

    This may be a dumb question, but I have an asset that also has a normal map, could I use this shader with normals etc?

    • @EnriqueVenturaGames
      @EnriqueVenturaGames  2 роки тому

      No dumb question at all! Yeah it shouldn't have an effect, unless you want to.

    • @littlemountainanimation
      @littlemountainanimation 2 роки тому

      @@EnriqueVenturaGames thank you!! I’m a shader graph newbie - how do I add those attributes?

  • @MarleDFC
    @MarleDFC 2 роки тому

    Thank you Enrique. I used it for a Shift Pixy Labs project of a character. I am more of a Artist but I followed and through tribulation I got it to work and it looks awesome. Considering its for a mobile app it is awesome sauce.

  • @GreenGuru23
    @GreenGuru23 Рік тому

    Wother song in the background, mmm..
    so relax))

  • @zackblack2630
    @zackblack2630 2 роки тому +1

    I just tried re-creating this exactly as described in Unity 2021.3.6f1 and it just won't render properly. I have the AO map for the model exported correctly from Substance Painter. When I move the main light behind the object, it's just completely opaque. Any help, please?

  • @dylanhunter7238
    @dylanhunter7238 11 місяців тому

    Lol, amazing. Hi Enrique! This tutorial is super helpful for my current gig so thanks for that. =D

  • @lijinlong9951
    @lijinlong9951 Рік тому

    hello sir thank you for the tutorial. Why when I create the material from the shader, my surface inputs doesn't have anything ?

    • @lijinlong9951
      @lijinlong9951 Рік тому

      ok i manage to get the surface inputs out already. But now I am getting only pink textures

  • @circamagoo
    @circamagoo Рік тому

    looks beautiful! cant wait to give it a try

  • @psubst9054
    @psubst9054 Рік тому +1

    Thank you amigo!

  • @sanderl4779
    @sanderl4779 Рік тому

    Can this be used with a thickness map as well?

  • @hristoborisov3713
    @hristoborisov3713 2 роки тому

    hi, how would you add sss for point lights
    amazing tutorial otherwise!

  • @gerardoArF
    @gerardoArF 2 роки тому +1

    Gracias!

  • @codeer4805
    @codeer4805 Рік тому

    amazing work. thank you!

  • @omernomer7429
    @omernomer7429 Рік тому

    Hi there, If I wanted to use this shader with some PBR textures, how could I combine them so I could use the textures to sub surface scatter?

  • @rocateithmore3706
    @rocateithmore3706 Рік тому +1

    Hi, I am not sure where I went wrong but I have no output - meaning the preview is hot pink like it is missing a texture... And this doesn't change when I add textures in the inspector. I got the error message that "MainLightDirection is not completly initalized" my Unity Version is 2021.31f1 and I could use URP shader graph the scene has a directional light. thx for any help or suggestions

  • @ytblian
    @ytblian 2 роки тому +1

    Thanks for the tutorial! But I have three errors after following the video, do you know how to solve it?
    Shader error in 'Master': undeclared identifier 'GetMainLight' at line 1169 (on d3d11)
    Shader error in 'Master': undeclared identifier 'GetMainLight' at line 2094 (on d3d11)
    Shader error in 'Master': undeclared identifier 'GetMainLight' at line 3037 (on d3d11)

    • @EnriqueVenturaGames
      @EnriqueVenturaGames  2 роки тому +4

      Two possible causes:
      - If you are using a different rendering pipeline, GetMainLight might not work on it
      - A syntax error, like missing the parenthesis () after GetMainLight

  • @dropthall3001
    @dropthall3001 2 роки тому

    You are really cool! Thank you very much for this video!

  • @petermartin9394
    @petermartin9394 2 роки тому

    Really nice technique, cheers!

  • @lickysstrike7952
    @lickysstrike7952 Рік тому

    Nope, its too advanced for a bloody beginner 😅
    Is there a chance to purchace this shader on the Asset Store?

  • @HeyWhoTheFuckAreYou
    @HeyWhoTheFuckAreYou 2 роки тому +3

    Could you make a video on how to use Substance Scattering in HDRP? I tried to figure out how to import plants from Quixel Bridge, but didn't figure out how to properly set them up in HDRP.

    • @EnriqueVenturaGames
      @EnriqueVenturaGames  2 роки тому

      Should be similar, but I will have to take a look

    • @schala002
      @schala002 2 роки тому +2

      It is even easier in HDRP, since HDRP supports SSS out of the box, so you can easily add the subsurface mask, thickness, etc and other nodes.

    • @shuzhelan6676
      @shuzhelan6676 2 роки тому +1

      @@EnriqueVenturaGames I tried to make a subsurface scattering shader in HDRP, but there is a error “ Shader error in 'Master': undeclared identifier 'GetMianLight' ”. otherwise,I can not find the " GetMianLight() " in HDRP shader code library.

    • @EnriqueVenturaGames
      @EnriqueVenturaGames  2 роки тому +1

      @@shuzhelan6676 not sure if the small typo (GetMianLight() should be GetMainLight()) is in your code or just in the post.
      however, if you are using HDRP GetMainLight is not available; you need to create some custom variables to hold those values; for example:
      public class ShaderGlobals : MonoBehaviour
      {
      [SerializeField] Light mainLight;

      void Start()
      {
      Shader.SetGlobalVector("_MainLightDirection", mainLight.transform.forward);
      Shader.SetGlobalFloat("_MainLightAttenuation", mainLight.range);
      Shader.SetGlobalColor("_MainLightColor", mainLight.color);
      }
      }

    • @codeer4805
      @codeer4805 Рік тому

      @@EnriqueVenturaGames how do you implement this? in the same Custom Function node? in a C# script? do you then call this from the node? how? (I sadly know nothing about coding a shader)