From bc7db7a08dd2e645fbe3f6fda4771fb8f3b8bedf Mon Sep 17 00:00:00 2001 From: Neil Graham Date: Wed, 21 Feb 2018 11:43:58 +1300 Subject: [PATCH 1/3] add Image::bumpmap_to_normalmap conversion function --- core/image.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ core/image.h | 1 + 2 files changed, 43 insertions(+) diff --git a/core/image.cpp b/core/image.cpp index 07e705265d6..c6d1c07332e 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -2271,6 +2271,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("premultiply_alpha"), &Image::premultiply_alpha); ClassDB::bind_method(D_METHOD("srgb_to_linear"), &Image::srgb_to_linear); ClassDB::bind_method(D_METHOD("normalmap_to_xy"), &Image::normalmap_to_xy); + ClassDB::bind_method(D_METHOD("bumpmap_to_normalmap","bump_scale"), &Image::bumpmap_to_normalmap, DEFVAL(1.0)); ClassDB::bind_method(D_METHOD("blit_rect", "src", "src_rect", "dst"), &Image::blit_rect); ClassDB::bind_method(D_METHOD("blit_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blit_rect_mask); @@ -2379,6 +2380,47 @@ void Image::normalmap_to_xy() { convert(Image::FORMAT_LA8); } +void Image::bumpmap_to_normalmap(float bump_scale) { + ERR_FAIL_COND(!_can_modify(format)); + convert(Image::FORMAT_RF); + + PoolVector result_image; //rgba output + result_image.resize(width * height* 4); + + { + PoolVector::Read rp = data.read(); + PoolVector::Write wp = result_image.write(); + + unsigned char *write_ptr = wp.ptr(); + float *read_ptr = (float*) rp.ptr(); + + for (int ty = 0; ty < height; ty++) { + int py = ty+1; + if (py >= height) py-=height; + + for (int tx = 0; tx < width; tx++) { + int px=tx+1; + if (px >= width) px-=width; + float here = read_ptr[ty*width + tx]; + float to_right= read_ptr[ty*width + px]; + float above= read_ptr[py*width + tx]; + Vector3 up = Vector3(0,1,(here-above)*bump_scale); + Vector3 across = Vector3(1,0,(to_right-here)*bump_scale); + + Vector3 normal = across.cross(up); + normal.normalize(); + + write_ptr[ ((ty*width + tx) <<2) +0 ]= (128.0 + normal.x * 127.0); + write_ptr[ ((ty*width + tx) <<2) +1 ]= (128.0 + normal.y * 127.0); + write_ptr[ ((ty*width + tx) <<2) +2 ]= (128.0 + normal.z * 127.0); + write_ptr[ ((ty*width + tx) <<2) +3 ]= 255; + } + } + } + format=FORMAT_RGBA8; + data=result_image; +} + void Image::srgb_to_linear() { if (data.size() == 0) diff --git a/core/image.h b/core/image.h index e962787ae9f..69f7bb2ff07 100644 --- a/core/image.h +++ b/core/image.h @@ -284,6 +284,7 @@ public: void premultiply_alpha(); void srgb_to_linear(); void normalmap_to_xy(); + void bumpmap_to_normalmap(float bump_scale=1.0); void blit_rect(const Ref &p_src, const Rect2 &p_src_rect, const Point2 &p_dest); void blit_rect_mask(const Ref &p_src, const Ref &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest); From 617d3cde1ac34213a432c4750ffdacb01a780ac2 Mon Sep 17 00:00:00 2001 From: Neil Graham Date: Sat, 24 Feb 2018 13:59:02 +1300 Subject: [PATCH 2/3] change to clang format --- core/image.cpp | 38 +++++++++++++++++++------------------- core/image.h | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/core/image.cpp b/core/image.cpp index c6d1c07332e..3a7c3a9d7e1 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -2271,7 +2271,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("premultiply_alpha"), &Image::premultiply_alpha); ClassDB::bind_method(D_METHOD("srgb_to_linear"), &Image::srgb_to_linear); ClassDB::bind_method(D_METHOD("normalmap_to_xy"), &Image::normalmap_to_xy); - ClassDB::bind_method(D_METHOD("bumpmap_to_normalmap","bump_scale"), &Image::bumpmap_to_normalmap, DEFVAL(1.0)); + ClassDB::bind_method(D_METHOD("bumpmap_to_normalmap", "bump_scale"), &Image::bumpmap_to_normalmap, DEFVAL(1.0)); ClassDB::bind_method(D_METHOD("blit_rect", "src", "src_rect", "dst"), &Image::blit_rect); ClassDB::bind_method(D_METHOD("blit_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blit_rect_mask); @@ -2384,41 +2384,41 @@ void Image::bumpmap_to_normalmap(float bump_scale) { ERR_FAIL_COND(!_can_modify(format)); convert(Image::FORMAT_RF); - PoolVector result_image; //rgba output - result_image.resize(width * height* 4); + PoolVector result_image; //rgba output + result_image.resize(width * height * 4); { PoolVector::Read rp = data.read(); PoolVector::Write wp = result_image.write(); unsigned char *write_ptr = wp.ptr(); - float *read_ptr = (float*) rp.ptr(); + float *read_ptr = (float *)rp.ptr(); for (int ty = 0; ty < height; ty++) { - int py = ty+1; - if (py >= height) py-=height; + int py = ty + 1; + if (py >= height) py -= height; for (int tx = 0; tx < width; tx++) { - int px=tx+1; - if (px >= width) px-=width; - float here = read_ptr[ty*width + tx]; - float to_right= read_ptr[ty*width + px]; - float above= read_ptr[py*width + tx]; - Vector3 up = Vector3(0,1,(here-above)*bump_scale); - Vector3 across = Vector3(1,0,(to_right-here)*bump_scale); + int px = tx + 1; + if (px >= width) px -= width; + float here = read_ptr[ty * width + tx]; + float to_right = read_ptr[ty * width + px]; + float above = read_ptr[py * width + tx]; + Vector3 up = Vector3(0, 1, (here - above) * bump_scale); + Vector3 across = Vector3(1, 0, (to_right - here) * bump_scale); Vector3 normal = across.cross(up); normal.normalize(); - write_ptr[ ((ty*width + tx) <<2) +0 ]= (128.0 + normal.x * 127.0); - write_ptr[ ((ty*width + tx) <<2) +1 ]= (128.0 + normal.y * 127.0); - write_ptr[ ((ty*width + tx) <<2) +2 ]= (128.0 + normal.z * 127.0); - write_ptr[ ((ty*width + tx) <<2) +3 ]= 255; + write_ptr[((ty * width + tx) << 2) + 0] = (128.0 + normal.x * 127.0); + write_ptr[((ty * width + tx) << 2) + 1] = (128.0 + normal.y * 127.0); + write_ptr[((ty * width + tx) << 2) + 2] = (128.0 + normal.z * 127.0); + write_ptr[((ty * width + tx) << 2) + 3] = 255; } } } - format=FORMAT_RGBA8; - data=result_image; + format = FORMAT_RGBA8; + data = result_image; } void Image::srgb_to_linear() { diff --git a/core/image.h b/core/image.h index 69f7bb2ff07..17477d88ea4 100644 --- a/core/image.h +++ b/core/image.h @@ -284,7 +284,7 @@ public: void premultiply_alpha(); void srgb_to_linear(); void normalmap_to_xy(); - void bumpmap_to_normalmap(float bump_scale=1.0); + void bumpmap_to_normalmap(float bump_scale = 1.0); void blit_rect(const Ref &p_src, const Rect2 &p_src_rect, const Point2 &p_dest); void blit_rect_mask(const Ref &p_src, const Ref &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest); From 4bea98de4039cc21496db5a7259fdab74a93b386 Mon Sep 17 00:00:00 2001 From: Neil Graham Date: Fri, 16 Mar 2018 10:22:48 +1300 Subject: [PATCH 3/3] change bumpmap_to_normalmap range from [1...255] to [0...255] --- core/image.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/image.cpp b/core/image.cpp index 3a7c3a9d7e1..74c529512ba 100644 --- a/core/image.cpp +++ b/core/image.cpp @@ -2410,9 +2410,9 @@ void Image::bumpmap_to_normalmap(float bump_scale) { Vector3 normal = across.cross(up); normal.normalize(); - write_ptr[((ty * width + tx) << 2) + 0] = (128.0 + normal.x * 127.0); - write_ptr[((ty * width + tx) << 2) + 1] = (128.0 + normal.y * 127.0); - write_ptr[((ty * width + tx) << 2) + 2] = (128.0 + normal.z * 127.0); + write_ptr[((ty * width + tx) << 2) + 0] = (127.5 + normal.x * 127.5); + write_ptr[((ty * width + tx) << 2) + 1] = (127.5 + normal.y * 127.5); + write_ptr[((ty * width + tx) << 2) + 2] = (127.5 + normal.z * 127.5); write_ptr[((ty * width + tx) << 2) + 3] = 255; } }