/
CreatureRenderModule.cs
443 lines (399 loc) · 15.4 KB
/
CreatureRenderModule.cs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using System;
using System.IO;
using System.Collections.Generic;
using CreatureModule;
using MeshBoneUtil;
using UnityEngine;
namespace CreatureModule
{
public class CreatureMeshModifier
{
public int num_indices = 0;
public int num_pts = 0;
public int m_numIndices = 0;
public int m_maxIndice = -1;
public bool m_isValid = false;
public CreatureMeshModifier(int num_indices_in, int num_pts_in)
{
num_indices = num_indices_in;
num_pts = num_pts_in;
}
public virtual void initData(CreatureManager creature_manager, CreatureAsset creature_asset)
{
}
public virtual void update(
CreatureManager creature_manager,
bool counter_clockwise,
ref Vector3[] vertices,
ref Vector3[] normals,
ref Vector4[] tangents,
ref Color32[] colors,
ref Vector2[] uvs,
CreatureAsset creature_asset,
float region_overlap_z_delta,
bool skin_swap_active,
string skin_swap_name,
string active_animation_name,
ref List<int> final_indices,
ref List<int> final_skin_swap_indices,
ref int[] triangles,
ref int[] skin_swap_triangles)
{
}
}
public class CreatureRenderModule
{
public static Mesh createMesh()
{
Mesh new_mesh = new Mesh();
new_mesh.name = "Creature Mesh Object";
new_mesh.hideFlags = HideFlags.HideAndDontSave;
new_mesh.MarkDynamic();
return new_mesh;
}
public static void SetIndexBuffer(List<int> input_list, int[] output_array, bool do_counterclockwise)
{
if (!do_counterclockwise)
{
for (int i = 0; i < input_list.Count; i++)
{
output_array[i] = input_list[i];
}
}
else
{
for (int i = 0; i < input_list.Count; i += 3)
{
output_array[i] = input_list[i];
output_array[i + 1] = input_list[i + 2];
output_array[i + 2] = input_list[i + 1];
}
}
}
public static void SetIndexBufferWithResizing(List<int> input_list, ref int[] output_array, int num_elems, bool do_counterclockwise)
{
output_array = new int[num_elems];
if (!do_counterclockwise)
{
for (int i = 0; i < num_elems; i++)
{
output_array[i] = input_list[i];
}
}
else
{
for (int i = 0; i < num_elems; i += 3)
{
output_array[i] = input_list[i];
output_array[i + 1] = input_list[i + 2];
output_array[i + 2] = input_list[i + 1];
}
}
}
public static bool shouldSkinSwap(CreatureAsset creature_asset, bool skin_swap_active, ref int[] skin_swap_triangles)
{
return (creature_asset.creature_meta_data != null) &&
skin_swap_active &&
(skin_swap_triangles != null);
}
public static bool AddSkinSwap(CreatureAsset creature_asset, String swap_name, HashSet<String> swap_set)
{
if (creature_asset.creature_meta_data == null)
{
return false;
}
if (creature_asset.creature_meta_data.skin_swaps.ContainsKey(swap_name))
{
return false;
}
creature_asset.creature_meta_data.skin_swaps[swap_name] = swap_set;
return true;
}
public static void EnableSkinSwap(
String swap_name_in,
bool active,
CreatureManager creature_manager,
CreatureAsset creature_asset,
ref bool skin_swap_active,
ref string skin_swap_name,
ref int[] skin_swap_triangles,
ref List<int> final_skin_swap_indices)
{
skin_swap_active = active;
if (!skin_swap_active)
{
skin_swap_name = "";
skin_swap_triangles = null;
final_skin_swap_indices = null;
}
else
{
skin_swap_name = swap_name_in;
if (creature_asset.creature_meta_data != null)
{
final_skin_swap_indices = new List<int>();
creature_asset.creature_meta_data.buildSkinSwapIndices(
skin_swap_name,
creature_manager.GetCreature().render_composition,
final_skin_swap_indices);
skin_swap_triangles = new int[final_skin_swap_indices.Count];
}
}
}
public static void CreateRenderingData(
CreatureManager creature_manager,
ref Vector3[] vertices,
ref Vector3[] normals,
ref Vector4[] tangents,
ref Color32[] colors,
ref Vector2[] uvs,
ref int[] triangles,
ref List<int> final_indices,
CreatureMeshModifier mesh_modifier = null)
{
if (mesh_modifier != null)
{
vertices = new Vector3[mesh_modifier.num_pts];
normals = new Vector3[mesh_modifier.num_pts];
tangents = new Vector4[mesh_modifier.num_pts];
colors = new Color32[mesh_modifier.num_pts];
uvs = new Vector2[mesh_modifier.num_pts];
triangles = new int[mesh_modifier.m_maxIndice];
final_indices = new List<int>(new int[mesh_modifier.m_maxIndice]);
}
else
{
vertices = new Vector3[creature_manager.target_creature.total_num_pts];
normals = new Vector3[creature_manager.target_creature.total_num_pts];
tangents = new Vector4[creature_manager.target_creature.total_num_pts];
colors = new Color32[creature_manager.target_creature.total_num_pts];
uvs = new Vector2[creature_manager.target_creature.total_num_pts];
triangles = new int[creature_manager.target_creature.total_num_indices];
final_indices = new List<int>(new int[creature_manager.target_creature.total_num_indices]);
}
}
public static void UpdateRenderingData(
CreatureManager creature_manager,
bool counter_clockwise,
ref Vector3[] vertices,
ref Vector3[] normals,
ref Vector4[] tangents,
ref Color32[] colors,
ref Vector2[] uvs,
CreatureAsset creature_asset,
bool skin_swap_active,
string active_animation_name,
ref List<int> final_indices,
ref List<int> final_skin_swap_indices,
ref int[] triangles,
ref int[] skin_swap_triangles)
{
int pt_index = 0;
int uv_index = 0;
int color_index = 0;
List<float> render_pts = creature_manager.target_creature.render_pts;
List<float> render_uvs = creature_manager.target_creature.global_uvs;
List<byte> render_colors = creature_manager.target_creature.render_colours;
float normal_z = 1.0f;
if (counter_clockwise)
{
normal_z = -1.0f;
}
for (int i = 0; i < creature_manager.target_creature.total_num_pts; i++)
{
vertices[i].x = render_pts[pt_index + 0];
vertices[i].y = render_pts[pt_index + 1];
vertices[i].z = render_pts[pt_index + 2];
normals[i].x = 0;
normals[i].y = 0;
normals[i].z = normal_z;
tangents[i].x = 1.0f;
tangents[i].y = 0;
tangents[i].z = 0;
uvs[i].x = render_uvs[uv_index + 0];
uvs[i].y = 1.0f - render_uvs[uv_index + 1];
colors[i].r = render_colors[color_index + 0];
colors[i].g = render_colors[color_index + 1];
colors[i].b = render_colors[color_index + 2];
colors[i].a = render_colors[color_index + 3];
pt_index += 3;
uv_index += 2;
color_index += 4;
}
List<int> render_indices = creature_manager.target_creature.global_indices;
var real_run_time = creature_manager.run_time;
if (creature_manager.do_blending &&
creature_manager.active_blend_run_times.ContainsKey(active_animation_name))
{
real_run_time = creature_manager.active_blend_run_times[active_animation_name];
}
bool is_animate_order = false;
if (creature_asset.creature_meta_data != null)
{
is_animate_order = creature_asset.creature_meta_data.hasAnimatedOrder(active_animation_name, (int)real_run_time);
}
// index re-ordering
if (creature_asset.creature_meta_data != null)
{
if (is_animate_order)
{
// do index re-ordering
creature_asset.creature_meta_data.updateIndicesAndPoints(
final_indices,
render_indices,
render_pts,
0,
creature_manager.target_creature.total_num_indices,
creature_manager.target_creature.total_num_pts,
active_animation_name,
skin_swap_active,
(int)real_run_time);
if (skin_swap_active)
{
for (int i = 0; i < final_skin_swap_indices.Count; i++)
{
final_skin_swap_indices[i] = final_indices[i];
}
SetIndexBuffer(final_skin_swap_indices, skin_swap_triangles, counter_clockwise);
}
else
{
SetIndexBuffer(final_indices, triangles, counter_clockwise);
}
}
}
if (shouldSkinSwap(creature_asset, skin_swap_active, ref skin_swap_triangles) && (is_animate_order == false))
{
// Skin Swap with no Animated Ordering
SetIndexBuffer(final_skin_swap_indices, skin_swap_triangles, counter_clockwise);
}
else if (is_animate_order == false)
{
// plain copy
for (int i = 0; i < render_indices.Count; i++)
{
final_indices[i] = render_indices[i];
}
SetIndexBuffer(final_indices, triangles, counter_clockwise);
}
}
public static void UpdateWithParticlesForRenderingData(
CreatureParticlesAsset particles_asset,
CreatureManager creature_manager,
bool counter_clockwise,
ref Vector3[] vertices,
ref Vector3[] normals,
ref Vector4[] tangents,
ref Color32[] colors,
ref Vector2[] uvs,
CreatureAsset creature_asset,
float region_overlap_z_delta,
bool skin_swap_active,
string skin_swap_name,
string active_animation_name,
ref List<int> final_indices,
ref List<int> final_skin_swap_indices,
ref int[] triangles,
ref int[] skin_swap_triangles)
{
particles_asset.mesh_modifier.update(
creature_manager,
counter_clockwise,
ref vertices,
ref normals,
ref tangents,
ref colors,
ref uvs,
creature_asset,
region_overlap_z_delta,
skin_swap_active,
skin_swap_name,
active_animation_name,
ref final_indices,
ref final_skin_swap_indices,
ref triangles,
ref skin_swap_triangles);
float normal_z = 1.0f;
if (counter_clockwise)
{
normal_z = -1.0f;
}
for (int i = 0; i < vertices.Length; i++)
{
normals[i].x = 0;
normals[i].y = 0;
normals[i].z = normal_z;
tangents[i].x = 1.0f;
tangents[i].y = 0;
tangents[i].z = 0;
}
for (int i =0; i < triangles.Length; i++)
{
triangles[i] = 0;
}
SetIndexBuffer(final_indices, triangles, counter_clockwise);
}
public static void UpdateTime(
CreatureManager creature_manager,
CreatureGameController game_controller,
CreatureMetaData meta_data,
string active_animation_name,
float local_time_scale,
float region_offsets_z,
bool should_loop,
ref float local_time)
{
if (active_animation_name == null || active_animation_name.Length == 0)
{
return;
}
bool morph_targets_valid = false;
if (game_controller != null)
{
if ((meta_data != null) && game_controller.morph_targets_active)
{
morph_targets_valid = meta_data.morph_data.isValid();
}
}
var old_time = creature_manager.getActualRuntime();
float time_delta = (Time.deltaTime * local_time_scale);
creature_manager.region_offsets_z = region_offsets_z;
creature_manager.should_loop = should_loop;
if (morph_targets_valid)
{
game_controller.computeMorphTargetsPt();
meta_data.updateMorphStep(creature_manager, time_delta);
}
else
{
creature_manager.Update(time_delta);
}
local_time = creature_manager.getActualRuntime();
bool reached_anim_end = false;
if ((local_time < old_time) && (game_controller != null))
{
game_controller.AnimClipFrameResetEvent();
reached_anim_end = true;
}
if (local_time >= creature_manager.GetAnimation(active_animation_name).end_time)
{
reached_anim_end = true;
}
if (reached_anim_end && (game_controller != null))
{
game_controller.AnimationReachedEnd(active_animation_name);
}
}
public static void debugDrawBones(MeshBone bone_in)
{
XnaGeometry.Vector4 pt1 = bone_in.world_start_pt;
XnaGeometry.Vector4 pt2 = bone_in.world_end_pt;
Debug.DrawLine(new Vector3((float)pt1.X, (float)pt1.Y, 0),
new Vector3((float)pt2.X, (float)pt2.Y, 0), Color.white);
foreach (MeshBone cur_child in bone_in.children)
{
debugDrawBones(cur_child);
}
}
}
}