grunk/src/world/gunk_body.gd

97 lines
3.3 KiB
GDScript3
Raw Normal View History

2025-03-01 14:40:02 -07:00
class_name GunkBody extends StaticBody3D
## StaticBody3D with an associated "gunkable" mesh.
const MASK_DIM := 512
const FACE_EPSILON := 0.4
const MASK_COLOR := Color.RED
@export var mask_image: Image
var meshtool := MeshDataTool.new()
@onready var mesh_instance: MeshInstance3D = $MeshInstance3D
@onready var mesh: ArrayMesh = mesh_instance.mesh
@onready var gunk_mat: ShaderMaterial = mesh_instance.get_surface_override_material(0).next_pass
@onready var debug_draw: DebugDraw = %DebugDraw
func _ready() -> void:
if not mask_image:
mask_image = Image.create_empty(MASK_DIM, MASK_DIM, false, Image.FORMAT_L8)
_update_gunk_material()
meshtool.create_from_surface(mesh, 0)
## Must be called internally whenever `mask_image` is updated
func _update_gunk_material() -> void:
gunk_mat.set_shader_parameter("gunk_mask", ImageTexture.create_from_image(mask_image))
## Transform cartesian coordinates to barycentric wrt the given triangle
func _barycentric(p: Vector3, a: Vector3, b: Vector3, c: Vector3) -> Vector3:
var v0 := b - a
var v1 := c - a
var v2 := p - a
var d00 := v0.dot(v0)
var d01 := v0.dot(v1)
var d11 := v1.dot(v1)
var d20 := v2.dot(v0)
var d21 := v2.dot(v1)
var denom := d00 * d11 - d01 * d01
var v := (d11 * d20 - d01 * d21) / denom
var w := (d00 * d21 - d01 * d20) / denom
var u := 1.0 - v - w
return Vector3(u, v, w)
func _is_in_triangle(point: Vector3, v1: Vector3, v2: Vector3, v3: Vector3) -> bool:
var bc := _barycentric(point, v1, v2, v3)
return (bc.x > 0 and bc.x < 1) and (bc.y > 0 and bc.y < 1) and (bc.z > 0 and bc.z < 1)
func _get_face(point: Vector3, normal: Vector3) -> int:
for i in range(meshtool.get_face_count()):
if meshtool.get_face_normal(i).distance_squared_to(normal) > FACE_EPSILON:
continue
# Normals match, so check if the point is on this face
var v1 := meshtool.get_vertex(meshtool.get_face_vertex(i, 0))
var v2 := meshtool.get_vertex(meshtool.get_face_vertex(i, 1))
var v3 := meshtool.get_vertex(meshtool.get_face_vertex(i, 2))
if _is_in_triangle(point, v1, v2, v3):
return i
return -1
func _get_uv(point: Vector3, normal: Vector3) -> Vector2:
var face := _get_face(point, normal)
if face < 0:
return Vector2.INF
var fv0 := meshtool.get_face_vertex(face, 0)
var fv1 := meshtool.get_face_vertex(face, 1)
var fv2 := meshtool.get_face_vertex(face, 2)
var v1 := meshtool.get_vertex(fv0)
var v2 := meshtool.get_vertex(fv1)
var v3 := meshtool.get_vertex(fv2)
var bc := _barycentric(point, v1, v2, v3) # TODO memoize
var uv1 := meshtool.get_vertex_uv(fv0)
var uv2 := meshtool.get_vertex_uv(fv1)
var uv3 := meshtool.get_vertex_uv(fv2)
return (uv1 * bc.x) + (uv2 * bc.y) + (uv3 * bc.z)
## Paint a rectangle on the mask at a given point & normal on the mesh.
func paint_mask(point: Vector3, normal: Vector3, radius: int) -> void:
# debug_draw.draw_vector(normal, point)
var local_point := point * global_transform
var local_normal := normal * global_basis
var uv := _get_uv(local_point, local_normal)
if uv == Vector2.INF:
return
var px_center: Vector2i = Vector2i(uv * Vector2(mask_image.get_size()))
var rect := Rect2i(px_center.x - radius, px_center.y - radius, 2 * radius, 2 * radius)
mask_image.fill_rect(rect, MASK_COLOR)
# TODO: can we call this once after all painting in a single frame is finished?
_update_gunk_material()