Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Influence of terrain height on texture.

Discussion in 'Shader Graph' started by Zimaell, Sep 4, 2023.

  1. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    417
    How to build graphs so that they do the following - if the height of the vertex of the terrain ends at 0.0001 then one texture, if 0.0002 then another, and so on several different textures.

    If necessary, I can construct a heightmap with code, if it is needed in the shader, and if it is not needed and you can somehow do without it, then even better.

    I am new to graphs and therefore I don’t even know what nodes are needed for this and according to what logic they should line up ...

    (standart terrain, URP)
     
  2. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    767
    I dont believe you can use shadergraph on unity terrain.

    Maybe its new in Unity 2023 and i missed the memo...

    Anyways, lots of ways to do as you ask. Position node set to object or world would be a good start. Play with, try to understand. SplitRGB, use G,(y axis).
     
    Zimaell likes this.
  3. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    417
    what's the problem with using a graph shader on a terrain?
    At the moment, I have a question of how to use different textures for the desired areas, 2 textures can be applied using a terrain information map, a PNG file that contains coordinates and vertex data.

    The problem with two textures is that I don’t know how to specify something similar to a switch in the shader graph, that is, so that I can set several textures and several case registers for them, and choose the one I need to display from them.

    I'm just new to shader graphs, so I don't know how...
     
  4. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    767
    Last i checked, (4 years ago), you could not apply shadergraph shaders to terrain.

    Perhaps its the tesselation that terrain uses? I dont know, I just checked again, and terrain does not appear to have the standard material set up.

    Things may have changed. I use a shader graph on my "terrain", but my "terrain" is a mesh object, not a unity terrain object.
     
    Zimaell likes this.
  5. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    417
    I create a material, assign my shader graph to it, and replace this material in the terrain, and that's all, I can apply there, for example, a triplanar, also different textures, only two so far, I don't see any problems...
    just need a switch textures mechanism in the shader graph...
     
  6. BenCloward

    BenCloward

    Unity Technologies

    Joined:
    Jul 15, 2021
    Posts:
    145
    You can get the position of the vertices using the Position node in World space and then use the Y component of the Position for height. Here's a very simple subgraph that you can make that will generate a black to white mask based on a minimum height and a maximum height. You could use the results of this to blend from one texture to another.

    upload_2023-9-5_8-43-51.png
     
    Zimaell likes this.
  7. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    417
    thanks for the tip, but how to make a selection from multiple textures? if there are more than 2 textures, I still don’t understand how to make such logic in which a specific texture was selected from several (more than 2) by specific values
     
  8. Ne0mega

    Ne0mega

    Joined:
    Feb 18, 2018
    Posts:
    767
    Zimaell likes this.
  9. BenCloward

    BenCloward

    Unity Technologies

    Joined:
    Jul 15, 2021
    Posts:
    145
    To do that, you can daisy-chain several lerp nodes together - so you blend between the first texture and the next lerp with the first mask. With the second mask, you blend between the second texture and the next lerp, etc. I can post a screen shot of that as well if you need it.
     
    Zimaell likes this.
  10. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    417
    Yes, please show how to choose one of several textures (more than two) using conditions.

    I eventually need to display the desired texture at the desired height of the terrain, if it's not difficult, then show how the nodes should line up, I don't understand yet ...
     
  11. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    417
    maybe can create custom node, similar to switch?
    although I’m not very good at graphs yet, but in my opinion something like that can be ...
    it would be nice if you could do it partly in graphs and partly in code...
     
  12. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,341
    Code (CSharp):
    1. float3 factors = float3(0.1, 0.5, 0.2);
    2. float4 finalColor = lerp(lerp(lerp(color1, color2, factors.x), color3, factors.y), color4, factors.z);
     
    Zimaell likes this.
  13. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    417
    I think it will be much better and more convenient to use custom nodes, I just made a test node that includes 4 textures and a vertex height, and inside a switch that, based on the height, sets what texture will be output, it's much more convenient than a bunch of lerps...
    (Yes, and the code can achieve a more flexible result)
     
  14. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,341
    Lerp solution was proposed for a reason.
    First of all when you create terrain texture you want to blend layers, not just pick one of them, and lerping allows that (but it's fine to just pick one if this is what you want).
    Second, depending on how you structure your code branching might be expansive, because GPU does not work in the same way as CPU.
    Third, if you really want to pick just one texture, then you could use Texture2DArray and set level you want to sample. This way you can have a lot of different textures without branching and with only one sampling.
     
    Zimaell likes this.
  15. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    417
    but if I have, for example, 20 textures and at the right heights I need to display the right textures, won't this complicate the calculations?

    I will explain my idea - I set vertices in the terrain with a script, and I can set vertices with such small numbers as (1.0001, 45.0002, 567.3403), it is these ten thousandths that will be converted to integers for use in the switch, these numbers will contain information about what texture you need to output and into which it should overflow.

    ten-thousandths will not affect the height significantly, but they will contain all the necessary information about the texture display, wouldn't that be better?

    p.s. - so far I've thought about it, now I'm trying to recreate it ....