Path: blob/master/scene/animation/root_motion_view.cpp
9896 views
/**************************************************************************/1/* root_motion_view.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#ifndef _3D_DISABLED3132#include "root_motion_view.h"3334#include "scene/animation/animation_mixer.h"35#include "scene/resources/material.h"3637void RootMotionView::set_animation_mixer(const NodePath &p_path) {38path = p_path;39first = true;40}4142NodePath RootMotionView::get_animation_mixer() const {43return path;44}4546void RootMotionView::set_color(const Color &p_color) {47color = p_color;48first = true;49}5051Color RootMotionView::get_color() const {52return color;53}5455void RootMotionView::set_cell_size(float p_size) {56cell_size = p_size;57first = true;58}5960float RootMotionView::get_cell_size() const {61return cell_size;62}6364void RootMotionView::set_radius(float p_radius) {65radius = p_radius;66first = true;67}6869float RootMotionView::get_radius() const {70return radius;71}7273void RootMotionView::set_zero_y(bool p_zero_y) {74zero_y = p_zero_y;75}7677bool RootMotionView::get_zero_y() const {78return zero_y;79}8081void RootMotionView::_notification(int p_what) {82switch (p_what) {83case NOTIFICATION_ENTER_TREE: {84immediate_material = StandardMaterial3D::get_material_for_2d(false, BaseMaterial3D::TRANSPARENCY_ALPHA, false);8586first = true;87} break;8889case NOTIFICATION_INTERNAL_PROCESS:90case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {91Transform3D transform;92Basis diff;9394if (has_node(path)) {95Node *node = get_node(path);96AnimationMixer *mixer = Object::cast_to<AnimationMixer>(node);97if (mixer && mixer->is_active() && mixer->get_root_motion_track() != NodePath()) {98if (is_processing_internal() && mixer->get_callback_mode_process() == AnimationMixer::ANIMATION_CALLBACK_MODE_PROCESS_PHYSICS) {99set_process_internal(false);100set_physics_process_internal(true);101}102103if (is_physics_processing_internal() && mixer->get_callback_mode_process() == AnimationMixer::ANIMATION_CALLBACK_MODE_PROCESS_IDLE) {104set_process_internal(true);105set_physics_process_internal(false);106}107108transform.origin = mixer->get_root_motion_position();109transform.basis = mixer->get_root_motion_rotation(); // Scale is meaningless.110diff = mixer->is_root_motion_local() ? Quaternion() : mixer->get_root_motion_rotation_accumulator();111}112}113if (!first && transform == Transform3D()) {114return;115}116117first = false;118119accumulated.basis *= transform.basis;120transform.origin = (diff.inverse() * accumulated.basis).xform(transform.origin);121accumulated.origin += transform.origin;122123accumulated.origin.x = Math::fposmod(accumulated.origin.x, cell_size);124if (zero_y) {125accumulated.origin.y = 0;126}127accumulated.origin.z = Math::fposmod(accumulated.origin.z, cell_size);128129immediate->clear_surfaces();130131int cells_in_radius = int((radius / cell_size) + 1.0);132133immediate->surface_begin(Mesh::PRIMITIVE_LINES, immediate_material);134135for (int i = -cells_in_radius; i < cells_in_radius; i++) {136for (int j = -cells_in_radius; j < cells_in_radius; j++) {137Vector3 from(i * cell_size, 0, j * cell_size);138Vector3 from_i((i + 1) * cell_size, 0, j * cell_size);139Vector3 from_j(i * cell_size, 0, (j + 1) * cell_size);140from = accumulated.xform_inv(from);141from_i = accumulated.xform_inv(from_i);142from_j = accumulated.xform_inv(from_j);143144Color c = color, c_i = color, c_j = color;145c.a *= MAX(0, 1.0 - from.length() / radius);146c_i.a *= MAX(0, 1.0 - from_i.length() / radius);147c_j.a *= MAX(0, 1.0 - from_j.length() / radius);148149immediate->surface_set_color(c);150immediate->surface_add_vertex(from);151152immediate->surface_set_color(c_i);153immediate->surface_add_vertex(from_i);154155immediate->surface_set_color(c);156immediate->surface_add_vertex(from);157158immediate->surface_set_color(c_j);159immediate->surface_add_vertex(from_j);160}161}162163immediate->surface_end();164} break;165}166}167168AABB RootMotionView::get_aabb() const {169return AABB(Vector3(-radius, 0, -radius), Vector3(radius * 2, 0.001, radius * 2));170}171172void RootMotionView::_bind_methods() {173ClassDB::bind_method(D_METHOD("set_animation_path", "path"), &RootMotionView::set_animation_mixer);174ClassDB::bind_method(D_METHOD("get_animation_path"), &RootMotionView::get_animation_mixer);175176ClassDB::bind_method(D_METHOD("set_color", "color"), &RootMotionView::set_color);177ClassDB::bind_method(D_METHOD("get_color"), &RootMotionView::get_color);178179ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &RootMotionView::set_cell_size);180ClassDB::bind_method(D_METHOD("get_cell_size"), &RootMotionView::get_cell_size);181182ClassDB::bind_method(D_METHOD("set_radius", "size"), &RootMotionView::set_radius);183ClassDB::bind_method(D_METHOD("get_radius"), &RootMotionView::get_radius);184185ClassDB::bind_method(D_METHOD("set_zero_y", "enable"), &RootMotionView::set_zero_y);186ClassDB::bind_method(D_METHOD("get_zero_y"), &RootMotionView::get_zero_y);187188ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "animation_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "AnimationMixer"), "set_animation_path", "get_animation_path");189ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");190ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_size", PROPERTY_HINT_RANGE, "0.1,16,0.01,or_greater,suffix:m"), "set_cell_size", "get_cell_size");191ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.1,16,0.01,or_greater,suffix:m"), "set_radius", "get_radius");192ADD_PROPERTY(PropertyInfo(Variant::BOOL, "zero_y"), "set_zero_y", "get_zero_y");193}194195RootMotionView::RootMotionView() {196if (Engine::get_singleton()->is_editor_hint()) {197set_process_internal(true);198}199immediate.instantiate();200set_base(immediate->get_rid());201}202203RootMotionView::~RootMotionView() {204set_base(RID());205}206207#endif // _3D_DISABLED208209210