Skip to content

Commit

Permalink
Change shader to support new format. Improve initial conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Oct 26, 2023
1 parent ab69a75 commit 1724403
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 38 deletions.
58 changes: 30 additions & 28 deletions src/shaders/main.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
R"(shader_type spatial;
render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlick_ggx;

const float RANGE[] = { 0.0f, .125f, .25f, .334f, .5f, .667f, .8f, 1.0f };

uniform float _region_size = 1024.0;
uniform float _region_pixel_size = 0.0009765625; // 1.0 / 1024.0
uniform int _region_map_size = 16;
uniform int _region_uv_limit = 8;
uniform int _region_map[256];
uniform vec2 _region_offsets[256];
uniform sampler2DArray _height_maps : filter_linear_mipmap, repeat_disable;
uniform sampler2DArray _control_maps : filter_linear_mipmap, repeat_disable;
uniform sampler2DArray _color_maps : source_color, filter_linear, repeat_disable;

uniform sampler2DArray _height_maps : repeat_disable;
uniform sampler2DArray _control_maps : repeat_disable;
uniform sampler2DArray _color_maps : source_color, repeat_disable;
uniform sampler2DArray _texture_array_albedo : source_color, filter_linear_mipmap_anisotropic, repeat_enable;
uniform sampler2DArray _texture_array_normal : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable;
uniform float _texture_uv_scale_array[32];
Expand Down Expand Up @@ -97,32 +98,33 @@ vec3 get_normal(vec2 uv, out vec3 tangent, out vec3 binormal) {
return normal;
}

vec4 get_material(vec2 uv, vec4 index, vec2 uv_center, float weight, inout float total_weight, inout vec4 out_normal) {
float material = index.r * 255.0;
vec4 get_material(vec2 uv, uint control, vec2 uv_center, float weight, inout float total_weight, inout vec4 out_normal) {
uint base_tex = control>>27u & 0x1Fu;
float r = random(uv_center) * PI;
float rand = r * _texture_uv_rotation_array[int(material)];
float rand = r * _texture_uv_rotation_array[base_tex];
vec2 rot = vec2(cos(rand), sin(rand));
vec2 matUV = rotate(uv, rot.x, rot.y) * _texture_uv_scale_array[int(material)];
vec2 matUV = rotate(uv, rot.x, rot.y) * _texture_uv_scale_array[base_tex];

vec4 albedo = texture(_texture_array_albedo, vec3(matUV, material));
albedo.rgb *= _texture_color_array[int(material)].rgb;
vec4 normal = texture(_texture_array_normal, vec3(matUV, material));
vec4 albedo = texture(_texture_array_albedo, vec3(matUV, float(base_tex)));
albedo.rgb *= _texture_color_array[base_tex].rgb;
vec4 normal = texture(_texture_array_normal, vec3(matUV, float(base_tex)));
vec3 n = unpack_normal(normal);
normal.xz = rotate(n.xz, rot.x, -rot.y);

if (index.b > 0.0) {
float materialOverlay = index.g * 255.0;
float rand2 = r * _texture_uv_rotation_array[int(materialOverlay)];
uint blend_idx = control >> 19u & 0x7u;
if (blend_idx > 0u) {
uint over_tex = control >> 22u & 0x1Fu;
float rand2 = r * _texture_uv_rotation_array[over_tex];
vec2 rot2 = vec2(cos(rand2), sin(rand2));
vec2 matUV2 = rotate(uv, rot2.x, rot2.y) * _texture_uv_scale_array[int(materialOverlay)];
vec4 albedo2 = texture(_texture_array_albedo, vec3(matUV2, materialOverlay));
albedo2.rgb *= _texture_color_array[int(materialOverlay)].rgb;
vec4 normal2 = texture(_texture_array_normal, vec3(matUV2, materialOverlay));
vec2 matUV2 = rotate(uv, rot2.x, rot2.y) * _texture_uv_scale_array[over_tex];
vec4 albedo2 = texture(_texture_array_albedo, vec3(matUV2, float(over_tex)));
albedo2.rgb *= _texture_color_array[over_tex].rgb;
vec4 normal2 = texture(_texture_array_normal, vec3(matUV2, float(over_tex)));
n = unpack_normal(normal2);
normal2.xz = rotate(n.xz, rot2.x, -rot2.y);

albedo = depth_blend(albedo, albedo.a, albedo2, albedo2.a, index.b);
normal = depth_blend(normal, albedo.a, normal2, albedo2.a, index.b);
albedo = depth_blend(albedo, albedo.a, albedo2, albedo2.a, RANGE[blend_idx]);
normal = depth_blend(normal, albedo.a, normal2, albedo2.a, RANGE[blend_idx]);
}

normal = pack_normal(normal.xyz, normal.a);
Expand Down Expand Up @@ -161,10 +163,10 @@ void fragment() {
ivec3 index10UV = get_region((pos_texel00 + mirror.zy) * _region_pixel_size);
ivec3 index11UV = get_region((pos_texel00 + mirror.zw) * _region_pixel_size);

vec4 index00 = texelFetch(_control_maps, index00UV, 0);
vec4 index01 = texelFetch(_control_maps, index01UV, 0);
vec4 index10 = texelFetch(_control_maps, index10UV, 0);
vec4 index11 = texelFetch(_control_maps, index11UV, 0);
uint control00 = floatBitsToUint(texelFetch(_control_maps, index00UV, 0).r);
uint control01 = floatBitsToUint(texelFetch(_control_maps, index01UV, 0).r);
uint control10 = floatBitsToUint(texelFetch(_control_maps, index10UV, 0).r);
uint control11 = floatBitsToUint(texelFetch(_control_maps, index11UV, 0).r);

vec2 weights1 = clamp(pos_texel - pos_texel00, 0, 1);
weights1 = mix(weights1, vec2(1.0) - weights1, mirror.xy);
Expand All @@ -174,10 +176,10 @@ void fragment() {
vec4 normal = vec4(0.0);
vec4 color = vec4(0.0);

color = get_material(UV, index00, vec2(index00UV.xy), weights0.x * weights0.y, total_weight, normal);
color += get_material(UV, index01, vec2(index01UV.xy), weights0.x * weights1.y, total_weight, normal);
color += get_material(UV, index10, vec2(index10UV.xy), weights1.x * weights0.y, total_weight, normal);
color += get_material(UV, index11, vec2(index11UV.xy), weights1.x * weights1.y, total_weight, normal);
color = get_material(UV, control00, vec2(index00UV.xy), weights0.x * weights0.y, total_weight, normal);
color += get_material(UV, control01, vec2(index01UV.xy), weights0.x * weights1.y, total_weight, normal);
color += get_material(UV, control10, vec2(index10UV.xy), weights1.x * weights0.y, total_weight, normal);
color += get_material(UV, control11, vec2(index11UV.xy), weights1.x * weights1.y, total_weight, normal);
total_weight = 1.0 / total_weight;
normal *= total_weight;
color *= total_weight;
Expand Down
14 changes: 4 additions & 10 deletions src/terrain_3d_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,10 @@ void Terrain3DStorage::set_control_maps(const TypedArray<Image> &p_maps) {
for (int x = 0; x < old_img->get_width(); x++) {
for (int y = 0; y < old_img->get_height(); y++) {
Color pixel = old_img->get_pixel(x, y);
int base = (int(pixel.r * 255) & 0x1F) << 27; // 5 bits 31-27
int over = (int(pixel.g * 255) & 0x1F) << 22; // 5 bits 26-22
int blend_index = 7;
for (int k = 0; k < (sizeof(RANGE) / sizeof(*RANGE)); k++) {
if (pixel.b < (RANGE[k] + RANGE[k + 1]) / 2.0f) {
blend_index = k;
break;
}
}
blend_index <<= 19; // 3 bits 21-19
uint32_t base = (uint32_t(pixel.r * 255) & 0x1F) << 27; // 5 bits 32-28
uint32_t over = (uint32_t(pixel.g * 255) & 0x1F) << 22; // 5 bits 27-23
uint32_t blend_index = Util::closest_index(pixel.b, RANGE, 8);
blend_index <<= 19; // 3 bits 22-20
uint32_t value = base | over | blend_index;
pba.encode_u32((y * _region_size + x) * sizeof(uint32_t), value);
}
Expand Down
13 changes: 13 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
// Public Functions
///////////////////////////

// Given a float and a 0-1 range array like { 0.0f, .125f, .25f, .334f, .5f, .667f, .8f, 1.0f }
// This function returns the index closest to a
int Util::closest_index(float value, const float *p_array, int size) {
int index = size - 1;
for (int k = 0; k < size; k++) {
if (value < (p_array[k] + p_array[k + 1]) / 2.0f) {
index = k;
break;
}
}
return index;
}

void Util::print_dict(String p_name, const Dictionary &p_dict, int level) {
LOG(level, "Printing Dictionary: ", p_name);
Array keys = p_dict.keys();
Expand Down
6 changes: 6 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ class Util {
static inline const char *__class__ = "Terrain3DUtil";

public:
// Math
static int closest_index(float value, const float *p_array, int size);

// Print info to the console
static void print_dict(String name, const Dictionary &p_dict, int p_level = 1); // Defaults to INFO
static void dump_gen(GeneratedTex p_gen, String name = "");
static void dump_maps(const TypedArray<Image> p_maps, String p_name = "");

// Image operations
static Vector2 get_min_max(const Ref<Image> p_image);
static Ref<Image> get_thumbnail(const Ref<Image> p_image, Vector2i p_size = Vector2i(256, 256));
static Ref<Image> get_filled_image(Vector2i p_size,
Expand Down

0 comments on commit 1724403

Please sign in to comment.