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