(Godot 引擎)复制 Line2D 在第一个点和最后一个点上的联合功能
(Godot Engine) Replicating Line2D's joint functionality on the first + last points
我创建了以下方法来使用 Line2D 节点绘制二维多边形的轮廓(由于 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)
我尝试过的一种方法是向 _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);}
结果看起来不错,但我无法想象在大量多边形上使用这种方法。
谢谢你,
迈克.
如果使用 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。
我创建了以下方法来使用 Line2D 节点绘制二维多边形的轮廓(由于 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)
我尝试过的一种方法是向 _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);}
结果看起来不错,但我无法想象在大量多边形上使用这种方法。
谢谢你, 迈克.
如果使用 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。