1 | // Copyright (C) 2014-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/particle_engine.hh"
|
---|
8 |
|
---|
9 | #include <nv/interface/device.hh>
|
---|
10 | #include <nv/core/random.hh>
|
---|
11 | #include <nv/stl/utility.hh>
|
---|
12 | #include <nv/lua/lua_math.hh>
|
---|
13 | #include <nv/core/logging.hh>
|
---|
14 |
|
---|
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;
|
---|
17 |
|
---|
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 |
|
---|
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 |
|
---|
26 | using namespace nv;
|
---|
27 |
|
---|
28 | static void nv_particle_emitter_point( random_base*, const particle_emitter_data*, particle* p, uint32 count )
|
---|
29 | {
|
---|
30 | for ( uint32 i = 0; i < count; ++i )
|
---|
31 | {
|
---|
32 | p[i].position = vec3();
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | static void nv_particle_emitter_box( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
37 | {
|
---|
38 | for ( uint32 i = 0; i < count; ++i )
|
---|
39 | {
|
---|
40 | p[i].position =
|
---|
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;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | static void nv_particle_emitter_cylinder( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
48 | {
|
---|
49 | for ( uint32 i = 0; i < count; ++i )
|
---|
50 | {
|
---|
51 | vec2 rellipse( r->disk_point( pe->precise ) * pe->extents[0] );
|
---|
52 | p[i].position =
|
---|
53 | rellipse.x * pe->cdir +
|
---|
54 | r->frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
55 | rellipse.y * pe->odir;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | static void nv_particle_emitter_sphere( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
60 | {
|
---|
61 | for ( uint32 i = 0; i < count; ++i )
|
---|
62 | {
|
---|
63 | vec3 rsphere = r->sphere_point( pe->precise ) * pe->extents[0];
|
---|
64 | p[i].position =
|
---|
65 | rsphere.x * pe->cdir +
|
---|
66 | rsphere.y * pe->dir +
|
---|
67 | rsphere.z * pe->odir;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | static void nv_particle_emitter_cylindroid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
72 | {
|
---|
73 | for ( uint32 i = 0; i < count; ++i )
|
---|
74 | {
|
---|
75 | vec2 rellipse = r->ellipse_point( vec2( pe->hextents[0], pe->hextents[2] ), pe->precise );
|
---|
76 | p[i].position =
|
---|
77 | rellipse.x * pe->cdir +
|
---|
78 | r->frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
79 | rellipse.y * pe->odir;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | static void nv_particle_emitter_ellipsoid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
84 | {
|
---|
85 | for ( uint32 i = 0; i < count; ++i )
|
---|
86 | {
|
---|
87 | vec3 rsphere = r->ellipsoid_point( pe->hextents, pe->precise );
|
---|
88 | p[i].position =
|
---|
89 | rsphere.x * pe->cdir +
|
---|
90 | rsphere.y * pe->dir +
|
---|
91 | rsphere.z * pe->odir;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | static void nv_particle_emitter_hollow_cylinder( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
96 | {
|
---|
97 | for ( uint32 i = 0; i < count; ++i )
|
---|
98 | {
|
---|
99 | vec2 rellipse = r->hollow_disk_point(
|
---|
100 | pe->ihextents[0],
|
---|
101 | pe->hextents[0],
|
---|
102 | pe->precise );
|
---|
103 | p[i].position =
|
---|
104 | rellipse.x * pe->cdir +
|
---|
105 | r->frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
106 | rellipse.y * pe->odir;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | static void nv_particle_emitter_hollow_sphere( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
111 | {
|
---|
112 | for ( uint32 i = 0; i < count; ++i )
|
---|
113 | {
|
---|
114 | vec3 rellipse = r->hollow_sphere_point( pe->ihextents[0], pe->hextents[0], pe->precise );
|
---|
115 | p[i].position =
|
---|
116 | rellipse.x * pe->cdir +
|
---|
117 | rellipse.y * pe->dir +
|
---|
118 | rellipse.z * pe->odir;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | static void nv_particle_emitter_hollow_cylindroid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
123 | {
|
---|
124 | for ( uint32 i = 0; i < count; ++i )
|
---|
125 | {
|
---|
126 | vec2 rellipse = r->hollow_ellipse_point(
|
---|
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 +
|
---|
132 | r->frange( 0.0f, pe->extents[1] ) * pe->dir +
|
---|
133 | rellipse.y * pe->odir;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void nv_particle_emitter_hollow_ellipsoid( random_base* r, const particle_emitter_data* pe, particle* p, uint32 count )
|
---|
138 | {
|
---|
139 | for ( uint32 i = 0; i < count; ++i )
|
---|
140 | {
|
---|
141 | vec3 rellipse = r->hollow_ellipsoid_point( pe->ihextents, pe->hextents, pe->precise );
|
---|
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 |
|
---|
155 | NV_RTTI_DECLARE( nvpe_linear_force_data )
|
---|
156 |
|
---|
157 | static bool nv_particle_affector_linear_force_init( lua::table_guard* table, particle_affector_data* data )
|
---|
158 | {
|
---|
159 | nvpe_linear_force_data* datap = reinterpret_cast<nvpe_linear_force_data*>( data->paramters );
|
---|
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 | {
|
---|
167 | const nvpe_linear_force_data* datap = reinterpret_cast<const nvpe_linear_force_data*>( data->paramters );
|
---|
168 | if ( datap->average )
|
---|
169 | {
|
---|
170 | float norm_factor = nv::min( factor, 1.0f, p->lifetime );
|
---|
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 | {
|
---|
176 | vec3 scvector = datap->force_vector * nv::min( factor, p->lifetime );
|
---|
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 |
|
---|
189 | NV_RTTI_DECLARE( nvpe_deflector_plane_data )
|
---|
190 |
|
---|
191 | static bool nv_particle_affector_deflector_plane_init( lua::table_guard* table, particle_affector_data* data )
|
---|
192 | {
|
---|
193 | nvpe_deflector_plane_data* datap = reinterpret_cast<nvpe_deflector_plane_data*>( data->paramters );
|
---|
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 );
|
---|
198 | datap->distance = -math::dot( datap->plane_normal, datap->plane_point ) / sqrt( math::dot( datap->plane_normal, datap->plane_normal ) );
|
---|
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 | {
|
---|
204 | const nvpe_deflector_plane_data* datap = reinterpret_cast<const nvpe_deflector_plane_data*>( data->paramters );
|
---|
205 | for ( uint32 i = 0; i < count; ++i )
|
---|
206 | {
|
---|
207 | particle& pt = p[i];
|
---|
208 | vec3 direction = pt.velocity * nv::min( factor, p->lifetime );
|
---|
209 | if ( math::dot( datap->plane_normal, pt.position + direction ) + datap->distance <= 0.0f )
|
---|
210 | {
|
---|
211 | float val = math::dot( datap->plane_normal, pt.position ) + datap->distance;
|
---|
212 | if ( val > 0.0f )
|
---|
213 | {
|
---|
214 | vec3 part_dir = direction * ( -val / math::dot( datap->plane_normal, direction ) );
|
---|
215 | pt.position = pt.position + part_dir + ( part_dir - direction ) * datap->bounce;
|
---|
216 | pt.velocity = math::reflect( pt.velocity, datap->plane_normal ) * datap->bounce;
|
---|
217 | }
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | struct nvpe_color_fader_data
|
---|
223 | {
|
---|
224 | nv::vec4 adjustment;
|
---|
225 | };
|
---|
226 |
|
---|
227 | NV_RTTI_DECLARE( nvpe_color_fader_data )
|
---|
228 |
|
---|
229 | static bool nv_particle_affector_color_fader_init( lua::table_guard* table, particle_affector_data* data )
|
---|
230 | {
|
---|
231 | nvpe_color_fader_data* datap = reinterpret_cast<nvpe_color_fader_data*>( data->paramters );
|
---|
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 | {
|
---|
238 | const nvpe_color_fader_data* datap = reinterpret_cast<const nvpe_color_fader_data*>( data->paramters );
|
---|
239 | vec4 adjustment = datap->adjustment * nv::min( factor, p->lifetime );
|
---|
240 | for ( uint32 i = 0; i < count; ++i )
|
---|
241 | {
|
---|
242 | p[i].color = math::clamp( p[i].color + adjustment, 0.0f, 1.0f );
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | struct nvpe_scaler_data
|
---|
247 | {
|
---|
248 | nv::vec2 adjustment;
|
---|
249 | };
|
---|
250 |
|
---|
251 | NV_RTTI_DECLARE( nvpe_scaler_data )
|
---|
252 |
|
---|
253 | static bool nv_particle_affector_scaler_init( lua::table_guard* table, particle_affector_data* data )
|
---|
254 | {
|
---|
255 | nvpe_scaler_data* datap = reinterpret_cast<nvpe_scaler_data*>( data->paramters );
|
---|
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 | {
|
---|
263 | const nvpe_scaler_data* datap = reinterpret_cast<const nvpe_scaler_data*>( data->paramters );
|
---|
264 | vec2 adjustment = datap->adjustment * nv::min( factor, p->lifetime );
|
---|
265 | for ( uint32 i = 0; i < count; ++i )
|
---|
266 | {
|
---|
267 | p[i].size = math::max( p[i].size + adjustment, vec2() );
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | nv::particle_engine::particle_engine( context* a_context, random_base* rng, particle_group_manager* groups, bool debug_data )
|
---|
272 | {
|
---|
273 | m_context = a_context;
|
---|
274 | m_pgm = groups;
|
---|
275 | m_rng = rng;
|
---|
276 | if ( m_rng == nullptr )
|
---|
277 | m_rng = &random::get();
|
---|
278 |
|
---|
279 | if ( debug_data )
|
---|
280 | {
|
---|
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 >;
|
---|
283 | m_debug_emitter_list = new nv::vector< nv::particle_emitter_func >;
|
---|
284 | m_debug_affector_list = new nv::vector< nv::particle_affector_func >;
|
---|
285 | }
|
---|
286 |
|
---|
287 | register_standard_emitters();
|
---|
288 | register_standard_affectors();
|
---|
289 | }
|
---|
290 |
|
---|
291 | nv::particle_system nv::particle_engine::create_system( const particle_system_data* data, particle_group group )
|
---|
292 | {
|
---|
293 | if ( !m_pgm->ref( group ) )
|
---|
294 | return nv::particle_system();
|
---|
295 |
|
---|
296 | particle_system result = m_systems.create();
|
---|
297 | particle_system_info* info = m_systems.get( result );
|
---|
298 | info->group = group;
|
---|
299 | info->data = data;
|
---|
300 | uint32 ecount = data->emitter_count;
|
---|
301 | for ( uint32 i = 0; i < ecount; ++i )
|
---|
302 | {
|
---|
303 | info->emitters[i].active = true;
|
---|
304 | if ( data->emitters[i].duration_max == 0.0f )
|
---|
305 | info->emitters[i].pause = 0;
|
---|
306 | else
|
---|
307 | info->emitters[i].pause = m_rng->frange( data->emitters[i].duration_min, data->emitters[i].duration_max );
|
---|
308 |
|
---|
309 | }
|
---|
310 |
|
---|
311 | info->count = 0;
|
---|
312 | info->particles = new particle[data->quota];
|
---|
313 |
|
---|
314 | return result;
|
---|
315 | }
|
---|
316 |
|
---|
317 | nv::particle_system nv::particle_engine::create_system( resource< nv::particle_system_data > rdata, particle_group group )
|
---|
318 | {
|
---|
319 | if ( auto data = rdata.lock() )
|
---|
320 | return create_system( &*data, group );
|
---|
321 | return {};
|
---|
322 | }
|
---|
323 |
|
---|
324 | void nv::particle_engine::prepare( particle_group group )
|
---|
325 | {
|
---|
326 | m_pgm->prepare( group );
|
---|
327 | }
|
---|
328 |
|
---|
329 | nv::particle_group nv::particle_engine::create_group( uint32 max_particles )
|
---|
330 | {
|
---|
331 | return m_pgm->create_group( max_particles );
|
---|
332 | }
|
---|
333 |
|
---|
334 | nv::particle_engine::~particle_engine()
|
---|
335 | {
|
---|
336 | clear();
|
---|
337 | }
|
---|
338 |
|
---|
339 | void nv::particle_engine::reset()
|
---|
340 | {
|
---|
341 | clear();
|
---|
342 | register_standard_emitters( );
|
---|
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 ) );
|
---|
350 | if ( m_pgm )
|
---|
351 | m_pgm->reset();
|
---|
352 | m_emitters.clear();
|
---|
353 | m_affectors.clear();
|
---|
354 | }
|
---|
355 |
|
---|
356 |
|
---|
357 | void nv::particle_engine::release( particle_system system )
|
---|
358 | {
|
---|
359 | particle_system_info* info = m_systems.get( system );
|
---|
360 | if ( info )
|
---|
361 | {
|
---|
362 | m_pgm->unref( info->group );
|
---|
363 | delete[] info->particles;
|
---|
364 | m_systems.destroy( system );
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | void nv::particle_engine::release( particle_group group )
|
---|
369 | {
|
---|
370 | m_pgm->release( group );
|
---|
371 | }
|
---|
372 |
|
---|
373 | void nv::particle_engine::render( particle_system system, const scene_state& s )
|
---|
374 | {
|
---|
375 | particle_system_info* info = m_systems.get( system );
|
---|
376 | if ( info )
|
---|
377 | m_pgm->generate_data( array_view< particle >( info->particles, info->count ), info->data, info->group, s );
|
---|
378 | }
|
---|
379 |
|
---|
380 | void nv::particle_engine::update( particle_system system, transform model, float dtime )
|
---|
381 | {
|
---|
382 | particle_system_info* info = m_systems.get( system );
|
---|
383 | if ( info )
|
---|
384 | {
|
---|
385 | // while ( dtime > 0.2 )
|
---|
386 | // {
|
---|
387 | // update_emitters( info, 0.2 );
|
---|
388 | // destroy_particles( info, 0.2 );
|
---|
389 | // create_particles( info, 0.2 );
|
---|
390 | // update_particles( info, 0.2 );
|
---|
391 | // dtime -= 0.2;
|
---|
392 | // }
|
---|
393 |
|
---|
394 | update_emitters( info, dtime );
|
---|
395 | destroy_particles( info, dtime );
|
---|
396 | create_particles( info, model, dtime );
|
---|
397 | update_particles( info, dtime );
|
---|
398 |
|
---|
399 | // generate_data( info );
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | void nv::particle_engine::set_texcoords( particle_system system, vec2 a, vec2 b )
|
---|
404 | {
|
---|
405 |
|
---|
406 | particle_system_info* info = m_systems.get( system );
|
---|
407 | if ( info )
|
---|
408 | {
|
---|
409 | info->texcoords[0] = a;
|
---|
410 | info->texcoords[1] = b;
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | void nv::particle_engine::destroy_particles( particle_system_info* info, float dtime )
|
---|
415 | {
|
---|
416 | if ( info->count > 0 )
|
---|
417 | for ( sint32 i = sint32( info->count ) - 1; i >= 0; --i )
|
---|
418 | {
|
---|
419 | particle& pinfo = info->particles[i];
|
---|
420 | pinfo.lifetime += dtime;
|
---|
421 | if ( pinfo.lifetime >= pinfo.death )
|
---|
422 | {
|
---|
423 | info->count--;
|
---|
424 | swap( info->particles[i], info->particles[info->count] );
|
---|
425 | }
|
---|
426 | }
|
---|
427 | }
|
---|
428 |
|
---|
429 | void nv::particle_engine::create_particles( particle_system_info* info, transform model, float dtime )
|
---|
430 | {
|
---|
431 | uint32 ecount = info->data->emitter_count;
|
---|
432 | if ( ecount == 0 ) return;
|
---|
433 |
|
---|
434 | bool local = model.is_identity();
|
---|
435 | // if ( !local )
|
---|
436 | // {
|
---|
437 | // source = vec3( m_model_matrix[3] );
|
---|
438 | // orient = mat3( m_model_matrix );
|
---|
439 | // }
|
---|
440 |
|
---|
441 | for ( uint32 i = 0; i < ecount; ++i )
|
---|
442 | {
|
---|
443 | const auto& edata = info->data->emitters[i];
|
---|
444 | auto& einfo = info->emitters[i];
|
---|
445 | if ( einfo.active )
|
---|
446 | {
|
---|
447 | einfo.next -= dtime;
|
---|
448 | float fperiod = 1.0f / edata.rate;
|
---|
449 | while ( einfo.next < 0.0f )
|
---|
450 | {
|
---|
451 | if ( info->count < info->data->quota-1 )
|
---|
452 | {
|
---|
453 | particle& pinfo = info->particles[info->count];
|
---|
454 | edata.emitter_func( m_rng, &(info->data->emitters[i]), &pinfo, 1 );
|
---|
455 | pinfo.position = vec3();
|
---|
456 | pinfo.position+= edata.position;
|
---|
457 | // if ( !local )
|
---|
458 | pinfo.position = pinfo.position * model;
|
---|
459 | pinfo.color = edata.color_min == edata.color_max ?
|
---|
460 | edata.color_min : m_rng->range( edata.color_min, edata.color_max );
|
---|
461 | pinfo.size = edata.size_min == edata.size_max ?
|
---|
462 | edata.size_min : m_rng->range( edata.size_min, edata.size_max );
|
---|
463 | if ( edata.square ) pinfo.size.y = pinfo.size.x;
|
---|
464 | float velocity = edata.velocity_min == edata.velocity_max ?
|
---|
465 | edata.velocity_min : m_rng->frange( edata.velocity_min, edata.velocity_max );
|
---|
466 | pinfo.lifetime = dtime + einfo.next;
|
---|
467 | pinfo.death = ( edata.lifetime_min == edata.lifetime_max ?
|
---|
468 | edata.lifetime_min : m_rng->frange( edata.lifetime_min, edata.lifetime_max ) );
|
---|
469 | pinfo.rotation = m_rng->frand( 2* math::pi<float>() );
|
---|
470 | pinfo.tcoord_a = info->texcoords[0];
|
---|
471 | pinfo.tcoord_b = info->texcoords[1];
|
---|
472 |
|
---|
473 | pinfo.velocity = edata.dir;
|
---|
474 | if ( edata.angle > 0.0f )
|
---|
475 | {
|
---|
476 | float emission_angle = math::radians( edata.angle );
|
---|
477 | float cos_theta = m_rng->frange( cos( emission_angle ), 1.0f );
|
---|
478 | float sin_theta = sqrt(1.0f - cos_theta * cos_theta );
|
---|
479 | float phi = m_rng->frange( 0.0f, 2* math::pi<float>() );
|
---|
480 | pinfo.velocity = model.get_orientation() *
|
---|
481 | ( edata.odir * ( cos(phi) * sin_theta ) +
|
---|
482 | edata.cdir * ( sin(phi)*sin_theta ) +
|
---|
483 | edata.dir * cos_theta );
|
---|
484 | }
|
---|
485 |
|
---|
486 | pinfo.velocity *= velocity;
|
---|
487 |
|
---|
488 | info->count++;
|
---|
489 | }
|
---|
490 | einfo.next += fperiod;
|
---|
491 | }
|
---|
492 |
|
---|
493 | }
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 | void nv::particle_engine::update_particles( particle_system_info* info, float dtime )
|
---|
498 | {
|
---|
499 | if ( dtime <= 0.0f ) return;
|
---|
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]);
|
---|
505 | padata->process( padata, info->particles, dtime, info->count );
|
---|
506 | }
|
---|
507 |
|
---|
508 |
|
---|
509 | for ( uint32 i = 0; i < info->count; ++i )
|
---|
510 | {
|
---|
511 | particle& pdata = info->particles[i];
|
---|
512 | float factor = min( dtime, pdata.lifetime );
|
---|
513 | pdata.position += pdata.velocity * factor;
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | void nv::particle_engine::update_emitters( particle_system_info* info, float dtime )
|
---|
518 | {
|
---|
519 | uint32 ecount = info->data->emitter_count;
|
---|
520 | if ( ecount == 0 ) return;
|
---|
521 |
|
---|
522 | for ( uint32 i = 0; i < ecount; ++i )
|
---|
523 | {
|
---|
524 | const auto& edata = info->data->emitters[i];
|
---|
525 | auto& einfo = info->emitters[i];
|
---|
526 |
|
---|
527 | if ( einfo.pause > 0.0f )
|
---|
528 | {
|
---|
529 | einfo.pause -= dtime;
|
---|
530 | if ( einfo.pause == 0.0f ) einfo.pause = -0.001f;
|
---|
531 | }
|
---|
532 |
|
---|
533 | if ( einfo.pause < 0.0f )
|
---|
534 | {
|
---|
535 | if ( einfo.active )
|
---|
536 | {
|
---|
537 | einfo.active = false;
|
---|
538 | if ( edata.repeat_min > 0.0f )
|
---|
539 | einfo.pause += m_rng->frange( edata.repeat_min, edata.repeat_max );
|
---|
540 | else
|
---|
541 | einfo.pause = 0.0f;
|
---|
542 | }
|
---|
543 | else
|
---|
544 | {
|
---|
545 | einfo.active = true;
|
---|
546 | einfo.pause += m_rng->frange( edata.duration_min, edata.duration_max );
|
---|
547 | }
|
---|
548 | }
|
---|
549 | }
|
---|
550 |
|
---|
551 | }
|
---|
552 |
|
---|
553 | void nv::particle_engine::register_emitter_type( const string_view& name, particle_emitter_func func )
|
---|
554 | {
|
---|
555 | if ( m_debug_emitter_names )
|
---|
556 | ( *m_debug_emitter_names )[func] = name;
|
---|
557 | if ( m_debug_emitter_list )
|
---|
558 | ( *m_debug_emitter_list ).push_back( func );
|
---|
559 |
|
---|
560 | m_emitters[ name ] = func;
|
---|
561 | }
|
---|
562 |
|
---|
563 | void nv::particle_engine::register_standard_emitters()
|
---|
564 | {
|
---|
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 );
|
---|
575 | }
|
---|
576 |
|
---|
577 | void nv::particle_engine::register_affector_type( const string_view& name, particle_affector_init_func init, particle_affector_func process )
|
---|
578 | {
|
---|
579 | if ( m_debug_affector_names )
|
---|
580 | ( *m_debug_affector_names )[process] = name;
|
---|
581 | if ( m_debug_affector_list )
|
---|
582 | ( *m_debug_affector_list ).push_back( process );
|
---|
583 |
|
---|
584 | m_affectors[ name ].init = init;
|
---|
585 | m_affectors[ name ].process = process;
|
---|
586 | }
|
---|
587 |
|
---|
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 |
|
---|
624 | nv::particle_render_data nv::particle_engine::get_render_data( particle_group group )
|
---|
625 | {
|
---|
626 | return m_pgm->get_render_data( group );
|
---|
627 | }
|
---|
628 |
|
---|
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 |
|
---|
637 | const nv::type_entry* nv::particle_engine::get_debug_type( particle_affector_func f )
|
---|
638 | {
|
---|
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 | {
|
---|
650 | db->create_type<particle_orientation>()
|
---|
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 |
|
---|
658 | db->create_type<particle_origin>()
|
---|
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 |
|
---|
670 | if ( debug_data )
|
---|
671 | {
|
---|
672 | if ( !m_debug_affector_types )
|
---|
673 | m_debug_affector_types = new nv::hash_map< nv::particle_affector_func, nv::type_entry* >;
|
---|
674 |
|
---|
675 | int you_could_get_rid_of_the_init_function_using_these; int error;
|
---|
676 | int universal_vm_based_affector;
|
---|
677 | // compile_affector( ... ) in lua, returns array of bytecode
|
---|
678 | // function is a normal bytecode runner!
|
---|
679 |
|
---|
680 |
|
---|
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 | }
|
---|
706 |
|
---|