Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved URP Shader Graph Issue: Procedural Terrain Generation - Seeking Solutions and Advice

Discussion in 'Shader Graph' started by JustTiredOfEverything, Mar 23, 2023.

  1. JustTiredOfEverything

    JustTiredOfEverything

    Joined:
    Aug 4, 2022
    Posts:
    80
    Greetings,

    I have been working on a project that involves procedural terrain generation using Sebastian Lague's tutorial series. The primary issue I've encountered is with terrain coloring, as the tutorial was not specifically designed for Unity's Universal Render Pipeline (URP). The shader techniques in the tutorial are based on the built-in rendering pipeline, which differs from URP. Specifically, the tutorial makes use of arrays for material properties in the shader, which is not supported in URP's Shader Graph.

    Procedural Landmass Generation (E16: colour shader) - YouTube

    I found a Unity forum thread discussing this, and a community member provided a solution involving passing arrays to shaders using custom functions and separate .hlsl files in Shader Graph. However, this approach was too complex for me to understand and lacks full example code.

    Question - URP shadard surface shader best approximation - Unity Forum

    I came across a video for URP that looks promising. However, I've encountered another issue: the textures blend horizontally instead of vertically, as illustrated in the attached screenshots and mentioned by a user in the comments:

    Screenshot 2023-03-23 161317.png Screenshot 2023-03-23 161444.png

    Comment: "I made this shader, but it's applying the changes on the x-axis instead of the y-axis. Anyone know why and how I can change this?"

    Unity Shader Graph (URP)Terrain Shader - YouTube

    I'm seeking advice from those who have successfully followed Sebastian Lague's tutorial or similar ones and have managed to adapt it for URP or High Definition Render Pipeline (HDRP). If you've encountered and solved this issue, I'd greatly appreciate any tips, resources, or suggestions on how to proceed.

    Has anyone implemented the solution suggested in the referenced thread, addressed the horizontal blending issue from the video, or found a more straightforward approach? I'd love to hear about your experiences and learn from them.

    Thank you in advance for your assistance!
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,291
    I don't think Ben's example is lacking. You just create custom node with uniform array variable.
    You probably just swapped Z and Y axis in the shader, I guess the part where you use world position.
     
  3. JustTiredOfEverything

    JustTiredOfEverything

    Joined:
    Aug 4, 2022
    Posts:
    80
    Thank you. I appreciate your input, but I'm still struggling with this:

    It's not using a World Position node, and it's unclear to me where exactly to swap the Y and Z axis as you suggested.

    I'm not being dense on purpose - I didn't find the instructions on how to go about this clear enough. Specifically, I'm unsure where to put the following:

    Code (CSharp):
    1. #ifndef MYSHADER_CUSTOM_ARRAY
    2. #define MYSHADER_CUSTOM_ARRAY
    3. float _MyFloats[];
    4. void GetMyFloatValue_float(int index, out float value)
    5. {
    6.   return _MyFloats[index];
    7. }
    8. float4 _MyColors[];
    9. void GetMyColorValue_float(int index, out float4 value)
    10. {
    11.   return _MyColors[index];
    12. }
    13. #endif MYSHADER_CUSTOM_ARRAY
    He also says: " But for Shader Graph the only way to do it would be to use a custom function that uses a separate .hlsl file.*

    https://docs.unity3d.com/Packages/com.unity.shadergraph@11.0/manual/Custom-Function-Node.html"

    I have never tried to make a custom node before this - and they didn't really provide instructions at all on how I would go about making one for this specific scenario. I gave it my best but I cant figure out how to make arrays as an input/output for the custom node. That's about as far as I got.
     
    Last edited: Mar 23, 2023
  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,291
    Well, I have not seen your shader, so it was just a guess, in any case you use wrong component somewhere (most likely).

    I haven't watched his videos for a long time, but as far as I remember they are rather harder than easier, so I assumed you have some basic knowledge how things work. Long story short custom node is just way to inject your custom hlsl code to shader graph. You create .hlsl file and put that code there, next you select that file in custom node and set in/out parameters to match the function you use. It is explained in docs, you might want to use a bit newer version as maybe it's more detailed.

    The trick about array is that you can't create array property, but you can create uniform variable that is not directly exposed in inspector and set it via code (check Ben's example). Furthermore shader graph has no way to use arrays, so you must do your calculations inside custom node. Probably something like this (not tested, pseudocode):
    Code (CSharp):
    1.     #ifndef MYSHADER_CUSTOM_ARRAY // <- guard check to ensure this code is included only once
    2.     #define MYSHADER_CUSTOM_ARRAY
    3.  
    4.     float4 _YourColors[]; // This is the array variable you wanted
    5.  
    6.     void GetInterpolatedColor_float(in float t, out float4 outValue)
    7.     {
    8.         // Loop _YourColors here to interpolate the value based on provided 't'
    9.         outValue = interpolatedColor;
    10.     }
    11.     #endif MYSHADER_CUSTOM_ARRAY
    12.  
    In case array stuff is still to much I suggest you create super simple shader with super simple custom node returning some float value [0-1] and this value is set in C#, then you use it as material color, then expand to arrays etc...
     
  5. JustTiredOfEverything

    JustTiredOfEverything

    Joined:
    Aug 4, 2022
    Posts:
    80
    lol a month and a half later, but I figured something out: there's a position node near the beginning that was set to UV space. I set it to object and/or absolute world and either of those options fixed the appearance, object working the best.