2019-03-17 09:47:19 +00:00
|
|
|
/**************************************************************************/
|
2020-03-27 07:44:44 +00:00
|
|
|
/* height_map_shape_3d.cpp */
|
2019-03-17 09:47:19 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2020-03-27 07:44:44 +00:00
|
|
|
#include "height_map_shape_3d.h"
|
2022-03-26 15:20:30 +00:00
|
|
|
|
2024-02-02 23:33:11 +00:00
|
|
|
#include "core/io/image.h"
|
2020-03-27 18:21:27 +00:00
|
|
|
#include "servers/physics_server_3d.h"
|
2019-03-17 09:47:19 +00:00
|
|
|
|
2020-08-02 09:10:43 +00:00
|
|
|
Vector<Vector3> HeightMapShape3D::get_debug_mesh_lines() const {
|
2019-03-17 09:47:19 +00:00
|
|
|
Vector<Vector3> points;
|
|
|
|
|
2019-04-23 11:51:56 +00:00
|
|
|
if ((map_width != 0) && (map_depth != 0)) {
|
|
|
|
// This will be slow for large maps...
|
|
|
|
// also we'll have to figure out how well bullet centers this shape...
|
2019-03-17 09:47:19 +00:00
|
|
|
|
2019-04-23 11:51:56 +00:00
|
|
|
Vector2 size(map_width - 1, map_depth - 1);
|
|
|
|
Vector2 start = size * -0.5;
|
2019-03-17 09:47:19 +00:00
|
|
|
|
2021-08-09 22:15:17 +00:00
|
|
|
const real_t *r = map_data.ptr();
|
2019-03-17 09:47:19 +00:00
|
|
|
|
2019-04-23 11:51:56 +00:00
|
|
|
// reserve some memory for our points..
|
2021-03-25 00:58:54 +00:00
|
|
|
points.resize(((map_width - 1) * map_depth * 2) + (map_width * (map_depth - 1) * 2) + ((map_width - 1) * (map_depth - 1) * 2));
|
2019-04-23 11:51:56 +00:00
|
|
|
|
|
|
|
// now set our points
|
|
|
|
int r_offset = 0;
|
|
|
|
int w_offset = 0;
|
|
|
|
for (int d = 0; d < map_depth; d++) {
|
|
|
|
Vector3 height(start.x, 0.0, start.y);
|
|
|
|
|
|
|
|
for (int w = 0; w < map_width; w++) {
|
2019-04-25 03:03:48 +00:00
|
|
|
height.y = r[r_offset++];
|
2019-03-17 09:47:19 +00:00
|
|
|
|
2019-04-23 11:51:56 +00:00
|
|
|
if (w != map_width - 1) {
|
|
|
|
points.write[w_offset++] = height;
|
2019-04-25 03:03:48 +00:00
|
|
|
points.write[w_offset++] = Vector3(height.x + 1.0, r[r_offset], height.z);
|
2019-04-23 11:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (d != map_depth - 1) {
|
|
|
|
points.write[w_offset++] = height;
|
2019-04-25 03:03:48 +00:00
|
|
|
points.write[w_offset++] = Vector3(height.x, r[r_offset + map_width - 1], height.z + 1.0);
|
2019-04-23 11:51:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 00:58:54 +00:00
|
|
|
if ((w != map_width - 1) && (d != map_depth - 1)) {
|
|
|
|
points.write[w_offset++] = Vector3(height.x + 1.0, r[r_offset], height.z);
|
|
|
|
points.write[w_offset++] = Vector3(height.x, r[r_offset + map_width - 1], height.z + 1.0);
|
|
|
|
}
|
|
|
|
|
2019-04-23 11:51:56 +00:00
|
|
|
height.x += 1.0;
|
2019-03-17 09:47:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 11:51:56 +00:00
|
|
|
start.y += 1.0;
|
2019-03-17 09:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
2020-03-27 07:44:44 +00:00
|
|
|
real_t HeightMapShape3D::get_enclosing_radius() const {
|
2020-01-10 11:22:34 +00:00
|
|
|
return Vector3(real_t(map_width), max_height - min_height, real_t(map_depth)).length();
|
|
|
|
}
|
|
|
|
|
2020-03-27 07:44:44 +00:00
|
|
|
void HeightMapShape3D::_update_shape() {
|
2019-03-17 09:47:19 +00:00
|
|
|
Dictionary d;
|
|
|
|
d["width"] = map_width;
|
|
|
|
d["depth"] = map_depth;
|
|
|
|
d["heights"] = map_data;
|
|
|
|
d["min_height"] = min_height;
|
|
|
|
d["max_height"] = max_height;
|
2020-03-27 18:21:27 +00:00
|
|
|
PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), d);
|
2020-03-26 21:49:16 +00:00
|
|
|
Shape3D::_update_shape();
|
2019-03-17 09:47:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 07:44:44 +00:00
|
|
|
void HeightMapShape3D::set_map_width(int p_new) {
|
2019-03-17 09:47:19 +00:00
|
|
|
if (p_new < 1) {
|
|
|
|
// ignore
|
|
|
|
} else if (map_width != p_new) {
|
|
|
|
int was_size = map_width * map_depth;
|
|
|
|
map_width = p_new;
|
|
|
|
|
|
|
|
int new_size = map_width * map_depth;
|
|
|
|
map_data.resize(map_width * map_depth);
|
|
|
|
|
2021-08-09 22:15:17 +00:00
|
|
|
real_t *w = map_data.ptrw();
|
2019-03-17 09:47:19 +00:00
|
|
|
while (was_size < new_size) {
|
|
|
|
w[was_size++] = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_update_shape();
|
2023-07-03 19:29:37 +00:00
|
|
|
emit_changed();
|
2019-03-17 09:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 07:44:44 +00:00
|
|
|
int HeightMapShape3D::get_map_width() const {
|
2019-03-17 09:47:19 +00:00
|
|
|
return map_width;
|
|
|
|
}
|
|
|
|
|
2020-03-27 07:44:44 +00:00
|
|
|
void HeightMapShape3D::set_map_depth(int p_new) {
|
2019-03-17 09:47:19 +00:00
|
|
|
if (p_new < 1) {
|
|
|
|
// ignore
|
|
|
|
} else if (map_depth != p_new) {
|
|
|
|
int was_size = map_width * map_depth;
|
|
|
|
map_depth = p_new;
|
|
|
|
|
|
|
|
int new_size = map_width * map_depth;
|
|
|
|
map_data.resize(new_size);
|
|
|
|
|
2021-08-09 22:15:17 +00:00
|
|
|
real_t *w = map_data.ptrw();
|
2019-03-17 09:47:19 +00:00
|
|
|
while (was_size < new_size) {
|
|
|
|
w[was_size++] = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_update_shape();
|
2023-07-03 19:29:37 +00:00
|
|
|
emit_changed();
|
2019-03-17 09:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 07:44:44 +00:00
|
|
|
int HeightMapShape3D::get_map_depth() const {
|
2019-03-17 09:47:19 +00:00
|
|
|
return map_depth;
|
|
|
|
}
|
|
|
|
|
2021-08-09 22:15:17 +00:00
|
|
|
void HeightMapShape3D::set_map_data(Vector<real_t> p_new) {
|
2019-03-17 09:47:19 +00:00
|
|
|
int size = (map_width * map_depth);
|
|
|
|
if (p_new.size() != size) {
|
|
|
|
// fail
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy
|
2021-08-09 22:15:17 +00:00
|
|
|
real_t *w = map_data.ptrw();
|
|
|
|
const real_t *r = p_new.ptr();
|
2019-03-17 09:47:19 +00:00
|
|
|
for (int i = 0; i < size; i++) {
|
2021-08-09 22:15:17 +00:00
|
|
|
real_t val = r[i];
|
2019-03-17 09:47:19 +00:00
|
|
|
w[i] = val;
|
|
|
|
if (i == 0) {
|
|
|
|
min_height = val;
|
|
|
|
max_height = val;
|
|
|
|
} else {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (min_height > val) {
|
2019-03-17 09:47:19 +00:00
|
|
|
min_height = val;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-03-17 09:47:19 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (max_height < val) {
|
2019-03-17 09:47:19 +00:00
|
|
|
max_height = val;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-03-17 09:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_update_shape();
|
2023-07-03 19:29:37 +00:00
|
|
|
emit_changed();
|
2019-03-17 09:47:19 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 22:15:17 +00:00
|
|
|
Vector<real_t> HeightMapShape3D::get_map_data() const {
|
2019-03-17 09:47:19 +00:00
|
|
|
return map_data;
|
|
|
|
}
|
|
|
|
|
2024-02-02 22:07:53 +00:00
|
|
|
real_t HeightMapShape3D::get_min_height() const {
|
|
|
|
return min_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t HeightMapShape3D::get_max_height() const {
|
|
|
|
return max_height;
|
|
|
|
}
|
|
|
|
|
2024-02-02 23:33:11 +00:00
|
|
|
void HeightMapShape3D::update_map_data_from_image(const Ref<Image> &p_image, real_t p_height_min, real_t p_height_max) {
|
|
|
|
ERR_FAIL_COND_MSG(p_image.is_null(), "Heightmap update image requires a valid Image reference.");
|
|
|
|
ERR_FAIL_COND_MSG(p_image->get_format() != Image::FORMAT_RF && p_image->get_format() != Image::FORMAT_RH && p_image->get_format() != Image::FORMAT_R8, "Heightmap update image requires Image in format FORMAT_RF (32 bit), FORMAT_RH (16 bit), or FORMAT_R8 (8 bit).");
|
|
|
|
ERR_FAIL_COND_MSG(p_image->get_width() < 2, "Heightmap update image requires a minimum Image width of 2.");
|
|
|
|
ERR_FAIL_COND_MSG(p_image->get_height() < 2, "Heightmap update image requires a minimum Image height of 2.");
|
|
|
|
ERR_FAIL_COND_MSG(p_height_min > p_height_max, "Heightmap update image requires height_max to be greater than height_min.");
|
|
|
|
|
|
|
|
map_width = p_image->get_width();
|
|
|
|
map_depth = p_image->get_height();
|
|
|
|
map_data.resize(map_width * map_depth);
|
|
|
|
|
|
|
|
real_t new_min_height = FLT_MAX;
|
|
|
|
real_t new_max_height = -FLT_MAX;
|
|
|
|
|
|
|
|
float remap_height_min = float(p_height_min);
|
|
|
|
float remap_height_max = float(p_height_max);
|
|
|
|
|
|
|
|
real_t *map_data_ptrw = map_data.ptrw();
|
|
|
|
|
|
|
|
switch (p_image->get_format()) {
|
|
|
|
case Image::FORMAT_RF: {
|
|
|
|
const float *image_data_ptr = (float *)p_image->get_data().ptr();
|
|
|
|
|
|
|
|
for (int i = 0; i < map_data.size(); i++) {
|
|
|
|
float pixel_value = image_data_ptr[i];
|
|
|
|
|
|
|
|
DEV_ASSERT(pixel_value >= 0.0 && pixel_value <= 1.0);
|
|
|
|
|
|
|
|
real_t height_value = Math::remap(pixel_value, 0.0f, 1.0f, remap_height_min, remap_height_max);
|
|
|
|
|
|
|
|
if (height_value < new_min_height) {
|
|
|
|
new_min_height = height_value;
|
|
|
|
}
|
|
|
|
if (height_value > new_max_height) {
|
|
|
|
new_max_height = height_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
map_data_ptrw[i] = height_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Image::FORMAT_RH: {
|
|
|
|
const uint16_t *image_data_ptr = (uint16_t *)p_image->get_data().ptr();
|
|
|
|
|
|
|
|
for (int i = 0; i < map_data.size(); i++) {
|
|
|
|
float pixel_value = Math::half_to_float(image_data_ptr[i]);
|
|
|
|
|
|
|
|
DEV_ASSERT(pixel_value >= 0.0 && pixel_value <= 1.0);
|
|
|
|
|
|
|
|
real_t height_value = Math::remap(pixel_value, 0.0f, 1.0f, remap_height_min, remap_height_max);
|
|
|
|
|
|
|
|
if (height_value < new_min_height) {
|
|
|
|
new_min_height = height_value;
|
|
|
|
}
|
|
|
|
if (height_value > new_max_height) {
|
|
|
|
new_max_height = height_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
map_data_ptrw[i] = height_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Image::FORMAT_R8: {
|
|
|
|
const uint8_t *image_data_ptr = (uint8_t *)p_image->get_data().ptr();
|
|
|
|
|
|
|
|
for (int i = 0; i < map_data.size(); i++) {
|
|
|
|
float pixel_value = float(image_data_ptr[i] / 255.0);
|
|
|
|
|
|
|
|
DEV_ASSERT(pixel_value >= 0.0 && pixel_value <= 1.0);
|
|
|
|
|
|
|
|
real_t height_value = Math::remap(pixel_value, 0.0f, 1.0f, remap_height_min, remap_height_max);
|
|
|
|
|
|
|
|
if (height_value < new_min_height) {
|
|
|
|
new_min_height = height_value;
|
|
|
|
}
|
|
|
|
if (height_value > new_max_height) {
|
|
|
|
new_max_height = height_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
map_data_ptrw[i] = height_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
min_height = new_min_height;
|
|
|
|
max_height = new_max_height;
|
|
|
|
|
|
|
|
_update_shape();
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2020-03-27 07:44:44 +00:00
|
|
|
void HeightMapShape3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_map_width", "width"), &HeightMapShape3D::set_map_width);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_map_width"), &HeightMapShape3D::get_map_width);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_map_depth", "height"), &HeightMapShape3D::set_map_depth);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_map_depth"), &HeightMapShape3D::get_map_depth);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_map_data", "data"), &HeightMapShape3D::set_map_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_map_data"), &HeightMapShape3D::get_map_data);
|
2024-02-02 22:07:53 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_min_height"), &HeightMapShape3D::get_min_height);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_max_height"), &HeightMapShape3D::get_max_height);
|
2019-03-17 09:47:19 +00:00
|
|
|
|
2024-02-02 23:33:11 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("update_map_data_from_image", "image", "height_min", "height_max"), &HeightMapShape3D::update_map_data_from_image);
|
|
|
|
|
2022-03-26 15:20:30 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "map_width", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater"), "set_map_width", "get_map_width");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "map_depth", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater"), "set_map_depth", "get_map_depth");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "map_data"), "set_map_data", "get_map_data");
|
2019-03-17 09:47:19 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 07:44:44 +00:00
|
|
|
HeightMapShape3D::HeightMapShape3D() :
|
2020-03-27 18:21:27 +00:00
|
|
|
Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_HEIGHTMAP)) {
|
2019-03-17 09:47:19 +00:00
|
|
|
map_data.resize(map_width * map_depth);
|
2021-08-09 22:15:17 +00:00
|
|
|
real_t *w = map_data.ptrw();
|
2019-03-17 09:47:19 +00:00
|
|
|
w[0] = 0.0;
|
|
|
|
w[1] = 0.0;
|
|
|
|
w[2] = 0.0;
|
|
|
|
w[3] = 0.0;
|
|
|
|
|
|
|
|
_update_shape();
|
|
|
|
}
|