2018-08-29 22:38:13 +02:00
|
|
|
/**************************************************************************/
|
|
|
|
/* root_motion_view.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2024-01-06 14:06:10 +01:00
|
|
|
#ifndef _3D_DISABLED
|
|
|
|
|
2018-06-27 00:05:11 +02:00
|
|
|
#include "root_motion_view.h"
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2018-06-27 00:05:11 +02:00
|
|
|
#include "scene/animation/animation_tree.h"
|
|
|
|
#include "scene/resources/material.h"
|
2022-02-15 18:06:48 +01:00
|
|
|
|
2023-07-20 17:34:06 +02:00
|
|
|
void RootMotionView::set_animation_mixer(const NodePath &p_path) {
|
2018-06-27 00:05:11 +02:00
|
|
|
path = p_path;
|
|
|
|
first = true;
|
|
|
|
}
|
|
|
|
|
2023-07-20 17:34:06 +02:00
|
|
|
NodePath RootMotionView::get_animation_mixer() const {
|
2018-06-27 00:05:11 +02:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RootMotionView::set_color(const Color &p_color) {
|
|
|
|
color = p_color;
|
|
|
|
first = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color RootMotionView::get_color() const {
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RootMotionView::set_cell_size(float p_size) {
|
|
|
|
cell_size = p_size;
|
|
|
|
first = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
float RootMotionView::get_cell_size() const {
|
|
|
|
return cell_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RootMotionView::set_radius(float p_radius) {
|
|
|
|
radius = p_radius;
|
|
|
|
first = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
float RootMotionView::get_radius() const {
|
|
|
|
return radius;
|
|
|
|
}
|
|
|
|
|
2018-07-01 00:58:37 +02:00
|
|
|
void RootMotionView::set_zero_y(bool p_zero_y) {
|
|
|
|
zero_y = p_zero_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RootMotionView::get_zero_y() const {
|
|
|
|
return zero_y;
|
|
|
|
}
|
|
|
|
|
2018-06-27 00:05:11 +02:00
|
|
|
void RootMotionView::_notification(int p_what) {
|
2022-02-15 18:06:48 +01:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2023-01-24 11:24:53 +01:00
|
|
|
immediate_material = StandardMaterial3D::get_material_for_2d(false, BaseMaterial3D::TRANSPARENCY_ALPHA, false);
|
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
first = true;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_INTERNAL_PROCESS:
|
|
|
|
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
|
|
|
|
Transform3D transform;
|
2023-02-07 22:57:00 +01:00
|
|
|
Basis diff;
|
2022-02-15 18:06:48 +01:00
|
|
|
|
|
|
|
if (has_node(path)) {
|
|
|
|
Node *node = get_node(path);
|
|
|
|
|
2023-07-20 17:34:06 +02:00
|
|
|
AnimationMixer *mixer = Object::cast_to<AnimationMixer>(node);
|
|
|
|
if (mixer && mixer->is_active() && mixer->get_root_motion_track() != NodePath()) {
|
|
|
|
if (is_processing_internal() && mixer->get_callback_mode_process() == AnimationMixer::ANIMATION_CALLBACK_MODE_PROCESS_PHYSICS) {
|
2022-02-15 18:06:48 +01:00
|
|
|
set_process_internal(false);
|
|
|
|
set_physics_process_internal(true);
|
|
|
|
}
|
|
|
|
|
2023-07-20 17:34:06 +02:00
|
|
|
if (is_physics_processing_internal() && mixer->get_callback_mode_process() == AnimationMixer::ANIMATION_CALLBACK_MODE_PROCESS_IDLE) {
|
2022-02-15 18:06:48 +01:00
|
|
|
set_process_internal(true);
|
|
|
|
set_physics_process_internal(false);
|
|
|
|
}
|
2023-07-20 17:34:06 +02:00
|
|
|
transform.origin = mixer->get_root_motion_position();
|
|
|
|
transform.basis = mixer->get_root_motion_rotation(); // Scale is meaningless.
|
|
|
|
diff = mixer->get_root_motion_rotation_accumulator();
|
2018-06-27 00:05:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
if (!first && transform == Transform3D()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
first = false;
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-11-26 08:04:15 +01:00
|
|
|
accumulated.basis *= transform.basis;
|
2023-02-07 22:57:00 +01:00
|
|
|
transform.origin = (diff.inverse() * accumulated.basis).xform(transform.origin);
|
|
|
|
accumulated.origin += transform.origin;
|
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
accumulated.origin.x = Math::fposmod(accumulated.origin.x, cell_size);
|
|
|
|
if (zero_y) {
|
|
|
|
accumulated.origin.y = 0;
|
|
|
|
}
|
|
|
|
accumulated.origin.z = Math::fposmod(accumulated.origin.z, cell_size);
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
immediate->clear_surfaces();
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
int cells_in_radius = int((radius / cell_size) + 1.0);
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
immediate->surface_begin(Mesh::PRIMITIVE_LINES, immediate_material);
|
2021-06-30 03:55:11 +02:00
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
for (int i = -cells_in_radius; i < cells_in_radius; i++) {
|
|
|
|
for (int j = -cells_in_radius; j < cells_in_radius; j++) {
|
|
|
|
Vector3 from(i * cell_size, 0, j * cell_size);
|
|
|
|
Vector3 from_i((i + 1) * cell_size, 0, j * cell_size);
|
|
|
|
Vector3 from_j(i * cell_size, 0, (j + 1) * cell_size);
|
2022-04-18 17:03:58 +02:00
|
|
|
from = accumulated.xform_inv(from);
|
|
|
|
from_i = accumulated.xform_inv(from_i);
|
|
|
|
from_j = accumulated.xform_inv(from_j);
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
Color c = color, c_i = color, c_j = color;
|
|
|
|
c.a *= MAX(0, 1.0 - from.length() / radius);
|
|
|
|
c_i.a *= MAX(0, 1.0 - from_i.length() / radius);
|
|
|
|
c_j.a *= MAX(0, 1.0 - from_j.length() / radius);
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
immediate->surface_set_color(c);
|
|
|
|
immediate->surface_add_vertex(from);
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
immediate->surface_set_color(c_i);
|
|
|
|
immediate->surface_add_vertex(from_i);
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
immediate->surface_set_color(c);
|
|
|
|
immediate->surface_add_vertex(from);
|
2018-06-27 00:05:11 +02:00
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
immediate->surface_set_color(c_j);
|
|
|
|
immediate->surface_add_vertex(from_j);
|
|
|
|
}
|
2018-06-27 00:05:11 +02:00
|
|
|
}
|
|
|
|
|
2022-02-15 18:06:48 +01:00
|
|
|
immediate->surface_end();
|
|
|
|
} break;
|
2018-06-27 00:05:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AABB RootMotionView::get_aabb() const {
|
|
|
|
return AABB(Vector3(-radius, 0, -radius), Vector3(radius * 2, 0.001, radius * 2));
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2018-06-27 00:05:11 +02:00
|
|
|
void RootMotionView::_bind_methods() {
|
2023-07-20 17:34:06 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_animation_path", "path"), &RootMotionView::set_animation_mixer);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_animation_path"), &RootMotionView::get_animation_mixer);
|
2018-06-27 00:05:11 +02:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_color", "color"), &RootMotionView::set_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_color"), &RootMotionView::get_color);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &RootMotionView::set_cell_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_cell_size"), &RootMotionView::get_cell_size);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_radius", "size"), &RootMotionView::set_radius);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_radius"), &RootMotionView::get_radius);
|
|
|
|
|
2018-07-01 00:58:37 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_zero_y", "enable"), &RootMotionView::set_zero_y);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_zero_y"), &RootMotionView::get_zero_y);
|
|
|
|
|
2023-07-20 17:34:06 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "animation_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "AnimationMixer"), "set_animation_path", "get_animation_path");
|
2018-06-27 00:05:11 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
|
2022-05-20 07:24:41 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_size", PROPERTY_HINT_RANGE, "0.1,16,0.01,or_greater,suffix:m"), "set_cell_size", "get_cell_size");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.1,16,0.01,or_greater,suffix:m"), "set_radius", "get_radius");
|
2018-07-01 00:58:37 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "zero_y"), "set_zero_y", "get_zero_y");
|
2018-06-27 00:05:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RootMotionView::RootMotionView() {
|
2021-06-30 03:55:11 +02:00
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
set_process_internal(true);
|
|
|
|
}
|
|
|
|
immediate.instantiate();
|
|
|
|
set_base(immediate->get_rid());
|
2018-06-27 00:05:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RootMotionView::~RootMotionView() {
|
|
|
|
set_base(RID());
|
|
|
|
}
|
2024-01-06 14:06:10 +01:00
|
|
|
|
|
|
|
#endif // _3D_DISABLED
|