forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_effects.cpp
2221 lines (1771 loc) · 63.9 KB
/
c_effects.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include "cbase.h"
#include "c_tracer.h"
#include "view.h"
#include "initializer.h"
#include "particles_simple.h"
#include "env_wind_shared.h"
#include "engine/IEngineTrace.h"
#include "engine/ivmodelinfo.h"
#include "precipitation_shared.h"
#include "fx_water.h"
#include "c_world.h"
#include "iviewrender.h"
#include "engine/ivdebugoverlay.h"
#include "clienteffectprecachesystem.h"
#include "collisionutils.h"
#include "tier0/vprof.h"
#include "viewrender.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
ConVar cl_winddir ( "cl_winddir", "0", FCVAR_CHEAT, "Weather effects wind direction angle" );
ConVar cl_windspeed ( "cl_windspeed", "0", FCVAR_CHEAT, "Weather effects wind speed scalar" );
Vector g_vSplashColor( 0.5, 0.5, 0.5 );
float g_flSplashScale = 0.15;
float g_flSplashLifetime = 0.5f;
float g_flSplashAlpha = 0.3f;
ConVar r_RainSplashPercentage( "r_RainSplashPercentage", "20", FCVAR_CHEAT ); // N% chance of a rain particle making a splash.
float GUST_INTERVAL_MIN = 1;
float GUST_INTERVAL_MAX = 2;
float GUST_LIFETIME_MIN = 1;
float GUST_LIFETIME_MAX = 3;
float MIN_SCREENSPACE_RAIN_WIDTH = 1;
#ifndef _XBOX
ConVar r_RainHack( "r_RainHack", "0", FCVAR_CHEAT );
ConVar r_RainRadius( "r_RainRadius", "1500", FCVAR_CHEAT );
ConVar r_RainSideVel( "r_RainSideVel", "130", FCVAR_CHEAT, "How much sideways velocity rain gets." );
ConVar r_RainSimulate( "r_RainSimulate", "1", FCVAR_CHEAT, "Enable/disable rain simulation." );
ConVar r_DrawRain( "r_DrawRain", "1", FCVAR_CHEAT, "Enable/disable rain rendering." );
ConVar r_RainProfile( "r_RainProfile", "0", FCVAR_CHEAT, "Enable/disable rain profiling." );
//Precahce the effects
CLIENTEFFECT_REGISTER_BEGIN( PrecachePrecipitation )
CLIENTEFFECT_MATERIAL( "particle/rain" )
CLIENTEFFECT_MATERIAL( "particle/snow" )
CLIENTEFFECT_REGISTER_END()
//-----------------------------------------------------------------------------
// Precipitation particle type
//-----------------------------------------------------------------------------
class CPrecipitationParticle
{
public:
Vector m_Pos;
Vector m_Velocity;
float m_SpawnTime; // Note: Tweak with this to change lifetime
float m_Mass;
float m_Ramp;
float m_flCurLifetime;
float m_flMaxLifetime;
};
class CClient_Precipitation;
static CUtlVector<CClient_Precipitation*> g_Precipitations;
//===========
// Snow fall
//===========
class CSnowFallManager;
static CSnowFallManager *s_pSnowFallMgr = NULL;
bool SnowFallManagerCreate( CClient_Precipitation *pSnowEntity );
void SnowFallManagerDestroy( void );
class AshDebrisEffect : public CSimpleEmitter
{
public:
AshDebrisEffect( const char *pDebugName ) : CSimpleEmitter( pDebugName ) {}
static AshDebrisEffect* Create( const char *pDebugName );
virtual float UpdateAlpha( const SimpleParticle *pParticle );
virtual float UpdateRoll( SimpleParticle *pParticle, float timeDelta );
private:
AshDebrisEffect( const AshDebrisEffect & );
};
//-----------------------------------------------------------------------------
// Precipitation base entity
//-----------------------------------------------------------------------------
class CClient_Precipitation : public C_BaseEntity
{
class CPrecipitationEffect;
friend class CClient_Precipitation::CPrecipitationEffect;
public:
DECLARE_CLASS( CClient_Precipitation, C_BaseEntity );
DECLARE_CLIENTCLASS();
CClient_Precipitation();
virtual ~CClient_Precipitation();
// Inherited from C_BaseEntity
virtual void Precache( );
void Render();
private:
// Creates a single particle
CPrecipitationParticle* CreateParticle();
virtual void OnDataChanged( DataUpdateType_t updateType );
virtual void ClientThink();
void Simulate( float dt );
// Renders the particle
void RenderParticle( CPrecipitationParticle* pParticle, CMeshBuilder &mb );
void CreateWaterSplashes();
// Emits the actual particles
void EmitParticles( float fTimeDelta );
// Computes where we're gonna emit
bool ComputeEmissionArea( Vector& origin, Vector2D& size );
// Gets the tracer width and speed
float GetWidth() const;
float GetLength() const;
float GetSpeed() const;
// Gets the remaining lifetime of the particle
float GetRemainingLifetime( CPrecipitationParticle* pParticle ) const;
// Computes the wind vector
static void ComputeWindVector( );
// simulation methods
bool SimulateRain( CPrecipitationParticle* pParticle, float dt );
bool SimulateSnow( CPrecipitationParticle* pParticle, float dt );
void CreateAshParticle( void );
void CreateRainOrSnowParticle( Vector vSpawnPosition, Vector vVelocity );
// Information helpful in creating and rendering particles
IMaterial *m_MatHandle; // material used
float m_Color[4]; // precip color
float m_Lifetime; // Precip lifetime
float m_InitialRamp; // Initial ramp value
float m_Speed; // Precip speed
float m_Width; // Tracer width
float m_Remainder; // particles we should render next time
PrecipitationType_t m_nPrecipType; // Precip type
float m_flHalfScreenWidth; // Precalculated each frame.
float m_flDensity;
// Some state used in rendering and simulation
// Used to modify the rain density and wind from the console
static ConVar s_raindensity;
static ConVar s_rainwidth;
static ConVar s_rainlength;
static ConVar s_rainspeed;
static Vector s_WindVector; // Stores the wind speed vector
CUtlLinkedList<CPrecipitationParticle> m_Particles;
CUtlVector<Vector> m_Splashes;
CSmartPtr<AshDebrisEffect> m_pAshEmitter;
TimedEvent m_tAshParticleTimer;
TimedEvent m_tAshParticleTraceTimer;
bool m_bActiveAshEmitter;
Vector m_vAshSpawnOrigin;
int m_iAshCount;
private:
CClient_Precipitation( const CClient_Precipitation & ); // not defined, not accessible
};
// Just receive the normal data table stuff
IMPLEMENT_CLIENTCLASS_DT(CClient_Precipitation, DT_Precipitation, CPrecipitation)
RecvPropInt( RECVINFO( m_nPrecipType ) )
END_RECV_TABLE()
static ConVar r_SnowEnable( "r_SnowEnable", "1", FCVAR_CHEAT, "Snow Enable" );
static ConVar r_SnowParticles( "r_SnowParticles", "500", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowInsideRadius( "r_SnowInsideRadius", "256", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowOutsideRadius( "r_SnowOutsideRadius", "1024", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowSpeedScale( "r_SnowSpeedScale", "1", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowPosScale( "r_SnowPosScale", "1", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowFallSpeed( "r_SnowFallSpeed", "1.5", FCVAR_CHEAT, "Snow fall speed scale." );
static ConVar r_SnowWindScale( "r_SnowWindScale", "0.0035", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowDebugBox( "r_SnowDebugBox", "0", FCVAR_CHEAT, "Snow Debug Boxes." );
static ConVar r_SnowZoomOffset( "r_SnowZoomOffset", "384.0f", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowZoomRadius( "r_SnowZoomRadius", "512.0f", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowStartAlpha( "r_SnowStartAlpha", "25", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowEndAlpha( "r_SnowEndAlpha", "255", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowColorRed( "r_SnowColorRed", "150", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowColorGreen( "r_SnowColorGreen", "175", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowColorBlue( "r_SnowColorBlue", "200", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowStartSize( "r_SnowStartSize", "1", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowEndSize( "r_SnowEndSize", "0", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowRayLength( "r_SnowRayLength", "8192.0f", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowRayRadius( "r_SnowRayRadius", "256", FCVAR_CHEAT, "Snow." );
static ConVar r_SnowRayEnable( "r_SnowRayEnable", "1", FCVAR_CHEAT, "Snow." );
void DrawPrecipitation()
{
for ( int i=0; i < g_Precipitations.Count(); i++ )
{
g_Precipitations[i]->Render();
}
}
//-----------------------------------------------------------------------------
// determines if a weather particle has hit something other than air
//-----------------------------------------------------------------------------
static bool IsInAir( const Vector& position )
{
int contents = enginetrace->GetPointContents( position );
return (contents & CONTENTS_SOLID) == 0;
}
//-----------------------------------------------------------------------------
// Globals
//-----------------------------------------------------------------------------
ConVar CClient_Precipitation::s_raindensity( "r_raindensity","0.001", FCVAR_CHEAT);
ConVar CClient_Precipitation::s_rainwidth( "r_rainwidth", "0.5", FCVAR_CHEAT );
ConVar CClient_Precipitation::s_rainlength( "r_rainlength", "0.1f", FCVAR_CHEAT );
ConVar CClient_Precipitation::s_rainspeed( "r_rainspeed", "600.0f", FCVAR_CHEAT );
ConVar r_rainalpha( "r_rainalpha", "0.4", FCVAR_CHEAT );
ConVar r_rainalphapow( "r_rainalphapow", "0.8", FCVAR_CHEAT );
Vector CClient_Precipitation::s_WindVector; // Stores the wind speed vector
void CClient_Precipitation::OnDataChanged( DataUpdateType_t updateType )
{
// Simulate every frame.
if ( updateType == DATA_UPDATE_CREATED )
{
SetNextClientThink( CLIENT_THINK_ALWAYS );
if ( m_nPrecipType == PRECIPITATION_TYPE_SNOWFALL )
{
SnowFallManagerCreate( this );
}
}
m_flDensity = RemapVal( m_clrRender->a, 0, 255, 0, 0.001 );
BaseClass::OnDataChanged( updateType );
}
void CClient_Precipitation::ClientThink()
{
Simulate( gpGlobals->frametime );
}
//-----------------------------------------------------------------------------
//
// Utility methods for the various simulation functions
//
//-----------------------------------------------------------------------------
inline bool CClient_Precipitation::SimulateRain( CPrecipitationParticle* pParticle, float dt )
{
if (GetRemainingLifetime( pParticle ) < 0.0f)
return false;
Vector vOldPos = pParticle->m_Pos;
// Update position
VectorMA( pParticle->m_Pos, dt, pParticle->m_Velocity,
pParticle->m_Pos );
// wind blows rain around
for ( int i = 0 ; i < 2 ; i++ )
{
if ( pParticle->m_Velocity[i] < s_WindVector[i] )
{
pParticle->m_Velocity[i] += ( 5 / pParticle->m_Mass );
// clamp
if ( pParticle->m_Velocity[i] > s_WindVector[i] )
pParticle->m_Velocity[i] = s_WindVector[i];
}
else if (pParticle->m_Velocity[i] > s_WindVector[i] )
{
pParticle->m_Velocity[i] -= ( 5 / pParticle->m_Mass );
// clamp.
if ( pParticle->m_Velocity[i] < s_WindVector[i] )
pParticle->m_Velocity[i] = s_WindVector[i];
}
}
// No longer in the air? punt.
if ( !IsInAir( pParticle->m_Pos ) )
{
// Possibly make a splash if we hit a water surface and it's in front of the view.
if ( m_Splashes.Count() < 20 )
{
if ( RandomInt( 0, 100 ) < r_RainSplashPercentage.GetInt() )
{
trace_t trace;
UTIL_TraceLine(vOldPos, pParticle->m_Pos, MASK_WATER, NULL, COLLISION_GROUP_NONE, &trace);
if( trace.fraction < 1 )
{
m_Splashes.AddToTail( trace.endpos );
}
}
}
// Tell the framework it's time to remove the particle from the list
return false;
}
// We still want this particle
return true;
}
inline bool CClient_Precipitation::SimulateSnow( CPrecipitationParticle* pParticle, float dt )
{
if ( IsInAir( pParticle->m_Pos ) )
{
// Update position
VectorMA( pParticle->m_Pos, dt, pParticle->m_Velocity,
pParticle->m_Pos );
// wind blows rain around
for ( int i = 0 ; i < 2 ; i++ )
{
if ( pParticle->m_Velocity[i] < s_WindVector[i] )
{
pParticle->m_Velocity[i] += ( 5.0f / pParticle->m_Mass );
// accelerating flakes get a trail
pParticle->m_Ramp = 0.5f;
// clamp
if ( pParticle->m_Velocity[i] > s_WindVector[i] )
pParticle->m_Velocity[i] = s_WindVector[i];
}
else if (pParticle->m_Velocity[i] > s_WindVector[i] )
{
pParticle->m_Velocity[i] -= ( 5.0f / pParticle->m_Mass );
// accelerating flakes get a trail
pParticle->m_Ramp = 0.5f;
// clamp.
if ( pParticle->m_Velocity[i] < s_WindVector[i] )
pParticle->m_Velocity[i] = s_WindVector[i];
}
}
return true;
}
// Kill the particle immediately!
return false;
}
void CClient_Precipitation::Simulate( float dt )
{
// NOTE: When client-side prechaching works, we need to remove this
Precache();
m_flHalfScreenWidth = (float)ScreenWidth() / 2;
// Our sim methods needs dt and wind vector
if ( dt )
{
ComputeWindVector( );
}
if ( m_nPrecipType == PRECIPITATION_TYPE_ASH )
{
CreateAshParticle();
return;
}
// The snow fall manager handles the simulation.
if ( m_nPrecipType == PRECIPITATION_TYPE_SNOWFALL )
return;
// calculate the max amount of time it will take this flake to fall.
// This works if we assume the wind doesn't have a z component
if ( r_RainHack.GetInt() )
m_Lifetime = (GetClientWorldEntity()->m_WorldMaxs[2] - GetClientWorldEntity()->m_WorldMins[2]) / m_Speed;
else
m_Lifetime = (WorldAlignMaxs()[2] - WorldAlignMins()[2]) / m_Speed;
if ( !r_RainSimulate.GetInt() )
return;
CFastTimer timer;
timer.Start();
// Emit new particles
EmitParticles( dt );
// Simulate all the particles.
int iNext;
if ( m_nPrecipType == PRECIPITATION_TYPE_RAIN )
{
for ( int i=m_Particles.Head(); i != m_Particles.InvalidIndex(); i=iNext )
{
iNext = m_Particles.Next( i );
if ( !SimulateRain( &m_Particles[i], dt ) )
m_Particles.Remove( i );
}
}
else if ( m_nPrecipType == PRECIPITATION_TYPE_SNOW )
{
for ( int i=m_Particles.Head(); i != m_Particles.InvalidIndex(); i=iNext )
{
iNext = m_Particles.Next( i );
if ( !SimulateSnow( &m_Particles[i], dt ) )
m_Particles.Remove( i );
}
}
if ( r_RainProfile.GetInt() )
{
timer.End();
engine->Con_NPrintf( 15, "Rain simulation: %du (%d tracers)", timer.GetDuration().GetMicroseconds(), m_Particles.Count() );
}
}
//-----------------------------------------------------------------------------
// tracer rendering
//-----------------------------------------------------------------------------
inline void CClient_Precipitation::RenderParticle( CPrecipitationParticle* pParticle, CMeshBuilder &mb )
{
float scale;
Vector start, delta;
if ( m_nPrecipType == PRECIPITATION_TYPE_ASH )
return;
if ( m_nPrecipType == PRECIPITATION_TYPE_SNOWFALL )
return;
// make streaks 0.1 seconds long, but prevent from going past end
float lifetimeRemaining = GetRemainingLifetime( pParticle );
if (lifetimeRemaining >= GetLength())
scale = GetLength() * pParticle->m_Ramp;
else
scale = lifetimeRemaining * pParticle->m_Ramp;
// NOTE: We need to do everything in screen space
Vector3DMultiplyPosition( CurrentWorldToViewMatrix(), pParticle->m_Pos, start );
if ( start.z > -1 )
return;
Vector3DMultiply( CurrentWorldToViewMatrix(), pParticle->m_Velocity, delta );
// give a spiraling pattern to snow particles
if ( m_nPrecipType == PRECIPITATION_TYPE_SNOW )
{
Vector spiral, camSpiral;
float s, c;
if ( pParticle->m_Mass > 1.0f )
{
SinCos( gpGlobals->curtime * M_PI * (1+pParticle->m_Mass * 0.1f) +
pParticle->m_Mass * 5.0f, &s , &c );
// only spiral particles with a mass > 1, so some fall straight down
spiral[0] = 28 * c;
spiral[1] = 28 * s;
spiral[2] = 0.0f;
Vector3DMultiply( CurrentWorldToViewMatrix(), spiral, camSpiral );
// X and Y are measured in world space; need to convert to camera space
VectorAdd( start, camSpiral, start );
VectorAdd( delta, camSpiral, delta );
}
// shrink the trails on spiraling flakes.
pParticle->m_Ramp = 0.3f;
}
delta[0] *= scale;
delta[1] *= scale;
delta[2] *= scale;
// See c_tracer.* for this method
float flAlpha = r_rainalpha.GetFloat();
float flWidth = GetWidth();
float flScreenSpaceWidth = flWidth * m_flHalfScreenWidth / -start.z;
if ( flScreenSpaceWidth < MIN_SCREENSPACE_RAIN_WIDTH )
{
// Make the rain tracer at least the min size, but fade its alpha the smaller it gets.
flAlpha *= flScreenSpaceWidth / MIN_SCREENSPACE_RAIN_WIDTH;
flWidth = MIN_SCREENSPACE_RAIN_WIDTH * -start.z / m_flHalfScreenWidth;
}
flAlpha = pow( flAlpha, r_rainalphapow.GetFloat() );
float flColor[4] = { 1, 1, 1, flAlpha };
Tracer_Draw( &mb, start, delta, flWidth, flColor, 1 );
}
void CClient_Precipitation::CreateWaterSplashes()
{
for ( int i=0; i < m_Splashes.Count(); i++ )
{
Vector vSplash = m_Splashes[i];
if ( CurrentViewForward().Dot( vSplash - CurrentViewOrigin() ) > 1 )
{
FX_WaterRipple( vSplash, g_flSplashScale, &g_vSplashColor, g_flSplashLifetime, g_flSplashAlpha );
}
}
m_Splashes.Purge();
}
void CClient_Precipitation::Render()
{
if ( !r_DrawRain.GetInt() )
return;
// Don't render in monitors or in reflections or refractions.
if ( CurrentViewID() == VIEW_MONITOR )
return;
if ( view->GetDrawFlags() & (DF_RENDER_REFLECTION | DF_RENDER_REFRACTION) )
return;
if ( m_nPrecipType == PRECIPITATION_TYPE_ASH )
return;
if ( m_nPrecipType == PRECIPITATION_TYPE_SNOWFALL )
return;
// Create any queued up water splashes.
CreateWaterSplashes();
CFastTimer timer;
timer.Start();
CMatRenderContextPtr pRenderContext( materials );
// We want to do our calculations in view space.
VMatrix tempView;
pRenderContext->GetMatrix( MATERIAL_VIEW, &tempView );
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->LoadIdentity();
// Force the user clip planes to use the old view matrix
pRenderContext->EnableUserClipTransformOverride( true );
pRenderContext->UserClipTransform( tempView );
// Draw all the rain tracers.
pRenderContext->Bind( m_MatHandle );
IMesh *pMesh = pRenderContext->GetDynamicMesh();
if ( pMesh )
{
CMeshBuilder mb;
mb.Begin( pMesh, MATERIAL_QUADS, m_Particles.Count() );
for ( int i=m_Particles.Head(); i != m_Particles.InvalidIndex(); i=m_Particles.Next( i ) )
{
CPrecipitationParticle *p = &m_Particles[i];
RenderParticle( p, mb );
}
mb.End( false, true );
}
pRenderContext->EnableUserClipTransformOverride( false );
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->LoadMatrix( tempView );
if ( r_RainProfile.GetInt() )
{
timer.End();
engine->Con_NPrintf( 16, "Rain render : %du", timer.GetDuration().GetMicroseconds() );
}
}
//-----------------------------------------------------------------------------
// Constructor, destructor
//-----------------------------------------------------------------------------
CClient_Precipitation::CClient_Precipitation() : m_Remainder(0.0f)
{
m_nPrecipType = PRECIPITATION_TYPE_RAIN;
m_MatHandle = INVALID_MATERIAL_HANDLE;
m_flHalfScreenWidth = 1;
g_Precipitations.AddToTail( this );
}
CClient_Precipitation::~CClient_Precipitation()
{
g_Precipitations.FindAndRemove( this );
SnowFallManagerDestroy();
}
//-----------------------------------------------------------------------------
// Precache data
//-----------------------------------------------------------------------------
#define SNOW_SPEED 80.0f
#define RAIN_SPEED 425.0f
#define RAIN_TRACER_WIDTH 0.35f
#define SNOW_TRACER_WIDTH 0.7f
void CClient_Precipitation::Precache( )
{
if ( !m_MatHandle )
{
// Compute precipitation emission speed
switch( m_nPrecipType )
{
case PRECIPITATION_TYPE_SNOW:
m_Speed = SNOW_SPEED;
m_MatHandle = materials->FindMaterial( "particle/snow", TEXTURE_GROUP_CLIENT_EFFECTS );
m_InitialRamp = 0.6f;
m_Width = SNOW_TRACER_WIDTH;
break;
case PRECIPITATION_TYPE_RAIN:
Assert( m_nPrecipType == PRECIPITATION_TYPE_RAIN );
m_Speed = RAIN_SPEED;
m_MatHandle = materials->FindMaterial( "particle/rain", TEXTURE_GROUP_CLIENT_EFFECTS );
m_InitialRamp = 1.0f;
m_Color[3] = 1.0f; // make translucent
m_Width = RAIN_TRACER_WIDTH;
break;
default:
m_InitialRamp = 1.0f;
m_Color[3] = 1.0f; // make translucent
break;
}
// Store off the color
m_Color[0] = 1.0f;
m_Color[1] = 1.0f;
m_Color[2] = 1.0f;
}
}
//-----------------------------------------------------------------------------
// Gets the tracer width and speed
//-----------------------------------------------------------------------------
inline float CClient_Precipitation::GetWidth() const
{
// return m_Width;
return s_rainwidth.GetFloat();
}
inline float CClient_Precipitation::GetLength() const
{
// return m_Length;
return s_rainlength.GetFloat();
}
inline float CClient_Precipitation::GetSpeed() const
{
// return m_Speed;
return s_rainspeed.GetFloat();
}
//-----------------------------------------------------------------------------
// Gets the remaining lifetime of the particle
//-----------------------------------------------------------------------------
inline float CClient_Precipitation::GetRemainingLifetime( CPrecipitationParticle* pParticle ) const
{
float timeSinceSpawn = gpGlobals->curtime - pParticle->m_SpawnTime;
return m_Lifetime - timeSinceSpawn;
}
//-----------------------------------------------------------------------------
// Creates a particle
//-----------------------------------------------------------------------------
inline CPrecipitationParticle* CClient_Precipitation::CreateParticle()
{
int i = m_Particles.AddToTail();
CPrecipitationParticle* pParticle = &m_Particles[i];
pParticle->m_SpawnTime = gpGlobals->curtime;
pParticle->m_Ramp = m_InitialRamp;
return pParticle;
}
//-----------------------------------------------------------------------------
// Compute the emission area
//-----------------------------------------------------------------------------
bool CClient_Precipitation::ComputeEmissionArea( Vector& origin, Vector2D& size )
{
// FIXME: Compute the precipitation area based on computational power
float emissionSize = r_RainRadius.GetFloat(); // size of box to emit particles in
Vector vMins = WorldAlignMins();
Vector vMaxs = WorldAlignMaxs();
if ( r_RainHack.GetInt() )
{
vMins = GetClientWorldEntity()->m_WorldMins;
vMaxs = GetClientWorldEntity()->m_WorldMaxs;
}
// calculate a volume around the player to snow in. Intersect this big magic
// box around the player with the volume of the current environmental ent.
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return false;
// Determine how much time it'll take a falling particle to hit the player
float emissionHeight = MIN( vMaxs[2], pPlayer->GetAbsOrigin()[2] + 512 );
float distToFall = emissionHeight - pPlayer->GetAbsOrigin()[2];
float fallTime = distToFall / GetSpeed();
// Based on the windspeed, figure out the center point of the emission
Vector2D center;
center[0] = pPlayer->GetAbsOrigin()[0] - fallTime * s_WindVector[0];
center[1] = pPlayer->GetAbsOrigin()[1] - fallTime * s_WindVector[1];
Vector2D lobound, hibound;
lobound[0] = center[0] - emissionSize * 0.5f;
lobound[1] = center[1] - emissionSize * 0.5f;
hibound[0] = lobound[0] + emissionSize;
hibound[1] = lobound[1] + emissionSize;
// Cull non-intersecting.
if ( ( vMaxs[0] < lobound[0] ) || ( vMaxs[1] < lobound[1] ) ||
( vMins[0] > hibound[0] ) || ( vMins[1] > hibound[1] ) )
return false;
origin[0] = MAX( vMins[0], lobound[0] );
origin[1] = MAX( vMins[1], lobound[1] );
origin[2] = emissionHeight;
hibound[0] = MIN( vMaxs[0], hibound[0] );
hibound[1] = MIN( vMaxs[1], hibound[1] );
size[0] = hibound[0] - origin[0];
size[1] = hibound[1] - origin[1];
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pDebugName -
// Output : AshDebrisEffect*
//-----------------------------------------------------------------------------
AshDebrisEffect* AshDebrisEffect::Create( const char *pDebugName )
{
return new AshDebrisEffect( pDebugName );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pParticle -
// timeDelta -
// Output : float
//-----------------------------------------------------------------------------
float AshDebrisEffect::UpdateAlpha( const SimpleParticle *pParticle )
{
return ( ((float)pParticle->m_uchStartAlpha/255.0f) * sin( M_PI * (pParticle->m_flLifetime / pParticle->m_flDieTime) ) );
}
#define ASH_PARTICLE_NOISE 0x4
float AshDebrisEffect::UpdateRoll( SimpleParticle *pParticle, float timeDelta )
{
float flRoll = CSimpleEmitter::UpdateRoll(pParticle, timeDelta );
if ( pParticle->m_iFlags & ASH_PARTICLE_NOISE )
{
Vector vTempEntVel = pParticle->m_vecVelocity;
float fastFreq = gpGlobals->curtime * 1.5;
float s, c;
SinCos( fastFreq, &s, &c );
pParticle->m_Pos = ( pParticle->m_Pos + Vector(
vTempEntVel[0] * timeDelta * s,
vTempEntVel[1] * timeDelta * s, 0 ) );
}
return flRoll;
}
void CClient_Precipitation::CreateAshParticle( void )
{
// Make sure the emitter is setup
if ( m_pAshEmitter == NULL )
{
if ( ( m_pAshEmitter = AshDebrisEffect::Create( "ashtray" ) ) == NULL )
return;
m_tAshParticleTimer.Init( 192 );
m_tAshParticleTraceTimer.Init( 15 );
m_bActiveAshEmitter = false;
m_iAshCount = 0;
}
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( pPlayer == NULL )
return;
Vector vForward;
pPlayer->GetVectors( &vForward, NULL, NULL );
vForward.z = 0.0f;
float curTime = gpGlobals->frametime;
Vector vPushOrigin;
Vector absmins = WorldAlignMins();
Vector absmaxs = WorldAlignMaxs();
//15 Traces a second.
while ( m_tAshParticleTraceTimer.NextEvent( curTime ) )
{
trace_t tr;
Vector vTraceStart = pPlayer->EyePosition();
Vector vTraceEnd = pPlayer->EyePosition() + vForward * MAX_TRACE_LENGTH;
UTIL_TraceLine( vTraceStart, vTraceEnd, MASK_SHOT_HULL & (~CONTENTS_GRATE), pPlayer, COLLISION_GROUP_NONE, &tr );
//debugoverlay->AddLineOverlay( vTraceStart, tr.endpos, 255, 0, 0, 0, 0.2 );
if ( tr.fraction != 1.0f )
{
trace_t tr2;
UTIL_TraceModel( vTraceStart, tr.endpos, Vector( -1, -1, -1 ), Vector( 1, 1, 1 ), this, COLLISION_GROUP_NONE, &tr2 );
if ( tr2.m_pEnt == this )
{
m_bActiveAshEmitter = true;
if ( tr2.startsolid == false )
{
m_vAshSpawnOrigin = tr2.endpos + vForward * 256;
}
else
{
m_vAshSpawnOrigin = vTraceStart;
}
}
else
{
m_bActiveAshEmitter = false;
}
}
}
if ( m_bActiveAshEmitter == false )
return;
Vector vecVelocity = pPlayer->GetAbsVelocity();
float flVelocity = VectorNormalize( vecVelocity );
Vector offset = m_vAshSpawnOrigin;
m_pAshEmitter->SetSortOrigin( offset );
PMaterialHandle hMaterial[4];
hMaterial[0] = ParticleMgr()->GetPMaterial( "effects/fleck_ash1" );
hMaterial[1] = ParticleMgr()->GetPMaterial( "effects/fleck_ash2" );
hMaterial[2] = ParticleMgr()->GetPMaterial( "effects/fleck_ash3" );
hMaterial[3] = ParticleMgr()->GetPMaterial( "effects/ember_swirling001" );
SimpleParticle *pParticle;
Vector vSpawnOrigin = vec3_origin;
if ( flVelocity > 0 )
{
vSpawnOrigin = ( vForward * 256 ) + ( vecVelocity * ( flVelocity * 2 ) );
}
// Add as many particles as we need
while ( m_tAshParticleTimer.NextEvent( curTime ) )
{
int iRandomAltitude = RandomInt( 0, 128 );
offset = m_vAshSpawnOrigin + vSpawnOrigin + RandomVector( -256, 256 );
offset.z = m_vAshSpawnOrigin.z + iRandomAltitude;
if ( offset[0] > absmaxs[0]
|| offset[1] > absmaxs[1]
|| offset[2] > absmaxs[2]
|| offset[0] < absmins[0]
|| offset[1] < absmins[1]
|| offset[2] < absmins[2] )
continue;
m_iAshCount++;
bool bEmberTime = false;
if ( m_iAshCount >= 250 )
{
bEmberTime = true;
m_iAshCount = 0;
}
int iRandom = random->RandomInt(0,2);
if ( bEmberTime == true )
{
offset = m_vAshSpawnOrigin + (vForward * 256) + RandomVector( -128, 128 );
offset.z = pPlayer->EyePosition().z + RandomFloat( -16, 64 );
iRandom = 3;
}
pParticle = (SimpleParticle *) m_pAshEmitter->AddParticle( sizeof(SimpleParticle), hMaterial[iRandom], offset );
if (pParticle == NULL)
continue;
pParticle->m_flLifetime = 0.0f;
pParticle->m_flDieTime = RemapVal( iRandomAltitude, 0, 128, 4, 8 );
if ( bEmberTime == true )
{
Vector vGoal = pPlayer->EyePosition() + RandomVector( -64, 64 );
Vector vDir = vGoal - offset;
VectorNormalize( vDir );
pParticle->m_vecVelocity = vDir * 75;
pParticle->m_flDieTime = 2.5f;
}
else
{
pParticle->m_vecVelocity = Vector( RandomFloat( -20.0f, 20.0f ), RandomFloat( -20.0f, 20.0f ), RandomFloat( -10, -15 ) );
}
float color = random->RandomInt( 125, 225 );
pParticle->m_uchColor[0] = color;
pParticle->m_uchColor[1] = color;
pParticle->m_uchColor[2] = color;
pParticle->m_uchStartSize = 1;
pParticle->m_uchEndSize = 1;
pParticle->m_uchStartAlpha = 255;