Grass shader heightmap problems

DschoonmakerDschoonmaker Posts: 266Member

I'm working on a grass shader using particles. I'm starting with the source code from https://github.com/BastiaanOlij/godot-grass-tutorial/blob/master/materials-and-shaders/particle.shader. It has a sampler2D used to set the height of the grass. The problem is, if I try to add an image, the grass stays flat. It seems that certain images will displace the grass but not others. If I add a different texture, it works and the grass is displaced. It works with types of textures other than StreamTextures, (e.g. NoiseTextures) but it also works with some StreamTextures (it works with normal maps). I think it might have something to do with the format of the texture or something like that. In the top right corner of the texture, it says the dimensions of the image and something else, usually RGB8. On other textures, it sometimes says something else (normal maps say RGTC RedGreen8). Does anyone know what the problem might be and how it could be solved?

«1

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 3,076Admin

    This discussion was caught in the moderation queue since you have not confirmed your account yet.

    Upon creating your account you should have received an account verification email. The confirmation email may have been incorrectly flagged as spam, so please also check your spam filter. Without confirming your account, future posts may also be caught in the moderation queue. You can resend a confirmation email when you log into your account if you cannot find the first verification email.

    If you need any help, please let us know! Thanks! :smile:

  • MegalomaniakMegalomaniak Posts: 2,884Admin

    In case you haven't watched his youtube video on it, you might want to do so. And if you have, perhaps watching it again as a refresher might be good:

  • TwistedTwiglegTwistedTwigleg Posts: 3,076Admin
    edited March 30

    @Dschoonmaker - The Godot email server is unable to send emails to the address you associated with your account. It seems the email address is misspelled or off in some way.
    Please send a message to forum staff with your email address so we can update your profile details, so you can confirm your account.

    (You can also email me needed. My email can be found on this topic and on my profile page)

  • DschoonmakerDschoonmaker Posts: 266Member

    Thanks for the fast replies! I did get my email address wrong, how can I change that?

  • DschoonmakerDschoonmaker Posts: 266Member

    @Megalomaniak , I did watch that video, but maybe I should check it again to be sure I didn't miss anything he might have said about that.

  • MegalomaniakMegalomaniak Posts: 2,884Admin
    edited March 30

    @Dschoonmaker said:
    Thanks for the fast replies! I did get my email address wrong, how can I change that?

    At the top of the page, assuming the default forum theme, there should be a blue banner/bar. Towards the right of it is the userbox, that is the area showing your avatar, username and the expandable menus/panels for messages and such, last among them is a white gearwheel icon. press it and the menu that opens should say edit profile in there.

  • DschoonmakerDschoonmaker Posts: 266Member

    I clicked the gear icon, it gives me two options: View Profile and Preferences. I searched through both of them, but I didn't see anywhere I could change my email. Which is it under?

  • DschoonmakerDschoonmaker Posts: 266Member

    I emailed you my email, @TwistedTwigleg

  • DschoonmakerDschoonmaker Posts: 266Member

    Okay, so I've checked the video multiple times. He doesn't really say anything about problems you might have, he just slapped on a texture and it worked. Nobody in the comments had any problems, either...

  • MegalomaniakMegalomaniak Posts: 2,884Admin

    No but he explains that the heightmap texture is the same grayscale heightmap he is using for the terrain, and that the noisemap is just a random color noise just to get the grass oriented randomly.

    Note that heightmaps are typically 16bpc and unless you are dealing with vector displacement it's just a single color channel. Say red for an example.

  • DschoonmakerDschoonmaker Posts: 266Member
    edited March 31

    Yes, I also have a greyscale heightmap I'm using. I just rendered it in blender because I'm using mesh + custom terrain shader. The code does just use the red channel, this is the function to get the height at the current position:

    float get_height(vec2 pos) {
        pos -= 0.5 * heightmap_size;
        pos /= heightmap_size;
    
        return amplitude * texture(heightmap, pos).r;
    }
    

    The pos parameter should be the x and z position of the current grass blade.

    BPC is the amout of different colors a texture will store per channel, correct? And is that what the 8 is in "RGB8"?

    Also, how do I add code? I tried with that function, but it only recognized the last line.

  • MegalomaniakMegalomaniak Posts: 2,884Admin
    edited March 31

    @Dschoonmaker said:
    BPC is the amout of different colors a texture will store per channel, correct? And is that what the 8 is in "RGB8"?

    Yes

    Also, how do I add code? I tried with that function, but it only recognized the last line.

    there's two ways to format code the inline method uses the ` sign(grave accent) to encapsulate, which is what you used. And the block of code can either be done by using multiple layers of that encapsulation, three to be exact or to indent all of the code one more time.

    You can do the latter either once in your IDE before copying then undoing the indentation in there after pasting here. Or you can just paste your code block here and select it to highlight it then go to the formatting menu(the paragraph icon) and pick code from there.

  • DschoonmakerDschoonmaker Posts: 266Member

    Cool, it works.

    float get_height(vec2 pos) {
        pos -= 0.5 * heightmap_size;
        pos /= heightmap_size;
    
        return amplitude * texture(heightmap, pos).r;
    }
    

    Thanks!

  • DschoonmakerDschoonmaker Posts: 266Member

    BPC shouldn't be a problem, though it could be if I had a wide range of heights. The height of my grass won't change at all if I assign my heightmap texture. I know the get_height function works, some textures do displace the grass. But I think the problem is with texture format, because it works with some textures. The first image uses my heightmap and stays flat, the second uses a noise texture and works. But it also works with certain images, like normal maps. Could this have something to do with "RGB8"? If so, how can I change it?

  • MegalomaniakMegalomaniak Posts: 2,884Admin

    If you mean that the second image works also with normal maps then that should be no problem, so long as it gets some data from the image to randomize the orientation of the grass, that's all that matters. Though a normal map might be a little too uniform in sections perhaps.

    There should be no inherent issue with the heightmap being 8 bits per channel, only as you noted that the terrain might look very 'posterised' if the variance in elevation is too high.

  • DschoonmakerDschoonmaker Posts: 266Member
    edited March 31

    I don't actually have any problems with the noisemap, it's just the heightmap. I think the reason normal maps work is because Godot automatically converts them to "RGTC RedGreen8", while the heightmap stays at "RGB8". I think it might work if I converted the heightmap to something else, but how would I do that? Should I do it in photo editing software, or maybe GDScript?

    I also tried saving the image in different file formats, but that didn't work either.

  • MegalomaniakMegalomaniak Posts: 2,884Admin

    Ah, you should just select the heightmap in the file browser then check the import tab. Sadly it doesn't make it clear what gets imported as what, but you can easily find out via trial and error.

  • DschoonmakerDschoonmaker Posts: 266Member

    I copied the same import settings the normal map uses. Sure enough, it does switch to saying "RGTC RedGreen 8", but the grass stays flat. I thought that was it, but now I don't know what it could be. My heightmap should definitely change it:

  • DschoonmakerDschoonmaker Posts: 266Member

    Wait, if I import it under the preset "3D", it seems to do some displacement? It certainly doesn't seem to be working correctly:

  • MegalomaniakMegalomaniak Posts: 2,884Admin

    is your terrain at world center?

  • DschoonmakerDschoonmaker Posts: 266Member
    edited March 31

    Yes, but black on the heightmap represents the lowest point on the map mesh, not necessarily a height of 0.

  • MegalomaniakMegalomaniak Posts: 2,884Admin

    no I meant the terrain objects pivot point. but yeah, if your terrain height doesn't assume 0 as 0 but Olijs shaders do then that might be a cause of your issue, just modify the shaders to include some uniforms for offset values then.

  • DschoonmakerDschoonmaker Posts: 266Member

    I'm pretty sure the pivot point is at the center, I can make it be in blender if it isn't. I also think I should add a variable for height offset, that will be quick and easy.

    Do you know what the 3D import preset is? I don't really have any clue.

  • MegalomaniakMegalomaniak Posts: 2,884Admin

    In the importer? I think it's the preset that will try to detect things such as if the texture is a normal map and such.

  • DschoonmakerDschoonmaker Posts: 266Member

    So it's just for 3D texture as opposed to 2D sprites. Thanks, I just heard about some "3D texture" that was just a bunch of texture stuck together to be 3D.

  • DschoonmakerDschoonmaker Posts: 266Member

    I added a parameter called "base height", all it does is offset the whole thing. I also moved the pivot point to the middle in blender. It still doesn't seem right, and I get these weird holes:

    The grass in the hole isn't above or below, it looks like it's just gone.

  • MegalomaniakMegalomaniak Posts: 2,884Admin

    It might be easier to analyze if you shared your scene and assets, since the original shader obviously was working for Olij as intended.

  • DschoonmakerDschoonmaker Posts: 266Member

    I think the problem is adjusting the scale. With my testing terrain shader, it's aligned only when the size of the plane is 100x100. I have to adjust the heightmap size parameter on both the terrain and the grass, but I don't know what to set it to. Also, note that I'm using a mesh I made in blender, the heightmap shader is just for debugging.
    I can send you the project, how should I do that?

  • MegalomaniakMegalomaniak Posts: 2,884Admin

    I've been using my google drive for it.

  • DschoonmakerDschoonmaker Posts: 266Member

    Okay, I uploaded the project:
    https://drive.google.com/drive/folders/1fMr1R34NReKUZt-A3NleQB8-z4QGCTSI?usp=sharing
    I didn't really clean it up, the only three nodes relevant are the top three: Terrain, Grass, and Test Terrain.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file