Shader Graph and C# Interaction! Set Properties from Scripts! ✔️ 2020.3 | Unity Game Dev Tutorial

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

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

  • @GamesEngineer
    @GamesEngineer 4 ปีที่แล้ว +5

    Instead of copying the material, you could use a material property block. That has the benefit of being able to set the property values differently per renderer or mesh without having to create new materials for each one. However, it also has the cost of reducing batching. There are always tradeoffs.

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

      Hi, thanks for the insight, and you're right! I felt material property blocks deserved their own video, so I hope to cover them soon.

  • @benjaminborja5029
    @benjaminborja5029 3 ปีที่แล้ว

    BRO I WAS LITERALLY LIKE 6 HOURS TRYING TO FIGURE OUT WHY DOESN´T THE FLOAT ISN´T RETURNING ANYTHING AND IT WAS JUST BECAUSE OF THE REFERENCES, YOU SAVE ME I FUCKING LOVE YOU SO MUCH

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

      Glad to help out! Thanks for watching!

  • @ZacharyAghaizu
    @ZacharyAghaizu 3 ปีที่แล้ว

    Wow. Simple to follow and so powerful for my app development thanks!

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

    Thank You!
    This is excellently done. Clear. Concise. All the details.

    • @emiliotube01
      @emiliotube01 3 ปีที่แล้ว

      Does it work with URP?

  • @leemiller2243
    @leemiller2243 4 ปีที่แล้ว

    my man ned these tutorials are amazing

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

    That was awesome, very informative!

  • @Mad3011
    @Mad3011 4 ปีที่แล้ว

    Your videos are really great! Short and to the point.

  • @subhadas
    @subhadas 3 ปีที่แล้ว

    Thanks a lot I was wondering how to link between shaders and scripts. Plus the optimization tip at the end was also very helpful.

    • @NedMakesGames
      @NedMakesGames  3 ปีที่แล้ว

      Thank you, I’m glad to help out!

  • @paufenollosa
    @paufenollosa 3 ปีที่แล้ว

    3:29 -> Back in the scene E-DI-TOR
    He became a robot for a moment! Lol

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

    LOOKS AWESOME!!! Keep up the good work buddy, liked and subscribed!

  • @hibiscusbear
    @hibiscusbear 4 ปีที่แล้ว

    looks amazing! great job!!

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

    REMEMBER: In the Graph Insperctor, add your active target (URP, HDRP or Build-in). If it is not set correctly, you will have a pink material.

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

      OMG YOUR COMMENT SAVED MY LIFE!!! No wonder i was stuck! I will always remember you my man!

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

      @@khairulirfan9396 😘

  • @ThomasChen-ur2gt
    @ThomasChen-ur2gt 4 ปีที่แล้ว +2

    Your content is good enough to take over Brackeys' throne.

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

    Thank you for this useful video. We can do the same using animation. I found that doing this with animation is more stable and less complecated.

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

      Thanks for watching! Very true. This video was more about how you might pass values to a Shader Graph. The effect was mostly for demonstration.

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

    I love you for making this. ❤️

  • @mariovelez578
    @mariovelez578 3 ปีที่แล้ว

    Thanks dude, this really helped!

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

      Glad to hear it! Thanks for watching!

  • @Maestroshnitzel
    @Maestroshnitzel 4 ปีที่แล้ว

    Great tutorial!

  • @ludenusport
    @ludenusport 3 ปีที่แล้ว

    Thanks for video. I understood how interact with material from component, don't get it before.

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

      I'm glad to hear that! Thank you for watching!

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

    Hey not sure if I will explain myself correctly but if I understood this right, before working in a shader you need to understand what type of information you need to feed to it (kinda obvious right?) like for example, you knew you have to define the distance and you literally apply a physics concept of "Time . velocity = Distance" you already knew you needed that information. My question is... how can you tell what type of information you need to feed the shader? Is this something that comes up by practice or is there another type of documentation other than the Unity manual? And if it's just becose of practice and experience how do you come up with an effect from scratch without knowing what type of information you need to feed in the shader?
    Is this just a matter of playing with the options until you come up with something interesting, or these types of things are something calculated and planned ahead?

  • @laurencevanleuken5592
    @laurencevanleuken5592 4 ปีที่แล้ว

    Good video!
    What about a video that covers the subject of interfaces. You could have used an interface to safe some performance with the raycast by using an interface, RayAbleObject, with a function(in float3 hitPos) and then attaching it to the color changing ball script. Then you have a script to cast rays and call on every object that it hits the RayAbleObject function(of course the rayAbleObject attached to the change ball colour script). Therefor generelising the raycaster and broading the logic while saving computation(one ray for multiple balls instead of one per ball).

    • @NedMakesGames
      @NedMakesGames  4 ปีที่แล้ว

      Hi! Thank you!
      Thanks for the detailed suggestion! I think that’s a good idea for a video; I’ll add it to my list!

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

    I have Problem on that, in this color is auto filling without i am touching. what should i do help me.

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

      Hi! You need to set the start time to sometime far in the future to keep it from automatically filling when the scene first starts up.

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

      @@NedMakesGames I don't need to auto fill-up I only fill-up when I was click.

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

      @@argonprit7592 OK! In your C# script, set the start time to some time in the future. That way it shouldn't fill up until you reset the StartTime when clicking.

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

    i love you bro, thanks you

  • @fulongfromthegrave
    @fulongfromthegrave 4 ปีที่แล้ว

    Really cool intro project to shadergraph, tanks a lot!
    What about a shader that hides/reveals items for your next video? 🤷‍♂️😀

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

      Hi, thanks for watching!
      By "hide/reveal items", do you mean like a dissolve/gradient disappear effect, an x-ray vision effect, or something else? Either would be good suggestions!

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

    Thanks for this clear tutorial! Helps a lot! I’m curious, how would you make the ripple a square getting bigger around the mesh, instead of being a circle getting bigger from the starting point?

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

      Hi! I'm glad you enjoyed it!
      To make it a square, you need to change the way you test distance. Basically, subtract position from the click position and take the minimum of each component. Use that instead of the length or distance node.
      I'm not quite sure what you mean by getting bigger around the mesh? If you mean outside of the actual mesh object, that is more complicated. You would either have to resort to a screen-space/post-processing effect or bake the effect into to all materials in the scene. Definitely possible but you will have to do a little more work!

  • @소드마스터-j2r
    @소드마스터-j2r 4 ปีที่แล้ว

    looks beautiful

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

    I'm working on a horror game. I was thinking of using this to make a pool of water become red and then switch back to water, back and forth as an effect.

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

      Hi! Yes that should be possible here. The effect in this tutorial is more a demonstration of how to control shaders from C#. You can replace the pulsing colors with any effect you'd like.

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

    Ned, I have a question, is there a way that we can set a 3d meshes material color automatically depending on this terrain/surface color? For example if there is a toon ramp blend effect on the surface, i want the objects laying on the surface to change its color from dark, grey or white depending on its surface color which is created by the toon light effect

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

      Hi! This is actually pretty difficult. It's not easy to get data calculated in a shader back to the game's logic to be used by a script.
      I would recommend you retrieve the normal vector of the surface beneath your object (either with a raycast or reading mesh data). From there, you can calculating the lighting at that point and apply it to your object.

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

    Hi I was wondering how would you go about limiting the size of the ripple. for example, lets say you want it to stop the ripple once it covers 50% of the sphere?

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

      Hi! I haven't tested this, but you might be able to by inserting a max node between the Distance node and Divide node. Add a max radius property into the other node input.

  • @СергейИвченко-д2д
    @СергейИвченко-д2д 2 ปีที่แล้ว

    Hi! Thank You for clear and on point tutorial.
    Is There any drawbacks of getting instantiated material (“GetComponent().material”) instead of instantiating new material runtime?

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

      Hi! I'm glad you liked it!
      There's no real drawback, except that by instantiating a material, you create a new instance. So, if you edit that material, changes won't show in other meshes sharing the original. This might actually be advantageous in the case of this tutorial though!

    • @СергейИвченко-д2д
      @СергейИвченко-д2д 2 ปีที่แล้ว +1

      HI! Thanks for answer!
      Sorry, I, probably, didn’t asked a question correctly. I mean is there a difference between using renderer.material property and manually instantiating and assigning material.
      Did some research:
      If I understand right, there are two properties: renderer.sharedMaterial and renderer.material. When we get renderer.SharedMaterial (var something = renderer.sharedMaterial) and then edit it, we make permanent changes to this material.
      However, when we get renderer.material, as described in docs, it returns first instantiated material. In addition, it create new instance of material and make it unique to this renderer. Little confusing, but under the hood, I think, it something like that:
      private Material _material;
      public Material material
      {
      get
      {
      If(_materilal != null) return _material;
      _material = Instantiate(sharedMaterial);
      return _material;
      }
      set
      {
      _material = value;
      }
      }
      So, it seems, we can use renderer.material as shortcut to manually instantiating and assigning material to the renderer.

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

      @@СергейИвченко-д2д Hey, sorry for the late reply, this got withheld for moderation and I'm bad about checking that!
      Anyway, your code is pretty much exactly how it works, from my understanding. Instantiating new materials does disable certain batching techniques, so you should be careful about using it. But, it does make it easy to edit properties for a single game object, for example, and your changes will not persist after you exit play mode.

    • @СергейИвченко-д2д
      @СергейИвченко-д2д 2 ปีที่แล้ว

      @@NedMakesGames Thanks for reply!

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

    Thank you very much! :D
    I'm using HDRP and I'm having a delay of 2 seconds before the ripple starts. Any idea why is this?
    Edit: This improves with more RippleSpeed and just a bit of RippleSmoothing

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

      Hi! I’ve barely worked with HDRP, so I don’t know for sure. But the fact that increasing ripple speed helps leads me to think it might be an issue with the math in your graph. I can take a look at it if you’d like to post a screenshot!

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

      @@NedMakesGames I found the solution. In the position node, the space variable must be in Absolute World. At least in th HDRP.
      Thanks for all, incredible shader!

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

      @@paufenollosa Oh good! Thank you for watching!

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

    I've a shader with a variable called Fill. When I use material.SetFloat("_Fill",value) doesn't work I don't know why.
    But searching in the script generated by the Shader Graphs, that "Fill " variable is designed with "Vector1_52c8550397f046eebffbe6a5b78bfb19". So when I use material.SetFloat("Vector1_52c...blablabla",value) works.
    If I change this Vector1_52c8blablabla" to _Fill neither works. Do you know why? I tried folloing the video steps but I just can make it works this way.

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

      Oh yes, the "reference" (which by default is the property type plus a jumbled mess of characters) is how the property should be referred to by your scripts. Be sure to change it to something sensible if you ever plan to change a property from C#. The name in the shader graph is just cosmetic. Just make sure the reference field matches the string you pass to SetFloat.
      When you do this, does it still not work?

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

      @@NedMakesGames Hi, thanks for replying.
      Exactly, it still doesn't work. By default, the "Fill" value is "Vector1_52c8550397f046eebffbe6a5b78bfb19". And if I use material.SetFloat("Vector1_52c8550397f046eebffbe6a5b78bfb19",value) it works.
      But if I change the reference "Vector1_52c8550397f046eebffbe6a5b78bfb19" to "_Fill",it won't work. In fact, if I change the name of this reference, when calling "Vector1_52c8550397f046eebffbe6a5b78bfb19" it still works even when the reference name has been changed.

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

      @@HehasChinese Just to make sure, you are including the underscore when you call SetFloat?
      That sounds like a bug though! You can inspect the generated shader code of your shader graph by selecting the shader graph asset and clicking the option. From there, you can see what the property is really called. If SetFloat still doesn't work, then either there's a bug in Unity or something is overriding your value.

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

    Hello, is it possible to make the color change a gradient? So that it fades away from the point of contact?

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

      Hi! Yes, you will just need to adjust the math behind the blend "opacity." Instead of having it use smoothstep for an abrupt edge, you can just divide the distance by some value, giving more of a cone-like pattern.

  • @AtmosMr
    @AtmosMr 3 ปีที่แล้ว

    Hi, thank you so much. I'm stuck trying to change the properties of multiple materials. For instance in your example at the end you have lots of spheres all changing at the same time. I'm looking to do that but for a different project. Do I have to loop through all the materials? Is there a way to get all the objects share the same material (do I have to change this at runtime?) or can I set a Shader.SetGlobalFloat() - which doesn't seem to work for me yet. Is there a way to change multiple materials all using the same shader inside the Update()
    Confused and stuck!

    • @NedMakesGames
      @NedMakesGames  3 ปีที่แล้ว

      Hi! In that scene, I do loop through each material and set their properties. There's no way to set properties globally, but you can set a global variable.
      In the shader graph, they're basically the same thing. Go into your graph and uncheck the exposed option. Then, you can set the variable through Shader.SetGlobal*

    • @AtmosMr
      @AtmosMr 3 ปีที่แล้ว

      @@NedMakesGames oh right... That's interesting. I'll try that. Thank you!

    • @AtmosMr
      @AtmosMr 3 ปีที่แล้ว

      @@NedMakesGames Ah man. That works a treat. I suppose the 'exposed' property makes it a local to each instance of the material. Thanks man.

    • @NedMakesGames
      @NedMakesGames  3 ปีที่แล้ว

      @@AtmosMr I think that's exactly how it works! No problem!

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

    Can you make a shader tutorial that changes colour of the whole world from the point?

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

      Hi! Thanks for watching!
      That would be interesting, implemented in a post processing effect? You could kind of make that effect now by applying this shader to all materials in the scene.

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

      @@NedMakesGames Thank you so much for the quick response 😇
      I have a question, won't applying the shader to all materials still need clicking them individually?
      I am searching for an effect that is shown in this video : th-cam.com/video/UhaXcolcFfM/w-d-xo.html.
      There are multiple, like the one where the whole world changes from a grayscale look to being colours and a small area coloured when the rest of the world is grayscale.
      I have been searching for days now, I think my search terms are wrong. 😭

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

      @@ShinichiKudoQatnip You would need to modify the C# script to set the location on all materials whenever any one of them is clicked!
      That's a cool effect though! Maybe search for world space area effects? I'm not sure if this technique has a formal term!

  • @michaelwalker7706
    @michaelwalker7706 3 ปีที่แล้ว

    strange, I have reached 5:02 and nothing happens when i click the sphere

    • @michaelwalker7706
      @michaelwalker7706 3 ปีที่แล้ว

      ah never minde. The blend node was defaulted to Overlay instead of Overwrite.

    • @NedMakesGames
      @NedMakesGames  3 ปีที่แล้ว

      @@michaelwalker7706 Yeah, I’ve done that many times. Glad it’s working now!

  • @GymCritical
    @GymCritical 4 ปีที่แล้ว +3

    This man is Unity Jesus.

  • @jashwaaradhya2989
    @jashwaaradhya2989 3 ปีที่แล้ว

    How do we do it if there are multiple materials for a game object

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

      Hi! Mesh renderer have a “materials” field that holds a list of all materials on the object, so just select the one at the correct index. Everything else works the same!

  • @kunkerkar
    @kunkerkar 4 ปีที่แล้ว

    Can u make volumetric clouds with shader graph?

    • @NedMakesGames
      @NedMakesGames  4 ปีที่แล้ว

      Thank you for the suggestion! I’ll add it to my todo list

    • @kunkerkar
      @kunkerkar 4 ปีที่แล้ว

      @@NedMakesGames thank u very much for sharing the knowledge

  • @ThomasChen-ur2gt
    @ThomasChen-ur2gt 4 ปีที่แล้ว

    Oh gee, any hint for the exercise?

    • @NedMakesGames
      @NedMakesGames  4 ปีที่แล้ว

      Hi! You would either need to add a timer that blocks new ripples from starting, or add support for multiple ripples in the shader. The second is pretty complicated, but possible by using arrays!

    • @LeviS142
      @LeviS142 3 ปีที่แล้ว

      @@NedMakesGames Thanks so much for the tutorial! It helped a lot. I've been trying for the past few days to get multiple ripples working, and simply can't get it working. Do you have any other hints that might get me closer to on track?

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

      @@LeviS142 Hi, I'm glad you enjoyed it!
      It's a little complicated. You would need to create an array of positions and loop through each position to see if the current pixel is within range. You'll have to do this in a custom function node, since loops aren't possible using just nodes. In the custom function file, declare an array like "float4 _RippleCenters[8]", then you can set it from C# using "Shader.SetGlobalVectorArray("_RippleCenters, rippleCentersCSharpArray);"

    • @LeviS142
      @LeviS142 3 ปีที่แล้ว

      @@NedMakesGames Thank you SO much for the help!! I've been quite frankly overcomplicating this a lot. I was instead trying to read/write to an image texture because that was the only array property available in shadergraph. Thank you for the quick reply as well! Your tutorials were incredibly helpful and I'm grateful that you took the time to share your knowledge!

    • @LeviS142
      @LeviS142 3 ปีที่แล้ว

      @@NedMakesGames I got it working! Thank you for the guidance!

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

    Is it even possible to GET values from Shader Graph in Scripts?

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

      Yes! You can use Material.GetFloat, GetTexture, GetColor, etc.

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

    Ty