Tomcat
line_script.gd
@tool
extends Node3D
# --- Exported variables ---
@export var start_pos: Vector3 = Vector3.ZERO
@export var end_pos: Vector3 = Vector3(0, 1, 0)
@export var thickness: float = 0.05
@export var color_hex: Color = "#FF8000"
@export var auto_update: bool = true
# --- Internal variables ---
var mesh_instance: MeshInstance3D
var mat: StandardMaterial3D
func _ready():
# Create the MeshInstance3D for the line
mesh_instance = MeshInstance3D.new()
add_child(mesh_instance)
# Create the material
mat = StandardMaterial3D.new()
mat.albedo_color = Color(color_hex)
mesh_instance.material_override = mat
# Initial update
update_line()
func _process(_delta):
if auto_update:
update_line()
func update_line():
# Direction and length
var dir = end_pos - start_pos
var length = dir.length()
if length < 0.001:
length = 0.001 # prevent zero-length lines
# Create cylinder mesh
var cylinder = CylinderMesh.new()
cylinder.top_radius = thickness
cylinder.bottom_radius = thickness
cylinder.height = length
mesh_instance.mesh = cylinder
# Update material color
mat.albedo_color = Color(color_hex)
# Midpoint
var mid = start_pos + dir * 0.5
# Rotation: cylinder extends along Y by default, rotate to align with line
var up_vector = Vector3(0, 0, 1) if abs(dir.normalized().dot(Vector3.UP)) > 0.99 else Vector3.UP
var basis = Basis().looking_at(dir.normalized(), up_vector)
var rot_correction = Basis(Vector3.RIGHT, deg_to_rad(90))
# Assign global transform directly
mesh_instance.global_transform = Transform3D(basis, mid) * Transform3D(rot_correction, Vector3.ZERO)
lines3d.gd
@tool
extends EditorPlugin
func _enable_plugin() -> void:
# Add autoloads here.
pass
func _disable_plugin() -> void:
# Remove autoloads here.
pass
func _enter_tree() -> void:
# Initialization of the plugin goes here.
add_custom_type("LineDrawer3D", "Node3D", preload("res://addons/lines3d/line_script.gd"), preload("res://addons/lines3d/line_icon.svg"))
func _exit_tree() -> void:
# Clean-up of the plugin goes here.
remove_custom_type("LineDrawer3D")
plugin.cfg
[plugin]
name="Lines3D"
description="a great way to create lines"
author="Chezz2413"
version="1.0"
script="lines3d.gd"