diff options
Diffstat (limited to 'scene/resources')
37 files changed, 3803 insertions, 658 deletions
diff --git a/scene/resources/animation.cpp b/scene/resources/animation.cpp index b4eec2530b..06ce993cc7 100644 --- a/scene/resources/animation.cpp +++ b/scene/resources/animation.cpp @@ -30,21 +30,54 @@ #include "animation.h" +#include "core/io/marshalls.h" #include "core/math/geometry_3d.h" #include "scene/scene_string_names.h" bool Animation::_set(const StringName &p_name, const Variant &p_value) { String name = p_name; - if (name.begins_with("tracks/")) { + if (p_name == SNAME("_compression")) { + ERR_FAIL_COND_V(tracks.size() > 0, false); //can only set compression if no tracks exist + Dictionary comp = p_value; + ERR_FAIL_COND_V(!comp.has("fps"), false); + ERR_FAIL_COND_V(!comp.has("bounds"), false); + ERR_FAIL_COND_V(!comp.has("pages"), false); + ERR_FAIL_COND_V(!comp.has("format_version"), false); + uint32_t format_version = comp["format_version"]; + ERR_FAIL_COND_V(format_version > Compression::FORMAT_VERSION, false); // version does not match this supported version + compression.fps = comp["fps"]; + Array bounds = comp["bounds"]; + compression.bounds.resize(bounds.size()); + for (int i = 0; i < bounds.size(); i++) { + compression.bounds[i] = bounds[i]; + } + Array pages = comp["pages"]; + compression.pages.resize(pages.size()); + for (int i = 0; i < pages.size(); i++) { + Dictionary page = pages[i]; + ERR_FAIL_COND_V(!page.has("data"), false); + ERR_FAIL_COND_V(!page.has("time_offset"), false); + compression.pages[i].data = page["data"]; + compression.pages[i].time_offset = page["time_offset"]; + } + compression.enabled = true; + return true; + } else if (name.begins_with("tracks/")) { int track = name.get_slicec('/', 1).to_int(); String what = name.get_slicec('/', 2); if (tracks.size() == track && what == "type") { String type = p_value; - if (type == "transform" || type == "transform3d") { - add_track(TYPE_TRANSFORM3D); + if (type == "position_3d") { + add_track(TYPE_POSITION_3D); + } else if (type == "rotation_3d") { + add_track(TYPE_ROTATION_3D); + } else if (type == "scale_3d") { + add_track(TYPE_SCALE_3D); + } else if (type == "blend_shape") { + add_track(TYPE_BLEND_SHAPE); } else if (type == "value") { add_track(TYPE_VALUE); } else if (type == "method") { @@ -66,6 +99,34 @@ bool Animation::_set(const StringName &p_name, const Variant &p_value) { if (what == "path") { track_set_path(track, p_value); + } else if (what == "compressed_track") { + int index = p_value; + ERR_FAIL_COND_V(!compression.enabled, false); + ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)index, compression.bounds.size(), false); + Track *t = tracks[track]; + t->interpolation = INTERPOLATION_LINEAR; //only linear supported + switch (t->type) { + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + tt->compressed_track = index; + } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = static_cast<RotationTrack *>(t); + rt->compressed_track = index; + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = static_cast<ScaleTrack *>(t); + st->compressed_track = index; + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + bst->compressed_track = index; + } break; + default: { + return false; + } + } + return true; } else if (what == "interp") { track_set_interpolation_type(track, InterpolationType(p_value.operator int())); } else if (what == "loop_wrap") { @@ -75,35 +136,91 @@ bool Animation::_set(const StringName &p_name, const Variant &p_value) { } else if (what == "enabled") { track_set_enabled(track, p_value); } else if (what == "keys" || what == "key_values") { - if (track_get_type(track) == TYPE_TRANSFORM3D) { - TransformTrack *tt = static_cast<TransformTrack *>(tracks[track]); + if (track_get_type(track) == TYPE_POSITION_3D) { + PositionTrack *tt = static_cast<PositionTrack *>(tracks[track]); Vector<real_t> values = p_value; int vcount = values.size(); - ERR_FAIL_COND_V(vcount % TRANSFORM_TRACK_SIZE, false); + ERR_FAIL_COND_V(vcount % POSITION_TRACK_SIZE, false); const real_t *r = values.ptr(); - int64_t count = vcount / TRANSFORM_TRACK_SIZE; - tt->transforms.resize(count); + int64_t count = vcount / POSITION_TRACK_SIZE; + tt->positions.resize(count); + TKey<Vector3> *tw = tt->positions.ptrw(); for (int i = 0; i < count; i++) { - TKey<TransformKey> &tk = tt->transforms.write[i]; - const real_t *ofs = &r[i * TRANSFORM_TRACK_SIZE]; + TKey<Vector3> &tk = tw[i]; + const real_t *ofs = &r[i * POSITION_TRACK_SIZE]; tk.time = ofs[0]; tk.transition = ofs[1]; - tk.value.loc.x = ofs[2]; - tk.value.loc.y = ofs[3]; - tk.value.loc.z = ofs[4]; + tk.value.x = ofs[2]; + tk.value.y = ofs[3]; + tk.value.z = ofs[4]; + } + } else if (track_get_type(track) == TYPE_ROTATION_3D) { + RotationTrack *rt = static_cast<RotationTrack *>(tracks[track]); + Vector<real_t> values = p_value; + int vcount = values.size(); + ERR_FAIL_COND_V(vcount % ROTATION_TRACK_SIZE, false); + + const real_t *r = values.ptr(); - tk.value.rot.x = ofs[5]; - tk.value.rot.y = ofs[6]; - tk.value.rot.z = ofs[7]; - tk.value.rot.w = ofs[8]; + int64_t count = vcount / ROTATION_TRACK_SIZE; + rt->rotations.resize(count); - tk.value.scale.x = ofs[9]; - tk.value.scale.y = ofs[10]; - tk.value.scale.z = ofs[11]; + TKey<Quaternion> *rw = rt->rotations.ptrw(); + for (int i = 0; i < count; i++) { + TKey<Quaternion> &rk = rw[i]; + const real_t *ofs = &r[i * ROTATION_TRACK_SIZE]; + rk.time = ofs[0]; + rk.transition = ofs[1]; + + rk.value.x = ofs[2]; + rk.value.y = ofs[3]; + rk.value.z = ofs[4]; + rk.value.w = ofs[5]; + } + } else if (track_get_type(track) == TYPE_SCALE_3D) { + ScaleTrack *st = static_cast<ScaleTrack *>(tracks[track]); + Vector<real_t> values = p_value; + int vcount = values.size(); + ERR_FAIL_COND_V(vcount % SCALE_TRACK_SIZE, false); + + const real_t *r = values.ptr(); + + int64_t count = vcount / SCALE_TRACK_SIZE; + st->scales.resize(count); + + TKey<Vector3> *sw = st->scales.ptrw(); + for (int i = 0; i < count; i++) { + TKey<Vector3> &sk = sw[i]; + const real_t *ofs = &r[i * SCALE_TRACK_SIZE]; + sk.time = ofs[0]; + sk.transition = ofs[1]; + + sk.value.x = ofs[2]; + sk.value.y = ofs[3]; + sk.value.z = ofs[4]; + } + } else if (track_get_type(track) == TYPE_BLEND_SHAPE) { + BlendShapeTrack *st = static_cast<BlendShapeTrack *>(tracks[track]); + Vector<real_t> values = p_value; + int vcount = values.size(); + ERR_FAIL_COND_V(vcount % BLEND_SHAPE_TRACK_SIZE, false); + + const real_t *r = values.ptr(); + + int64_t count = vcount / BLEND_SHAPE_TRACK_SIZE; + st->blend_shapes.resize(count); + + TKey<float> *sw = st->blend_shapes.ptrw(); + for (int i = 0; i < count; i++) { + TKey<float> &sk = sw[i]; + const real_t *ofs = &r[i * BLEND_SHAPE_TRACK_SIZE]; + sk.time = ofs[0]; + sk.transition = ofs[1]; + sk.value = ofs[2]; } } else if (track_get_type(track) == TYPE_VALUE) { @@ -307,7 +424,30 @@ bool Animation::_set(const StringName &p_name, const Variant &p_value) { bool Animation::_get(const StringName &p_name, Variant &r_ret) const { String name = p_name; - if (name == "length") { + if (p_name == SNAME("_compression")) { + ERR_FAIL_COND_V(!compression.enabled, false); + Dictionary comp; + comp["fps"] = compression.fps; + Array bounds; + bounds.resize(compression.bounds.size()); + for (uint32_t i = 0; i < compression.bounds.size(); i++) { + bounds[i] = compression.bounds[i]; + } + comp["bounds"] = bounds; + Array pages; + pages.resize(compression.pages.size()); + for (uint32_t i = 0; i < compression.pages.size(); i++) { + Dictionary page; + page["data"] = compression.pages[i].data; + page["time_offset"] = compression.pages[i].time_offset; + pages[i] = page; + } + comp["pages"] = pages; + comp["format_version"] = Compression::FORMAT_VERSION; + + r_ret = comp; + return true; + } else if (name == "length") { r_ret = length; } else if (name == "loop") { r_ret = loop; @@ -319,8 +459,17 @@ bool Animation::_get(const StringName &p_name, Variant &r_ret) const { ERR_FAIL_INDEX_V(track, tracks.size(), false); if (what == "type") { switch (track_get_type(track)) { - case TYPE_TRANSFORM3D: - r_ret = "transform"; + case TYPE_POSITION_3D: + r_ret = "position_3d"; + break; + case TYPE_ROTATION_3D: + r_ret = "rotation_3d"; + break; + case TYPE_SCALE_3D: + r_ret = "scale_3d"; + break; + case TYPE_BLEND_SHAPE: + r_ret = "blend_shape"; break; case TYPE_VALUE: r_ret = "value"; @@ -343,6 +492,34 @@ bool Animation::_get(const StringName &p_name, Variant &r_ret) const { } else if (what == "path") { r_ret = track_get_path(track); + } else if (what == "compressed_track") { + ERR_FAIL_COND_V(!compression.enabled, false); + Track *t = tracks[track]; + switch (t->type) { + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + r_ret = tt->compressed_track; + } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = static_cast<RotationTrack *>(t); + r_ret = rt->compressed_track; + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = static_cast<ScaleTrack *>(t); + r_ret = st->compressed_track; + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + r_ret = bst->compressed_track; + } break; + default: { + r_ret = Variant(); + ERR_FAIL_V(false); + } + } + + return true; + } else if (what == "interp") { r_ret = track_get_interpolation_type(track); } else if (what == "loop_wrap") { @@ -352,31 +529,64 @@ bool Animation::_get(const StringName &p_name, Variant &r_ret) const { } else if (what == "enabled") { r_ret = track_is_enabled(track); } else if (what == "keys") { - if (track_get_type(track) == TYPE_TRANSFORM3D) { + if (track_get_type(track) == TYPE_POSITION_3D) { Vector<real_t> keys; int kk = track_get_key_count(track); - keys.resize(kk * TRANSFORM_TRACK_SIZE); + keys.resize(kk * POSITION_TRACK_SIZE); real_t *w = keys.ptrw(); int idx = 0; for (int i = 0; i < track_get_key_count(track); i++) { Vector3 loc; - Quaternion rot; - Vector3 scale; - transform_track_get_key(track, i, &loc, &rot, &scale); + position_track_get_key(track, i, &loc); w[idx++] = track_get_key_time(track, i); w[idx++] = track_get_key_transition(track, i); w[idx++] = loc.x; w[idx++] = loc.y; w[idx++] = loc.z; + } + + r_ret = keys; + return true; + } else if (track_get_type(track) == TYPE_ROTATION_3D) { + Vector<real_t> keys; + int kk = track_get_key_count(track); + keys.resize(kk * ROTATION_TRACK_SIZE); + + real_t *w = keys.ptrw(); + int idx = 0; + for (int i = 0; i < track_get_key_count(track); i++) { + Quaternion rot; + rotation_track_get_key(track, i, &rot); + + w[idx++] = track_get_key_time(track, i); + w[idx++] = track_get_key_transition(track, i); w[idx++] = rot.x; w[idx++] = rot.y; w[idx++] = rot.z; w[idx++] = rot.w; + } + + r_ret = keys; + return true; + + } else if (track_get_type(track) == TYPE_SCALE_3D) { + Vector<real_t> keys; + int kk = track_get_key_count(track); + keys.resize(kk * SCALE_TRACK_SIZE); + + real_t *w = keys.ptrw(); + int idx = 0; + for (int i = 0; i < track_get_key_count(track); i++) { + Vector3 scale; + scale_track_get_key(track, i, &scale); + + w[idx++] = track_get_key_time(track, i); + w[idx++] = track_get_key_transition(track, i); w[idx++] = scale.x; w[idx++] = scale.y; w[idx++] = scale.z; @@ -384,7 +594,25 @@ bool Animation::_get(const StringName &p_name, Variant &r_ret) const { r_ret = keys; return true; + } else if (track_get_type(track) == TYPE_BLEND_SHAPE) { + Vector<real_t> keys; + int kk = track_get_key_count(track); + keys.resize(kk * BLEND_SHAPE_TRACK_SIZE); + real_t *w = keys.ptrw(); + + int idx = 0; + for (int i = 0; i < track_get_key_count(track); i++) { + float bs; + blend_shape_track_get_key(track, i, &bs); + + w[idx++] = track_get_key_time(track, i); + w[idx++] = track_get_key_transition(track, i); + w[idx++] = bs; + } + + r_ret = keys; + return true; } else if (track_get_type(track) == TYPE_VALUE) { const ValueTrack *vt = static_cast<const ValueTrack *>(tracks[track]); @@ -570,14 +798,21 @@ bool Animation::_get(const StringName &p_name, Variant &r_ret) const { } void Animation::_get_property_list(List<PropertyInfo> *p_list) const { + if (compression.enabled) { + p_list->push_back(PropertyInfo(Variant::DICTIONARY, "_compression", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); + } for (int i = 0; i < tracks.size(); i++) { p_list->push_back(PropertyInfo(Variant::STRING, "tracks/" + itos(i) + "/type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); - p_list->push_back(PropertyInfo(Variant::NODE_PATH, "tracks/" + itos(i) + "/path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); - p_list->push_back(PropertyInfo(Variant::INT, "tracks/" + itos(i) + "/interp", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); - p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/loop_wrap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/imported", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); - p_list->push_back(PropertyInfo(Variant::ARRAY, "tracks/" + itos(i) + "/keys", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); + p_list->push_back(PropertyInfo(Variant::NODE_PATH, "tracks/" + itos(i) + "/path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); + if (track_is_compressed(i)) { + p_list->push_back(PropertyInfo(Variant::INT, "tracks/" + itos(i) + "/compressed_track", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); + } else { + p_list->push_back(PropertyInfo(Variant::INT, "tracks/" + itos(i) + "/interp", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); + p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/loop_wrap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); + p_list->push_back(PropertyInfo(Variant::ARRAY, "tracks/" + itos(i) + "/keys", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); + } } } @@ -591,10 +826,22 @@ int Animation::add_track(TrackType p_type, int p_at_pos) { } switch (p_type) { - case TYPE_TRANSFORM3D: { - TransformTrack *tt = memnew(TransformTrack); + case TYPE_POSITION_3D: { + PositionTrack *tt = memnew(PositionTrack); tracks.insert(p_at_pos, tt); } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = memnew(RotationTrack); + tracks.insert(p_at_pos, rt); + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = memnew(ScaleTrack); + tracks.insert(p_at_pos, st); + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = memnew(BlendShapeTrack); + tracks.insert(p_at_pos, bst); + } break; case TYPE_VALUE: { tracks.insert(p_at_pos, memnew(ValueTrack)); @@ -629,9 +876,28 @@ void Animation::remove_track(int p_track) { Track *t = tracks[p_track]; switch (t->type) { - case TYPE_TRANSFORM3D: { - TransformTrack *tt = static_cast<TransformTrack *>(t); - _clear(tt->transforms); + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + ERR_FAIL_COND_MSG(tt->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first."); + _clear(tt->positions); + + } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = static_cast<RotationTrack *>(t); + ERR_FAIL_COND_MSG(rt->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first."); + _clear(rt->rotations); + + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = static_cast<ScaleTrack *>(t); + ERR_FAIL_COND_MSG(st->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first."); + _clear(st->scales); + + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + ERR_FAIL_COND_MSG(bst->compressed_track >= 0, "Compressed tracks can't be manually removed. Call clear() to get rid of compression first."); + _clear(bst->blend_shapes); } break; case TYPE_VALUE: { @@ -672,7 +938,7 @@ int Animation::get_track_count() const { } Animation::TrackType Animation::track_get_type(int p_track) const { - ERR_FAIL_INDEX_V(p_track, tracks.size(), TYPE_TRANSFORM3D); + ERR_FAIL_INDEX_V(p_track, tracks.size(), TYPE_VALUE); return tracks[p_track]->type; } @@ -688,9 +954,9 @@ NodePath Animation::track_get_path(int p_track) const { return tracks[p_track]->path; } -int Animation::find_track(const NodePath &p_path) const { +int Animation::find_track(const NodePath &p_path, const TrackType p_type) const { for (int i = 0; i < tracks.size(); i++) { - if (tracks[i]->path == p_path) { + if (tracks[i]->path == p_path && tracks[i]->type == p_type) { return i; } }; @@ -720,31 +986,6 @@ bool Animation::track_get_interpolation_loop_wrap(int p_track) const { return tracks[p_track]->loop_wrap; } -// transform -/* -template<class T> -int Animation::_insert_pos(double p_time, T& p_keys) { - // simple, linear time inset that should be fast enough in reality. - - int idx=p_keys.size(); - - while(true) { - - - if (idx==0 || p_keys[idx-1].time < p_time) { - //condition for insertion. - p_keys.insert(idx,T()); - return idx; - } else if (p_keys[idx-1].time == p_time) { - // condition for replacing. - return idx-1; - } - - idx--; - } -} - -*/ template <class T, class V> int Animation::_insert(double p_time, T &p_keys, const V &p_value) { int idx = p_keys.size(); @@ -774,45 +1015,292 @@ void Animation::_clear(T &p_keys) { p_keys.clear(); } -Error Animation::transform_track_get_key(int p_track, int p_key, Vector3 *r_loc, Quaternion *r_rot, Vector3 *r_scale) const { +//// + +int Animation::position_track_insert_key(int p_track, double p_time, const Vector3 &p_position) { + ERR_FAIL_INDEX_V(p_track, tracks.size(), -1); + Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_POSITION_3D, -1); + + PositionTrack *tt = static_cast<PositionTrack *>(t); + + ERR_FAIL_COND_V(tt->compressed_track >= 0, -1); + + TKey<Vector3> tkey; + tkey.time = p_time; + tkey.value = p_position; + + int ret = _insert(p_time, tt->positions, tkey); + emit_changed(); + return ret; +} + +Error Animation::position_track_get_key(int p_track, int p_key, Vector3 *r_position) const { + ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER); + Track *t = tracks[p_track]; + + PositionTrack *tt = static_cast<PositionTrack *>(t); + ERR_FAIL_COND_V(t->type != TYPE_POSITION_3D, ERR_INVALID_PARAMETER); + + if (tt->compressed_track >= 0) { + Vector3i key; + double time; + bool fetch_success = _fetch_compressed_by_index<3>(tt->compressed_track, p_key, key, time); + if (!fetch_success) { + return ERR_INVALID_PARAMETER; + } + + *r_position = _uncompress_pos_scale(tt->compressed_track, key); + return OK; + } + + ERR_FAIL_INDEX_V(p_key, tt->positions.size(), ERR_INVALID_PARAMETER); + + *r_position = tt->positions[p_key].value; + + return OK; +} + +Error Animation::position_track_interpolate(int p_track, double p_time, Vector3 *r_interpolation) const { + ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER); + Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_POSITION_3D, ERR_INVALID_PARAMETER); + + PositionTrack *tt = static_cast<PositionTrack *>(t); + + if (tt->compressed_track >= 0) { + if (_pos_scale_interpolate_compressed(tt->compressed_track, p_time, *r_interpolation)) { + return OK; + } else { + return ERR_UNAVAILABLE; + } + } + + bool ok = false; + + Vector3 tk = _interpolate(tt->positions, p_time, tt->interpolation, tt->loop_wrap, &ok); + + if (!ok) { + return ERR_UNAVAILABLE; + } + *r_interpolation = tk; + return OK; +} + +//// + +int Animation::rotation_track_insert_key(int p_track, double p_time, const Quaternion &p_rotation) { + ERR_FAIL_INDEX_V(p_track, tracks.size(), -1); + Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_ROTATION_3D, -1); + + RotationTrack *rt = static_cast<RotationTrack *>(t); + + ERR_FAIL_COND_V(rt->compressed_track >= 0, -1); + + TKey<Quaternion> tkey; + tkey.time = p_time; + tkey.value = p_rotation; + + int ret = _insert(p_time, rt->rotations, tkey); + emit_changed(); + return ret; +} + +Error Animation::rotation_track_get_key(int p_track, int p_key, Quaternion *r_rotation) const { + ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER); + Track *t = tracks[p_track]; + + RotationTrack *rt = static_cast<RotationTrack *>(t); + ERR_FAIL_COND_V(t->type != TYPE_ROTATION_3D, ERR_INVALID_PARAMETER); + + if (rt->compressed_track >= 0) { + Vector3i key; + double time; + bool fetch_success = _fetch_compressed_by_index<3>(rt->compressed_track, p_key, key, time); + if (!fetch_success) { + return ERR_INVALID_PARAMETER; + } + + *r_rotation = _uncompress_quaternion(key); + return OK; + } + + ERR_FAIL_INDEX_V(p_key, rt->rotations.size(), ERR_INVALID_PARAMETER); + + *r_rotation = rt->rotations[p_key].value; + + return OK; +} + +Error Animation::rotation_track_interpolate(int p_track, double p_time, Quaternion *r_interpolation) const { ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER); Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_ROTATION_3D, ERR_INVALID_PARAMETER); - TransformTrack *tt = static_cast<TransformTrack *>(t); - ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM3D, ERR_INVALID_PARAMETER); - ERR_FAIL_INDEX_V(p_key, tt->transforms.size(), ERR_INVALID_PARAMETER); + RotationTrack *rt = static_cast<RotationTrack *>(t); - if (r_loc) { - *r_loc = tt->transforms[p_key].value.loc; + if (rt->compressed_track >= 0) { + if (_rotation_interpolate_compressed(rt->compressed_track, p_time, *r_interpolation)) { + return OK; + } else { + return ERR_UNAVAILABLE; + } } - if (r_rot) { - *r_rot = tt->transforms[p_key].value.rot; + + bool ok = false; + + Quaternion tk = _interpolate(rt->rotations, p_time, rt->interpolation, rt->loop_wrap, &ok); + + if (!ok) { + return ERR_UNAVAILABLE; } - if (r_scale) { - *r_scale = tt->transforms[p_key].value.scale; + *r_interpolation = tk; + return OK; +} + +//// + +int Animation::scale_track_insert_key(int p_track, double p_time, const Vector3 &p_scale) { + ERR_FAIL_INDEX_V(p_track, tracks.size(), -1); + Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_SCALE_3D, -1); + + ScaleTrack *st = static_cast<ScaleTrack *>(t); + + ERR_FAIL_COND_V(st->compressed_track >= 0, -1); + + TKey<Vector3> tkey; + tkey.time = p_time; + tkey.value = p_scale; + + int ret = _insert(p_time, st->scales, tkey); + emit_changed(); + return ret; +} + +Error Animation::scale_track_get_key(int p_track, int p_key, Vector3 *r_scale) const { + ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER); + Track *t = tracks[p_track]; + + ScaleTrack *st = static_cast<ScaleTrack *>(t); + ERR_FAIL_COND_V(t->type != TYPE_SCALE_3D, ERR_INVALID_PARAMETER); + + if (st->compressed_track >= 0) { + Vector3i key; + double time; + bool fetch_success = _fetch_compressed_by_index<3>(st->compressed_track, p_key, key, time); + if (!fetch_success) { + return ERR_INVALID_PARAMETER; + } + + *r_scale = _uncompress_pos_scale(st->compressed_track, key); + return OK; } + ERR_FAIL_INDEX_V(p_key, st->scales.size(), ERR_INVALID_PARAMETER); + + *r_scale = st->scales[p_key].value; + return OK; } -int Animation::transform_track_insert_key(int p_track, double p_time, const Vector3 &p_loc, const Quaternion &p_rot, const Vector3 &p_scale) { +Error Animation::scale_track_interpolate(int p_track, double p_time, Vector3 *r_interpolation) const { + ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER); + Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_SCALE_3D, ERR_INVALID_PARAMETER); + + ScaleTrack *st = static_cast<ScaleTrack *>(t); + + if (st->compressed_track >= 0) { + if (_pos_scale_interpolate_compressed(st->compressed_track, p_time, *r_interpolation)) { + return OK; + } else { + return ERR_UNAVAILABLE; + } + } + + bool ok = false; + + Vector3 tk = _interpolate(st->scales, p_time, st->interpolation, st->loop_wrap, &ok); + + if (!ok) { + return ERR_UNAVAILABLE; + } + *r_interpolation = tk; + return OK; +} + +int Animation::blend_shape_track_insert_key(int p_track, double p_time, float p_blend_shape) { ERR_FAIL_INDEX_V(p_track, tracks.size(), -1); Track *t = tracks[p_track]; - ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM3D, -1); + ERR_FAIL_COND_V(t->type != TYPE_BLEND_SHAPE, -1); + + BlendShapeTrack *st = static_cast<BlendShapeTrack *>(t); - TransformTrack *tt = static_cast<TransformTrack *>(t); + ERR_FAIL_COND_V(st->compressed_track >= 0, -1); - TKey<TransformKey> tkey; + TKey<float> tkey; tkey.time = p_time; - tkey.value.loc = p_loc; - tkey.value.rot = p_rot; - tkey.value.scale = p_scale; + tkey.value = p_blend_shape; - int ret = _insert(p_time, tt->transforms, tkey); + int ret = _insert(p_time, st->blend_shapes, tkey); emit_changed(); return ret; } +Error Animation::blend_shape_track_get_key(int p_track, int p_key, float *r_blend_shape) const { + ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER); + Track *t = tracks[p_track]; + + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + ERR_FAIL_COND_V(t->type != TYPE_BLEND_SHAPE, ERR_INVALID_PARAMETER); + + if (bst->compressed_track >= 0) { + Vector3i key; + double time; + bool fetch_success = _fetch_compressed_by_index<1>(bst->compressed_track, p_key, key, time); + if (!fetch_success) { + return ERR_INVALID_PARAMETER; + } + + *r_blend_shape = _uncompress_blend_shape(key); + return OK; + } + + ERR_FAIL_INDEX_V(p_key, bst->blend_shapes.size(), ERR_INVALID_PARAMETER); + + *r_blend_shape = bst->blend_shapes[p_key].value; + + return OK; +} + +Error Animation::blend_shape_track_interpolate(int p_track, double p_time, float *r_interpolation) const { + ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER); + Track *t = tracks[p_track]; + ERR_FAIL_COND_V(t->type != TYPE_BLEND_SHAPE, ERR_INVALID_PARAMETER); + + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + + if (bst->compressed_track >= 0) { + if (_blend_shape_interpolate_compressed(bst->compressed_track, p_time, *r_interpolation)) { + return OK; + } else { + return ERR_UNAVAILABLE; + } + } + + bool ok = false; + + float tk = _interpolate(bst->blend_shapes, p_time, bst->interpolation, bst->loop_wrap, &ok); + + if (!ok) { + return ERR_UNAVAILABLE; + } + *r_interpolation = tk; + return OK; +} + void Animation::track_remove_key_at_time(int p_track, double p_time) { int idx = track_find_key(p_track, p_time, true); ERR_FAIL_COND(idx < 0); @@ -824,10 +1312,40 @@ void Animation::track_remove_key(int p_track, int p_idx) { Track *t = tracks[p_track]; switch (t->type) { - case TYPE_TRANSFORM3D: { - TransformTrack *tt = static_cast<TransformTrack *>(t); - ERR_FAIL_INDEX(p_idx, tt->transforms.size()); - tt->transforms.remove(p_idx); + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + + ERR_FAIL_COND(tt->compressed_track >= 0); + + ERR_FAIL_INDEX(p_idx, tt->positions.size()); + tt->positions.remove(p_idx); + + } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = static_cast<RotationTrack *>(t); + + ERR_FAIL_COND(rt->compressed_track >= 0); + + ERR_FAIL_INDEX(p_idx, rt->rotations.size()); + rt->rotations.remove(p_idx); + + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = static_cast<ScaleTrack *>(t); + + ERR_FAIL_COND(st->compressed_track >= 0); + + ERR_FAIL_INDEX(p_idx, st->scales.size()); + st->scales.remove(p_idx); + + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + + ERR_FAIL_COND(bst->compressed_track >= 0); + + ERR_FAIL_INDEX(p_idx, bst->blend_shapes.size()); + bst->blend_shapes.remove(p_idx); } break; case TYPE_VALUE: { @@ -870,13 +1388,109 @@ int Animation::track_find_key(int p_track, double p_time, bool p_exact) const { Track *t = tracks[p_track]; switch (t->type) { - case TYPE_TRANSFORM3D: { - TransformTrack *tt = static_cast<TransformTrack *>(t); - int k = _find(tt->transforms, p_time); - if (k < 0 || k >= tt->transforms.size()) { + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + + if (tt->compressed_track >= 0) { + double time; + double time_next; + Vector3i key; + Vector3i key_next; + uint32_t key_index; + bool fetch_compressed_success = _fetch_compressed<3>(tt->compressed_track, p_time, key, time, key_next, time_next, &key_index); + ERR_FAIL_COND_V(!fetch_compressed_success, -1); + if (p_exact && time != p_time) { + return -1; + } + return key_index; + } + + int k = _find(tt->positions, p_time); + if (k < 0 || k >= tt->positions.size()) { + return -1; + } + if (tt->positions[k].time != p_time && p_exact) { + return -1; + } + return k; + + } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = static_cast<RotationTrack *>(t); + + if (rt->compressed_track >= 0) { + double time; + double time_next; + Vector3i key; + Vector3i key_next; + uint32_t key_index; + bool fetch_compressed_success = _fetch_compressed<3>(rt->compressed_track, p_time, key, time, key_next, time_next, &key_index); + ERR_FAIL_COND_V(!fetch_compressed_success, -1); + if (p_exact && time != p_time) { + return -1; + } + return key_index; + } + + int k = _find(rt->rotations, p_time); + if (k < 0 || k >= rt->rotations.size()) { + return -1; + } + if (rt->rotations[k].time != p_time && p_exact) { + return -1; + } + return k; + + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = static_cast<ScaleTrack *>(t); + + if (st->compressed_track >= 0) { + double time; + double time_next; + Vector3i key; + Vector3i key_next; + uint32_t key_index; + bool fetch_compressed_success = _fetch_compressed<3>(st->compressed_track, p_time, key, time, key_next, time_next, &key_index); + ERR_FAIL_COND_V(!fetch_compressed_success, -1); + if (p_exact && time != p_time) { + return -1; + } + return key_index; + } + + int k = _find(st->scales, p_time); + if (k < 0 || k >= st->scales.size()) { + return -1; + } + if (st->scales[k].time != p_time && p_exact) { + return -1; + } + return k; + + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + + if (bst->compressed_track >= 0) { + double time; + double time_next; + Vector3i key; + Vector3i key_next; + uint32_t key_index; + bool fetch_compressed_success = _fetch_compressed<1>(bst->compressed_track, p_time, key, time, key_next, time_next, &key_index); + ERR_FAIL_COND_V(!fetch_compressed_success, -1); + if (p_exact && time != p_time) { + return -1; + } + return key_index; + } + + int k = _find(bst->blend_shapes, p_time); + if (k < 0 || k >= bst->blend_shapes.size()) { return -1; } - if (tt->transforms[k].time != p_time && p_exact) { + if (bst->blend_shapes[k].time != p_time && p_exact) { return -1; } return k; @@ -952,24 +1566,27 @@ void Animation::track_insert_key(int p_track, double p_time, const Variant &p_ke Track *t = tracks[p_track]; switch (t->type) { - case TYPE_TRANSFORM3D: { - Dictionary d = p_key; - Vector3 loc; - if (d.has("location")) { - loc = d["location"]; - } + case TYPE_POSITION_3D: { + ERR_FAIL_COND((p_key.get_type() != Variant::VECTOR3) && (p_key.get_type() != Variant::VECTOR3I)); + int idx = position_track_insert_key(p_track, p_time, p_key); + track_set_key_transition(p_track, idx, p_transition); - Quaternion rot; - if (d.has("rotation")) { - rot = d["rotation"]; - } + } break; + case TYPE_ROTATION_3D: { + ERR_FAIL_COND((p_key.get_type() != Variant::QUATERNION) && (p_key.get_type() != Variant::BASIS)); + int idx = rotation_track_insert_key(p_track, p_time, p_key); + track_set_key_transition(p_track, idx, p_transition); - Vector3 scale; - if (d.has("scale")) { - scale = d["scale"]; - } + } break; + case TYPE_SCALE_3D: { + ERR_FAIL_COND((p_key.get_type() != Variant::VECTOR3) && (p_key.get_type() != Variant::VECTOR3I)); + int idx = scale_track_insert_key(p_track, p_time, p_key); + track_set_key_transition(p_track, idx, p_transition); - int idx = transform_track_insert_key(p_track, p_time, loc, rot, scale); + } break; + case TYPE_BLEND_SHAPE: { + ERR_FAIL_COND((p_key.get_type() != Variant::FLOAT) && (p_key.get_type() != Variant::INT)); + int idx = blend_shape_track_insert_key(p_track, p_time, p_key); track_set_key_transition(p_track, idx, p_transition); } break; @@ -1054,9 +1671,33 @@ int Animation::track_get_key_count(int p_track) const { Track *t = tracks[p_track]; switch (t->type) { - case TYPE_TRANSFORM3D: { - TransformTrack *tt = static_cast<TransformTrack *>(t); - return tt->transforms.size(); + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + if (tt->compressed_track >= 0) { + return _get_compressed_key_count(tt->compressed_track); + } + return tt->positions.size(); + } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = static_cast<RotationTrack *>(t); + if (rt->compressed_track >= 0) { + return _get_compressed_key_count(rt->compressed_track); + } + return rt->rotations.size(); + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = static_cast<ScaleTrack *>(t); + if (st->compressed_track >= 0) { + return _get_compressed_key_count(st->compressed_track); + } + return st->scales.size(); + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + if (bst->compressed_track >= 0) { + return _get_compressed_key_count(bst->compressed_track); + } + return bst->blend_shapes.size(); } break; case TYPE_VALUE: { ValueTrack *vt = static_cast<ValueTrack *>(t); @@ -1089,16 +1730,25 @@ Variant Animation::track_get_key_value(int p_track, int p_key_idx) const { Track *t = tracks[p_track]; switch (t->type) { - case TYPE_TRANSFORM3D: { - TransformTrack *tt = static_cast<TransformTrack *>(t); - ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), Variant()); - - Dictionary d; - d["location"] = tt->transforms[p_key_idx].value.loc; - d["rotation"] = tt->transforms[p_key_idx].value.rot; - d["scale"] = tt->transforms[p_key_idx].value.scale; - - return d; + case TYPE_POSITION_3D: { + Vector3 value; + position_track_get_key(p_track, p_key_idx, &value); + return value; + } break; + case TYPE_ROTATION_3D: { + Quaternion value; + rotation_track_get_key(p_track, p_key_idx, &value); + return value; + } break; + case TYPE_SCALE_3D: { + Vector3 value; + scale_track_get_key(p_track, p_key_idx, &value); + return value; + } break; + case TYPE_BLEND_SHAPE: { + float value; + blend_shape_track_get_key(p_track, p_key_idx, &value); + return value; } break; case TYPE_VALUE: { ValueTrack *vt = static_cast<ValueTrack *>(t); @@ -1157,10 +1807,53 @@ double Animation::track_get_key_time(int p_track, int p_key_idx) const { Track *t = tracks[p_track]; switch (t->type) { - case TYPE_TRANSFORM3D: { - TransformTrack *tt = static_cast<TransformTrack *>(t); - ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), -1); - return tt->transforms[p_key_idx].time; + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + if (tt->compressed_track >= 0) { + Vector3i value; + double time; + bool fetch_compressed_success = _fetch_compressed_by_index<3>(tt->compressed_track, p_key_idx, value, time); + ERR_FAIL_COND_V(!fetch_compressed_success, false); + return time; + } + ERR_FAIL_INDEX_V(p_key_idx, tt->positions.size(), -1); + return tt->positions[p_key_idx].time; + } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = static_cast<RotationTrack *>(t); + if (rt->compressed_track >= 0) { + Vector3i value; + double time; + bool fetch_compressed_success = _fetch_compressed_by_index<3>(rt->compressed_track, p_key_idx, value, time); + ERR_FAIL_COND_V(!fetch_compressed_success, false); + return time; + } + ERR_FAIL_INDEX_V(p_key_idx, rt->rotations.size(), -1); + return rt->rotations[p_key_idx].time; + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = static_cast<ScaleTrack *>(t); + if (st->compressed_track >= 0) { + Vector3i value; + double time; + bool fetch_compressed_success = _fetch_compressed_by_index<3>(st->compressed_track, p_key_idx, value, time); + ERR_FAIL_COND_V(!fetch_compressed_success, false); + return time; + } + ERR_FAIL_INDEX_V(p_key_idx, st->scales.size(), -1); + return st->scales[p_key_idx].time; + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + if (bst->compressed_track >= 0) { + Vector3i value; + double time; + bool fetch_compressed_success = _fetch_compressed_by_index<1>(bst->compressed_track, p_key_idx, value, time); + ERR_FAIL_COND_V(!fetch_compressed_success, false); + return time; + } + ERR_FAIL_INDEX_V(p_key_idx, bst->blend_shapes.size(), -1); + return bst->blend_shapes[p_key_idx].time; } break; case TYPE_VALUE: { ValueTrack *vt = static_cast<ValueTrack *>(t); @@ -1202,13 +1895,44 @@ void Animation::track_set_key_time(int p_track, int p_key_idx, double p_time) { Track *t = tracks[p_track]; switch (t->type) { - case TYPE_TRANSFORM3D: { - TransformTrack *tt = static_cast<TransformTrack *>(t); - ERR_FAIL_INDEX(p_key_idx, tt->transforms.size()); - TKey<TransformKey> key = tt->transforms[p_key_idx]; + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + ERR_FAIL_COND(tt->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, tt->positions.size()); + TKey<Vector3> key = tt->positions[p_key_idx]; + key.time = p_time; + tt->positions.remove(p_key_idx); + _insert(p_time, tt->positions, key); + return; + } + case TYPE_ROTATION_3D: { + RotationTrack *tt = static_cast<RotationTrack *>(t); + ERR_FAIL_COND(tt->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, tt->rotations.size()); + TKey<Quaternion> key = tt->rotations[p_key_idx]; + key.time = p_time; + tt->rotations.remove(p_key_idx); + _insert(p_time, tt->rotations, key); + return; + } + case TYPE_SCALE_3D: { + ScaleTrack *tt = static_cast<ScaleTrack *>(t); + ERR_FAIL_COND(tt->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, tt->scales.size()); + TKey<Vector3> key = tt->scales[p_key_idx]; + key.time = p_time; + tt->scales.remove(p_key_idx); + _insert(p_time, tt->scales, key); + return; + } + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *tt = static_cast<BlendShapeTrack *>(t); + ERR_FAIL_COND(tt->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, tt->blend_shapes.size()); + TKey<float> key = tt->blend_shapes[p_key_idx]; key.time = p_time; - tt->transforms.remove(p_key_idx); - _insert(p_time, tt->transforms, key); + tt->blend_shapes.remove(p_key_idx); + _insert(p_time, tt->blend_shapes, key); return; } case TYPE_VALUE: { @@ -1266,10 +1990,37 @@ real_t Animation::track_get_key_transition(int p_track, int p_key_idx) const { Track *t = tracks[p_track]; switch (t->type) { - case TYPE_TRANSFORM3D: { - TransformTrack *tt = static_cast<TransformTrack *>(t); - ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), -1); - return tt->transforms[p_key_idx].transition; + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + if (tt->compressed_track >= 0) { + return 1.0; + } + ERR_FAIL_INDEX_V(p_key_idx, tt->positions.size(), -1); + return tt->positions[p_key_idx].transition; + } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = static_cast<RotationTrack *>(t); + if (rt->compressed_track >= 0) { + return 1.0; + } + ERR_FAIL_INDEX_V(p_key_idx, rt->rotations.size(), -1); + return rt->rotations[p_key_idx].transition; + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = static_cast<ScaleTrack *>(t); + if (st->compressed_track >= 0) { + return 1.0; + } + ERR_FAIL_INDEX_V(p_key_idx, st->scales.size(), -1); + return st->scales[p_key_idx].transition; + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + if (bst->compressed_track >= 0) { + return 1.0; + } + ERR_FAIL_INDEX_V(p_key_idx, bst->blend_shapes.size(), -1); + return bst->blend_shapes[p_key_idx].transition; } break; case TYPE_VALUE: { ValueTrack *vt = static_cast<ValueTrack *>(t); @@ -1297,26 +2048,74 @@ real_t Animation::track_get_key_transition(int p_track, int p_key_idx) const { ERR_FAIL_V(0); } +bool Animation::track_is_compressed(int p_track) const { + ERR_FAIL_INDEX_V(p_track, tracks.size(), false); + Track *t = tracks[p_track]; + + switch (t->type) { + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + return tt->compressed_track >= 0; + } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = static_cast<RotationTrack *>(t); + return rt->compressed_track >= 0; + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = static_cast<ScaleTrack *>(t); + return st->compressed_track >= 0; + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + return bst->compressed_track >= 0; + } break; + default: { + return false; //animation does not really use transitions + } break; + } + + ERR_FAIL_V(false); +} + void Animation::track_set_key_value(int p_track, int p_key_idx, const Variant &p_value) { ERR_FAIL_INDEX(p_track, tracks.size()); Track *t = tracks[p_track]; switch (t->type) { - case TYPE_TRANSFORM3D: { - TransformTrack *tt = static_cast<TransformTrack *>(t); - ERR_FAIL_INDEX(p_key_idx, tt->transforms.size()); + case TYPE_POSITION_3D: { + ERR_FAIL_COND((p_value.get_type() != Variant::VECTOR3) && (p_value.get_type() != Variant::VECTOR3I)); + PositionTrack *tt = static_cast<PositionTrack *>(t); + ERR_FAIL_COND(tt->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, tt->positions.size()); - Dictionary d = p_value; + tt->positions.write[p_key_idx].value = p_value; - if (d.has("location")) { - tt->transforms.write[p_key_idx].value.loc = d["location"]; - } - if (d.has("rotation")) { - tt->transforms.write[p_key_idx].value.rot = d["rotation"]; - } - if (d.has("scale")) { - tt->transforms.write[p_key_idx].value.scale = d["scale"]; - } + } break; + case TYPE_ROTATION_3D: { + ERR_FAIL_COND((p_value.get_type() != Variant::QUATERNION) && (p_value.get_type() != Variant::BASIS)); + RotationTrack *rt = static_cast<RotationTrack *>(t); + ERR_FAIL_COND(rt->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, rt->rotations.size()); + + rt->rotations.write[p_key_idx].value = p_value; + + } break; + case TYPE_SCALE_3D: { + ERR_FAIL_COND((p_value.get_type() != Variant::VECTOR3) && (p_value.get_type() != Variant::VECTOR3I)); + ScaleTrack *st = static_cast<ScaleTrack *>(t); + ERR_FAIL_COND(st->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, st->scales.size()); + + st->scales.write[p_key_idx].value = p_value; + + } break; + case TYPE_BLEND_SHAPE: { + ERR_FAIL_COND((p_value.get_type() != Variant::FLOAT) && (p_value.get_type() != Variant::INT)); + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + ERR_FAIL_COND(bst->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, bst->blend_shapes.size()); + + bst->blend_shapes.write[p_key_idx].value = p_value; } break; case TYPE_VALUE: { @@ -1385,10 +2184,29 @@ void Animation::track_set_key_transition(int p_track, int p_key_idx, real_t p_tr Track *t = tracks[p_track]; switch (t->type) { - case TYPE_TRANSFORM3D: { - TransformTrack *tt = static_cast<TransformTrack *>(t); - ERR_FAIL_INDEX(p_key_idx, tt->transforms.size()); - tt->transforms.write[p_key_idx].transition = p_transition; + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + ERR_FAIL_COND(tt->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, tt->positions.size()); + tt->positions.write[p_key_idx].transition = p_transition; + } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = static_cast<RotationTrack *>(t); + ERR_FAIL_COND(rt->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, rt->rotations.size()); + rt->rotations.write[p_key_idx].transition = p_transition; + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = static_cast<ScaleTrack *>(t); + ERR_FAIL_COND(st->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, st->scales.size()); + st->scales.write[p_key_idx].transition = p_transition; + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + ERR_FAIL_COND(bst->compressed_track >= 0); + ERR_FAIL_INDEX(p_key_idx, bst->blend_shapes.size()); + bst->blend_shapes.write[p_key_idx].transition = p_transition; } break; case TYPE_VALUE: { ValueTrack *vt = static_cast<ValueTrack *>(t); @@ -1450,15 +2268,6 @@ int Animation::_find(const Vector<K> &p_keys, double p_time) const { return middle; } -Animation::TransformKey Animation::_interpolate(const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, real_t p_c) const { - TransformKey ret; - ret.loc = _interpolate(p_a.loc, p_b.loc, p_c); - ret.rot = _interpolate(p_a.rot, p_b.rot, p_c); - ret.scale = _interpolate(p_a.scale, p_b.scale, p_c); - - return ret; -} - Vector3 Animation::_interpolate(const Vector3 &p_a, const Vector3 &p_b, real_t p_c) const { return p_a.lerp(p_b, p_c); } @@ -1477,16 +2286,6 @@ real_t Animation::_interpolate(const real_t &p_a, const real_t &p_b, real_t p_c) return p_a * (1.0 - p_c) + p_b * p_c; } -Animation::TransformKey Animation::_cubic_interpolate(const Animation::TransformKey &p_pre_a, const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, const Animation::TransformKey &p_post_b, real_t p_c) const { - Animation::TransformKey tk; - - tk.loc = p_a.loc.cubic_interpolate(p_b.loc, p_pre_a.loc, p_post_b.loc, p_c); - tk.scale = p_a.scale.cubic_interpolate(p_b.scale, p_pre_a.scale, p_post_b.scale, p_c); - tk.rot = p_a.rot.cubic_slerp(p_b.rot, p_pre_a.rot, p_post_b.rot, p_c); - - return tk; -} - Vector3 Animation::_cubic_interpolate(const Vector3 &p_pre_a, const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_post_b, real_t p_c) const { return p_a.cubic_interpolate(p_b, p_pre_a, p_post_b, p_c); } @@ -1520,10 +2319,11 @@ Variant Animation::_cubic_interpolate(const Variant &p_pre_a, const Variant &p_a real_t t2 = t * t; real_t t3 = t2 * t; - return 0.5f * ((p1 * 2.0f) + - (-p0 + p2) * t + - (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 + - (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3); + return 0.5f * + ((p1 * 2.0f) + + (-p0 + p2) * t + + (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 + + (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3); } else if ((vformat & (vformat - 1))) { return p_a; //can't interpolate, mix of types @@ -1729,36 +2529,6 @@ T Animation::_interpolate(const Vector<TKey<T>> &p_keys, double p_time, Interpol // do a barrel roll } -Error Animation::transform_track_interpolate(int p_track, double p_time, Vector3 *r_loc, Quaternion *r_rot, Vector3 *r_scale) const { - ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER); - Track *t = tracks[p_track]; - ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM3D, ERR_INVALID_PARAMETER); - - TransformTrack *tt = static_cast<TransformTrack *>(t); - - bool ok = false; - - TransformKey tk = _interpolate(tt->transforms, p_time, tt->interpolation, tt->loop_wrap, &ok); - - if (!ok) { - return ERR_UNAVAILABLE; - } - - if (r_loc) { - *r_loc = tk.loc; - } - - if (r_rot) { - *r_rot = tk.rot; - } - - if (r_scale) { - *r_scale = tk.scale; - } - - return OK; -} - Variant Animation::value_track_interpolate(int p_track, double p_time) const { ERR_FAIL_INDEX_V(p_track, tracks.size(), 0); Track *t = tracks[p_track]; @@ -1933,10 +2703,52 @@ void Animation::track_get_key_indices_in_range(int p_track, double p_time, doubl // handle loop by splitting switch (t->type) { - case TYPE_TRANSFORM3D: { - const TransformTrack *tt = static_cast<const TransformTrack *>(t); - _track_get_key_indices_in_range(tt->transforms, from_time, length, p_indices); - _track_get_key_indices_in_range(tt->transforms, 0, to_time, p_indices); + case TYPE_POSITION_3D: { + const PositionTrack *tt = static_cast<const PositionTrack *>(t); + if (tt->compressed_track >= 0) { + _get_compressed_key_indices_in_range<3>(tt->compressed_track, from_time, length, p_indices); + _get_compressed_key_indices_in_range<3>(tt->compressed_track, 0, to_time, p_indices); + + } else { + _track_get_key_indices_in_range(tt->positions, from_time, length, p_indices); + _track_get_key_indices_in_range(tt->positions, 0, to_time, p_indices); + } + + } break; + case TYPE_ROTATION_3D: { + const RotationTrack *rt = static_cast<const RotationTrack *>(t); + if (rt->compressed_track >= 0) { + _get_compressed_key_indices_in_range<3>(rt->compressed_track, from_time, length, p_indices); + _get_compressed_key_indices_in_range<3>(rt->compressed_track, 0, to_time, p_indices); + + } else { + _track_get_key_indices_in_range(rt->rotations, from_time, length, p_indices); + _track_get_key_indices_in_range(rt->rotations, 0, to_time, p_indices); + } + + } break; + case TYPE_SCALE_3D: { + const ScaleTrack *st = static_cast<const ScaleTrack *>(t); + if (st->compressed_track >= 0) { + _get_compressed_key_indices_in_range<3>(st->compressed_track, from_time, length, p_indices); + _get_compressed_key_indices_in_range<3>(st->compressed_track, 0, to_time, p_indices); + + } else { + _track_get_key_indices_in_range(st->scales, from_time, length, p_indices); + _track_get_key_indices_in_range(st->scales, 0, to_time, p_indices); + } + + } break; + case TYPE_BLEND_SHAPE: { + const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t); + if (bst->compressed_track >= 0) { + _get_compressed_key_indices_in_range<1>(bst->compressed_track, from_time, length, p_indices); + _get_compressed_key_indices_in_range<1>(bst->compressed_track, 0, to_time, p_indices); + + } else { + _track_get_key_indices_in_range(bst->blend_shapes, from_time, length, p_indices); + _track_get_key_indices_in_range(bst->blend_shapes, 0, to_time, p_indices); + } } break; case TYPE_VALUE: { @@ -1989,9 +2801,40 @@ void Animation::track_get_key_indices_in_range(int p_track, double p_time, doubl } switch (t->type) { - case TYPE_TRANSFORM3D: { - const TransformTrack *tt = static_cast<const TransformTrack *>(t); - _track_get_key_indices_in_range(tt->transforms, from_time, to_time, p_indices); + case TYPE_POSITION_3D: { + const PositionTrack *tt = static_cast<const PositionTrack *>(t); + if (tt->compressed_track >= 0) { + _get_compressed_key_indices_in_range<3>(tt->compressed_track, from_time, to_time - from_time, p_indices); + } else { + _track_get_key_indices_in_range(tt->positions, from_time, to_time, p_indices); + } + + } break; + case TYPE_ROTATION_3D: { + const RotationTrack *rt = static_cast<const RotationTrack *>(t); + if (rt->compressed_track >= 0) { + _get_compressed_key_indices_in_range<3>(rt->compressed_track, from_time, to_time - from_time, p_indices); + } else { + _track_get_key_indices_in_range(rt->rotations, from_time, to_time, p_indices); + } + + } break; + case TYPE_SCALE_3D: { + const ScaleTrack *st = static_cast<const ScaleTrack *>(t); + if (st->compressed_track >= 0) { + _get_compressed_key_indices_in_range<3>(st->compressed_track, from_time, to_time - from_time, p_indices); + } else { + _track_get_key_indices_in_range(st->scales, from_time, to_time, p_indices); + } + + } break; + case TYPE_BLEND_SHAPE: { + const BlendShapeTrack *bst = static_cast<const BlendShapeTrack *>(t); + if (bst->compressed_track >= 0) { + _get_compressed_key_indices_in_range<1>(bst->compressed_track, from_time, to_time - from_time, p_indices); + } else { + _track_get_key_indices_in_range(bst->blend_shapes, from_time, to_time, p_indices); + } } break; case TYPE_VALUE: { @@ -2595,7 +3438,7 @@ void Animation::_bind_methods() { ClassDB::bind_method(D_METHOD("track_get_type", "track_idx"), &Animation::track_get_type); ClassDB::bind_method(D_METHOD("track_get_path", "track_idx"), &Animation::track_get_path); ClassDB::bind_method(D_METHOD("track_set_path", "track_idx", "path"), &Animation::track_set_path); - ClassDB::bind_method(D_METHOD("find_track", "path"), &Animation::find_track); + ClassDB::bind_method(D_METHOD("find_track", "path", "type"), &Animation::find_track); ClassDB::bind_method(D_METHOD("track_move_up", "track_idx"), &Animation::track_move_up); ClassDB::bind_method(D_METHOD("track_move_down", "track_idx"), &Animation::track_move_down); @@ -2608,7 +3451,11 @@ void Animation::_bind_methods() { ClassDB::bind_method(D_METHOD("track_set_enabled", "track_idx", "enabled"), &Animation::track_set_enabled); ClassDB::bind_method(D_METHOD("track_is_enabled", "track_idx"), &Animation::track_is_enabled); - ClassDB::bind_method(D_METHOD("transform_track_insert_key", "track_idx", "time", "location", "rotation", "scale"), &Animation::transform_track_insert_key); + ClassDB::bind_method(D_METHOD("position_track_insert_key", "track_idx", "time", "position"), &Animation::position_track_insert_key); + ClassDB::bind_method(D_METHOD("rotation_track_insert_key", "track_idx", "time", "rotation"), &Animation::rotation_track_insert_key); + ClassDB::bind_method(D_METHOD("scale_track_insert_key", "track_idx", "time", "scale"), &Animation::scale_track_insert_key); + ClassDB::bind_method(D_METHOD("blend_shape_track_insert_key", "track_idx", "time", "amount"), &Animation::blend_shape_track_insert_key); + ClassDB::bind_method(D_METHOD("track_insert_key", "track_idx", "time", "key", "transition"), &Animation::track_insert_key, DEFVAL(1)); ClassDB::bind_method(D_METHOD("track_remove_key", "track_idx", "key_idx"), &Animation::track_remove_key); ClassDB::bind_method(D_METHOD("track_remove_key_at_time", "track_idx", "time"), &Animation::track_remove_key_at_time); @@ -2628,7 +3475,8 @@ void Animation::_bind_methods() { ClassDB::bind_method(D_METHOD("track_set_interpolation_loop_wrap", "track_idx", "interpolation"), &Animation::track_set_interpolation_loop_wrap); ClassDB::bind_method(D_METHOD("track_get_interpolation_loop_wrap", "track_idx"), &Animation::track_get_interpolation_loop_wrap); - ClassDB::bind_method(D_METHOD("transform_track_interpolate", "track_idx", "time_sec"), &Animation::_transform_track_interpolate); + ClassDB::bind_method(D_METHOD("track_is_compressed", "track_idx"), &Animation::track_is_compressed); + ClassDB::bind_method(D_METHOD("value_track_set_update_mode", "track_idx", "mode"), &Animation::value_track_set_update_mode); ClassDB::bind_method(D_METHOD("value_track_get_update_mode", "track_idx"), &Animation::value_track_get_update_mode); @@ -2675,6 +3523,8 @@ void Animation::_bind_methods() { ClassDB::bind_method(D_METHOD("clear"), &Animation::clear); ClassDB::bind_method(D_METHOD("copy_track", "track_idx", "to_animation"), &Animation::copy_track); + ClassDB::bind_method(D_METHOD("compress", "page_size", "fps", "split_tolerance"), &Animation::compress, DEFVAL(8192), DEFVAL(120), DEFVAL(4.0)); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "0.001,99999,0.001"), "set_length", "get_length"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step", PROPERTY_HINT_RANGE, "0,4096,0.001"), "set_step", "get_step"); @@ -2682,7 +3532,10 @@ void Animation::_bind_methods() { ADD_SIGNAL(MethodInfo("tracks_changed")); BIND_ENUM_CONSTANT(TYPE_VALUE); - BIND_ENUM_CONSTANT(TYPE_TRANSFORM3D); + BIND_ENUM_CONSTANT(TYPE_POSITION_3D); + BIND_ENUM_CONSTANT(TYPE_ROTATION_3D); + BIND_ENUM_CONSTANT(TYPE_SCALE_3D); + BIND_ENUM_CONSTANT(TYPE_BLEND_SHAPE); BIND_ENUM_CONSTANT(TYPE_METHOD); BIND_ENUM_CONSTANT(TYPE_BEZIER); BIND_ENUM_CONSTANT(TYPE_AUDIO); @@ -2705,223 +3558,1475 @@ void Animation::clear() { tracks.clear(); loop = false; length = 1; + compression.enabled = false; + compression.bounds.clear(); + compression.pages.clear(); + compression.fps = 120; emit_changed(); emit_signal(SceneStringNames::get_singleton()->tracks_changed); } -bool Animation::_transform_track_optimize_key(const TKey<TransformKey> &t0, const TKey<TransformKey> &t1, const TKey<TransformKey> &t2, real_t p_alowed_linear_err, real_t p_alowed_angular_err, real_t p_max_optimizable_angle, const Vector3 &p_norm) { - real_t c = (t1.time - t0.time) / (t2.time - t0.time); - real_t t[3] = { -1, -1, -1 }; +bool Animation::_position_track_optimize_key(const TKey<Vector3> &t0, const TKey<Vector3> &t1, const TKey<Vector3> &t2, real_t p_allowed_linear_err, real_t p_allowed_angular_error, const Vector3 &p_norm) { + const Vector3 &v0 = t0.value; + const Vector3 &v1 = t1.value; + const Vector3 &v2 = t2.value; - { //translation + if (v0.is_equal_approx(v2)) { + //0 and 2 are close, let's see if 1 is close + if (!v0.is_equal_approx(v1)) { + //not close, not optimizable + return false; + } - const Vector3 &v0 = t0.value.loc; - const Vector3 &v1 = t1.value.loc; - const Vector3 &v2 = t2.value.loc; + } else { + Vector3 pd = (v2 - v0); + real_t d0 = pd.dot(v0); + real_t d1 = pd.dot(v1); + real_t d2 = pd.dot(v2); + if (d1 < d0 || d1 > d2) { + return false; + } - if (v0.is_equal_approx(v2)) { - //0 and 2 are close, let's see if 1 is close - if (!v0.is_equal_approx(v1)) { - //not close, not optimizable - return false; + Vector3 s[2] = { v0, v2 }; + real_t d = Geometry3D::get_closest_point_to_segment(v1, s).distance_to(v1); + + if (d > pd.length() * p_allowed_linear_err) { + return false; //beyond allowed error for collinearity + } + + if (p_norm != Vector3() && Math::acos(pd.normalized().dot(p_norm)) > p_allowed_angular_error) { + return false; + } + } + + return true; +} + +bool Animation::_rotation_track_optimize_key(const TKey<Quaternion> &t0, const TKey<Quaternion> &t1, const TKey<Quaternion> &t2, real_t p_allowed_angular_error, float p_max_optimizable_angle) { + const Quaternion &q0 = t0.value; + const Quaternion &q1 = t1.value; + const Quaternion &q2 = t2.value; + + //localize both to rotation from q0 + + if (q0.is_equal_approx(q2)) { + if (!q0.is_equal_approx(q1)) { + return false; + } + + } else { + Quaternion r02 = (q0.inverse() * q2).normalized(); + Quaternion r01 = (q0.inverse() * q1).normalized(); + + Vector3 v02, v01; + real_t a02, a01; + + r02.get_axis_angle(v02, a02); + r01.get_axis_angle(v01, a01); + + if (Math::abs(a02) > p_max_optimizable_angle) { + return false; + } + + if (v01.dot(v02) < 0) { + //make sure both rotations go the same way to compare + v02 = -v02; + a02 = -a02; + } + + real_t err_01 = Math::acos(v01.normalized().dot(v02.normalized())) / Math_PI; + if (err_01 > p_allowed_angular_error) { + //not rotating in the same axis + return false; + } + + if (a01 * a02 < 0) { + //not rotating in the same direction + return false; + } + + real_t tr = a01 / a02; + if (tr < 0 || tr > 1) { + return false; //rotating too much or too less + } + } + + return true; +} + +bool Animation::_scale_track_optimize_key(const TKey<Vector3> &t0, const TKey<Vector3> &t1, const TKey<Vector3> &t2, real_t p_allowed_linear_error) { + const Vector3 &v0 = t0.value; + const Vector3 &v1 = t1.value; + const Vector3 &v2 = t2.value; + + if (v0.is_equal_approx(v2)) { + //0 and 2 are close, let's see if 1 is close + if (!v0.is_equal_approx(v1)) { + //not close, not optimizable + return false; + } + + } else { + Vector3 pd = (v2 - v0); + real_t d0 = pd.dot(v0); + real_t d1 = pd.dot(v1); + real_t d2 = pd.dot(v2); + if (d1 < d0 || d1 > d2) { + return false; //beyond segment range + } + + Vector3 s[2] = { v0, v2 }; + real_t d = Geometry3D::get_closest_point_to_segment(v1, s).distance_to(v1); + + if (d > pd.length() * p_allowed_linear_error) { + return false; //beyond allowed error for colinearity + } + } + + return true; +} + +bool Animation::_blend_shape_track_optimize_key(const TKey<float> &t0, const TKey<float> &t1, const TKey<float> &t2, real_t p_allowed_unit_error) { + float v0 = t0.value; + float v1 = t1.value; + float v2 = t2.value; + + if (Math::is_equal_approx(v1, v2, p_allowed_unit_error)) { + //0 and 2 are close, let's see if 1 is close + if (!Math::is_equal_approx(v0, v1, p_allowed_unit_error)) { + //not close, not optimizable + return false; + } + + } else { + /* + TODO eventually discuss a way to optimize these better. + float pd = (v2 - v0); + real_t d0 = pd.dot(v0); + real_t d1 = pd.dot(v1); + real_t d2 = pd.dot(v2); + if (d1 < d0 || d1 > d2) { + return false; //beyond segment range + } + + float s[2] = { v0, v2 }; + real_t d = Geometry3D::get_closest_point_to_segment(v1, s).distance_to(v1); + + if (d > pd.length() * p_allowed_linear_error) { + return false; //beyond allowed error for colinearity + } +*/ + } + + return true; +} + +void Animation::_position_track_optimize(int p_idx, real_t p_allowed_linear_err, real_t p_allowed_angular_err) { + ERR_FAIL_INDEX(p_idx, tracks.size()); + ERR_FAIL_COND(tracks[p_idx]->type != TYPE_POSITION_3D); + PositionTrack *tt = static_cast<PositionTrack *>(tracks[p_idx]); + bool prev_erased = false; + TKey<Vector3> first_erased; + + Vector3 norm; + + for (int i = 1; i < tt->positions.size() - 1; i++) { + TKey<Vector3> &t0 = tt->positions.write[i - 1]; + TKey<Vector3> &t1 = tt->positions.write[i]; + TKey<Vector3> &t2 = tt->positions.write[i + 1]; + + bool erase = _position_track_optimize_key(t0, t1, t2, p_allowed_linear_err, p_allowed_angular_err, norm); + if (erase && !prev_erased) { + norm = (t2.value - t1.value).normalized(); + } + + if (prev_erased && !_position_track_optimize_key(t0, first_erased, t2, p_allowed_linear_err, p_allowed_angular_err, norm)) { + //avoid error to go beyond first erased key + erase = false; + } + + if (erase) { + if (!prev_erased) { + first_erased = t1; + prev_erased = true; } + tt->positions.remove(i); + i--; + } else { - Vector3 pd = (v2 - v0); - real_t d0 = pd.dot(v0); - real_t d1 = pd.dot(v1); - real_t d2 = pd.dot(v2); - if (d1 < d0 || d1 > d2) { - return false; - } + prev_erased = false; + norm = Vector3(); + } + } +} + +void Animation::_rotation_track_optimize(int p_idx, real_t p_allowed_angular_err, real_t p_max_optimizable_angle) { + ERR_FAIL_INDEX(p_idx, tracks.size()); + ERR_FAIL_COND(tracks[p_idx]->type != TYPE_ROTATION_3D); + RotationTrack *tt = static_cast<RotationTrack *>(tracks[p_idx]); + bool prev_erased = false; + TKey<Quaternion> first_erased; - Vector3 s[2] = { v0, v2 }; - real_t d = Geometry3D::get_closest_point_to_segment(v1, s).distance_to(v1); + for (int i = 1; i < tt->rotations.size() - 1; i++) { + TKey<Quaternion> &t0 = tt->rotations.write[i - 1]; + TKey<Quaternion> &t1 = tt->rotations.write[i]; + TKey<Quaternion> &t2 = tt->rotations.write[i + 1]; - if (d > pd.length() * p_alowed_linear_err) { - return false; //beyond allowed error for collinearity + bool erase = _rotation_track_optimize_key(t0, t1, t2, p_allowed_angular_err, p_max_optimizable_angle); + + if (prev_erased && !_rotation_track_optimize_key(t0, first_erased, t2, p_allowed_angular_err, p_max_optimizable_angle)) { + //avoid error to go beyond first erased key + erase = false; + } + + if (erase) { + if (!prev_erased) { + first_erased = t1; + prev_erased = true; } - if (p_norm != Vector3() && Math::acos(pd.normalized().dot(p_norm)) > p_alowed_angular_err) { - return false; + tt->rotations.remove(i); + i--; + + } else { + prev_erased = false; + } + } +} + +void Animation::_scale_track_optimize(int p_idx, real_t p_allowed_linear_err) { + ERR_FAIL_INDEX(p_idx, tracks.size()); + ERR_FAIL_COND(tracks[p_idx]->type != TYPE_SCALE_3D); + ScaleTrack *tt = static_cast<ScaleTrack *>(tracks[p_idx]); + bool prev_erased = false; + TKey<Vector3> first_erased; + + for (int i = 1; i < tt->scales.size() - 1; i++) { + TKey<Vector3> &t0 = tt->scales.write[i - 1]; + TKey<Vector3> &t1 = tt->scales.write[i]; + TKey<Vector3> &t2 = tt->scales.write[i + 1]; + + bool erase = _scale_track_optimize_key(t0, t1, t2, p_allowed_linear_err); + + if (prev_erased && !_scale_track_optimize_key(t0, first_erased, t2, p_allowed_linear_err)) { + //avoid error to go beyond first erased key + erase = false; + } + + if (erase) { + if (!prev_erased) { + first_erased = t1; + prev_erased = true; } - t[0] = (d1 - d0) / (d2 - d0); + tt->scales.remove(i); + i--; + + } else { + prev_erased = false; } } +} + +void Animation::_blend_shape_track_optimize(int p_idx, real_t p_allowed_linear_err) { + ERR_FAIL_INDEX(p_idx, tracks.size()); + ERR_FAIL_COND(tracks[p_idx]->type != TYPE_BLEND_SHAPE); + BlendShapeTrack *tt = static_cast<BlendShapeTrack *>(tracks[p_idx]); + bool prev_erased = false; + TKey<float> first_erased; + first_erased.value = 0.0; - { //rotation + for (int i = 1; i < tt->blend_shapes.size() - 1; i++) { + TKey<float> &t0 = tt->blend_shapes.write[i - 1]; + TKey<float> &t1 = tt->blend_shapes.write[i]; + TKey<float> &t2 = tt->blend_shapes.write[i + 1]; - const Quaternion &q0 = t0.value.rot; - const Quaternion &q1 = t1.value.rot; - const Quaternion &q2 = t2.value.rot; + bool erase = _blend_shape_track_optimize_key(t0, t1, t2, p_allowed_linear_err); - //localize both to rotation from q0 + if (prev_erased && !_blend_shape_track_optimize_key(t0, first_erased, t2, p_allowed_linear_err)) { + //avoid error to go beyond first erased key + erase = false; + } - if (q0.is_equal_approx(q2)) { - if (!q0.is_equal_approx(q1)) { - return false; + if (erase) { + if (!prev_erased) { + first_erased = t1; + prev_erased = true; } + tt->blend_shapes.remove(i); + i--; + } else { - Quaternion r02 = (q0.inverse() * q2).normalized(); - Quaternion r01 = (q0.inverse() * q1).normalized(); + prev_erased = false; + } + } +} + +void Animation::optimize(real_t p_allowed_linear_err, real_t p_allowed_angular_err, real_t p_max_optimizable_angle) { + for (int i = 0; i < tracks.size(); i++) { + if (track_is_compressed(i)) { + continue; //not possible to optimize compressed track + } + if (tracks[i]->type == TYPE_POSITION_3D) { + _position_track_optimize(i, p_allowed_linear_err, p_allowed_angular_err); + } else if (tracks[i]->type == TYPE_ROTATION_3D) { + _rotation_track_optimize(i, p_allowed_angular_err, p_max_optimizable_angle); + } else if (tracks[i]->type == TYPE_SCALE_3D) { + _scale_track_optimize(i, p_allowed_linear_err); + } else if (tracks[i]->type == TYPE_BLEND_SHAPE) { + _blend_shape_track_optimize(i, p_allowed_linear_err); + } + } +} - Vector3 v02, v01; - real_t a02, a01; +#define print_animc(m_str) +//#define print_animc(m_str) print_line(m_str); - r02.get_axis_angle(v02, a02); - r01.get_axis_angle(v01, a01); +struct AnimationCompressionDataState { + enum { + MIN_OPTIMIZE_PACKETS = 5, + MAX_PACKETS = 16 + }; - if (Math::abs(a02) > p_max_optimizable_angle) { - return false; + uint32_t components = 3; + LocalVector<uint8_t> data; //commited packets + struct PacketData { + int32_t data[3] = { 0, 0, 0 }; + uint32_t frame = 0; + }; + + float split_tolerance = 1.5; + + LocalVector<PacketData> temp_packets; + + //used for rollback if the new frame does not fit + int32_t validated_packet_count = -1; + + static int32_t _compute_delta16_signed(int32_t p_from, int32_t p_to) { + int32_t delta = p_to - p_from; + if (delta > 32767) { + return delta - 65536; // use wrap around + } else if (delta < -32768) { + return 65536 + delta; // use wrap around + } + return delta; + } + + static uint32_t _compute_shift_bits_signed(int32_t p_delta) { + if (p_delta == 0) { + return 0; + } else if (p_delta < 0) { + p_delta = ABS(p_delta) - 1; + if (p_delta == 0) { + return 1; } + } + return nearest_shift(p_delta); + } - if (v01.dot(v02) < 0) { - //make sure both rotations go the same way to compare - v02 = -v02; - a02 = -a02; + void _compute_max_shifts(uint32_t p_from, uint32_t p_to, uint32_t *max_shifts, uint32_t &max_frame_delta_shift) const { + for (uint32_t j = 0; j < components; j++) { + max_shifts[j] = 0; + } + max_frame_delta_shift = 0; + + for (uint32_t i = p_from + 1; i <= p_to; i++) { + int32_t frame_delta = temp_packets[i].frame - temp_packets[i - 1].frame; + max_frame_delta_shift = MAX(max_frame_delta_shift, nearest_shift(frame_delta)); + for (uint32_t j = 0; j < components; j++) { + int32_t diff = _compute_delta16_signed(temp_packets[i - 1].data[j], temp_packets[i].data[j]); + uint32_t shift = _compute_shift_bits_signed(diff); + max_shifts[j] = MAX(shift, max_shifts[j]); } + } + } - real_t err_01 = Math::acos(v01.normalized().dot(v02.normalized())) / Math_PI; - if (err_01 > p_alowed_angular_err) { - //not rotating in the same axis - return false; + bool insert_key(uint32_t p_frame, const Vector3i &p_key) { + if (temp_packets.size() == MAX_PACKETS) { + commit_temp_packets(); + } + PacketData packet; + packet.frame = p_frame; + for (int i = 0; i < 3; i++) { + ERR_FAIL_COND_V(p_key[i] > 65535, false); // Sanity check + packet.data[i] = p_key[i]; + } + + temp_packets.push_back(packet); + + if (temp_packets.size() >= MIN_OPTIMIZE_PACKETS) { + uint32_t max_shifts[3] = { 0, 0, 0 }; // Base sizes, 16 bit + uint32_t max_frame_delta_shift = 0; + // Compute the average shift before the packet was added + _compute_max_shifts(0, temp_packets.size() - 2, max_shifts, max_frame_delta_shift); + + float prev_packet_size_avg = 0; + prev_packet_size_avg = float(1 << max_frame_delta_shift); + for (uint32_t i = 0; i < components; i++) { + prev_packet_size_avg += float(1 << max_shifts[i]); } + prev_packet_size_avg /= float(1 + components); - if (a01 * a02 < 0) { - //not rotating in the same direction - return false; + _compute_max_shifts(temp_packets.size() - 2, temp_packets.size() - 1, max_shifts, max_frame_delta_shift); + + float new_packet_size_avg = 0; + new_packet_size_avg = float(1 << max_frame_delta_shift); + for (uint32_t i = 0; i < components; i++) { + new_packet_size_avg += float(1 << max_shifts[i]); } + new_packet_size_avg /= float(1 + components); + + print_animc("packet count: " + rtos(temp_packets.size() - 1) + " size avg " + rtos(prev_packet_size_avg) + " new avg " + rtos(new_packet_size_avg)); + float ratio = (prev_packet_size_avg < new_packet_size_avg) ? (new_packet_size_avg / prev_packet_size_avg) : (prev_packet_size_avg / new_packet_size_avg); - real_t tr = a01 / a02; - if (tr < 0 || tr > 1) { - return false; //rotating too much or too less + if (ratio > split_tolerance) { + print_animc("split!"); + temp_packets.resize(temp_packets.size() - 1); + commit_temp_packets(); + temp_packets.push_back(packet); } + } - t[1] = tr; + return temp_packets.size() == 1; // First key + } + + uint32_t get_temp_packet_size() const { + if (temp_packets.size() == 0) { + return 0; + } else if (temp_packets.size() == 1) { + return components == 1 ? 4 : 8; // 1 component packet is 16 bits and 16 bits unused. 3 component packets is 48 bits and 16 bits unused + } + uint32_t max_shifts[3] = { 0, 0, 0 }; //base sizes, 16 bit + uint32_t max_frame_delta_shift = 0; + + _compute_max_shifts(0, temp_packets.size() - 1, max_shifts, max_frame_delta_shift); + + uint32_t size_bits = 16; //base value (all 4 bits of shift sizes for x,y,z,time) + size_bits += max_frame_delta_shift * (temp_packets.size() - 1); //times + for (uint32_t j = 0; j < components; j++) { + size_bits += 16; //base value + uint32_t shift = max_shifts[j]; + if (shift > 0) { + shift += 1; //if not zero, add sign bit + } + size_bits += shift * (temp_packets.size() - 1); } + if (size_bits % 8 != 0) { //wrap to 8 bits + size_bits += 8 - (size_bits % 8); + } + uint32_t size_bytes = size_bits / 8; //wrap to words + if (size_bytes % 4 != 0) { + size_bytes += 4 - (size_bytes % 4); + } + return size_bytes; } - { //scale + static void _push_bits(LocalVector<uint8_t> &data, uint32_t &r_buffer, uint32_t &r_bits_used, uint32_t p_value, uint32_t p_bits) { + r_buffer |= p_value << r_bits_used; + r_bits_used += p_bits; + while (r_bits_used >= 8) { + uint8_t byte = r_buffer & 0xFF; + data.push_back(byte); + r_buffer >>= 8; + r_bits_used -= 8; + } + } + + void commit_temp_packets() { + if (temp_packets.size() == 0) { + return; //nohing to do + } +#define DEBUG_PACKET_PUSH +#ifdef DEBUG_PACKET_PUSH +#ifndef _MSC_VER +#warning Debugging packet push, disable this code in production to gain a bit more import performance. +#endif + uint32_t debug_packet_push = get_temp_packet_size(); + uint32_t debug_data_size = data.size(); +#endif + // Store header + + uint8_t header[8]; + uint32_t header_bytes = 0; + for (uint32_t i = 0; i < components; i++) { + encode_uint16(temp_packets[0].data[i], &header[header_bytes]); + header_bytes += 2; + } - const Vector3 &v0 = t0.value.scale; - const Vector3 &v1 = t1.value.scale; - const Vector3 &v2 = t2.value.scale; + uint32_t max_shifts[3] = { 0, 0, 0 }; //base sizes, 16 bit + uint32_t max_frame_delta_shift = 0; - if (v0.is_equal_approx(v2)) { - //0 and 2 are close, let's see if 1 is close - if (!v0.is_equal_approx(v1)) { - //not close, not optimizable - return false; + if (temp_packets.size() > 1) { + _compute_max_shifts(0, temp_packets.size() - 1, max_shifts, max_frame_delta_shift); + uint16_t shift_header = (max_frame_delta_shift - 1) << 12; + for (uint32_t i = 0; i < components; i++) { + shift_header |= max_shifts[i] << (4 * i); } - } else { - Vector3 pd = (v2 - v0); - real_t d0 = pd.dot(v0); - real_t d1 = pd.dot(v1); - real_t d2 = pd.dot(v2); - if (d1 < d0 || d1 > d2) { - return false; //beyond segment range + encode_uint16(shift_header, &header[header_bytes]); + header_bytes += 2; + } + + while (header_bytes % 4 != 0) { + header[header_bytes++] = 0; + } + + for (uint32_t i = 0; i < header_bytes; i++) { + data.push_back(header[i]); + } + + if (temp_packets.size() == 1) { + temp_packets.clear(); + validated_packet_count = 0; + return; //only header stored, nothing else to do + } + + uint32_t bit_buffer = 0; + uint32_t bits_used = 0; + + for (uint32_t i = 1; i < temp_packets.size(); i++) { + uint32_t frame_delta = temp_packets[i].frame - temp_packets[i - 1].frame; + _push_bits(data, bit_buffer, bits_used, frame_delta, max_frame_delta_shift); + + for (uint32_t j = 0; j < components; j++) { + if (max_shifts[j] == 0) { + continue; // Zero delta, do not store + } + int32_t delta = _compute_delta16_signed(temp_packets[i - 1].data[j], temp_packets[i].data[j]); + + ERR_FAIL_COND(delta < -32768 || delta > 32767); //sanity check + + uint16_t deltau; + if (delta < 0) { + deltau = (ABS(delta) - 1) | (1 << max_shifts[j]); + } else { + deltau = delta; + } + _push_bits(data, bit_buffer, bits_used, deltau, max_shifts[j] + 1); // Include sign bit } + } + if (bits_used != 0) { + ERR_FAIL_COND(bit_buffer > 0xFF); // Sanity check + data.push_back(bit_buffer); + } - Vector3 s[2] = { v0, v2 }; - real_t d = Geometry3D::get_closest_point_to_segment(v1, s).distance_to(v1); + while (data.size() % 4 != 0) { + data.push_back(0); //pad to align with 4 + } + + temp_packets.clear(); + validated_packet_count = 0; + +#ifdef DEBUG_PACKET_PUSH + ERR_FAIL_COND((data.size() - debug_data_size) != debug_packet_push); +#endif + } +}; + +struct AnimationCompressionTimeState { + struct Packet { + uint32_t frame; + uint32_t offset; + uint32_t count; + }; + + LocalVector<Packet> packets; + //used for rollback + int32_t key_index = 0; + int32_t validated_packet_count = 0; + int32_t validated_key_index = -1; + bool needs_start_frame = false; +}; - if (d > pd.length() * p_alowed_linear_err) { - return false; //beyond allowed error for collinearity +Vector3i Animation::_compress_key(uint32_t p_track, const AABB &p_bounds, int32_t p_key, float p_time) { + Vector3i values; + TrackType tt = track_get_type(p_track); + switch (tt) { + case TYPE_POSITION_3D: { + Vector3 pos; + if (p_key >= 0) { + position_track_get_key(p_track, p_key, &pos); + } else { + position_track_interpolate(p_track, p_time, &pos); + } + pos = (pos - p_bounds.position) / p_bounds.size; + for (int j = 0; j < 3; j++) { + values[j] = CLAMP(int32_t(pos[j] * 65535.0), 0, 65535); + } + } break; + case TYPE_ROTATION_3D: { + Quaternion rot; + if (p_key >= 0) { + rotation_track_get_key(p_track, p_key, &rot); + } else { + rotation_track_interpolate(p_track, p_time, &rot); + } + Vector3 axis = rot.get_axis(); + float angle = rot.get_angle(); + angle = Math::fposmod(double(angle), double(Math_PI * 2.0)); + Vector2 oct = axis.octahedron_encode(); + Vector3 rot_norm(oct.x, oct.y, angle / (Math_PI * 2.0)); // high resolution rotation in 0-1 angle. + + for (int j = 0; j < 3; j++) { + values[j] = CLAMP(int32_t(rot_norm[j] * 65535.0), 0, 65535); + } + } break; + case TYPE_SCALE_3D: { + Vector3 scale; + if (p_key >= 0) { + scale_track_get_key(p_track, p_key, &scale); + } else { + scale_track_interpolate(p_track, p_time, &scale); + } + scale = (scale - p_bounds.position) / p_bounds.size; + for (int j = 0; j < 3; j++) { + values[j] = CLAMP(int32_t(scale[j] * 65535.0), 0, 65535); + } + } break; + case TYPE_BLEND_SHAPE: { + float blend; + if (p_key >= 0) { + blend_shape_track_get_key(p_track, p_key, &blend); + } else { + blend_shape_track_interpolate(p_track, p_time, &blend); } - t[2] = (d1 - d0) / (d2 - d0); + blend = (blend / float(Compression::BLEND_SHAPE_RANGE)) * 0.5 + 0.5; + values[0] = CLAMP(int32_t(blend * 65535.0), 0, 65535); + } break; + default: { + ERR_FAIL_V(Vector3i()); //sanity check + } break; + } + + return values; +} + +struct AnimationCompressionBufferBitsRead { + uint32_t buffer = 0; + uint32_t used = 0; + const uint8_t *src_data = nullptr; + + _FORCE_INLINE_ uint32_t read(uint32_t p_bits) { + uint32_t output = 0; + uint32_t written = 0; + while (p_bits > 0) { + if (used == 0) { + used = 8; + buffer = *src_data; + src_data++; + } + uint32_t to_write = MIN(used, p_bits); + output |= (buffer & ((1 << to_write) - 1)) << written; + buffer >>= to_write; + used -= to_write; + p_bits -= to_write; + written += to_write; } + return output; } +}; - bool erase = false; - if (t[0] == -1 && t[1] == -1 && t[2] == -1) { - erase = true; - } else { - erase = true; - real_t lt = -1.0; - for (int j = 0; j < 3; j++) { - //search for t on first, one must be it - if (t[j] != -1) { - lt = t[j]; //official t - //validate rest - for (int k = j + 1; k < 3; k++) { - if (t[k] == -1) { - continue; +void Animation::compress(uint32_t p_page_size, uint32_t p_fps, float p_split_tolerance) { + ERR_FAIL_COND_MSG(compression.enabled, "This animation is already compressed"); + + p_split_tolerance = CLAMP(p_split_tolerance, 1.1, 8.0); + compression.pages.clear(); + + uint32_t base_page_size = 0; // Before compressing pages, compute how large the "end page" datablock is. + LocalVector<uint32_t> tracks_to_compress; + LocalVector<AABB> track_bounds; + const uint32_t time_packet_size = 4; + + const uint32_t track_header_size = 4 + 4 + 4; // pointer to time (4 bytes), amount of time keys (4 bytes) pointer to track data (4 bytes) + + for (int i = 0; i < get_track_count(); i++) { + TrackType type = track_get_type(i); + if (type != TYPE_POSITION_3D && type != TYPE_ROTATION_3D && type != TYPE_SCALE_3D && type != TYPE_BLEND_SHAPE) { + continue; + } + if (track_get_key_count(i) == 0) { + continue; //do not compress, no keys + } + base_page_size += track_header_size; //pointer to beginning of each track timeline and amount of time keys + base_page_size += time_packet_size; //for end of track time marker + base_page_size += (type == TYPE_BLEND_SHAPE) ? 4 : 8; // at least the end of track packet (at much 8 bytes). This could be less, but have to be pessimistic. + tracks_to_compress.push_back(i); + + AABB bounds; + + if (type == TYPE_POSITION_3D) { + AABB aabb; + int kcount = track_get_key_count(i); + for (int j = 0; j < kcount; j++) { + Vector3 pos; + position_track_get_key(i, j, &pos); + if (j == 0) { + aabb.position = pos; + } else { + aabb.expand_to(pos); + } + } + for (int j = 0; j < 3; j++) { + //cant have zero + if (aabb.size[j] < CMP_EPSILON) { + aabb.size[j] = CMP_EPSILON; + } + } + bounds = aabb; + } + if (type == TYPE_SCALE_3D) { + AABB aabb; + int kcount = track_get_key_count(i); + for (int j = 0; j < kcount; j++) { + Vector3 scale; + scale_track_get_key(i, j, &scale); + if (j == 0) { + aabb.position = scale; + } else { + aabb.expand_to(scale); + } + } + for (int j = 0; j < 3; j++) { + //cant have zero + if (aabb.size[j] < CMP_EPSILON) { + aabb.size[j] = CMP_EPSILON; + } + } + bounds = aabb; + } + + track_bounds.push_back(bounds); + } + + if (tracks_to_compress.size() == 0) { + return; //nothing to compress + } + + print_animc("Anim Compression:"); + print_animc("-----------------"); + print_animc("Tracks to compress: " + itos(tracks_to_compress.size())); + + uint32_t current_frame = 0; + uint32_t base_page_frame = 0; + double frame_len = 1.0 / double(p_fps); + const uint32_t max_frames_per_page = 65536; + + print_animc("Frame Len: " + rtos(frame_len)); + + LocalVector<AnimationCompressionDataState> data_tracks; + LocalVector<AnimationCompressionTimeState> time_tracks; + + data_tracks.resize(tracks_to_compress.size()); + time_tracks.resize(tracks_to_compress.size()); + + for (uint32_t i = 0; i < data_tracks.size(); i++) { + data_tracks[i].split_tolerance = p_split_tolerance; + if (track_get_type(tracks_to_compress[i]) == TYPE_BLEND_SHAPE) { + data_tracks[i].components = 1; + } else { + data_tracks[i].components = 3; + } + } + + while (true) { + // Begin by finding the keyframe in all tracks with the time closest to the current time + const uint32_t FRAME_MAX = 0xFFFFFFFF; + const int32_t NO_TRACK_FOUND = -1; + uint32_t best_frame = FRAME_MAX; + uint32_t best_invalid_frame = FRAME_MAX; + int32_t best_frame_track = NO_TRACK_FOUND; // Default is -1, which means all keyframes for this page are exhausted. + bool start_frame = false; + + for (uint32_t i = 0; i < tracks_to_compress.size(); i++) { + uint32_t uncomp_track = tracks_to_compress[i]; + + if (time_tracks[i].key_index == track_get_key_count(uncomp_track)) { + if (time_tracks[i].needs_start_frame) { + start_frame = true; + best_frame = base_page_frame; + best_frame_track = i; + time_tracks[i].needs_start_frame = false; + break; + } else { + continue; // This track is exhausted (all keys were added already), don't consider. + } + } + + uint32_t key_frame = double(track_get_key_time(uncomp_track, time_tracks[i].key_index)) / frame_len; + + if (time_tracks[i].needs_start_frame && key_frame > base_page_frame) { + start_frame = true; + best_frame = base_page_frame; + best_frame_track = i; + time_tracks[i].needs_start_frame = false; + break; + } + + ERR_FAIL_COND(key_frame < base_page_frame); // Sanity check, should never happen + + if (key_frame - base_page_frame >= max_frames_per_page) { + // Invalid because beyond the max frames allowed per page + best_invalid_frame = MIN(best_invalid_frame, key_frame); + } else if (key_frame < best_frame) { + best_frame = key_frame; + best_frame_track = i; + } + } + + print_animc("*KEY*: Current Frame: " + itos(current_frame) + " Best Frame: " + rtos(best_frame) + " Best Track: " + itos(best_frame_track) + " Start: " + String(start_frame ? "true" : "false")); + + if (!start_frame && best_frame > current_frame) { + // Any case where the current frame advanced, either because nothing was found or because something was found greater than the current one. + print_animc("\tAdvance Condition."); + bool rollback = false; + + // The frame has advanced, time to validate the previous frame + uint32_t current_page_size = base_page_size; + for (uint32_t i = 0; i < data_tracks.size(); i++) { + uint32_t track_size = data_tracks[i].data.size(); // track size + track_size += data_tracks[i].get_temp_packet_size(); // Add the temporary data + if (track_size > Compression::MAX_DATA_TRACK_SIZE) { + rollback = true; //track to large, time track can't point to keys any longer, because key offset is 12 bits + break; + } + current_page_size += track_size; + } + for (uint32_t i = 0; i < time_tracks.size(); i++) { + current_page_size += time_tracks[i].packets.size() * 4; // time packet is 32 bits + } + + if (!rollback && current_page_size > p_page_size) { + rollback = true; + } + + print_animc("\tCurrent Page Size: " + itos(current_page_size) + "/" + itos(p_page_size) + " Rollback? " + String(rollback ? "YES!" : "no")); + + if (rollback) { + // Not valid any longer, so rollback and commit page + + for (uint32_t i = 0; i < data_tracks.size(); i++) { + data_tracks[i].temp_packets.resize(data_tracks[i].validated_packet_count); + } + for (uint32_t i = 0; i < time_tracks.size(); i++) { + time_tracks[i].key_index = time_tracks[i].validated_key_index; //rollback key + time_tracks[i].packets.resize(time_tracks[i].validated_packet_count); + } + + } else { + // All valid, so save rollback information + for (uint32_t i = 0; i < data_tracks.size(); i++) { + data_tracks[i].validated_packet_count = data_tracks[i].temp_packets.size(); + } + for (uint32_t i = 0; i < time_tracks.size(); i++) { + time_tracks[i].validated_key_index = time_tracks[i].key_index; + time_tracks[i].validated_packet_count = time_tracks[i].packets.size(); + } + + // Accept this frame as the frame being processed (as long as it exists) + if (best_frame != FRAME_MAX) { + current_frame = best_frame; + print_animc("\tValidated, New Current Frame: " + itos(current_frame)); + } + } + + if (rollback || best_frame == FRAME_MAX) { + // Commit the page if had to rollback or if no track was found + print_animc("\tCommiting page.."); + + // The end frame for the page depends entirely on whether its valid or + // no more keys were found. + // If not valid, then the end frame is the current frame (as this means the current frame is being rolled back + // If valid, then the end frame is the next invalid one (in case more frames exist), or the current frame in case no more frames exist. + uint32_t page_end_frame = (rollback || best_frame == FRAME_MAX) ? current_frame : best_invalid_frame; + + print_animc("\tEnd Frame: " + itos(page_end_frame) + ", " + rtos(page_end_frame * frame_len) + "s"); + + // Add finalizer frames and commit pending tracks + uint32_t finalizer_local_frame = page_end_frame - base_page_frame; + + uint32_t total_page_size = 0; + + for (uint32_t i = 0; i < data_tracks.size(); i++) { + if (data_tracks[i].temp_packets.size() == 0 || (data_tracks[i].temp_packets[data_tracks[i].temp_packets.size() - 1].frame) < finalizer_local_frame) { + // Add finalizer frame if it makes sense + Vector3i values = _compress_key(tracks_to_compress[i], track_bounds[i], -1, page_end_frame * frame_len); + + bool first_key = data_tracks[i].insert_key(finalizer_local_frame, values); + if (first_key) { + AnimationCompressionTimeState::Packet p; + p.count = 1; + p.frame = finalizer_local_frame; + p.offset = data_tracks[i].data.size(); + time_tracks[i].packets.push_back(p); + } else { + ERR_FAIL_COND(time_tracks[i].packets.size() == 0); + time_tracks[i].packets[time_tracks[i].packets.size() - 1].count++; + } } - if (Math::abs(lt - t[k]) > p_alowed_linear_err) { - erase = false; - break; + data_tracks[i].commit_temp_packets(); + total_page_size += data_tracks[i].data.size(); + total_page_size += time_tracks[i].packets.size() * 4; + total_page_size += track_header_size; + + print_animc("\tTrack " + itos(i) + " time packets: " + itos(time_tracks[i].packets.size()) + " Packet data: " + itos(data_tracks[i].data.size())); + } + + print_animc("\tTotal page Size: " + itos(total_page_size) + "/" + itos(p_page_size)); + + // Create Page + Vector<uint8_t> page_data; + page_data.resize(total_page_size); + { + uint8_t *page_ptr = page_data.ptrw(); + uint32_t base_offset = data_tracks.size() * track_header_size; + + for (uint32_t i = 0; i < data_tracks.size(); i++) { + encode_uint32(base_offset, page_ptr + (track_header_size * i + 0)); + uint16_t *key_time_ptr = (uint16_t *)(page_ptr + base_offset); + for (uint32_t j = 0; j < time_tracks[i].packets.size(); j++) { + key_time_ptr[j * 2 + 0] = uint16_t(time_tracks[i].packets[j].frame); + uint16_t ptr = time_tracks[i].packets[j].offset / 4; + ptr |= (time_tracks[i].packets[j].count - 1) << 12; + key_time_ptr[j * 2 + 1] = ptr; + base_offset += 4; + } + encode_uint32(time_tracks[i].packets.size(), page_ptr + (track_header_size * i + 4)); + encode_uint32(base_offset, page_ptr + (track_header_size * i + 8)); + memcpy(page_ptr + base_offset, data_tracks[i].data.ptr(), data_tracks[i].data.size()); + base_offset += data_tracks[i].data.size(); + + //reset track + data_tracks[i].data.clear(); + data_tracks[i].temp_packets.clear(); + data_tracks[i].validated_packet_count = -1; + + time_tracks[i].needs_start_frame = true; //Not required the first time, but from now on it is. + time_tracks[i].packets.clear(); + time_tracks[i].validated_key_index = -1; + time_tracks[i].validated_packet_count = 0; } } - break; + + Compression::Page page; + page.data = page_data; + page.time_offset = base_page_frame * frame_len; + compression.pages.push_back(page); + + if (!rollback && best_invalid_frame == FRAME_MAX) { + break; // No more pages to add. + } + + current_frame = page_end_frame; + base_page_frame = page_end_frame; + + continue; // Start over } } - ERR_FAIL_COND_V(lt == -1, false); + // A key was found for the current frame and all is ok - if (erase) { - if (Math::abs(lt - c) > p_alowed_linear_err) { - //todo, evaluate changing the transition if this fails? - //this could be done as a second pass and would be - //able to optimize more - erase = false; + uint32_t comp_track = best_frame_track; + Vector3i values; + + if (start_frame) { + // Interpolate + values = _compress_key(tracks_to_compress[comp_track], track_bounds[comp_track], -1, base_page_frame * frame_len); + } else { + uint32_t key = time_tracks[comp_track].key_index; + values = _compress_key(tracks_to_compress[comp_track], track_bounds[comp_track], key); + time_tracks[comp_track].key_index++; //goto next key (but could be rolled back if beyond page size). + } + + bool first_key = data_tracks[comp_track].insert_key(best_frame - base_page_frame, values); + if (first_key) { + AnimationCompressionTimeState::Packet p; + p.count = 1; + p.frame = best_frame - base_page_frame; + p.offset = data_tracks[comp_track].data.size(); + time_tracks[comp_track].packets.push_back(p); + } else { + ERR_CONTINUE(time_tracks[comp_track].packets.size() == 0); + time_tracks[comp_track].packets[time_tracks[comp_track].packets.size() - 1].count++; + } + } + + compression.bounds = track_bounds; + compression.fps = p_fps; + compression.enabled = true; + + for (uint32_t i = 0; i < tracks_to_compress.size(); i++) { + Track *t = tracks[tracks_to_compress[i]]; + t->interpolation = INTERPOLATION_LINEAR; //only linear supported + switch (t->type) { + case TYPE_POSITION_3D: { + PositionTrack *tt = static_cast<PositionTrack *>(t); + tt->positions.clear(); + tt->compressed_track = i; + } break; + case TYPE_ROTATION_3D: { + RotationTrack *rt = static_cast<RotationTrack *>(t); + rt->rotations.clear(); + rt->compressed_track = i; + } break; + case TYPE_SCALE_3D: { + ScaleTrack *st = static_cast<ScaleTrack *>(t); + st->scales.clear(); + st->compressed_track = i; + print_line("Scale Bounds " + itos(i) + ": " + track_bounds[i]); + } break; + case TYPE_BLEND_SHAPE: { + BlendShapeTrack *bst = static_cast<BlendShapeTrack *>(t); + bst->blend_shapes.clear(); + bst->compressed_track = i; + } break; + default: { + } + } + } +#if 1 + uint32_t orig_size = 0; + for (int i = 0; i < get_track_count(); i++) { + switch (track_get_type(i)) { + case TYPE_SCALE_3D: + case TYPE_POSITION_3D: { + orig_size += sizeof(TKey<Vector3>) * track_get_key_count(i); + } break; + case TYPE_ROTATION_3D: { + orig_size += sizeof(TKey<Quaternion>) * track_get_key_count(i); + } break; + case TYPE_BLEND_SHAPE: { + orig_size += sizeof(TKey<float>) * track_get_key_count(i); + } break; + default: { } } } - return erase; + uint32_t new_size = 0; + for (uint32_t i = 0; i < compression.pages.size(); i++) { + new_size += compression.pages[i].data.size(); + } + + print_line("Original size: " + itos(orig_size) + " - Compressed size: " + itos(new_size) + " " + String::num(float(new_size) / float(orig_size) * 100, 2) + "% pages: " + itos(compression.pages.size())); +#endif } -void Animation::_transform_track_optimize(int p_idx, real_t p_allowed_linear_err, real_t p_allowed_angular_err, real_t p_max_optimizable_angle) { - ERR_FAIL_INDEX(p_idx, tracks.size()); - ERR_FAIL_COND(tracks[p_idx]->type != TYPE_TRANSFORM3D); - TransformTrack *tt = static_cast<TransformTrack *>(tracks[p_idx]); - bool prev_erased = false; - TKey<TransformKey> first_erased; +bool Animation::_rotation_interpolate_compressed(uint32_t p_compressed_track, double p_time, Quaternion &r_ret) const { + Vector3i current; + Vector3i next; + double time_current; + double time_next; - Vector3 norm; + if (!_fetch_compressed<3>(p_compressed_track, p_time, current, time_current, next, time_next)) { + return false; //some sort of problem + } - for (int i = 1; i < tt->transforms.size() - 1; i++) { - TKey<TransformKey> &t0 = tt->transforms.write[i - 1]; - TKey<TransformKey> &t1 = tt->transforms.write[i]; - TKey<TransformKey> &t2 = tt->transforms.write[i + 1]; + if (time_current >= p_time || time_current == time_next) { + r_ret = _uncompress_quaternion(current); + } else if (p_time >= time_next) { + r_ret = _uncompress_quaternion(next); + } else { + double c = (p_time - time_current) / (time_next - time_current); + Quaternion from = _uncompress_quaternion(current); + Quaternion to = _uncompress_quaternion(next); + r_ret = from.slerp(to, c); + } - bool erase = _transform_track_optimize_key(t0, t1, t2, p_allowed_linear_err, p_allowed_angular_err, p_max_optimizable_angle, norm); - if (erase && !prev_erased) { - norm = (t2.value.loc - t1.value.loc).normalized(); + return true; +} + +bool Animation::_pos_scale_interpolate_compressed(uint32_t p_compressed_track, double p_time, Vector3 &r_ret) const { + Vector3i current; + Vector3i next; + double time_current; + double time_next; + + if (!_fetch_compressed<3>(p_compressed_track, p_time, current, time_current, next, time_next)) { + return false; //some sort of problem + } + + if (time_current >= p_time || time_current == time_next) { + r_ret = _uncompress_pos_scale(p_compressed_track, current); + } else if (p_time >= time_next) { + r_ret = _uncompress_pos_scale(p_compressed_track, next); + } else { + double c = (p_time - time_current) / (time_next - time_current); + Vector3 from = _uncompress_pos_scale(p_compressed_track, current); + Vector3 to = _uncompress_pos_scale(p_compressed_track, next); + r_ret = from.lerp(to, c); + } + + return true; +} +bool Animation::_blend_shape_interpolate_compressed(uint32_t p_compressed_track, double p_time, float &r_ret) const { + Vector3i current; + Vector3i next; + double time_current; + double time_next; + + if (!_fetch_compressed<1>(p_compressed_track, p_time, current, time_current, next, time_next)) { + return false; //some sort of problem + } + + if (time_current >= p_time || time_current == time_next) { + r_ret = _uncompress_blend_shape(current); + } else if (p_time >= time_next) { + r_ret = _uncompress_blend_shape(next); + } else { + float c = (p_time - time_current) / (time_next - time_current); + float from = _uncompress_blend_shape(current); + float to = _uncompress_blend_shape(next); + r_ret = Math::lerp(from, to, c); + } + + return true; +} + +template <uint32_t COMPONENTS> +bool Animation::_fetch_compressed(uint32_t p_compressed_track, double p_time, Vector3i &r_current_value, double &r_current_time, Vector3i &r_next_value, double &r_next_time, uint32_t *key_index) const { + ERR_FAIL_COND_V(!compression.enabled, false); + ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), false); + p_time = CLAMP(p_time, 0, length); + if (key_index) { + *key_index = 0; + } + + double frame_to_sec = 1.0 / double(compression.fps); + + int32_t page_index = -1; + for (uint32_t i = 0; i < compression.pages.size(); i++) { + if (compression.pages[i].time_offset > p_time) { + break; } + page_index = i; + } - if (prev_erased && !_transform_track_optimize_key(t0, first_erased, t2, p_allowed_linear_err, p_allowed_angular_err, p_max_optimizable_angle, norm)) { - //avoid error to go beyond first erased key - erase = false; + ERR_FAIL_COND_V(page_index == -1, false); //should not happen + + double page_base_time = compression.pages[page_index].time_offset; + const uint8_t *page_data = compression.pages[page_index].data.ptr(); +#ifndef _MSC_VER +#warning Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported +#endif + const uint32_t *indices = (const uint32_t *)page_data; + const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]]; + uint32_t time_key_count = indices[p_compressed_track * 3 + 1]; + + int32_t packet_idx = 0; + double packet_time = double(time_keys[0]) * frame_to_sec + page_base_time; + uint32_t base_frame = time_keys[0]; + + for (uint32_t i = 1; i < time_key_count; i++) { + uint32_t f = time_keys[i * 2 + 0]; + double frame_time = double(f) * frame_to_sec + page_base_time; + + if (frame_time > p_time) { + break; } - if (erase) { - if (!prev_erased) { - first_erased = t1; - prev_erased = true; + if (key_index) { + (*key_index) += (time_keys[(i - 1) * 2 + 1] >> 12) + 1; + } + + packet_idx = i; + packet_time = frame_time; + base_frame = f; + } + + const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]]; + + uint16_t time_key_data = time_keys[packet_idx * 2 + 1]; + uint32_t data_offset = (time_key_data & 0xFFF) * 4; // lower 12 bits + uint32_t data_count = (time_key_data >> 12) + 1; + + const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset); + + uint16_t decode[COMPONENTS]; + uint16_t decode_next[COMPONENTS]; + + for (uint32_t i = 0; i < COMPONENTS; i++) { + decode[i] = data_key[i]; + decode_next[i] = data_key[i]; + } + + double next_time = packet_time; + + if (p_time > packet_time) { // If its equal or less, then don't bother + if (data_count > 1) { + //decode forward + uint32_t bit_width[COMPONENTS]; + for (uint32_t i = 0; i < COMPONENTS; i++) { + bit_width[i] = (data_key[COMPONENTS] >> (i * 4)) & 0xF; } - tt->transforms.remove(i); - i--; + uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1; - } else { - prev_erased = false; - norm = Vector3(); + AnimationCompressionBufferBitsRead buffer; + + buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1]; + + for (uint32_t i = 1; i < data_count; i++) { + uint32_t frame_delta = buffer.read(frame_bit_width); + base_frame += frame_delta; + + for (uint32_t j = 0; j < COMPONENTS; j++) { + if (bit_width[j] == 0) { + continue; // do none + } + uint32_t valueu = buffer.read(bit_width[j] + 1); + bool sign = valueu & (1 << bit_width[j]); + int16_t value = valueu & ((1 << bit_width[j]) - 1); + if (sign) { + value = -value - 1; + } + + decode_next[j] += value; + } + + next_time = double(base_frame) * frame_to_sec + page_base_time; + if (p_time < next_time) { + break; + } + + packet_time = next_time; + + for (uint32_t j = 0; j < COMPONENTS; j++) { + decode[j] = decode_next[j]; + } + + if (key_index) { + (*key_index)++; + } + } } + + if (p_time > next_time) { // > instead of >= because if its equal, then it will be properly interpolated anyway + // So, the last frame found still has a time that is less than the required frame, + // will have to interpolate with the first frame of the next timekey. + + if ((uint32_t)packet_idx < time_key_count - 1) { // Sanity check but should not matter much, otherwise current next packet is last packet + + uint16_t time_key_data_next = time_keys[(packet_idx + 1) * 2 + 1]; + uint32_t data_offset_next = (time_key_data_next & 0xFFF) * 4; // Lower 12 bits + + const uint16_t *data_key_next = (const uint16_t *)(data_keys_base + data_offset_next); + base_frame = time_keys[(packet_idx + 1) * 2 + 0]; + next_time = double(base_frame) * frame_to_sec + page_base_time; + for (uint32_t i = 0; i < COMPONENTS; i++) { + decode_next[i] = data_key_next[i]; + } + } + } + } + + r_current_time = packet_time; + r_next_time = next_time; + + for (uint32_t i = 0; i < COMPONENTS; i++) { + r_current_value[i] = decode[i]; + r_next_value[i] = decode_next[i]; } + + return true; } -void Animation::optimize(real_t p_allowed_linear_err, real_t p_allowed_angular_err, real_t p_max_optimizable_angle) { - for (int i = 0; i < tracks.size(); i++) { - if (tracks[i]->type == TYPE_TRANSFORM3D) { - _transform_track_optimize(i, p_allowed_linear_err, p_allowed_angular_err, p_max_optimizable_angle); +template <uint32_t COMPONENTS> +void Animation::_get_compressed_key_indices_in_range(uint32_t p_compressed_track, double p_time, double p_delta, List<int> *r_indices) const { + ERR_FAIL_COND(!compression.enabled); + ERR_FAIL_UNSIGNED_INDEX(p_compressed_track, compression.bounds.size()); + + double frame_to_sec = 1.0 / double(compression.fps); + uint32_t key_index = 0; + + for (uint32_t p = 0; p < compression.pages.size(); p++) { + if (compression.pages[p].time_offset >= p_time + p_delta) { + // Page beyond range + return; + } + + // Page within range + + uint32_t page_index = p; + + double page_base_time = compression.pages[page_index].time_offset; + const uint8_t *page_data = compression.pages[page_index].data.ptr(); +#ifndef _MSC_VER +#warning Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported +#endif + const uint32_t *indices = (const uint32_t *)page_data; + const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]]; + uint32_t time_key_count = indices[p_compressed_track * 3 + 1]; + + for (uint32_t i = 0; i < time_key_count; i++) { + uint32_t f = time_keys[i * 2 + 0]; + double frame_time = f * frame_to_sec + page_base_time; + if (frame_time >= p_time + p_delta) { + return; + } else if (frame_time >= p_time) { + r_indices->push_back(key_index); + } + + key_index++; + + const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]]; + + uint16_t time_key_data = time_keys[i * 2 + 1]; + uint32_t data_offset = (time_key_data & 0xFFF) * 4; // lower 12 bits + uint32_t data_count = (time_key_data >> 12) + 1; + + const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset); + + if (data_count > 1) { + //decode forward + uint32_t bit_width[COMPONENTS]; + for (uint32_t j = 0; j < COMPONENTS; j++) { + bit_width[j] = (data_key[COMPONENTS] >> (j * 4)) & 0xF; + } + + uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1; + + AnimationCompressionBufferBitsRead buffer; + + buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1]; + + for (uint32_t j = 1; j < data_count; j++) { + uint32_t frame_delta = buffer.read(frame_bit_width); + f += frame_delta; + + frame_time = f * frame_to_sec + page_base_time; + if (frame_time >= p_time + p_delta) { + return; + } else if (frame_time >= p_time) { + r_indices->push_back(key_index); + } + + for (uint32_t k = 0; k < COMPONENTS; k++) { + if (bit_width[k] == 0) { + continue; // do none + } + buffer.read(bit_width[k] + 1); // skip + } + + key_index++; + } + } + } + } +} + +int Animation::_get_compressed_key_count(uint32_t p_compressed_track) const { + ERR_FAIL_COND_V(!compression.enabled, -1); + ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), -1); + + int key_count = 0; + + for (uint32_t i = 0; i < compression.pages.size(); i++) { + const uint8_t *page_data = compression.pages[i].data.ptr(); +#ifndef _MSC_VER +#warning Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported +#endif + const uint32_t *indices = (const uint32_t *)page_data; + const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]]; + uint32_t time_key_count = indices[p_compressed_track * 3 + 1]; + + for (uint32_t j = 0; j < time_key_count; j++) { + key_count += (time_keys[j * 2 + 1] >> 12) + 1; } } + + return key_count; +} + +Quaternion Animation::_uncompress_quaternion(const Vector3i &p_value) const { + Vector3 axis = Vector3::octahedron_decode(Vector2(float(p_value.x) / 65535.0, float(p_value.y) / 65535.0)); + float angle = (float(p_value.z) / 65535.0) * 2.0 * Math_PI; + return Quaternion(axis, angle); +} +Vector3 Animation::_uncompress_pos_scale(uint32_t p_compressed_track, const Vector3i &p_value) const { + Vector3 pos_norm(float(p_value.x) / 65535.0, float(p_value.y) / 65535.0, float(p_value.z) / 65535.0); + return compression.bounds[p_compressed_track].position + pos_norm * compression.bounds[p_compressed_track].size; +} +float Animation::_uncompress_blend_shape(const Vector3i &p_value) const { + float bsn = float(p_value.x) / 65535.0; + return (bsn * 2.0 - 1.0) * float(Compression::BLEND_SHAPE_RANGE); +} + +template <uint32_t COMPONENTS> +bool Animation::_fetch_compressed_by_index(uint32_t p_compressed_track, int p_index, Vector3i &r_value, double &r_time) const { + ERR_FAIL_COND_V(!compression.enabled, false); + ERR_FAIL_UNSIGNED_INDEX_V(p_compressed_track, compression.bounds.size(), false); + + for (uint32_t i = 0; i < compression.pages.size(); i++) { + const uint8_t *page_data = compression.pages[i].data.ptr(); +#ifndef _MSC_VER +#warning Little endian assumed. No major big endian hardware exists any longer, but in case it does it will need to be supported +#endif + const uint32_t *indices = (const uint32_t *)page_data; + const uint16_t *time_keys = (const uint16_t *)&page_data[indices[p_compressed_track * 3 + 0]]; + uint32_t time_key_count = indices[p_compressed_track * 3 + 1]; + const uint8_t *data_keys_base = (const uint8_t *)&page_data[indices[p_compressed_track * 3 + 2]]; + + for (uint32_t j = 0; j < time_key_count; j++) { + uint32_t subkeys = (time_keys[j * 2 + 1] >> 12) + 1; + if ((uint32_t)p_index < subkeys) { + uint16_t data_offset = (time_keys[j * 2 + 1] & 0xFFF) * 4; + + const uint16_t *data_key = (const uint16_t *)(data_keys_base + data_offset); + + uint16_t frame = time_keys[j * 2 + 0]; + uint16_t decode[COMPONENTS]; + + for (uint32_t k = 0; k < COMPONENTS; k++) { + decode[k] = data_key[k]; + } + + if (p_index > 0) { + uint32_t bit_width[COMPONENTS]; + for (uint32_t k = 0; k < COMPONENTS; k++) { + bit_width[k] = (data_key[COMPONENTS] >> (k * 4)) & 0xF; + } + uint32_t frame_bit_width = (data_key[COMPONENTS] >> 12) + 1; + + AnimationCompressionBufferBitsRead buffer; + buffer.src_data = (const uint8_t *)&data_key[COMPONENTS + 1]; + + for (int k = 0; k < p_index; k++) { + uint32_t frame_delta = buffer.read(frame_bit_width); + frame += frame_delta; + for (uint32_t l = 0; l < COMPONENTS; l++) { + if (bit_width[l] == 0) { + continue; // do none + } + uint32_t valueu = buffer.read(bit_width[l] + 1); + bool sign = valueu & (1 << bit_width[l]); + int16_t value = valueu & ((1 << bit_width[l]) - 1); + if (sign) { + value = -value - 1; + } + + decode[l] += value; + } + } + } + + r_time = compression.pages[i].time_offset + double(frame) / double(compression.fps); + for (uint32_t l = 0; l < COMPONENTS; l++) { + r_value[l] = decode[l]; + } + + return true; + + } else { + p_index -= subkeys; + } + } + } + + return false; } Animation::Animation() {} diff --git a/scene/resources/animation.h b/scene/resources/animation.h index 9a410bd566..ee07fb19d3 100644 --- a/scene/resources/animation.h +++ b/scene/resources/animation.h @@ -32,6 +32,7 @@ #define ANIMATION_H #include "core/io/resource.h" +#include "core/templates/local_vector.h" #define ANIM_MIN_LENGTH 0.001 @@ -42,7 +43,10 @@ class Animation : public Resource { public: enum TrackType { TYPE_VALUE, ///< Set a value in a property, can be interpolated. - TYPE_TRANSFORM3D, ///< Transform a node or a bone. + TYPE_POSITION_3D, ///< Position 3D track + TYPE_ROTATION_3D, ///< Rotation 3D track + TYPE_SCALE_3D, ///< Scale 3D track + TYPE_BLEND_SHAPE, ///< Blend Shape track TYPE_METHOD, ///< Call any method on a specific node. TYPE_BEZIER, ///< Bezier curve TYPE_AUDIO, @@ -86,21 +90,41 @@ private: T value; }; - struct TransformKey { - Vector3 loc; - Quaternion rot; - Vector3 scale; + const int32_t POSITION_TRACK_SIZE = 5; + const int32_t ROTATION_TRACK_SIZE = 6; + const int32_t SCALE_TRACK_SIZE = 5; + const int32_t BLEND_SHAPE_TRACK_SIZE = 3; + + /* POSITION TRACK */ + + struct PositionTrack : public Track { + Vector<TKey<Vector3>> positions; + int32_t compressed_track = -1; + PositionTrack() { type = TYPE_POSITION_3D; } + }; + + /* ROTATION TRACK */ + + struct RotationTrack : public Track { + Vector<TKey<Quaternion>> rotations; + int32_t compressed_track = -1; + RotationTrack() { type = TYPE_ROTATION_3D; } }; - // Not necessarily the same size as Transform3D. The amount of numbers in Animation::Key and TransformKey. - const int32_t TRANSFORM_TRACK_SIZE = 12; + /* SCALE TRACK */ - /* TRANSFORM TRACK */ + struct ScaleTrack : public Track { + Vector<TKey<Vector3>> scales; + int32_t compressed_track = -1; + ScaleTrack() { type = TYPE_SCALE_3D; } + }; - struct TransformTrack : public Track { - Vector<TKey<TransformKey>> transforms; + /* BLEND SHAPE TRACK */ - TransformTrack() { type = TYPE_TRANSFORM3D; } + struct BlendShapeTrack : public Track { + Vector<TKey<float>> blend_shapes; + int32_t compressed_track = -1; + BlendShapeTrack() { type = TYPE_BLEND_SHAPE; } }; /* PROPERTY VALUE TRACK */ @@ -186,14 +210,11 @@ private: template <class K> inline int _find(const Vector<K> &p_keys, double p_time) const; - _FORCE_INLINE_ Animation::TransformKey _interpolate(const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, real_t p_c) const; - _FORCE_INLINE_ Vector3 _interpolate(const Vector3 &p_a, const Vector3 &p_b, real_t p_c) const; _FORCE_INLINE_ Quaternion _interpolate(const Quaternion &p_a, const Quaternion &p_b, real_t p_c) const; _FORCE_INLINE_ Variant _interpolate(const Variant &p_a, const Variant &p_b, real_t p_c) const; _FORCE_INLINE_ real_t _interpolate(const real_t &p_a, const real_t &p_b, real_t p_c) const; - _FORCE_INLINE_ Animation::TransformKey _cubic_interpolate(const Animation::TransformKey &p_pre_a, const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, const Animation::TransformKey &p_post_b, real_t p_c) const; _FORCE_INLINE_ Vector3 _cubic_interpolate(const Vector3 &p_pre_a, const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_post_b, real_t p_c) const; _FORCE_INLINE_ Quaternion _cubic_interpolate(const Quaternion &p_pre_a, const Quaternion &p_a, const Quaternion &p_b, const Quaternion &p_post_b, real_t p_c) const; _FORCE_INLINE_ Variant _cubic_interpolate(const Variant &p_pre_a, const Variant &p_a, const Variant &p_b, const Variant &p_post_b, real_t p_c) const; @@ -212,20 +233,91 @@ private: real_t step = 0.1; bool loop = false; + /* Animation compression page format (version 1): + * + * Animation uses bitwidth based compression separated into small pages. The intention is that pages fit easily in the cache, so decoding is cache efficient. + * The page-based nature also makes future animation streaming from disk possible. + * + * Actual format: + * + * num_compressed_tracks = bounds.size() + * header : (x num_compressed_tracks) + * ------- + * timeline_keys_offset : uint32_t - offset to time keys + * timeline_size : uint32_t - amount of time keys + * data_keys_offset : uint32_t offset to key data + * + * time key (uint32_t): + * ------------------ + * frame : bits 0-15 - time offset of key, computed as: page.time_offset + frame * (1.0/fps) + * data_key_offset : bits 16-27 - offset to key data, computed as: data_keys_offset * 4 + data_key_offset + * data_key_count : bits 28-31 - amount of data keys pointed to, computed as: data_key_count+1 (max 16) + * + * data key: + * --------- + * X / Blend Shape : uint16_t - X coordinate of XYZ vector key, or Blend Shape value. If Blend shape, Y and Z are not present and can be ignored. + * Y : uint16_t + * Z : uint16_t + * If data_key_count+1 > 1 (if more than 1 key is stored): + * data_bitwidth : uint16_t - This is only present if data_key_count > 1. Contains delta bitwidth information. + * X / Blend Shape delta bitwidth: bits 0-3 - + * if 0, nothing is present for X (use the first key-value for subsequent keys), + * else assume the number of bits present for each element (+ 1 for sign). Assumed always 16 bits, delta max signed 15 bits, with underflow and overflow supported. + * Y delta bitwidth : bits 4-7 + * Z delta bitwidth : bits 8-11 + * FRAME delta bitwidth : 12-15 bits - always present (obviously), actual bitwidth is FRAME+1 + * Data key is 4 bytes long for Blend Shapes, 8 bytes long for pos/rot/scale. + * + * delta keys: + * ----------- + * Compressed format is packed in the following format after the data key, containing delta keys one after the next in a tightly bit packed fashion. + * FRAME bits -> X / Blend Shape Bits (if bitwidth > 0) -> Y Bits (if not Blend Shape and Y Bitwidth > 0) -> Z Bits (if not Blend Shape and Z Bitwidth > 0) + * + * data key format: + * ---------------- + * Decoding keys means starting from the base key and going key by key applying deltas until the proper position is reached needed for interpolation. + * Resulting values are uint32_t + * data for X / Blend Shape, Y and Z must be normalized first: unorm = float(data) / 65535.0 + * **Blend Shape**: (unorm * 2.0 - 1.0) * Compression::BLEND_SHAPE_RANGE + * **Pos/Scale**: unorm_vec3 * bounds[track].size + bounds[track].position + * **Rotation**: Quaternion(Vector3::octahedron_decode(unorm_vec3.xy),unorm_vec3.z * Math_PI * 2.0) + * **Frame**: page.time_offset + frame * (1.0/fps) + */ + + struct Compression { + enum { + MAX_DATA_TRACK_SIZE = 16384, + BLEND_SHAPE_RANGE = 8, // - 8.0 to 8.0 + FORMAT_VERSION = 1 + }; + struct Page { + Vector<uint8_t> data; + double time_offset; + }; + + uint32_t fps = 120; + LocalVector<Page> pages; + LocalVector<AABB> bounds; //used by position and scale tracks (which contain index to track and index to bounds). + bool enabled = false; + } compression; + + Vector3i _compress_key(uint32_t p_track, const AABB &p_bounds, int32_t p_key = -1, float p_time = 0.0); + bool _rotation_interpolate_compressed(uint32_t p_compressed_track, double p_time, Quaternion &r_ret) const; + bool _pos_scale_interpolate_compressed(uint32_t p_compressed_track, double p_time, Vector3 &r_ret) const; + bool _blend_shape_interpolate_compressed(uint32_t p_compressed_track, double p_time, float &r_ret) const; + template <uint32_t COMPONENTS> + bool _fetch_compressed(uint32_t p_compressed_track, double p_time, Vector3i &r_current_value, double &r_current_time, Vector3i &r_next_value, double &r_next_time, uint32_t *key_index = nullptr) const; + template <uint32_t COMPONENTS> + bool _fetch_compressed_by_index(uint32_t p_compressed_track, int p_index, Vector3i &r_value, double &r_time) const; + int _get_compressed_key_count(uint32_t p_compressed_track) const; + template <uint32_t COMPONENTS> + void _get_compressed_key_indices_in_range(uint32_t p_compressed_track, double p_time, double p_delta, List<int> *r_indices) const; + _FORCE_INLINE_ Quaternion _uncompress_quaternion(const Vector3i &p_value) const; + _FORCE_INLINE_ Vector3 _uncompress_pos_scale(uint32_t p_compressed_track, const Vector3i &p_value) const; + _FORCE_INLINE_ float _uncompress_blend_shape(const Vector3i &p_value) const; + // bind helpers private: - Array _transform_track_interpolate(int p_track, double p_time) const { - Vector3 loc; - Quaternion rot; - Vector3 scale; - transform_track_interpolate(p_track, p_time, &loc, &rot, &scale); - Array ret; - ret.push_back(loc); - ret.push_back(rot); - ret.push_back(scale); - return ret; - } - Vector<int> _value_track_get_key_indices(int p_track, double p_time, double p_delta) const { List<int> idxs; value_track_get_key_indices(p_track, p_time, p_delta, &idxs); @@ -247,8 +339,15 @@ private: return idxr; } - bool _transform_track_optimize_key(const TKey<TransformKey> &t0, const TKey<TransformKey> &t1, const TKey<TransformKey> &t2, real_t p_alowed_linear_err, real_t p_alowed_angular_err, real_t p_max_optimizable_angle, const Vector3 &p_norm); - void _transform_track_optimize(int p_idx, real_t p_allowed_linear_err = 0.05, real_t p_allowed_angular_err = 0.01, real_t p_max_optimizable_angle = Math_PI * 0.125); + bool _position_track_optimize_key(const TKey<Vector3> &t0, const TKey<Vector3> &t1, const TKey<Vector3> &t2, real_t p_alowed_linear_err, real_t p_allowed_angular_error, const Vector3 &p_norm); + bool _rotation_track_optimize_key(const TKey<Quaternion> &t0, const TKey<Quaternion> &t1, const TKey<Quaternion> &t2, real_t p_allowed_angular_error, float p_max_optimizable_angle); + bool _scale_track_optimize_key(const TKey<Vector3> &t0, const TKey<Vector3> &t1, const TKey<Vector3> &t2, real_t p_allowed_linear_error); + bool _blend_shape_track_optimize_key(const TKey<float> &t0, const TKey<float> &t1, const TKey<float> &t2, real_t p_allowed_unit_error); + + void _position_track_optimize(int p_idx, real_t p_allowed_linear_err, real_t p_allowed_angular_err); + void _rotation_track_optimize(int p_idx, real_t p_allowed_angular_err, real_t p_max_optimizable_angle); + void _scale_track_optimize(int p_idx, real_t p_allowed_linear_err); + void _blend_shape_track_optimize(int p_idx, real_t p_allowed_unit_error); protected: bool _set(const StringName &p_name, const Variant &p_value); @@ -268,8 +367,7 @@ public: void track_set_path(int p_track, const NodePath &p_path); NodePath track_get_path(int p_track) const; - int find_track(const NodePath &p_path) const; - // transform + int find_track(const NodePath &p_path, const TrackType p_type) const; void track_move_up(int p_track); void track_move_down(int p_track); @@ -293,9 +391,24 @@ public: Variant track_get_key_value(int p_track, int p_key_idx) const; double track_get_key_time(int p_track, int p_key_idx) const; real_t track_get_key_transition(int p_track, int p_key_idx) const; + bool track_is_compressed(int p_track) const; + + int position_track_insert_key(int p_track, double p_time, const Vector3 &p_position); + Error position_track_get_key(int p_track, int p_key, Vector3 *r_position) const; + Error position_track_interpolate(int p_track, double p_time, Vector3 *r_interpolation) const; + + int rotation_track_insert_key(int p_track, double p_time, const Quaternion &p_rotation); + Error rotation_track_get_key(int p_track, int p_key, Quaternion *r_rotation) const; + Error rotation_track_interpolate(int p_track, double p_time, Quaternion *r_interpolation) const; + + int scale_track_insert_key(int p_track, double p_time, const Vector3 &p_scale); + Error scale_track_get_key(int p_track, int p_key, Vector3 *r_scale) const; + Error scale_track_interpolate(int p_track, double p_time, Vector3 *r_interpolation) const; + + int blend_shape_track_insert_key(int p_track, double p_time, float p_blend); + Error blend_shape_track_get_key(int p_track, int p_key, float *r_blend) const; + Error blend_shape_track_interpolate(int p_track, double p_time, float *r_blend) const; - int transform_track_insert_key(int p_track, double p_time, const Vector3 &p_loc, const Quaternion &p_rot = Quaternion(), const Vector3 &p_scale = Vector3()); - Error transform_track_get_key(int p_track, int p_key, Vector3 *r_loc, Quaternion *r_rot, Vector3 *r_scale) const; void track_set_interpolation_type(int p_track, InterpolationType p_interp); InterpolationType track_get_interpolation_type(int p_track) const; @@ -324,8 +437,6 @@ public: void track_set_interpolation_loop_wrap(int p_track, bool p_enable); bool track_get_interpolation_loop_wrap(int p_track) const; - Error transform_track_interpolate(int p_track, double p_time, Vector3 *r_loc, Quaternion *r_rot, Vector3 *r_scale) const; - Variant value_track_interpolate(int p_track, double p_time) const; void value_track_get_key_indices(int p_track, double p_time, double p_delta, List<int> *p_indices) const; void value_track_set_update_mode(int p_track, UpdateMode p_mode); @@ -351,6 +462,7 @@ public: void clear(); void optimize(real_t p_allowed_linear_err = 0.05, real_t p_allowed_angular_err = 0.01, real_t p_max_optimizable_angle = Math_PI * 0.125); + void compress(uint32_t p_page_size = 8192, uint32_t p_fps = 120, float p_split_tolerance = 4.0); // 4.0 seems to be the split tolerance sweet spot from many tests Animation(); ~Animation(); diff --git a/scene/resources/default_theme/SCsub b/scene/resources/default_theme/SCsub index 0fb6bb2c62..3667ab7c14 100644 --- a/scene/resources/default_theme/SCsub +++ b/scene/resources/default_theme/SCsub @@ -2,8 +2,6 @@ Import("env") -import os -import os.path from platform_methods import run_in_subprocess import default_theme_builders diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 54bb7a82cf..f21a070133 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -147,6 +147,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const Color control_font_lower_color = Color(0.63, 0.63, 0.63); Color control_font_low_color = Color(0.69, 0.69, 0.69); Color control_font_hover_color = Color(0.94, 0.94, 0.94); + Color control_font_focus_color = Color(0.94, 0.94, 0.94); Color control_font_disabled_color = Color(0.9, 0.9, 0.9, 0.2); Color control_font_pressed_color = Color(1, 1, 1); @@ -185,6 +186,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_color("font_color", "Button", control_font_color); theme->set_color("font_pressed_color", "Button", control_font_pressed_color); theme->set_color("font_hover_color", "Button", control_font_hover_color); + theme->set_color("font_focus_color", "Button", control_font_focus_color); theme->set_color("font_hover_pressed_color", "Button", control_font_pressed_color); theme->set_color("font_disabled_color", "Button", control_font_disabled_color); theme->set_color("font_outline_color", "Button", Color(1, 1, 1)); @@ -193,6 +195,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_color("icon_pressed_color", "Button", Color(1, 1, 1, 1)); theme->set_color("icon_hover_color", "Button", Color(1, 1, 1, 1)); theme->set_color("icon_hover_pressed_color", "Button", Color(1, 1, 1, 1)); + theme->set_color("icon_focus_color", "Button", Color(1, 1, 1, 1)); theme->set_color("icon_disabled_color", "Button", Color(1, 1, 1, 1)); theme->set_constant("hseparation", "Button", 2 * scale); @@ -207,6 +210,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_color("font_color", "LinkButton", control_font_color); theme->set_color("font_pressed_color", "LinkButton", control_font_pressed_color); theme->set_color("font_hover_color", "LinkButton", control_font_hover_color); + theme->set_color("font_focus_color", "LinkButton", control_font_focus_color); theme->set_color("font_outline_color", "LinkButton", Color(1, 1, 1)); theme->set_constant("outline_size", "LinkButton", 0); @@ -245,6 +249,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_color("font_color", "OptionButton", control_font_color); theme->set_color("font_pressed_color", "OptionButton", control_font_pressed_color); theme->set_color("font_hover_color", "OptionButton", control_font_hover_color); + theme->set_color("font_focus_color", "OptionButton", control_font_focus_color); theme->set_color("font_disabled_color", "OptionButton", control_font_disabled_color); theme->set_color("font_outline_color", "OptionButton", Color(1, 1, 1)); @@ -266,6 +271,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_color("font_color", "MenuButton", control_font_color); theme->set_color("font_pressed_color", "MenuButton", control_font_pressed_color); theme->set_color("font_hover_color", "MenuButton", control_font_hover_color); + theme->set_color("font_focus_color", "MenuButton", control_font_focus_color); theme->set_color("font_disabled_color", "MenuButton", Color(1, 1, 1, 0.3)); theme->set_color("font_outline_color", "MenuButton", Color(1, 1, 1)); @@ -308,6 +314,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_color("font_pressed_color", "CheckBox", control_font_pressed_color); theme->set_color("font_hover_color", "CheckBox", control_font_hover_color); theme->set_color("font_hover_pressed_color", "CheckBox", control_font_pressed_color); + theme->set_color("font_focus_color", "CheckBox", control_font_focus_color); theme->set_color("font_disabled_color", "CheckBox", control_font_disabled_color); theme->set_color("font_outline_color", "CheckBox", Color(1, 1, 1)); @@ -347,6 +354,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_color("font_pressed_color", "CheckButton", control_font_pressed_color); theme->set_color("font_hover_color", "CheckButton", control_font_hover_color); theme->set_color("font_hover_pressed_color", "CheckButton", control_font_pressed_color); + theme->set_color("font_focus_color", "CheckButton", control_font_focus_color); theme->set_color("font_disabled_color", "CheckButton", control_font_disabled_color); theme->set_color("font_outline_color", "CheckButton", Color(1, 1, 1)); @@ -785,30 +793,30 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_constant("icon_separation", "TabContainer", 4 * scale); theme->set_constant("outline_size", "TabContainer", 0); - // Tabs + // TabBar - theme->set_stylebox("tab_selected", "Tabs", sb_expand(make_stylebox(tab_current_png, 4, 3, 4, 1, 16, 3, 16, 2), 2, 2, 2, 2)); - theme->set_stylebox("tab_unselected", "Tabs", sb_expand(make_stylebox(tab_behind_png, 5, 4, 5, 1, 16, 5, 16, 2), 3, 3, 3, 3)); - theme->set_stylebox("tab_disabled", "Tabs", sb_expand(make_stylebox(tab_disabled_png, 5, 5, 5, 1, 16, 6, 16, 4), 3, 0, 3, 3)); - theme->set_stylebox("close_bg_pressed", "Tabs", make_stylebox(button_pressed_png, 4, 4, 4, 4)); - theme->set_stylebox("close_bg_highlight", "Tabs", make_stylebox(button_normal_png, 4, 4, 4, 4)); + theme->set_stylebox("tab_selected", "TabBar", sb_expand(make_stylebox(tab_current_png, 4, 3, 4, 1, 16, 3, 16, 2), 2, 2, 2, 2)); + theme->set_stylebox("tab_unselected", "TabBar", sb_expand(make_stylebox(tab_behind_png, 5, 4, 5, 1, 16, 5, 16, 2), 3, 3, 3, 3)); + theme->set_stylebox("tab_disabled", "TabBar", sb_expand(make_stylebox(tab_disabled_png, 5, 5, 5, 1, 16, 6, 16, 4), 3, 0, 3, 3)); + theme->set_stylebox("close_bg_pressed", "TabBar", make_stylebox(button_pressed_png, 4, 4, 4, 4)); + theme->set_stylebox("close_bg_highlight", "TabBar", make_stylebox(button_normal_png, 4, 4, 4, 4)); - theme->set_icon("increment", "Tabs", make_icon(scroll_button_right_png)); - theme->set_icon("increment_highlight", "Tabs", make_icon(scroll_button_right_hl_png)); - theme->set_icon("decrement", "Tabs", make_icon(scroll_button_left_png)); - theme->set_icon("decrement_highlight", "Tabs", make_icon(scroll_button_left_hl_png)); - theme->set_icon("close", "Tabs", make_icon(tab_close_png)); + theme->set_icon("increment", "TabBar", make_icon(scroll_button_right_png)); + theme->set_icon("increment_highlight", "TabBar", make_icon(scroll_button_right_hl_png)); + theme->set_icon("decrement", "TabBar", make_icon(scroll_button_left_png)); + theme->set_icon("decrement_highlight", "TabBar", make_icon(scroll_button_left_hl_png)); + theme->set_icon("close", "TabBar", make_icon(tab_close_png)); - theme->set_font("font", "Tabs", Ref<Font>()); - theme->set_font_size("font_size", "Tabs", -1); + theme->set_font("font", "TabBar", Ref<Font>()); + theme->set_font_size("font_size", "TabBar", -1); - theme->set_color("font_selected_color", "Tabs", control_font_hover_color); - theme->set_color("font_unselected_color", "Tabs", control_font_low_color); - theme->set_color("font_disabled_color", "Tabs", control_font_disabled_color); - theme->set_color("font_outline_color", "Tabs", Color(1, 1, 1)); + theme->set_color("font_selected_color", "TabBar", control_font_hover_color); + theme->set_color("font_unselected_color", "TabBar", control_font_low_color); + theme->set_color("font_disabled_color", "TabBar", control_font_disabled_color); + theme->set_color("font_outline_color", "TabBar", Color(1, 1, 1)); - theme->set_constant("hseparation", "Tabs", 4 * scale); - theme->set_constant("outline_size", "Tabs", 0); + theme->set_constant("hseparation", "TabBar", 4 * scale); + theme->set_constant("outline_size", "TabBar", 0); // Separators @@ -867,6 +875,7 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_color("font_color", "ColorPickerButton", Color(1, 1, 1, 1)); theme->set_color("font_pressed_color", "ColorPickerButton", Color(0.8, 0.8, 0.8, 1)); theme->set_color("font_hover_color", "ColorPickerButton", Color(1, 1, 1, 1)); + theme->set_color("font_focus_color", "ColorPickerButton", Color(1, 1, 1, 1)); theme->set_color("font_disabled_color", "ColorPickerButton", Color(0.9, 0.9, 0.9, 0.3)); theme->set_color("font_outline_color", "ColorPickerButton", Color(1, 1, 1)); @@ -1030,7 +1039,6 @@ void make_default_theme(bool p_hidpi, Ref<Font> p_font) { dynamic_font_data.instantiate(); dynamic_font_data->set_data_ptr(_font_OpenSans_SemiBold, _font_OpenSans_SemiBold_size); dynamic_font->add_data(dynamic_font_data); - dynamic_font->set_base_size(default_font_size); default_font = dynamic_font; } diff --git a/scene/resources/environment.cpp b/scene/resources/environment.cpp index 9a3f081a8b..9f8e89564d 100644 --- a/scene/resources/environment.cpp +++ b/scene/resources/environment.cpp @@ -782,7 +782,7 @@ void Environment::_update_fog() { // Volumetric Fog void Environment::_update_volumetric_fog() { - RS::get_singleton()->environment_set_volumetric_fog(environment, volumetric_fog_enabled, volumetric_fog_density, volumetric_fog_light, volumetric_fog_light_energy, volumetric_fog_length, volumetric_fog_detail_spread, volumetric_fog_gi_inject, volumetric_fog_temporal_reproject, volumetric_fog_temporal_reproject_amount); + RS::get_singleton()->environment_set_volumetric_fog(environment, volumetric_fog_enabled, volumetric_fog_density, volumetric_fog_albedo, volumetric_fog_emission, volumetric_fog_emission_energy, volumetric_fog_anisotropy, volumetric_fog_length, volumetric_fog_detail_spread, volumetric_fog_gi_inject, volumetric_fog_temporal_reproject, volumetric_fog_temporal_reproject_amount, volumetric_fog_ambient_inject); } void Environment::set_volumetric_fog_enabled(bool p_enable) { @@ -795,26 +795,39 @@ bool Environment::is_volumetric_fog_enabled() const { return volumetric_fog_enabled; } void Environment::set_volumetric_fog_density(float p_density) { - p_density = CLAMP(p_density, 0.0000001, 1.0); volumetric_fog_density = p_density; _update_volumetric_fog(); } float Environment::get_volumetric_fog_density() const { return volumetric_fog_density; } -void Environment::set_volumetric_fog_light(Color p_color) { - volumetric_fog_light = p_color; +void Environment::set_volumetric_fog_albedo(Color p_color) { + volumetric_fog_albedo = p_color; _update_volumetric_fog(); } -Color Environment::get_volumetric_fog_light() const { - return volumetric_fog_light; +Color Environment::get_volumetric_fog_albedo() const { + return volumetric_fog_albedo; } -void Environment::set_volumetric_fog_light_energy(float p_begin) { - volumetric_fog_light_energy = p_begin; +void Environment::set_volumetric_fog_emission(Color p_color) { + volumetric_fog_emission = p_color; _update_volumetric_fog(); } -float Environment::get_volumetric_fog_light_energy() const { - return volumetric_fog_light_energy; +Color Environment::get_volumetric_fog_emission() const { + return volumetric_fog_emission; +} +void Environment::set_volumetric_fog_emission_energy(float p_begin) { + volumetric_fog_emission_energy = p_begin; + _update_volumetric_fog(); +} +float Environment::get_volumetric_fog_emission_energy() const { + return volumetric_fog_emission_energy; +} +void Environment::set_volumetric_fog_anisotropy(float p_anisotropy) { + volumetric_fog_anisotropy = p_anisotropy; + _update_volumetric_fog(); +} +float Environment::get_volumetric_fog_anisotropy() const { + return volumetric_fog_anisotropy; } void Environment::set_volumetric_fog_length(float p_length) { volumetric_fog_length = p_length; @@ -824,6 +837,7 @@ float Environment::get_volumetric_fog_length() const { return volumetric_fog_length; } void Environment::set_volumetric_fog_detail_spread(float p_detail_spread) { + p_detail_spread = CLAMP(p_detail_spread, 0.5, 6.0); volumetric_fog_detail_spread = p_detail_spread; _update_volumetric_fog(); } @@ -838,6 +852,13 @@ void Environment::set_volumetric_fog_gi_inject(float p_gi_inject) { float Environment::get_volumetric_fog_gi_inject() const { return volumetric_fog_gi_inject; } +void Environment::set_volumetric_fog_ambient_inject(float p_ambient_inject) { + volumetric_fog_ambient_inject = p_ambient_inject; + _update_volumetric_fog(); +} +float Environment::get_volumetric_fog_ambient_inject() const { + return volumetric_fog_ambient_inject; +} void Environment::set_volumetric_fog_temporal_reprojection_enabled(bool p_enable) { volumetric_fog_temporal_reproject = p_enable; @@ -1291,22 +1312,28 @@ void Environment::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fog_density", PROPERTY_HINT_RANGE, "0,16,0.0001"), "set_fog_density", "get_fog_density"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fog_aerial_perspective", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_fog_aerial_perspective", "get_fog_aerial_perspective"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fog_height", PROPERTY_HINT_RANGE, "-1024,1024,0.01,or_lesser,or_greater"), "set_fog_height", "get_fog_height"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fog_height_density", PROPERTY_HINT_RANGE, "0,128,0.001,or_greater"), "set_fog_height_density", "get_fog_height_density"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fog_height_density", PROPERTY_HINT_RANGE, "-16,16,0.0001,or_lesser,or_greater"), "set_fog_height_density", "get_fog_height_density"); ClassDB::bind_method(D_METHOD("set_volumetric_fog_enabled", "enabled"), &Environment::set_volumetric_fog_enabled); ClassDB::bind_method(D_METHOD("is_volumetric_fog_enabled"), &Environment::is_volumetric_fog_enabled); - ClassDB::bind_method(D_METHOD("set_volumetric_fog_light", "color"), &Environment::set_volumetric_fog_light); - ClassDB::bind_method(D_METHOD("get_volumetric_fog_light"), &Environment::get_volumetric_fog_light); + ClassDB::bind_method(D_METHOD("set_volumetric_fog_emission", "color"), &Environment::set_volumetric_fog_emission); + ClassDB::bind_method(D_METHOD("get_volumetric_fog_emission"), &Environment::get_volumetric_fog_emission); + ClassDB::bind_method(D_METHOD("set_volumetric_fog_albedo", "color"), &Environment::set_volumetric_fog_albedo); + ClassDB::bind_method(D_METHOD("get_volumetric_fog_albedo"), &Environment::get_volumetric_fog_albedo); ClassDB::bind_method(D_METHOD("set_volumetric_fog_density", "density"), &Environment::set_volumetric_fog_density); ClassDB::bind_method(D_METHOD("get_volumetric_fog_density"), &Environment::get_volumetric_fog_density); - ClassDB::bind_method(D_METHOD("set_volumetric_fog_light_energy", "begin"), &Environment::set_volumetric_fog_light_energy); - ClassDB::bind_method(D_METHOD("get_volumetric_fog_light_energy"), &Environment::get_volumetric_fog_light_energy); + ClassDB::bind_method(D_METHOD("set_volumetric_fog_emission_energy", "begin"), &Environment::set_volumetric_fog_emission_energy); + ClassDB::bind_method(D_METHOD("get_volumetric_fog_emission_energy"), &Environment::get_volumetric_fog_emission_energy); + ClassDB::bind_method(D_METHOD("set_volumetric_fog_anisotropy", "anisotropy"), &Environment::set_volumetric_fog_anisotropy); + ClassDB::bind_method(D_METHOD("get_volumetric_fog_anisotropy"), &Environment::get_volumetric_fog_anisotropy); ClassDB::bind_method(D_METHOD("set_volumetric_fog_length", "length"), &Environment::set_volumetric_fog_length); ClassDB::bind_method(D_METHOD("get_volumetric_fog_length"), &Environment::get_volumetric_fog_length); ClassDB::bind_method(D_METHOD("set_volumetric_fog_detail_spread", "detail_spread"), &Environment::set_volumetric_fog_detail_spread); ClassDB::bind_method(D_METHOD("get_volumetric_fog_detail_spread"), &Environment::get_volumetric_fog_detail_spread); ClassDB::bind_method(D_METHOD("set_volumetric_fog_gi_inject", "gi_inject"), &Environment::set_volumetric_fog_gi_inject); ClassDB::bind_method(D_METHOD("get_volumetric_fog_gi_inject"), &Environment::get_volumetric_fog_gi_inject); + ClassDB::bind_method(D_METHOD("set_volumetric_fog_ambient_inject", "enabled"), &Environment::set_volumetric_fog_ambient_inject); + ClassDB::bind_method(D_METHOD("get_volumetric_fog_ambient_inject"), &Environment::get_volumetric_fog_ambient_inject); ClassDB::bind_method(D_METHOD("set_volumetric_fog_temporal_reprojection_enabled", "enabled"), &Environment::set_volumetric_fog_temporal_reprojection_enabled); ClassDB::bind_method(D_METHOD("is_volumetric_fog_temporal_reprojection_enabled"), &Environment::is_volumetric_fog_temporal_reprojection_enabled); ClassDB::bind_method(D_METHOD("set_volumetric_fog_temporal_reprojection_amount", "temporal_reprojection_amount"), &Environment::set_volumetric_fog_temporal_reprojection_amount); @@ -1315,11 +1342,14 @@ void Environment::_bind_methods() { ADD_GROUP("Volumetric Fog", "volumetric_fog_"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "volumetric_fog_enabled"), "set_volumetric_fog_enabled", "is_volumetric_fog_enabled"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volumetric_fog_density", PROPERTY_HINT_RANGE, "0,1,0.0001,or_greater"), "set_volumetric_fog_density", "get_volumetric_fog_density"); - ADD_PROPERTY(PropertyInfo(Variant::COLOR, "volumetric_fog_light", PROPERTY_HINT_COLOR_NO_ALPHA), "set_volumetric_fog_light", "get_volumetric_fog_light"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volumetric_fog_light_energy", PROPERTY_HINT_RANGE, "0,1024,0.01,or_greater"), "set_volumetric_fog_light_energy", "get_volumetric_fog_light_energy"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volumetric_fog_gi_inject", PROPERTY_HINT_RANGE, "0.00,16,0.01,exp"), "set_volumetric_fog_gi_inject", "get_volumetric_fog_gi_inject"); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "volumetric_fog_albedo", PROPERTY_HINT_COLOR_NO_ALPHA), "set_volumetric_fog_albedo", "get_volumetric_fog_albedo"); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "volumetric_fog_emission", PROPERTY_HINT_COLOR_NO_ALPHA), "set_volumetric_fog_emission", "get_volumetric_fog_emission"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volumetric_fog_emission_energy", PROPERTY_HINT_RANGE, "0,1024,0.01,or_greater"), "set_volumetric_fog_emission_energy", "get_volumetric_fog_emission_energy"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volumetric_fog_gi_inject", PROPERTY_HINT_RANGE, "0.0,16,0.01,exp"), "set_volumetric_fog_gi_inject", "get_volumetric_fog_gi_inject"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volumetric_fog_anisotropy", PROPERTY_HINT_RANGE, "-0.9,0.9,0.01"), "set_volumetric_fog_anisotropy", "get_volumetric_fog_anisotropy"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volumetric_fog_length", PROPERTY_HINT_RANGE, "0,1024,0.01,or_greater"), "set_volumetric_fog_length", "get_volumetric_fog_length"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volumetric_fog_detail_spread", PROPERTY_HINT_EXP_EASING, "0.01,16,0.01"), "set_volumetric_fog_detail_spread", "get_volumetric_fog_detail_spread"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volumetric_fog_detail_spread", PROPERTY_HINT_EXP_EASING), "set_volumetric_fog_detail_spread", "get_volumetric_fog_detail_spread"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volumetric_fog_ambient_inject", PROPERTY_HINT_RANGE, "0.0,16,0.01,exp"), "set_volumetric_fog_ambient_inject", "get_volumetric_fog_ambient_inject"); ADD_SUBGROUP("Temporal Reprojection", "volumetric_fog_temporal_reprojection_"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "volumetric_fog_temporal_reprojection_enabled"), "set_volumetric_fog_temporal_reprojection_enabled", "is_volumetric_fog_temporal_reprojection_enabled"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "volumetric_fog_temporal_reprojection_amount", PROPERTY_HINT_RANGE, "0.0,0.999,0.001"), "set_volumetric_fog_temporal_reprojection_amount", "get_volumetric_fog_temporal_reprojection_amount"); @@ -1381,11 +1411,6 @@ void Environment::_bind_methods() { BIND_ENUM_CONSTANT(SDFGI_Y_SCALE_DISABLED); BIND_ENUM_CONSTANT(SDFGI_Y_SCALE_75_PERCENT); BIND_ENUM_CONSTANT(SDFGI_Y_SCALE_50_PERCENT); - - BIND_ENUM_CONSTANT(VOLUMETRIC_FOG_SHADOW_FILTER_DISABLED); - BIND_ENUM_CONSTANT(VOLUMETRIC_FOG_SHADOW_FILTER_LOW); - BIND_ENUM_CONSTANT(VOLUMETRIC_FOG_SHADOW_FILTER_MEDIUM); - BIND_ENUM_CONSTANT(VOLUMETRIC_FOG_SHADOW_FILTER_HIGH); } Environment::Environment() { diff --git a/scene/resources/environment.h b/scene/resources/environment.h index 46842754f4..024bef34de 100644 --- a/scene/resources/environment.h +++ b/scene/resources/environment.h @@ -90,13 +90,6 @@ public: GLOW_BLEND_MODE_MIX, }; - enum VolumetricFogShadowFilter { - VOLUMETRIC_FOG_SHADOW_FILTER_DISABLED, - VOLUMETRIC_FOG_SHADOW_FILTER_LOW, - VOLUMETRIC_FOG_SHADOW_FILTER_MEDIUM, - VOLUMETRIC_FOG_SHADOW_FILTER_HIGH, - }; - private: RID environment; @@ -190,12 +183,15 @@ private: // Volumetric Fog bool volumetric_fog_enabled = false; - float volumetric_fog_density = 0.01; - Color volumetric_fog_light = Color(0.0, 0.0, 0.0); - float volumetric_fog_light_energy = 1.0; + float volumetric_fog_density = 0.05; + Color volumetric_fog_albedo = Color(1.0, 1.0, 1.0); + Color volumetric_fog_emission = Color(0.0, 0.0, 0.0); + float volumetric_fog_emission_energy = 1.0; + float volumetric_fog_anisotropy = 0.2; float volumetric_fog_length = 64.0; float volumetric_fog_detail_spread = 2.0; float volumetric_fog_gi_inject = 0.0; + float volumetric_fog_ambient_inject = false; bool volumetric_fog_temporal_reproject = true; float volumetric_fog_temporal_reproject_amount = 0.9; void _update_volumetric_fog(); @@ -375,16 +371,22 @@ public: bool is_volumetric_fog_enabled() const; void set_volumetric_fog_density(float p_density); float get_volumetric_fog_density() const; - void set_volumetric_fog_light(Color p_color); - Color get_volumetric_fog_light() const; - void set_volumetric_fog_light_energy(float p_begin); - float get_volumetric_fog_light_energy() const; + void set_volumetric_fog_albedo(Color p_color); + Color get_volumetric_fog_albedo() const; + void set_volumetric_fog_emission(Color p_color); + Color get_volumetric_fog_emission() const; + void set_volumetric_fog_emission_energy(float p_begin); + float get_volumetric_fog_emission_energy() const; + void set_volumetric_fog_anisotropy(float p_anisotropy); + float get_volumetric_fog_anisotropy() const; void set_volumetric_fog_length(float p_length); float get_volumetric_fog_length() const; void set_volumetric_fog_detail_spread(float p_detail_spread); float get_volumetric_fog_detail_spread() const; void set_volumetric_fog_gi_inject(float p_gi_inject); float get_volumetric_fog_gi_inject() const; + void set_volumetric_fog_ambient_inject(float p_ambient_inject); + float get_volumetric_fog_ambient_inject() const; void set_volumetric_fog_temporal_reprojection_enabled(bool p_enable); bool is_volumetric_fog_temporal_reprojection_enabled() const; void set_volumetric_fog_temporal_reprojection_amount(float p_amount); @@ -413,6 +415,5 @@ VARIANT_ENUM_CAST(Environment::ToneMapper) VARIANT_ENUM_CAST(Environment::SDFGICascades) VARIANT_ENUM_CAST(Environment::SDFGIYScale) VARIANT_ENUM_CAST(Environment::GlowBlendMode) -VARIANT_ENUM_CAST(Environment::VolumetricFogShadowFilter) #endif // ENVIRONMENT_H diff --git a/scene/resources/fog_material.cpp b/scene/resources/fog_material.cpp new file mode 100644 index 0000000000..978aaca33d --- /dev/null +++ b/scene/resources/fog_material.cpp @@ -0,0 +1,181 @@ +/*************************************************************************/ +/* fog_material.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#include "fog_material.h" + +#include "core/version.h" + +Mutex FogMaterial::shader_mutex; +RID FogMaterial::shader; + +void FogMaterial::set_density(float p_density) { + density = p_density; + RS::get_singleton()->material_set_param(_get_material(), "density", density); +} + +float FogMaterial::get_density() const { + return density; +} + +void FogMaterial::set_albedo(Color p_albedo) { + albedo = p_albedo; + RS::get_singleton()->material_set_param(_get_material(), "albedo", albedo); +} + +Color FogMaterial::get_albedo() const { + return albedo; +} + +void FogMaterial::set_emission(Color p_emission) { + emission = p_emission; + RS::get_singleton()->material_set_param(_get_material(), "emission", emission); +} + +Color FogMaterial::get_emission() const { + return emission; +} + +void FogMaterial::set_height_falloff(float p_falloff) { + height_falloff = MAX(p_falloff, 0.0f); + RS::get_singleton()->material_set_param(_get_material(), "height_falloff", height_falloff); +} + +float FogMaterial::get_height_falloff() const { + return height_falloff; +} + +void FogMaterial::set_edge_fade(float p_edge_fade) { + edge_fade = MAX(p_edge_fade, 0.0f); + RS::get_singleton()->material_set_param(_get_material(), "edge_fade", edge_fade); +} + +float FogMaterial::get_edge_fade() const { + return edge_fade; +} + +void FogMaterial::set_density_texture(const Ref<Texture3D> &p_texture) { + density_texture = p_texture; + RID tex_rid = p_texture.is_valid() ? p_texture->get_rid() : RID(); + RS::get_singleton()->material_set_param(_get_material(), "density_texture", tex_rid); +} + +Ref<Texture3D> FogMaterial::get_density_texture() const { + return density_texture; +} + +Shader::Mode FogMaterial::get_shader_mode() const { + return Shader::MODE_FOG; +} + +RID FogMaterial::get_shader_rid() const { + _update_shader(); + return shader; +} + +RID FogMaterial::get_rid() const { + _update_shader(); + if (!shader_set) { + RS::get_singleton()->material_set_shader(_get_material(), shader); + shader_set = true; + } + return _get_material(); +} + +void FogMaterial::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_density", "density"), &FogMaterial::set_density); + ClassDB::bind_method(D_METHOD("get_density"), &FogMaterial::get_density); + ClassDB::bind_method(D_METHOD("set_albedo", "albedo"), &FogMaterial::set_albedo); + ClassDB::bind_method(D_METHOD("get_albedo"), &FogMaterial::get_albedo); + ClassDB::bind_method(D_METHOD("set_emission", "emission"), &FogMaterial::set_emission); + ClassDB::bind_method(D_METHOD("get_emission"), &FogMaterial::get_emission); + ClassDB::bind_method(D_METHOD("set_height_falloff", "height_falloff"), &FogMaterial::set_height_falloff); + ClassDB::bind_method(D_METHOD("get_height_falloff"), &FogMaterial::get_height_falloff); + ClassDB::bind_method(D_METHOD("set_edge_fade", "edge_fade"), &FogMaterial::set_edge_fade); + ClassDB::bind_method(D_METHOD("get_edge_fade"), &FogMaterial::get_edge_fade); + ClassDB::bind_method(D_METHOD("set_density_texture", "density_texture"), &FogMaterial::set_density_texture); + ClassDB::bind_method(D_METHOD("get_density_texture"), &FogMaterial::get_density_texture); + + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "density", PROPERTY_HINT_RANGE, "0.0,16.0,0.0001,or_greater,or_lesser"), "set_density", "get_density"); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "albedo", PROPERTY_HINT_COLOR_NO_ALPHA), "set_albedo", "get_albedo"); + ADD_PROPERTY(PropertyInfo(Variant::COLOR, "emission", PROPERTY_HINT_COLOR_NO_ALPHA), "set_emission", "get_emission"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height_falloff", PROPERTY_HINT_EXP_EASING, "attenuation"), "set_height_falloff", "get_height_falloff"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "edge_fade", PROPERTY_HINT_EXP_EASING), "set_edge_fade", "get_edge_fade"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "density_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture3D"), "set_density_texture", "get_density_texture"); +} + +void FogMaterial::cleanup_shader() { + if (shader.is_valid()) { + RS::get_singleton()->free(shader); + } +} + +void FogMaterial::_update_shader() { + shader_mutex.lock(); + if (shader.is_null()) { + shader = RS::get_singleton()->shader_create(); + + // Add a comment to describe the shader origin (useful when converting to ShaderMaterial). + RS::get_singleton()->shader_set_code(shader, R"( +// NOTE: Shader automatically converted from )" VERSION_NAME " " VERSION_FULL_CONFIG R"('s FogMaterial. + +shader_type fog; + +uniform float density : hint_range(0, 1, 0.0001) = 1.0; +uniform vec4 albedo : hint_color = vec4(1.0); +uniform vec4 emission : hint_color = vec4(0, 0, 0, 1); +uniform float height_falloff = 0.0; +uniform float edge_fade = 0.1; +uniform sampler3D density_texture: hint_white; + + +void fog() { + DENSITY = density * clamp(exp2(-height_falloff * (WORLD_POSITION.y - OBJECT_POSITION.y)), 0.0, 1.0); + DENSITY *= texture(density_texture, UVW).r; + DENSITY *= pow(clamp(-SDF / min(min(EXTENTS.x, EXTENTS.y), EXTENTS.z), 0.0, 1.0), edge_fade); + ALBEDO = albedo.rgb; + EMISSION = emission.rgb; +} +)"); + } + shader_mutex.unlock(); +} + +FogMaterial::FogMaterial() { + set_density(1.0); + set_albedo(Color(1, 1, 1, 1)); + set_emission(Color(0, 0, 0, 1)); + + set_height_falloff(0.0); + set_edge_fade(0.1); +} + +FogMaterial::~FogMaterial() { + RS::get_singleton()->material_set_shader(_get_material(), RID()); +} diff --git a/scene/resources/fog_material.h b/scene/resources/fog_material.h new file mode 100644 index 0000000000..e256bd4719 --- /dev/null +++ b/scene/resources/fog_material.h @@ -0,0 +1,87 @@ +/*************************************************************************/ +/* fog_material.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#ifndef FOG_MATERIAL_H +#define FOG_MATERIAL_H + +#include "scene/resources/material.h" + +class FogMaterial : public Material { + GDCLASS(FogMaterial, Material); + +private: + float density = 1.0; + Color albedo = Color(1, 1, 1, 1); + Color emission = Color(0, 0, 0, 0); + + float height_falloff = 0.0; + + float edge_fade = 0.1; + + Ref<Texture3D> density_texture; + + static Mutex shader_mutex; + static RID shader; + static void _update_shader(); + mutable bool shader_set = false; + +protected: + static void _bind_methods(); + +public: + void set_density(float p_density); + float get_density() const; + + void set_albedo(Color p_color); + Color get_albedo() const; + + void set_emission(Color p_color); + Color get_emission() const; + + void set_height_falloff(float p_falloff); + float get_height_falloff() const; + + void set_edge_fade(float p_edge_fade); + float get_edge_fade() const; + + void set_density_texture(const Ref<Texture3D> &p_texture); + Ref<Texture3D> get_density_texture() const; + + virtual Shader::Mode get_shader_mode() const override; + virtual RID get_shader_rid() const override; + virtual RID get_rid() const override; + + static void cleanup_shader(); + + FogMaterial(); + virtual ~FogMaterial(); +}; + +#endif // FOG_MATERIAL_H diff --git a/scene/resources/font.cpp b/scene/resources/font.cpp index 04e2b0dc70..819ae95715 100644 --- a/scene/resources/font.cpp +++ b/scene/resources/font.cpp @@ -1072,10 +1072,6 @@ void Font::_bind_methods() { ClassDB::bind_method(D_METHOD("clear_data"), &Font::clear_data); ClassDB::bind_method(D_METHOD("remove_data", "idx"), &Font::remove_data); - ClassDB::bind_method(D_METHOD("set_base_size", "size"), &Font::set_base_size); - ClassDB::bind_method(D_METHOD("get_base_size"), &Font::get_base_size); - ADD_PROPERTY(PropertyInfo(Variant::INT, "base_size"), "set_base_size", "get_base_size"); - ClassDB::bind_method(D_METHOD("set_variation_coordinates", "variation_coordinates"), &Font::set_variation_coordinates); ClassDB::bind_method(D_METHOD("get_variation_coordinates"), &Font::get_variation_coordinates); ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "variation_coordinates"), "set_variation_coordinates", "get_variation_coordinates"); @@ -1087,20 +1083,20 @@ void Font::_bind_methods() { ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_top"), "set_spacing", "get_spacing", TextServer::SPACING_TOP); ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_bottom"), "set_spacing", "get_spacing", TextServer::SPACING_BOTTOM); - ClassDB::bind_method(D_METHOD("get_height", "size"), &Font::get_height, DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("get_ascent", "size"), &Font::get_ascent, DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("get_descent", "size"), &Font::get_descent, DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("get_underline_position", "size"), &Font::get_underline_position, DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("get_underline_thickness", "size"), &Font::get_underline_thickness, DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("get_height", "size"), &Font::get_height, DEFVAL(DEFAULT_FONT_SIZE)); + ClassDB::bind_method(D_METHOD("get_ascent", "size"), &Font::get_ascent, DEFVAL(DEFAULT_FONT_SIZE)); + ClassDB::bind_method(D_METHOD("get_descent", "size"), &Font::get_descent, DEFVAL(DEFAULT_FONT_SIZE)); + ClassDB::bind_method(D_METHOD("get_underline_position", "size"), &Font::get_underline_position, DEFVAL(DEFAULT_FONT_SIZE)); + ClassDB::bind_method(D_METHOD("get_underline_thickness", "size"), &Font::get_underline_thickness, DEFVAL(DEFAULT_FONT_SIZE)); - ClassDB::bind_method(D_METHOD("get_string_size", "text", "size", "align", "width", "flags"), &Font::get_string_size, DEFVAL(-1), DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); - ClassDB::bind_method(D_METHOD("get_multiline_string_size", "text", "width", "size", "flags"), &Font::get_multiline_string_size, DEFVAL(-1), DEFVAL(-1), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND)); + ClassDB::bind_method(D_METHOD("get_string_size", "text", "size", "align", "width", "flags"), &Font::get_string_size, DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); + ClassDB::bind_method(D_METHOD("get_multiline_string_size", "text", "width", "size", "flags"), &Font::get_multiline_string_size, DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND)); - ClassDB::bind_method(D_METHOD("draw_string", "canvas_item", "pos", "text", "align", "width", "size", "modulate", "outline_size", "outline_modulate", "flags"), &Font::draw_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); - ClassDB::bind_method(D_METHOD("draw_multiline_string", "canvas_item", "pos", "text", "align", "width", "max_lines", "size", "modulate", "outline_size", "outline_modulate", "flags"), &Font::draw_multiline_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(-1), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); + ClassDB::bind_method(D_METHOD("draw_string", "canvas_item", "pos", "text", "align", "width", "size", "modulate", "outline_size", "outline_modulate", "flags"), &Font::draw_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); + ClassDB::bind_method(D_METHOD("draw_multiline_string", "canvas_item", "pos", "text", "align", "width", "max_lines", "size", "modulate", "outline_size", "outline_modulate", "flags"), &Font::draw_multiline_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); - ClassDB::bind_method(D_METHOD("get_char_size", "char", "next", "size"), &Font::get_char_size, DEFVAL(0), DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("draw_char", "canvas_item", "pos", "char", "next", "size", "modulate", "outline_size", "outline_modulate"), &Font::draw_char, DEFVAL(0), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0))); + ClassDB::bind_method(D_METHOD("get_char_size", "char", "next", "size"), &Font::get_char_size, DEFVAL(0), DEFVAL(DEFAULT_FONT_SIZE)); + ClassDB::bind_method(D_METHOD("draw_char", "canvas_item", "pos", "char", "next", "size", "modulate", "outline_size", "outline_modulate"), &Font::draw_char, DEFVAL(0), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0))); ClassDB::bind_method(D_METHOD("has_char", "char"), &Font::has_char); ClassDB::bind_method(D_METHOD("get_supported_chars"), &Font::get_supported_chars); @@ -1195,7 +1191,6 @@ void Font::reset_state() { data.clear(); rids.clear(); - base_size = 16; variation_coordinates.clear(); spacing_bottom = 0; spacing_top = 0; @@ -1308,14 +1303,6 @@ void Font::remove_data(int p_idx) { notify_property_list_changed(); } -void Font::set_base_size(int p_size) { - base_size = p_size; -} - -int Font::get_base_size() const { - return base_size; -} - void Font::set_variation_coordinates(const Dictionary &p_variation_coordinates) { _data_changed(); variation_coordinates = p_variation_coordinates; @@ -1355,51 +1342,46 @@ int Font::get_spacing(TextServer::SpacingType p_spacing) const { } real_t Font::get_height(int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; real_t ret = 0.f; for (int i = 0; i < data.size(); i++) { _ensure_rid(i); - ret = MAX(ret, TS->font_get_ascent(rids[i], size) + TS->font_get_descent(rids[i], size)); + ret = MAX(ret, TS->font_get_ascent(rids[i], p_size) + TS->font_get_descent(rids[i], p_size)); } return ret + spacing_bottom + spacing_top; } real_t Font::get_ascent(int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; real_t ret = 0.f; for (int i = 0; i < data.size(); i++) { _ensure_rid(i); - ret = MAX(ret, TS->font_get_ascent(rids[i], size)); + ret = MAX(ret, TS->font_get_ascent(rids[i], p_size)); } return ret + spacing_top; } real_t Font::get_descent(int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; real_t ret = 0.f; for (int i = 0; i < data.size(); i++) { _ensure_rid(i); - ret = MAX(ret, TS->font_get_descent(rids[i], size)); + ret = MAX(ret, TS->font_get_descent(rids[i], p_size)); } return ret + spacing_bottom; } real_t Font::get_underline_position(int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; real_t ret = 0.f; for (int i = 0; i < data.size(); i++) { _ensure_rid(i); - ret = MAX(ret, TS->font_get_underline_position(rids[i], size)); + ret = MAX(ret, TS->font_get_underline_position(rids[i], p_size)); } return ret + spacing_top; } real_t Font::get_underline_thickness(int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; real_t ret = 0.f; for (int i = 0; i < data.size(); i++) { _ensure_rid(i); - ret = MAX(ret, TS->font_get_underline_thickness(rids[i], size)); + ret = MAX(ret, TS->font_get_underline_thickness(rids[i], p_size)); } return ret; } @@ -1407,8 +1389,6 @@ real_t Font::get_underline_thickness(int p_size) const { Size2 Font::get_string_size(const String &p_text, int p_size, HAlign p_align, real_t p_width, uint16_t p_flags) const { ERR_FAIL_COND_V(data.is_empty(), Size2()); - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); } @@ -1418,14 +1398,14 @@ Size2 Font::get_string_size(const String &p_text, int p_size, HAlign p_align, re hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash); hash = hash_djb2_one_64(p_flags, hash); } - hash = hash_djb2_one_64(size, hash); + hash = hash_djb2_one_64(p_size, hash); Ref<TextLine> buffer; if (cache.has(hash)) { buffer = cache.get(hash); } else { buffer.instantiate(); - buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); + buffer->add_string(p_text, Ref<Font>(this), p_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); cache.insert(hash, buffer); } return buffer->get_size(); @@ -1434,8 +1414,6 @@ Size2 Font::get_string_size(const String &p_text, int p_size, HAlign p_align, re Size2 Font::get_multiline_string_size(const String &p_text, real_t p_width, int p_size, uint16_t p_flags) const { ERR_FAIL_COND_V(data.is_empty(), Size2()); - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); } @@ -1443,14 +1421,14 @@ Size2 Font::get_multiline_string_size(const String &p_text, real_t p_width, int uint64_t hash = p_text.hash64(); uint64_t wrp_hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash); wrp_hash = hash_djb2_one_64(p_flags, wrp_hash); - wrp_hash = hash_djb2_one_64(size, wrp_hash); + wrp_hash = hash_djb2_one_64(p_size, wrp_hash); Ref<TextParagraph> lines_buffer; if (cache_wrap.has(wrp_hash)) { lines_buffer = cache_wrap.get(wrp_hash); } else { lines_buffer.instantiate(); - lines_buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); + lines_buffer->add_string(p_text, Ref<Font>(this), p_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); lines_buffer->set_width(p_width); lines_buffer->set_flags(p_flags); cache_wrap.insert(wrp_hash, lines_buffer); @@ -1473,8 +1451,6 @@ Size2 Font::get_multiline_string_size(const String &p_text, real_t p_width, int void Font::draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align, real_t p_width, int p_size, const Color &p_modulate, int p_outline_size, const Color &p_outline_modulate, uint16_t p_flags) const { ERR_FAIL_COND(data.is_empty()); - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); } @@ -1484,14 +1460,14 @@ void Font::draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_t hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash); hash = hash_djb2_one_64(p_flags, hash); } - hash = hash_djb2_one_64(size, hash); + hash = hash_djb2_one_64(p_size, hash); Ref<TextLine> buffer; if (cache.has(hash)) { buffer = cache.get(hash); } else { buffer.instantiate(); - buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); + buffer->add_string(p_text, Ref<Font>(this), p_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); cache.insert(hash, buffer); } @@ -1515,8 +1491,6 @@ void Font::draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_t void Font::draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align, float p_width, int p_max_lines, int p_size, const Color &p_modulate, int p_outline_size, const Color &p_outline_modulate, uint16_t p_flags) const { ERR_FAIL_COND(data.is_empty()); - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); } @@ -1524,14 +1498,14 @@ void Font::draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const S uint64_t hash = p_text.hash64(); uint64_t wrp_hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash); wrp_hash = hash_djb2_one_64(p_flags, wrp_hash); - wrp_hash = hash_djb2_one_64(size, wrp_hash); + wrp_hash = hash_djb2_one_64(p_size, wrp_hash); Ref<TextParagraph> lines_buffer; if (cache_wrap.has(wrp_hash)) { lines_buffer = cache_wrap.get(wrp_hash); } else { lines_buffer.instantiate(); - lines_buffer->add_string(p_text, Ref<Font>(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); + lines_buffer->add_string(p_text, Ref<Font>(this), p_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); lines_buffer->set_width(p_width); lines_buffer->set_flags(p_flags); cache_wrap.insert(wrp_hash, lines_buffer); @@ -1573,16 +1547,14 @@ void Font::draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const S } Size2 Font::get_char_size(char32_t p_char, char32_t p_next, int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); if (data[i]->has_char(p_char)) { - int32_t glyph_a = TS->font_get_glyph_index(rids[i], size, p_char, 0); - Size2 ret = Size2(TS->font_get_glyph_advance(rids[i], size, glyph_a).x, TS->font_get_ascent(rids[i], size) + TS->font_get_descent(rids[i], size)); + int32_t glyph_a = TS->font_get_glyph_index(rids[i], p_size, p_char, 0); + Size2 ret = Size2(TS->font_get_glyph_advance(rids[i], p_size, glyph_a).x, TS->font_get_ascent(rids[i], p_size) + TS->font_get_descent(rids[i], p_size)); if ((p_next != 0) && data[i]->has_char(p_next)) { - int32_t glyph_b = TS->font_get_glyph_index(rids[i], size, p_next, 0); - ret.x -= TS->font_get_kerning(rids[i], size, Vector2i(glyph_a, glyph_b)).x; + int32_t glyph_b = TS->font_get_glyph_index(rids[i], p_size, p_next, 0); + ret.x -= TS->font_get_kerning(rids[i], p_size, Vector2i(glyph_a, glyph_b)).x; } return ret; } @@ -1591,22 +1563,20 @@ Size2 Font::get_char_size(char32_t p_char, char32_t p_next, int p_size) const { } real_t Font::draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next, int p_size, const Color &p_modulate, int p_outline_size, const Color &p_outline_modulate) const { - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); if (data[i]->has_char(p_char)) { - int32_t glyph_a = TS->font_get_glyph_index(rids[i], size, p_char, 0); - real_t ret = TS->font_get_glyph_advance(rids[i], size, glyph_a).x; + int32_t glyph_a = TS->font_get_glyph_index(rids[i], p_size, p_char, 0); + real_t ret = TS->font_get_glyph_advance(rids[i], p_size, glyph_a).x; if ((p_next != 0) && data[i]->has_char(p_next)) { - int32_t glyph_b = TS->font_get_glyph_index(rids[i], size, p_next, 0); - ret -= TS->font_get_kerning(rids[i], size, Vector2i(glyph_a, glyph_b)).x; + int32_t glyph_b = TS->font_get_glyph_index(rids[i], p_size, p_next, 0); + ret -= TS->font_get_kerning(rids[i], p_size, Vector2i(glyph_a, glyph_b)).x; } if (p_outline_size > 0 && p_outline_modulate.a != 0.0f) { - TS->font_draw_glyph_outline(rids[i], p_canvas_item, size, p_outline_size, p_pos, glyph_a, p_outline_modulate); + TS->font_draw_glyph_outline(rids[i], p_canvas_item, p_size, p_outline_size, p_pos, glyph_a, p_outline_modulate); } - TS->font_draw_glyph(rids[i], p_canvas_item, size, p_pos, glyph_a, p_modulate); + TS->font_draw_glyph(rids[i], p_canvas_item, p_size, p_pos, glyph_a, p_modulate); return ret; } } diff --git a/scene/resources/font.h b/scene/resources/font.h index e65fdb0751..e1f1f6d742 100644 --- a/scene/resources/font.h +++ b/scene/resources/font.h @@ -219,7 +219,6 @@ class Font : public Resource { mutable Vector<RID> rids; // Font config. - int base_size = 16; Dictionary variation_coordinates; int spacing_bottom = 0; int spacing_top = 0; @@ -237,6 +236,8 @@ protected: virtual void reset_state() override; public: + static const int DEFAULT_FONT_SIZE = 16; + Dictionary get_feature_list() const; // Font data. @@ -249,9 +250,6 @@ public: virtual void remove_data(int p_idx); // Font configuration. - virtual void set_base_size(int p_size); - virtual int get_base_size() const; - virtual void set_variation_coordinates(const Dictionary &p_variation_coordinates); virtual Dictionary get_variation_coordinates() const; @@ -259,26 +257,26 @@ public: virtual int get_spacing(TextServer::SpacingType p_spacing) const; // Font metrics. - virtual real_t get_height(int p_size = -1) const; - virtual real_t get_ascent(int p_size = -1) const; - virtual real_t get_descent(int p_size = -1) const; - virtual real_t get_underline_position(int p_size = -1) const; - virtual real_t get_underline_thickness(int p_size = -1) const; + virtual real_t get_height(int p_size = DEFAULT_FONT_SIZE) const; + virtual real_t get_ascent(int p_size = DEFAULT_FONT_SIZE) const; + virtual real_t get_descent(int p_size = DEFAULT_FONT_SIZE) const; + virtual real_t get_underline_position(int p_size = DEFAULT_FONT_SIZE) const; + virtual real_t get_underline_thickness(int p_size = DEFAULT_FONT_SIZE) const; // Drawing string. - virtual Size2 get_string_size(const String &p_text, int p_size = -1, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, uint16_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; - virtual Size2 get_multiline_string_size(const String &p_text, real_t p_width = -1, int p_size = -1, uint16_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND) const; + virtual Size2 get_string_size(const String &p_text, int p_size = DEFAULT_FONT_SIZE, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, uint16_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; + virtual Size2 get_multiline_string_size(const String &p_text, real_t p_width = -1, int p_size = DEFAULT_FONT_SIZE, uint16_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND) const; - virtual void draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint16_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; - virtual void draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, int p_max_lines = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint16_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; + virtual void draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, int p_size = DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint16_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; + virtual void draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, int p_max_lines = -1, int p_size = DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint16_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; // Helper functions. virtual bool has_char(char32_t p_char) const; virtual String get_supported_chars() const; // Drawing char. - virtual Size2 get_char_size(char32_t p_char, char32_t p_next = 0, int p_size = -1) const; - virtual real_t draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next = 0, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0)) const; + virtual Size2 get_char_size(char32_t p_char, char32_t p_next = 0, int p_size = DEFAULT_FONT_SIZE) const; + virtual real_t draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next = 0, int p_size = DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0)) const; Vector<RID> get_rids() const; diff --git a/scene/resources/importer_mesh.cpp b/scene/resources/importer_mesh.cpp index af69b799cc..076b8312b6 100644 --- a/scene/resources/importer_mesh.cpp +++ b/scene/resources/importer_mesh.cpp @@ -37,26 +37,34 @@ #include <cstdint> void ImporterMesh::Surface::split_normals(const LocalVector<int> &p_indices, const LocalVector<Vector3> &p_normals) { - ERR_FAIL_COND(arrays.size() != RS::ARRAY_MAX); + _split_normals(arrays, p_indices, p_normals); - const PackedVector3Array &vertices = arrays[RS::ARRAY_VERTEX]; + for (BlendShape &blend_shape : blend_shape_data) { + _split_normals(blend_shape.arrays, p_indices, p_normals); + } +} + +void ImporterMesh::Surface::_split_normals(Array &r_arrays, const LocalVector<int> &p_indices, const LocalVector<Vector3> &p_normals) { + ERR_FAIL_COND(r_arrays.size() != RS::ARRAY_MAX); + + const PackedVector3Array &vertices = r_arrays[RS::ARRAY_VERTEX]; int current_vertex_count = vertices.size(); int new_vertex_count = p_indices.size(); int final_vertex_count = current_vertex_count + new_vertex_count; const int *indices_ptr = p_indices.ptr(); - for (int i = 0; i < arrays.size(); i++) { + for (int i = 0; i < r_arrays.size(); i++) { if (i == RS::ARRAY_INDEX) { continue; } - if (arrays[i].get_type() == Variant::NIL) { + if (r_arrays[i].get_type() == Variant::NIL) { continue; } - switch (arrays[i].get_type()) { + switch (r_arrays[i].get_type()) { case Variant::PACKED_VECTOR3_ARRAY: { - PackedVector3Array data = arrays[i]; + PackedVector3Array data = r_arrays[i]; data.resize(final_vertex_count); Vector3 *data_ptr = data.ptrw(); if (i == RS::ARRAY_NORMAL) { @@ -67,55 +75,55 @@ void ImporterMesh::Surface::split_normals(const LocalVector<int> &p_indices, con data_ptr[current_vertex_count + j] = data_ptr[indices_ptr[j]]; } } - arrays[i] = data; + r_arrays[i] = data; } break; case Variant::PACKED_VECTOR2_ARRAY: { - PackedVector2Array data = arrays[i]; + PackedVector2Array data = r_arrays[i]; data.resize(final_vertex_count); Vector2 *data_ptr = data.ptrw(); for (int j = 0; j < new_vertex_count; j++) { data_ptr[current_vertex_count + j] = data_ptr[indices_ptr[j]]; } - arrays[i] = data; + r_arrays[i] = data; } break; case Variant::PACKED_FLOAT32_ARRAY: { - PackedFloat32Array data = arrays[i]; + PackedFloat32Array data = r_arrays[i]; int elements = data.size() / current_vertex_count; data.resize(final_vertex_count * elements); float *data_ptr = data.ptrw(); for (int j = 0; j < new_vertex_count; j++) { memcpy(&data_ptr[(current_vertex_count + j) * elements], &data_ptr[indices_ptr[j] * elements], sizeof(float) * elements); } - arrays[i] = data; + r_arrays[i] = data; } break; case Variant::PACKED_INT32_ARRAY: { - PackedInt32Array data = arrays[i]; + PackedInt32Array data = r_arrays[i]; int elements = data.size() / current_vertex_count; data.resize(final_vertex_count * elements); int32_t *data_ptr = data.ptrw(); for (int j = 0; j < new_vertex_count; j++) { memcpy(&data_ptr[(current_vertex_count + j) * elements], &data_ptr[indices_ptr[j] * elements], sizeof(int32_t) * elements); } - arrays[i] = data; + r_arrays[i] = data; } break; case Variant::PACKED_BYTE_ARRAY: { - PackedByteArray data = arrays[i]; + PackedByteArray data = r_arrays[i]; int elements = data.size() / current_vertex_count; data.resize(final_vertex_count * elements); uint8_t *data_ptr = data.ptrw(); for (int j = 0; j < new_vertex_count; j++) { memcpy(&data_ptr[(current_vertex_count + j) * elements], &data_ptr[indices_ptr[j] * elements], sizeof(uint8_t) * elements); } - arrays[i] = data; + r_arrays[i] = data; } break; case Variant::PACKED_COLOR_ARRAY: { - PackedColorArray data = arrays[i]; + PackedColorArray data = r_arrays[i]; data.resize(final_vertex_count); Color *data_ptr = data.ptrw(); for (int j = 0; j < new_vertex_count; j++) { data_ptr[current_vertex_count + j] = data_ptr[indices_ptr[j]]; } - arrays[i] = data; + r_arrays[i] = data; } break; default: { ERR_FAIL_MSG("Unhandled array type."); @@ -261,9 +269,6 @@ void ImporterMesh::generate_lods(float p_normal_merge_angle, float p_normal_spli if (surfaces[i].primitive != Mesh::PRIMITIVE_TRIANGLES) { continue; } - if (get_blend_shape_count()) { - continue; - } surfaces.write[i].lods.clear(); Vector<Vector3> vertices = surfaces[i].arrays[RS::ARRAY_VERTEX]; @@ -992,7 +997,7 @@ Ref<NavigationMesh> ImporterMesh::create_navigation_mesh() { extern bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y); -struct EditorSceneImporterMeshLightmapSurface { +struct EditorSceneFormatImporterMeshLightmapSurface { Ref<Material> material; LocalVector<SurfaceTool::Vertex> vertices; Mesh::PrimitiveType primitive = Mesh::PrimitiveType::PRIMITIVE_MAX; @@ -1010,7 +1015,7 @@ Error ImporterMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform, LocalVector<float> uv; LocalVector<Pair<int, int>> uv_indices; - Vector<EditorSceneImporterMeshLightmapSurface> lightmap_surfaces; + Vector<EditorSceneFormatImporterMeshLightmapSurface> lightmap_surfaces; // Keep only the scale Basis basis = p_base_transform.get_basis(); @@ -1022,7 +1027,7 @@ Error ImporterMesh::lightmap_unwrap_cached(const Transform3D &p_base_transform, Basis normal_basis = transform.basis.inverse().transposed(); for (int i = 0; i < get_surface_count(); i++) { - EditorSceneImporterMeshLightmapSurface s; + EditorSceneFormatImporterMeshLightmapSurface s; s.primitive = get_surface_primitive_type(i); ERR_FAIL_COND_V_MSG(s.primitive != Mesh::PRIMITIVE_TRIANGLES, ERR_UNAVAILABLE, "Only triangles are supported for lightmap unwrap."); diff --git a/scene/resources/importer_mesh.h b/scene/resources/importer_mesh.h index 89909f17f0..8576312a8a 100644 --- a/scene/resources/importer_mesh.h +++ b/scene/resources/importer_mesh.h @@ -70,6 +70,7 @@ class ImporterMesh : public Resource { }; void split_normals(const LocalVector<int> &p_indices, const LocalVector<Vector3> &p_normals); + static void _split_normals(Array &r_arrays, const LocalVector<int> &p_indices, const LocalVector<Vector3> &p_normals); }; Vector<Surface> surfaces; Vector<String> blend_shapes; diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index abb3381c4e..e01be7cc84 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -1086,7 +1086,7 @@ void BaseMaterial3D::_update_shader() { code += " ALPHA = 1.0;\n"; } else if (transparency != TRANSPARENCY_DISABLED || flags[FLAG_USE_SHADOW_TO_OPACITY] || (distance_fade == DISTANCE_FADE_PIXEL_ALPHA) || proximity_fade_enabled) { - code += " ALPHA = albedo.a * albedo_tex.a;\n"; + code += " ALPHA *= albedo.a * albedo_tex.a;\n"; } if (transparency == TRANSPARENCY_ALPHA_HASH) { code += " ALPHA_HASH_SCALE = alpha_hash_scale;\n"; @@ -1100,7 +1100,7 @@ void BaseMaterial3D::_update_shader() { if (proximity_fade_enabled) { code += " float depth_tex = textureLod(DEPTH_TEXTURE,SCREEN_UV,0.0).r;\n"; - code += " vec4 world_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV*2.0-1.0,depth_tex*2.0-1.0,1.0);\n"; + code += " vec4 world_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV*2.0-1.0,depth_tex,1.0);\n"; code += " world_pos.xyz/=world_pos.w;\n"; code += " ALPHA*=clamp(1.0-smoothstep(world_pos.z+proximity_fade_distance,world_pos.z,VERTEX.z),0.0,1.0);\n"; } diff --git a/scene/resources/mesh.cpp b/scene/resources/mesh.cpp index 18e6a51118..7ffe0b03e1 100644 --- a/scene/resources/mesh.cpp +++ b/scene/resources/mesh.cpp @@ -1577,7 +1577,7 @@ void ArrayMesh::regen_normal_maps() { } //dirty hack -bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y) = NULL; +bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y) = nullptr; struct ArrayMeshLightmapSurface { Ref<Material> material; diff --git a/scene/resources/mesh.h b/scene/resources/mesh.h index 8d5571d67c..a95b4d4a5e 100644 --- a/scene/resources/mesh.h +++ b/scene/resources/mesh.h @@ -278,7 +278,6 @@ public: int surface_get_array_index_len(int p_idx) const override; uint32_t surface_get_format(int p_idx) const override; PrimitiveType surface_get_primitive_type(int p_idx) const override; - bool surface_is_alpha_sorting_enabled(int p_idx) const; virtual void surface_set_material(int p_idx, const Ref<Material> &p_material) override; virtual Ref<Material> surface_get_material(int p_idx) const override; diff --git a/scene/resources/navigation_mesh.h b/scene/resources/navigation_mesh.h index 1cdf7a07ed..009239838f 100644 --- a/scene/resources/navigation_mesh.h +++ b/scene/resources/navigation_mesh.h @@ -85,7 +85,7 @@ protected: float cell_size = 0.3f; float cell_height = 0.2f; float agent_height = 2.0f; - float agent_radius = 0.6f; + float agent_radius = 1.0f; float agent_max_climb = 0.9f; float agent_max_slope = 45.0f; float region_min_size = 8.0f; diff --git a/scene/resources/shader.cpp b/scene/resources/shader.cpp index 242e20f3b0..4ba8d4d494 100644 --- a/scene/resources/shader.cpp +++ b/scene/resources/shader.cpp @@ -49,6 +49,8 @@ void Shader::set_code(const String &p_code) { mode = MODE_PARTICLES; } else if (type == "sky") { mode = MODE_SKY; + } else if (type == "fog") { + mode = MODE_FOG; } else { mode = MODE_SPATIAL; } @@ -149,6 +151,7 @@ void Shader::_bind_methods() { BIND_ENUM_CONSTANT(MODE_CANVAS_ITEM); BIND_ENUM_CONSTANT(MODE_PARTICLES); BIND_ENUM_CONSTANT(MODE_SKY); + BIND_ENUM_CONSTANT(MODE_FOG); } Shader::Shader() { diff --git a/scene/resources/shader.h b/scene/resources/shader.h index 6563181ca2..c0dc07b403 100644 --- a/scene/resources/shader.h +++ b/scene/resources/shader.h @@ -46,6 +46,7 @@ public: MODE_CANVAS_ITEM, MODE_PARTICLES, MODE_SKY, + MODE_FOG, MODE_MAX }; diff --git a/scene/resources/shape_2d.h b/scene/resources/shape_2d.h index 14bdd60e4b..7c5d1344e8 100644 --- a/scene/resources/shape_2d.h +++ b/scene/resources/shape_2d.h @@ -64,7 +64,6 @@ public: static bool is_collision_outline_enabled(); - Shape2D(); ~Shape2D(); }; diff --git a/scene/resources/skeleton_modification_2d_lookat.cpp b/scene/resources/skeleton_modification_2d_lookat.cpp index 2da770f012..740937fc44 100644 --- a/scene/resources/skeleton_modification_2d_lookat.cpp +++ b/scene/resources/skeleton_modification_2d_lookat.cpp @@ -241,7 +241,7 @@ int SkeletonModification2DLookAt::get_bone_index() const { void SkeletonModification2DLookAt::set_bone_index(int p_bone_idx) { ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!"); - if (is_setup) { + if (is_setup && stack) { if (stack->skeleton) { ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!"); bone_idx = p_bone_idx; diff --git a/scene/resources/skeleton_modification_3d_fabrik.cpp b/scene/resources/skeleton_modification_3d_fabrik.cpp index e615615924..dedea3e282 100644 --- a/scene/resources/skeleton_modification_3d_fabrik.cpp +++ b/scene/resources/skeleton_modification_3d_fabrik.cpp @@ -149,6 +149,11 @@ void SkeletonModification3DFABRIK::_execute(real_t p_delta) { return; } + // Make sure the transform cache is the correct size + if (fabrik_transforms.size() != fabrik_data_chain.size()) { + fabrik_transforms.resize(fabrik_data_chain.size()); + } + // Verify that all joints have a valid bone ID, and that all bone lengths are zero or more // Also, while we are here, apply magnet positions. for (uint32_t i = 0; i < fabrik_data_chain.size(); i++) { @@ -162,27 +167,24 @@ void SkeletonModification3DFABRIK::_execute(real_t p_delta) { if (_print_execution_error(fabrik_data_chain[i].length < 0, "FABRIK Joint " + itos(i) + " has an invalid joint length. Cannot execute!")) { return; } - - Transform3D local_pose_override = stack->skeleton->get_bone_local_pose_override(fabrik_data_chain[i].bone_idx); + fabrik_transforms[i] = stack->skeleton->get_bone_global_pose(fabrik_data_chain[i].bone_idx); // Apply magnet positions: if (stack->skeleton->get_bone_parent(fabrik_data_chain[i].bone_idx) >= 0) { int parent_bone_idx = stack->skeleton->get_bone_parent(fabrik_data_chain[i].bone_idx); - Transform3D conversion_transform = (stack->skeleton->get_bone_global_pose(parent_bone_idx) * stack->skeleton->get_bone_rest(parent_bone_idx)); - local_pose_override.origin += conversion_transform.basis.xform_inv(fabrik_data_chain[i].magnet_position); + Transform3D conversion_transform = (stack->skeleton->get_bone_global_pose(parent_bone_idx)); + fabrik_transforms[i].origin += conversion_transform.basis.xform_inv(fabrik_data_chain[i].magnet_position); } else { - local_pose_override.origin += fabrik_data_chain[i].magnet_position; + fabrik_transforms[i].origin += fabrik_data_chain[i].magnet_position; } - - stack->skeleton->set_bone_local_pose_override(fabrik_data_chain[i].bone_idx, local_pose_override, stack->strength, true); } + Transform3D origin_global_pose_trans = stack->skeleton->get_bone_global_pose_no_override(fabrik_data_chain[0].bone_idx); target_global_pose = stack->skeleton->world_transform_to_global_pose(node_target->get_global_transform()); - origin_global_pose = stack->skeleton->local_pose_to_global_pose( - fabrik_data_chain[0].bone_idx, stack->skeleton->get_bone_local_pose_override(fabrik_data_chain[0].bone_idx)); + origin_global_pose = origin_global_pose_trans; final_joint_idx = fabrik_data_chain.size() - 1; - real_t target_distance = stack->skeleton->global_pose_to_local_pose(fabrik_data_chain[final_joint_idx].bone_idx, target_global_pose).origin.length(); + real_t target_distance = fabrik_transforms[final_joint_idx].origin.distance_to(target_global_pose.origin); chain_iterations = 0; while (target_distance > chain_tolerance) { @@ -190,7 +192,7 @@ void SkeletonModification3DFABRIK::_execute(real_t p_delta) { chain_forwards(); // update the target distance - target_distance = stack->skeleton->global_pose_to_local_pose(fabrik_data_chain[final_joint_idx].bone_idx, target_global_pose).origin.length(); + target_distance = fabrik_transforms[final_joint_idx].origin.distance_to(target_global_pose.origin); // update chain iterations chain_iterations += 1; @@ -205,7 +207,7 @@ void SkeletonModification3DFABRIK::_execute(real_t p_delta) { void SkeletonModification3DFABRIK::chain_backwards() { int final_bone_idx = fabrik_data_chain[final_joint_idx].bone_idx; - Transform3D final_joint_trans = stack->skeleton->local_pose_to_global_pose(final_bone_idx, stack->skeleton->get_bone_local_pose_override(final_bone_idx)); + Transform3D final_joint_trans = fabrik_transforms[final_joint_idx]; // Get the direction the final bone is facing in. stack->skeleton->update_bone_rest_forward_vector(final_bone_idx); @@ -220,52 +222,46 @@ void SkeletonModification3DFABRIK::chain_backwards() { // set the position of the final joint to the target position final_joint_trans.origin = target_global_pose.origin - (direction * fabrik_data_chain[final_joint_idx].length); - final_joint_trans = stack->skeleton->global_pose_to_local_pose(final_bone_idx, final_joint_trans); - stack->skeleton->set_bone_local_pose_override(final_bone_idx, final_joint_trans, stack->strength, true); + fabrik_transforms[final_joint_idx] = final_joint_trans; // for all other joints, move them towards the target int i = final_joint_idx; while (i >= 1) { - int next_bone_idx = fabrik_data_chain[i].bone_idx; - Transform3D next_bone_trans = stack->skeleton->local_pose_to_global_pose(next_bone_idx, stack->skeleton->get_bone_local_pose_override(next_bone_idx)); + Transform3D next_bone_trans = fabrik_transforms[i]; i -= 1; - int current_bone_idx = fabrik_data_chain[i].bone_idx; - Transform3D current_trans = stack->skeleton->local_pose_to_global_pose(current_bone_idx, stack->skeleton->get_bone_local_pose_override(current_bone_idx)); + Transform3D current_trans = fabrik_transforms[i]; real_t length = fabrik_data_chain[i].length / (current_trans.origin.distance_to(next_bone_trans.origin)); current_trans.origin = next_bone_trans.origin.lerp(current_trans.origin, length); - // Apply it back to the skeleton - stack->skeleton->set_bone_local_pose_override(current_bone_idx, stack->skeleton->global_pose_to_local_pose(current_bone_idx, current_trans), stack->strength, true); + // Save the result + fabrik_transforms[i] = current_trans; } } void SkeletonModification3DFABRIK::chain_forwards() { // Set root at the initial position. - int origin_bone_idx = fabrik_data_chain[0].bone_idx; - Transform3D root_transform = stack->skeleton->local_pose_to_global_pose(origin_bone_idx, stack->skeleton->get_bone_local_pose_override(origin_bone_idx)); + Transform3D root_transform = fabrik_transforms[0]; + root_transform.origin = origin_global_pose.origin; - stack->skeleton->set_bone_local_pose_override(origin_bone_idx, stack->skeleton->global_pose_to_local_pose(origin_bone_idx, root_transform), stack->strength, true); + fabrik_transforms[0] = origin_global_pose; for (uint32_t i = 0; i < fabrik_data_chain.size() - 1; i++) { - int current_bone_idx = fabrik_data_chain[i].bone_idx; - Transform3D current_trans = stack->skeleton->local_pose_to_global_pose(current_bone_idx, stack->skeleton->get_bone_local_pose_override(current_bone_idx)); - int next_bone_idx = fabrik_data_chain[i + 1].bone_idx; - Transform3D next_bone_trans = stack->skeleton->local_pose_to_global_pose(next_bone_idx, stack->skeleton->get_bone_local_pose_override(next_bone_idx)); + Transform3D current_trans = fabrik_transforms[i]; + Transform3D next_bone_trans = fabrik_transforms[i + 1]; real_t length = fabrik_data_chain[i].length / (next_bone_trans.origin.distance_to(current_trans.origin)); next_bone_trans.origin = current_trans.origin.lerp(next_bone_trans.origin, length); - // Apply it back to the skeleton - stack->skeleton->set_bone_local_pose_override(next_bone_idx, stack->skeleton->global_pose_to_local_pose(next_bone_idx, next_bone_trans), stack->strength, true); + // Save the result + fabrik_transforms[i + 1] = next_bone_trans; } } void SkeletonModification3DFABRIK::chain_apply() { for (uint32_t i = 0; i < fabrik_data_chain.size(); i++) { int current_bone_idx = fabrik_data_chain[i].bone_idx; - Transform3D current_trans = stack->skeleton->get_bone_local_pose_override(current_bone_idx); - current_trans = stack->skeleton->local_pose_to_global_pose(current_bone_idx, current_trans); + Transform3D current_trans = fabrik_transforms[i]; // If this is the last bone in the chain... if (i == fabrik_data_chain.size() - 1) { @@ -280,8 +276,7 @@ void SkeletonModification3DFABRIK::chain_apply() { current_trans.basis = target_global_pose.basis.orthonormalized().scaled(current_trans.basis.get_scale()); } } else { // every other bone in the chain... - int next_bone_idx = fabrik_data_chain[i + 1].bone_idx; - Transform3D next_trans = stack->skeleton->local_pose_to_global_pose(next_bone_idx, stack->skeleton->get_bone_local_pose_override(next_bone_idx)); + Transform3D next_trans = fabrik_transforms[i + 1]; // Get the forward direction that the basis is facing in right now. stack->skeleton->update_bone_rest_forward_vector(current_bone_idx); @@ -290,9 +285,7 @@ void SkeletonModification3DFABRIK::chain_apply() { current_trans.basis.rotate_to_align(forward_vector, current_trans.origin.direction_to(next_trans.origin)); current_trans.basis.rotate_local(forward_vector, fabrik_data_chain[i].roll); } - current_trans = stack->skeleton->global_pose_to_local_pose(current_bone_idx, current_trans); - current_trans.origin = Vector3(0, 0, 0); - stack->skeleton->set_bone_local_pose_override(current_bone_idx, current_trans, stack->strength, true); + stack->skeleton->set_bone_local_pose_override(current_bone_idx, stack->skeleton->global_pose_to_local_pose(current_bone_idx, current_trans), stack->strength, true); } // Update all the bones so the next modification has up-to-date data. @@ -374,6 +367,7 @@ int SkeletonModification3DFABRIK::get_fabrik_data_chain_length() { void SkeletonModification3DFABRIK::set_fabrik_data_chain_length(int p_length) { ERR_FAIL_COND(p_length < 0); fabrik_data_chain.resize(p_length); + fabrik_transforms.resize(p_length); execution_error_found = false; notify_property_list_changed(); } @@ -513,8 +507,11 @@ void SkeletonModification3DFABRIK::fabrik_joint_auto_calculate_length(int p_join Transform3D node_trans = tip_node->get_global_transform(); node_trans = stack->skeleton->world_transform_to_global_pose(node_trans); - node_trans = stack->skeleton->global_pose_to_local_pose(fabrik_data_chain[p_joint_idx].bone_idx, node_trans); - fabrik_data_chain[p_joint_idx].length = node_trans.origin.length(); + //node_trans = stack->skeleton->global_pose_to_local_pose(fabrik_data_chain[p_joint_idx].bone_idx, node_trans); + //fabrik_data_chain[p_joint_idx].length = node_trans.origin.length(); + + fabrik_data_chain[p_joint_idx].length = stack->skeleton->get_bone_global_pose(fabrik_data_chain[p_joint_idx].bone_idx).origin.distance_to(node_trans.origin); + } else { // Use child bone(s) to update joint length, if possible Vector<int> bone_children = stack->skeleton->get_bone_children(fabrik_data_chain[p_joint_idx].bone_idx); if (bone_children.size() <= 0) { @@ -522,10 +519,13 @@ void SkeletonModification3DFABRIK::fabrik_joint_auto_calculate_length(int p_join return; } + Transform3D bone_trans = stack->skeleton->get_bone_global_pose(fabrik_data_chain[p_joint_idx].bone_idx); + real_t final_length = 0; for (int i = 0; i < bone_children.size(); i++) { Transform3D child_transform = stack->skeleton->get_bone_global_pose(bone_children[i]); - final_length += stack->skeleton->global_pose_to_local_pose(fabrik_data_chain[p_joint_idx].bone_idx, child_transform).origin.length(); + final_length += bone_trans.origin.distance_to(child_transform.origin); + //final_length += stack->skeleton->global_pose_to_local_pose(fabrik_data_chain[p_joint_idx].bone_idx, child_transform).origin.length(); } fabrik_data_chain[p_joint_idx].length = final_length / bone_children.size(); } diff --git a/scene/resources/skeleton_modification_3d_fabrik.h b/scene/resources/skeleton_modification_3d_fabrik.h index 9b5da883d4..6c58b8a07a 100644 --- a/scene/resources/skeleton_modification_3d_fabrik.h +++ b/scene/resources/skeleton_modification_3d_fabrik.h @@ -55,6 +55,8 @@ private: }; LocalVector<FABRIK_Joint_Data> fabrik_data_chain; + LocalVector<Transform3D> fabrik_transforms; + NodePath target_node; ObjectID target_node_cache; diff --git a/scene/resources/skeleton_modification_3d_jiggle.cpp b/scene/resources/skeleton_modification_3d_jiggle.cpp index 1fb7dad2ad..a6bcb0176a 100644 --- a/scene/resources/skeleton_modification_3d_jiggle.cpp +++ b/scene/resources/skeleton_modification_3d_jiggle.cpp @@ -172,7 +172,12 @@ void SkeletonModification3DJiggle::_execute_jiggle_joint(int p_joint_idx, Node3D return; } - Transform3D new_bone_trans = stack->skeleton->local_pose_to_global_pose(jiggle_data_chain[p_joint_idx].bone_idx, stack->skeleton->get_bone_local_pose_override(jiggle_data_chain[p_joint_idx].bone_idx)); + Transform3D bone_local_pos = stack->skeleton->get_bone_local_pose_override(jiggle_data_chain[p_joint_idx].bone_idx); + if (bone_local_pos == Transform3D()) { + bone_local_pos = stack->skeleton->get_bone_pose(jiggle_data_chain[p_joint_idx].bone_idx); + } + + Transform3D new_bone_trans = stack->skeleton->local_pose_to_global_pose(jiggle_data_chain[p_joint_idx].bone_idx, bone_local_pos); Vector3 target_position = stack->skeleton->world_transform_to_global_pose(p_target->get_global_transform()).origin; jiggle_data_chain[p_joint_idx].force = (target_position - jiggle_data_chain[p_joint_idx].dynamic_position) * jiggle_data_chain[p_joint_idx].stiffness * p_delta; diff --git a/scene/resources/skeleton_modification_3d_lookat.cpp b/scene/resources/skeleton_modification_3d_lookat.cpp index afdb077e71..f3b0f41d60 100644 --- a/scene/resources/skeleton_modification_3d_lookat.cpp +++ b/scene/resources/skeleton_modification_3d_lookat.cpp @@ -96,8 +96,10 @@ void SkeletonModification3DLookAt::_execute(real_t p_delta) { if (_print_execution_error(bone_idx <= -1, "Bone index is invalid. Cannot execute modification!")) { return; } - Transform3D new_bone_trans = stack->skeleton->get_bone_local_pose_override(bone_idx); + if (new_bone_trans == Transform3D()) { + new_bone_trans = stack->skeleton->get_bone_pose(bone_idx); + } Vector3 target_pos = stack->skeleton->global_pose_to_local_pose(bone_idx, stack->skeleton->world_transform_to_global_pose(target->get_global_transform())).origin; // Lock the rotation to a plane relative to the bone by changing the target position diff --git a/scene/resources/skeleton_modification_3d_twoboneik.cpp b/scene/resources/skeleton_modification_3d_twoboneik.cpp index ae7a3bab7e..93ec155a88 100644 --- a/scene/resources/skeleton_modification_3d_twoboneik.cpp +++ b/scene/resources/skeleton_modification_3d_twoboneik.cpp @@ -178,7 +178,16 @@ void SkeletonModification3DTwoBoneIK::_execute(real_t p_delta) { } Transform3D pole_trans = stack->skeleton->world_transform_to_global_pose(pole->get_global_transform()); - bone_one_trans = stack->skeleton->local_pose_to_global_pose(joint_one_bone_idx, stack->skeleton->get_bone_local_pose_override(joint_one_bone_idx)); + Transform3D bone_one_local_pos = stack->skeleton->get_bone_local_pose_override(joint_one_bone_idx); + if (bone_one_local_pos == Transform3D()) { + bone_one_local_pos = stack->skeleton->get_bone_pose(joint_one_bone_idx); + } + Transform3D bone_two_local_pos = stack->skeleton->get_bone_local_pose_override(joint_two_bone_idx); + if (bone_two_local_pos == Transform3D()) { + bone_two_local_pos = stack->skeleton->get_bone_pose(joint_two_bone_idx); + } + + bone_one_trans = stack->skeleton->local_pose_to_global_pose(joint_one_bone_idx, bone_one_local_pos); bone_one_trans = bone_one_trans.looking_at(pole_trans.origin, Vector3(0, 1, 0)); bone_one_trans.basis = stack->skeleton->global_pose_z_forward_to_bone_forward(joint_one_bone_idx, bone_one_trans.basis); stack->skeleton->update_bone_rest_forward_vector(joint_one_bone_idx); @@ -186,7 +195,7 @@ void SkeletonModification3DTwoBoneIK::_execute(real_t p_delta) { stack->skeleton->set_bone_local_pose_override(joint_one_bone_idx, stack->skeleton->global_pose_to_local_pose(joint_one_bone_idx, bone_one_trans), stack->strength, true); stack->skeleton->force_update_bone_children_transforms(joint_one_bone_idx); - bone_two_trans = stack->skeleton->local_pose_to_global_pose(joint_two_bone_idx, stack->skeleton->get_bone_local_pose_override(joint_two_bone_idx)); + bone_two_trans = stack->skeleton->local_pose_to_global_pose(joint_two_bone_idx, bone_two_local_pos); bone_two_trans = bone_two_trans.looking_at(target_trans.origin, Vector3(0, 1, 0)); bone_two_trans.basis = stack->skeleton->global_pose_z_forward_to_bone_forward(joint_two_bone_idx, bone_two_trans.basis); stack->skeleton->update_bone_rest_forward_vector(joint_two_bone_idx); @@ -194,8 +203,17 @@ void SkeletonModification3DTwoBoneIK::_execute(real_t p_delta) { stack->skeleton->set_bone_local_pose_override(joint_two_bone_idx, stack->skeleton->global_pose_to_local_pose(joint_two_bone_idx, bone_two_trans), stack->strength, true); stack->skeleton->force_update_bone_children_transforms(joint_two_bone_idx); } else { - bone_one_trans = stack->skeleton->local_pose_to_global_pose(joint_one_bone_idx, stack->skeleton->get_bone_local_pose_override(joint_one_bone_idx)); - bone_two_trans = stack->skeleton->local_pose_to_global_pose(joint_two_bone_idx, stack->skeleton->get_bone_local_pose_override(joint_two_bone_idx)); + Transform3D bone_one_local_pos = stack->skeleton->get_bone_local_pose_override(joint_one_bone_idx); + if (bone_one_local_pos == Transform3D()) { + bone_one_local_pos = stack->skeleton->get_bone_pose(joint_one_bone_idx); + } + Transform3D bone_two_local_pos = stack->skeleton->get_bone_local_pose_override(joint_two_bone_idx); + if (bone_two_local_pos == Transform3D()) { + bone_two_local_pos = stack->skeleton->get_bone_pose(joint_two_bone_idx); + } + + bone_one_trans = stack->skeleton->local_pose_to_global_pose(joint_one_bone_idx, bone_one_local_pos); + bone_two_trans = stack->skeleton->local_pose_to_global_pose(joint_two_bone_idx, bone_two_local_pos); } Transform3D bone_two_tip_trans; @@ -455,7 +473,7 @@ void SkeletonModification3DTwoBoneIK::calculate_joint_lengths() { joint_two_length = 0; for (int i = 0; i < bone_two_children.size(); i++) { joint_two_length += bone_two_rest_trans.origin.distance_to( - stack->skeleton->local_pose_to_global_pose(bone_two_children[i], stack->skeleton->get_bone_rest(bone_two_children[i])).origin); + stack->skeleton->get_bone_global_pose(bone_two_children[i]).origin); } joint_two_length = joint_two_length / bone_two_children.size(); } else { diff --git a/scene/resources/skeleton_modification_stack_3d.cpp b/scene/resources/skeleton_modification_stack_3d.cpp index a724b732b9..c03210cf48 100644 --- a/scene/resources/skeleton_modification_stack_3d.cpp +++ b/scene/resources/skeleton_modification_stack_3d.cpp @@ -125,6 +125,7 @@ Ref<SkeletonModification3D> SkeletonModificationStack3D::get_modification(int p_ } void SkeletonModificationStack3D::add_modification(Ref<SkeletonModification3D> p_mod) { + ERR_FAIL_NULL(p_mod); p_mod->_setup_modification(this); modifications.push_back(p_mod); } diff --git a/scene/resources/surface_tool.cpp b/scene/resources/surface_tool.cpp index a8cd872408..455af8a40c 100644 --- a/scene/resources/surface_tool.cpp +++ b/scene/resources/surface_tool.cpp @@ -1174,9 +1174,11 @@ Vector<int> SurfaceTool::generate_lod(float p_threshold, int p_target_index_coun Vector<int> lod; ERR_FAIL_COND_V(simplify_func == nullptr, lod); + ERR_FAIL_COND_V(p_target_index_count < 0, lod); ERR_FAIL_COND_V(vertex_array.size() == 0, lod); ERR_FAIL_COND_V(index_array.size() == 0, lod); ERR_FAIL_COND_V(index_array.size() % 3 != 0, lod); + ERR_FAIL_COND_V(index_array.size() < (unsigned int)p_target_index_count, lod); lod.resize(index_array.size()); LocalVector<float> vertices; //uses floats diff --git a/scene/resources/text_paragraph.cpp b/scene/resources/text_paragraph.cpp index b2e18e2451..fae1de94d3 100644 --- a/scene/resources/text_paragraph.cpp +++ b/scene/resources/text_paragraph.cpp @@ -84,7 +84,7 @@ void TextParagraph::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width"), "set_width", "get_width"); - ClassDB::bind_method(D_METHOD("get_non_wraped_size"), &TextParagraph::get_non_wraped_size); + ClassDB::bind_method(D_METHOD("get_non_wrapped_size"), &TextParagraph::get_non_wrapped_size); ClassDB::bind_method(D_METHOD("get_size"), &TextParagraph::get_size); ClassDB::bind_method(D_METHOD("get_rid"), &TextParagraph::get_rid); @@ -417,7 +417,7 @@ float TextParagraph::get_width() const { return width; } -Size2 TextParagraph::get_non_wraped_size() const { +Size2 TextParagraph::get_non_wrapped_size() const { const_cast<TextParagraph *>(this)->_shape_lines(); if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) { return Size2(TS->shaped_text_get_size(rid).x, TS->shaped_text_get_size(rid).y + spacing_top + spacing_bottom); diff --git a/scene/resources/text_paragraph.h b/scene/resources/text_paragraph.h index 69c50559df..701c9a17cd 100644 --- a/scene/resources/text_paragraph.h +++ b/scene/resources/text_paragraph.h @@ -120,7 +120,7 @@ public: void set_max_lines_visible(int p_lines); int get_max_lines_visible() const; - Size2 get_non_wraped_size() const; + Size2 get_non_wrapped_size() const; Size2 get_size() const; diff --git a/scene/resources/theme.cpp b/scene/resources/theme.cpp index 65cdc1e24e..99977a20f2 100644 --- a/scene/resources/theme.cpp +++ b/scene/resources/theme.cpp @@ -169,7 +169,7 @@ void Theme::_get_property_list(List<PropertyInfo> *p_list) const { const StringName *key2 = nullptr; while ((key2 = font_size_map[*key].next(key2))) { - list.push_back(PropertyInfo(Variant::INT, String() + *key + "/font_sizes/" + *key2)); + list.push_back(PropertyInfo(Variant::INT, String() + *key + "/font_sizes/" + *key2, PROPERTY_HINT_RANGE, "0,256,1,or_greater")); } } @@ -1655,7 +1655,7 @@ void Theme::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "default_base_scale", PROPERTY_HINT_RANGE, "0.0,2.0,0.01,or_greater"), "set_default_base_scale", "get_default_base_scale"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "default_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_default_font", "get_default_font"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "default_font_size"), "set_default_font_size", "get_default_font_size"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "default_font_size", PROPERTY_HINT_RANGE, "0,256,1,or_greater"), "set_default_font_size", "get_default_font_size"); BIND_ENUM_CONSTANT(DATA_TYPE_COLOR); BIND_ENUM_CONSTANT(DATA_TYPE_CONSTANT); diff --git a/scene/resources/tile_set.cpp b/scene/resources/tile_set.cpp index e19ca38b82..141e9e1b0e 100644 --- a/scene/resources/tile_set.cpp +++ b/scene/resources/tile_set.cpp @@ -31,6 +31,7 @@ #include "tile_set.h" #include "core/core_string_names.h" +#include "core/io/marshalls.h" #include "core/math/geometry_2d.h" #include "core/templates/local_vector.h" @@ -39,6 +40,189 @@ #include "scene/resources/convex_polygon_shape_2d.h" #include "servers/navigation_server_2d.h" +/////////////////////////////// TileMapPattern ////////////////////////////////////// + +void TileMapPattern::_set_tile_data(const Vector<int> &p_data) { + int c = p_data.size(); + const int *r = p_data.ptr(); + + int offset = 3; + ERR_FAIL_COND_MSG(c % offset != 0, "Corrupted tile data."); + + clear(); + + for (int i = 0; i < c; i += offset) { + const uint8_t *ptr = (const uint8_t *)&r[i]; + uint8_t local[12]; + for (int j = 0; j < 12; j++) { + local[j] = ptr[j]; + } + +#ifdef BIG_ENDIAN_ENABLED + SWAP(local[0], local[3]); + SWAP(local[1], local[2]); + SWAP(local[4], local[7]); + SWAP(local[5], local[6]); + SWAP(local[8], local[11]); + SWAP(local[9], local[10]); +#endif + + int16_t x = decode_uint16(&local[0]); + int16_t y = decode_uint16(&local[2]); + uint16_t source_id = decode_uint16(&local[4]); + uint16_t atlas_coords_x = decode_uint16(&local[6]); + uint16_t atlas_coords_y = decode_uint16(&local[8]); + uint16_t alternative_tile = decode_uint16(&local[10]); + set_cell(Vector2i(x, y), source_id, Vector2i(atlas_coords_x, atlas_coords_y), alternative_tile); + } + emit_signal(SNAME("changed")); +} + +Vector<int> TileMapPattern::_get_tile_data() const { + // Export tile data to raw format + Vector<int> data; + data.resize(pattern.size() * 3); + int *w = data.ptrw(); + + // Save in highest format + + int idx = 0; + for (const KeyValue<Vector2i, TileMapCell> &E : pattern) { + uint8_t *ptr = (uint8_t *)&w[idx]; + encode_uint16((int16_t)(E.key.x), &ptr[0]); + encode_uint16((int16_t)(E.key.y), &ptr[2]); + encode_uint16(E.value.source_id, &ptr[4]); + encode_uint16(E.value.coord_x, &ptr[6]); + encode_uint16(E.value.coord_y, &ptr[8]); + encode_uint16(E.value.alternative_tile, &ptr[10]); + idx += 3; + } + + return data; +} + +void TileMapPattern::set_cell(const Vector2i &p_coords, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile) { + ERR_FAIL_COND_MSG(p_coords.x < 0 || p_coords.y < 0, vformat("Cannot set cell with negative coords in a TileMapPattern. Wrong coords: %s", p_coords)); + + size = size.max(p_coords + Vector2i(1, 1)); + pattern[p_coords] = TileMapCell(p_source_id, p_atlas_coords, p_alternative_tile); + emit_changed(); +} + +bool TileMapPattern::has_cell(const Vector2i &p_coords) const { + return pattern.has(p_coords); +} + +void TileMapPattern::remove_cell(const Vector2i &p_coords, bool p_update_size) { + ERR_FAIL_COND(!pattern.has(p_coords)); + + pattern.erase(p_coords); + if (p_update_size) { + size = Vector2i(); + for (const KeyValue<Vector2i, TileMapCell> &E : pattern) { + size = size.max(E.key + Vector2i(1, 1)); + } + } + emit_changed(); +} + +int TileMapPattern::get_cell_source_id(const Vector2i &p_coords) const { + ERR_FAIL_COND_V(!pattern.has(p_coords), TileSet::INVALID_SOURCE); + + return pattern[p_coords].source_id; +} + +Vector2i TileMapPattern::get_cell_atlas_coords(const Vector2i &p_coords) const { + ERR_FAIL_COND_V(!pattern.has(p_coords), TileSetSource::INVALID_ATLAS_COORDS); + + return pattern[p_coords].get_atlas_coords(); +} + +int TileMapPattern::get_cell_alternative_tile(const Vector2i &p_coords) const { + ERR_FAIL_COND_V(!pattern.has(p_coords), TileSetSource::INVALID_TILE_ALTERNATIVE); + + return pattern[p_coords].alternative_tile; +} + +TypedArray<Vector2i> TileMapPattern::get_used_cells() const { + // Returns the cells used in the tilemap. + TypedArray<Vector2i> a; + a.resize(pattern.size()); + int i = 0; + for (const KeyValue<Vector2i, TileMapCell> &E : pattern) { + Vector2i p(E.key.x, E.key.y); + a[i++] = p; + } + + return a; +} + +Vector2i TileMapPattern::get_size() const { + return size; +} + +void TileMapPattern::set_size(const Vector2i &p_size) { + for (const KeyValue<Vector2i, TileMapCell> &E : pattern) { + Vector2i coords = E.key; + if (p_size.x <= coords.x || p_size.y <= coords.y) { + ERR_FAIL_MSG(vformat("Cannot set pattern size to %s, it contains a tile at %s. Size can only be increased.", p_size, coords)); + }; + } + + size = p_size; + emit_changed(); +} + +bool TileMapPattern::is_empty() const { + return pattern.is_empty(); +}; + +void TileMapPattern::clear() { + size = Vector2i(); + pattern.clear(); + emit_changed(); +}; + +bool TileMapPattern::_set(const StringName &p_name, const Variant &p_value) { + if (p_name == "tile_data") { + if (p_value.is_array()) { + _set_tile_data(p_value); + return true; + } + return false; + } + return false; +} + +bool TileMapPattern::_get(const StringName &p_name, Variant &r_ret) const { + if (p_name == "tile_data") { + r_ret = _get_tile_data(); + return true; + } + return false; +} + +void TileMapPattern::_get_property_list(List<PropertyInfo> *p_list) const { + p_list->push_back(PropertyInfo(Variant::OBJECT, "tile_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); +} + +void TileMapPattern::_bind_methods() { + ClassDB::bind_method(D_METHOD("_set_tile_data", "data"), &TileMapPattern::_set_tile_data); + ClassDB::bind_method(D_METHOD("_get_tile_data"), &TileMapPattern::_get_tile_data); + + ClassDB::bind_method(D_METHOD("set_cell", "coords", "source_id", "atlas_coords", "alternative_tile"), &TileMapPattern::set_cell, DEFVAL(TileSet::INVALID_SOURCE), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(TileSetSource::INVALID_TILE_ALTERNATIVE)); + ClassDB::bind_method(D_METHOD("has_cell", "coords"), &TileMapPattern::has_cell); + ClassDB::bind_method(D_METHOD("remove_cell", "coords"), &TileMapPattern::remove_cell); + ClassDB::bind_method(D_METHOD("get_cell_source_id", "coords"), &TileMapPattern::get_cell_source_id); + ClassDB::bind_method(D_METHOD("get_cell_atlas_coords", "coords"), &TileMapPattern::get_cell_atlas_coords); + ClassDB::bind_method(D_METHOD("get_cell_alternative_tile", "coords"), &TileMapPattern::get_cell_alternative_tile); + + ClassDB::bind_method(D_METHOD("get_used_cells"), &TileMapPattern::get_used_cells); + ClassDB::bind_method(D_METHOD("get_size"), &TileMapPattern::get_size); + ClassDB::bind_method(D_METHOD("set_size", "size"), &TileMapPattern::set_size); + ClassDB::bind_method(D_METHOD("is_empty"), &TileMapPattern::is_empty); +} + /////////////////////////////// TileSet ////////////////////////////////////// const int TileSet::INVALID_SOURCE = -1; @@ -117,6 +301,66 @@ int TileSet::get_next_source_id() const { return next_source_id; } +void TileSet::_update_terrains_cache() { + if (terrains_cache_dirty) { + // Organizes tiles into structures. + per_terrain_pattern_tiles.resize(terrain_sets.size()); + for (int i = 0; i < (int)per_terrain_pattern_tiles.size(); i++) { + per_terrain_pattern_tiles[i].clear(); + } + + for (const KeyValue<int, Ref<TileSetSource>> &kv : sources) { + Ref<TileSetSource> source = kv.value; + Ref<TileSetAtlasSource> atlas_source = source; + if (atlas_source.is_valid()) { + for (int tile_index = 0; tile_index < source->get_tiles_count(); tile_index++) { + Vector2i tile_id = source->get_tile_id(tile_index); + for (int alternative_index = 0; alternative_index < source->get_alternative_tiles_count(tile_id); alternative_index++) { + int alternative_id = source->get_alternative_tile_id(tile_id, alternative_index); + + // Executed for each tile_data. + TileData *tile_data = Object::cast_to<TileData>(atlas_source->get_tile_data(tile_id, alternative_id)); + int terrain_set = tile_data->get_terrain_set(); + if (terrain_set >= 0) { + TileMapCell cell; + cell.source_id = kv.key; + cell.set_atlas_coords(tile_id); + cell.alternative_tile = alternative_id; + + TileSet::TerrainsPattern terrains_pattern = tile_data->get_terrains_pattern(); + + // Terrain bits. + for (int i = 0; i < terrains_pattern.size(); i++) { + int terrain = terrains_pattern[i]; + if (terrain >= 0) { + per_terrain_pattern_tiles[terrain_set][terrains_pattern].insert(cell); + } + } + } + } + } + } + } + + // Add the empty cell in the possible patterns and cells. + for (int i = 0; i < terrain_sets.size(); i++) { + TileSet::TerrainsPattern empty_pattern; + for (int j = 0; j < TileSet::CELL_NEIGHBOR_MAX; j++) { + if (is_valid_peering_bit_terrain(i, TileSet::CellNeighbor(j))) { + empty_pattern.push_back(-1); + } + } + + TileMapCell empty_cell; + empty_cell.source_id = TileSet::INVALID_SOURCE; + empty_cell.set_atlas_coords(TileSetSource::INVALID_ATLAS_COORDS); + empty_cell.alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE; + per_terrain_pattern_tiles[i][empty_pattern].insert(empty_cell); + } + terrains_cache_dirty = false; + } +} + void TileSet::_compute_next_source_id() { while (sources.has(next_source_id)) { next_source_id = (next_source_id + 1) % 1073741824; // 2 ** 30 @@ -137,6 +381,7 @@ int TileSet::add_source(Ref<TileSetSource> p_tile_set_source, int p_atlas_source sources[new_source_id]->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TileSet::_source_changed)); + terrains_cache_dirty = true; emit_changed(); return new_source_id; @@ -152,6 +397,7 @@ void TileSet::remove_source(int p_source_id) { source_ids.erase(p_source_id); source_ids.sort(); + terrains_cache_dirty = true; emit_changed(); } @@ -171,6 +417,9 @@ void TileSet::set_source_id(int p_source_id, int p_new_source_id) { source_ids.append(p_new_source_id); source_ids.sort(); + _compute_next_source_id(); + + terrains_cache_dirty = true; emit_changed(); } @@ -359,6 +608,7 @@ void TileSet::add_terrain_set(int p_index) { } notify_property_list_changed(); + terrains_cache_dirty = true; emit_changed(); } @@ -371,6 +621,7 @@ void TileSet::move_terrain_set(int p_from_index, int p_to_pos) { source.value->move_terrain_set(p_from_index, p_to_pos); } notify_property_list_changed(); + terrains_cache_dirty = true; emit_changed(); } @@ -381,6 +632,7 @@ void TileSet::remove_terrain_set(int p_index) { source.value->remove_terrain_set(p_index); } notify_property_list_changed(); + terrains_cache_dirty = true; emit_changed(); } @@ -392,6 +644,7 @@ void TileSet::set_terrain_set_mode(int p_terrain_set, TerrainMode p_terrain_mode } notify_property_list_changed(); + terrains_cache_dirty = true; emit_changed(); } @@ -426,6 +679,7 @@ void TileSet::add_terrain(int p_terrain_set, int p_index) { } notify_property_list_changed(); + terrains_cache_dirty = true; emit_changed(); } @@ -441,6 +695,7 @@ void TileSet::move_terrain(int p_terrain_set, int p_from_index, int p_to_pos) { source.value->move_terrain(p_terrain_set, p_from_index, p_to_pos); } notify_property_list_changed(); + terrains_cache_dirty = true; emit_changed(); } @@ -454,6 +709,7 @@ void TileSet::remove_terrain(int p_terrain_set, int p_index) { source.value->remove_terrain(p_terrain_set, p_index); } notify_property_list_changed(); + terrains_cache_dirty = true; emit_changed(); } @@ -980,6 +1236,103 @@ void TileSet::clear_tile_proxies() { emit_changed(); } +int TileSet::add_pattern(Ref<TileMapPattern> p_pattern, int p_index) { + ERR_FAIL_COND_V(!p_pattern.is_valid(), -1); + ERR_FAIL_COND_V_MSG(p_pattern->is_empty(), -1, "Cannot add an empty pattern to the TileSet."); + for (unsigned int i = 0; i < patterns.size(); i++) { + ERR_FAIL_COND_V_MSG(patterns[i] == p_pattern, -1, "TileSet has already this pattern."); + } + ERR_FAIL_COND_V(p_index > (int)patterns.size(), -1); + if (p_index < 0) { + p_index = patterns.size(); + } + patterns.insert(p_index, p_pattern); + emit_changed(); + return p_index; +} + +Ref<TileMapPattern> TileSet::get_pattern(int p_index) { + ERR_FAIL_INDEX_V(p_index, (int)patterns.size(), Ref<TileMapPattern>()); + return patterns[p_index]; +} + +void TileSet::remove_pattern(int p_index) { + ERR_FAIL_INDEX(p_index, (int)patterns.size()); + patterns.remove(p_index); + emit_changed(); +} + +int TileSet::get_patterns_count() { + return patterns.size(); +} + +Set<TileSet::TerrainsPattern> TileSet::get_terrains_pattern_set(int p_terrain_set) { + ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), Set<TileSet::TerrainsPattern>()); + _update_terrains_cache(); + + Set<TileSet::TerrainsPattern> output; + for (KeyValue<TileSet::TerrainsPattern, Set<TileMapCell>> kv : per_terrain_pattern_tiles[p_terrain_set]) { + output.insert(kv.key); + } + return output; +} + +Set<TileMapCell> TileSet::get_tiles_for_terrains_pattern(int p_terrain_set, TerrainsPattern p_terrain_tile_pattern) { + ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), Set<TileMapCell>()); + _update_terrains_cache(); + return per_terrain_pattern_tiles[p_terrain_set][p_terrain_tile_pattern]; +} + +TileMapCell TileSet::get_random_tile_from_pattern(int p_terrain_set, TileSet::TerrainsPattern p_terrain_tile_pattern) { + ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), TileMapCell()); + _update_terrains_cache(); + + // Count the sum of probabilities. + double sum = 0.0; + Set<TileMapCell> set = per_terrain_pattern_tiles[p_terrain_set][p_terrain_tile_pattern]; + for (Set<TileMapCell>::Element *E = set.front(); E; E = E->next()) { + if (E->get().source_id >= 0) { + Ref<TileSetSource> source = sources[E->get().source_id]; + Ref<TileSetAtlasSource> atlas_source = source; + if (atlas_source.is_valid()) { + TileData *tile_data = Object::cast_to<TileData>(atlas_source->get_tile_data(E->get().get_atlas_coords(), E->get().alternative_tile)); + sum += tile_data->get_probability(); + } else { + sum += 1.0; + } + } else { + sum += 1.0; + } + } + + // Generate a random number. + double count = 0.0; + double picked = Math::random(0.0, sum); + + // Pick the tile. + for (Set<TileMapCell>::Element *E = set.front(); E; E = E->next()) { + if (E->get().source_id >= 0) { + Ref<TileSetSource> source = sources[E->get().source_id]; + + Ref<TileSetAtlasSource> atlas_source = source; + if (atlas_source.is_valid()) { + TileData *tile_data = Object::cast_to<TileData>(atlas_source->get_tile_data(E->get().get_atlas_coords(), E->get().alternative_tile)); + count += tile_data->get_probability(); + } else { + count += 1.0; + } + } else { + count += 1.0; + } + + if (count >= picked) { + return E->get(); + } + } + + ERR_FAIL_V(TileMapCell()); +} + Vector<Vector2> TileSet::get_tile_shape_polygon() { Vector<Vector2> points; if (tile_shape == TileSet::TILE_SHAPE_SQUARE) { @@ -1294,6 +1647,7 @@ Vector<Vector<Ref<Texture2D>>> TileSet::generate_terrains_icons(Size2i p_size) { } void TileSet::_source_changed() { + terrains_cache_dirty = true; emit_changed(); } @@ -2481,6 +2835,12 @@ bool TileSet::_set(const StringName &p_name, const Variant &p_value) { return true; } return false; + } else if (components.size() == 1 && components[0].begins_with("pattern_") && components[0].trim_prefix("pattern_").is_valid_int()) { + int pattern_index = components[0].trim_prefix("pattern_").to_int(); + for (int i = patterns.size(); i <= pattern_index; i++) { + add_pattern(p_value); + } + return true; } #ifndef DISABLE_DEPRECATED @@ -2604,6 +2964,13 @@ bool TileSet::_get(const StringName &p_name, Variant &r_ret) const { return true; } return false; + } else if (components.size() == 1 && components[0].begins_with("pattern_") && components[0].trim_prefix("pattern_").is_valid_int()) { + int pattern_index = components[0].trim_prefix("pattern_").to_int(); + if (pattern_index < 0 || pattern_index >= (int)patterns.size()) { + return false; + } + r_ret = patterns[pattern_index]; + return true; } return false; @@ -2684,6 +3051,11 @@ void TileSet::_get_property_list(List<PropertyInfo> *p_list) const { p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/source_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR)); p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/coords_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR)); p_list->push_back(PropertyInfo(Variant::ARRAY, "tile_proxies/alternative_level", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR)); + + // Patterns. + for (unsigned int pattern_index = 0; pattern_index < patterns.size(); pattern_index++) { + p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("pattern_%d", pattern_index), PROPERTY_HINT_RESOURCE_TYPE, "TileMapPattern", PROPERTY_USAGE_NOEDITOR)); + } } void TileSet::_validate_property(PropertyInfo &property) const { @@ -2797,6 +3169,12 @@ void TileSet::_bind_methods() { ClassDB::bind_method(D_METHOD("cleanup_invalid_tile_proxies"), &TileSet::cleanup_invalid_tile_proxies); ClassDB::bind_method(D_METHOD("clear_tile_proxies"), &TileSet::clear_tile_proxies); + // Patterns + ClassDB::bind_method(D_METHOD("add_pattern", "pattern", "index"), &TileSet::add_pattern, DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("get_pattern", "index"), &TileSet::get_pattern, DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("remove_pattern", "index"), &TileSet::remove_pattern); + ClassDB::bind_method(D_METHOD("get_patterns_count"), &TileSet::get_patterns_count); + ADD_GROUP("Rendering", ""); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uv_clipping"), "set_uv_clipping", "is_uv_clipping"); ADD_ARRAY("occlusion_layers", "occlusion_layer_"); @@ -3064,6 +3442,7 @@ void TileSetAtlasSource::reset_state() { void TileSetAtlasSource::set_texture(Ref<Texture2D> p_texture) { texture = p_texture; + _clear_tiles_outside_texture(); emit_changed(); } @@ -3079,6 +3458,7 @@ void TileSetAtlasSource::set_margins(Vector2i p_margins) { margins = p_margins; } + _clear_tiles_outside_texture(); emit_changed(); } Vector2i TileSetAtlasSource::get_margins() const { @@ -3093,6 +3473,7 @@ void TileSetAtlasSource::set_separation(Vector2i p_separation) { separation = p_separation; } + _clear_tiles_outside_texture(); emit_changed(); } Vector2i TileSetAtlasSource::get_separation() const { @@ -3107,6 +3488,7 @@ void TileSetAtlasSource::set_texture_region_size(Vector2i p_tile_size) { texture_region_size = p_tile_size; } + _clear_tiles_outside_texture(); emit_changed(); } Vector2i TileSetAtlasSource::get_texture_region_size() const { @@ -3352,7 +3734,7 @@ void TileSetAtlasSource::create_tile(const Vector2i p_atlas_coords, const Vector ERR_FAIL_COND(p_size.x <= 0 || p_size.y <= 0); bool room_for_tile = has_room_for_tile(p_atlas_coords, p_size, 1, Vector2i(), 1); - ERR_FAIL_COND_MSG(!room_for_tile, "Cannot create tile, tiles are already present in the space the tile would cover."); + ERR_FAIL_COND_MSG(!room_for_tile, "Cannot create tile. The tile is outside the texture or tiles are already present in the space the tile would cover."); // Initialize the tile data. TileAlternativesData tad; @@ -3550,9 +3932,7 @@ bool TileSetAtlasSource::has_room_for_tile(Vector2i p_atlas_coords, Vector2i p_s return false; } if (coords.x >= atlas_grid_size.x || coords.y >= atlas_grid_size.y) { - if (!(_coords_mapping_cache.has(coords) && _coords_mapping_cache[coords] == p_ignored_tile)) { - return false; // Only accept tiles outside the atlas if they are part of the ignored tile. - } + return false; } } } @@ -3560,6 +3940,37 @@ bool TileSetAtlasSource::has_room_for_tile(Vector2i p_atlas_coords, Vector2i p_s return true; } +PackedVector2Array TileSetAtlasSource::get_tiles_to_be_removed_on_change(Ref<Texture2D> p_texture, Vector2i p_margins, Vector2i p_separation, Vector2i p_texture_region_size) { + ERR_FAIL_COND_V(p_margins.x < 0 || p_margins.y < 0, PackedVector2Array()); + ERR_FAIL_COND_V(p_separation.x < 0 || p_separation.y < 0, PackedVector2Array()); + ERR_FAIL_COND_V(p_texture_region_size.x <= 0 || p_texture_region_size.y <= 0, PackedVector2Array()); + + // Compute the new atlas grid size. + Size2 new_grid_size; + if (p_texture.is_valid()) { + Size2i valid_area = p_texture->get_size() - p_margins; + + // Compute the number of valid tiles in the tiles atlas + if (valid_area.x >= p_texture_region_size.x && valid_area.y >= p_texture_region_size.y) { + valid_area -= p_texture_region_size; + new_grid_size = Size2i(1, 1) + valid_area / (p_texture_region_size + p_separation); + } + } + + Vector<Vector2> output; + for (KeyValue<Vector2i, TileAlternativesData> &E : tiles) { + for (unsigned int frame = 0; frame < E.value.animation_frames_durations.size(); frame++) { + Vector2i frame_coords = E.key + (E.value.size_in_atlas + E.value.animation_separation) * ((E.value.animation_columns > 0) ? Vector2i(frame % E.value.animation_columns, frame / E.value.animation_columns) : Vector2i(frame, 0)); + frame_coords += E.value.size_in_atlas; + if (frame_coords.x > new_grid_size.x || frame_coords.y > new_grid_size.y) { + output.push_back(E.key); + break; + } + } + } + return output; +} + Rect2i TileSetAtlasSource::get_tile_texture_region(Vector2i p_atlas_coords, int p_frame) const { ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), Rect2i(), vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords))); ERR_FAIL_INDEX_V(p_frame, (int)tiles[p_atlas_coords].animation_frames_durations.size(), Rect2i()); @@ -3624,34 +4035,6 @@ void TileSetAtlasSource::move_tile_in_atlas(Vector2i p_atlas_coords, Vector2i p_ emit_signal(SNAME("changed")); } -bool TileSetAtlasSource::has_tiles_outside_texture() { - Vector2i grid_size = get_atlas_grid_size(); - Vector<Vector2i> to_remove; - - for (const KeyValue<Vector2i, TileSetAtlasSource::TileAlternativesData> &E : tiles) { - if (E.key.x >= grid_size.x || E.key.y >= grid_size.y) { - return true; - } - } - - return false; -} - -void TileSetAtlasSource::clear_tiles_outside_texture() { - Vector2i grid_size = get_atlas_grid_size(); - Vector<Vector2i> to_remove; - - for (const KeyValue<Vector2i, TileSetAtlasSource::TileAlternativesData> &E : tiles) { - if (E.key.x >= grid_size.x || E.key.y >= grid_size.y) { - to_remove.append(E.key); - } - } - - for (int i = 0; i < to_remove.size(); i++) { - remove_tile(to_remove[i]); - } -} - int TileSetAtlasSource::create_alternative_tile(const Vector2i p_atlas_coords, int p_alternative_id_override) { ERR_FAIL_COND_V_MSG(!tiles.has(p_atlas_coords), TileSetSource::INVALID_TILE_ALTERNATIVE, vformat("TileSetAtlasSource has no tile at %s.", String(p_atlas_coords))); ERR_FAIL_COND_V_MSG(p_alternative_id_override >= 0 && tiles[p_atlas_coords].alternatives.has(p_alternative_id_override), TileSetSource::INVALID_TILE_ALTERNATIVE, vformat("Cannot create alternative tile. Another alternative exists with id %d.", p_alternative_id_override)); @@ -3752,7 +4135,7 @@ void TileSetAtlasSource::_bind_methods() { ClassDB::bind_method(D_METHOD("get_tile_size_in_atlas", "atlas_coords"), &TileSetAtlasSource::get_tile_size_in_atlas); ClassDB::bind_method(D_METHOD("has_room_for_tile", "atlas_coords", "size", "animation_columns", "animation_separation", "frames_count", "ignored_tile"), &TileSetAtlasSource::has_room_for_tile, DEFVAL(INVALID_ATLAS_COORDS)); - + ClassDB::bind_method(D_METHOD("get_tiles_to_be_removed_on_change", "texture", "margins", "separation", "texture_region_size"), &TileSetAtlasSource::get_tiles_to_be_removed_on_change); ClassDB::bind_method(D_METHOD("get_tile_at_coords", "atlas_coords"), &TileSetAtlasSource::get_tile_at_coords); ClassDB::bind_method(D_METHOD("set_tile_animation_columns", "atlas_coords", "frame_columns"), &TileSetAtlasSource::set_tile_animation_columns); @@ -3777,8 +4160,6 @@ void TileSetAtlasSource::_bind_methods() { // Helpers. ClassDB::bind_method(D_METHOD("get_atlas_grid_size"), &TileSetAtlasSource::get_atlas_grid_size); - ClassDB::bind_method(D_METHOD("has_tiles_outside_texture"), &TileSetAtlasSource::has_tiles_outside_texture); - ClassDB::bind_method(D_METHOD("clear_tiles_outside_texture"), &TileSetAtlasSource::clear_tiles_outside_texture); ClassDB::bind_method(D_METHOD("get_tile_texture_region", "atlas_coords", "frame"), &TileSetAtlasSource::get_tile_texture_region, DEFVAL(0)); } @@ -3852,6 +4233,20 @@ void TileSetAtlasSource::_create_coords_mapping_cache(Vector2i p_atlas_coords) { } } +void TileSetAtlasSource::_clear_tiles_outside_texture() { + LocalVector<Vector2i> to_remove; + + for (const KeyValue<Vector2i, TileSetAtlasSource::TileAlternativesData> &E : tiles) { + if (!has_room_for_tile(E.key, E.value.size_in_atlas, E.value.animation_columns, E.value.animation_separation, E.value.animation_frames_durations.size(), E.key)) { + to_remove.push_back(E.key); + } + } + + for (unsigned int i = 0; i < to_remove.size(); i++) { + remove_tile(to_remove[i]); + } +} + /////////////////////////////// TileSetScenesCollectionSource ////////////////////////////////////// void TileSetScenesCollectionSource::_compute_next_alternative_id() { @@ -4243,6 +4638,37 @@ bool TileData::is_allowing_transform() const { return allow_transform; } +TileData *TileData::duplicate() { + TileData *output = memnew(TileData); + output->tile_set = tile_set; + + output->allow_transform = allow_transform; + + // Rendering + output->flip_h = flip_h; + output->flip_v = flip_v; + output->transpose = transpose; + output->tex_offset = tex_offset; + output->material = material; + output->modulate = modulate; + output->z_index = z_index; + output->y_sort_origin = y_sort_origin; + output->occluders = occluders; + // Physics + output->physics = physics; + // Terrain + output->terrain_set = -1; + memcpy(output->terrain_peering_bits, terrain_peering_bits, 16 * sizeof(int)); + // Navigation + output->navigation = navigation; + // Misc + output->probability = probability; + // Custom data + output->custom_data = custom_data; + + return output; +} + // Rendering void TileData::set_flip_h(bool p_flip_h) { ERR_FAIL_COND_MSG(!allow_transform && p_flip_h, "Transform is only allowed for alternative tiles (with its alternative_id != 0)"); @@ -4486,6 +4912,18 @@ bool TileData::is_valid_peering_bit_terrain(TileSet::CellNeighbor p_peering_bit) return tile_set->is_valid_peering_bit_terrain(terrain_set, p_peering_bit); } +TileSet::TerrainsPattern TileData::get_terrains_pattern() const { + ERR_FAIL_COND_V(!tile_set, TileSet::TerrainsPattern()); + + TileSet::TerrainsPattern output; + for (int i = 0; i < TileSet::CELL_NEIGHBOR_MAX; i++) { + if (tile_set->is_valid_peering_bit_terrain(terrain_set, TileSet::CellNeighbor(i))) { + output.push_back(get_peering_bit_terrain(TileSet::CellNeighbor(i))); + } + } + return output; +} + // Navigation void TileData::set_navigation_polygon(int p_layer_id, Ref<NavigationPolygon> p_navigation_polygon) { ERR_FAIL_INDEX(p_layer_id, navigation.size()); diff --git a/scene/resources/tile_set.h b/scene/resources/tile_set.h index 716b66405f..077315e58d 100644 --- a/scene/resources/tile_set.h +++ b/scene/resources/tile_set.h @@ -60,6 +60,84 @@ class TileSetPluginAtlasRendering; class TileSetPluginAtlasPhysics; class TileSetPluginAtlasNavigation; +union TileMapCell { + struct { + int32_t source_id : 16; + int16_t coord_x : 16; + int16_t coord_y : 16; + int32_t alternative_tile : 16; + }; + + uint64_t _u64t; + TileMapCell(int p_source_id = -1, Vector2i p_atlas_coords = Vector2i(-1, -1), int p_alternative_tile = -1) { // default are INVALID_SOURCE, INVALID_ATLAS_COORDS, INVALID_TILE_ALTERNATIVE + source_id = p_source_id; + set_atlas_coords(p_atlas_coords); + alternative_tile = p_alternative_tile; + } + + Vector2i get_atlas_coords() const { + return Vector2i(coord_x, coord_y); + } + + void set_atlas_coords(const Vector2i &r_coords) { + coord_x = r_coords.x; + coord_y = r_coords.y; + } + + bool operator<(const TileMapCell &p_other) const { + if (source_id == p_other.source_id) { + if (coord_x == p_other.coord_x) { + if (coord_y == p_other.coord_y) { + return alternative_tile < p_other.alternative_tile; + } else { + return coord_y < p_other.coord_y; + } + } else { + return coord_x < p_other.coord_x; + } + } else { + return source_id < p_other.source_id; + } + } + + bool operator!=(const TileMapCell &p_other) const { + return !(source_id == p_other.source_id && coord_x == p_other.coord_x && coord_y == p_other.coord_y && alternative_tile == p_other.alternative_tile); + } +}; + +class TileMapPattern : public Resource { + GDCLASS(TileMapPattern, Resource); + + Vector2i size; + Map<Vector2i, TileMapCell> pattern; + + void _set_tile_data(const Vector<int> &p_data); + Vector<int> _get_tile_data() const; + +protected: + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _get_property_list(List<PropertyInfo> *p_list) const; + + static void _bind_methods(); + +public: + void set_cell(const Vector2i &p_coords, int p_source_id, const Vector2i p_atlas_coords, int p_alternative_tile = 0); + bool has_cell(const Vector2i &p_coords) const; + void remove_cell(const Vector2i &p_coords, bool p_update_size = true); + int get_cell_source_id(const Vector2i &p_coords) const; + Vector2i get_cell_atlas_coords(const Vector2i &p_coords) const; + int get_cell_alternative_tile(const Vector2i &p_coords) const; + + TypedArray<Vector2i> get_used_cells() const; + + Vector2i get_size() const; + void set_size(const Vector2i &p_size); + bool is_empty() const; + + void clear(); +}; + class TileSet : public Resource { GDCLASS(TileSet, Resource); @@ -175,6 +253,7 @@ public: Ref<PackedScene> scene; Vector2 offset; }; + typedef Array TerrainsPattern; protected: bool _set(const StringName &p_name, const Variant &p_value); @@ -225,6 +304,10 @@ private: Map<TerrainMode, Map<CellNeighbor, Ref<ArrayMesh>>> terrain_bits_meshes; bool terrain_bits_meshes_dirty = true; + LocalVector<Map<TileSet::TerrainsPattern, Set<TileMapCell>>> per_terrain_pattern_tiles; // Cached data. + bool terrains_cache_dirty = true; + void _update_terrains_cache(); + // Navigation struct NavigationLayer { uint32_t layers = 1; @@ -245,6 +328,8 @@ private: int next_source_id = 0; // --------------------- + LocalVector<Ref<TileMapPattern>> patterns; + void _compute_next_source_id(); void _source_changed(); @@ -384,6 +469,17 @@ public: void cleanup_invalid_tile_proxies(); void clear_tile_proxies(); + // Patterns. + int add_pattern(Ref<TileMapPattern> p_pattern, int p_index = -1); + Ref<TileMapPattern> get_pattern(int p_index); + void remove_pattern(int p_index); + int get_patterns_count(); + + // Terrains. + Set<TerrainsPattern> get_terrains_pattern_set(int p_terrain_set); + Set<TileMapCell> get_tiles_for_terrains_pattern(int p_terrain_set, TerrainsPattern p_terrain_tile_pattern); + TileMapCell get_random_tile_from_pattern(int p_terrain_set, TerrainsPattern p_terrain_tile_pattern); + // Helpers Vector<Vector2> get_tile_shape_polygon(); void draw_tile_shape(CanvasItem *p_canvas_item, Transform2D p_transform, Color p_color, bool p_filled = false, Ref<Texture2D> p_texture = Ref<Texture2D>()); @@ -479,8 +575,10 @@ private: void _compute_next_alternative_id(const Vector2i p_atlas_coords); - void _create_coords_mapping_cache(Vector2i p_atlas_coords); void _clear_coords_mapping_cache(Vector2i p_atlas_coords); + void _create_coords_mapping_cache(Vector2i p_atlas_coords); + + void _clear_tiles_outside_texture(); protected: bool _set(const StringName &p_name, const Variant &p_value); @@ -534,7 +632,7 @@ public: virtual Vector2i get_tile_id(int p_index) const override; bool has_room_for_tile(Vector2i p_atlas_coords, Vector2i p_size, int p_animation_columns, Vector2i p_animation_separation, int p_frames_count, Vector2i p_ignored_tile = INVALID_ATLAS_COORDS) const; - + PackedVector2Array get_tiles_to_be_removed_on_change(Ref<Texture2D> p_texture, Vector2i p_margins, Vector2i p_separation, Vector2i p_texture_region_size); Vector2i get_tile_at_coords(Vector2i p_atlas_coords) const; // Animation. @@ -565,8 +663,6 @@ public: // Helpers. Vector2i get_atlas_grid_size() const; - bool has_tiles_outside_texture(); - void clear_tiles_outside_texture(); Rect2i get_tile_texture_region(Vector2i p_atlas_coords, int p_frame = 0) const; Vector2i get_tile_effective_texture_offset(Vector2i p_atlas_coords, int p_alternative_tile) const; @@ -698,6 +794,9 @@ public: void set_allow_transform(bool p_allow_transform); bool is_allowing_transform() const; + // To duplicate a TileData object, needed for runtiume update. + TileData *duplicate(); + // Rendering void set_flip_h(bool p_flip_h); bool get_flip_h() const; @@ -745,6 +844,8 @@ public: int get_peering_bit_terrain(TileSet::CellNeighbor p_peering_bit) const; bool is_valid_peering_bit_terrain(TileSet::CellNeighbor p_peering_bit) const; + TileSet::TerrainsPattern get_terrains_pattern() const; // Not exposed. + // Navigation void set_navigation_polygon(int p_layer_id, Ref<NavigationPolygon> p_navigation_polygon); Ref<NavigationPolygon> get_navigation_polygon(int p_layer_id) const; diff --git a/scene/resources/visual_shader.cpp b/scene/resources/visual_shader.cpp index 934d16bd7e..fd785631a8 100644 --- a/scene/resources/visual_shader.cpp +++ b/scene/resources/visual_shader.cpp @@ -113,6 +113,10 @@ bool VisualShaderNode::is_output_port_expandable(int p_port) const { return false; } +bool VisualShaderNode::has_output_port_preview(int p_port) const { + return true; +} + void VisualShaderNode::_set_output_ports_expanded(const Array &p_values) { for (int i = 0; i < p_values.size(); i++) { expanded_output_ports[p_values[i]] = true; @@ -1092,6 +1096,7 @@ static const char *type_string[VisualShader::TYPE_MAX] = { "start_custom", "process_custom", "sky", + "fog", }; bool VisualShader::_set(const StringName &p_name, const Variant &p_value) { @@ -1241,7 +1246,7 @@ void VisualShader::reset_state() { } void VisualShader::_get_property_list(List<PropertyInfo> *p_list) const { //mode - p_list->push_back(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Node3D,CanvasItem,Particles,Sky")); + p_list->push_back(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Node3D,CanvasItem,Particles,Sky,Fog")); //render modes Map<String, String> blend_mode_enums; @@ -1632,7 +1637,7 @@ void VisualShader::_update_shader() const { Vector<VisualShader::DefaultTextureParam> default_tex_params; Set<StringName> classes; Map<int, int> insertion_pos; - static const char *shader_mode_str[Shader::MODE_MAX] = { "spatial", "canvas_item", "particles", "sky" }; + static const char *shader_mode_str[Shader::MODE_MAX] = { "spatial", "canvas_item", "particles", "sky", "fog" }; global_code += String() + "shader_type " + shader_mode_str[shader_mode] + ";\n"; @@ -1680,7 +1685,7 @@ void VisualShader::_update_shader() const { global_code += "render_mode " + render_mode + ";\n\n"; } - static const char *func_name[TYPE_MAX] = { "vertex", "fragment", "light", "start", "process", "collide", "start_custom", "process_custom", "sky" }; + static const char *func_name[TYPE_MAX] = { "vertex", "fragment", "light", "start", "process", "collide", "start_custom", "process_custom", "sky", "fog" }; String global_expressions; Set<String> used_uniform_names; @@ -1751,7 +1756,7 @@ void VisualShader::_update_shader() const { StringBuilder func_code; bool is_empty_func = false; - if (shader_mode != Shader::MODE_PARTICLES && shader_mode != Shader::MODE_SKY) { + if (shader_mode != Shader::MODE_PARTICLES && shader_mode != Shader::MODE_SKY && shader_mode != Shader::MODE_FOG) { is_empty_func = true; } @@ -2026,6 +2031,7 @@ void VisualShader::_bind_methods() { BIND_ENUM_CONSTANT(TYPE_START_CUSTOM); BIND_ENUM_CONSTANT(TYPE_PROCESS_CUSTOM); BIND_ENUM_CONSTANT(TYPE_SKY); + BIND_ENUM_CONSTANT(TYPE_FOG); BIND_ENUM_CONSTANT(TYPE_MAX); BIND_CONSTANT(NODE_ID_INVALID); @@ -2303,11 +2309,35 @@ const VisualShaderNodeInput::Port VisualShaderNodeInput::ports[] = { { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR, "sky_coords", "vec3(SKY_COORDS, 0.0)" }, { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, + // Fog, Fog + + { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR, "world_position", "WORLD_POSITION" }, + { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR, "object_position", "OBJECT_POSITION" }, + { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR, "uvw", "UVW" }, + { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR, "extents", "EXTENTS" }, + { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" }, + { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "sdf", "SDF" }, + { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, + { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr }, }; const VisualShaderNodeInput::Port VisualShaderNodeInput::preview_ports[] = { + // Spatial, Vertex + + { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "vec3(0.0, 0.0, 1.0)" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "tangent", "vec3(0.0, 1.0, 0.0)" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "binormal", "vec3(1.0, 0.0, 0.0)" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv2", "vec3(UV, 0.0)" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "viewport_size", "vec3(1.0, 1.0, 0.0)" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, + // Spatial, Fragment + + { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "fragcoord", "FRAGCOORD.rgb" }, { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "vec3(0.0, 0.0, 1.0)" }, { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "tangent", "vec3(0.0, 1.0, 0.0)" }, { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "binormal", "vec3(1.0, 0.0, 0.0)" }, @@ -2315,44 +2345,63 @@ const VisualShaderNodeInput::Port VisualShaderNodeInput::preview_ports[] = { { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv2", "vec3(UV, 0.0)" }, { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" }, { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" }, - { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV, 0.0)" }, - { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "side", "1.0" }, - - { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "viewport_size", "vec3(1.0, 1.0, 0.0)" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, // Spatial, Light - { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "vec3(0.0, 0.0, 1.0)" }, - { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, + + { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "fragcoord", "FRAGCOORD.rgb" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "vec3(0.0, 0.0, 1.0)" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "uv2", "vec3(UV, 0.0)" }, { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "viewport_size", "vec3(1.0, 1.0, 0.0)" }, + { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, + // Canvas Item, Vertex + { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "vec3(VERTEX, 0.0)" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, + // Canvas Item, Fragment + + { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "fragcoord", "FRAGCOORD.rgb" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV, 0.0)" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, + // Canvas Item, Light + + { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "fragcoord", "FRAGCOORD.rgb" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "vec3(0.0, 0.0, 1.0)" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" }, - { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV, 0.0)" }, { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, // Particles + { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, + + // Sky + + { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV, 0.0)" }, + { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, + + // Fog + + { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" }, + { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr }, }; @@ -2413,13 +2462,10 @@ String VisualShaderNodeInput::generate_code(Shader::Mode p_mode, VisualShader::T case PORT_TYPE_VECTOR: { code = " " + p_output_vars[0] + " = vec3(0.0);\n"; } break; - case PORT_TYPE_TRANSFORM: { - code = " " + p_output_vars[0] + " = mat4(vec4(1.0, 0.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n"; - } break; case PORT_TYPE_BOOLEAN: { code = " " + p_output_vars[0] + " = false;\n"; } break; - default: //default (none found) is scalar + default: break; } } @@ -2903,6 +2949,13 @@ const VisualShaderNodeOutput::Port VisualShaderNodeOutput::ports[] = { { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "fog_alpha", "FOG.a" }, //////////////////////////////////////////////////////////////////////// + // Fog, Fog. + //////////////////////////////////////////////////////////////////////// + { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "density", "DENSITY" }, + { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR, "albedo", "ALBEDO" }, + { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR, "emission", "EMISSION" }, + + //////////////////////////////////////////////////////////////////////// { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr }, }; diff --git a/scene/resources/visual_shader.h b/scene/resources/visual_shader.h index b3efac02aa..19530e5a34 100644 --- a/scene/resources/visual_shader.h +++ b/scene/resources/visual_shader.h @@ -57,6 +57,7 @@ public: TYPE_START_CUSTOM, TYPE_PROCESS_CUSTOM, TYPE_SKY, + TYPE_FOG, TYPE_MAX }; @@ -254,6 +255,8 @@ public: void set_input_port_connected(int p_port, bool p_connected); virtual bool is_generate_input_var(int p_port) const; + virtual bool has_output_port_preview(int p_port) const; + virtual bool is_output_port_expandable(int p_port) const; void _set_output_ports_expanded(const Array &p_data); Array _get_output_ports_expanded() const; diff --git a/scene/resources/visual_shader_nodes.cpp b/scene/resources/visual_shader_nodes.cpp index e45dfdcb1b..c3d9ef7b04 100644 --- a/scene/resources/visual_shader_nodes.cpp +++ b/scene/resources/visual_shader_nodes.cpp @@ -918,6 +918,7 @@ bool VisualShaderNodeCurveTexture::is_use_prop_slots() const { } VisualShaderNodeCurveTexture::VisualShaderNodeCurveTexture() { + set_input_port_default_value(0, 0.0); simple_decl = true; allow_v_resize = false; } @@ -1002,6 +1003,7 @@ bool VisualShaderNodeCurveXYZTexture::is_use_prop_slots() const { } VisualShaderNodeCurveXYZTexture::VisualShaderNodeCurveXYZTexture() { + set_input_port_default_value(0, 0.0); simple_decl = true; allow_v_resize = false; } diff --git a/scene/resources/visual_shader_particle_nodes.cpp b/scene/resources/visual_shader_particle_nodes.cpp index 5fe801e037..18b933e5cf 100644 --- a/scene/resources/visual_shader_particle_nodes.cpp +++ b/scene/resources/visual_shader_particle_nodes.cpp @@ -47,6 +47,10 @@ String VisualShaderNodeParticleEmitter::get_output_port_name(int p_port) const { return String(); } +bool VisualShaderNodeParticleEmitter::has_output_port_preview(int p_port) const { + return false; +} + VisualShaderNodeParticleEmitter::VisualShaderNodeParticleEmitter() { } @@ -265,6 +269,10 @@ Vector<StringName> VisualShaderNodeParticleMultiplyByAxisAngle::get_editable_pro return props; } +bool VisualShaderNodeParticleMultiplyByAxisAngle::has_output_port_preview(int p_port) const { + return false; +} + VisualShaderNodeParticleMultiplyByAxisAngle::VisualShaderNodeParticleMultiplyByAxisAngle() { set_input_port_default_value(1, Vector3(1, 0, 0)); set_input_port_default_value(2, 0.0); @@ -313,6 +321,10 @@ String VisualShaderNodeParticleConeVelocity::get_output_port_name(int p_port) co return String(); } +bool VisualShaderNodeParticleConeVelocity::has_output_port_preview(int p_port) const { + return false; +} + String VisualShaderNodeParticleConeVelocity::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const { String code; code += " __radians = radians(" + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n"; @@ -421,6 +433,10 @@ VisualShaderNodeParticleRandomness::OpType VisualShaderNodeParticleRandomness::g return op_type; } +bool VisualShaderNodeParticleRandomness::has_output_port_preview(int p_port) const { + return false; +} + VisualShaderNodeParticleRandomness::VisualShaderNodeParticleRandomness() { set_input_port_default_value(0, 0.0); set_input_port_default_value(1, 1.0); @@ -521,6 +537,10 @@ VisualShaderNodeParticleAccelerator::Mode VisualShaderNodeParticleAccelerator::g return mode; } +bool VisualShaderNodeParticleAccelerator::has_output_port_preview(int p_port) const { + return false; +} + VisualShaderNodeParticleAccelerator::VisualShaderNodeParticleAccelerator() { set_input_port_default_value(0, Vector3(1, 1, 1)); set_input_port_default_value(1, 0.0); diff --git a/scene/resources/visual_shader_particle_nodes.h b/scene/resources/visual_shader_particle_nodes.h index f5435c3511..b8bc7992cc 100644 --- a/scene/resources/visual_shader_particle_nodes.h +++ b/scene/resources/visual_shader_particle_nodes.h @@ -42,6 +42,7 @@ public: virtual int get_output_port_count() const override; virtual PortType get_output_port_type(int p_port) const override; virtual String get_output_port_name(int p_port) const override; + virtual bool has_output_port_preview(int p_port) const override; VisualShaderNodeParticleEmitter(); }; @@ -112,6 +113,7 @@ public: virtual int get_output_port_count() const override; virtual PortType get_output_port_type(int p_port) const override; virtual String get_output_port_name(int p_port) const override; + virtual bool has_output_port_preview(int p_port) const override; virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview = false) const override; @@ -135,6 +137,7 @@ public: virtual int get_output_port_count() const override; virtual PortType get_output_port_type(int p_port) const override; virtual String get_output_port_name(int p_port) const override; + virtual bool has_output_port_preview(int p_port) const override; virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview = false) const override; @@ -168,6 +171,7 @@ public: virtual int get_output_port_count() const override; virtual PortType get_output_port_type(int p_port) const override; virtual String get_output_port_name(int p_port) const override; + virtual bool has_output_port_preview(int p_port) const override; virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview = false) const override; @@ -209,6 +213,7 @@ public: virtual int get_output_port_count() const override; virtual PortType get_output_port_type(int p_port) const override; virtual String get_output_port_name(int p_port) const override; + virtual bool has_output_port_preview(int p_port) const override; virtual String generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview = false) const override; |