-
Notifications
You must be signed in to change notification settings - Fork 5
/
CreatureModule.h
397 lines (295 loc) · 14.4 KB
/
CreatureModule.h
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
/******************************************************************************
* Creature Runtimes License
*
* Copyright (c) 2015, Kestrel Moon Studios
* All rights reserved.
*
* Preamble: This Agreement governs the relationship between Licensee and Kestrel Moon Studios(Hereinafter: Licensor).
* This Agreement sets the terms, rights, restrictions and obligations on using [Creature Runtimes] (hereinafter: The Software) created and owned by Licensor,
* as detailed herein:
* License Grant: Licensor hereby grants Licensee a Sublicensable, Non-assignable & non-transferable, Commercial, Royalty free,
* Including the rights to create but not distribute derivative works, Non-exclusive license, all with accordance with the terms set forth and
* other legal restrictions set forth in 3rd party software used while running Software.
* Limited: Licensee may use Software for the purpose of:
* Running Software on Licensee’s Website[s] and Server[s];
* Allowing 3rd Parties to run Software on Licensee’s Website[s] and Server[s];
* Publishing Software’s output to Licensee and 3rd Parties;
* Distribute verbatim copies of Software’s output (including compiled binaries);
* Modify Software to suit Licensee’s needs and specifications.
* Binary Restricted: Licensee may sublicense Software as a part of a larger work containing more than Software,
* distributed solely in Object or Binary form under a personal, non-sublicensable, limited license. Such redistribution shall be limited to unlimited codebases.
* Non Assignable & Non-Transferable: Licensee may not assign or transfer his rights and duties under this license.
* Commercial, Royalty Free: Licensee may use Software for any purpose, including paid-services, without any royalties
* Including the Right to Create Derivative Works: Licensee may create derivative works based on Software,
* including amending Software’s source code, modifying it, integrating it into a larger work or removing portions of Software,
* as long as no distribution of the derivative works is made
*
* THE RUNTIMES IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE RUNTIMES OR THE USE OR OTHER DEALINGS IN THE
* RUNTIMES.
*****************************************************************************/
#pragma once
#include <iostream>
#include <functional>
#include "MeshBone.h"
#include "gason.h"
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <sstream>
#include <vector>
#include <memory>
namespace CreatureModule {
class CreatureLoadDataPacket {
public:
CreatureLoadDataPacket()
{
src_chars = NULL;
}
~CreatureLoadDataPacket()
{
if(src_chars)
{
delete [] src_chars;
}
}
JsonValue base_node;
JsonAllocator allocator;
char * src_chars;
};
// Opens the json file and returns the entire json structure for a creature
// Use this to load your creatures and animatons
void LoadCreatureJSONData(const std::string& filename_in,
CreatureLoadDataPacket& load_data);
// Parses and creates a json from an input string and returns the entire json structure for a creature
// Use this to load your creatures and animatons
void LoadCreatureJSONDataFromString(const std::string& string_in,
CreatureLoadDataPacket& load_data);
struct CreatureUVSwapPacket {
CreatureUVSwapPacket(const glm::vec2& local_offset_in,
const glm::vec2& global_offset_in,
const glm::vec2& scale_in,
int tag_in)
{
local_offset = local_offset_in;
global_offset = global_offset_in;
scale = scale_in;
tag = tag_in;
}
glm::vec2 local_offset;
glm::vec2 global_offset;
glm::vec2 scale;
int tag;
};
// Class for the creature character
class Creature {
public:
Creature(CreatureLoadDataPacket& load_data);
virtual ~Creature();
// Returns the filename of the texture
const std::string& getTextureFilename() const;
// Fills entire mesh with (r,g,b,a) colours
void FillRenderColours(glm::uint8 r, glm::uint8 g, glm::uint8 b, glm::uint8 a);
// Returns the global indices
glm::uint32 * GetGlobalIndices();
// Returns the global rest points
glm::float32 * GetGlobalPts();
// Returns the global uvs
glm::float32 * GetGlobalUvs();
// Returns the render points
glm::float32 * GetRenderPts();
// Returns the render colours
glm::uint8 * GetRenderColours();
// Returns the total number of points
int GetTotalNumPoints() const;
// Returns the total number of indices
int GetTotalNumIndices() const;
// Returns the render composition object
meshRenderBoneComposition * GetRenderComposition();
// Get Available Animation names. Note that these animations might not have been loaded yet.
const std::vector<std::string>& GetAnimationNames() const;
// Returns the UV Swap Item Packet map
const std::map<std::string, std::vector<CreatureUVSwapPacket> >& GetUvSwapPackets() const;
// Sets up an Active UV Item Swap for a particular region
void SetActiveItemSwap(const std::string& region_name, int swap_idx);
// Removes an Active UV Item Swap from a particular region
void RemoveActiveItemSwap(const std::string& region_name);
// Returns the Actiev UV Swap Items
std::map<std::string, int>& GetActiveItemSwaps();
// Enables/Disables Anchor Points
void SetAnchorPointsActive(bool flag_in);
// Returns whether Anchor Points are active or not
bool GetAnchorPointsActive() const;
// Returns an Anchor Point based on an input animation clip name
glm::vec2 GetAnchorPoint(const std::string& anim_clip_name_in) const;
protected:
void LoadFromData(CreatureLoadDataPacket& load_data);
// mesh and skeleton data
glm::uint32 * global_indices;
glm::float32 * global_pts, * global_uvs;
glm::float32 * render_pts;
glm::uint8 * render_colours;
int total_num_pts, total_num_indices;
meshRenderBoneComposition * render_composition;
std::vector<std::string> animation_names;
std::map<std::string, std::vector<CreatureUVSwapPacket> > uv_swap_packets;
std::map<std::string, int> active_uv_swap_actions;
std::map<std::string, glm::vec2> anchor_point_map;
bool anchor_points_active;
};
// Class for animating the creature character
class CreatureAnimation {
public:
CreatureAnimation(CreatureLoadDataPacket& load_data,
const std::string& name_in);
virtual ~CreatureAnimation();
// Return the start time of the animation
float getStartTime() const;
// Return the end time of the animation
float getEndTime() const;
// Sets the start time of this animation
void setStartTime(int value_in);
// Sets the end time of this animation
void setEndTime(int value_in);
meshBoneCacheManager& getBonesCache();
meshDisplacementCacheManager& getDisplacementCache();
meshUVWarpCacheManager& getUVWarpCache();
meshOpacityCacheManager& getOpacityCache();
const std::string& getName() const;
bool hasCachePts() const;
std::vector<glm::float32 *>& getCachePts();
void clearCachePts();
void poseFromCachePts(float time_in, glm::float32 * target_pts, int num_pts);
protected:
void LoadFromData(const std::string& name_in,
CreatureLoadDataPacket& load_data);
int getIndexByTime(int time_in) const;
std::string name;
float start_time, end_time;
meshBoneCacheManager bones_cache;
meshDisplacementCacheManager displacement_cache;
meshUVWarpCacheManager uv_warp_cache;
meshOpacityCacheManager opacity_cache;
std::vector<glm::float32 *> cache_pts;
};
// Class for managing a collection of animations and a creature character
class CreatureManager {
public:
CreatureManager(std::shared_ptr<CreatureModule::Creature> target_creature_in);
virtual ~CreatureManager();
// Create an animation
void CreateAnimation(CreatureLoadDataPacket& load_data,
const std::string& name_in);
// Add an animation
void AddAnimation(std::shared_ptr<CreatureModule::CreatureAnimation> animation_in);
// Return an animation
CreatureModule::CreatureAnimation *
GetAnimation(const std::string name_in);
// Return the creature
CreatureModule::Creature *
GetCreature();
// Sets the current animation to be active by name
void SetActiveAnimationName(const std::string& name_in, bool check_already_active=false);
// Returns the name of the currently active animation
const std::string& GetActiveAnimationName() const;
// Returns the table of all animations
std::unordered_map<std::string, std::shared_ptr<CreatureModule::CreatureAnimation> >&
GetAllAnimations();
// Returns if animation is playing
bool GetIsPlaying() const;
// Sets whether the animation is playing
void SetIsPlaying(bool flag_in);
// Resets animation to start time
void ResetToStartTimes();
// Sets the run time of the animation
void setRunTime(float time_in);
// Increments the run time of the animation by a delta value
void increRunTime(float delta_in);
// Returns the current run time of the animation
float getRunTime() const;
// Returns the actual run time if auto blending is involved
float getActualRunTime() const;
// Runs a single step of the animation for a given delta timestep
void Update(float delta);
// Sets scaling for time
void SetTimeScale(float scale_in);
// Enables/Disables blending
void SetBlending(bool flag_in);
// Sets blending animation names
void SetBlendingAnimations(const std::string& name_1, const std::string& name_2);
// Sets the blending factor
void SetBlendingFactor(float value_in);
// Given a set of coordinates in local creature space,
// see if any bone is in contact
std::string IsContactBone(const glm::vec2& pt_in,
const glm::mat4& creature_xform,
float radius) const;
// Mirrors the model along the Y-Axis
void SetMirrorY(bool flag_in);
// Decides whether to use a custom time range or the default
// animation clip's time range
void SetUseCustomTimeRange(bool flag_in);
// Sets the values for the custom time range
void SetCustomTimeRange(int start_time_in, int end_time_in);
// Sets whether the animation loops or not
void SetShouldLoop(bool flag_in);
// Sets the callback to modify/override bone positions
void SetBonesOverrideCallback(std::function<void (std::unordered_map<std::string, meshBone *>&) >& callback_in);
// Creates point cache for animation
void MakePointCache(const std::string& animation_name_in, int gap_step);
// Clears point cache for animation
void ClearPointCache(const std::string& animation_name_in);
// Sets auto blending
void SetAutoBlending(bool flag_in);
// Uses auto blending to blend to the next animation
void AutoBlendTo(const std::string& animation_name_in, float blend_delta);
// Global switch to enable/disable point caching
void SetDoPointCache(bool flag_in);
// Returns whether to globally enable or disable point caching
bool GetDoPointCache() const;
protected:
float correctRunTime(float time_in, const std::string& animation_name);
std::string ProcessContactBone(const glm::vec2& pt_in,
float radius,
meshBone * bone_in) const;
void PoseCreature(const std::string& animation_name_in,
glm::float32 * target_pts,
float input_run_time);
void ProcessAutoBlending();
void increAutoBlendRuntimes(float delta_in);
glm::float32 * interpFloatArray(glm::float32 * first_list, glm::float32 * second_list, float factor, int array_size);
void ResetBlendTime(const std::string& name_in);
void UpdateRegionSwitches(const std::string& animation_name_in);
void PoseJustBones(const std::string& animation_name_in,
glm::float32 * target_pts,
float input_run_time);
void JustRunUVWarps(const std::string& animation_name_in, float input_run_time);
void RunUVItemSwap();
void AlterBonesByAnchor(std::unordered_map<std::string, meshBone *>& bones_map, const std::string& animation_name_in);
std::unordered_map<std::string, std::shared_ptr<CreatureModule::CreatureAnimation> > animations;
std::shared_ptr<CreatureModule::Creature> target_creature;
std::string active_animation_name;
bool is_playing;
float run_time;
float time_scale;
glm::float32 * blend_render_pts[2];
bool do_blending;
float blending_factor;
std::string active_blend_animation_names[2];
std::unordered_map<std::string, float> active_blend_run_times;
bool mirror_y;
bool use_custom_time_range;
int custom_start_time, custom_end_time;
bool should_loop;
bool do_auto_blending;
std::string auto_blend_names[2];
float auto_blend_delta;
bool do_point_caching;
std::function<void (std::unordered_map<std::string, meshBone *>&) > bones_override_callback;
};
};