[520] | 1 | // Copyright (C) 2014-2016 ChaosForge Ltd
|
---|
[395] | 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.
|
---|
[319] | 6 |
|
---|
[320] | 7 | #include "nv/engine/particle_engine.hh"
|
---|
[306] | 8 |
|
---|
| 9 | #include <nv/interface/device.hh>
|
---|
[319] | 10 | #include <nv/core/random.hh>
|
---|
[374] | 11 | #include <nv/stl/utility.hh>
|
---|
[452] | 12 | #include <nv/lua/lua_math.hh>
|
---|
[319] | 13 | #include <nv/core/logging.hh>
|
---|
[306] | 14 |
|
---|
[518] | 15 | nv::hash_store< nv::shash64, nv::particle_emitter_func > nv::particle_engine::m_emitters;
|
---|
| 16 | nv::hash_store< nv::shash64, nv::particle_affector_funcs > nv::particle_engine::m_affectors;
|
---|
[306] | 17 |
|
---|
[519] | 18 | nv::vector< nv::particle_emitter_func >* nv::particle_engine::m_debug_emitter_list;
|
---|
| 19 | nv::vector< nv::particle_affector_func >* nv::particle_engine::m_debug_affector_list;
|
---|
| 20 |
|
---|
| 21 |
|
---|
[518] | 22 | nv::hash_map< nv::particle_emitter_func, nv::const_string >* nv::particle_engine::m_debug_emitter_names = nullptr;
|
---|
| 23 | nv::hash_map< nv::particle_affector_func, nv::const_string >* nv::particle_engine::m_debug_affector_names = nullptr;
|
---|
| 24 | nv::hash_map< nv::particle_affector_func, nv::type_entry* >* nv::particle_engine::m_debug_affector_types = nullptr;
|
---|
| 25 |
|
---|
[312] | 26 | using namespace nv;
|
---|
| 27 |
|
---|
[520] | 28 | static void nv_particle_emitter_point( random_base*, const particle_emitter_data*, particle* p, uint32 count )
|
---|
[312] | 29 | {
|
---|
| 30 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 31 | {
|
---|
| 32 | p[i].position = vec3();
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[520] | 36 | static void nv_particle_emitter_box( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
[312] | 37 | {
|
---|
| 38 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 39 | {
|
---|
| 40 | p[i].position =
|
---|
[520] | 41 | r->frange( -pe->hextents[0], pe->hextents[0] ) * pe->cdir +
|
---|
| 42 | r->frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
| 43 | r->frange( -pe->hextents[2], pe->hextents[2] ) * pe->odir;
|
---|
[312] | 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[520] | 47 | static void nv_particle_emitter_cylinder( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
[312] | 48 | {
|
---|
| 49 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 50 | {
|
---|
[520] | 51 | vec2 rellipse( r->disk_point( pe->precise ) * pe->extents[0] );
|
---|
[312] | 52 | p[i].position =
|
---|
| 53 | rellipse.x * pe->cdir +
|
---|
[520] | 54 | r->frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
[312] | 55 | rellipse.y * pe->odir;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[520] | 59 | static void nv_particle_emitter_sphere( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
[312] | 60 | {
|
---|
| 61 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 62 | {
|
---|
[520] | 63 | vec3 rsphere = r->sphere_point( pe->precise ) * pe->extents[0];
|
---|
[312] | 64 | p[i].position =
|
---|
| 65 | rsphere.x * pe->cdir +
|
---|
| 66 | rsphere.y * pe->dir +
|
---|
| 67 | rsphere.z * pe->odir;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[520] | 71 | static void nv_particle_emitter_cylindroid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
[312] | 72 | {
|
---|
| 73 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 74 | {
|
---|
[520] | 75 | vec2 rellipse = r->ellipse_point( vec2( pe->hextents[0], pe->hextents[2] ), pe->precise );
|
---|
[312] | 76 | p[i].position =
|
---|
| 77 | rellipse.x * pe->cdir +
|
---|
[520] | 78 | r->frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
[312] | 79 | rellipse.y * pe->odir;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[520] | 83 | static void nv_particle_emitter_ellipsoid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
[312] | 84 | {
|
---|
| 85 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 86 | {
|
---|
[520] | 87 | vec3 rsphere = r->ellipsoid_point( pe->hextents, pe->precise );
|
---|
[312] | 88 | p[i].position =
|
---|
| 89 | rsphere.x * pe->cdir +
|
---|
| 90 | rsphere.y * pe->dir +
|
---|
| 91 | rsphere.z * pe->odir;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[520] | 95 | static void nv_particle_emitter_hollow_cylinder( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
[312] | 96 | {
|
---|
| 97 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 98 | {
|
---|
[520] | 99 | vec2 rellipse = r->hollow_disk_point(
|
---|
[312] | 100 | pe->ihextents[0],
|
---|
| 101 | pe->hextents[0],
|
---|
| 102 | pe->precise );
|
---|
| 103 | p[i].position =
|
---|
| 104 | rellipse.x * pe->cdir +
|
---|
[520] | 105 | r->frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
[312] | 106 | rellipse.y * pe->odir;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[520] | 110 | static void nv_particle_emitter_hollow_sphere( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
[312] | 111 | {
|
---|
| 112 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 113 | {
|
---|
[520] | 114 | vec3 rellipse = r->hollow_sphere_point( pe->ihextents[0], pe->hextents[0], pe->precise );
|
---|
[312] | 115 | p[i].position =
|
---|
| 116 | rellipse.x * pe->cdir +
|
---|
| 117 | rellipse.y * pe->dir +
|
---|
| 118 | rellipse.z * pe->odir;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[520] | 122 | static void nv_particle_emitter_hollow_cylindroid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
[312] | 123 | {
|
---|
| 124 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 125 | {
|
---|
[520] | 126 | vec2 rellipse = r->hollow_ellipse_point(
|
---|
[312] | 127 | vec2( pe->ihextents[0], pe->ihextents[2] ),
|
---|
| 128 | vec2( pe->hextents[0], pe->hextents[2] ),
|
---|
| 129 | pe->precise );
|
---|
| 130 | p[i].position =
|
---|
| 131 | rellipse.x * pe->cdir +
|
---|
[520] | 132 | r->frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
[312] | 133 | rellipse.y * pe->odir;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[520] | 137 | static void nv_particle_emitter_hollow_ellipsoid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
[312] | 138 | {
|
---|
| 139 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 140 | {
|
---|
[520] | 141 | vec3 rellipse = r->hollow_ellipsoid_point( pe->ihextents, pe->hextents, pe->precise );
|
---|
[312] | 142 | p[i].position =
|
---|
| 143 | rellipse.x * pe->cdir +
|
---|
| 144 | rellipse.y * pe->dir +
|
---|
| 145 | rellipse.z * pe->odir;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | struct nvpe_linear_force_data
|
---|
| 150 | {
|
---|
| 151 | nv::vec3 force_vector;
|
---|
| 152 | bool average;
|
---|
| 153 | };
|
---|
| 154 |
|
---|
[519] | 155 | NV_RTTI_DECLARE( nvpe_linear_force_data )
|
---|
| 156 |
|
---|
[312] | 157 | static bool nv_particle_affector_linear_force_init( lua::table_guard* table, particle_affector_data* data )
|
---|
| 158 | {
|
---|
[406] | 159 | nvpe_linear_force_data* datap = reinterpret_cast<nvpe_linear_force_data*>( data->paramters );
|
---|
[312] | 160 | datap->force_vector = table->get<vec3>("force_vector", vec3() );
|
---|
| 161 | datap->average = table->get<bool>("average", false );
|
---|
| 162 | return true;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | static void nv_particle_affector_linear_force( const particle_affector_data* data, particle* p, float factor, uint32 count )
|
---|
| 166 | {
|
---|
[406] | 167 | const nvpe_linear_force_data* datap = reinterpret_cast<const nvpe_linear_force_data*>( data->paramters );
|
---|
[312] | 168 | if ( datap->average )
|
---|
| 169 | {
|
---|
[500] | 170 | float norm_factor = nv::min( factor, 1.0f, p->lifetime );
|
---|
[312] | 171 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 172 | p[i].velocity = datap->force_vector * norm_factor + p[i].velocity * ( 1.0f - norm_factor );
|
---|
| 173 | }
|
---|
| 174 | else
|
---|
| 175 | {
|
---|
[500] | 176 | vec3 scvector = datap->force_vector * nv::min( factor, p->lifetime );
|
---|
[312] | 177 | for ( uint32 i = 0; i < count; ++i ) p[i].velocity += scvector;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | struct nvpe_deflector_plane_data
|
---|
| 182 | {
|
---|
| 183 | nv::vec3 plane_point;
|
---|
| 184 | nv::vec3 plane_normal;
|
---|
| 185 | float bounce;
|
---|
| 186 | float distance;
|
---|
| 187 | };
|
---|
| 188 |
|
---|
[519] | 189 | NV_RTTI_DECLARE( nvpe_deflector_plane_data )
|
---|
| 190 |
|
---|
[312] | 191 | static bool nv_particle_affector_deflector_plane_init( lua::table_guard* table, particle_affector_data* data )
|
---|
| 192 | {
|
---|
[406] | 193 | nvpe_deflector_plane_data* datap = reinterpret_cast<nvpe_deflector_plane_data*>( data->paramters );
|
---|
[312] | 194 | datap->plane_point = table->get<vec3>("plane_point", vec3() );
|
---|
| 195 | datap->plane_normal = table->get<vec3>("plane_normal", vec3(0.0f,1.0f,0.0f) );
|
---|
| 196 | datap->plane_normal = normalize_safe( datap->plane_normal, vec3(0.0f,1.0f,0.0f) );
|
---|
| 197 | datap->bounce = table->get<float>("bounce", 0.0f );
|
---|
[534] | 198 | datap->distance = -math::dot( datap->plane_normal, datap->plane_point ) / static_cast<float>( math::sqrt( math::dot( datap->plane_normal, datap->plane_normal ) ) );
|
---|
[312] | 199 | return true;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | static void nv_particle_affector_deflector_plane( const particle_affector_data* data, particle* p, float factor, uint32 count )
|
---|
| 203 | {
|
---|
[406] | 204 | const nvpe_deflector_plane_data* datap = reinterpret_cast<const nvpe_deflector_plane_data*>( data->paramters );
|
---|
[312] | 205 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 206 | {
|
---|
| 207 | particle& pt = p[i];
|
---|
[500] | 208 | vec3 direction = pt.velocity * nv::min( factor, p->lifetime );
|
---|
[454] | 209 | if ( math::dot( datap->plane_normal, pt.position + direction ) + datap->distance <= 0.0f )
|
---|
[312] | 210 | {
|
---|
[454] | 211 | float val = math::dot( datap->plane_normal, pt.position ) + datap->distance;
|
---|
[312] | 212 | if ( val > 0.0f )
|
---|
| 213 | {
|
---|
[454] | 214 | vec3 part_dir = direction * ( -val / math::dot( datap->plane_normal, direction ) );
|
---|
[312] | 215 | pt.position = pt.position + part_dir + ( part_dir - direction ) * datap->bounce;
|
---|
[454] | 216 | pt.velocity = math::reflect( pt.velocity, datap->plane_normal ) * datap->bounce;
|
---|
[312] | 217 | }
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | struct nvpe_color_fader_data
|
---|
| 223 | {
|
---|
| 224 | nv::vec4 adjustment;
|
---|
| 225 | };
|
---|
| 226 |
|
---|
[519] | 227 | NV_RTTI_DECLARE( nvpe_color_fader_data )
|
---|
| 228 |
|
---|
[312] | 229 | static bool nv_particle_affector_color_fader_init( lua::table_guard* table, particle_affector_data* data )
|
---|
| 230 | {
|
---|
[406] | 231 | nvpe_color_fader_data* datap = reinterpret_cast<nvpe_color_fader_data*>( data->paramters );
|
---|
[312] | 232 | datap->adjustment = table->get<vec4>("adjustment", vec4() );
|
---|
| 233 | return true;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | static void nv_particle_affector_color_fader( const particle_affector_data* data, particle* p, float factor, uint32 count )
|
---|
| 237 | {
|
---|
[406] | 238 | const nvpe_color_fader_data* datap = reinterpret_cast<const nvpe_color_fader_data*>( data->paramters );
|
---|
[500] | 239 | vec4 adjustment = datap->adjustment * nv::min( factor, p->lifetime );
|
---|
[312] | 240 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 241 | {
|
---|
[454] | 242 | p[i].color = math::clamp( p[i].color + adjustment, 0.0f, 1.0f );
|
---|
[312] | 243 | }
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | struct nvpe_scaler_data
|
---|
| 247 | {
|
---|
| 248 | nv::vec2 adjustment;
|
---|
| 249 | };
|
---|
| 250 |
|
---|
[519] | 251 | NV_RTTI_DECLARE( nvpe_scaler_data )
|
---|
| 252 |
|
---|
[312] | 253 | static bool nv_particle_affector_scaler_init( lua::table_guard* table, particle_affector_data* data )
|
---|
| 254 | {
|
---|
[406] | 255 | nvpe_scaler_data* datap = reinterpret_cast<nvpe_scaler_data*>( data->paramters );
|
---|
[312] | 256 | float rate = table->get<float>("rate", 0.0f );
|
---|
| 257 | datap->adjustment = table->get<vec2>("adjustment", vec2(rate,rate) );
|
---|
| 258 | return true;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | static void nv_particle_affector_scaler( const particle_affector_data* data, particle* p, float factor, uint32 count )
|
---|
| 262 | {
|
---|
[406] | 263 | const nvpe_scaler_data* datap = reinterpret_cast<const nvpe_scaler_data*>( data->paramters );
|
---|
[500] | 264 | vec2 adjustment = datap->adjustment * nv::min( factor, p->lifetime );
|
---|
[312] | 265 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 266 | {
|
---|
[454] | 267 | p[i].size = math::max( p[i].size + adjustment, vec2() );
|
---|
[312] | 268 | }
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[520] | 271 | nv::particle_engine::particle_engine( context* a_context, random_base* rng, particle_group_manager* groups, bool debug_data )
|
---|
[306] | 272 | {
|
---|
[520] | 273 | m_context = a_context;
|
---|
| 274 | m_pgm = groups;
|
---|
| 275 | m_rng = rng;
|
---|
| 276 | if ( m_rng == nullptr )
|
---|
| 277 | m_rng = &random::get();
|
---|
| 278 |
|
---|
[518] | 279 | if ( debug_data )
|
---|
[306] | 280 | {
|
---|
[518] | 281 | m_debug_emitter_names = new nv::hash_map< nv::particle_emitter_func, nv::const_string >;
|
---|
| 282 | m_debug_affector_names = new nv::hash_map< nv::particle_affector_func, nv::const_string >;
|
---|
[519] | 283 | m_debug_emitter_list = new nv::vector< nv::particle_emitter_func >;
|
---|
| 284 | m_debug_affector_list = new nv::vector< nv::particle_affector_func >;
|
---|
[306] | 285 | }
|
---|
| 286 |
|
---|
[518] | 287 | register_standard_emitters();
|
---|
[312] | 288 | register_standard_affectors();
|
---|
[306] | 289 | }
|
---|
| 290 |
|
---|
[520] | 291 | nv::particle_system nv::particle_engine::create_system( const particle_system_data* data, particle_group group )
|
---|
[306] | 292 | {
|
---|
[520] | 293 | if ( !m_pgm->ref( group ) )
|
---|
| 294 | return nv::particle_system();
|
---|
| 295 |
|
---|
[306] | 296 | particle_system result = m_systems.create();
|
---|
| 297 | particle_system_info* info = m_systems.get( result );
|
---|
[499] | 298 | info->group = group;
|
---|
[518] | 299 | info->data = data;
|
---|
| 300 | uint32 ecount = data->emitter_count;
|
---|
[306] | 301 | for ( uint32 i = 0; i < ecount; ++i )
|
---|
| 302 | {
|
---|
[518] | 303 | info->emitters[i].active = true;
|
---|
| 304 | if ( data->emitters[i].duration_max == 0.0f )
|
---|
| 305 | info->emitters[i].pause = 0;
|
---|
[353] | 306 | else
|
---|
[520] | 307 | info->emitters[i].pause = m_rng->frange( data->emitters[i].duration_min, data->emitters[i].duration_max );
|
---|
[306] | 308 | }
|
---|
| 309 |
|
---|
| 310 | info->count = 0;
|
---|
[518] | 311 | info->particles = new particle[data->quota];
|
---|
[306] | 312 |
|
---|
| 313 | return result;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[522] | 316 | void nv::particle_engine::set_on_collide( particle_system system, const particle_on_collide_func& f )
|
---|
| 317 | {
|
---|
| 318 | particle_system_info* info = m_systems.get( system );
|
---|
| 319 | if ( info )
|
---|
| 320 | info->on_collide = f;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[520] | 323 | nv::particle_system nv::particle_engine::create_system( resource< nv::particle_system_data > rdata, particle_group group )
|
---|
[306] | 324 | {
|
---|
[518] | 325 | if ( auto data = rdata.lock() )
|
---|
| 326 | return create_system( &*data, group );
|
---|
| 327 | return {};
|
---|
[306] | 328 | }
|
---|
| 329 |
|
---|
[520] | 330 | void nv::particle_engine::prepare( particle_group group )
|
---|
[499] | 331 | {
|
---|
[520] | 332 | m_pgm->prepare( group );
|
---|
[499] | 333 | }
|
---|
| 334 |
|
---|
[520] | 335 | nv::particle_group nv::particle_engine::create_group( uint32 max_particles )
|
---|
[499] | 336 | {
|
---|
[520] | 337 | return m_pgm->create_group( max_particles );
|
---|
[499] | 338 | }
|
---|
| 339 |
|
---|
[306] | 340 | nv::particle_engine::~particle_engine()
|
---|
| 341 | {
|
---|
[353] | 342 | clear();
|
---|
[306] | 343 | }
|
---|
| 344 |
|
---|
[353] | 345 | void nv::particle_engine::reset()
|
---|
| 346 | {
|
---|
| 347 | clear();
|
---|
[518] | 348 | register_standard_emitters( );
|
---|
[353] | 349 | register_standard_affectors();
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | void nv::particle_engine::clear()
|
---|
| 353 | {
|
---|
| 354 | while ( m_systems.size() > 0 )
|
---|
| 355 | release( m_systems.get_handle( 0 ) );
|
---|
[520] | 356 | if ( m_pgm )
|
---|
| 357 | m_pgm->reset();
|
---|
[518] | 358 | m_emitters.clear();
|
---|
[353] | 359 | m_affectors.clear();
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 |
|
---|
[306] | 363 | void nv::particle_engine::release( particle_system system )
|
---|
| 364 | {
|
---|
| 365 | particle_system_info* info = m_systems.get( system );
|
---|
| 366 | if ( info )
|
---|
| 367 | {
|
---|
[520] | 368 | m_pgm->unref( info->group );
|
---|
| 369 | delete[] info->particles;
|
---|
[499] | 370 | m_systems.destroy( system );
|
---|
| 371 | }
|
---|
| 372 | }
|
---|
| 373 |
|
---|
[522] | 374 | bool nv::particle_engine::is_finished( particle_system system )
|
---|
| 375 | {
|
---|
| 376 | particle_system_info* info = m_systems.get( system );
|
---|
| 377 | if ( info )
|
---|
| 378 | {
|
---|
| 379 | if ( info->count > 0 ) return false;
|
---|
| 380 | for ( uint32 i = 0; i < info->data->emitter_count; ++i )
|
---|
| 381 | {
|
---|
| 382 | const auto& edata = info->emitters[i];
|
---|
| 383 | if ( edata.active || edata.pause > 0.0f )
|
---|
| 384 | {
|
---|
| 385 | return false;
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 | return true;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
[520] | 392 | void nv::particle_engine::release( particle_group group )
|
---|
[499] | 393 | {
|
---|
[520] | 394 | m_pgm->release( group );
|
---|
[306] | 395 | }
|
---|
| 396 |
|
---|
[500] | 397 | void nv::particle_engine::render( particle_system system, const scene_state& s )
|
---|
[306] | 398 | {
|
---|
[500] | 399 | particle_system_info* info = m_systems.get( system );
|
---|
| 400 | if ( info )
|
---|
[520] | 401 | m_pgm->generate_data( array_view< particle >( info->particles, info->count ), info->data, info->group, s );
|
---|
[499] | 402 | }
|
---|
| 403 |
|
---|
[515] | 404 | void nv::particle_engine::update( particle_system system, transform model, float dtime )
|
---|
[499] | 405 | {
|
---|
[306] | 406 | particle_system_info* info = m_systems.get( system );
|
---|
| 407 | if ( info )
|
---|
| 408 | {
|
---|
[500] | 409 | // while ( dtime > 0.2 )
|
---|
| 410 | // {
|
---|
[518] | 411 | // update_emitters( info, 0.2 );
|
---|
[500] | 412 | // destroy_particles( info, 0.2 );
|
---|
| 413 | // create_particles( info, 0.2 );
|
---|
| 414 | // update_particles( info, 0.2 );
|
---|
| 415 | // dtime -= 0.2;
|
---|
| 416 | // }
|
---|
[306] | 417 |
|
---|
[518] | 418 | update_emitters( info, dtime );
|
---|
[500] | 419 | destroy_particles( info, dtime );
|
---|
[515] | 420 | create_particles( info, model, dtime );
|
---|
[500] | 421 | update_particles( info, dtime );
|
---|
| 422 |
|
---|
| 423 | // generate_data( info );
|
---|
[306] | 424 | }
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | void nv::particle_engine::set_texcoords( particle_system system, vec2 a, vec2 b )
|
---|
| 428 | {
|
---|
| 429 | particle_system_info* info = m_systems.get( system );
|
---|
| 430 | if ( info )
|
---|
| 431 | {
|
---|
[500] | 432 | info->texcoords[0] = a;
|
---|
| 433 | info->texcoords[1] = b;
|
---|
[306] | 434 | }
|
---|
| 435 | }
|
---|
| 436 |
|
---|
[500] | 437 | void nv::particle_engine::destroy_particles( particle_system_info* info, float dtime )
|
---|
[306] | 438 | {
|
---|
| 439 | if ( info->count > 0 )
|
---|
[406] | 440 | for ( sint32 i = sint32( info->count ) - 1; i >= 0; --i )
|
---|
[306] | 441 | {
|
---|
| 442 | particle& pinfo = info->particles[i];
|
---|
[500] | 443 | pinfo.lifetime += dtime;
|
---|
| 444 | if ( pinfo.lifetime >= pinfo.death )
|
---|
[306] | 445 | {
|
---|
| 446 | info->count--;
|
---|
[374] | 447 | swap( info->particles[i], info->particles[info->count] );
|
---|
[306] | 448 | }
|
---|
| 449 | }
|
---|
| 450 | }
|
---|
| 451 |
|
---|
[515] | 452 | void nv::particle_engine::create_particles( particle_system_info* info, transform model, float dtime )
|
---|
[306] | 453 | {
|
---|
[518] | 454 | uint32 ecount = info->data->emitter_count;
|
---|
[306] | 455 | if ( ecount == 0 ) return;
|
---|
| 456 |
|
---|
| 457 | for ( uint32 i = 0; i < ecount; ++i )
|
---|
| 458 | {
|
---|
[518] | 459 | const auto& edata = info->data->emitters[i];
|
---|
| 460 | auto& einfo = info->emitters[i];
|
---|
[307] | 461 | if ( einfo.active )
|
---|
[306] | 462 | {
|
---|
[500] | 463 | einfo.next -= dtime;
|
---|
| 464 | float fperiod = 1.0f / edata.rate;
|
---|
| 465 | while ( einfo.next < 0.0f )
|
---|
[306] | 466 | {
|
---|
[307] | 467 | if ( info->count < info->data->quota-1 )
|
---|
| 468 | {
|
---|
| 469 | particle& pinfo = info->particles[info->count];
|
---|
[520] | 470 | edata.emitter_func( m_rng, &(info->data->emitters[i]), &pinfo, 1 );
|
---|
[500] | 471 | pinfo.position = vec3();
|
---|
| 472 | pinfo.position+= edata.position;
|
---|
[518] | 473 | // if ( !local )
|
---|
[515] | 474 | pinfo.position = pinfo.position * model;
|
---|
| 475 | pinfo.color = edata.color_min == edata.color_max ?
|
---|
[520] | 476 | edata.color_min : m_rng->range( edata.color_min, edata.color_max );
|
---|
[500] | 477 | pinfo.size = edata.size_min == edata.size_max ?
|
---|
[520] | 478 | edata.size_min : m_rng->range( edata.size_min, edata.size_max );
|
---|
[307] | 479 | if ( edata.square ) pinfo.size.y = pinfo.size.x;
|
---|
[500] | 480 | float velocity = edata.velocity_min == edata.velocity_max ?
|
---|
[520] | 481 | edata.velocity_min : m_rng->frange( edata.velocity_min, edata.velocity_max );
|
---|
[500] | 482 | pinfo.lifetime = dtime + einfo.next;
|
---|
| 483 | pinfo.death = ( edata.lifetime_min == edata.lifetime_max ?
|
---|
[520] | 484 | edata.lifetime_min : m_rng->frange( edata.lifetime_min, edata.lifetime_max ) );
|
---|
| 485 | pinfo.rotation = m_rng->frand( 2* math::pi<float>() );
|
---|
[500] | 486 | pinfo.tcoord_a = info->texcoords[0];
|
---|
| 487 | pinfo.tcoord_b = info->texcoords[1];
|
---|
[306] | 488 |
|
---|
[312] | 489 | pinfo.velocity = edata.dir;
|
---|
[307] | 490 | if ( edata.angle > 0.0f )
|
---|
| 491 | {
|
---|
[453] | 492 | float emission_angle = math::radians( edata.angle );
|
---|
[520] | 493 | float cos_theta = m_rng->frange( cos( emission_angle ), 1.0f );
|
---|
[471] | 494 | float sin_theta = sqrt(1.0f - cos_theta * cos_theta );
|
---|
[520] | 495 | float phi = m_rng->frange( 0.0f, 2* math::pi<float>() );
|
---|
[515] | 496 | pinfo.velocity = model.get_orientation() *
|
---|
[471] | 497 | ( edata.odir * ( cos(phi) * sin_theta ) +
|
---|
| 498 | edata.cdir * ( sin(phi)*sin_theta ) +
|
---|
[307] | 499 | edata.dir * cos_theta );
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[312] | 502 | pinfo.velocity *= velocity;
|
---|
| 503 |
|
---|
[307] | 504 | info->count++;
|
---|
[306] | 505 | }
|
---|
[500] | 506 | einfo.next += fperiod;
|
---|
[306] | 507 | }
|
---|
[500] | 508 |
|
---|
[306] | 509 | }
|
---|
| 510 | }
|
---|
| 511 | }
|
---|
| 512 |
|
---|
[500] | 513 | void nv::particle_engine::update_particles( particle_system_info* info, float dtime )
|
---|
[306] | 514 | {
|
---|
[500] | 515 | if ( dtime <= 0.0f ) return;
|
---|
[312] | 516 |
|
---|
| 517 | uint32 acount = info->data->affector_count;
|
---|
| 518 | for ( uint32 i = 0; i < acount; ++i )
|
---|
| 519 | {
|
---|
| 520 | const particle_affector_data* padata = &(info->data->affectors[i]);
|
---|
[500] | 521 | padata->process( padata, info->particles, dtime, info->count );
|
---|
[312] | 522 | }
|
---|
| 523 |
|
---|
| 524 |
|
---|
| 525 | for ( uint32 i = 0; i < info->count; ++i )
|
---|
| 526 | {
|
---|
| 527 | particle& pdata = info->particles[i];
|
---|
[500] | 528 | float factor = min( dtime, pdata.lifetime );
|
---|
[312] | 529 | pdata.position += pdata.velocity * factor;
|
---|
| 530 | }
|
---|
[522] | 531 |
|
---|
| 532 | if ( info->on_collide )
|
---|
| 533 | {
|
---|
| 534 | for ( uint32 i = 0; i < info->count; ++i )
|
---|
| 535 | {
|
---|
| 536 | particle& pdata = info->particles[i];
|
---|
| 537 | if ( pdata.position.y <= 0.0f )
|
---|
| 538 | {
|
---|
| 539 | info->on_collide( pdata.position, pdata.velocity );
|
---|
| 540 | pdata.death = pdata.lifetime;
|
---|
| 541 | }
|
---|
| 542 | }
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[307] | 545 | }
|
---|
| 546 |
|
---|
[518] | 547 | void nv::particle_engine::update_emitters( particle_system_info* info, float dtime )
|
---|
[307] | 548 | {
|
---|
[518] | 549 | uint32 ecount = info->data->emitter_count;
|
---|
[307] | 550 | if ( ecount == 0 ) return;
|
---|
| 551 |
|
---|
| 552 | for ( uint32 i = 0; i < ecount; ++i )
|
---|
| 553 | {
|
---|
[518] | 554 | const auto& edata = info->data->emitters[i];
|
---|
| 555 | auto& einfo = info->emitters[i];
|
---|
[307] | 556 |
|
---|
[500] | 557 | if ( einfo.pause > 0.0f )
|
---|
[307] | 558 | {
|
---|
[500] | 559 | einfo.pause -= dtime;
|
---|
| 560 | if ( einfo.pause == 0.0f ) einfo.pause = -0.001f;
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | if ( einfo.pause < 0.0f )
|
---|
| 564 | {
|
---|
[307] | 565 | if ( einfo.active )
|
---|
| 566 | {
|
---|
| 567 | einfo.active = false;
|
---|
[500] | 568 | if ( edata.repeat_min > 0.0f )
|
---|
[520] | 569 | einfo.pause += m_rng->frange( edata.repeat_min, edata.repeat_max );
|
---|
[307] | 570 | else
|
---|
[500] | 571 | einfo.pause = 0.0f;
|
---|
[307] | 572 | }
|
---|
| 573 | else
|
---|
| 574 | {
|
---|
| 575 | einfo.active = true;
|
---|
[520] | 576 | einfo.pause += m_rng->frange( edata.duration_min, edata.duration_max );
|
---|
[307] | 577 | }
|
---|
| 578 | }
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | }
|
---|
[312] | 582 |
|
---|
[518] | 583 | void nv::particle_engine::register_emitter_type( const string_view& name, particle_emitter_func func )
|
---|
[312] | 584 | {
|
---|
[518] | 585 | if ( m_debug_emitter_names )
|
---|
| 586 | ( *m_debug_emitter_names )[func] = name;
|
---|
[519] | 587 | if ( m_debug_emitter_list )
|
---|
| 588 | ( *m_debug_emitter_list ).push_back( func );
|
---|
| 589 |
|
---|
[518] | 590 | m_emitters[ name ] = func;
|
---|
[312] | 591 | }
|
---|
| 592 |
|
---|
[518] | 593 | void nv::particle_engine::register_standard_emitters()
|
---|
[312] | 594 | {
|
---|
[518] | 595 | register_emitter_type( "point", nv_particle_emitter_point );
|
---|
| 596 | register_emitter_type( "box", nv_particle_emitter_box );
|
---|
| 597 | register_emitter_type( "cylinder", nv_particle_emitter_cylinder );
|
---|
| 598 | register_emitter_type( "sphere", nv_particle_emitter_sphere );
|
---|
| 599 | register_emitter_type( "cylindroid", nv_particle_emitter_cylindroid );
|
---|
| 600 | register_emitter_type( "ellipsoid", nv_particle_emitter_ellipsoid );
|
---|
| 601 | register_emitter_type( "hollow_cylinder", nv_particle_emitter_hollow_cylinder );
|
---|
| 602 | register_emitter_type( "hollow_sphere", nv_particle_emitter_hollow_sphere );
|
---|
| 603 | register_emitter_type( "hollow_cylindroid", nv_particle_emitter_hollow_cylindroid );
|
---|
| 604 | register_emitter_type( "hollow_ellipsoid", nv_particle_emitter_hollow_ellipsoid );
|
---|
[312] | 605 | }
|
---|
| 606 |
|
---|
[439] | 607 | void nv::particle_engine::register_affector_type( const string_view& name, particle_affector_init_func init, particle_affector_func process )
|
---|
[312] | 608 | {
|
---|
[518] | 609 | if ( m_debug_affector_names )
|
---|
| 610 | ( *m_debug_affector_names )[process] = name;
|
---|
[519] | 611 | if ( m_debug_affector_list )
|
---|
| 612 | ( *m_debug_affector_list ).push_back( process );
|
---|
[518] | 613 |
|
---|
[312] | 614 | m_affectors[ name ].init = init;
|
---|
| 615 | m_affectors[ name ].process = process;
|
---|
| 616 | }
|
---|
| 617 |
|
---|
[518] | 618 | nv::particle_emitter_func nv::particle_engine::get_emitter( shash64 emitter )
|
---|
| 619 | {
|
---|
| 620 | auto emitter_iter = m_emitters.find( emitter );
|
---|
| 621 | if ( emitter_iter != m_emitters.end() )
|
---|
| 622 | {
|
---|
| 623 | return emitter_iter->second;
|
---|
| 624 | }
|
---|
| 625 | return nullptr;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | nv::particle_affector_funcs nv::particle_engine::get_affector( shash64 affector )
|
---|
| 629 | {
|
---|
| 630 | auto affector_iter = m_affectors.find( affector );
|
---|
| 631 | if ( affector_iter != m_affectors.end() )
|
---|
| 632 | {
|
---|
| 633 | return affector_iter->second;
|
---|
| 634 | }
|
---|
| 635 | return { nullptr, nullptr };
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | nv::string_view nv::particle_engine::get_debug_name( particle_emitter_func f )
|
---|
| 639 | {
|
---|
| 640 | auto i = m_debug_emitter_names->find( f );
|
---|
| 641 | if ( i != m_debug_emitter_names->end() )
|
---|
| 642 | return i->second;
|
---|
| 643 | return {};
|
---|
| 644 | }
|
---|
| 645 |
|
---|
| 646 | nv::string_view nv::particle_engine::get_debug_name( particle_affector_func f )
|
---|
| 647 | {
|
---|
| 648 | auto i = m_debug_affector_names->find( f );
|
---|
| 649 | if ( i != m_debug_affector_names->end() )
|
---|
| 650 | return i->second;
|
---|
| 651 | return {};
|
---|
| 652 | }
|
---|
| 653 |
|
---|
[520] | 654 | nv::particle_render_data nv::particle_engine::get_render_data( particle_group group )
|
---|
[500] | 655 | {
|
---|
[520] | 656 | return m_pgm->get_render_data( group );
|
---|
[500] | 657 | }
|
---|
| 658 |
|
---|
[312] | 659 | void nv::particle_engine::register_standard_affectors()
|
---|
| 660 | {
|
---|
| 661 | register_affector_type( "linear_force", nv_particle_affector_linear_force_init, nv_particle_affector_linear_force );
|
---|
| 662 | register_affector_type( "deflector_plane", nv_particle_affector_deflector_plane_init, nv_particle_affector_deflector_plane );
|
---|
| 663 | register_affector_type( "color_fader", nv_particle_affector_color_fader_init, nv_particle_affector_color_fader );
|
---|
| 664 | register_affector_type( "scaler", nv_particle_affector_scaler_init, nv_particle_affector_scaler );
|
---|
| 665 | }
|
---|
| 666 |
|
---|
[520] | 667 | const nv::type_entry* nv::particle_engine::get_debug_type( particle_affector_func f )
|
---|
[518] | 668 | {
|
---|
[520] | 669 | if ( m_debug_affector_types )
|
---|
| 670 | {
|
---|
| 671 | auto it = m_debug_affector_types->find( f );
|
---|
| 672 | if ( it != m_debug_affector_types->end() )
|
---|
| 673 | return it->second;
|
---|
| 674 | }
|
---|
| 675 | return nullptr;
|
---|
| 676 | }
|
---|
| 677 |
|
---|
| 678 | void nv::particle_engine::register_types( type_database* db, bool debug_data )
|
---|
| 679 | {
|
---|
[519] | 680 | db->create_type<particle_orientation>()
|
---|
[518] | 681 | .value( "PS_POINT", sint32( particle_orientation::POINT ), "Point" )
|
---|
| 682 | .value( "PS_ORIENTED", sint32( particle_orientation::ORIENTED ), "Oriented" )
|
---|
| 683 | .value( "PS_ORIENTED_COMMON", sint32( particle_orientation::ORIENTED_COMMON ), "Oriented Common" )
|
---|
| 684 | .value( "PS_PERPENDICULAR", sint32( particle_orientation::PERPENDICULAR ), "Perpendicular" )
|
---|
| 685 | .value( "PS_PERPENDICULAR_COMMON", sint32( particle_orientation::PERPENDICULAR_COMMON ), "Perpendicular Common" )
|
---|
| 686 | ;
|
---|
| 687 |
|
---|
[519] | 688 | db->create_type<particle_origin>()
|
---|
[518] | 689 | .value( "PS_CENTER", sint32( particle_origin::CENTER ), "Center" )
|
---|
| 690 | .value( "PS_TOP_LEFT", sint32( particle_origin::TOP_LEFT ), "Top left" )
|
---|
| 691 | .value( "PS_TOP_CENTER", sint32( particle_origin::TOP_CENTER ), "Top center" )
|
---|
| 692 | .value( "PS_TOP_RIGHT", sint32( particle_origin::TOP_RIGHT ), "Top right" )
|
---|
| 693 | .value( "PS_CENTER_LEFT", sint32( particle_origin::CENTER_LEFT ), "Center left" )
|
---|
| 694 | .value( "PS_CENTER_RIGHT", sint32( particle_origin::CENTER_RIGHT ), "Center right" )
|
---|
| 695 | .value( "PS_BOTTOM_LEFT", sint32( particle_origin::BOTTOM_LEFT ), "Bottom left" )
|
---|
| 696 | .value( "PS_BOTTOM_CENTER", sint32( particle_origin::BOTTOM_CENTER ), "Bottom center" )
|
---|
| 697 | .value( "PS_BOTTOM_RIGHT", sint32( particle_origin::BOTTOM_RIGHT ), "Bottom right" )
|
---|
| 698 | ;
|
---|
| 699 |
|
---|
[520] | 700 | if ( debug_data )
|
---|
[518] | 701 | {
|
---|
[520] | 702 | if ( !m_debug_affector_types )
|
---|
| 703 | m_debug_affector_types = new nv::hash_map< nv::particle_affector_func, nv::type_entry* >;
|
---|
| 704 |
|
---|
[518] | 705 | int you_could_get_rid_of_the_init_function_using_these; int error;
|
---|
[519] | 706 | int universal_vm_based_affector;
|
---|
| 707 | // compile_affector( ... ) in lua, returns array of bytecode
|
---|
| 708 | // function is a normal bytecode runner!
|
---|
[518] | 709 |
|
---|
[519] | 710 |
|
---|
[518] | 711 | (*m_debug_affector_types)[ nv_particle_affector_linear_force ]
|
---|
| 712 | = db->create_type<nvpe_linear_force_data>( "linear force" )
|
---|
| 713 | .field( "force_vector", &nvpe_linear_force_data::force_vector )
|
---|
| 714 | .field( "average", &nvpe_linear_force_data::average )
|
---|
| 715 | .get();
|
---|
| 716 |
|
---|
| 717 | ( *m_debug_affector_types )[nv_particle_affector_deflector_plane]
|
---|
| 718 | = db->create_type<nvpe_deflector_plane_data>( "deflector plane" )
|
---|
| 719 | .field( "plane_point", &nvpe_deflector_plane_data::plane_point )
|
---|
| 720 | .field( "plane_normal", &nvpe_deflector_plane_data::plane_normal )
|
---|
| 721 | .field( "bounce", &nvpe_deflector_plane_data::bounce )
|
---|
| 722 | .field( "distance", &nvpe_deflector_plane_data::distance )
|
---|
| 723 | .get();
|
---|
| 724 |
|
---|
| 725 | ( *m_debug_affector_types )[nv_particle_affector_color_fader]
|
---|
| 726 | = db->create_type<nvpe_linear_force_data>( "color fader" )
|
---|
| 727 | .field( "adjustment", & nvpe_color_fader_data::adjustment )
|
---|
| 728 | .get();
|
---|
| 729 |
|
---|
| 730 | ( *m_debug_affector_types )[nv_particle_affector_scaler]
|
---|
| 731 | = db->create_type<nvpe_scaler_data>( "scaler" )
|
---|
| 732 | .field( "adjustment", & nvpe_scaler_data::adjustment )
|
---|
| 733 | .get();
|
---|
| 734 | }
|
---|
| 735 | }
|
---|
[520] | 736 |
|
---|