64 lines
1.8 KiB
GDScript
64 lines
1.8 KiB
GDScript
extends Node3D
|
|
const CENTER := Vector3.ZERO
|
|
@export var threshold: float = 0.5
|
|
|
|
var generate_mesh_shader = preload("res://SurfaceNetsWorld/generate_mesh.tres")
|
|
|
|
@export var regenerate_mesh = false
|
|
|
|
|
|
@export var chunk_size = 16
|
|
@export var show_sample_points = false
|
|
@export var show_surface_points = false
|
|
@export var show_surface = true
|
|
|
|
var mesh
|
|
|
|
var color = Color.RED
|
|
|
|
var meshinstance = MeshInstance3D.new()
|
|
var material = ShaderMaterial.new()
|
|
|
|
var gpu_sdf
|
|
|
|
func generate_chunk(pgpu_sdf) -> void:
|
|
gpu_sdf = pgpu_sdf
|
|
material.shader = generate_mesh_shader
|
|
meshinstance.material_override = material
|
|
add_child(meshinstance)
|
|
meshinstance.mesh = gpu_sdf.compute_mesh(chunk_size, threshold, self.position)
|
|
|
|
func _input(event):
|
|
if event is InputEventKey and event.is_action_released("RegenerateMesh"):
|
|
if regenerate_mesh:
|
|
regenerate_mesh = false
|
|
else:
|
|
regenerate_mesh = true
|
|
|
|
func _process(_delta: float) -> void:
|
|
if show_surface && regenerate_mesh:
|
|
regenerate_mesh = false
|
|
clear()
|
|
meshinstance.mesh = gpu_sdf.compute_mesh(chunk_size, threshold, self.position)
|
|
if show_surface_points:
|
|
var idx = 0
|
|
var text_id = 0
|
|
while idx < gpu_sdf.iout_surface_points.size()/ 2:
|
|
text_id += 1
|
|
var value = Vector3(gpu_sdf.iout_surface_points.get(idx), gpu_sdf.iout_surface_points.get(idx+1), gpu_sdf.iout_surface_points.get(idx+2)) - Vector3(chunk_size / 2, chunk_size / 2, chunk_size / 2)
|
|
if gpu_sdf.iout_surface_points.get(idx) != -1.0:
|
|
DebugDraw3D.draw_square(value, 0.2, color.from_rgba8(255, 128, 128, 255))
|
|
if text_id % 1 == 0:
|
|
DebugDraw3D.draw_text(value, str(value), 35)
|
|
idx += 3
|
|
else:
|
|
idx += 1
|
|
|
|
|
|
func clear():
|
|
mesh = ArrayMesh.new()
|
|
|
|
|
|
func get_index_from_coords(coords: Vector3i):
|
|
return coords.x + coords.y * chunk_size + coords.z * chunk_size * chunk_size
|