【问题标题】:(Godot Engine) Replicating Line2D's joint functionality on the first + last points(Godot 引擎)在第一个 + 最后一个点上复制 Line2D 的关节功能
【发布时间】:2021-05-07 06:19:18
【问题描述】:

我创建了以下方法来使用 Line2D 节点勾勒出 2D 多边形(支持 _drawing,因为 Line2D 节点的纹理和圆形连接功能):

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)

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

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

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);}

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

谢谢你, 迈克。

【问题讨论】:

    标签: geometry line polygon godot gdscript


    【解决方案1】:

    如果使用 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。

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多