Last active
April 20, 2022 05:24
-
-
Save HungryProton/b3b187b8f8499fef1164376140c55c99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func extrude(bevel: Array, path: Path): | |
# Bevel is your cross section, it's an array of Vector3 | |
# Path is the curve we're following | |
var resolution = 1.0 | |
var curve: Curve3D = path.curve | |
var length: float = curve.get_baked_length() | |
var steps: int = floor(length / resolution) | |
var offset: float = length / steps | |
var bevel_count: int = bevel.size() | |
var up = Vector3(0, 1, 0) | |
var surface_tool := SurfaceTool.new() | |
surface_tool.begin(Mesh.PRIMITIVE_TRIANGLES); | |
surface_tool.add_smooth_group(true) | |
for i in range(steps + 1): | |
var current_offset: float = i * offset | |
var position_on_curve: Vector3 = curve.interpolate_baked(current_offset) | |
var position_2: Vector3 | |
if current_offset + 0.1 < length: | |
position_2 = curve.interpolate_baked(current_offset + 0.1) | |
else: | |
position_2 = curve.interpolate_baked(current_offset - 0.1) | |
position_2 += (position_on_curve - position_2) * 2.0 | |
var node = Spatial.new() | |
node.look_at_from_position(position_on_curve, position_2, up) | |
up = node.transform.basis.y | |
for j in range(bevel_count): | |
var pos = bevel.points[j] | |
pos = node.transform.xform(pos) | |
surface_tool.add_color(Color(1, 1, 1, 1)); | |
surface_tool.add_vertex(pos) | |
if i > 0: | |
for k in range(bevel_count - 1): | |
surface_tool.add_index((i - 1) * bevel_count + k) | |
surface_tool.add_index((i - 1) * bevel_count + k + 1) | |
surface_tool.add_index(i * bevel_count + k) | |
surface_tool.add_index(i * bevel_count + k) | |
surface_tool.add_index((i - 1) * bevel_count + k + 1) | |
surface_tool.add_index(i * bevel_count + k + 1) | |
node.queue_free() | |
surface_tool.generate_normals() | |
var mesh_instance = MeshInstance.new() | |
mesh_instance.transform = path.transform | |
mesh_instance.mesh = surface_tool.commit() | |
return mesh_instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment