Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/pooled_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class PooledList {
r_id = list.size();
list.resize(r_id + 1);

static_assert((!zero_on_first_request) || (__is_pod(T)), "zero_on_first_request requires trivial type");
static_assert((!zero_on_first_request) || (std::is_trivially_destructible<T>::value), "zero_on_first_request requires trivial type");
if (zero_on_first_request && __is_pod(T)) {
list[r_id] = {};
}
Expand Down
59 changes: 59 additions & 0 deletions doc/classes/VisualServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,65 @@
[/codeblock]
</description>
</method>
<method name="fti_instance_create">
<return type="RID" />
<description>
FTI ("fixed timestep interpolation") instance provides a convenience physics interpolation wrapper for users creating instances directly on the server rather than through Nodes.
FTI instance internally contains a regular instance, but provides extra functionality. It manages the lifetime of the regular instance for you (freeing it when the FTI instance is freed).
There are thus two [code]RIDs[/code] to be concerned with - the FTI instance [code]RID[/code], and the internal regular instance [code]RID[/code], which can be retrieved using [method fti_instance_get_instance].
[codeblock]
# Create an FTI instance and link it to the regular instance.
var fti_instance = VisualServer.fti_instance_create()
var instance = VisualServer.fti_instance_get_instance(fti_instance)

# Set the scenario from the world, this ensures it
# appears with the same objects as the scene.
var scenario = get_world().scenario
VisualServer.instance_set_scenario(instance, scenario)

# Add an example mesh to it.
var mesh = load("res://test_model.obj")
VisualServer.instance_set_base(instance, mesh)

# Use this function instead of instance_set_transform().
VisualServer.fti_instance_set_transform(fti_instance, xform)

# This function provides reset_physics_interpolation().
VisualServer.fti_instance_reset(fti_instance)

# Be sure to free RID on the server after use.
# Note that the fti_instance automatically frees
# the underlying regular instance, so you should not
# call free_rid(instance), as it would result
# in trying to free an already deleted object.
VisualServer.free_rid(fti_instance)
[/codeblock]
</description>
</method>
<method name="fti_instance_get_instance">
<return type="RID" />
<argument index="0" name="fti_instance" type="RID" />
<description>
This function provides access to the internal regular instance [code]RID[/code], which you need in order to call regular non-FTI VisualServer functions.
[b]Note:[/b] There is no need to call free for this [code]RID[/code]. The lifetime will be managed for you by the FTI instance.
[b]Note:[/b] It is recommended to call this function only once during FTI instance creation, and keep a record of the instance [code]RID[/code] in your own code. This is because the function could potentially cause a stall reading back from the server, so it is important not to call it every frame.
</description>
</method>
<method name="fti_instance_reset">
<return type="void" />
<argument index="0" name="fti_instance" type="RID" />
<description>
Equivalent to [method Node.reset_physics_interpolation] for Nodes.
</description>
</method>
<method name="fti_instance_set_transform">
<return type="void" />
<argument index="0" name="fti_instance" type="RID" />
<argument index="1" name="transform" type="Transform" />
<description>
Equivalent to [method instance_set_transform], but provides automatic physics interpolation.
</description>
</method>
<method name="get_render_info">
<return type="int" />
<argument index="0" name="info" type="int" enum="VisualServer.RenderInfo" />
Expand Down
48 changes: 38 additions & 10 deletions scene/3d/collision_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ void CollisionObject::_notification(int p_what) {
}

} break;
case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
if (debug_shapes_count > 0 && is_inside_tree()) {
for (Map<uint32_t, ShapeData>::Element *E = shapes.front(); E; E = E->next()) {
ShapeData &shapedata = E->get();
const ShapeData::ShapeBase *shapes = shapedata.shapes.ptr();
for (int i = 0; i < shapedata.shapes.size(); i++) {
if (shapes[i].fti_instance_debug_shape.is_valid()) {
VS::get_singleton()->fti_instance_reset(shapes[i].fti_instance_debug_shape);
}
}
}
}

} break;
}
}

Expand Down Expand Up @@ -237,14 +251,20 @@ void CollisionObject::_update_debug_shapes() {
for (int i = 0; i < shapedata.shapes.size(); i++) {
ShapeData::ShapeBase &s = shapes[i];
if (s.shape.is_null() || shapedata.disabled) {
if (s.debug_shape.is_valid()) {
VS::get_singleton()->free(s.debug_shape);
if (s.fti_instance_debug_shape.is_valid()) {
VS::get_singleton()->free(s.fti_instance_debug_shape);
s.fti_instance_debug_shape = RID();
s.debug_shape = RID();
--debug_shapes_count;
}
}
if (!s.debug_shape.is_valid()) {
s.debug_shape = RID_PRIME(VS::get_singleton()->instance_create());

bool needs_fti_reset = false;

if (!s.fti_instance_debug_shape.is_valid()) {
needs_fti_reset = true;
s.fti_instance_debug_shape = RID_PRIME(VS::get_singleton()->fti_instance_create());
s.debug_shape = VS::get_singleton()->fti_instance_get_instance(s.fti_instance_debug_shape);
VS::get_singleton()->instance_set_scenario(s.debug_shape, get_world()->get_scenario());

if (!s.shape->is_connected("changed", this, "_shape_changed")) {
Expand All @@ -256,8 +276,12 @@ void CollisionObject::_update_debug_shapes() {

Ref<Mesh> mesh = s.shape->get_debug_mesh();
VS::get_singleton()->instance_set_base(s.debug_shape, mesh->get_rid());
VS::get_singleton()->instance_set_transform(s.debug_shape, get_global_transform() * shapedata.xform);
VS::get_singleton()->fti_instance_set_transform(s.fti_instance_debug_shape, get_global_transform() * shapedata.xform);
VS::get_singleton()->instance_set_portal_mode(s.debug_shape, VisualServer::INSTANCE_PORTAL_MODE_GLOBAL);

if (needs_fti_reset) {
VS::get_singleton()->fti_instance_reset(s.fti_instance_debug_shape);
}
}
}
}
Expand All @@ -270,8 +294,9 @@ void CollisionObject::_clear_debug_shapes() {
ShapeData::ShapeBase *shapes = shapedata.shapes.ptrw();
for (int i = 0; i < shapedata.shapes.size(); i++) {
ShapeData::ShapeBase &s = shapes[i];
if (s.debug_shape.is_valid()) {
VS::get_singleton()->free(s.debug_shape);
if (s.fti_instance_debug_shape.is_valid()) {
VS::get_singleton()->free(s.fti_instance_debug_shape);
s.fti_instance_debug_shape = RID();
s.debug_shape = RID();
if (s.shape.is_valid() && s.shape->is_connected("changed", this, "_shape_changed")) {
s.shape->disconnect("changed", this, "_shape_changed");
Expand All @@ -290,7 +315,7 @@ void CollisionObject::_on_transform_changed() {
ShapeData &shapedata = E->get();
const ShapeData::ShapeBase *shapes = shapedata.shapes.ptr();
for (int i = 0; i < shapedata.shapes.size(); i++) {
VS::get_singleton()->instance_set_transform(shapes[i].debug_shape, debug_shape_old_transform * shapedata.xform);
VS::get_singleton()->fti_instance_set_transform(shapes[i].fti_instance_debug_shape, debug_shape_old_transform * shapedata.xform);
}
}
}
Expand Down Expand Up @@ -496,8 +521,11 @@ void CollisionObject::shape_owner_remove_shape(uint32_t p_owner, int p_shape) {
PhysicsServer::get_singleton()->body_remove_shape(rid, index_to_remove);
}

if (s.debug_shape.is_valid()) {
VS::get_singleton()->free(s.debug_shape);
if (s.fti_instance_debug_shape.is_valid()) {
VS::get_singleton()->free(s.fti_instance_debug_shape);
s.fti_instance_debug_shape = RID();
s.debug_shape = RID();

if (s.shape.is_valid() && s.shape->is_connected("changed", this, "_shape_changed")) {
s.shape->disconnect("changed", this, "_shape_changed");
}
Expand Down
1 change: 1 addition & 0 deletions scene/3d/collision_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CollisionObject : public Spatial {
ObjectID owner_id;
Transform xform;
struct ShapeBase {
RID fti_instance_debug_shape;
RID debug_shape;
Ref<Shape> shape;
int index;
Expand Down
183 changes: 183 additions & 0 deletions servers/visual/fti_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/**************************************************************************/
/* fti_helper.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. */
/**************************************************************************/

#include "fti_helper.h"

#include "core/engine.h"
#include "core/math/transform_interpolator.h"
#include "servers/visual/visual_server_globals.h"
#include "servers/visual/visual_server_scene.h"
#include "servers/visual_server.h"

FTIHelper::Handle FTIHelper::instance_create() {
Handle h;
Instance *i = _instances.request(h.id());
i->clear();
i->revision += 1;
h.set_revision(i->revision);

i->instance = RID_PRIME(VSG::scene->instance_create());
return h;
}

RID FTIHelper::instance_get_instance(Handle h_instance) {
FTIHelper::Instance *instance = get_instance(h_instance);
ERR_FAIL_NULL_V(instance, RID());
return instance->instance;
}

bool FTIHelper::instance_free(Handle h_instance) {
FTIHelper::Instance *instance = get_instance(h_instance);
ERR_FAIL_NULL_V(instance, false);

// Free the regular instance, as the lifetime of the regular instance
// is handled by the fti_instance automatically.
VisualServer::get_singleton()->free(instance->instance);
instance->instance = RID();

_instances.free(h_instance.id());

// Remove from all lists.
_instance_frame_list.erase_multiple_unordered(h_instance);
_instance_tick_list_curr->erase_multiple_unordered(h_instance);
_instance_tick_list_prev->erase_multiple_unordered(h_instance);
return true;
}

FTIHelper::Instance *FTIHelper::get_instance(Handle h_instance) {
ERR_FAIL_COND_V(h_instance.id() >= _instances.reserved_size(), nullptr);
Instance &i = _instances[h_instance.id()];
ERR_FAIL_COND_V(h_instance.revision() != i.revision, nullptr);
return &i;
}

void FTIHelper::instance_changed(Instance &r_instance, Handle h_instance) {
// Add to tick list
if (!r_instance.on_tick_list) {
r_instance.on_tick_list = true;
_instance_tick_list_curr->push_back(h_instance);
}

// Add to frame list.
if (!r_instance.on_frame_list) {
r_instance.on_frame_list = true;
_instance_frame_list.push_back(h_instance);
}
}

void FTIHelper::instance_set_transform(Handle h_instance, const Transform &p_xform) {
Instance *i = get_instance(h_instance);
ERR_FAIL_NULL(i);

if (_interpolation_enabled) {
i->curr = p_xform;
instance_changed(*i, h_instance);
} else {
// Pass through.
VisualServer::get_singleton()->instance_set_transform(i->instance, p_xform);
}
}

void FTIHelper::instance_reset_physics_interpolation(Handle h_instance) {
if (!_interpolation_enabled) {
return;
}
Instance *i = get_instance(h_instance);
ERR_FAIL_NULL(i);
i->pump();
instance_changed(*i, h_instance);
}

void FTIHelper::set_interpolation_enabled(bool p_enabled) {
if (p_enabled == _interpolation_enabled) {
return;
}

_interpolation_enabled = p_enabled;
}

void FTIHelper::tick_update() {
LocalVector<Handle> &curr = *_instance_tick_list_curr;
LocalVector<Handle> &prev = *_instance_tick_list_prev;

// First detect on the previous list but not on this tick list.
for (uint32_t n = 0; n < prev.size(); n++) {
const Handle &h = prev[n];
Instance *i = get_instance(h);
if (i) {
if (!i->on_tick_list) {
// Needs a reset so jittering will stop.
i->pump();

// Remove from interpolation list.
if (i->on_frame_list) {
i->on_frame_list = false;
_instance_frame_list.erase_unordered(h);
}
}
}
}

// Now pump all on the current list.
for (uint32_t n = 0; n < curr.size(); n++) {
const Handle &h = curr[n];
Instance *i = get_instance(h);
if (i) {
// Reset, needs to be marked each tick.
i->on_tick_list = false;
}

// Pump.
i->pump();
}

// Clear previous list and flip.
prev.clear();

SWAP(_instance_tick_list_curr, _instance_tick_list_prev);
}

void FTIHelper::frame_update() {
float f = Engine::get_singleton()->get_physics_interpolation_fraction();

Transform x;
VisualServer *vs = VisualServer::get_singleton();

for (uint32_t n = 0; n < _instance_frame_list.size(); n++) {
const Handle &h = _instance_frame_list[n];
Instance *i = get_instance(h);
if (i) {
TransformInterpolator::interpolate_transform(i->prev, i->curr, x, f);

// Send to server.
vs->instance_set_transform(i->instance, x);
}
}
}
Loading