Fade Objects with C# and the Standard URP Shaders | Unity Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 ม.ค. 2025

ความคิดเห็น • 98

  • @miguelbr8567
    @miguelbr8567 2 ปีที่แล้ว +4

    THANK YOU MAN, YOU SAVED MY PROJECT. I spent all along last day trying to figure out something, trying to compute shaders to do this, THANK YOU VERY MUCH

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว +1

      You’re very welcome! I’m glad it could save your project!

  • @poobagification
    @poobagification 2 ปีที่แล้ว +3

    Great video as always. Congrats to you and Andrew Bowen on that phenomenal support!

  • @eyadmohamed-vt7gl
    @eyadmohamed-vt7gl 11 หลายเดือนก่อน +1

    You are the Smartest man on the earth you solved the problem that i could not solve for 5 month
    Thank you very much

    • @LlamAcademy
      @LlamAcademy  11 หลายเดือนก่อน

      😅 I don’t know about that, but thank you! I’m glad it helped you!

  • @NatadTech
    @NatadTech 11 หลายเดือนก่อน +1

    Thank you so much bro for hosting it at github and not like some other who host it on their patreon

  • @renman3000
    @renman3000 ปีที่แล้ว +2

    Wow!
    Thanks. Crazy that such a simple task would be so complex!

    • @LlamAcademy
      @LlamAcademy  ปีที่แล้ว

      Yeah it was a little tricky to figure out 😅

    • @stylie473joker5
      @stylie473joker5 ปีที่แล้ว

      @@LlamAcademy Thanks for the hard work, just wanted to ask if this also works with details that are painted with unity's detail paint brush like trees and such

    • @LlamAcademy
      @LlamAcademy  ปีที่แล้ว +1

      I don’t think this will work out of the box with Unity Terrain with trees, grass, and other details placed.

  • @infernumex
    @infernumex 2 ปีที่แล้ว +2

    I can't pretend I completely understand this script, but I was able to tweak it for my game, so thank you!

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว +1

      Great! 🙌 I don't fully understand what all of the things we're toggling do either so 🤷‍♂️sometimes just have to just accept some magic :)

  • @ray691108
    @ray691108 2 ปีที่แล้ว +1

    Omg, you are awesome! super thanks for your example! you deserve more followers and attention indeed!

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว +1

      Thank you 🙏 I appreciate that

  • @Romored
    @Romored 11 หลายเดือนก่อน +1

    I rarely comment on videos but this time I will !
    Really nice video and awesome tutorial 🔥
    Have a nice day ! ✌

  • @dreamisover9813
    @dreamisover9813 2 ปีที่แล้ว +2

    This channel covers interesting topics, thx

  • @alejandrocabrera6089
    @alejandrocabrera6089 2 หลายเดือนก่อน

    Good video! Learned a lot! Out of topic, what computer case is the one you are using in the video? Cheers!

  • @tsingtaozhang9158
    @tsingtaozhang9158 2 ปีที่แล้ว +1

    Great work and I've learned a lot from your video! Unfortuantely when I tried using your code in my project, it appeared to have a bug when encountered with more than one object in the way. When there are 2 objects between player and camera, the fading process worked fine, but when the player walked out of the objects, only the object near the camera returned to opaque, and the other one (blocked by the object near the camera) stayed transparent. That's how I conclude the problem here. Bad at code, I can't fix the issue by myself. Guess this methond only works well in open area.
    But still, this method is fantastic!

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว +2

      Hmm……. I will check that out maybe I pushed some incorrect code after doing the editing on the video. Let me get back to you. The idea is this supports as many objects blocking view as you allow RaycastHit array size to be.

  • @dimitardimitrov7950
    @dimitardimitrov7950 8 หลายเดือนก่อน +1

    LoL, thanks for the tutorial, I followed it to the tee, it worked for me, no errors, but at one point the material wouldn't fade back in, I'm not a programmer, so I shamelessly copied your script from , git, thanks man, amazing tutorial :D

    • @dimitardimitrov7950
      @dimitardimitrov7950 8 หลายเดือนก่อน

      did change it to SphereCastNonAlloc with a radius of 1 for my game

    • @LlamAcademy
      @LlamAcademy  8 หลายเดือนก่อน

      That's why it's available :) Glad it helps you

  • @LuqmanHidayat00
    @LuqmanHidayat00 2 ปีที่แล้ว +1

    awesome, great tutorial!
    i can see myself using this in the future.
    i'm gonna save it to my playlist

  • @oldbelt2578
    @oldbelt2578 ปีที่แล้ว +1

    Thank you! this works perfectly!

  • @Equisdeification
    @Equisdeification 2 ปีที่แล้ว +1

    Hey! I love your channel, you are always giving excellent info

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว

      Awesome to hear! 🙏 I appreciate that

  • @chrisbee1984
    @chrisbee1984 7 หลายเดือนก่อน +1

    Thanks for doing this tutorial

    • @LlamAcademy
      @LlamAcademy  7 หลายเดือนก่อน

      Glad it could help you!

  • @rqbh
    @rqbh 10 หลายเดือนก่อน +1

    Not work anymore in Unity2022.3 , is there any other steps to do except add two scripts ?

    • @LlamAcademy
      @LlamAcademy  10 หลายเดือนก่อน

      The objects that can obstruct the camera need a reasonable collider or it won’t work! Do you have colliders on your objects?

    • @rqbh
      @rqbh 10 หลายเดือนก่อน

      ​@@LlamAcademy Yes, i use the same sense , the starter 3th person

  • @hamzakontaz6066
    @hamzakontaz6066 ปีที่แล้ว +1

    Thank you so much for this effort

  • @antlermind
    @antlermind ปีที่แล้ว +1

    Awesome tutorial here, what an amazing wealth of knowledge your channel has! That being said.. I am having some difficulties getting this working. Currently, there is one spot on my mesh that does turn transparent when blocking the player, but the rest of the mesh stays opaque. Is there any special magic that needs to be done within the URP shader to make this work correctly?

    • @LlamAcademy
      @LlamAcademy  ปีที่แล้ว

      Hi! Sorry for the delay in response, it's been a crazy few weeks for me lately.
      For your issue... I'm not sure how you could have that happen.
      Because the FadingObject script finds all child renderers and we iterate over all materials, I can't imagine a scenario where you could run into that if FadingObject just pulls all children. There's no magic material properties on the Standard Lit Shader in URP that would prevent it from fading out.
      Some ways I could imagine that would result in what you described would be:
      1. If there are overlapping objects, some without colliders, that are not parented under the object that has a collider + FadingObject. The secondary object would not be considered and not fade out
      2. If FadingObject has Renderers assigned in the inspector that are not the full set of Renderers to consider.
      If you can send a video of your scene setup and the issue maybe I could spot something funny? Or if you have a small reproducible repository I could check it out as well

  • @falricthesleeping9717
    @falricthesleeping9717 ปีที่แล้ว

    Hey nice video, at the first of the video you said you can use shaders, well where can I find any source for how to do it? thanks

  • @rqbh
    @rqbh 10 หลายเดือนก่อน +1

    Raycast can not detect object

  • @DioTravel
    @DioTravel 9 หลายเดือนก่อน

    This isn't working for me yet. I feel like I'm really close though.
    Nothing is showing up in the Objects Blocking View array. I have two objects with colliders and the Fading Object script.
    I thought it might be the Target Position Offset because I'm not seeing anything change when I change the X,Y,Z values. Either that or the Raycast is not hitting the objects.
    What would you recommend I do to debug this?

    • @LlamAcademy
      @LlamAcademy  9 หลายเดือนก่อน +1

      To debug this I like to make an editor script that draws the raycast for me with Handles.DrawLine() based on the direction and start position of the camera. Somehow a lot of times I mess up the raycast and it's not hitting what I expect. If you see the line going through all the objects, double check the layer mask you're raycasting on and the layers of the objects you expect to be hitting to see if there's something wonky going on there.

    • @DioTravel
      @DioTravel 9 หลายเดือนก่อน

      @@LlamAcademy Thanks so much, I got to work! Looks like it was the layers of the objects and the layermask that was making everything wonky!

  • @lawrence9713
    @lawrence9713 9 หลายเดือนก่อน

    Do you need a fix player for it? Could it be done in a game like Age of Empires/ City Skylines, just fading out objects because the cam is zoomed in or something

    • @LlamAcademy
      @LlamAcademy  9 หลายเดือนก่อน

      Works with any target

  • @mavor101
    @mavor101 10 วันที่ผ่านมา

    Would this also be possible with GPU instanced objects? It's working great with normal gameobjects I've spawned into my game but I can't seem to get the material modifications to work on GPU instanced objects.

    • @LlamAcademy
      @LlamAcademy  6 วันที่ผ่านมา

      Hmm…I think so but it would need some more work. You’d need to create a separate material to use for the specific object(s) that need to fade and manage those materials separately in/around where you’re doing the gpu instanced drawing

    • @mavor101
      @mavor101 6 วันที่ผ่านมา

      @@LlamAcademy Yes, I came to the same conclusion as you. For now I've gone the shader route and am using highlight/silhouetting in a custom render pipeline to show my target when behind a wall. Seems to accomplish the same goal and I don't have to deal with switching GPU instanced objects back and forth.

  • @jameslafritz2867
    @jameslafritz2867 ปีที่แล้ว

    Hey, Great Video. Is there a reason you chose to go this route instead of using the "Render Feature" of the Rendering->URP Render Pipeline, you may have a video on this already. I use 2 "Render Objects" One for when the character is behind an object and one for when the character is in front using the depth test. if a character is behind an object it use a dither material that I have set up to make it appear as the object is sim-transparent. But there is always the ability to create your own custom render feature that could fade the object out instead.

    • @LlamAcademy
      @LlamAcademy  ปีที่แล้ว +1

      Honestly, I haven't looked into Render Features. When I implemented this effect, URP and Render Features were not out yet. This video was a direct URP translation from what I implemented in the Built-In Render Pipeline.

  • @Garu2020
    @Garu2020 หลายเดือนก่อน

    Hey buddy thanks for sharing this kind of beautiful video really appreciate this. I need one help from you can you please make one urp shader using that I can clip the one object with another.

    • @LlamAcademy
      @LlamAcademy  หลายเดือนก่อน

      That's much more complex and unfortunately doesn't work with ShaderGraph and you have to dive into HLSL which has (in my opinion) a pretty steep learning curve.

  • @NeonNG
    @NeonNG 2 หลายเดือนก่อน

    I'm using this tutorial for my side scrolling platform game in 2022.3 URP. I have a realtime light for the whole scene. It works perfectly as it fades out and back in, but retain shadows doesn't seem to keep the shadows of the wall when faded out in the game view, but shadows are still there in the scene view? Any help on this?

    • @LlamAcademy
      @LlamAcademy  2 หลายเดือนก่อน

      Hmm...not sure on that one. Before applying this effect do you see the shadows in the game view? Only thought that comes to mind is maybe the URP settings are configured to not show this shadow.

    • @NeonNG
      @NeonNG 2 หลายเดือนก่อน

      @LlamAcademy Yeah it's what confuses me. Graphical settings are on highest as default so no loss in shadows.
      And yeah, in the scene view the shadows are there and remain there even when the wall fades out but in game view the shadows dissappear the instant the wall goes from opaque to transparent

  • @alexleonardkrea
    @alexleonardkrea 2 ปีที่แล้ว +1

    Very clever sir!

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว +1

      Glad you think so! Thanks!

  • @jmprsh153
    @jmprsh153 2 ปีที่แล้ว

    Hey so im guessing i have to import the universal render pipeline for this to work? I was wondering what i would need to do to just use the normal 'standard' shader thats with the normal one. Also if i have to use the universal render pipeline do i have to do anything specific with the project as i try the shader but it makes all the textures pink

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว

      Hi! Yes this is specifically for URP. In the description of the video I have a link to the video showing how to do it with the Built-in render pipeline. The process is the same, but I give you the specific properties and values over there.

  • @watercat1248
    @watercat1248 2 ปีที่แล้ว

    Wen I have create therd person player controller in past I have use one system that makes the camera to get closer wean the hit in collisions
    The problem with that system is the fact block even if the are something small like columns fences ECT but I think if someone make acception for the spefic object I will perfectly fine

  • @danielhasi6974
    @danielhasi6974 2 ปีที่แล้ว

    nice work, but its not possible if i using terrain and try to hide trees right?

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว +1

      That’s right. The terrain uses a different shader and does not use the standard rendering with a MeshRenderer

  • @maxg5196
    @maxg5196 2 ปีที่แล้ว

    I'm currently implementing this into my game. I got it working like a charm like in the video. However, I want it to check an list of targets, not just one player target. Currently I am doing the foreach loop for each target in the list in the CheckForObjects coroutine. However, it is still only working for one target at a time. Maybe you would have a better understanding of why that is and how to implement this?
    Thanks

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว

      If you have a static list of items that need to be checked, I think the simplest solution is to attach multiple of the same script with different targets. If it's dynamic, then I would expect you're doing it conceptually correct on the CheckForObjects. Could you send me a gist or something with the code? Maybe it's just a small tweak that needs to be made to what you already did.

    • @maxg5196
      @maxg5196 2 ปีที่แล้ว

      @@LlamAcademy yeah it'll be a dynamic list: I have a script on the targets that adds them to the list when they enter camera view and removes them when they exit camera.
      What would be the best way for me to share my code with you? Discord?

    • @maxg5196
      @maxg5196 2 ปีที่แล้ว

      Actually I believe I solved it myself.
      For other people's reference: essentially when doing the raycast check for each consecutive target, it overrides the array of the previous. And since the function which fades back in objects that are no longer being hit uses this Hits array, it only takes into consideration the last target checked.
      You need to save all the objects hit in a new array/list and use that in the FadeObjectsNoLongerBeingHit function

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว

      Oh! Great to hear. I like to use GitHub Gists to share individual scripts: gist.github.com/starred

  • @jjanor7745
    @jjanor7745 ปีที่แล้ว

    is there any way to fix the SSAO popping in and out? looks pretty terrible

    • @LlamAcademy
      @LlamAcademy  ปีที่แล้ว

      Hmm...I am not 100% sure you can achieve that very easily. Transparent objects usually are not included in the SSAO pass. Maybe you can tweak the settings of SSAO to go after opaque and change the render queue to something like Opaque + 1 to see if that works? Otherwise it might require custom implementation of SSAO to consider these objects.

    • @jjanor7745
      @jjanor7745 ปีที่แล้ว

      @@LlamAcademy Thank you for the answer! I'll see what I can do!

  • @phantomosteam3727
    @phantomosteam3727 2 ปีที่แล้ว

    Hello i have a different stile of my game that consist of cubes. I think this method is good for big objects instead but actually in my case only 2 cubes disappear and the rest remain opaque so i still can't see my player behind that cubes. Is there a method to give raycast more radius to area??

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว

      Yes! A Raycast is an “infinitely thin” line. You can use a boxcast or spherecast if you need a wider area. You can check out this video to see how all types of “casts” work: th-cam.com/video/fJyi7l2tWKo/w-d-xo.html

    • @phantomosteam3727
      @phantomosteam3727 2 ปีที่แล้ว

      @@LlamAcademy Thank you very much for answer! But in your case you are actually using RaycastNonAlloc and i don't know how to feet it exactly to your script because each Raycast have different settings allocation and i think using non alloc is better isn't that?

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว

      Yes, the NonAlloc is the most performant option since it doesn’t generate garbage. You can feed the BoxCastNonAlloc very similarly to RaycastNonAlloc. The video linked above has examples of how to feed each “cast” along with links to the documentation pages!

    • @phantomosteam3727
      @phantomosteam3727 2 ปีที่แล้ว

      @@LlamAcademy Yes i already watched these video before

    • @LlamAcademy
      @LlamAcademy  2 ปีที่แล้ว

      I will review this in the morning and get back to you

  • @steve-banbury-creative-xr
    @steve-banbury-creative-xr ปีที่แล้ว

    Thanks for putting this together - just what I need for a project I'm working on and I will be following it in detail. BUT... can I just ask... is it absolutely necessary to put music in the background, particularly on a repetitive loop? I find it hinders following the tutorial, which is kinda counter productive. I've had to remove my headphones and take a break so many times purely due to the background music. It will eventually drive me to distraction! Can we just have the education without the background elevator music, please? Otherwise, keep up the fantastic work!!

    • @LlamAcademy
      @LlamAcademy  ปีที่แล้ว +1

      Thanks for the feedback! When I go back in time and review my older videos I see a lot of room for improvement. One of those (many areas 😅) is the too loud background music that obviously loops. I hope in more recent videos you find the music choice and volume less distracting.

    • @steve-banbury-creative-xr
      @steve-banbury-creative-xr ปีที่แล้ว

      @@LlamAcademy Thanks for the response! Unfortunately, once you've focussed on the music it's really hard to just ignore it - I have tried :). The content of the tutorial was very helpful though. I only really wanted the bit about fading a 3D object with URP, but I found much more than that, which I am sure will be very useful in the future.

  • @michaelvicente5365
    @michaelvicente5365 ปีที่แล้ว +1

    looks promising, sadly I get an error "Assets\urp-fading-standard-shaders-main\Assets\StarterAssets\TutorialInfo\Scripts\Editor\ReadmeEditor.cs(79,23): error CS0115: 'ReadmeEditor.OnInspectorGUI()': no suitable method found to override"

    • @LlamAcademy
      @LlamAcademy  ปีที่แล้ว

      Hmm..... you can safely delete that file. It was just carried over from the Third Person Starter Assets from Unity.
      Which version of Unity are you on?

  • @calvinms7503
    @calvinms7503 ปีที่แล้ว +1

    thank you!

    • @LlamAcademy
      @LlamAcademy  ปีที่แล้ว

      You’re very welcome! ☺️

  • @qqqwwwwq
    @qqqwwwwq ปีที่แล้ว

    Hi, I used your tutorial but for HDRP with a shader graph. On the line:
    " private IEnumerator FadeObjectOut(FadingObject FadingObject)
    {
    foreach (Material material in FadingObject.Materials)
    {
    material.SetFloat("_Alpha", 0.6f); // Here I pass the value of the variable
    }
    yield return null;

    if (RunningCoroutines.ContainsKey(FadingObject))
    {
    StopCoroutine(RunningCoroutines[FadingObject]);
    RunningCoroutines.Remove(FadingObject);
    }
    } "
    I used my float variable. But I can't achieve a smooth fade out, because... "alphaFloat = Mathf.Lerp(alphaFloat, 0.6f, Time.deltaTime*1.1f);" triggers only in one frame. Tell me how to modify your code. Thank you!

    • @qqqwwwwq
      @qqqwwwwq ปีที่แล้ว

      Thank you, I figured it out myself))

    • @chuozt
      @chuozt 7 หลายเดือนก่อน

      Could you explain your solutions for this? I got the same issues.

    • @qqqwwwwq
      @qqqwwwwq 7 หลายเดือนก่อน

      @@chuozt I'll answer you a little later!

    • @qqqwwwwq
      @qqqwwwwq 7 หลายเดือนก่อน

      @@chuozt it was so long ago that I don’t remember what was there, here is the entire coroutine:
      private IEnumerator FadeObjectOut(FadingObject FadingObject)
      {
      float time = 0;
      while (FadingObject.Materials[0].color.a > FadedAlpha)
      {

      foreach (Material material in FadingObject.Materials)
      {

      if (material.HasProperty("_Color"))
      {
      material.SetFloat("_FloatAlph", 1f);
      AlphaStep = FadingObject.InitialAlpha;
      // AlphaFloat = Mathf.Lerp(FadingObject.InitialAlpha, 0.4f, time * FadeSpeed);
      for (int i = 0; i < 3; i++)
      {
      AlphaStep = AlphaStep - 0.2f;
      material.color = new Color(
      material.color.r,
      material.color.g,
      material.color.b,
      AlphaStep );
      yield return new WaitForSeconds(FadeSpeedOn);
      Debug.Log(AlphaStep);
      }
      }
      }
      time += Time.deltaTime;
      yield return null;
      }
      if (RunningCoroutines.ContainsKey(FadingObject))
      {
      StopCoroutine(RunningCoroutines[FadingObject]);
      RunningCoroutines.Remove(FadingObject);
      }
      }

    • @chuozt
      @chuozt 7 หลายเดือนก่อน

      @@qqqwwwwq Your objects' surface types are set to Transparent initially right?

  • @adamzadorski7764
    @adamzadorski7764 2 ปีที่แล้ว +1

    Great!

  • @brianparkhurst5352
    @brianparkhurst5352 6 หลายเดือนก่อน

    This works great, until I add a ridgedbody component to my object. I need objects to fade, but also need collision detection. I cant seem to figure out what I'm overlooking here.
    UPDATE:: My children object had the Fade Object component added to them which worked great, Until I added a ridged body component to the Empty parent object for box collision. To fix the issue I added the Fade Object to the parent object, and it worked again. Problem Solved!

    • @LlamAcademy
      @LlamAcademy  6 หลายเดือนก่อน

      Thank you for sharing your update 🧡! I'm sure someone else will run into the same issue and appreciate that

    • @brianparkhurst5352
      @brianparkhurst5352 6 หลายเดือนก่อน

      @@LlamAcademy So I did a build and run test on my project and the fade function doesn't work. Works great in the editor but fails to implement in the build. Any thoughts where to begin debugging this issue?

    • @LlamAcademy
      @LlamAcademy  6 หลายเดือนก่อน +1

      Sometimes in the build Unity will strip shader variants that are not used. That’s configured in your project settings / graphic settings. If you aren’t using any transparent materials sometimes this variant doesn’t get included in the build. You can add that variant as a preloaded shader to ensure it’s always included.

  • @cyco3oy
    @cyco3oy 2 ปีที่แล้ว +1

    +1