Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Add Trail3D and Line3D class.

Qbieshayf2aee4e14274
diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt
index 489bd1a2842..c390d778e20 100644
--- a/COPYRIGHT.txt
+++ b/COPYRIGHT.txt
@@ -144,6 +144,12 @@ Copyright: 2001, Robert Penner
2007-2014, Juan Linietsky, Ariel Manzur
License: Expat
+Files: scene/3d/trail_3d.cpp
+ scene/3d/trail_3d.h
+Comment: CozyCubeGames' trail and lines plugin
+Copyright: 2024, CozyCubeGames
+License: Expat
+
Files: servers/audio/effects/audio_effect_pitch_shift.cpp
Copyright: 2014-present Godot Engine contributors
2007-2014 Juan Linietsky, Ariel Manzur
diff --git a/doc/classes/Line3D.xml b/doc/classes/Line3D.xml
new file mode 100644
index 00000000000..984a8bcdaf2
--- /dev/null
+++ b/doc/classes/Line3D.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<class name="Line3D" inherits="GeometryInstance3D" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
+ <brief_description>
+ A node to draw 3D lines in space.
+ [b]Note:[/b] This class currently has no functionality and only serves as a base class for [Trail3D].
+ </brief_description>
+ <description>
+ </description>
+ <tutorials>
+ </tutorials>
+ <constants>
+ <constant name="MESH_ALIGNMENT_LOCAL" value="0" enum="MeshAlignment">
+ Align the line normal to the local Y axis of the line node.
+ </constant>
+ <constant name="MESH_ALIGNMENT_BILLBOARD" value="1" enum="MeshAlignment">
+ Align the normal of each section of the Line3D to face the camera.
+ See also [member SpriteBase3D.billboard] and [enum BaseMaterial3D.BillboardMode]
+ </constant>
+ <constant name="MESH_ALIGNMENT_MAX" value="2" enum="MeshAlignment">
+ Represents the size of the [enum MeshAlignment] enum.
+ </constant>
+ <constant name="TILING_MODE_UNIT" value="0" enum="TilingMode">
+ Assign UVs from [code]0[/code] to [code]1[/code] across the length of the line.
+ </constant>
+ <constant name="TILING_MODE_LENGTH" value="1" enum="TilingMode">
+ Assign UVs from [code]0[/code] to current length, across the length of the line.
+ </constant>
+ <constant name="TILING_MAX" value="2" enum="TilingMode">
+ Represents the size of the [enum TilingMode] enum.
+ </constant>
+ <constant name="MATERIAL_MODE_MIX" value="0" enum="MaterialMode">
+ Assign a built-in mix material to the line.
+ </constant>
+ <constant name="MATERIAL_MODE_ADD" value="1" enum="MaterialMode">
+ Assign a built-in additive material to the line.
+ </constant>
+ <constant name="MATERIAL_MODE_CUSTOM" value="2" enum="MaterialMode">
+ Assign a custom material to the line.
+ </constant>
+ <constant name="MATERIAL_MODE_MAX" value="3" enum="MaterialMode">
+ Represents the size of the [enum MaterialMode] enum.
+ </constant>
+ </constants>
+</class>
diff --git a/doc/classes/Trail3D.xml b/doc/classes/Trail3D.xml
new file mode 100644
index 00000000000..524951af3cc
--- /dev/null
+++ b/doc/classes/Trail3D.xml
@@ -0,0 +1,116 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<class name="Trail3D" inherits="Line3D" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
+ <brief_description>
+ A node to draw lines behind moving objects.
+ </brief_description>
+ <description>
+ The [Trail3D] node draws a flat line in space, with an optional billboard mode.
+ [b]Note:[/b] This node is designed for visual effects. For solid lines, pipes, and tubes, use a [CSGPolygon3D] node in [constant CSGPolygon3D.MODE_PATH] mode.
+ </description>
+ <tutorials>
+ </tutorials>
+ <methods>
+ <method name="clear">
+ <return type="void" />
+ <description>
+ Clears the trail.
+ </description>
+ </method>
+ <method name="get_current_length" qualifiers="const">
+ <return type="float" />
+ <description>
+ Returns the current length of the trail.
+ </description>
+ </method>
+ </methods>
+ <members>
+ <member name="color" type="Color" setter="set_color" getter="get_color" default="Color(1, 1, 1, 1)">
+ The color of the trail.
+ [b]Note:[/b] Colors with values above [code]1.0[/code] (overbright) are not supported.
+ [b]Note:[/b] To have a visible effect on a [BaseMaterial3D], [member BaseMaterial3D.vertex_color_use_as_albedo] [i]must[/i] be [code]true[/code]. For a [ShaderMaterial], [code]ALBEDO *= COLOR.rgb;[/code] must be inserted in the shader's [code]fragment()[/code] function. Otherwise, [member color] will have no visible effect.
+ </member>
+ <member name="color_gradient" type="Gradient" setter="set_color_gradient" getter="get_color_gradient">
+ The color gradient over the trail length.
+ [b]Note:[/b] Colors with values above [code]1.0[/code] (overbright) are not supported.
+ [b]Note:[/b] To have a visible effect on a [BaseMaterial3D], [member BaseMaterial3D.vertex_color_use_as_albedo] [i]must[/i] be [code]true[/code]. For a [ShaderMaterial], [code]ALBEDO *= COLOR.rgb;[/code] must be inserted in the shader's [code]fragment()[/code] function. Otherwise, [member color] will have no visible effect.
+ </member>
+ <member name="emitting" type="bool" setter="set_emitting" getter="is_emitting" default="true">
+ If [code]true[/code], the trail will generate new points from its head.
+ [b]Note:[/b] Only one active trail is supported. Setting this property to [code]true[/code] will cause the existing trail mesh to be cleared.
+ </member>
+ <member name="lifetime" type="float" setter="set_lifetime" getter="get_lifetime" default="0.2">
+ The lifetime of the trail when [member limit_mode] is [constant LIMIT_MODE_LIFETIME].
+ </member>
+ <member name="limit_mode" type="int" setter="set_limit_mode" getter="get_limit_mode" enum="Trail3D.LimitMode" default="0">
+ Whether to limit the trail by lifetime or length.
+ </member>
+ <member name="material" type="ShaderMaterial" setter="set_material" getter="get_material">
+ The material to assign to the trail mesh when [member material_mode] is [constant Line3D.MATERIAL_MODE_CUSTOM].
+ [b]Tip:[/b] To preserve billboarding, use the following shader as a base:
+ [codeblock]
+ shader_type spatial;
+ render_mode blend_mix, depth_draw_never, unshaded, skip_vertex_transform, cull_disabled;
+
+ void vertex() {
+ // Billboarding code for trails
+ // CUSTOM0 contains the tangent of the trail and the width.
+ if (dot(CUSTOM0.xyz, CUSTOM0.xyz) &gt; 0.0) {
+ vec3 p = (inverse(MODELVIEW_MATRIX) * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
+ p = VERTEX - p;
+ vec3 t = CUSTOM0.xyz;
+ vec3 binormal = normalize(cross(p, t));
+ vec3 normal = normalize(cross(t, binormal));
+ vec3 tangent = normalize(cross(binormal, normal));
+ VERTEX += CUSTOM0.w * binormal;
+ NORMAL = normal;
+ TANGENT = tangent;
+ BINORMAL = binormal;
+ }
+ }
+
+ void fragment() {
+ ALBEDO = COLOR.rgb;
+ ALPHA = COLOR.a;
+ }
+ [/codeblock]
+ </member>
+ <member name="material_mode" type="int" setter="set_material_mode" getter="get_material_mode" enum="Line3D.MaterialMode" default="0">
+ The material mode for the trail.
+ </member>
+ <member name="max_length" type="float" setter="set_max_length" getter="get_max_length">
+ The maximum length of the trail when [member limit_mode] is [constant LIMIT_MODE_MAX_LENGTH].
+ </member>
+ <member name="mesh_alignment" type="int" setter="set_mesh_alignment" getter="get_mesh_alignment" enum="Line3D.MeshAlignment" default="1">
+ Alignment of the trail. Possible options are [constant Line3D.MESH_ALIGNMENT_LOCAL] for a 3D line, and [constant Line3D.MESH_ALIGNMENT_BILLBOARD] for a billboarded line.
+ </member>
+ <member name="min_section_length" type="float" setter="set_min_section_length" getter="get_min_section_length" default="0.2">
+ Minimum required distance before adding a new section.
+ </member>
+ <member name="pin_uv" type="bool" setter="set_pin_uv" getter="get_pin_uv" default="false">
+ If [code]true[/code], pins the UVs to their positions in space.
+ </member>
+ <member name="tiling_mode" type="int" setter="set_tiling_mode" getter="get_tiling_mode" enum="Line3D.TilingMode" default="1">
+ How to tile the UVs across the length of the trail.
+ </member>
+ <member name="tiling_multiplier" type="float" setter="set_tiling_multiplier" getter="get_tiling_multiplier" default="1.0">
+ UV multiplier across the length of the trail.
+ </member>
+ <member name="width" type="float" setter="set_width" getter="get_width" default="0.3">
+ Width of the trail.
+ </member>
+ <member name="width_curve" type="Curve" setter="set_width_curve" getter="get_width_curve">
+ Width across the length of the trail.
+ </member>
+ </members>
+ <constants>
+ <constant name="LIMIT_MODE_LIFETIME" value="0" enum="LimitMode">
+ Limit the trail length in time.
+ </constant>
+ <constant name="LIMIT_MODE_MAX_LENGTH" value="1" enum="LimitMode">
+ Limit the trail length in size.
+ </constant>
+ <constant name="LIMIT_MODE_MAX" value="2" enum="LimitMode">
+ Represents the size of the [enum LimitMode] enum.
+ </constant>
+ </constants>
+</class>
diff --git a/editor/icons/Trail3D.svg b/editor/icons/Trail3D.svg
new file mode 100644
index 00000000000..ba63f3989a1
--- /dev/null
+++ b/editor/icons/Trail3D.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="#fc7f7f" d="M 4.4785156,1.015625 C 2.5757418,1.0156727 1.0332508,2.5581637 1.0332031,4.4609375 1.0332508,6.3637113 2.5757418,7.9062023 4.4785156,7.90625 6.3812894,7.9062023 7.9237804,6.3637113 7.9238281,4.4609375 7.9237804,2.5581637 6.3812894,1.0156727 4.4785156,1.015625 Z m 4.0812712,0.1191406 c 1.0275564,0.7976278 1.6288122,2.0253713 1.6289062,3.3261719 -0.0017,1.234585 -0.5450099,2.4062522 -1.4863281,3.2050781 1.0797576,-0.00317 2.4720821,0.5534307 2.4204571,1.5014725 -0.174166,3.1984429 -6.162629,3.9368779 -8.6677439,4.1489179 2.0047826,0.47939 6.9122045,0.948171 9.6239939,-0.785619 C 13.871693,11.38467 15.324347,8.8832252 15.220401,6.9741937 15.100827,4.7781388 13.826066,1.2334116 8.5597868,1.1347656 Z"/></svg>
diff --git a/modules/mono/editor/bindings_generator.cpp b/modules/mono/editor/bindings_generator.cpp
index 40b761738cd..716649a4e91 100644
--- a/modules/mono/editor/bindings_generator.cpp
+++ b/modules/mono/editor/bindings_generator.cpp
@@ -153,7 +153,11 @@ const Vector<String> prop_allowed_inherited_member_hiding = {
// This avoids hiding the enum when the property is declared in a derived class,
// and the need for the 'new' keyword. It can also be used to avoid breaking compat
// later if a new member is added with the same name as the enum.
-const Vector<String> enums_with_forced_suffix = {};
+const Vector<String> enums_with_forced_suffix = {
+ "Line3D.MaterialMode",
+ "Line3D.MeshAlignment",
+ "Line3D.TilingMode",
+};
void BindingsGenerator::TypeInterface::postsetup_enum_type(BindingsGenerator::TypeInterface &r_enum_itype) {
// C interface for enums is the same as that of 'uint32_t'. Remember to apply
diff --git a/scene/3d/line_3d.cpp b/scene/3d/line_3d.cpp
new file mode 100644
index 00000000000..70c92028bc9
--- /dev/null
+++ b/scene/3d/line_3d.cpp
@@ -0,0 +1,54 @@
+/**************************************************************************/
+/* line_3d.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 "line_3d.h"
+
+#include "core/object/class_db.h"
+
+PackedStringArray Line3D::get_configuration_warnings() const {
+ PackedStringArray warnings = VisualInstance3D::get_configuration_warnings();
+ EXPERIMENTAL_NODE_WARNING;
+ return warnings;
+}
+
+void Line3D::_bind_methods() {
+ BIND_ENUM_CONSTANT(MESH_ALIGNMENT_LOCAL);
+ BIND_ENUM_CONSTANT(MESH_ALIGNMENT_BILLBOARD);
+ BIND_ENUM_CONSTANT(MESH_ALIGNMENT_MAX);
+
+ BIND_ENUM_CONSTANT(TILING_MODE_UNIT);
+ BIND_ENUM_CONSTANT(TILING_MODE_LENGTH);
+ BIND_ENUM_CONSTANT(TILING_MAX);
+
+ BIND_ENUM_CONSTANT(MATERIAL_MODE_MIX);
+ BIND_ENUM_CONSTANT(MATERIAL_MODE_ADD);
+ BIND_ENUM_CONSTANT(MATERIAL_MODE_CUSTOM);
+ BIND_ENUM_CONSTANT(MATERIAL_MODE_MAX);
+}
diff --git a/scene/3d/line_3d.h b/scene/3d/line_3d.h
new file mode 100644
index 00000000000..134839d3919
--- /dev/null
+++ b/scene/3d/line_3d.h
@@ -0,0 +1,66 @@
+/**************************************************************************/
+/* line_3d.h */
+/**************************************************************************/
+/* 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. */
+/**************************************************************************/
+
+#pragma once
+
+#include "scene/3d/visual_instance_3d.h"
+
+class Line3D : public GeometryInstance3D {
+ GDCLASS(Line3D, GeometryInstance3D);
+
+protected:
+ static void _bind_methods();
+
+public:
+ enum MeshAlignment {
+ MESH_ALIGNMENT_LOCAL,
+ MESH_ALIGNMENT_BILLBOARD,
+ MESH_ALIGNMENT_MAX,
+ };
+
+ enum TilingMode {
+ TILING_MODE_UNIT,
+ TILING_MODE_LENGTH,
+ TILING_MAX,
+ };
+
+ enum MaterialMode {
+ MATERIAL_MODE_MIX,
+ MATERIAL_MODE_ADD,
+ MATERIAL_MODE_CUSTOM,
+ MATERIAL_MODE_MAX,
+ };
+
+ PackedStringArray get_configuration_warnings() const override;
+};
+
+VARIANT_ENUM_CAST(Line3D::TilingMode)
+VARIANT_ENUM_CAST(Line3D::MeshAlignment)
+VARIANT_ENUM_CAST(Line3D::MaterialMode)
diff --git a/scene/3d/trail_3d.cpp b/scene/3d/trail_3d.cpp
new file mode 100644
index 00000000000..db28e38567e
--- /dev/null
+++ b/scene/3d/trail_3d.cpp
@@ -0,0 +1,985 @@
+/**************************************************************************/
+/* trail_3d.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 "trail_3d.h"
+
+#include "core/object/callable_mp.h"
+#include "core/object/class_db.h"
+#include "scene/resources/material.h"
+#include "servers/rendering/rendering_server.h"
+
+// Points calculation, line construction and shaders are taken
+// and adapted from CozyCubeGames.
+
+/**************************************************************************/
+/* Copyright (c) 2024 CozyCubeGames */
+/* */
+/* 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. */
+/**************************************************************************/
+
+void Trail3D::init_shaders() {
+ billboard_additive_shader.instantiate();
+ billboard_additive_shader->set_code(R"(
+shader_type spatial;
+render_mode blend_add, depth_draw_never, unshaded, cull_disabled;
+
+void vertex() {
+ // Billboarding code for trails
+ // CUSTOM0 contains the tangent of the trail and the width.
+ if (dot(CUSTOM0.xyz, CUSTOM0.xyz) > 0.0) {
+ vec3 p = (inverse(MODELVIEW_MATRIX) * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
+ p = VERTEX - p;
+ vec3 t = CUSTOM0.xyz;
+ vec3 binormal = normalize(cross(p, t));
+ vec3 normal = normalize(cross(t, binormal));
+ vec3 tangent = normalize(cross(binormal, normal));
+ VERTEX += CUSTOM0.w * binormal;
+ NORMAL = normal;
+ TANGENT = tangent;
+ BINORMAL = binormal;
+ }
+}
+
+void fragment() {
+ ALBEDO = COLOR.rgb;
+ ALPHA = COLOR.a;
+}
+)");
+ billboard_shader.instantiate();
+ billboard_shader->set_code(R"(
+shader_type spatial;
+render_mode blend_mix, depth_draw_never, unshaded, cull_disabled;
+
+void vertex() {
+ // Billboarding code for trails
+ // CUSTOM0 contains the tangent of the trail and the width
+ if (dot(CUSTOM0.xyz, CUSTOM0.xyz) > 0.0) {
+ vec3 p = (inverse(MODELVIEW_MATRIX) * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
+ p = VERTEX - p;
+ vec3 t = CUSTOM0.xyz;
+ vec3 binormal = normalize(cross(p, t));
+ vec3 normal = normalize(cross(t, binormal));
+ vec3 tangent = normalize(cross(binormal, normal));
+ VERTEX += CUSTOM0.w * binormal;
+ NORMAL = normal;
+ TANGENT = tangent;
+ BINORMAL = binormal;
+ }
+}
+
+void fragment() {
+ ALBEDO = COLOR.rgb;
+ ALPHA = COLOR.a;
+}
+)");
+ local_additive_shader.instantiate();
+ local_additive_shader->set_code(R"(
+shader_type spatial;
+render_mode blend_add, depth_draw_never, unshaded, cull_disabled;
+
+void fragment() {
+ ALBEDO = COLOR.rgb;
+ ALPHA = COLOR.a;
+}
+)");
+ local_shader.instantiate();
+ local_shader->set_code(R"(
+shader_type spatial;
+render_mode blend_mix, depth_draw_never, unshaded, cull_disabled;
+
+void fragment() {
+ ALBEDO = COLOR.rgb;
+ ALPHA = COLOR.a;
+}
+)");
+
+ billboard_additive_material.instantiate();
+ billboard_additive_material->set_shader(billboard_additive_shader);
+ billboard_material.instantiate();
+ billboard_material->set_shader(billboard_shader);
+ local_additive_material.instantiate();
+ local_additive_material->set_shader(local_additive_shader);
+ local_material.instantiate();
+ local_material->set_shader(local_shader);
+}
+
+void Trail3D::finish_shaders() {
+ billboard_additive_material.unref();
+ billboard_material.unref();
+ local_additive_material.unref();
+ local_material.unref();
+ billboard_shader.unref();
+ billboard_additive_shader.unref();
+ local_shader.unref();
+ local_additive_shader.unref();
+}
+
+void Trail3D::_notification(int p_what) {
+ switch (p_what) {
+ case NOTIFICATION_ENTER_TREE: {
+ clear();
+ RS::get_singleton()->connect("frame_pre_draw", callable_mp(this, &Trail3D::_process_trail));
+ RS::get_singleton()->instance_geometry_set_flag(get_instance(), RSE::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, true);
+ } break;
+ case NOTIFICATION_EXIT_TREE: {
+ if (RS::get_singleton()->is_connected("frame_pre_draw", callable_mp(this, &Trail3D::_process_trail))) {
+ RS::get_singleton()->disconnect("frame_pre_draw", callable_mp(this, &Trail3D::_process_trail));
+ }
+
+ } break;
+ case NOTIFICATION_TRANSFORM_CHANGED: {
+ if (limit_mode == LIMIT_MODE_MAX_LENGTH) {
+ _process_trail();
+ }
+ _transform_changed = true;
+ } break;
+ }
+}
+
+void Trail3D::set_width(float p_width) {
+ width = p_width;
+ _needs_rebuilding = true;
+}
+
+float Trail3D::get_width() const {
+ return width;
+}
+
+void Trail3D::set_emitting(bool p_emitting) {
+ if (emitting != p_emitting && p_emitting) {
+ clear();
+ }
+ emitting = p_emitting;
+}
+
+bool Trail3D::is_emitting() const {
+ return emitting;
+}
+
+void Trail3D::set_width_curve(Ref<Curve> p_width_curve) {
+ if (p_width_curve == width_curve) {
+ return;
+ }
+ if (width_curve.is_valid()) {
+ width_curve->disconnect_changed(callable_mp(this, &Trail3D::rebuild));
+ }
+ width_curve = p_width_curve;
+ if (width_curve.is_valid()) {
+ width_curve->connect_changed(callable_mp(this, &Trail3D::rebuild));
+ }
+ _needs_rebuilding = true;
+}
+
+Ref<Curve> Trail3D::get_width_curve() const {
+ return width_curve;
+}
+
+void Trail3D::set_color(const Color &p_color) {
+ if (p_color != color) {
+ _needs_rebuilding = true;
+ }
+ color = p_color;
+}
+
+Color Trail3D::get_color() const {
+ return color;
+}
+
+void Trail3D::set_color_gradient(Ref<Gradient> p_color_gradient) {
+ if (p_color_gradient == color_gradient) {
+ return;
+ }
+ if (color_gradient.is_valid()) {
+ color_gradient->disconnect_changed(callable_mp(this, &Trail3D::rebuild));
+ }
+ color_gradient = p_color_gradient;
+ if (color_gradient.is_valid()) {
+ color_gradient->connect_changed(callable_mp(this, &Trail3D::rebuild));
+ }
+ _needs_rebuilding = true;
+}
+
+Ref<Gradient> Trail3D::get_color_gradient() const {
+ return color_gradient;
+}
+
+void Trail3D::set_material_mode(MaterialMode p_material_mode) {
+ if (material_mode == p_material_mode) {
+ return;
+ }
+ if (material.is_null()) {
+ if (material_mode == MATERIAL_MODE_ADD) {
+ if (alignment == MESH_ALIGNMENT_LOCAL) {
+ material = local_additive_material->duplicate_deep();
+ } else {
+ material = billboard_additive_material->duplicate_deep();
+ }
+ } else if (material_mode == MATERIAL_MODE_MIX) {
+ if (alignment == MESH_ALIGNMENT_LOCAL) {
+ material = local_material->duplicate_deep();
+ } else {
+ material = billboard_material->duplicate_deep();
+ }
+ }
+ }
+ material_mode = p_material_mode;
+ notify_property_list_changed();
+ _ensure_material();
+}
+
+Trail3D::MaterialMode Trail3D::get_material_mode() const {
+ return material_mode;
+}
+
+void Trail3D::set_material(Ref<ShaderMaterial> p_material) {
+ if (material_mode != MATERIAL_MODE_CUSTOM) {
+ return;
+ }
+ material = p_material;
+ _ensure_material();
+}
+
+Ref<ShaderMaterial> Trail3D::get_material() const {
+ return material;
+}
+
+void Trail3D::set_mesh_alignment(MeshAlignment p_alignment) {
+ if (p_alignment != alignment) {
+ _needs_rebuilding = true;
+ alignment = p_alignment;
+ // We need to reallocate the custom vertex buffer
+ clear();
+ }
+}
+
+Trail3D::MeshAlignment Trail3D::get_mesh_alignment() const {
+ return alignment;
+}
+
+void Trail3D::set_tiling_mode(TilingMode p_tiling_mode) {
+ if (p_tiling_mode != tiling_mode) {
+ _needs_rebuilding = true;
+ }
+ tiling_mode = p_tiling_mode;
+ notify_property_list_changed();
+}
+
+Trail3D::TilingMode Trail3D::get_tiling_mode() const {
+ return tiling_mode;
+}
+
+void Trail3D::set_tiling_multiplier(float p_tiling_multiplier) {
+ if (p_tiling_multiplier != tiling_multiplier) {
+ _needs_rebuilding = true;
+ }
+ tiling_multiplier = p_tiling_multiplier;
+}
+
+float Trail3D::get_tiling_multiplier() const {
+ return tiling_multiplier;
+}
+
+void Trail3D::rebuild() {
+ _needs_rebuilding = true;
+ _do_rebuild();
+}
+
+void Trail3D::clear() {
+ points.clear();
+ normals.clear();
+ _times.clear();
+ tangents.clear();
+ _velocities.clear();
+ tiling_offset = 0.;
+ _last_vertex_count = 600;
+ _last_index_count = 0;
+ _init_clear_mesh();
+ rebuild();
+}
+
+// Trail3D
+
+void Trail3D::set_limit_mode(LimitMode p_limit_mode) {
+ if (limit_mode == p_limit_mode) {
+ return;
+ }
+ limit_mode = p_limit_mode;
+ if (p_limit_mode == LIMIT_MODE_LIFETIME) {
+ RS::get_singleton()->connect("frame_pre_draw", callable_mp(this, &Trail3D::_process_trail));
+ } else {
+ if (RS::get_singleton()->is_connected("frame_pre_draw", callable_mp(this, &Trail3D::_process_trail))) {
+ RS::get_singleton()->disconnect("frame_pre_draw", callable_mp(this, &Trail3D::_process_trail));
+ }
+ }
+ clear();
+ notify_property_list_changed();
+}
+
+Trail3D::LimitMode Trail3D::get_limit_mode() const {
+ return limit_mode;
+}
+
+void Trail3D::set_min_section_length(real_t p_min_section_length) {
+ if (p_min_section_length != min_section_length) {
+ _needs_rebuilding = true;
+ }
+ min_section_length = p_min_section_length;
+}
+
+real_t Trail3D::get_min_section_length() const {
+ return min_section_length;
+}
+
+void Trail3D::set_lifetime(real_t p_lifetime) {
+ lifetime = p_lifetime;
+}
+
+real_t Trail3D::get_lifetime() const {
+ return lifetime;
+}
+
+void Trail3D::set_max_length(real_t p_max_length) {
+ max_length = p_max_length;
+}
+
+real_t Trail3D::get_max_length() const {
+ return max_length;
+}
+
+void Trail3D::set_pin_uv(bool p_pin_uv) {
+ if (p_pin_uv != pin_uv) {
+ _needs_rebuilding = true;
+ }
+ pin_uv = p_pin_uv;
+}
+
+bool Trail3D::get_pin_uv() const {
+ return pin_uv;
+}
+
+real_t Trail3D::_calc_current_length() const {
+ real_t length = 0.0;
+ for (int i = 0; i < points.size() - 1; i++) {
+ length += points.get(i).distance_to(points.get(i + 1));
+ }
+
+ return length;
+}
+
+real_t Trail3D::get_current_length() const {
+ return _calc_current_length();
+}
+
+void Trail3D::_init_clear_mesh() {
+ if (_mesh.is_null()) {
+ _mesh.instantiate();
+ set_base(_mesh->get_rid());
+ }
+ Array arrays;
+
+ PackedVector3Array mesh_vertices;
+ PackedVector3Array mesh_normals;
+ PackedColorArray mesh_colors;
+ PackedVector2Array mesh_uvs;
+ PackedInt32Array mesh_indices;
+ PackedFloat32Array mesh_custom0;
+
+ mesh_vertices.resize(_last_vertex_count);
+ mesh_normals.resize(_last_vertex_count);
+ mesh_uvs.resize(_last_vertex_count);
+ mesh_colors.resize(_last_vertex_count);
+ mesh_indices.resize(_last_vertex_count * 4);
+
+ mesh_vertices.fill(Vector3(0.0, 0.0, 0.0));
+ mesh_normals.fill(Vector3(0.0, 0.0, 0.0));
+ mesh_uvs.fill(Vector2(0.0, 0.0));
+ mesh_colors.fill(Color(1.0, 1.0, 1.0, 0.0));
+ mesh_indices.fill(0);
+
+ arrays.resize(RSE::ARRAY_MAX);
+ arrays[RSE::ARRAY_VERTEX] = mesh_vertices;
+ arrays[RSE::ARRAY_NORMAL] = mesh_normals;
+ arrays[RSE::ARRAY_TEX_UV] = mesh_uvs;
+ arrays[RSE::ARRAY_COLOR] = mesh_colors;
+ arrays[RSE::ARRAY_INDEX] = mesh_indices;
+
+ if (alignment == MESH_ALIGNMENT_BILLBOARD) {
+ mesh_custom0.resize(_last_vertex_count * 4);
+ mesh_custom0.fill(0.);
+ arrays[RSE::ARRAY_CUSTOM0] = mesh_custom0;
+ }
+
+ RenderingServerTypes::SurfaceData sd;
+
+ Error err = RS::get_singleton()->mesh_create_surface_data_from_arrays(&sd, RSE::PRIMITIVE_TRIANGLES, arrays, Array(), Dictionary(), RSE::ARRAY_CUSTOM_RGBA_FLOAT << RSE::ARRAY_FORMAT_CUSTOM0_SHIFT);
+ ERR_FAIL_COND_MSG(err != OK, "Trail3D failed mesh initialization. Please open a bug report");
+
+ mesh_surface_format = sd.format;
+ vertex_buffer = sd.vertex_data;
+ attribute_buffer = sd.attribute_data;
+ index_buffer = sd.index_data;
+
+ // We need to have it calculate the offset info
+ RS::get_singleton()->mesh_surface_make_offsets_from_format(sd.format | RSE::ARRAY_CUSTOM_RGBA_FLOAT << RSE::ARRAY_FORMAT_CUSTOM0_SHIFT, sd.vertex_count, sd.index_count, mesh_surface_offsets, vertex_stride, normal_tangent_stride, attrib_stride, skin_stride);
+
+ _mesh->clear_surfaces();
+ _mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays, Array(), Dictionary(), RSE::ARRAY_CUSTOM_RGBA_FLOAT << RSE::ARRAY_FORMAT_CUSTOM0_SHIFT);
+
+ _ensure_material();
+}
+
+void Trail3D::_do_rebuild() {
+ if (!_needs_rebuilding || !is_inside_tree() || !is_ready()) {
+ return;
+ }
+
+ int points_count = points.size();
+
+ if (points_count < 2) {
+ return;
+ }
+
+ if (points_count * 3 > _last_vertex_count) {
+ while (points_count * 3 > _last_vertex_count) {
+ _last_vertex_count *= 2;
+ }
+ if (_last_vertex_count >= 65536) {
+ WARN_PRINT("Vertex count for trail tried to exceed max vertex size.");
+ }
+ _last_vertex_count = MIN(_last_vertex_count, 65536);
+
+ _init_clear_mesh();
+
+ points_count = MIN(points_count, _last_vertex_count / 3);
+ }
+
+ _needs_rebuilding = false;
+
+ uint8_t *index_write_buffer = index_buffer.ptrw();
+
+ for (int i = 0; i < points_count - 1; i++) {
+ int j = i * 12;
+ uint16_t k = (uint16_t)i * 3;
+
+ uint16_t idx = k;
+
+ *(uint16_t *)&index_write_buffer[j * 2] = idx;
+ idx = k + 3;
+ *(uint16_t *)&index_write_buffer[(j + 1) * 2] = idx;
+ idx = k + 1;
+ *(uint16_t *)&index_write_buffer[(j + 2) * 2] = idx;
+ idx = k + 1;
+ *(uint16_t *)&index_write_buffer[(j + 3) * 2] = idx;
+ idx = k + 3;
+ *(uint16_t *)&index_write_buffer[(j + 4) * 2] = idx;
+ idx = k + 4;
+ *(uint16_t *)&index_write_buffer[(j + 5) * 2] = idx;
+ idx = k + 1;
+ *(uint16_t *)&index_write_buffer[(j + 6) * 2] = idx;
+ idx = k + 4;
+ *(uint16_t *)&index_write_buffer[(j + 7) * 2] = idx;
+ idx = k + 2;
+ *(uint16_t *)&index_write_buffer[(j + 8) * 2] = idx;
+ idx = k + 2;
+ *(uint16_t *)&index_write_buffer[(j + 9) * 2] = idx;
+ idx = k + 4;
+ *(uint16_t *)&index_write_buffer[(j + 10) * 2] = idx;
+ idx = k + 5;
+ *(uint16_t *)&index_write_buffer[(j + 11) * 2] = idx;
+ }
+
+ Transform3D inv_global_tf = get_global_transform_interpolated().inverse();
+
+ real_t length = _calc_current_length();
+
+ real_t dist = 0.0;
+ Vector3 min_bounds = Vector3();
+ Vector3 max_bounds = Vector3();
+ Vector3 width_bounds = Vector3(width, width, width);
+
+ uint8_t *write_vert = vertex_buffer.ptrw();
+ uint8_t *write_attrib = attribute_buffer.ptrw();
+
+ Color c = color.srgb_to_linear();
+ for (int i = 0; i < points_count; i++) {
+ int j0 = i * 3;
+ int j1 = j0 + 1;
+ int j2 = j0 + 2;
+
+ Vector3 p = points[i];
+
+ if (i > 0) {
+ dist += points[i - 1].distance_to(p);
+ }
+
+ real_t ratio = length > 0.0 ? dist / length : 0.0;
+ real_t length_uv = 0;
+
+ switch (tiling_mode) {
+ case TILING_MODE_LENGTH: {
+ if (pin_uv) {
+ length_uv = (dist + tiling_offset) * tiling_multiplier;
+ } else {
+ length_uv = dist * tiling_multiplier;
+ }
+ } break;
+ case TILING_MODE_UNIT: {
+ length_uv = ratio * tiling_multiplier;
+ } break;
+ default: {
+ }
+ }
+
+ real_t half_width = width * 0.5;
+
+ if (width_curve.is_valid()) {
+ half_width *= width_curve->sample(ratio);
+ }
+
+ Vector3 tangent = tangents[i];
+
+ p = inv_global_tf.xform(p);
+ tangent = inv_global_tf.basis.xform(tangent);
+ min_bounds = min_bounds.min(p - width_bounds);
+ max_bounds = max_bounds.max(p + width_bounds);
+
+ Vector3 normal = normals[i];
+ normal = inv_global_tf.basis.xform(normal);
+ uint32_t vnormal = _encode_normal(normal);
+
+ Vector3 curve_binormal = normal.cross(tangent);
+ // we don't actually add the side offset if we're billboarding
+ real_t is_billboard = alignment == MESH_ALIGNMENT_BILLBOARD ? 0.0 : 1.0;
+ _encode_vertex(p + half_width * curve_binormal * is_billboard, j2, write_vert);
+ _encode_vertex(p, j1, write_vert);
+ _encode_vertex(p - half_width * curve_binormal * is_billboard, j0, write_vert);
+
+ _write_normal(vnormal, j2, write_vert);
+ _write_normal(vnormal, j1, write_vert);
+ _write_normal(vnormal, j0, write_vert);
+
+ _encode_uv(Vector2(length_uv, 0), j2, write_attrib);
+ _encode_uv(Vector2(length_uv, 0.5), j1, write_attrib);
+ _encode_uv(Vector2(length_uv, 1), j0, write_attrib);
+
+ if (alignment == MESH_ALIGNMENT_BILLBOARD) {
+ Vector4 custom = Vector4();
+ custom.x = tangent.x;
+ custom.y = tangent.y;
+ custom.z = tangent.z;
+
+ custom.w = half_width;
+ _write_custom(custom, j2, write_attrib);
+ custom.w = 0.;
+ _write_custom(custom, j1, write_attrib);
+ custom.w = -half_width;
+ _write_custom(custom, j0, write_attrib);
+ }
+
+ Color local_c = c;
+ if (color_gradient.is_valid()) {
+ local_c *= color_gradient->get_color_at_offset(ratio).srgb_to_linear();
+ }
+
+ uint8_t col[4];
+ _encode_color(local_c, col);
+ _write_color(col, j2, write_attrib);
+ _write_color(col, j1, write_attrib);
+ _write_color(col, j0, write_attrib);
+ }
+
+ int current_idx_count = (points_count - 1) * 12;
+ if (current_idx_count >= _last_index_count) {
+ memset(&index_write_buffer[current_idx_count * 2], 0, (_last_vertex_count * 4 - ((points_count - 1) * 12)) * 2);
+ } else {
+ memset(&index_write_buffer[current_idx_count * 2], 0, (_last_index_count - current_idx_count) * 2);
+ }
+ _last_index_count = current_idx_count;
+
+ RID rid = _mesh->get_rid();
+
+ RS::get_singleton()->mesh_surface_update_vertex_region(rid, 0, 0, vertex_buffer);
+ RS::get_singleton()->mesh_surface_update_attribute_region(rid, 0, 0, attribute_buffer);
+ RS::get_singleton()->mesh_surface_update_index_region(rid, 0, 0, index_buffer);
+
+ set_custom_aabb(AABB(min_bounds, (max_bounds - min_bounds)));
+}
+
+void Trail3D::_process_trail() {
+ if (!is_inside_tree() || !is_ready()) {
+ return;
+ }
+
+ Transform3D tf = get_global_transform_interpolated();
+ Vector3 pos = tf.origin;
+
+ if (!_transform_changed && points.size() == 2 && pos.distance_squared_to(points[1]) < CMP_EPSILON) {
+ return;
+ }
+
+ Vector3 up = tf.basis.get_column(1);
+ real_t delta = get_process_delta_time();
+ _time += delta;
+ real_t head_velocity = (get_global_position() - _previous_position).length() / MAX(delta, CMP_EPSILON);
+
+ if (points.size() < 2) {
+ while (points.size() < 2) {
+ points.insert(0, pos);
+ normals.insert(0, up);
+ tangents.insert(0, Vector3(0.0, 0.0, 1.0));
+ if (limit_mode == LIMIT_MODE_LIFETIME) {
+ _times.insert(0, _time);
+ _velocities.insert(0, head_velocity);
+ }
+ }
+ } else {
+ if (emitting) {
+ points.write[0] = pos;
+ normals.write[0] = up;
+ if (pos.distance_squared_to(points[1]) > CMP_EPSILON) {
+ real_t len = (pos - points[1]).length();
+ Vector3 t = (pos - points[1]) / len;
+ tangents.write[0] = t;
+ if (points.size() == 2) {
+ tangents.write[1] = t;
+ if (_previous_position.distance_squared_to(points[1]) <= CMP_EPSILON && limit_mode == LIMIT_MODE_LIFETIME) {
+ _times.write[1] = _time - delta;
+ _velocities.write[1] = head_velocity;
+ }
+ }
+ } else {
+ tangents.write[1] = tangents[0];
+ normals.write[1] = normals[0];
+ points.write[1] = points[0];
+ if (limit_mode == LIMIT_MODE_LIFETIME) {
+ _times.write[1] = _time;
+ }
+ }
+ if (limit_mode == LIMIT_MODE_LIFETIME) {
+ _times.write[0] = _time;
+ _velocities.write[0] = head_velocity;
+ }
+ }
+ }
+
+ Vector3 leading = points.get(1);
+ Vector3 from_leading = pos - leading;
+ real_t dist_from_leading = from_leading.length();
+
+ if (pin_uv) {
+ tiling_offset = -_last_pinned_u - min_section_length;
+ } else {
+ _last_pinned_u = -tiling_offset - min_section_length;
+ }
+
+ switch (limit_mode) {
+ case LIMIT_MODE_MAX_LENGTH:
+ if (dist_from_leading > min_section_length && emitting) {
+ tangents.insert(1, (pos - points[1]).normalized());
+ points.insert(1, pos);
+ normals.insert(1, up);
+ _last_pinned_u += dist_from_leading;
+ if (points.size() > 3) {
+ tangents.write[2] = (points[1] - points[3]).normalized();
+ }
+ }
+ break;
+ case LIMIT_MODE_LIFETIME:
+ if (dist_from_leading > min_section_length && emitting) {
+ tangents.insert(1, (pos - points[1]).normalized());
+ points.insert(1, pos);
+ normals.insert(1, up);
+ _times.insert(1, _time);
+ _last_pinned_u += dist_from_leading;
+ _velocities.insert(1, (points[1] - points[2]).length() / (_times[1] - _times[2]));
+ if (points.size() > 3) {
+ tangents.write[2] = (points[1] - points[3]).normalized();
+ }
+ }
+ break;
+ default:
+ break;
+ }
+
+ if (pin_uv) {
+ tiling_offset -= dist_from_leading;
+ }
+
+ if (limit_mode == LIMIT_MODE_MAX_LENGTH) {
+ int last_index = points.size() - 1;
+ real_t total_length = 0.0;
+ for (int i = 0; i < last_index; i++) {
+ total_length += points[i].distance_to(points[i + 1]);
+ }
+
+ real_t extra_length = total_length - max_length;
+
+ while (extra_length > 0 && points.size() > 1) {
+ Vector3 last_point = points[last_index];
+ Vector3 second_last_point = points[last_index - 1];
+ real_t last_section_length = last_point.distance_to(second_last_point);
+ if (last_section_length > extra_length) {
+ real_t shortened_section_length = last_section_length - extra_length;
+ points.write[last_index] = second_last_point + (last_point - second_last_point) * (shortened_section_length / last_section_length);
+ } else {
+ points.remove_at(last_index);
+ normals.remove_at(last_index);
+ tangents.remove_at(last_index);
+ last_index -= 1;
+ }
+ extra_length -= last_section_length;
+ }
+
+ } else if (limit_mode == LIMIT_MODE_LIFETIME) {
+ // ATTEMPTS: 11
+ // Add +1 to this counter for every failed attempt to improve this code
+ int last_index = _times.size() - 1;
+ real_t second_last_time = _times.get(last_index - 1);
+ while ((second_last_time < _time - lifetime) && points.size() > 2) {
+ points.remove_at(last_index);
+ normals.remove_at(last_index);
+ tangents.remove_at(last_index);
+ _times.remove_at(last_index);
+ _velocities.remove_at(last_index);
+ last_index -= 1;
+ second_last_time = _times.get(last_index - 1);
+ }
+
+ real_t last_time = _times[last_index];
+ if (last_time <= _time - lifetime && points.size() > 2) {
+ Vector3 dir = points[last_index - 1] - points[last_index];
+ real_t distance = dir.length();
+ real_t speed_distance = _velocities[last_index] * delta;
+
+ if (distance > 0.) {
+ points.write[last_index] += dir / distance * MIN(distance, speed_distance);
+ }
+ if (speed_distance > distance) {
+ points.remove_at(last_index);
+ normals.remove_at(last_index);
+ tangents.remove_at(last_index);
+ _times.remove_at(last_index);
+ _velocities.remove_at(last_index);
+ }
+ }
+ {
+ // Handle this special case, since point zero isn't a "real" pinned point
+ Vector3 dir = points[0] - points[1];
+ real_t distance = dir.length();
+ if (points.size() == 2 && _times[0] - _times[1] > lifetime) {
+ if (distance > CMP_EPSILON) {
+ real_t speed_distance = _velocities[1] * delta;
+ points.write[1] += dir / distance * MIN(distance, speed_distance);
+ } else {
+ points.write[1] = points[0];
+ }
+ }
+ }
+ }
+ _previous_position = get_global_transform().get_origin();
+ _transform_changed = false;
+ rebuild();
+}
+
+void Trail3D::_validate_property(PropertyInfo &p_property) const {
+ if (p_property.name == "material" && material_mode != MATERIAL_MODE_CUSTOM) {
+ p_property.usage = PROPERTY_USAGE_NONE;
+ } else if (p_property.name == "lifetime" && limit_mode == LIMIT_MODE_MAX_LENGTH) {
+ p_property.usage = PROPERTY_USAGE_NONE;
+ } else if (p_property.name == "max_length" && limit_mode == LIMIT_MODE_LIFETIME) {
+ p_property.usage = PROPERTY_USAGE_NONE;
+ }
+}
+
+void Trail3D::_ensure_material() {
+ if (!_mesh.is_valid()) {
+ return;
+ }
+ switch (material_mode) {
+ case MATERIAL_MODE_ADD: {
+ if (alignment == MESH_ALIGNMENT_BILLBOARD) {
+ _mesh->surface_set_material(0, billboard_additive_material);
+ } else {
+ _mesh->surface_set_material(0, local_additive_material);
+ }
+
+ } break;
+ case MATERIAL_MODE_MIX: {
+ if (alignment == MESH_ALIGNMENT_BILLBOARD) {
+ _mesh->surface_set_material(0, billboard_material);
+ } else {
+ _mesh->surface_set_material(0, local_material);
+ }
+ } break;
+ case MATERIAL_MODE_CUSTOM: {
+ _mesh->surface_set_material(0, material);
+ } break;
+ case MATERIAL_MODE_MAX: {
+ } break;
+ }
+}
+
+PackedStringArray Trail3D::get_configuration_warnings() const {
+ PackedStringArray warnings = Line3D::get_configuration_warnings();
+ return warnings;
+}
+
+void Trail3D::_bind_methods() {
+ ClassDB::bind_method(D_METHOD("set_width", "width"), &Trail3D::set_width);
+ ClassDB::bind_method(D_METHOD("get_width"), &Trail3D::get_width);
+ ClassDB::bind_method(D_METHOD("set_width_curve", "curve"), &Trail3D::set_width_curve);
+ ClassDB::bind_method(D_METHOD("get_width_curve"), &Trail3D::get_width_curve);
+ ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &Trail3D::set_emitting);
+ ClassDB::bind_method(D_METHOD("is_emitting"), &Trail3D::is_emitting);
+
+ ClassDB::bind_method(D_METHOD("set_color", "color"), &Trail3D::set_color);
+ ClassDB::bind_method(D_METHOD("get_color"), &Trail3D::get_color);
+ ClassDB::bind_method(D_METHOD("set_color_gradient", "gradient"), &Trail3D::set_color_gradient);
+ ClassDB::bind_method(D_METHOD("get_color_gradient"), &Trail3D::get_color_gradient);
+
+ ClassDB::bind_method(D_METHOD("set_material_mode", "material_mode"), &Trail3D::set_material_mode);
+ ClassDB::bind_method(D_METHOD("get_material_mode"), &Trail3D::get_material_mode);
+ ClassDB::bind_method(D_METHOD("set_material", "material"), &Trail3D::set_material);
+ ClassDB::bind_method(D_METHOD("get_material"), &Trail3D::get_material);
+
+ ClassDB::bind_method(D_METHOD("set_mesh_alignment", "alignment"), &Trail3D::set_mesh_alignment);
+ ClassDB::bind_method(D_METHOD("get_mesh_alignment"), &Trail3D::get_mesh_alignment);
+
+ ClassDB::bind_method(D_METHOD("set_tiling_multiplier", "tiling_multiplier"), &Trail3D::set_tiling_multiplier);
+ ClassDB::bind_method(D_METHOD("get_tiling_multiplier"), &Trail3D::get_tiling_multiplier);
+ ClassDB::bind_method(D_METHOD("set_tiling_mode", "tiling_mode"), &Trail3D::set_tiling_mode);
+ ClassDB::bind_method(D_METHOD("get_tiling_mode"), &Trail3D::get_tiling_mode);
+
+ ClassDB::bind_method(D_METHOD("clear"), &Trail3D::clear);
+
+ // Beam and Trail
+ ClassDB::bind_method(D_METHOD("set_min_section_length", "min_section_length"), &Trail3D::set_min_section_length);
+ ClassDB::bind_method(D_METHOD("get_min_section_length"), &Trail3D::get_min_section_length);
+
+ // Trail
+
+ ClassDB::bind_method(D_METHOD("set_limit_mode", "limit_mode"), &Trail3D::set_limit_mode);
+ ClassDB::bind_method(D_METHOD("get_limit_mode"), &Trail3D::get_limit_mode);
+
+ ClassDB::bind_method(D_METHOD("set_lifetime", "lifetime"), &Trail3D::set_lifetime);
+ ClassDB::bind_method(D_METHOD("get_lifetime"), &Trail3D::get_lifetime);
+
+ ClassDB::bind_method(D_METHOD("set_max_length", "max_length"), &Trail3D::set_max_length);
+ ClassDB::bind_method(D_METHOD("get_max_length"), &Trail3D::get_max_length);
+
+ ClassDB::bind_method(D_METHOD("set_pin_uv", "pin_uv"), &Trail3D::set_pin_uv);
+ ClassDB::bind_method(D_METHOD("get_pin_uv"), &Trail3D::get_pin_uv);
+
+ ClassDB::bind_method(D_METHOD("get_current_length"), &Trail3D::get_current_length);
+
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "limit_mode", PROPERTY_HINT_ENUM, "Time,Length"), "set_limit_mode", "get_limit_mode");
+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.0,10.0,0.001,or_greater"), "set_lifetime", "get_lifetime");
+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_length", PROPERTY_HINT_RANGE, "0.0,10.0,0.01,or_greater"), "set_max_length", "get_max_length");
+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width", PROPERTY_HINT_RANGE, "0.0,10,0.1,or_greater"), "set_width", "get_width");
+ ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "width_curve", PROPERTY_HINT_RESOURCE_TYPE, Curve::get_class_static()), "set_width_curve", "get_width_curve");
+ ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color", PROPERTY_HINT_NONE), "set_color", "get_color");
+ ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_gradient", PROPERTY_HINT_RESOURCE_TYPE, Gradient::get_class_static()), "set_color_gradient", "get_color_gradient");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "mesh_alignment", PROPERTY_HINT_ENUM, "Local,Billboard"), "set_mesh_alignment", "get_mesh_alignment");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "material_mode", PROPERTY_HINT_ENUM, "Default,Default Additive,Custom"), "set_material_mode", "get_material_mode");
+ ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, ShaderMaterial::get_class_static()), "set_material", "get_material");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "tiling_mode", PROPERTY_HINT_ENUM, "Unit,Length"), "set_tiling_mode", "get_tiling_mode");
+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "tiling_multiplier"), "set_tiling_multiplier", "get_tiling_multiplier");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pin_uv"), "set_pin_uv", "get_pin_uv");
+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_section_length", PROPERTY_HINT_RANGE, "0.05,1.0,0.01"), "set_min_section_length", "get_min_section_length");
+
+ BIND_ENUM_CONSTANT(LIMIT_MODE_LIFETIME);
+ BIND_ENUM_CONSTANT(LIMIT_MODE_MAX_LENGTH);
+ BIND_ENUM_CONSTANT(LIMIT_MODE_MAX);
+}
+
+Trail3D::Trail3D() {
+ _init_clear_mesh();
+}
+
+void Trail3D::_encode_vertex(const Vector3 &p_vertex, int p_index, uint8_t *r_buffer) {
+ DEV_ASSERT((p_index * vertex_stride + mesh_surface_offsets[RSE::ARRAY_VERTEX] + 12) <= vertex_buffer.size());
+ float v_vertex[3] = { (float)p_vertex.x, (float)p_vertex.y, (float)p_vertex.z };
+ memcpy(&r_buffer[p_index * vertex_stride + mesh_surface_offsets[RSE::ARRAY_VERTEX]], &v_vertex, sizeof(float) * 3);
+}
+
+void Trail3D::_write_normal(const uint32_t p_normal, int p_index, uint8_t *r_buffer) {
+ DEV_ASSERT((p_index * normal_tangent_stride + mesh_surface_offsets[RSE::ARRAY_NORMAL] + 4) <= vertex_buffer.size());
+ memcpy(&r_buffer[p_index * normal_tangent_stride + mesh_surface_offsets[RSE::ARRAY_NORMAL]], &p_normal, 4);
+}
+
+void Trail3D::_write_custom(const Vector4 p_custom, int p_index, uint8_t *r_buffer) {
+ DEV_ASSERT((p_index * attrib_stride + mesh_surface_offsets[RSE::ARRAY_CUSTOM0] + 16) <= attribute_buffer.size());
+ float v_custom[4] = { (float)p_custom.x, (float)p_custom.y, (float)p_custom.z, (float)p_custom.w };
+ memcpy(&r_buffer[p_index * attrib_stride + mesh_surface_offsets[RSE::ARRAY_CUSTOM0]], &v_custom, sizeof(float) * 4);
+}
+
+uint32_t Trail3D::_encode_normal(const Vector3 &p_normal) {
+ uint32_t v_normal = 0;
+ Vector2 res = p_normal.octahedron_encode();
+ v_normal |= (uint16_t)CLAMP(res.x * 65535, 0, 65535);
+ v_normal |= (uint16_t)CLAMP(res.y * 65535, 0, 65535) << 16;
+ return v_normal;
+}
+
+void Trail3D::_encode_uv(const Vector2 &p_uv, int p_index, uint8_t *r_buffer) {
+ DEV_ASSERT((p_index * attrib_stride + mesh_surface_offsets[RSE::ARRAY_TEX_UV] + 8) <= attribute_buffer.size());
+ float v_uv[2] = { (float)p_uv.x, (float)p_uv.y };
+ memcpy(&r_buffer[p_index * attrib_stride + mesh_surface_offsets[RSE::ARRAY_TEX_UV]], v_uv, 8);
+}
+
+void Trail3D::_encode_color(const Color &p_color, uint8_t *r_color) {
+ r_color[0] = uint8_t(CLAMP(p_color.r * 255.0f, 0.0f, 255.0f));
+ r_color[1] = uint8_t(CLAMP(p_color.g * 255.0f, 0.0f, 255.0f));
+ r_color[2] = uint8_t(CLAMP(p_color.b * 255.0f, 0.0f, 255.0f));
+ r_color[3] = uint8_t(CLAMP(p_color.a * 255.0f, 0.0f, 255.0f));
+}
+
+void Trail3D::_write_color(uint8_t *p_color, int p_index, uint8_t *r_buffer) {
+ DEV_ASSERT((p_index * attrib_stride + mesh_surface_offsets[RSE::ARRAY_COLOR] + 4) <= attribute_buffer.size());
+ memcpy(&r_buffer[p_index * attrib_stride + mesh_surface_offsets[RSE::ARRAY_COLOR]], p_color, 4);
+}
diff --git a/scene/3d/trail_3d.h b/scene/3d/trail_3d.h
new file mode 100644
index 00000000000..e1fd5702ad8
--- /dev/null
+++ b/scene/3d/trail_3d.h
@@ -0,0 +1,190 @@
+/**************************************************************************/
+/* trail_3d.h */
+/**************************************************************************/
+/* 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. */
+/**************************************************************************/
+
+#pragma once
+
+#include "scene/3d/line_3d.h"
+#include "scene/resources/curve.h"
+#include "scene/resources/gradient.h"
+#include "scene/resources/mesh.h"
+
+class Trail3D : public Line3D {
+ GDCLASS(Trail3D, Line3D);
+
+ // Lots of code for this node is inspired/ported from
+ // https://codeberg.org/MajorMcDoom/cozy-cube-godot-addons/src/branch/main
+ // thank you so much for putting it out there!
+
+protected:
+ void _notification(int p_what);
+ void _validate_property(PropertyInfo &p_property) const;
+ static void _bind_methods();
+
+public:
+ static void init_shaders();
+ static void finish_shaders();
+
+ void set_width(float p_width);
+ float get_width() const;
+
+ void set_emitting(bool p_emitting);
+ bool is_emitting() const;
+
+ void set_width_curve(Ref<Curve> p_curve);
+ Ref<Curve> get_width_curve() const;
+
+ void set_color(const Color &p_color);
+ Color get_color() const;
+
+ void set_color_gradient(Ref<Gradient> p_color_gradient);
+ Ref<Gradient> get_color_gradient() const;
+
+ void set_material_mode(MaterialMode p_material_mode);
+ MaterialMode get_material_mode() const;
+
+ void set_material(Ref<ShaderMaterial> p_material);
+ Ref<ShaderMaterial> get_material() const;
+
+ void set_mesh_alignment(MeshAlignment p_alignment);
+ MeshAlignment get_mesh_alignment() const;
+
+ void set_tiling_mode(TilingMode p_tiling_mode);
+ TilingMode get_tiling_mode() const;
+
+ void set_tiling_multiplier(float p_tiling_multiplier);
+ float get_tiling_multiplier() const;
+
+ void rebuild();
+ void clear();
+
+ // Trail3D
+
+ enum LimitMode {
+ LIMIT_MODE_LIFETIME,
+ LIMIT_MODE_MAX_LENGTH,
+ LIMIT_MODE_MAX,
+ };
+
+ void set_limit_mode(LimitMode p_limit_mode);
+ LimitMode get_limit_mode() const;
+
+ void set_min_section_length(real_t p_min_section_length);
+ real_t get_min_section_length() const;
+
+ void set_lifetime(real_t p_lifetime);
+ real_t get_lifetime() const;
+
+ void set_max_length(real_t p_max_length);
+ real_t get_max_length() const;
+
+ void set_pin_uv(bool p_pin_uv);
+ bool get_pin_uv() const;
+
+ real_t get_current_length() const;
+
+ PackedStringArray get_configuration_warnings() const override;
+
+private:
+ // Ribbon
+ bool emitting = true;
+ float width = 0.3;
+ Ref<Curve> width_curve;
+ Color color = Color(1.0, 1.0, 1.0, 1.0);
+ Ref<Gradient> color_gradient;
+ MaterialMode material_mode = MATERIAL_MODE_MIX;
+ MeshAlignment alignment = MESH_ALIGNMENT_BILLBOARD;
+ TilingMode tiling_mode = TILING_MODE_LENGTH;
+ float tiling_multiplier = 1.0;
+ float tiling_offset = 0.0;
+ PackedVector3Array points;
+ PackedVector3Array normals;
+ PackedRealArray _velocities;
+ PackedVector3Array tangents;
+ Ref<ShaderMaterial> material;
+
+ // Mesh optimization code
+ uint32_t mesh_surface_offsets[RSE::ARRAY_MAX];
+ PackedByteArray vertex_buffer;
+ PackedByteArray attribute_buffer;
+ Vector<uint8_t> index_buffer;
+
+ uint32_t vertex_stride = 0;
+ uint32_t normal_tangent_stride = 0;
+ uint32_t attrib_stride = 0;
+ uint32_t skin_stride = 0;
+ uint32_t mesh_surface_format = 0;
+
+ bool _needs_rebuilding = false;
+ real_t _time = 0.0;
+ PackedRealArray _times;
+ // Reasonable number that's also a multiple of 3, otherwise the renderer screams at us
+ int _last_vertex_count = 600;
+ int _last_index_count = 0;
+ Ref<ArrayMesh> _mesh;
+
+ static inline Ref<Shader> billboard_additive_shader;
+ static inline Ref<Shader> billboard_shader;
+ static inline Ref<Shader> local_additive_shader;
+ static inline Ref<Shader> local_shader;
+
+ static inline Ref<ShaderMaterial> billboard_additive_material;
+ static inline Ref<ShaderMaterial> billboard_material;
+ static inline Ref<ShaderMaterial> local_additive_material;
+ static inline Ref<ShaderMaterial> local_material;
+
+ real_t min_section_length = 0.2;
+
+ //Trail3D
+ real_t lifetime = 0.2;
+ real_t max_length = 5.0;
+ LimitMode limit_mode = LIMIT_MODE_LIFETIME;
+ bool pin_uv = false;
+ real_t _last_pinned_u = 0.0;
+ Vector3 _previous_position;
+ bool _transform_changed = false;
+
+ //Ribbon
+ void _do_rebuild();
+ real_t _calc_current_length() const;
+ void _ensure_material();
+ void _init_clear_mesh();
+ void _process_trail();
+ void _encode_vertex(const Vector3 &p_vertex, int p_index, uint8_t *buffer);
+ uint32_t _encode_normal(const Vector3 &p_normal);
+ void _write_normal(const uint32_t p_normal, int p_index, uint8_t *buffer);
+ void _write_custom(const Vector4 p_custom, int p_index, uint8_t *r_buffer);
+ void _encode_uv(const Vector2 &p_uv, int p_index, uint8_t *buffer);
+ void _encode_color(const Color &p_color, uint8_t *r_color);
+ void _write_color(uint8_t *p_color, int p_index, uint8_t *r_buffer);
+
+ Trail3D();
+};
+
+VARIANT_ENUM_CAST(Trail3D::LimitMode)
diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp
index c5464add86e..a21264d0980 100644
--- a/scene/register_scene_types.cpp
+++ b/scene/register_scene_types.cpp
@@ -250,6 +250,7 @@
#include "scene/3d/lightmap_gi.h"
#include "scene/3d/lightmap_probe.h"
#include "scene/3d/limit_angular_velocity_modifier_3d.h"
+#include "scene/3d/line_3d.h"
#include "scene/3d/look_at_modifier_3d.h"
#include "scene/3d/marker_3d.h"
#include "scene/3d/mesh_instance_3d.h"
@@ -270,6 +271,7 @@
#include "scene/3d/spring_bone_collision_sphere_3d.h"
#include "scene/3d/spring_bone_simulator_3d.h"
#include "scene/3d/sprite_3d.h"
+#include "scene/3d/trail_3d.h"
#include "scene/3d/two_bone_ik_3d.h"
#include "scene/3d/visible_on_screen_notifier_3d.h"
#include "scene/3d/voxel_gi.h"
@@ -662,6 +664,10 @@ void register_scene_types() {
GDREGISTER_CLASS(LightmapGIData);
GDREGISTER_CLASS(LightmapProbe);
GDREGISTER_ABSTRACT_CLASS(Lightmapper);
+
+ GDREGISTER_CLASS(Line3D);
+ GDREGISTER_CLASS(Trail3D);
+
GDREGISTER_CLASS(GPUParticles3D);
GDREGISTER_ABSTRACT_CLASS(GPUParticlesCollision3D);
GDREGISTER_CLASS(GPUParticlesCollisionBox3D);
@@ -1306,6 +1312,9 @@ void register_scene_types() {
// RenderingServer needs to exist for this to succeed.
ColorPickerShape::init_shaders();
GraphEdit::init_shaders();
+#ifndef _3D_DISABLED
+ Trail3D::init_shaders();
+#endif //_3D_DISABLED
}
SceneDebugger::initialize();
@@ -1362,6 +1371,7 @@ void unregister_scene_types() {
PanoramaSkyMaterial::cleanup_shader();
ProceduralSkyMaterial::cleanup_shader();
FogMaterial::cleanup_shader();
+ Trail3D::finish_shaders();
#endif // _3D_DISABLED
ParticleProcessMaterial::finish_shaders();