[486] | 1 | // Copyright (C) 2016-2016 ChaosForge Ltd
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
| 6 |
|
---|
| 7 | #include "nv/engine/animation.hh"
|
---|
| 8 |
|
---|
| 9 | #include "nv/stl/range.hh"
|
---|
| 10 | #include "nv/lua/lua_nova.hh"
|
---|
| 11 | #include "nv/io/c_file_system.hh"
|
---|
| 12 | #include "nv/formats/nmd_loader.hh"
|
---|
| 13 |
|
---|
| 14 | using namespace nv;
|
---|
| 15 |
|
---|
| 16 | nv::resource< animator_data > nv::animator_manager::load_animator( const nv::string_view& id, nv::pose_data_set* poses )
|
---|
| 17 | {
|
---|
| 18 | lua::table_guard table( m_lua, lua::path( "animators", id ) );
|
---|
| 19 | return load_animator( table, id, poses );
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | bool nv::animator_manager::load_resource( nv::lua::table_guard& table, nv::shash64 id )
|
---|
| 23 | {
|
---|
| 24 | return load_animator( table, id, nullptr ).is_valid();
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | nv::resource< animator_data > nv::animator_manager::load_animator( nv::lua::table_guard& table, nv::shash64 id, nv::pose_data_set* poses /*= nullptr */ )
|
---|
| 28 | {
|
---|
| 29 | uint32 count = table.get_size();
|
---|
| 30 | if ( count == 0 )
|
---|
| 31 | {
|
---|
| 32 | return nv::resource< animator_data >();
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | nv::animator_data* animator = new nv::animator_data;
|
---|
[539] | 36 | animator->id = table["id"].get_string64();
|
---|
[486] | 37 | animator->poses = poses;
|
---|
| 38 | if ( poses == nullptr )
|
---|
| 39 | {
|
---|
[539] | 40 | string128 poses_path = table["poses"].get_string128();
|
---|
[486] | 41 | if ( !poses_path.empty() )
|
---|
| 42 | {
|
---|
| 43 | nv::c_file_system fs;
|
---|
[508] | 44 | nv::stream* poses_file = open_stream( fs, poses_path );
|
---|
| 45 | NV_ASSERT_ALWAYS( poses_file, "CANT FIND POSES FILE!" );
|
---|
[486] | 46 | nv::nmd_loader* ploader = new nv::nmd_loader( nullptr );
|
---|
| 47 | ploader->load( *poses_file );
|
---|
| 48 | delete poses_file;
|
---|
| 49 |
|
---|
| 50 | animator->poses = ploader->release_pose_data_set();
|
---|
| 51 | NV_ASSERT( animator->poses, "NO POSES LOADED?" );
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | animator->layers.resize( count );
|
---|
| 56 | for ( auto layer_idx : nv::range( 1u, count ) )
|
---|
| 57 | {
|
---|
| 58 | nv::hash_store< shash64, uint32 > state_names;
|
---|
| 59 | nv::lua::table_guard layer_table( table, layer_idx );
|
---|
| 60 | nv::animator_layer_data& layer = animator->layers[layer_idx - 1];
|
---|
[539] | 61 | layer.name = layer_table["name"].get_string( m_strings );
|
---|
| 62 | layer.mask = layer_table["mask"].get_sint32( -1 );
|
---|
[486] | 63 | layer.def_state = -1;
|
---|
| 64 |
|
---|
[487] | 65 | if ( layer.mask >= 0 )
|
---|
[486] | 66 | {
|
---|
| 67 | const auto& tree = animator->poses->get_tree();
|
---|
| 68 | layer.mask_vector.resize( tree.size(), false );
|
---|
[487] | 69 | fill_mask_vector_rec( layer.mask_vector, tree, uint32( layer.mask ) );
|
---|
[486] | 70 | }
|
---|
| 71 |
|
---|
[539] | 72 | if ( layer_table["states"].is_table() )
|
---|
[486] | 73 | {
|
---|
| 74 | nv::lua::table_guard states_table( layer_table, "states" );
|
---|
| 75 | uint32 state_count = states_table.get_size();
|
---|
| 76 | if ( state_count > 0 )
|
---|
| 77 | {
|
---|
| 78 | layer.states.resize( state_count );
|
---|
| 79 |
|
---|
| 80 | // Two passes for transition name resolution
|
---|
| 81 | for ( auto state_idx : nv::range( 1u, state_count ) )
|
---|
| 82 | {
|
---|
| 83 | nv::lua::table_guard state_table( states_table, state_idx );
|
---|
| 84 | nv::animator_state_data& state = layer.states[state_idx - 1];
|
---|
| 85 |
|
---|
[539] | 86 | state.name = state_table["name"].get_string( m_strings );
|
---|
[486] | 87 | NV_ASSERT( state.name, "Animation state without name is invalid!" );
|
---|
| 88 | state_names[state.name] = state_idx - 1;
|
---|
[539] | 89 | state.loop = state_table["loop"].get_bool( true );
|
---|
| 90 | state.duration = state_table["duration"].get_f32();
|
---|
| 91 | state.interp = state_table["interpolation"].get_enum( nv::interpolation::SPHERICAL );
|
---|
[486] | 92 |
|
---|
[539] | 93 | shash64 pose_set = state_table["pose_set"].get_shash64();
|
---|
[486] | 94 | if ( pose_set )
|
---|
| 95 | {
|
---|
| 96 | auto it = animator->poses->m_sets.find( pose_set );
|
---|
| 97 | NV_ASSERT( it->second.name, "POSE NOT FOUND" );
|
---|
| 98 | uint32 pid = it->second.start;
|
---|
| 99 | uint32 pose_count = it->second.count;
|
---|
[539] | 100 | uint32 start = state_table["start"].get_uint32();
|
---|
| 101 | uint32 stop = state_table["stop"].get_uint32( pose_count - 1 );
|
---|
[486] | 102 | for ( nv::uint32 i = pid + start; i < pid + stop + 1; ++i )
|
---|
| 103 | {
|
---|
| 104 | state.poses.push_back( nv::animator_pose_data{ i/*, 0.0f*/ } );
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | NV_ASSERT( state.poses.size() > 0, "POSES EMPTY!" );
|
---|
| 108 | state.base_pose = state.poses[0].pose;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | // Second pass for transitions only
|
---|
| 113 | for ( auto state_idx : nv::range( 1u, state_count ) )
|
---|
| 114 | {
|
---|
| 115 | nv::animator_state_data& state = layer.states[state_idx - 1];
|
---|
| 116 | nv::lua::table_guard state_table( states_table, state_idx );
|
---|
| 117 |
|
---|
[539] | 118 | if ( state_table["transitions"].is_table() )
|
---|
[486] | 119 | {
|
---|
| 120 | nv::lua::table_guard transitions_table( state_table, "transitions" );
|
---|
| 121 | uint32 transition_count = transitions_table.get_size();
|
---|
| 122 | if ( transition_count > 0 )
|
---|
| 123 | {
|
---|
| 124 | for ( auto transition_idx : nv::range( 1u, transition_count ) )
|
---|
| 125 | {
|
---|
| 126 | nv::lua::table_guard transition_table( transitions_table, transition_idx );
|
---|
[539] | 127 | shash64 name = transition_table["name"].get_string( m_strings );
|
---|
| 128 | shash64 target_name = transition_table["target"].get_shash64();
|
---|
[486] | 129 | NV_ASSERT( name, "Transition state without name is invalid!" );
|
---|
| 130 | NV_ASSERT( target_name, "Transition state without name is invalid!" );
|
---|
| 131 | auto it = state_names.find( target_name );
|
---|
| 132 | NV_ASSERT( it != state_names.end(), "Transition target NOT FOUND!" );
|
---|
| 133 |
|
---|
| 134 | nv::animator_transition_data tr_data;
|
---|
| 135 | tr_data.target = it->second;
|
---|
[539] | 136 | tr_data.duration = transition_table["duration"].get_f32();
|
---|
| 137 | tr_data.interp = transition_table["interpolation"].get_enum( nv::interpolation::SPHERICAL );
|
---|
[486] | 138 | tr_data.easing = read_easing( transition_table );
|
---|
| 139 |
|
---|
| 140 | state.transitions[name] = tr_data;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 |
|
---|
[539] | 151 | shash64 def_state_name = layer_table["default"].get_shash64();
|
---|
[486] | 152 | if ( def_state_name )
|
---|
| 153 | {
|
---|
| 154 | auto dsi = state_names.find( def_state_name );
|
---|
| 155 | NV_ASSERT( dsi != state_names.end(), "Unknown default!" );
|
---|
| 156 | layer.def_state = sint32( dsi->second );
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | return add( id, animator );
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | nv::easing animator_manager::read_easing( nv::lua::table_guard& table )
|
---|
| 164 | {
|
---|
| 165 | nv::easing result;
|
---|
[539] | 166 | result.in = table["easing"].get_enum( nv::easing_type::NONE );
|
---|
| 167 | result.in = table["ease_in"].get_enum( result.in );
|
---|
| 168 | result.out = table["ease_out"].get_enum( nv::easing_type::NONE );
|
---|
[486] | 169 | if ( table.has_field( "ease_in_out" ) )
|
---|
| 170 | {
|
---|
[539] | 171 | result.in = result.out = table["ease_in_out"].get_enum( nv::easing_type::NONE );
|
---|
[486] | 172 | }
|
---|
| 173 | if ( result.in == nv::easing_type::NONE && result.out == nv::easing_type::NONE )
|
---|
| 174 | {
|
---|
| 175 | result.in = nv::easing_type::LINEAR;
|
---|
| 176 | result.out = nv::easing_type::NONE;
|
---|
| 177 | }
|
---|
| 178 | else
|
---|
| 179 | {
|
---|
| 180 | return result;
|
---|
| 181 | }
|
---|
| 182 | return result;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | nv::resource< nv::animator_data > animator_manager::create_animator( const nv::string_view& id, nv::pose_data_set* poses )
|
---|
| 186 | {
|
---|
| 187 | animator_data* data = new animator_data;
|
---|
| 188 | data->poses = poses;
|
---|
| 189 | return add( id, data );
|
---|
[487] | 190 | }
|
---|