Godot Version
4.3
Question
I want to create a 3D line with adjustable thickness. This is the code I generated using AI, and it works, but it is always perpendicular to the y-axis. I don’t know how to modify it because I am not particularly familiar with 3D operations
extends Node3D
#extends Spatial
@onready var mesh_instance_3d: MeshInstance3D = $MeshInstance3D
@onready var camera_3d: Camera3D = $Camera3D
#var i_mesh:ImmediateMesh
var mesh:Mesh
func _ready() -> void:
#draw_debug_mesh = ImmediateMesh.new()
#i_mesh = mesh_instance_3d.mesh
var from = Vector3(0, 0, 0)
var to = Vector3(10, 12, 10)
var radius = 0.05
var segments = 8
var meshs = create_thick_line0(from, to, radius, segments)
mesh_instance_3d.mesh = meshs[0]
#mesh_instance_3d.transform = meshs[1]
func create_thick_line0(from: Vector3, to: Vector3, radius: float, segments: int):
var direction = to - from
var length = direction.length()
direction = direction.normalized()
## Create a basis (rotation) to align the cylinder along the direction
#var B = Basis()
var B1 = Basis.looking_at(direction, Vector3.UP)
var T = Transform3D()
T.basis = B1
T.origin = from + direction * length * 0.5 # Move to midpoint
# Build cylinder mesh using SurfaceTool
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.set_smooth_group(1)
for i in range(segments):
var angle1 = TAU * i / segments
var angle2 = TAU * (i + 1) / segments
var p1 = Vector3(radius * cos(angle1), -length / 2, radius * sin(angle1))
var p2 = Vector3(radius * cos(angle2), -length / 2, radius * sin(angle2))
var p3 = Vector3(radius * cos(angle1), length / 2, radius * sin(angle1))
var p4 = Vector3(radius * cos(angle2), length / 2, radius * sin(angle2))
# Side triangles (two per segment)
st.set_normal((p1 - p2).cross(p3 - p1).normalized())
st.add_vertex(p1)
st.add_vertex(p2)
st.add_vertex(p3)
st.set_normal((p2 - p4).cross(p3 - p2).normalized())
st.add_vertex(p2)
st.add_vertex(p4)
st.add_vertex(p3)
st.index()
mesh = st.commit()
#mesh = mesh.duplicate()
###mesh.surface_set_material(0, SpatialMaterial.new())
##
### Apply transform to orient the cylinder
#var xform = Transform3D()
#xform.basis = basis
#xform.origin = from + direction * length * 0.5
#mesh.transform=xform
return [mesh,T]