Skip to content

Instantly share code, notes, and snippets.

@Skaruts
Last active January 7, 2021 15:58
Show Gist options
  • Save Skaruts/149b73608d4aedc0601d988fd340d65f to your computer and use it in GitHub Desktop.
Save Skaruts/149b73608d4aedc0601d988fd340d65f to your computer and use it in GitHub Desktop.
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=
# Builds grid lines, grid bounds and x,y,z axis lines, using three meshes
#
# Attach this script to a 3D scene like:
# GridManager (Spatial)
# Grid (Spatial)
# Origin (Spatial)
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=
extends Spatial
export (int) var grid_size := 64 # size of the grid
export (int) var origin_size := 3 # size of the origin axes lines
export (int) var xz_steps := 8 # how many lines to skip between axis grid lines
export (Color) var color_x_axis := Color("aa0000") # origin axes colors
export (Color) var color_z_axis := Color("00aa00")
export (Color) var color_y_axis := Color.blue
export (Color) var color_grid_lines := Color("303030") # grid color
export (Color) var color_x_grid_line := Color("400000") # color of the x axis lines
export (Color) var color_z_grid_line := Color("004000") # color of the z axis lines
export (Color) var color_bounds := Color.blue # color of the grid bounds
export (bool) var origin_visible := true setget _set_origin_visible_
export (bool) var grid_visible := true setget _set_grid_visible_
onready var _origin := $Origin
onready var _grids := $Grid
var _has_grids_and_origin := false
var _origin_on_top := true
var _grid_on_top := false
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=
# Public interface
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=
func build() -> void:
if _has_grids_and_origin:
for c in _grids.get_children(): c.queue_free()
for c in _origin.get_children(): c.queue_free()
_create_grid_bounds()
_create_grid()
_create_origin()
_has_grids_and_origin = true
if not grid_visible: hide_grid()
if not origin_visible: hide_origin()
func toggle_origin() -> void:
origin_visible = not origin_visible
_origin.visible = origin_visible
func hide_origin() -> void:
origin_visible = false
_origin.visible = false
func show_origin() -> void:
origin_visible = true
_origin.visible = true
func toggle_grid() -> void:
grid_visible = not grid_visible
_grids.visible = grid_visible
func hide_grid() -> void:
grid_visible = false
_grids.visible = false
func show_grid() -> void:
grid_visible = true
_grids.visible = true
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=
# Internal stuff
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=
func _set_origin_visible_(enable) -> void:
origin_visible = enable
$Origin.visible = enable
func _set_grid_visible_(enable) -> void:
grid_visible = enable
$Grid.visible = enable
func _make_material(thickness:=1, on_top:=false, unshaded:=true, transparent:=true) -> SpatialMaterial:
var mat := SpatialMaterial.new()
mat.flags_unshaded = unshaded
mat.flags_no_depth_test = on_top
mat.params_line_width = thickness
mat.flags_transparent = transparent
mat.vertex_color_use_as_albedo = true
return mat
func _make_mesh(parent:Spatial, vertices:Array, colors:Array, material:SpatialMaterial) -> void:
var arrays := []
arrays.resize(Mesh.ARRAY_MAX)
arrays[Mesh.ARRAY_VERTEX] = PoolVector3Array(vertices)
arrays[Mesh.ARRAY_COLOR] = PoolColorArray(colors)
var m := ArrayMesh.new()
m.add_surface_from_arrays(Mesh.PRIMITIVE_LINES, arrays)
var mi := MeshInstance.new()
mi.mesh = m
mi.material_override = material
parent.add_child(mi)
func _create_grid() -> void:
var vertices := []
var colors := []
for i in range(1, grid_size):
vertices += [
Vector3( 0, 0, i),
Vector3(grid_size, 0, i),
Vector3( i, 0, 0),
Vector3( i, 0, grid_size)
]
if i % xz_steps != 0:
colors += [color_grid_lines, color_grid_lines, color_grid_lines, color_grid_lines]
else:
colors += [color_x_grid_line, color_x_grid_line, color_z_grid_line, color_z_grid_line]
_make_mesh(_grids, vertices, colors, _make_material(1, _grid_on_top, true, false))
func _create_grid_bounds() -> void:
var a := Vector3( 0, 0, 0)
var b := Vector3(grid_size, 0, 0)
var c := Vector3(grid_size, 0, grid_size)
var d := Vector3( 0, 0, grid_size)
var vertices := [a,b, b,c, c,d, d,a]
var colors := [ color_bounds, color_bounds, color_bounds, color_bounds,
color_bounds, color_bounds, color_bounds, color_bounds ]
_make_mesh(_grids, vertices, colors, _make_material(1, _grid_on_top, true, false))
func _create_origin() -> void:
var gc := Vector3( grid_size/2.0, 0, grid_size/2.0 ) # grid center
var os := origin_size
var vertices := [
gc + Vector3( 0, 0, 0 ), gc + Vector3( os, 0, 0 ),
gc + Vector3( 0, 0, 0 ), gc + Vector3( 0, os, 0 ),
gc + Vector3( 0, 0, 0 ), gc + Vector3( 0, 0, os )
]
var colors := [
color_x_axis, color_x_axis,
color_y_axis, color_y_axis,
color_z_axis, color_z_axis
]
_make_mesh(_origin, vertices, colors, _make_material(2, _origin_on_top, true, false))
[gd_scene load_steps=2 format=2]
[ext_resource path="res://scripts/GridManager.gd" type="Script" id=1]
[node name="GridManager" type="Spatial"]
script = ExtResource( 1 )
[node name="Grid" type="Spatial" parent="."]
[node name="Origin" type="Spatial" parent="."]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment