summaryrefslogtreecommitdiff
path: root/extractor/gnd_format.gd
blob: f13956ee18d40dee241ade5e0bdcbdd19a9b5958 (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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
class_name GNDFormat


## Byte Length: 4 [br]
## GRAT
var signature: String = "GRGN"

## Byte Type: u8 [br]
## Byte Length: 2
var version: Version

## Byte Type: i32 [br]
## Byte Length: 4
var width: int

## Byte Type: i32 [br]
## Byte Length: 4
var height: int

## Byte Type: f32 [br]
## Byte Length: 4 [br]
## Always 10.
var scale: float

## Byte Type: i32 [br]
## Byte Length: 4
var texture_count: int

## Byte Type: i32 [br]
## Byte Length: 4
## Always 80.
var texture_path_length: int

## Byte Length: [member texture_count] * [member texture_path_length] [br]
var texture_paths: Array

## Byte Type: i32 [br]
## Byte Length: 4
var light_map_slice_count: int

## Byte Type: i32 [br]
## Byte Length: 4
var light_map_width: int

## Byte Type: i32 [br]
## Byte Length: 4
var light_map_slice_height: int

## Byte Type: i32 [br]
## Byte Length: 4
var light_map_cells_per_grid: int

## Length: [member light_map_slice_count]
var light_map_slices: Array

## Byte Type: i32 [br]
## Byte Length: 4
var surface_count: int

## Length: [member surface_count]
var surfaces: Array[Surface]

## Length: [member width] * [member height]
var ground_mesh_cubes: Array[GroundMeshCube]


static func from_bytes(bytes: ByteStream) -> GNDFormat:
	var gnd_format = GNDFormat.new()
	
	bytes.advance(gnd_format.signature.length())
	
	@warning_ignore("shadowed_variable")
	var version = Version.new()
	version.major = bytes.decode_u8()
	version.minor = bytes.decode_u8()
	gnd_format.version = version
	
	if version.minor > 7:
		print(version)
		return gnd_format
	
	gnd_format.width = bytes.decode_s32()
	gnd_format.height = bytes.decode_s32()
	gnd_format.scale = bytes.decode_float()
	gnd_format.texture_count = bytes.decode_s32()
	gnd_format.texture_path_length = bytes.decode_s32()
	
	gnd_format.texture_paths = []
	for _n in gnd_format.texture_count:
		gnd_format.texture_paths.append(
			bytes.get_string_from_ascii(gnd_format.texture_path_length)
		)
	
	gnd_format.light_map_slice_count = bytes.decode_s32()
	gnd_format.light_map_width = bytes.decode_s32()
	gnd_format.light_map_slice_height = bytes.decode_s32()
	gnd_format.light_map_cells_per_grid = bytes.decode_s32()
	
	#gnd_format.light_map_slices = []
	#for _n in gnd_format.light_map_slice_count:
		#gnd_format.light_map_slices.append(LightMapSlice.from_bytes(bytes))
	bytes.advance(
		gnd_format.light_map_slice_count *
		gnd_format.light_map_width *
		gnd_format.light_map_slice_height *
		4
	)
	
	gnd_format.surface_count = bytes.decode_s32()
	
	gnd_format.surfaces = [] as Array[Surface]
	for _n in gnd_format.surface_count:
		gnd_format.surfaces.append(Surface.from_bytes(bytes))
	
	gnd_format.ground_mesh_cubes = [] as Array[GroundMeshCube]
	for _n in (gnd_format.width * gnd_format.height):
		gnd_format.ground_mesh_cubes.append(GroundMeshCube.from_bytes(bytes))
	
	#print(inst_to_dict(gnd_format))
	return gnd_format


class LightMapSlice:
	## Byte Type: i32 [br]
	## Byte Length: 4
	var pixel_format: int
	
	## Byte Type: i32 [br]
	## Byte Length: 4
	var width: int
	
	## Byte Type: i32 [br]
	## Byte Length: 4
	var height: int
	
	## Byte Length: [member width] * [member height]
	var shadow_map_pixels: ByteStream
	
	## Byte Length: [member width] * [member height]
	var light_map_pixels: ByteStream
	
	
	static func from_bytes(bytes: ByteStream) -> LightMapSlice:
		var slice = LightMapSlice.new()
		
		slice.pixel_format = bytes.decode_s32()
		slice.width = bytes.decode_s32()
		slice.height = bytes.decode_s32()
		slice.shadow_map_pixels = bytes.get_buffer(slice.width * slice.height)
		slice.light_map_pixels = bytes.get_buffer(slice.width * slice.height)
		
		#print(inst_to_dict(slice))
		return slice


class Surface:
	## Byte Type: f32 [br]
	## Byte Length: 4
	var u_bottom_left: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var u_bottom_right: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var u_top_left: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var u_top_right: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var v_bottom_left: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var v_bottom_right: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var v_top_left: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	var v_top_right: float
	
	## Byte Type: i16 [br]
	## Byte Length: 2
	var texture_index: int
	
	## Byte Type: i16 [br]
	## Byte Length: 2
	var light_map_index: int
	
	## Byte Type: u8 [br]
	## Byte Length: 1
	var vertex_color_blue: int
	
	## Byte Type: u8 [br]
	## Byte Length: 1
	var vertex_color_green: int
	
	## Byte Type: u8 [br]
	## Byte Length: 1
	var vertex_color_red: int
	
	## Byte Type: u8 [br]
	## Byte Length: 1
	var vertex_color_alpha: int
	
	
	func get_uvs() -> Dictionary: # Dictionary[Vector2, Vector2]
		return {
			Vector2(0, 0): Vector2(u_top_left, v_top_left),
			Vector2(1, 0): Vector2(u_top_right, v_top_right),
			Vector2(0, 1): Vector2(u_bottom_left, v_bottom_left),
			Vector2(1, 1): Vector2(u_bottom_right, v_bottom_right),
		}
	
	
	static func from_bytes(bytes: ByteStream) -> Surface:
		var surface = Surface.new()
		
		surface.u_bottom_left = bytes.decode_float()
		surface.u_bottom_right = bytes.decode_float()
		surface.u_top_left = bytes.decode_float()
		surface.u_top_right = bytes.decode_float()
		surface.v_bottom_left = bytes.decode_float()
		surface.v_bottom_right = bytes.decode_float()
		surface.v_top_left = bytes.decode_float()
		surface.v_top_right = bytes.decode_float()
		surface.texture_index = bytes.decode_s16()
		surface.light_map_index = bytes.decode_s16()
		surface.vertex_color_blue = bytes.decode_u8()
		surface.vertex_color_green = bytes.decode_u8()
		surface.vertex_color_red = bytes.decode_u8()
		surface.vertex_color_alpha = bytes.decode_u8()
		
		#print(inst_to_dict(surface))
		return surface


class GroundMeshCube:
	## Byte Type: f32 [br]
	## Byte Length: 4
	## upper left?
	var bottom_left_height: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	## upper right?
	var bottom_right_height: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	## lower left?
	var top_left_height: float
	
	## Byte Type: f32 [br]
	## Byte Length: 4
	## lower right?
	var top_right_height: float
	
	## Byte Type: i32 [br]
	## Byte Length: 4
	## top surface index?
	var upwards_facing_surface_index: int
	
	## Byte Type: i32 [br]
	## Byte Length: 4
	## front surface index?
	var northern_wall_surface_index: int
	
	## Byte Type: i32 [br]
	## Byte Length: 4
	## right surface index?
	var eastern_wall_surface_index: int
	
	
	static func from_bytes(bytes: ByteStream) -> GroundMeshCube:
		var mesh = GroundMeshCube.new()
		
		mesh.bottom_left_height = bytes.decode_float()
		mesh.bottom_right_height = bytes.decode_float()
		mesh.top_left_height = bytes.decode_float()
		mesh.top_right_height = bytes.decode_float()
		mesh.upwards_facing_surface_index = bytes.decode_s32()
		mesh.northern_wall_surface_index = bytes.decode_s32()
		mesh.eastern_wall_surface_index = bytes.decode_s32()
		
		#print(inst_to_dict(mesh))
		return mesh