微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Godot 引擎在第一个 + 最后一个点上复制 Line2D 的联合功能

如何解决Godot 引擎在第一个 + 最后一个点上复制 Line2D 的联合功能

我创建了以下方法来使用 Line2D 节点勾勒出 2D 多边形的轮廓(由于 Line2D 节点的纹理和圆形连接功能,我更喜欢使用 _drawing):

func Set_polygon_Outline(_polygon_node: Node2D,_width: int = 5,_color: Color = Color.black,_texture: Texture = null) -> void:
if _polygon_node is polygon2D:
    var _polygon: PoolVector2Array = (_polygon_node as polygon2D).polygon
    if _polygon.size() >= 3:
        # Line2D node setup
        var _line_node: Line2D = null
        var _line_name: String = str(_polygon_node.name,"_Line")
        if not _polygon_node.has_node(_line_name):
            _line_node = Line2D.new() ; _line_node.name = _line_name ; _polygon_node.add_child(_line_node)
        else: _line_node = _polygon_node.get_node(_line_name) as Line2D
        # Line2D properties setup
        if _line_node != null:
            _line_node.width = _width ; _line_node.default_color = _color ; _line_node.joint_mode = Line2D.LINE_JOINT_ROUND
            if _texture != null:
                _line_node.texture = _texture ; _line_node.texture_mode = Line2D.LINE_TEXTURE_STRETCH
            var _points: PoolVector2Array = _polygon ; _points.append(_polygon[0]) ; _line_node.points = _points

如何以与其他点相同的方式复制点 0 上的圆点连接?


结果符合预期,除了结束点(从 4 到 0)

enter image description here

我尝试过的一种方法是在 _points 数组中附加一个额外的点(点 1)。虽然无纹理的看起来像预期的那样,但纹理变体在附加线上略微偏离,因为两个带有 alpha 值的叠加纹理使它看起来“更大胆”。

enter image description here

另一种(非常非正统的方法)是使用以下方法创建两个多边形:一个黑色,一个带有模糊着色器:

func Set_polygon_Shadow(_polygon_node: Node2D,_size: float = 10.0,_color: Color = Color.black) -> void:
if _polygon_node is polygon2D:
    var _polygon: PoolVector2Array = (_polygon_node as polygon2D).polygon
    if _polygon.size() >= 3:
        # Shadow polygon node setup
        var _shadow_name: String = str(_polygon_node.name,"_Shadow")
        if not _polygon_node.has_node(_shadow_name):
            var _shadow_node: polygon2D = polygon2D.new()
            _shadow_node.polygon = Geometry.offset_polygon_2d(_polygon,_size).front() ; _shadow_node.color = _color
            _shadow_node.show_behind_parent = true ; _polygon_node.add_child(_shadow_node)
            # Blur polygon node setup
            var _blur_node: polygon2D = polygon2D.new()
            _blur_node.polygon = Geometry.offset_polygon_2d(_polygon,_size * 2.0).front()
            _blur_node.material = ShaderMaterial.new()
            _blur_node.material.shader = preload("res://shaders/Shader_Blur.shader")
            _blur_node.material.set_shader_param("Strength",2.0)
            _blur_node.show_behind_parent = true ; _polygon_node.add_child(_blur_node)

着色器代码

shader_type canvas_item;
uniform float Strength : hint_range(0.0,5.0);
void fragment() {COLOR = textureLod(SCREEN_TEXTURE,SCREEN_UV,Strength);}

结果看起来不错,但我无法想象在大量多边形上使用这种方法

enter image description here

谢谢你, 迈克。

解决方法

如果使用 Line2D 是一个可行的解决方案,除了修复循环,那么让我们修复循环。

要使 Line2D 无缝循环,即使是透明的,它也必须以直线段(而不是角)闭合。 并且没有结束帽。

因此,我建议将第一个点移动到其原始位置和第二个点之间的中间位置。然后在最后添加第一个点的原始位置,然后是其移动位置的副本...

像这样:

# Let o be a copy of the original first point
var o = _points[0]

# Let m be the middle of the straight segment between the first and second points
var m = o + (_points[1] - o) * 0.5

_points[0] = m    # The line now starts in m,we are moving the first point forth
_points.append(o) # Since we moved the first point forth,add its original position back
_points.append(m) # Add m at the end,so it loops,in the middle of a straight segment

这应该会产生一个无缝循环的 Line2D。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。