-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeshInstance.gd
125 lines (104 loc) · 5.37 KB
/
MeshInstance.gd
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
extends MeshInstance
tool
tool
export (Texture) var texture: Texture = null setget set_texture
export (Vector3) var pixel_size: Vector3 = Vector3(0.1, 0.1, 0.1) setget set_pixel_size
export (bool) var texture_is_srgb: bool = true setget set_texture_is_srgb
func set_pixel_size (_pixel_size: Vector3) -> void:
pixel_size = _pixel_size
update_mesh()
update_material()
func set_texture (_texture: Texture) -> void:
texture = _texture
update_mesh()
update_material()
func set_texture_is_srgb (value: bool) -> void:
texture_is_srgb = value
update_material()
func update_mesh () -> void:
mesh = ArrayMesh.new()
if texture == null:
return
var surface = SurfaceTool.new()
surface.begin(Mesh.PRIMITIVE_TRIANGLES)
var image = texture.get_data()
image.flip_y()
image.lock()
for x in image.get_width():
for y in image.get_height():
var color = image.get_pixel(x, y)
if color.a == 0:
continue
_add_front(surface, x, y, color)
_add_back(surface, x, y, color)
if x == 0 or image.get_pixel(x - 1, y).a == 0:
_add_right(surface, x, y, color)
if x == image.get_width() - 1 or image.get_pixel(x + 1, y).a == 0:
_add_left(surface, x, y, color)
if y == 0 or image.get_pixel(x, y - 1).a == 0:
_add_bottom(surface, x, y, color)
if y == image.get_width() - 1 or image.get_pixel(x, y + 1).a == 0:
_add_top(surface, x, y, color)
image.unlock()
mesh = surface.commit()
func update_material () -> void:
if mesh.get_surface_count() == 0:
return
var material = SpatialMaterial.new()
material.vertex_color_use_as_albedo = true
material.vertex_color_is_srgb = texture_is_srgb
mesh.surface_set_material(0, material)
func _add_bottom (surface: SurfaceTool, x: float, y: float, color: Color) -> void:
surface.add_normal(Vector3(0, -1, 0))
surface.add_color(color)
surface.add_vertex(Vector3((x + 1) * pixel_size.x, y * pixel_size.y, 0))
surface.add_vertex(Vector3(x * pixel_size.x, y * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, y * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, y * pixel_size.y, 0))
surface.add_vertex(Vector3(x * pixel_size.x, y * pixel_size.y, 0))
surface.add_vertex(Vector3(x * pixel_size.x, y * pixel_size.y, pixel_size.z))
func _add_top (surface: SurfaceTool, x: float, y: float, color: Color) -> void:
surface.add_normal(Vector3(0, 1, 0))
surface.add_color(color)
surface.add_vertex(Vector3((x + 1) * pixel_size.x, (y + 1) * pixel_size.y, 0))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, (y + 1) * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3(x * pixel_size.x, (y + 1) * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, (y + 1) * pixel_size.y, 0))
surface.add_vertex(Vector3(x * pixel_size.x, (y + 1) * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3(x * pixel_size.x, (y + 1) * pixel_size.y, 0))
func _add_right (surface: SurfaceTool, x: float, y: float, color: Color) -> void:
surface.add_normal(Vector3(-1, 0, 0))
surface.add_color(color)
surface.add_vertex(Vector3(x * pixel_size.x, y * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3(x * pixel_size.x, y * pixel_size.y, 0))
surface.add_vertex(Vector3(x * pixel_size.x, (y + 1) * pixel_size.y, 0))
surface.add_vertex(Vector3(x * pixel_size.x, (y + 1) * pixel_size.y, 0))
surface.add_vertex(Vector3(x * pixel_size.x, (y + 1) * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3(x * pixel_size.x, y * pixel_size.y, pixel_size.z))
func _add_left (surface: SurfaceTool, x: float, y: float, color: Color) -> void:
surface.add_normal(Vector3(1, 0, 0))
surface.add_color(color)
surface.add_vertex(Vector3((x + 1) * pixel_size.x, y * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, (y + 1) * pixel_size.y, 0))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, y * pixel_size.y, 0))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, (y + 1) * pixel_size.y, 0))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, y * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, (y + 1) * pixel_size.y, pixel_size.z))
func _add_front (surface: SurfaceTool, x: float, y: float, color: Color) -> void:
surface.add_normal(Vector3(0, 0, -1))
surface.add_color(color)
surface.add_vertex(Vector3(x * pixel_size.x, y * pixel_size.y, 0))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, y * pixel_size.y, 0))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, (y + 1) * pixel_size.y, 0))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, (y + 1) * pixel_size.y, 0))
surface.add_vertex(Vector3(x * pixel_size.x, (y + 1) * pixel_size.y, 0))
surface.add_vertex(Vector3(x * pixel_size.x, y * pixel_size.y, 0))
func _add_back (surface: SurfaceTool, x: float, y: float, color: Color) -> void:
surface.add_normal(Vector3(0, 0, 1))
surface.add_color(color)
surface.add_vertex(Vector3(x * pixel_size.x, y * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, (y + 1) * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, y * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3((x + 1) * pixel_size.x, (y + 1) * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3(x * pixel_size.x, y * pixel_size.y, pixel_size.z))
surface.add_vertex(Vector3(x * pixel_size.x, (y + 1) * pixel_size.y, pixel_size.z))