[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 );
|
---|
[471] | 198 | datap->distance = -math::dot( datap->plane_normal, datap->plane_point ) / 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 );
|
---|
[353] | 308 |
|
---|
[306] | 309 | }
|
---|
| 310 |
|
---|
| 311 | info->count = 0;
|
---|
[518] | 312 | info->particles = new particle[data->quota];
|
---|
[306] | 313 |
|
---|
| 314 | return result;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[520] | 317 | nv::particle_system nv::particle_engine::create_system( resource< nv::particle_system_data > rdata, particle_group group )
|
---|
[306] | 318 | {
|
---|
[518] | 319 | if ( auto data = rdata.lock() )
|
---|
| 320 | return create_system( &*data, group );
|
---|
| 321 | return {};
|
---|
[306] | 322 | }
|
---|
| 323 |
|
---|
[520] | 324 | void nv::particle_engine::prepare( particle_group group )
|
---|
[499] | 325 | {
|
---|
[520] | 326 | m_pgm->prepare( group );
|
---|
[499] | 327 | }
|
---|
| 328 |
|
---|
[520] | 329 | nv::particle_group nv::particle_engine::create_group( uint32 max_particles )
|
---|
[499] | 330 | {
|
---|
[520] | 331 | return m_pgm->create_group( max_particles );
|
---|
[499] | 332 | }
|
---|
| 333 |
|
---|
[306] | 334 | nv::particle_engine::~particle_engine()
|
---|
| 335 | {
|
---|
[353] | 336 | clear();
|
---|
[306] | 337 | }
|
---|
| 338 |
|
---|
[353] | 339 | void nv::particle_engine::reset()
|
---|
| 340 | {
|
---|
| 341 | clear();
|
---|
[518] | 342 | register_standard_emitters( );
|
---|
[353] | 343 | register_standard_affectors();
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | void nv::particle_engine::clear()
|
---|
| 347 | {
|
---|
| 348 | while ( m_systems.size() > 0 )
|
---|
| 349 | release( m_systems.get_handle( 0 ) );
|
---|
[520] | 350 | if ( m_pgm )
|
---|
| 351 | m_pgm->reset();
|
---|
[518] | 352 | m_emitters.clear();
|
---|
[353] | 353 | m_affectors.clear();
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 |
|
---|
[306] | 357 | void nv::particle_engine::release( particle_system system )
|
---|
| 358 | {
|
---|
| 359 | particle_system_info* info = m_systems.get( system );
|
---|
| 360 | if ( info )
|
---|
| 361 | {
|
---|
[520] | 362 | m_pgm->unref( info->group );
|
---|
| 363 | delete[] info->particles;
|
---|
[499] | 364 | m_systems.destroy( system );
|
---|
| 365 | }
|
---|
| 366 | }
|
---|
| 367 |
|
---|
[520] | 368 | void nv::particle_engine::release( particle_group group )
|
---|
[499] | 369 | {
|
---|
[520] | 370 | m_pgm->release( group );
|
---|
[306] | 371 | }
|
---|
| 372 |
|
---|
[500] | 373 | void nv::particle_engine::render( particle_system system, const scene_state& s )
|
---|
[306] | 374 | {
|
---|
[500] | 375 | particle_system_info* info = m_systems.get( system );
|
---|
| 376 | if ( info )
|
---|
[520] | 377 | m_pgm->generate_data( array_view< particle >( info->particles, info->count ), info->data, info->group, s );
|
---|
[499] | 378 | }
|
---|
| 379 |
|
---|
[515] | 380 | void nv::particle_engine::update( particle_system system, transform model, float dtime )
|
---|
[499] | 381 | {
|
---|
[306] | 382 | particle_system_info* info = m_systems.get( system );
|
---|
| 383 | if ( info )
|
---|
| 384 | {
|
---|
[500] | 385 | // while ( dtime > 0.2 )
|
---|
| 386 | // {
|
---|
[518] | 387 | // update_emitters( info, 0.2 );
|
---|
[500] | 388 | // destroy_particles( info, 0.2 );
|
---|
| 389 | // create_particles( info, 0.2 );
|
---|
| 390 | // update_particles( info, 0.2 );
|
---|
| 391 | // dtime -= 0.2;
|
---|
| 392 | // }
|
---|
[306] | 393 |
|
---|
[518] | 394 | update_emitters( info, dtime );
|
---|
[500] | 395 | destroy_particles( info, dtime );
|
---|
[515] | 396 | create_particles( info, model, dtime );
|
---|
[500] | 397 | update_particles( info, dtime );
|
---|
| 398 |
|
---|
| 399 | // generate_data( info );
|
---|
[306] | 400 | }
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | void nv::particle_engine::set_texcoords( particle_system system, vec2 a, vec2 b )
|
---|
| 404 | {
|
---|
[500] | 405 |
|
---|
[306] | 406 | particle_system_info* info = m_systems.get( system );
|
---|
| 407 | if ( info )
|
---|
| 408 | {
|
---|
[500] | 409 | info->texcoords[0] = a;
|
---|
| 410 | info->texcoords[1] = b;
|
---|
[306] | 411 | }
|
---|
| 412 | }
|
---|
| 413 |
|
---|
[500] | 414 | void nv::particle_engine::destroy_particles( particle_system_info* info, float dtime )
|
---|
[306] | 415 | {
|
---|
| 416 | if ( info->count > 0 )
|
---|
[406] | 417 | for ( sint32 i = sint32( info->count ) - 1; i >= 0; --i )
|
---|
[306] | 418 | {
|
---|
| 419 | particle& pinfo = info->particles[i];
|
---|
[500] | 420 | pinfo.lifetime += dtime;
|
---|
| 421 | if ( pinfo.lifetime >= pinfo.death )
|
---|
[306] | 422 | {
|
---|
| 423 | info->count--;
|
---|
[374] | 424 | swap( info->particles[i], info->particles[info->count] );
|
---|
[306] | 425 | }
|
---|
| 426 | }
|
---|
| 427 | }
|
---|
| 428 |
|
---|
[515] | 429 | void nv::particle_engine::create_particles( particle_system_info* info, transform model, float dtime )
|
---|
[306] | 430 | {
|
---|
[518] | 431 | uint32 ecount = info->data->emitter_count;
|
---|
[306] | 432 | if ( ecount == 0 ) return;
|
---|
| 433 |
|
---|
[515] | 434 | bool local = model.is_identity();
|
---|
[500] | 435 | // if ( !local )
|
---|
| 436 | // {
|
---|
| 437 | // source = vec3( m_model_matrix[3] );
|
---|
| 438 | // orient = mat3( m_model_matrix );
|
---|
| 439 | // }
|
---|
[312] | 440 |
|
---|
[306] | 441 | for ( uint32 i = 0; i < ecount; ++i )
|
---|
| 442 | {
|
---|
[518] | 443 | const auto& edata = info->data->emitters[i];
|
---|
| 444 | auto& einfo = info->emitters[i];
|
---|
[307] | 445 | if ( einfo.active )
|
---|
[306] | 446 | {
|
---|
[500] | 447 | einfo.next -= dtime;
|
---|
| 448 | float fperiod = 1.0f / edata.rate;
|
---|
| 449 | while ( einfo.next < 0.0f )
|
---|
[306] | 450 | {
|
---|
[307] | 451 | if ( info->count < info->data->quota-1 )
|
---|
| 452 | {
|
---|
| 453 | particle& pinfo = info->particles[info->count];
|
---|
[520] | 454 | edata.emitter_func( m_rng, &(info->data->emitters[i]), &pinfo, 1 );
|
---|
[500] | 455 | pinfo.position = vec3();
|
---|
| 456 | pinfo.position+= edata.position;
|
---|
[518] | 457 | // if ( !local )
|
---|
[515] | 458 | pinfo.position = pinfo.position * model;
|
---|
| 459 | pinfo.color = edata.color_min == edata.color_max ?
|
---|
[520] | 460 | edata.color_min : m_rng->range( edata.color_min, edata.color_max );
|
---|
[500] | 461 | pinfo.size = edata.size_min == edata.size_max ?
|
---|
[520] | 462 | edata.size_min : m_rng->range( edata.size_min, edata.size_max );
|
---|
[307] | 463 | if ( edata.square ) pinfo.size.y = pinfo.size.x;
|
---|
[500] | 464 | float velocity = edata.velocity_min == edata.velocity_max ?
|
---|
[520] | 465 | edata.velocity_min : m_rng->frange( edata.velocity_min, edata.velocity_max );
|
---|
[500] | 466 | pinfo.lifetime = dtime + einfo.next;
|
---|
| 467 | pinfo.death = ( edata.lifetime_min == edata.lifetime_max ?
|
---|
[520] | 468 | edata.lifetime_min : m_rng->frange( edata.lifetime_min, edata.lifetime_max ) );
|
---|
| 469 | pinfo.rotation = m_rng->frand( 2* math::pi<float>() );
|
---|
[500] | 470 | pinfo.tcoord_a = info->texcoords[0];
|
---|
| 471 | pinfo.tcoord_b = info->texcoords[1];
|
---|
[306] | 472 |
|
---|
[312] | 473 | pinfo.velocity = edata.dir;
|
---|
[307] | 474 | if ( edata.angle > 0.0f )
|
---|
| 475 | {
|
---|
[453] | 476 | float emission_angle = math::radians( edata.angle );
|
---|
[520] | 477 | float cos_theta = m_rng->frange( cos( emission_angle ), 1.0f );
|
---|
[471] | 478 | float sin_theta = sqrt(1.0f - cos_theta * cos_theta );
|
---|
[520] | 479 | float phi = m_rng->frange( 0.0f, 2* math::pi<float>() );
|
---|
[515] | 480 | pinfo.velocity = model.get_orientation() *
|
---|
[471] | 481 | ( edata.odir * ( cos(phi) * sin_theta ) +
|
---|
| 482 | edata.cdir * ( sin(phi)*sin_theta ) +
|
---|
[307] | 483 | edata.dir * cos_theta );
|
---|
| 484 | }
|
---|
| 485 |
|
---|
[312] | 486 | pinfo.velocity *= velocity;
|
---|
| 487 |
|
---|
[307] | 488 | info->count++;
|
---|
[306] | 489 | }
|
---|
[500] | 490 | einfo.next += fperiod;
|
---|
[306] | 491 | }
|
---|
[500] | 492 |
|
---|
[306] | 493 | }
|
---|
| 494 | }
|
---|
| 495 | }
|
---|
| 496 |
|
---|
[500] | 497 | void nv::particle_engine::update_particles( particle_system_info* info, float dtime )
|
---|
[306] | 498 | {
|
---|
[500] | 499 | if ( dtime <= 0.0f ) return;
|
---|
[312] | 500 |
|
---|
| 501 | uint32 acount = info->data->affector_count;
|
---|
| 502 | for ( uint32 i = 0; i < acount; ++i )
|
---|
| 503 | {
|
---|
| 504 | const particle_affector_data* padata = &(info->data->affectors[i]);
|
---|
[500] | 505 | padata->process( padata, info->particles, dtime, info->count );
|
---|
[312] | 506 | }
|
---|
| 507 |
|
---|
| 508 |
|
---|
| 509 | for ( uint32 i = 0; i < info->count; ++i )
|
---|
| 510 | {
|
---|
| 511 | particle& pdata = info->particles[i];
|
---|
[500] | 512 | float factor = min( dtime, pdata.lifetime );
|
---|
[312] | 513 | pdata.position += pdata.velocity * factor;
|
---|
| 514 | }
|
---|
[307] | 515 | }
|
---|
| 516 |
|
---|
[518] | 517 | void nv::particle_engine::update_emitters( particle_system_info* info, float dtime )
|
---|
[307] | 518 | {
|
---|
[518] | 519 | uint32 ecount = info->data->emitter_count;
|
---|
[307] | 520 | if ( ecount == 0 ) return;
|
---|
| 521 |
|
---|
| 522 | for ( uint32 i = 0; i < ecount; ++i )
|
---|
| 523 | {
|
---|
[518] | 524 | const auto& edata = info->data->emitters[i];
|
---|
| 525 | auto& einfo = info->emitters[i];
|
---|
[307] | 526 |
|
---|
[500] | 527 | if ( einfo.pause > 0.0f )
|
---|
[307] | 528 | {
|
---|
[500] | 529 | einfo.pause -= dtime;
|
---|
| 530 | if ( einfo.pause == 0.0f ) einfo.pause = -0.001f;
|
---|
| 531 | }
|
---|
| 532 |
|
---|
| 533 | if ( einfo.pause < 0.0f )
|
---|
| 534 | {
|
---|
[307] | 535 | if ( einfo.active )
|
---|
| 536 | {
|
---|
| 537 | einfo.active = false;
|
---|
[500] | 538 | if ( edata.repeat_min > 0.0f )
|
---|
[520] | 539 | einfo.pause += m_rng->frange( edata.repeat_min, edata.repeat_max );
|
---|
[307] | 540 | else
|
---|
[500] | 541 | einfo.pause = 0.0f;
|
---|
[307] | 542 | }
|
---|
| 543 | else
|
---|
| 544 | {
|
---|
| 545 | einfo.active = true;
|
---|
[520] | 546 | einfo.pause += m_rng->frange( edata.duration_min, edata.duration_max );
|
---|
[307] | 547 | }
|
---|
| 548 | }
|
---|
| 549 | }
|
---|
| 550 |
|
---|
| 551 | }
|
---|
[312] | 552 |
|
---|
[518] | 553 | void nv::particle_engine::register_emitter_type( const string_view& name, particle_emitter_func func )
|
---|
[312] | 554 | {
|
---|
[518] | 555 | if ( m_debug_emitter_names )
|
---|
| 556 | ( *m_debug_emitter_names )[func] = name;
|
---|
[519] | 557 | if ( m_debug_emitter_list )
|
---|
| 558 | ( *m_debug_emitter_list ).push_back( func );
|
---|
| 559 |
|
---|
[518] | 560 | m_emitters[ name ] = func;
|
---|
[312] | 561 | }
|
---|
| 562 |
|
---|
[518] | 563 | void nv::particle_engine::register_standard_emitters()
|
---|
[312] | 564 | {
|
---|
[518] | 565 | register_emitter_type( "point", nv_particle_emitter_point );
|
---|
| 566 | register_emitter_type( "box", nv_particle_emitter_box );
|
---|
| 567 | register_emitter_type( "cylinder", nv_particle_emitter_cylinder );
|
---|
| 568 | register_emitter_type( "sphere", nv_particle_emitter_sphere );
|
---|
| 569 | register_emitter_type( "cylindroid", nv_particle_emitter_cylindroid );
|
---|
| 570 | register_emitter_type( "ellipsoid", nv_particle_emitter_ellipsoid );
|
---|
| 571 | register_emitter_type( "hollow_cylinder", nv_particle_emitter_hollow_cylinder );
|
---|
| 572 | register_emitter_type( "hollow_sphere", nv_particle_emitter_hollow_sphere );
|
---|
| 573 | register_emitter_type( "hollow_cylindroid", nv_particle_emitter_hollow_cylindroid );
|
---|
| 574 | register_emitter_type( "hollow_ellipsoid", nv_particle_emitter_hollow_ellipsoid );
|
---|
[312] | 575 | }
|
---|
| 576 |
|
---|
[439] | 577 | void nv::particle_engine::register_affector_type( const string_view& name, particle_affector_init_func init, particle_affector_func process )
|
---|
[312] | 578 | {
|
---|
[518] | 579 | if ( m_debug_affector_names )
|
---|
| 580 | ( *m_debug_affector_names )[process] = name;
|
---|
[519] | 581 | if ( m_debug_affector_list )
|
---|
| 582 | ( *m_debug_affector_list ).push_back( process );
|
---|
[518] | 583 |
|
---|
[312] | 584 | m_affectors[ name ].init = init;
|
---|
| 585 | m_affectors[ name ].process = process;
|
---|
| 586 | }
|
---|
| 587 |
|
---|
[518] | 588 | nv::particle_emitter_func nv::particle_engine::get_emitter( shash64 emitter )
|
---|
| 589 | {
|
---|
| 590 | auto emitter_iter = m_emitters.find( emitter );
|
---|
| 591 | if ( emitter_iter != m_emitters.end() )
|
---|
| 592 | {
|
---|
| 593 | return emitter_iter->second;
|
---|
| 594 | }
|
---|
| 595 | return nullptr;
|
---|
| 596 | }
|
---|
| 597 |
|
---|
| 598 | nv::particle_affector_funcs nv::particle_engine::get_affector( shash64 affector )
|
---|
| 599 | {
|
---|
| 600 | auto affector_iter = m_affectors.find( affector );
|
---|
| 601 | if ( affector_iter != m_affectors.end() )
|
---|
| 602 | {
|
---|
| 603 | return affector_iter->second;
|
---|
| 604 | }
|
---|
| 605 | return { nullptr, nullptr };
|
---|
| 606 | }
|
---|
| 607 |
|
---|
| 608 | nv::string_view nv::particle_engine::get_debug_name( particle_emitter_func f )
|
---|
| 609 | {
|
---|
| 610 | auto i = m_debug_emitter_names->find( f );
|
---|
| 611 | if ( i != m_debug_emitter_names->end() )
|
---|
| 612 | return i->second;
|
---|
| 613 | return {};
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | nv::string_view nv::particle_engine::get_debug_name( particle_affector_func f )
|
---|
| 617 | {
|
---|
| 618 | auto i = m_debug_affector_names->find( f );
|
---|
| 619 | if ( i != m_debug_affector_names->end() )
|
---|
| 620 | return i->second;
|
---|
| 621 | return {};
|
---|
| 622 | }
|
---|
| 623 |
|
---|
[520] | 624 | nv::particle_render_data nv::particle_engine::get_render_data( particle_group group )
|
---|
[500] | 625 | {
|
---|
[520] | 626 | return m_pgm->get_render_data( group );
|
---|
[500] | 627 | }
|
---|
| 628 |
|
---|
[312] | 629 | void nv::particle_engine::register_standard_affectors()
|
---|
| 630 | {
|
---|
| 631 | register_affector_type( "linear_force", nv_particle_affector_linear_force_init, nv_particle_affector_linear_force );
|
---|
| 632 | register_affector_type( "deflector_plane", nv_particle_affector_deflector_plane_init, nv_particle_affector_deflector_plane );
|
---|
| 633 | register_affector_type( "color_fader", nv_particle_affector_color_fader_init, nv_particle_affector_color_fader );
|
---|
| 634 | register_affector_type( "scaler", nv_particle_affector_scaler_init, nv_particle_affector_scaler );
|
---|
| 635 | }
|
---|
| 636 |
|
---|
[520] | 637 | const nv::type_entry* nv::particle_engine::get_debug_type( particle_affector_func f )
|
---|
[518] | 638 | {
|
---|
[520] | 639 | if ( m_debug_affector_types )
|
---|
| 640 | {
|
---|
| 641 | auto it = m_debug_affector_types->find( f );
|
---|
| 642 | if ( it != m_debug_affector_types->end() )
|
---|
| 643 | return it->second;
|
---|
| 644 | }
|
---|
| 645 | return nullptr;
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | void nv::particle_engine::register_types( type_database* db, bool debug_data )
|
---|
| 649 | {
|
---|
[519] | 650 | db->create_type<particle_orientation>()
|
---|
[518] | 651 | .value( "PS_POINT", sint32( particle_orientation::POINT ), "Point" )
|
---|
| 652 | .value( "PS_ORIENTED", sint32( particle_orientation::ORIENTED ), "Oriented" )
|
---|
| 653 | .value( "PS_ORIENTED_COMMON", sint32( particle_orientation::ORIENTED_COMMON ), "Oriented Common" )
|
---|
| 654 | .value( "PS_PERPENDICULAR", sint32( particle_orientation::PERPENDICULAR ), "Perpendicular" )
|
---|
| 655 | .value( "PS_PERPENDICULAR_COMMON", sint32( particle_orientation::PERPENDICULAR_COMMON ), "Perpendicular Common" )
|
---|
| 656 | ;
|
---|
| 657 |
|
---|
[519] | 658 | db->create_type<particle_origin>()
|
---|
[518] | 659 | .value( "PS_CENTER", sint32( particle_origin::CENTER ), "Center" )
|
---|
| 660 | .value( "PS_TOP_LEFT", sint32( particle_origin::TOP_LEFT ), "Top left" )
|
---|
| 661 | .value( "PS_TOP_CENTER", sint32( particle_origin::TOP_CENTER ), "Top center" )
|
---|
| 662 | .value( "PS_TOP_RIGHT", sint32( particle_origin::TOP_RIGHT ), "Top right" )
|
---|
| 663 | .value( "PS_CENTER_LEFT", sint32( particle_origin::CENTER_LEFT ), "Center left" )
|
---|
| 664 | .value( "PS_CENTER_RIGHT", sint32( particle_origin::CENTER_RIGHT ), "Center right" )
|
---|
| 665 | .value( "PS_BOTTOM_LEFT", sint32( particle_origin::BOTTOM_LEFT ), "Bottom left" )
|
---|
| 666 | .value( "PS_BOTTOM_CENTER", sint32( particle_origin::BOTTOM_CENTER ), "Bottom center" )
|
---|
| 667 | .value( "PS_BOTTOM_RIGHT", sint32( particle_origin::BOTTOM_RIGHT ), "Bottom right" )
|
---|
| 668 | ;
|
---|
| 669 |
|
---|
[520] | 670 | if ( debug_data )
|
---|
[518] | 671 | {
|
---|
[520] | 672 | if ( !m_debug_affector_types )
|
---|
| 673 | m_debug_affector_types = new nv::hash_map< nv::particle_affector_func, nv::type_entry* >;
|
---|
| 674 |
|
---|
[518] | 675 | int you_could_get_rid_of_the_init_function_using_these; int error;
|
---|
[519] | 676 | int universal_vm_based_affector;
|
---|
| 677 | // compile_affector( ... ) in lua, returns array of bytecode
|
---|
| 678 | // function is a normal bytecode runner!
|
---|
[518] | 679 |
|
---|
[519] | 680 |
|
---|
[518] | 681 | (*m_debug_affector_types)[ nv_particle_affector_linear_force ]
|
---|
| 682 | = db->create_type<nvpe_linear_force_data>( "linear force" )
|
---|
| 683 | .field( "force_vector", &nvpe_linear_force_data::force_vector )
|
---|
| 684 | .field( "average", &nvpe_linear_force_data::average )
|
---|
| 685 | .get();
|
---|
| 686 |
|
---|
| 687 | ( *m_debug_affector_types )[nv_particle_affector_deflector_plane]
|
---|
| 688 | = db->create_type<nvpe_deflector_plane_data>( "deflector plane" )
|
---|
| 689 | .field( "plane_point", &nvpe_deflector_plane_data::plane_point )
|
---|
| 690 | .field( "plane_normal", &nvpe_deflector_plane_data::plane_normal )
|
---|
| 691 | .field( "bounce", &nvpe_deflector_plane_data::bounce )
|
---|
| 692 | .field( "distance", &nvpe_deflector_plane_data::distance )
|
---|
| 693 | .get();
|
---|
| 694 |
|
---|
| 695 | ( *m_debug_affector_types )[nv_particle_affector_color_fader]
|
---|
| 696 | = db->create_type<nvpe_linear_force_data>( "color fader" )
|
---|
| 697 | .field( "adjustment", & nvpe_color_fader_data::adjustment )
|
---|
| 698 | .get();
|
---|
| 699 |
|
---|
| 700 | ( *m_debug_affector_types )[nv_particle_affector_scaler]
|
---|
| 701 | = db->create_type<nvpe_scaler_data>( "scaler" )
|
---|
| 702 | .field( "adjustment", & nvpe_scaler_data::adjustment )
|
---|
| 703 | .get();
|
---|
| 704 | }
|
---|
| 705 | }
|
---|
[520] | 706 |
|
---|