aboutsummaryrefslogtreecommitdiff
path: root/src/addons/rmsmartshape/shapes/mesh.gd
blob: 1259c15a67aa0586c980bbe4eba33faae099b41e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
tool
extends Reference
class_name SS2D_Mesh

"""
Used to organize all requested meshes to be rendered by their textures
"""

var texture: Texture = null
var texture_normal: Texture = null
var flip_texture: bool = false
# Array of ArrayMesh
var meshes: Array = []
var mesh_transform: Transform2D = Transform2D()
var material: Material = null
var z_index: int = 0
var z_as_relative: bool = true


func _init(
	t: Texture = null,
	tn: Texture = null,
	f: bool = false,
	xform: Transform2D = Transform2D(),
	m: Array = [],
	mat: Material = null
):
	texture = t
	texture_normal = tn
	flip_texture = f
	meshes = m
	mesh_transform = xform
	material = mat


func duplicate(sub_resource: bool = false):
	var _new = __new()
	_new.texture = texture
	_new.texture_normal = texture_normal
	_new.flip_texture = flip_texture
	_new.mesh_transform = mesh_transform
	_new.material = material
	_new.z_index = z_index
	_new.z_as_relative = z_as_relative
	_new.meshes = []
	if sub_resource:
		for m in meshes:
			_new.meshes.push_back(m.duplicate(true))
	return _new


func matches(tex: Texture, tex_n: Texture, f: bool, t: Transform2D, m: Material, zi: int, zb: bool) -> bool:
	if (
		tex == texture
		and tex_n == texture_normal
		and f == flip_texture
		and t == mesh_transform
		and m == material
		and zi == z_index
		and zb == z_as_relative
	):
		return true
	return false


func mesh_matches(m) -> bool:
	return matches(
		m.texture,
		m.texture_normal,
		m.flip_texture,
		m.mesh_transform,
		m.material,
		m.z_index,
		m.z_as_relative
	)


func debug_print_array_mesh(am: ArrayMesh) -> String:
	var s = "Faces:%s  |  Surfs:%s  | " % [am.get_faces(), am.get_surface_count()]
	return s


func render(ci: CanvasItem):
	#print("mesh count %s" % meshes.size())
	for mesh in meshes:
		ci.draw_mesh(mesh, texture, texture_normal)


# Workaround (class cannot reference itself)
func __new() -> SS2D_Point:
	return get_script().new()