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