Zylann / godot_heightmap_plugin

HeightMap terrain for Godot implemented in GDScript
Other
1.74k stars 159 forks source link

Can you turn off texture filtering (want to use pixely low res textures) #233

Open Tim-Harlow opened 3 years ago

Tim-Harlow commented 3 years ago

Can you turn off texture filtering (want to use pixely low res textures)? Currently, if I lower the resolution of the texture, it just makes it more blury.

Zylann commented 3 years ago

This question was answered countless times for pixel-art games, in various ways. The same applies in 3D: find your texture in the filesystem dock, select it, then go to the Import tab: it has a filter option. Uncheck it and click "Reimport".

image

manum45 commented 1 year ago

This option is not present anymore in Godot4's Texture2D import. According to this, filtering is now configured in the CanvasItem, which I suppose only works for 2D. How can filtering be turned of in Godot 4?

Zylann commented 1 year ago

Changing the sampling mode is unfortunately more effort now, both for you and me:

The plugin requires a shader to render. The way Godot 4 expects you to do this now, is to modify the shader directly, by adding filter[_nearest, _linear][_mipmap][_anisotropic] after the uniform line corresponding to textures you want to change. For plugin makers, that means it is not exposable as a checkbox or dropdown. It requires a different shader for every possible combination of hints and render modes you want, and so duplicate variants of every shader the plugin provides.

Because of this, the easiest for you is to fork the shader you desire and add these hints to it. You can do so by creating a new shader under the Custom Shader property (which will copy the current built-in shader as a new unique shader; or create the copy manually), and switch Shader Type to Custom.

Then for example, in classic4: https://github.com/Zylann/godot_heightmap_plugin/blob/a79f6c4efdf1d160774d89d259ede097bd23aad7/addons/zylann.hterrain/shaders/simple4.gdshader#L22-L30 Change these lines into:

uniform sampler2D u_ground_albedo_bump_0 : source_color, filter_nearest_mipmap;
uniform sampler2D u_ground_albedo_bump_1 : source_color, filter_nearest_mipmap;
uniform sampler2D u_ground_albedo_bump_2 : source_color, filter_nearest_mipmap;
uniform sampler2D u_ground_albedo_bump_3 : source_color, filter_nearest_mipmap;

uniform sampler2D u_ground_normal_roughness_0 : filter_nearest_mipmap;
uniform sampler2D u_ground_normal_roughness_1 : filter_nearest_mipmap;
uniform sampler2D u_ground_normal_roughness_2 : filter_nearest_mipmap;
uniform sampler2D u_ground_normal_roughness_3 : filter_nearest_mipmap;

image

manum45 commented 1 year ago

Thanks for the quick reply, this works!

drd-dev commented 9 months ago

Just used this, works great!

cessar1 commented 9 months ago

Thanks!

Calinou commented 9 months ago

The way Godot 4 expects you to do this now, is to modify the shader directly, by adding filter[_nearest, _linear][_mipmap][_anisotropic] after the uniform line corresponding to textures you want to change.

I wonder if the Godot shader language could feature filter_tweakable and repeat_tweakable that can be set to any filter mode in the inspector if at least one instance of it is present in a shader. This would be a relatively easy way of exposing filter modes in a way that doesn't require extensive changes to the shader compiler. The main difficulty would be seeing whether the hint is used at least once and displaying it in the inspector in this case. The downside is that you wouldn't be able to have multiple filter modes configurable in a single shader, but it's a rare use case in my experience.

Zylann commented 9 months ago

Actually if there was a way to treat #define as tweakable in shader/material properties then it would make this easier to implement, because then I could expose a specific option that translates into #define PIXELATED_TERRAIN in the ShaderMaterial, which would then account for it if it's coded using it. Basic shader variants management really, I believe there was a PR but no idea where it went. Managing shader variants in Godot isn't quite fun at the moment, I believe Terrain3D has to develop their own shader generation system as well. If there is a place for the user to set global #defines then it could be a start, though that would only work globally and requires some clicking/typing.

Zylann commented 9 months ago

Actually, thinking about it, if sampler settings don't actually require a different shader under the hood (I don't recall GLSL requiring to specify that), it would be wasteful to go that route because it involves shader compilation (the choice of coupling this to shaders is purely a Godot thing?). But it would still be useful for other features.