Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7982 lines
266 KiB

  1. //===== Copyright � 1996-2006, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: particle system code
  4. //
  5. //===========================================================================//
  6. #include "tier0/platform.h"
  7. #include "particles/particles.h"
  8. #include "filesystem.h"
  9. #include "tier2/tier2.h"
  10. #include "tier2/fileutils.h"
  11. #include "tier2/renderutils.h"
  12. #include "tier1/UtlStringMap.h"
  13. #include "tier1/strtools.h"
  14. #include "dmxloader/dmxelement.h"
  15. #include "bitmap/psheet.h"
  16. #include "bspflags.h"
  17. #include "const.h"
  18. #include "particles_internal.h"
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. void CParticleOperatorInstance::InitScalarAttributeRandomRangeExpScalar(
  22. int nAttrNum, float flMin, float flMax, float flExp,
  23. CParticleCollection *pParticles, int nStartParticle, int nParticleCount ) const
  24. {
  25. if ( ! m_bStrengthFastPath )
  26. {
  27. // adjust max/mins based on strength
  28. float flStrength;
  29. pParticles->CheckIfOperatorShouldRun( this, &flStrength );
  30. flMin *= flStrength;
  31. flMax *= flStrength;
  32. }
  33. if ( flExp != 1.0 )
  34. {
  35. for( ; nParticleCount--; nStartParticle++ )
  36. {
  37. float *pAttr = pParticles->GetFloatAttributePtrForWrite( nAttrNum, nStartParticle );
  38. *pAttr = pParticles->RandomFloatExp( flMin, flMax, flExp );
  39. }
  40. }
  41. else
  42. {
  43. for( ; nParticleCount--; nStartParticle++ )
  44. {
  45. float *pAttr = pParticles->GetFloatAttributePtrForWrite( nAttrNum, nStartParticle );
  46. *pAttr = pParticles->RandomFloat( flMin, flMax );
  47. }
  48. }
  49. }
  50. void CParticleOperatorInstance::InitScalarAttributeRandomRangeExpBlock(
  51. int attr_num, float fMin, float fMax, float fExp,
  52. CParticleCollection *pParticles, int nStartBlock, int nBlockCount, bool bRandomlyInvert ) const
  53. {
  54. if ( ! m_bStrengthFastPath )
  55. {
  56. // adjust max/mins based on strength
  57. float flStrength;
  58. pParticles->CheckIfOperatorShouldRun( this, &flStrength );
  59. fMin *= flStrength;
  60. fMax *= flStrength;
  61. }
  62. size_t nAttrStride;
  63. fltx4 *pAttr = pParticles->GetM128AttributePtrForWrite( attr_num, &nAttrStride );
  64. pAttr += nAttrStride * nStartBlock;
  65. fltx4 val0 = ReplicateX4( fMin );
  66. fltx4 val_d = ReplicateX4( fMax - fMin );
  67. int nRandContext = GetSIMDRandContext();
  68. if ( !bRandomlyInvert )
  69. {
  70. if ( fExp == 1.0 )
  71. {
  72. while( nBlockCount-- )
  73. {
  74. *( pAttr ) = AddSIMD( val0, MulSIMD( RandSIMD( nRandContext ), val_d ) );
  75. pAttr += nAttrStride;
  76. }
  77. }
  78. else
  79. {
  80. while( nBlockCount-- )
  81. {
  82. *( pAttr ) = AddSIMD( val0, MulSIMD( Pow_FixedPoint_Exponent_SIMD( RandSIMD( nRandContext ), fExp ), val_d ) );
  83. pAttr += nAttrStride;
  84. }
  85. }
  86. }
  87. else
  88. {
  89. fltx4 fl4NegOne = ReplicateX4( -1.0f );
  90. if ( fExp == 1.0f )
  91. {
  92. while( nBlockCount-- )
  93. {
  94. fltx4 fl4RandVal = AddSIMD( val0, MulSIMD( RandSIMD( nRandContext ), val_d ) );
  95. fltx4 fl4Sign = MaskedAssign( CmpGeSIMD( RandSIMD( nRandContext ), Four_PointFives ), Four_Ones, fl4NegOne );
  96. *pAttr = MulSIMD( fl4RandVal, fl4Sign );
  97. pAttr += nAttrStride;
  98. }
  99. }
  100. else
  101. {
  102. while( nBlockCount-- )
  103. {
  104. fltx4 fl4RandVal = AddSIMD( val0, MulSIMD( Pow_FixedPoint_Exponent_SIMD( RandSIMD( nRandContext ), fExp ), val_d ) );
  105. fltx4 fl4Sign = MaskedAssign( CmpGeSIMD( RandSIMD( nRandContext ), Four_PointFives ), Four_Ones, fl4NegOne );
  106. *pAttr = MulSIMD( fl4RandVal, fl4Sign );
  107. pAttr += nAttrStride;
  108. }
  109. }
  110. }
  111. ReleaseSIMDRandContext( nRandContext );
  112. }
  113. void CParticleOperatorInstance::AddScalarAttributeRandomRangeExpBlock(
  114. int nAttributeId, float fMin, float fMax, float fExp,
  115. CParticleCollection *pParticles, int nStartBlock, int nBlockCount, bool bRandomlyInvert ) const
  116. {
  117. size_t nAttrStride;
  118. fltx4 *pAttr = pParticles->GetM128AttributePtrForWrite( nAttributeId, &nAttrStride );
  119. pAttr += nAttrStride * nStartBlock;
  120. fltx4 val0 = ReplicateX4( fMin );
  121. fltx4 val_d = ReplicateX4( fMax - fMin );
  122. int nRandContext = GetSIMDRandContext();
  123. if ( !bRandomlyInvert )
  124. {
  125. if ( fExp == 1.0f )
  126. {
  127. while( nBlockCount-- )
  128. {
  129. *pAttr = AddSIMD( *pAttr, AddSIMD( val0, MulSIMD( RandSIMD( nRandContext ), val_d ) ) );
  130. pAttr += nAttrStride;
  131. }
  132. }
  133. else
  134. {
  135. while( nBlockCount-- )
  136. {
  137. *( pAttr ) = AddSIMD( *pAttr, AddSIMD( val0, MulSIMD( Pow_FixedPoint_Exponent_SIMD( RandSIMD( nRandContext ), fExp ), val_d ) ) );
  138. pAttr += nAttrStride;
  139. }
  140. }
  141. }
  142. else
  143. {
  144. fltx4 fl4NegOne = ReplicateX4( -1.0f );
  145. if ( fExp == 1.0f )
  146. {
  147. while( nBlockCount-- )
  148. {
  149. fltx4 fl4RandVal = AddSIMD( val0, MulSIMD( RandSIMD( nRandContext ), val_d ) );
  150. fltx4 fl4Sign = MaskedAssign( CmpGeSIMD( RandSIMD( nRandContext ), Four_PointFives ), Four_Ones, fl4NegOne );
  151. *pAttr = AddSIMD( *pAttr, MulSIMD( fl4RandVal, fl4Sign ) );
  152. pAttr += nAttrStride;
  153. }
  154. }
  155. else
  156. {
  157. while( nBlockCount-- )
  158. {
  159. fltx4 fl4RandVal = AddSIMD( val0, MulSIMD( Pow_FixedPoint_Exponent_SIMD( RandSIMD( nRandContext ), fExp ), val_d ) );
  160. fltx4 fl4Sign = MaskedAssign( CmpGeSIMD( RandSIMD( nRandContext ), Four_PointFives ), Four_Ones, fl4NegOne );
  161. *pAttr = AddSIMD( *pAttr, MulSIMD( fl4RandVal, fl4Sign ) );
  162. pAttr += nAttrStride;
  163. }
  164. }
  165. }
  166. ReleaseSIMDRandContext( nRandContext );
  167. }
  168. class C_INIT_RingWave : public CParticleInitializerOperatorInstance
  169. {
  170. DECLARE_PARTICLE_OPERATOR( C_INIT_RingWave );
  171. int m_nControlPointNumber;
  172. int m_nOverrideCP;
  173. int m_nOverrideCP2;
  174. float m_flParticlesPerOrbit;
  175. float m_flInitialRadius;
  176. float m_flThickness;
  177. float m_flInitialSpeedMin;
  178. float m_flInitialSpeedMax;
  179. float m_flRoll;
  180. float m_flPitch;
  181. float m_flYaw;
  182. bool m_bEvenDistribution;
  183. bool m_bXYVelocityOnly;
  184. uint32 GetWrittenAttributes( void ) const
  185. {
  186. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  187. }
  188. uint32 GetReadAttributes( void ) const
  189. {
  190. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  191. }
  192. virtual uint64 GetReadControlPointMask() const
  193. {
  194. uint64 nMask = ( 1ULL << m_nControlPointNumber );
  195. if ( m_nOverrideCP != -1 )
  196. {
  197. nMask |= ( 1ULL << m_nOverrideCP );
  198. }
  199. if ( m_nOverrideCP2 != -1 )
  200. {
  201. nMask |= ( 1ULL << m_nOverrideCP2 );
  202. }
  203. return nMask;
  204. }
  205. struct RingWaveContext_t
  206. {
  207. unsigned int m_nCurrentCount;
  208. };
  209. size_t GetRequiredContextBytes( void ) const
  210. {
  211. return sizeof( RingWaveContext_t );
  212. }
  213. virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
  214. {
  215. RingWaveContext_t *pCtx=reinterpret_cast<RingWaveContext_t *>( pContext );
  216. pCtx->m_nCurrentCount = 0;
  217. }
  218. void InitParams( CParticleSystemDefinition *pDef )
  219. {
  220. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  221. m_nOverrideCP = MAX( -1, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nOverrideCP ) );
  222. m_nOverrideCP2 = MAX( -1, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nOverrideCP2 ) );
  223. m_flInitialSpeedMax = MAX( m_flInitialSpeedMin, m_flInitialSpeedMax );
  224. m_flInitialRadius = MAX(0, m_flInitialRadius);
  225. }
  226. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  227. int nParticleCount, int nAttributeWriteMask,
  228. void *pContext) const;
  229. };
  230. DEFINE_PARTICLE_OPERATOR (C_INIT_RingWave, "Position Along Ring", OPERATOR_PI_POSITION);
  231. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RingWave )
  232. DMXELEMENT_UNPACK_FIELD( "control point number", "0", int, m_nControlPointNumber )
  233. DMXELEMENT_UNPACK_FIELD( "initial radius", "0", float, m_flInitialRadius )
  234. DMXELEMENT_UNPACK_FIELD( "thickness", "0", float, m_flThickness)
  235. DMXELEMENT_UNPACK_FIELD( "min initial speed", "0", float, m_flInitialSpeedMin)
  236. DMXELEMENT_UNPACK_FIELD( "max initial speed", "0", float, m_flInitialSpeedMax)
  237. DMXELEMENT_UNPACK_FIELD( "yaw", "0", float, m_flYaw)
  238. DMXELEMENT_UNPACK_FIELD( "roll", "0", float, m_flRoll)
  239. DMXELEMENT_UNPACK_FIELD( "pitch", "0", float, m_flPitch)
  240. DMXELEMENT_UNPACK_FIELD( "even distribution", "0", bool, m_bEvenDistribution)
  241. DMXELEMENT_UNPACK_FIELD( "even distribution count", "-1", float, m_flParticlesPerOrbit)
  242. DMXELEMENT_UNPACK_FIELD( "XY velocity only", "1", bool, m_bXYVelocityOnly)
  243. DMXELEMENT_UNPACK_FIELD( "Override CP (X/Y/Z *= Radius/Thickness/Speed)", "-1", int, m_nOverrideCP )
  244. DMXELEMENT_UNPACK_FIELD( "Override CP 2 (X/Y/Z *= Pitch/Yaw/Roll)", "-1", int, m_nOverrideCP2 )
  245. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RingWave )
  246. void C_INIT_RingWave::InitNewParticlesScalar(
  247. CParticleCollection *pParticles, int start_p,
  248. int nParticleCount, int nAttributeWriteMask,
  249. void *pContext) const
  250. {
  251. RingWaveContext_t *pCtx=reinterpret_cast<RingWaveContext_t *>( pContext );
  252. if ( pCtx->m_nCurrentCount >= ( UINT_MAX - nParticleCount ) )
  253. pCtx->m_nCurrentCount = 0;
  254. float flRadius = m_flInitialRadius;
  255. float flThickness = m_flThickness;
  256. float flSpeedMin = m_flInitialSpeedMin;
  257. float flSpeedMax = m_flInitialSpeedMax;
  258. QAngle qatAngles( m_flPitch, m_flYaw, m_flRoll );
  259. if ( m_nOverrideCP != -1 )
  260. {
  261. Vector vecOverride = pParticles->GetControlPointAtCurrentTime( m_nOverrideCP );
  262. flRadius = vecOverride.x * m_flInitialRadius;
  263. flThickness = vecOverride.y * m_flThickness;
  264. flSpeedMin = vecOverride.z * m_flInitialSpeedMin;
  265. flSpeedMax = vecOverride.z * m_flInitialSpeedMax;
  266. }
  267. if ( m_nOverrideCP2 != -1 )
  268. {
  269. Vector vecOverride = pParticles->GetControlPointAtCurrentTime( m_nOverrideCP2 );
  270. qatAngles.x *= vecOverride.x;
  271. qatAngles.y *= vecOverride.y;
  272. qatAngles.z *= vecOverride.z;
  273. }
  274. float theta = 2 * M_PI / ( ( m_bEvenDistribution && m_flParticlesPerOrbit != -1 ) ? m_flParticlesPerOrbit : (float) nParticleCount );
  275. for( ; nParticleCount-- ; start_p++ )
  276. {
  277. pCtx->m_nCurrentCount++;
  278. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  279. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  280. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  281. float sinTheta = 0, cosTheta = 0;
  282. float speed;
  283. Vector pointOnCircle;
  284. Vector randPos;
  285. Vector prevPos;
  286. Vector vecControlPoint;
  287. Vector vecTransformLocal;
  288. matrix3x4_t mat;
  289. pParticles->RandomVectorInUnitSphere( &randPos );
  290. pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, *ct, &mat );
  291. MatrixGetColumn( mat, 3, vecControlPoint );
  292. SinCos( ( ( m_bEvenDistribution ) ? theta : pParticles->RandomFloat(0, 2*M_PI) )*( pCtx->m_nCurrentCount ), &sinTheta, &cosTheta );
  293. VectorRotate( ( Vector( cosTheta, sinTheta, 0 ) )*flRadius, qatAngles, pointOnCircle );
  294. speed = pParticles->RandomFloat( flSpeedMin, flSpeedMax );
  295. randPos = pointOnCircle + randPos*flThickness;
  296. VectorTransform( randPos, mat, vecTransformLocal );
  297. randPos = vecTransformLocal;
  298. prevPos = ( vecTransformLocal - vecControlPoint ).Normalized();
  299. prevPos *= speed * ( pParticles->m_flPreviousDt );
  300. prevPos = randPos - prevPos;
  301. xyz[0] = randPos.x;
  302. xyz[4] = randPos.y;
  303. xyz[8] = randPos.z;
  304. if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
  305. {
  306. pxyz[0] = prevPos.x;
  307. pxyz[4] = prevPos.y;
  308. pxyz[8] = (m_bXYVelocityOnly) ? randPos.z : prevPos.z;
  309. }
  310. }
  311. }
  312. class C_INIT_CreateInEpitrochoid : public CParticleInitializerOperatorInstance
  313. {
  314. DECLARE_PARTICLE_OPERATOR( C_INIT_CreateInEpitrochoid );
  315. int m_nComponent1;
  316. int m_nComponent2;
  317. int m_nControlPointNumber;
  318. int m_nScaleCP;
  319. float m_flParticleDensity;
  320. float m_flOffset;
  321. float m_flRadius1;
  322. float m_flRadius2;
  323. bool m_bUseCount;
  324. bool m_bUseLocalCoords;
  325. bool m_bOffsetExistingPos;
  326. uint32 GetWrittenAttributes( void ) const
  327. {
  328. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  329. }
  330. uint32 GetReadAttributes( void ) const
  331. {
  332. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  333. }
  334. virtual uint64 GetReadControlPointMask() const
  335. {
  336. return 1ULL << m_nControlPointNumber | 1ULL << m_nScaleCP;
  337. }
  338. virtual uint64 GetNonPositionalControlPointMask() const
  339. {
  340. return 1ULL << m_nScaleCP;
  341. }
  342. struct CreateInEpitrochoidContext_t
  343. {
  344. unsigned int m_nCurrentCount;
  345. };
  346. size_t GetRequiredContextBytes( void ) const
  347. {
  348. return sizeof( CreateInEpitrochoidContext_t );
  349. }
  350. virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
  351. {
  352. CreateInEpitrochoidContext_t *pCtx=reinterpret_cast<CreateInEpitrochoidContext_t *>( pContext );
  353. pCtx->m_nCurrentCount = 0;
  354. }
  355. bool InitMultipleOverride ( void )
  356. {
  357. if ( m_bOffsetExistingPos )
  358. return true;
  359. else
  360. return false;
  361. }
  362. void InitParams( CParticleSystemDefinition *pDef )
  363. {
  364. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  365. m_nScaleCP = MAX( -1, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nScaleCP ) );
  366. m_flParticleDensity = MAX( FLT_EPSILON, MIN ( FLT_MAX, m_flParticleDensity ) );
  367. m_nComponent1 = MAX( -1, MIN ( 2, m_nComponent1 ) );
  368. m_nComponent2 = MAX( -1, MIN ( 2, m_nComponent2 ) );
  369. }
  370. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  371. int nParticleCount, int nAttributeWriteMask,
  372. void *pContext) const;
  373. };
  374. DEFINE_PARTICLE_OPERATOR (C_INIT_CreateInEpitrochoid, "Position Along Epitrochoid", OPERATOR_GENERIC );
  375. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_CreateInEpitrochoid )
  376. DMXELEMENT_UNPACK_FIELD( "control point number", "0", int, m_nControlPointNumber )
  377. DMXELEMENT_UNPACK_FIELD( "first dimension 0-2 (-1 disables)", "0", int, m_nComponent1 )
  378. DMXELEMENT_UNPACK_FIELD( "second dimension 0-2 (-1 disables)", "1", int, m_nComponent2 )
  379. DMXELEMENT_UNPACK_FIELD( "radius 1", "40", float, m_flRadius1 )
  380. DMXELEMENT_UNPACK_FIELD( "radius 2", "24", float, m_flRadius2)
  381. DMXELEMENT_UNPACK_FIELD( "point offset", "4", float, m_flOffset)
  382. DMXELEMENT_UNPACK_FIELD( "particle density", "10", float, m_flParticleDensity)
  383. DMXELEMENT_UNPACK_FIELD( "use particle count instead of creation time", "0", bool, m_bUseCount )
  384. DMXELEMENT_UNPACK_FIELD( "local space", "0", bool, m_bUseLocalCoords )
  385. DMXELEMENT_UNPACK_FIELD( "offset from existing position", "0", bool, m_bOffsetExistingPos )
  386. DMXELEMENT_UNPACK_FIELD( "scale from conrol point (radius 1/radius 2/offset)", "-1", int, m_nScaleCP )
  387. END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateInEpitrochoid )
  388. void C_INIT_CreateInEpitrochoid::InitNewParticlesScalar(
  389. CParticleCollection *pParticles, int start_p,
  390. int nParticleCount, int nAttributeWriteMask,
  391. void *pContext) const
  392. {
  393. CreateInEpitrochoidContext_t *pCtx=reinterpret_cast<CreateInEpitrochoidContext_t *>( pContext );
  394. Vector vecCPPos = pParticles->GetControlPointAtCurrentTime( m_nControlPointNumber );
  395. matrix3x4_t Mat;
  396. pParticles->GetControlPointTransformAtCurrentTime( m_nControlPointNumber, &Mat );
  397. // We'll precompute a few bits rather than per particle
  398. float flRadiiAdd;
  399. float flOffset;
  400. float flRadius2;
  401. if ( m_nScaleCP > -1 ) // Do we have a CP to scale from?
  402. {
  403. Vector vecScaleCP = pParticles->GetControlPointAtCurrentTime( m_nScaleCP );
  404. flRadius2 = m_flRadius2 * vecScaleCP.y + FLT_EPSILON;;
  405. flRadiiAdd = ( m_flRadius1 * vecScaleCP.x ) + flRadius2;
  406. flOffset = m_flOffset * vecScaleCP.z;
  407. }
  408. else // if not, just use set values
  409. {
  410. flRadiiAdd = m_flRadius1 + m_flRadius2;
  411. flOffset = m_flOffset;
  412. flRadius2 = m_flRadius2 + FLT_EPSILON;
  413. }
  414. if ( flRadiiAdd != 0 && flRadius2 != 0 )
  415. {
  416. for( ; nParticleCount-- ; start_p++ )
  417. {
  418. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  419. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  420. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  421. float flTime;
  422. if ( m_bUseCount )
  423. {
  424. flTime = pCtx->m_nCurrentCount * m_flParticleDensity;
  425. pCtx->m_nCurrentCount++;
  426. }
  427. else
  428. flTime = *ct * m_flParticleDensity;
  429. Vector vecXYZ;
  430. Vector vecPXYZ;
  431. vecXYZ = vecPXYZ = vecCPPos;
  432. if ( m_bOffsetExistingPos )
  433. {
  434. SetVectorFromAttribute( vecXYZ, xyz );
  435. SetVectorFromAttribute( vecPXYZ, pxyz );
  436. }
  437. Vector vecEpitrochoid = vec3_origin;
  438. float flMultiplier = ( flRadiiAdd ) * ( flTime / flRadius2 );
  439. if ( m_nComponent1 != -1 )
  440. vecEpitrochoid[m_nComponent1] = ( flRadiiAdd ) * cos( flTime ) + flOffset * cos( flMultiplier );
  441. if ( m_nComponent2 != -1 )
  442. vecEpitrochoid[m_nComponent2] = ( flRadiiAdd ) * sin( flTime ) + flOffset * sin( flMultiplier );
  443. if ( m_bUseLocalCoords )
  444. {
  445. Vector vecWorld = vecEpitrochoid;
  446. VectorRotate( vecWorld, Mat, vecEpitrochoid );
  447. }
  448. vecXYZ += vecEpitrochoid;
  449. vecPXYZ += vecEpitrochoid;
  450. SetVectorAttribute( xyz, vecXYZ );
  451. SetVectorAttribute( pxyz, vecPXYZ );
  452. }
  453. }
  454. }
  455. class C_INIT_CreateOnModel : public CParticleInitializerOperatorInstance
  456. {
  457. DECLARE_PARTICLE_OPERATOR( C_INIT_CreateOnModel );
  458. int m_nControlPointNumber;
  459. int m_nForceInModel;
  460. int m_nDesiredHitbox;
  461. float m_flHitBoxScale;
  462. Vector m_vecDirectionBias;
  463. char m_HitboxSetName[128];
  464. uint32 GetWrittenAttributes( void ) const
  465. {
  466. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK |
  467. PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ_MASK | PARTICLE_ATTRIBUTE_HITBOX_INDEX_MASK | PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  468. }
  469. uint32 GetReadAttributes( void ) const
  470. {
  471. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  472. }
  473. virtual uint64 GetReadControlPointMask() const
  474. {
  475. return 1ULL << m_nControlPointNumber;
  476. }
  477. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  478. int nParticleCount, int nAttributeWriteMask,
  479. void *pContext) const;
  480. };
  481. DEFINE_PARTICLE_OPERATOR( C_INIT_CreateOnModel, "Position on Model Random", OPERATOR_PI_POSITION );
  482. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_CreateOnModel )
  483. DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
  484. DMXELEMENT_UNPACK_FIELD( "force to be inside model", "0", int, m_nForceInModel )
  485. DMXELEMENT_UNPACK_FIELD( "model hitbox scale", "1.0", float, m_flHitBoxScale )
  486. DMXELEMENT_UNPACK_FIELD( "direction bias", "0 0 0", Vector, m_vecDirectionBias )
  487. DMXELEMENT_UNPACK_FIELD( "desired hitbox", "-1", int, m_nDesiredHitbox )
  488. DMXELEMENT_UNPACK_FIELD_STRING( "hitbox set", "effects", m_HitboxSetName )
  489. END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateOnModel )
  490. void C_INIT_CreateOnModel::InitNewParticlesScalar(
  491. CParticleCollection *pParticles, int start_p,
  492. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  493. {
  494. pParticles->UpdateHitBoxInfo( m_nControlPointNumber, m_HitboxSetName );
  495. Vector vecCP = pParticles->GetControlPointAtCurrentTime( m_nControlPointNumber );
  496. Vector vecCPPrev;
  497. pParticles->GetControlPointAtPrevTime( m_nControlPointNumber, &vecCPPrev );
  498. Vector vecDelta = vecCPPrev - vecCP;
  499. float flPrevTime = pParticles->m_flCurTime - pParticles->m_flDt;
  500. while( nParticleCount )
  501. {
  502. Vector vecPnts[100]; // minimize stack usage
  503. Vector vecUVW[100];
  504. int nHitBoxIndex[100];
  505. int nToDo = MIN( ARRAYSIZE( vecPnts ), nParticleCount );
  506. Assert( m_nControlPointNumber <= pParticles->GetHighestControlPoint() );
  507. g_pParticleSystemMgr->Query()->GetRandomPointsOnControllingObjectHitBox(
  508. pParticles, m_nControlPointNumber,
  509. nToDo, m_flHitBoxScale, m_nForceInModel, vecPnts, m_vecDirectionBias, vecUVW,
  510. nHitBoxIndex, m_nDesiredHitbox, m_HitboxSetName );
  511. for( int i=0; i<nToDo; i++)
  512. {
  513. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  514. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  515. float *pHitboxRelXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ, start_p );
  516. float *pCt = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  517. int *pHitboxIndex = pParticles->GetIntAttributePtrForWrite( PARTICLE_ATTRIBUTE_HITBOX_INDEX, start_p );
  518. start_p++;
  519. Vector randpos = vecPnts[i];
  520. float flPerc = RemapValClamped( *pCt, flPrevTime, pParticles->m_flCurTime, 0.0f, 1.0f );
  521. VectorLerp( randpos - vecDelta, randpos, flPerc );
  522. xyz[0] = randpos.x;
  523. xyz[4] = randpos.y;
  524. xyz[8] = randpos.z;
  525. if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
  526. {
  527. pxyz[0] = randpos.x;
  528. pxyz[4] = randpos.y;
  529. pxyz[8] = randpos.z;
  530. }
  531. if ( pHitboxRelXYZ && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ_MASK ) )
  532. {
  533. pHitboxRelXYZ[0] = vecUVW[i].x;
  534. pHitboxRelXYZ[4] = vecUVW[i].y;
  535. pHitboxRelXYZ[8] = vecUVW[i].z;
  536. }
  537. if ( pHitboxIndex && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_HITBOX_INDEX_MASK ) )
  538. {
  539. *pHitboxIndex = nHitBoxIndex[i];
  540. }
  541. }
  542. nParticleCount -= nToDo;
  543. }
  544. }
  545. class C_INIT_SetHitboxToClosest : public CParticleInitializerOperatorInstance
  546. {
  547. DECLARE_PARTICLE_OPERATOR( C_INIT_SetHitboxToClosest );
  548. int m_nControlPointNumber;
  549. int m_nForceInModel;
  550. int m_nDesiredHitbox;
  551. float m_flHitBoxScale;
  552. char m_HitboxSetName[128];
  553. uint32 GetWrittenAttributes( void ) const
  554. {
  555. return PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ_MASK | PARTICLE_ATTRIBUTE_HITBOX_INDEX_MASK;
  556. }
  557. uint32 GetReadAttributes( void ) const
  558. {
  559. return 0;
  560. }
  561. virtual uint64 GetReadControlPointMask() const
  562. {
  563. return 1ULL << m_nControlPointNumber;
  564. }
  565. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  566. int nParticleCount, int nAttributeWriteMask,
  567. void *pContext) const;
  568. };
  569. DEFINE_PARTICLE_OPERATOR( C_INIT_SetHitboxToClosest, "Set Hitbox to Closest Hitbox", OPERATOR_GENERIC );
  570. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_SetHitboxToClosest )
  571. DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
  572. DMXELEMENT_UNPACK_FIELD( "model hitbox scale", "1.0", float, m_flHitBoxScale )
  573. DMXELEMENT_UNPACK_FIELD( "desired hitbox", "-1", int, m_nDesiredHitbox )
  574. DMXELEMENT_UNPACK_FIELD_STRING( "hitbox set", "effects", m_HitboxSetName )
  575. END_PARTICLE_OPERATOR_UNPACK( C_INIT_SetHitboxToClosest )
  576. void C_INIT_SetHitboxToClosest::InitNewParticlesScalar(
  577. CParticleCollection *pParticles, int start_p,
  578. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  579. {
  580. pParticles->UpdateHitBoxInfo( m_nControlPointNumber, m_HitboxSetName );
  581. while( nParticleCount )
  582. {
  583. Vector vecPnts[100]; // minimize stack usage
  584. Vector vecUVW[100];
  585. int nHitBoxIndex[100];
  586. int nToDo = MIN( ARRAYSIZE( vecPnts ), nParticleCount );
  587. for( int i=0; i<nToDo; i++)
  588. {
  589. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p + i );
  590. SetVectorFromAttribute( vecPnts[i], xyz );
  591. }
  592. Assert( m_nControlPointNumber <= pParticles->GetHighestControlPoint() );
  593. g_pParticleSystemMgr->Query()->GetClosestControllingObjectHitBox(
  594. pParticles, m_nControlPointNumber,
  595. nToDo, m_flHitBoxScale, vecPnts, vecUVW,
  596. nHitBoxIndex, m_nDesiredHitbox, m_HitboxSetName );
  597. for( int i=0; i<nToDo; i++)
  598. {
  599. float *pHitboxRelXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ, start_p );
  600. int *pHitboxIndex = pParticles->GetIntAttributePtrForWrite( PARTICLE_ATTRIBUTE_HITBOX_INDEX, start_p );
  601. start_p++;
  602. if ( pHitboxRelXYZ && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ_MASK ) )
  603. {
  604. pHitboxRelXYZ[0] = vecUVW[i].x;
  605. pHitboxRelXYZ[4] = vecUVW[i].y;
  606. pHitboxRelXYZ[8] = vecUVW[i].z;
  607. }
  608. if ( pHitboxIndex && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_HITBOX_INDEX_MASK ) )
  609. {
  610. *pHitboxIndex = nHitBoxIndex[i];
  611. }
  612. }
  613. nParticleCount -= nToDo;
  614. }
  615. }
  616. class C_INIT_SetHitboxToModel : public CParticleInitializerOperatorInstance
  617. {
  618. DECLARE_PARTICLE_OPERATOR( C_INIT_SetHitboxToModel );
  619. int m_nControlPointNumber;
  620. int m_nForceInModel;
  621. int m_nDesiredHitbox;
  622. float m_flHitBoxScale;
  623. Vector m_vecDirectionBias;
  624. bool m_bMaintainHitbox;
  625. char m_HitboxSetName[128];
  626. uint32 GetWrittenAttributes( void ) const
  627. {
  628. return PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ_MASK | PARTICLE_ATTRIBUTE_HITBOX_INDEX_MASK;
  629. }
  630. uint32 GetReadAttributes( void ) const
  631. {
  632. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  633. }
  634. virtual uint64 GetReadControlPointMask() const
  635. {
  636. return 1ULL << m_nControlPointNumber;
  637. }
  638. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  639. int nParticleCount, int nAttributeWriteMask,
  640. void *pContext) const;
  641. };
  642. DEFINE_PARTICLE_OPERATOR( C_INIT_SetHitboxToModel, "Set Hitbox Position on Model", OPERATOR_GENERIC );
  643. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_SetHitboxToModel )
  644. DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
  645. DMXELEMENT_UNPACK_FIELD( "force to be inside model", "0", int, m_nForceInModel )
  646. DMXELEMENT_UNPACK_FIELD( "model hitbox scale", "1.0", float, m_flHitBoxScale )
  647. DMXELEMENT_UNPACK_FIELD( "direction bias", "0 0 0", Vector, m_vecDirectionBias )
  648. DMXELEMENT_UNPACK_FIELD( "desired hitbox", "-1", int, m_nDesiredHitbox )
  649. DMXELEMENT_UNPACK_FIELD_STRING( "hitbox set", "effects", m_HitboxSetName )
  650. DMXELEMENT_UNPACK_FIELD( "maintain existing hitbox", "0", bool, m_bMaintainHitbox )
  651. END_PARTICLE_OPERATOR_UNPACK( C_INIT_SetHitboxToModel )
  652. void C_INIT_SetHitboxToModel::InitNewParticlesScalar(
  653. CParticleCollection *pParticles, int start_p,
  654. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  655. {
  656. pParticles->UpdateHitBoxInfo( m_nControlPointNumber, m_HitboxSetName );
  657. while( nParticleCount )
  658. {
  659. Vector vecPnts[100]; // minimize stack usage
  660. Vector vecUVW[100];
  661. int nHitBoxIndex[100];
  662. int nToDo = MIN( ARRAYSIZE( vecPnts ), nParticleCount );
  663. int nDesiredHitbox = m_nDesiredHitbox;
  664. Assert( m_nControlPointNumber <= pParticles->GetHighestControlPoint() );
  665. if ( m_bMaintainHitbox )
  666. {
  667. nDesiredHitbox = *pParticles->GetIntAttributePtr( PARTICLE_ATTRIBUTE_HITBOX_INDEX, start_p );
  668. }
  669. g_pParticleSystemMgr->Query()->GetRandomPointsOnControllingObjectHitBox(
  670. pParticles, m_nControlPointNumber,
  671. nToDo, m_flHitBoxScale, m_nForceInModel, vecPnts, m_vecDirectionBias, vecUVW,
  672. nHitBoxIndex, nDesiredHitbox, m_HitboxSetName );
  673. for( int i=0; i<nToDo; i++)
  674. {
  675. float *pHitboxRelXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ, start_p );
  676. int *pHitboxIndex = pParticles->GetIntAttributePtrForWrite( PARTICLE_ATTRIBUTE_HITBOX_INDEX, start_p );
  677. start_p++;
  678. Vector randpos = vecPnts[i];
  679. if ( pHitboxRelXYZ && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ_MASK ) )
  680. {
  681. pHitboxRelXYZ[0] = vecUVW[i].x;
  682. pHitboxRelXYZ[4] = vecUVW[i].y;
  683. pHitboxRelXYZ[8] = vecUVW[i].z;
  684. }
  685. if ( pHitboxIndex && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_HITBOX_INDEX_MASK ) )
  686. {
  687. *pHitboxIndex = nHitBoxIndex[i];
  688. }
  689. }
  690. nParticleCount -= nToDo;
  691. }
  692. }
  693. static inline void RandomPointOnUnitSphere( int nRandContext, FourVectors &out )
  694. {
  695. // generate 4 random points on the unit sphere. uses Marsaglia (1972) method from
  696. // http://mathworld.wolfram.com/SpherePointPicking.html
  697. fltx4 f4x1 = SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ); // -1..1
  698. fltx4 f4x2 = SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ); // -1..1
  699. fltx4 f4x1SQ = MulSIMD( f4x1, f4x1 );
  700. fltx4 f4x2SQ = MulSIMD( f4x2, f4x2 );
  701. bi32x4 badMask = CmpGeSIMD( AddSIMD( f4x1SQ, f4x2SQ ), Four_Ones );
  702. while( IsAnyTrue( badMask ) )
  703. {
  704. f4x1 = MaskedAssign( badMask, SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ), f4x1 );
  705. f4x2 = MaskedAssign( badMask, SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ), f4x2 );
  706. f4x1SQ = MulSIMD( f4x1, f4x1 );
  707. f4x2SQ = MulSIMD( f4x2, f4x2 );
  708. badMask = CmpGeSIMD( AddSIMD( f4x1SQ, f4x2SQ ), Four_Ones );
  709. }
  710. // now, we have 2 points on the unit circle
  711. fltx4 f4OuterArea = SqrtEstSIMD( SubSIMD( Four_Ones, SubSIMD( f4x1SQ, f4x2SQ ) ) );
  712. out.x = MulSIMD( AddSIMD( f4x1, f4x1 ), f4OuterArea );
  713. out.y = MulSIMD( AddSIMD( f4x2, f4x2 ), f4OuterArea );
  714. out.z = SubSIMD( Four_Ones, MulSIMD( Four_Twos, AddSIMD( f4x1, f4x2 ) ) );
  715. }
  716. static inline void RandomPointInUnitSphere( int nRandContext, FourVectors &out )
  717. {
  718. // generate 4 random points inside the unit sphere. uses rejection method.
  719. out.x = SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ); // -1..1
  720. out.y = SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ); // -1..1
  721. out.z = SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ); // -1..1
  722. fltx4 f4xSQ = MulSIMD( out.x, out.x );
  723. fltx4 f4ySQ = MulSIMD( out.y, out.y );
  724. fltx4 f4zSQ = MulSIMD( out.z, out.z );
  725. bi32x4 badMask = CmpGtSIMD( AddSIMD( AddSIMD( f4xSQ, f4ySQ ), f4zSQ ), Four_Ones );
  726. while( IsAnyTrue( badMask ) )
  727. {
  728. out.x = MaskedAssign( badMask, SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ), out.x );
  729. out.y = MaskedAssign( badMask, SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ), out.y );
  730. out.z = MaskedAssign( badMask, SubSIMD( MulSIMD( Four_Twos, RandSIMD( nRandContext ) ), Four_Ones ), out.z );
  731. f4xSQ = MulSIMD( out.x, out.x );
  732. f4ySQ = MulSIMD( out.y, out.y );
  733. f4zSQ = MulSIMD( out.z, out.z );
  734. badMask = CmpGeSIMD( AddSIMD( AddSIMD( f4xSQ, f4ySQ ), f4zSQ ), Four_Ones );
  735. }
  736. }
  737. class C_INIT_CreateWithinSphere : public CParticleInitializerOperatorInstance
  738. {
  739. DECLARE_PARTICLE_OPERATOR( C_INIT_CreateWithinSphere );
  740. float m_fRadiusMin;
  741. float m_fRadiusMax;
  742. Vector m_vecDistanceBias, m_vecDistanceBiasAbs;
  743. int m_nControlPointNumber;
  744. int m_nScaleCP;
  745. float m_fSpeedMin;
  746. float m_fSpeedMax;
  747. float m_fSpeedRandExp;
  748. bool m_bLocalCoords;
  749. bool m_bDistanceBiasAbs;
  750. bool m_bUseHighestEndCP;
  751. bool m_bDistanceBias;
  752. float m_flEndCPGrowthTime;
  753. Vector m_LocalCoordinateSystemSpeedMin;
  754. Vector m_LocalCoordinateSystemSpeedMax;
  755. uint32 GetWrittenAttributes( void ) const
  756. {
  757. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  758. }
  759. uint32 GetReadAttributes( void ) const
  760. {
  761. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  762. }
  763. virtual uint64 GetReadControlPointMask() const
  764. {
  765. uint64 nMask = ( 1ULL << m_nControlPointNumber );
  766. if ( m_nScaleCP != -1 )
  767. nMask |= ( 1ULL << m_nScaleCP );
  768. if ( !m_bUseHighestEndCP )
  769. return nMask;
  770. return ~( ( 1ULL << m_nControlPointNumber ) - 1 );
  771. }
  772. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  773. int nParticleCount, int nAttributeWriteMask,
  774. void *pContext) const;
  775. virtual void InitNewParticlesBlock( CParticleCollection *pParticles,
  776. int start_block, int n_blocks, int nAttributeWriteMask,
  777. void *pContext ) const;
  778. void InitParams( CParticleSystemDefinition *pDef )
  779. {
  780. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  781. m_nScaleCP = MAX( -1, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nScaleCP ) );
  782. m_bDistanceBias = ( m_vecDistanceBias.x != 1.0f ) || ( m_vecDistanceBias.y != 1.0f ) || ( m_vecDistanceBias.z != 1.0f );
  783. m_bDistanceBiasAbs = ( m_vecDistanceBiasAbs.x != 0.0f ) || ( m_vecDistanceBiasAbs.y != 0.0f ) || ( m_vecDistanceBiasAbs.z != 0.0f );
  784. }
  785. void Render( CParticleCollection *pParticles ) const;
  786. };
  787. DEFINE_PARTICLE_OPERATOR( C_INIT_CreateWithinSphere, "Position Within Sphere Random", OPERATOR_PI_POSITION );
  788. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_CreateWithinSphere )
  789. DMXELEMENT_UNPACK_FIELD( "distance_min", "0", float, m_fRadiusMin )
  790. DMXELEMENT_UNPACK_FIELD( "distance_max", "0", float, m_fRadiusMax )
  791. DMXELEMENT_UNPACK_FIELD( "distance_bias", "1 1 1", Vector, m_vecDistanceBias )
  792. DMXELEMENT_UNPACK_FIELD( "distance_bias_absolute_value", "0 0 0", Vector, m_vecDistanceBiasAbs )
  793. DMXELEMENT_UNPACK_FIELD( "bias in local system", "0", bool, m_bLocalCoords )
  794. DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
  795. DMXELEMENT_UNPACK_FIELD( "speed_min", "0", float, m_fSpeedMin )
  796. DMXELEMENT_UNPACK_FIELD( "speed_max", "0", float, m_fSpeedMax )
  797. DMXELEMENT_UNPACK_FIELD( "speed_random_exponent", "1", float, m_fSpeedRandExp )
  798. DMXELEMENT_UNPACK_FIELD( "speed_in_local_coordinate_system_min", "0 0 0", Vector, m_LocalCoordinateSystemSpeedMin )
  799. DMXELEMENT_UNPACK_FIELD( "speed_in_local_coordinate_system_max", "0 0 0", Vector, m_LocalCoordinateSystemSpeedMax )
  800. DMXELEMENT_UNPACK_FIELD( "randomly distribute to highest supplied Control Point", "0", bool, m_bUseHighestEndCP )
  801. DMXELEMENT_UNPACK_FIELD( "randomly distribution growth time", "0", float, m_flEndCPGrowthTime )
  802. DMXELEMENT_UNPACK_FIELD( "scale cp (distance/speed/local speed)", "-1", int, m_nScaleCP )
  803. END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateWithinSphere )
  804. ConVar r_sse_s( "r_sse_s", "1", 0, "sse ins for particle sphere create" );
  805. void C_INIT_CreateWithinSphere::InitNewParticlesScalar(
  806. CParticleCollection *pParticles, int start_p,
  807. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  808. {
  809. float flScaleDist = 1.0f;
  810. float flScaleSpeed = 1.0f;
  811. float flScaleLocalSpeed = 1.0f;
  812. if ( m_nScaleCP > -1 )
  813. {
  814. Vector vecScale = pParticles->GetControlPointAtCurrentTime( m_nScaleCP );
  815. flScaleDist = vecScale.x;
  816. flScaleSpeed = vecScale.y;
  817. flScaleLocalSpeed = vecScale.z;
  818. }
  819. CUtlVector< int > nValidControlPoints;
  820. if ( m_bUseHighestEndCP )
  821. {
  822. for ( int i = m_nControlPointNumber; i <= pParticles->GetHighestControlPoint(); i++ )
  823. {
  824. if ( pParticles->GetControlPointAtCurrentTime( i ) == vec3_invalid || pParticles->IsNonPositionalControlPoint( i ) )
  825. continue;
  826. nValidControlPoints.AddToTail( i );
  827. }
  828. }
  829. for( ; nParticleCount--; start_p++ )
  830. {
  831. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  832. float ct = *( pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p ) );
  833. ct = MAX( ct, pParticles->m_flCurTime - pParticles->m_flDt ); // don't ask for times too far in the past. This can happen in systems that don't use creation_time.
  834. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  835. int nCurrentControlPoint = m_nControlPointNumber;
  836. float flStrength = 1.0f;
  837. if ( m_bUseHighestEndCP )
  838. {
  839. //hack for growth time instead of using strength as currently initializers don't support it.
  840. if ( m_flEndCPGrowthTime != 0.0f )
  841. {
  842. flStrength = MIN( pParticles->m_flCurTime, m_flEndCPGrowthTime ) / m_flEndCPGrowthTime;
  843. }
  844. if ( nValidControlPoints.Count() > 0 )
  845. {
  846. if ( flStrength < 1.0 )
  847. {
  848. int nHighestControlPoint = floor( RemapValClamped( flStrength, 0.0f, 1.0f, m_nControlPointNumber, MAX( m_nControlPointNumber, pParticles->GetHighestControlPoint() ) ) );
  849. int nIndex = nValidControlPoints.Find( pParticles->RandomInt( m_nControlPointNumber, nHighestControlPoint ) );
  850. if ( nIndex < 0 || nIndex >= nValidControlPoints.Count() )
  851. {
  852. nIndex = pParticles->RandomInt( 0, nValidControlPoints.Count() - 1 );
  853. }
  854. nCurrentControlPoint = nValidControlPoints[nIndex];
  855. }
  856. else
  857. {
  858. nCurrentControlPoint = nValidControlPoints[pParticles->RandomInt( 0, nValidControlPoints.Count() - 1 )];
  859. }
  860. }
  861. else
  862. {
  863. nCurrentControlPoint = m_nControlPointNumber;
  864. }
  865. }
  866. Vector randpos, randDir;
  867. float flLength = pParticles->RandomVectorInUnitSphere( &randpos );
  868. // Absolute value and biasing for creating hemispheres and ovoids.
  869. if ( m_bDistanceBiasAbs )
  870. {
  871. if ( m_vecDistanceBiasAbs.x != 0.0f )
  872. {
  873. randpos.x = fabs(randpos.x);
  874. }
  875. if ( m_vecDistanceBiasAbs.y != 0.0f )
  876. {
  877. randpos.y = fabs(randpos.y);
  878. }
  879. if ( m_vecDistanceBiasAbs.z != 0.0f )
  880. {
  881. randpos.z = fabs(randpos.z);
  882. }
  883. }
  884. randpos *= m_vecDistanceBias;
  885. randpos.NormalizeInPlace();
  886. randDir = randpos;
  887. randpos *= Lerp( flLength, m_fRadiusMin, m_fRadiusMax );
  888. randpos *= flScaleDist;
  889. if ( !m_bDistanceBias || !m_bLocalCoords )
  890. {
  891. Vector vecControlPoint;
  892. pParticles->GetControlPointAtTime( nCurrentControlPoint, ct, &vecControlPoint );
  893. randpos += vecControlPoint;
  894. }
  895. else
  896. {
  897. matrix3x4_t mat;
  898. pParticles->GetControlPointTransformAtTime( nCurrentControlPoint, ct, &mat );
  899. Vector vecTransformLocal = vec3_origin;
  900. VectorTransform( randpos, mat, vecTransformLocal );
  901. randpos = vecTransformLocal;
  902. }
  903. xyz[0] = randpos.x;
  904. xyz[4] = randpos.y;
  905. xyz[8] = randpos.z;
  906. Assert( IsFinite( xyz[0] ) );
  907. // FIXME: Remove this into a speed setting initializer
  908. if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
  909. {
  910. Vector poffset(0,0,0);
  911. if ( m_fSpeedMax > 0.0 )
  912. {
  913. float rand_speed = pParticles->RandomFloatExp( m_fSpeedMin, m_fSpeedMax, m_fSpeedRandExp );
  914. rand_speed *= flScaleSpeed;
  915. poffset.x -= rand_speed * randDir.x;
  916. poffset.y -= rand_speed * randDir.y;
  917. poffset.z -= rand_speed * randDir.z;
  918. }
  919. poffset -=
  920. pParticles->RandomFloat( m_LocalCoordinateSystemSpeedMin.x, m_LocalCoordinateSystemSpeedMax.x )*
  921. pParticles->ControlPoint( nCurrentControlPoint ).m_ForwardVector * flScaleLocalSpeed;
  922. poffset -=
  923. pParticles->RandomFloat( m_LocalCoordinateSystemSpeedMin.y, m_LocalCoordinateSystemSpeedMax.y )*
  924. pParticles->ControlPoint( nCurrentControlPoint ).m_RightVector * flScaleLocalSpeed;
  925. poffset -=
  926. pParticles->RandomFloat( m_LocalCoordinateSystemSpeedMin.z, m_LocalCoordinateSystemSpeedMax.z )*
  927. pParticles->ControlPoint( nCurrentControlPoint ).m_UpVector * flScaleLocalSpeed;
  928. poffset *= pParticles->m_flPreviousDt;
  929. randpos += poffset;
  930. pxyz[0] = randpos.x;
  931. pxyz[4] = randpos.y;
  932. pxyz[8] = randpos.z;
  933. }
  934. }
  935. }
  936. void C_INIT_CreateWithinSphere::InitNewParticlesBlock( CParticleCollection *pParticles,
  937. int start_block, int n_blocks, int nAttributeWriteMask,
  938. void *pContext ) const
  939. {
  940. // sse-favorable settings
  941. bool bMustUseScalar = m_bUseHighestEndCP;
  942. if ( m_bDistanceBias && m_bLocalCoords )
  943. bMustUseScalar = true;
  944. if ( !bMustUseScalar ) //&&
  945. // (( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) == 0 ) &&
  946. {
  947. float flScaleDist = 1.0f;
  948. float flScaleSpeed = 1.0f;
  949. float flScaleLocalSpeed = 1.0f;
  950. if ( m_nScaleCP > -1 )
  951. {
  952. Vector vecScale = pParticles->GetControlPointAtCurrentTime( m_nScaleCP );
  953. flScaleDist = vecScale.x;
  954. flScaleSpeed = vecScale.y;
  955. flScaleLocalSpeed = vecScale.z;
  956. }
  957. C4VAttributeWriteIterator pXYZ( PARTICLE_ATTRIBUTE_XYZ, pParticles );
  958. pXYZ += start_block;
  959. C4VAttributeWriteIterator pPrevXYZ( PARTICLE_ATTRIBUTE_PREV_XYZ, pParticles );
  960. pPrevXYZ += start_block;
  961. CM128AttributeIterator pCT( PARTICLE_ATTRIBUTE_CREATION_TIME, pParticles );
  962. pCT += start_block;
  963. // now, calculate the terms we need for interpolating control points
  964. FourVectors v4PrevControlPointPosition;
  965. v4PrevControlPointPosition.DuplicateVector( pParticles->ControlPoint( m_nControlPointNumber ).m_PrevPosition );
  966. FourVectors v4ControlPointDelta;
  967. v4ControlPointDelta.DuplicateVector( pParticles->ControlPoint( m_nControlPointNumber ).m_Position );
  968. v4ControlPointDelta -= v4PrevControlPointPosition;
  969. float flOODT = ( pParticles->m_flDt > 0.0 ) ? ( 1.0 / pParticles->m_flDt ) : 0.0;
  970. fltx4 fl4OODt = ReplicateX4( flOODT );
  971. fltx4 fl4PrevTime = ReplicateX4( pParticles->m_flCurTime - pParticles->m_flDt );
  972. int nContext = GetSIMDRandContext();
  973. FourVectors v4DistanceBias;
  974. v4DistanceBias.DuplicateVector( m_vecDistanceBias );
  975. FourVectors v4ConditionalAbsMask;
  976. for( int nComp = 0 ; nComp < 3; nComp++ )
  977. {
  978. v4ConditionalAbsMask[nComp] = ( m_vecDistanceBiasAbs[nComp] > 0 ) ?
  979. LoadAlignedSIMD( ( const float *) g_SIMD_clear_signmask ) :
  980. LoadAlignedSIMD( ( const float *) g_SIMD_AllOnesMask );
  981. }
  982. fltx4 fl4RadiusMin = ReplicateX4( m_fRadiusMin * flScaleDist );
  983. fltx4 fl4RadiusSpread = ReplicateX4( ( m_fRadiusMax - m_fRadiusMin ) * flScaleDist );
  984. int nPowSSEMask = 4.0 * m_fSpeedRandExp;
  985. bool bDoRandSpeed =
  986. ( m_fSpeedMax > 0. ) ||
  987. ( m_LocalCoordinateSystemSpeedMax.x != 0 ) ||
  988. ( m_LocalCoordinateSystemSpeedMax.y != 0 ) ||
  989. ( m_LocalCoordinateSystemSpeedMax.z != 0 ) ||
  990. ( m_LocalCoordinateSystemSpeedMin.x != 0 ) ||
  991. ( m_LocalCoordinateSystemSpeedMin.y != 0 ) ||
  992. ( m_LocalCoordinateSystemSpeedMin.z != 0 );
  993. fltx4 fl4SpeedMin = ReplicateX4( m_fSpeedMin * flScaleSpeed );
  994. fltx4 fl4SpeedRange = ReplicateX4( ( m_fSpeedMax - m_fSpeedMin ) * flScaleSpeed );
  995. fltx4 fl4LocalSpeedMinX = ReplicateX4( m_LocalCoordinateSystemSpeedMin.x * flScaleLocalSpeed );
  996. fltx4 fl4LocalSpeedXSpread = ReplicateX4( ( m_LocalCoordinateSystemSpeedMax.x -
  997. m_LocalCoordinateSystemSpeedMin.x ) * flScaleLocalSpeed );
  998. fltx4 fl4LocalSpeedMinY = ReplicateX4( m_LocalCoordinateSystemSpeedMin.y * flScaleLocalSpeed );
  999. fltx4 fl4LocalSpeedYSpread = ReplicateX4( ( m_LocalCoordinateSystemSpeedMax.y -
  1000. m_LocalCoordinateSystemSpeedMin.y ) * flScaleLocalSpeed );
  1001. fltx4 fl4LocalSpeedMinZ = ReplicateX4( m_LocalCoordinateSystemSpeedMin.z * flScaleLocalSpeed );
  1002. fltx4 fl4LocalSpeedZSpread = ReplicateX4( ( m_LocalCoordinateSystemSpeedMax.z -
  1003. m_LocalCoordinateSystemSpeedMin.z ) * flScaleLocalSpeed );
  1004. FourVectors v4CPForward;
  1005. v4CPForward.DuplicateVector( pParticles->ControlPoint( m_nControlPointNumber ).m_ForwardVector );
  1006. FourVectors v4CPUp;
  1007. v4CPUp.DuplicateVector( pParticles->ControlPoint( m_nControlPointNumber ).m_UpVector );
  1008. FourVectors v4CPRight;
  1009. v4CPRight.DuplicateVector( pParticles->ControlPoint( m_nControlPointNumber ).m_RightVector );
  1010. fltx4 fl4PreviousDt = ReplicateX4( pParticles->m_flPreviousDt );
  1011. while( n_blocks-- )
  1012. {
  1013. FourVectors v4RandPos;
  1014. RandomPointInUnitSphere( nContext, v4RandPos );
  1015. fltx4 fl4Length = v4RandPos.length();
  1016. // conditional absolute value
  1017. v4RandPos.x = AndSIMD( v4RandPos.x, v4ConditionalAbsMask.x );
  1018. v4RandPos.y = AndSIMD( v4RandPos.y, v4ConditionalAbsMask.y );
  1019. v4RandPos.z = AndSIMD( v4RandPos.z, v4ConditionalAbsMask.z );
  1020. v4RandPos *= v4DistanceBias;
  1021. v4RandPos.VectorNormalizeFast();
  1022. FourVectors v4randDir = v4RandPos;
  1023. // lerp radius
  1024. v4RandPos *= AddSIMD( fl4RadiusMin, MulSIMD( fl4Length, fl4RadiusSpread ) );
  1025. v4RandPos += v4PrevControlPointPosition;
  1026. FourVectors cpnt = v4ControlPointDelta;
  1027. cpnt *= MulSIMD( SubSIMD( *pCT, fl4PrevTime ), fl4OODt );
  1028. v4RandPos += cpnt;
  1029. *(pXYZ) = v4RandPos;
  1030. if ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK )
  1031. {
  1032. if ( bDoRandSpeed )
  1033. {
  1034. fltx4 fl4Rand_speed = Pow_FixedPoint_Exponent_SIMD( RandSIMD( nContext ), nPowSSEMask );
  1035. fl4Rand_speed = AddSIMD( fl4SpeedMin, MulSIMD( fl4SpeedRange, fl4Rand_speed ) );
  1036. v4randDir *= fl4Rand_speed;
  1037. // local speed
  1038. FourVectors v4LocalOffset = v4CPForward;
  1039. v4LocalOffset *= AddSIMD( fl4LocalSpeedMinX,
  1040. MulSIMD( fl4LocalSpeedXSpread, RandSIMD( nContext ) ) );
  1041. v4randDir += v4LocalOffset;
  1042. v4LocalOffset = v4CPRight;
  1043. v4LocalOffset *= AddSIMD( fl4LocalSpeedMinY,
  1044. MulSIMD( fl4LocalSpeedYSpread, RandSIMD( nContext ) ) );
  1045. v4randDir += v4LocalOffset;
  1046. v4LocalOffset = v4CPUp;
  1047. v4LocalOffset *= AddSIMD( fl4LocalSpeedMinZ,
  1048. MulSIMD( fl4LocalSpeedZSpread, RandSIMD( nContext ) ) );
  1049. v4randDir += v4LocalOffset;
  1050. v4randDir *= fl4PreviousDt;
  1051. v4RandPos -= v4randDir;
  1052. }
  1053. *(pPrevXYZ) = v4RandPos;
  1054. }
  1055. ++pXYZ;
  1056. ++pPrevXYZ;
  1057. ++pCT;
  1058. }
  1059. ReleaseSIMDRandContext( nContext );
  1060. }
  1061. else
  1062. CParticleOperatorInstance::InitNewParticlesBlock( pParticles, start_block, n_blocks, nAttributeWriteMask, pContext );
  1063. }
  1064. //-----------------------------------------------------------------------------
  1065. // Render visualization
  1066. //-----------------------------------------------------------------------------
  1067. void C_INIT_CreateWithinSphere::Render( CParticleCollection *pParticles ) const
  1068. {
  1069. Vector vecOrigin;
  1070. pParticles->GetControlPointAtTime( m_nControlPointNumber, pParticles->m_flCurTime, &vecOrigin );
  1071. RenderWireframeSphere( vecOrigin, m_fRadiusMin, 16, 8, Color( 192, 192, 0, 255 ), false );
  1072. RenderWireframeSphere( vecOrigin, m_fRadiusMax, 16, 8, Color( 128, 128, 0, 255 ), false );
  1073. }
  1074. class C_INIT_CreateWithinBox : public CParticleInitializerOperatorInstance
  1075. {
  1076. DECLARE_PARTICLE_OPERATOR( C_INIT_CreateWithinBox );
  1077. Vector m_vecMin;
  1078. Vector m_vecMax;
  1079. int m_nControlPointNumber;
  1080. bool m_bLocalSpace;
  1081. uint32 GetWrittenAttributes( void ) const
  1082. {
  1083. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  1084. }
  1085. uint32 GetReadAttributes( void ) const
  1086. {
  1087. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  1088. }
  1089. virtual uint64 GetReadControlPointMask() const
  1090. {
  1091. return 1ULL << m_nControlPointNumber;
  1092. }
  1093. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  1094. int nParticleCount, int nAttributeWriteMask,
  1095. void *pContext) const;
  1096. void Render( CParticleCollection *pParticles ) const;
  1097. };
  1098. DEFINE_PARTICLE_OPERATOR( C_INIT_CreateWithinBox, "Position Within Box Random", OPERATOR_PI_POSITION );
  1099. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_CreateWithinBox )
  1100. DMXELEMENT_UNPACK_FIELD( "min", "0 0 0", Vector, m_vecMin )
  1101. DMXELEMENT_UNPACK_FIELD( "max", "0 0 0", Vector, m_vecMax )
  1102. DMXELEMENT_UNPACK_FIELD( "control point number", "0", int, m_nControlPointNumber )
  1103. DMXELEMENT_UNPACK_FIELD( "use local space", "0", bool, m_bLocalSpace )
  1104. END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateWithinBox )
  1105. void C_INIT_CreateWithinBox::InitNewParticlesScalar(
  1106. CParticleCollection *pParticles, int start_p,
  1107. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1108. {
  1109. int nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  1110. for( ; nParticleCount--; start_p++ )
  1111. {
  1112. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  1113. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  1114. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  1115. Vector randpos;
  1116. pParticles->RandomVector( m_vecMin, m_vecMax, &randpos );
  1117. if ( m_bLocalSpace )
  1118. {
  1119. matrix3x4_t mat;
  1120. pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, *ct, &mat );
  1121. Vector vecTransformLocal = vec3_origin;
  1122. VectorRotate( randpos, mat, vecTransformLocal );
  1123. randpos = vecTransformLocal;
  1124. }
  1125. else
  1126. {
  1127. Vector vecControlPoint;
  1128. pParticles->GetControlPointAtTime( nControlPointNumber, *ct, &vecControlPoint );
  1129. randpos += vecControlPoint;
  1130. }
  1131. xyz[0] = randpos.x;
  1132. xyz[4] = randpos.y;
  1133. xyz[8] = randpos.z;
  1134. if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
  1135. {
  1136. pxyz[0] = randpos.x;
  1137. pxyz[4] = randpos.y;
  1138. pxyz[8] = randpos.z;
  1139. }
  1140. }
  1141. }
  1142. //-----------------------------------------------------------------------------
  1143. // Render visualization
  1144. //-----------------------------------------------------------------------------
  1145. void C_INIT_CreateWithinBox::Render( CParticleCollection *pParticles ) const
  1146. {
  1147. Vector vecOrigin;
  1148. pParticles->GetControlPointAtTime( m_nControlPointNumber, pParticles->m_flCurTime, &vecOrigin );
  1149. RenderWireframeBox( vecOrigin, vec3_angle, m_vecMin, m_vecMax, Color( 192, 192, 0, 255 ), false );
  1150. }
  1151. //-----------------------------------------------------------------------------
  1152. // Position Offset Initializer
  1153. // offsets initial position of particles within a random vector range,
  1154. // while still respecting spherical/conical spacial and velocity initialization
  1155. //-----------------------------------------------------------------------------
  1156. class C_INIT_PositionOffset : public CParticleInitializerOperatorInstance
  1157. {
  1158. DECLARE_PARTICLE_OPERATOR( C_INIT_PositionOffset );
  1159. Vector m_OffsetMin;
  1160. Vector m_OffsetMax;
  1161. int m_nControlPointNumber;
  1162. bool m_bLocalCoords;
  1163. bool m_bProportional;
  1164. uint32 GetWrittenAttributes( void ) const
  1165. {
  1166. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  1167. }
  1168. uint32 GetReadAttributes( void ) const
  1169. {
  1170. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_CREATION_TIME_MASK | PARTICLE_ATTRIBUTE_RADIUS_MASK;
  1171. }
  1172. virtual uint64 GetReadControlPointMask() const
  1173. {
  1174. return 1ULL << m_nControlPointNumber;
  1175. }
  1176. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  1177. int nParticleCount, int nAttributeWriteMask,
  1178. void *pContext) const;
  1179. void InitParams( CParticleSystemDefinition *pDef )
  1180. {
  1181. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  1182. }
  1183. bool InitMultipleOverride ( void ) { return true; }
  1184. void Render( CParticleCollection *pParticles ) const;
  1185. };
  1186. DEFINE_PARTICLE_OPERATOR( C_INIT_PositionOffset, "Position Modify Offset Random", OPERATOR_GENERIC );
  1187. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_PositionOffset )
  1188. DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
  1189. DMXELEMENT_UNPACK_FIELD( "offset min", "0 0 0", Vector, m_OffsetMin )
  1190. DMXELEMENT_UNPACK_FIELD( "offset max", "0 0 0", Vector, m_OffsetMax )
  1191. DMXELEMENT_UNPACK_FIELD( "offset in local space 0/1", "0", bool, m_bLocalCoords )
  1192. DMXELEMENT_UNPACK_FIELD( "offset proportional to radius 0/1", "0", bool, m_bProportional )
  1193. END_PARTICLE_OPERATOR_UNPACK( C_INIT_PositionOffset )
  1194. void C_INIT_PositionOffset::InitNewParticlesScalar(
  1195. CParticleCollection *pParticles, int start_p,
  1196. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1197. {
  1198. for( ; nParticleCount--; start_p++ )
  1199. {
  1200. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  1201. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  1202. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  1203. const float *radius = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_RADIUS, start_p );
  1204. Vector randpos;
  1205. if ( m_bProportional )
  1206. {
  1207. pParticles->RandomVector( (m_OffsetMin * *radius), (m_OffsetMax * *radius), &randpos );
  1208. }
  1209. else
  1210. {
  1211. pParticles->RandomVector( m_OffsetMin, m_OffsetMax, &randpos );
  1212. }
  1213. if ( m_bLocalCoords )
  1214. {
  1215. matrix3x4_t mat;
  1216. pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, *ct, &mat );
  1217. Vector vecTransformLocal = vec3_origin;
  1218. VectorRotate( randpos, mat, vecTransformLocal );
  1219. randpos = vecTransformLocal;
  1220. }
  1221. xyz[0] += randpos.x;
  1222. xyz[4] += randpos.y;
  1223. xyz[8] += randpos.z;
  1224. pxyz[0] += randpos.x;
  1225. pxyz[4] += randpos.y;
  1226. pxyz[8] += randpos.z;
  1227. }
  1228. }
  1229. //-----------------------------------------------------------------------------
  1230. // Render visualization
  1231. //-----------------------------------------------------------------------------
  1232. void C_INIT_PositionOffset::Render( CParticleCollection *pParticles ) const
  1233. {
  1234. Vector vecOrigin (0,0,0);
  1235. Vector vecMinExtent = m_OffsetMin;
  1236. Vector vecMaxExtent = m_OffsetMax;
  1237. if ( m_bLocalCoords )
  1238. {
  1239. matrix3x4_t mat;
  1240. pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, pParticles->m_flCurTime, &mat );
  1241. VectorRotate( m_OffsetMin, mat, vecMinExtent );
  1242. VectorRotate( m_OffsetMax, mat, vecMaxExtent );
  1243. }
  1244. else
  1245. {
  1246. pParticles->GetControlPointAtTime( m_nControlPointNumber, pParticles->m_flCurTime, &vecOrigin );
  1247. }
  1248. RenderWireframeBox( vecOrigin, vec3_angle, vecMinExtent , vecMaxExtent , Color( 192, 192, 0, 255 ), false );
  1249. }
  1250. //-----------------------------------------------------------------------------
  1251. // Position Modify Place on Ground Initializer
  1252. //-----------------------------------------------------------------------------
  1253. class C_INIT_PositionPlaceOnGround : public CParticleInitializerOperatorInstance
  1254. {
  1255. DECLARE_PARTICLE_OPERATOR( C_INIT_PositionPlaceOnGround );
  1256. float m_flOffset;
  1257. float m_flMaxTraceLength;
  1258. char m_CollisionGroupName[128];
  1259. int m_nCollisionGroupNumber;
  1260. unsigned int m_CollisionMask;
  1261. bool m_bKill;
  1262. bool m_bIncludeWater;
  1263. bool m_bSetNormal;
  1264. uint32 GetWrittenAttributes( void ) const
  1265. {
  1266. uint32 nMask = PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
  1267. if ( m_bSetNormal )
  1268. nMask = PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK | PARTICLE_ATTRIBUTE_NORMAL_MASK;
  1269. return nMask;
  1270. }
  1271. uint32 GetReadAttributes( void ) const
  1272. {
  1273. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  1274. }
  1275. void InitParams( CParticleSystemDefinition *pDef )
  1276. {
  1277. m_nCollisionGroupNumber = g_pParticleSystemMgr->Query()->GetCollisionGroupFromName( m_CollisionGroupName );
  1278. if ( m_bIncludeWater )
  1279. m_CollisionMask = MASK_SHOT_HULL|MASK_SPLITAREAPORTAL;
  1280. else
  1281. m_CollisionMask = MASK_SHOT_HULL;
  1282. }
  1283. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  1284. int nParticleCount, int nAttributeWriteMask,
  1285. void *pContext) const;
  1286. bool InitMultipleOverride ( void ) { return true; }
  1287. };
  1288. DEFINE_PARTICLE_OPERATOR( C_INIT_PositionPlaceOnGround, "Position Modify Place On Ground", OPERATOR_GENERIC );
  1289. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_PositionPlaceOnGround )
  1290. DMXELEMENT_UNPACK_FIELD( "offset", "0", float, m_flOffset )
  1291. DMXELEMENT_UNPACK_FIELD( "kill on no collision", "0", bool, m_bKill )
  1292. DMXELEMENT_UNPACK_FIELD( "include water", "0", bool, m_bIncludeWater )
  1293. DMXELEMENT_UNPACK_FIELD( "set normal", "0", bool, m_bSetNormal )
  1294. DMXELEMENT_UNPACK_FIELD( "max trace length", "128", float, m_flMaxTraceLength )
  1295. DMXELEMENT_UNPACK_FIELD_STRING( "collision group", "NONE", m_CollisionGroupName )
  1296. END_PARTICLE_OPERATOR_UNPACK( C_INIT_PositionPlaceOnGround )
  1297. void C_INIT_PositionPlaceOnGround::InitNewParticlesScalar(
  1298. CParticleCollection *pParticles, int start_p,
  1299. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1300. {
  1301. // Trace down
  1302. Vector TraceDir=Vector(0, 0, -1);
  1303. for( ; nParticleCount--; start_p++ )
  1304. {
  1305. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  1306. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  1307. float *plife = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
  1308. Vector vecStartPos;
  1309. SetVectorFromAttribute( vecStartPos, xyz );
  1310. CBaseTrace tr;
  1311. g_pParticleSystemMgr->Query()->TraceLine( vecStartPos, ( vecStartPos + ( TraceDir * m_flMaxTraceLength ) ), m_CollisionMask, NULL, m_nCollisionGroupNumber, &tr );
  1312. if ( tr.fraction == 1.0 && m_bKill )
  1313. {
  1314. *plife = -1.0f;
  1315. }
  1316. else
  1317. {
  1318. float flGroundPos = ( tr.fraction * m_flMaxTraceLength ) - m_flOffset;
  1319. xyz[8] -= flGroundPos;
  1320. pxyz[8] -= flGroundPos;
  1321. }
  1322. if ( m_bSetNormal )
  1323. {
  1324. float *normal = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_NORMAL, start_p );
  1325. SetVectorAttribute( normal, tr.plane.normal );
  1326. }
  1327. }
  1328. }
  1329. //-----------------------------------------------------------------------------
  1330. //
  1331. // Velocity-based Operators
  1332. //
  1333. //-----------------------------------------------------------------------------
  1334. //-----------------------------------------------------------------------------
  1335. // Random velocity initializer
  1336. //-----------------------------------------------------------------------------
  1337. class C_INIT_VelocityRandom : public CParticleInitializerOperatorInstance
  1338. {
  1339. DECLARE_PARTICLE_OPERATOR( C_INIT_VelocityRandom );
  1340. uint32 GetWrittenAttributes( void ) const
  1341. {
  1342. return PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  1343. }
  1344. uint32 GetReadAttributes( void ) const
  1345. {
  1346. return PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  1347. }
  1348. virtual uint64 GetReadControlPointMask() const
  1349. {
  1350. if ( m_bHasLocalSpeed )
  1351. return 1ULL << m_nControlPointNumber;
  1352. return 0;
  1353. }
  1354. virtual bool InitMultipleOverride() { return true; }
  1355. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  1356. int nParticleCount, int nAttributeWriteMask,
  1357. void *pContext) const;
  1358. void InitParams( CParticleSystemDefinition *pDef )
  1359. {
  1360. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  1361. m_bHasLocalSpeed = ( m_LocalCoordinateSystemSpeedMin != vec3_origin ) || ( m_LocalCoordinateSystemSpeedMax != vec3_origin );
  1362. if ( m_fSpeedMax < m_fSpeedMin )
  1363. {
  1364. V_swap( m_fSpeedMin, m_fSpeedMax );
  1365. }
  1366. }
  1367. private:
  1368. int m_nControlPointNumber;
  1369. float m_fSpeedMin;
  1370. float m_fSpeedMax;
  1371. Vector m_LocalCoordinateSystemSpeedMin;
  1372. Vector m_LocalCoordinateSystemSpeedMax;
  1373. bool m_bHasLocalSpeed;
  1374. };
  1375. DEFINE_PARTICLE_OPERATOR( C_INIT_VelocityRandom, "Velocity Random", OPERATOR_GENERIC );
  1376. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_VelocityRandom )
  1377. DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
  1378. DMXELEMENT_UNPACK_FIELD( "random_speed_min", "0", float, m_fSpeedMin )
  1379. DMXELEMENT_UNPACK_FIELD( "random_speed_max", "0", float, m_fSpeedMax )
  1380. DMXELEMENT_UNPACK_FIELD( "speed_in_local_coordinate_system_min", "0 0 0", Vector, m_LocalCoordinateSystemSpeedMin )
  1381. DMXELEMENT_UNPACK_FIELD( "speed_in_local_coordinate_system_max", "0 0 0", Vector, m_LocalCoordinateSystemSpeedMax )
  1382. END_PARTICLE_OPERATOR_UNPACK( C_INIT_VelocityRandom )
  1383. void C_INIT_VelocityRandom::InitNewParticlesScalar(
  1384. CParticleCollection *pParticles, int start_p,
  1385. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1386. {
  1387. for( ; nParticleCount--; start_p++ )
  1388. {
  1389. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  1390. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  1391. Vector vecVelocity( 0.0f, 0.0f, 0.0f );
  1392. if ( m_bHasLocalSpeed )
  1393. {
  1394. Vector vecRandomSpeed, vecForward, vecUp, vecRight;
  1395. pParticles->RandomVector( m_LocalCoordinateSystemSpeedMin, m_LocalCoordinateSystemSpeedMax, &vecRandomSpeed );
  1396. pParticles->GetControlPointOrientationAtTime( m_nControlPointNumber, *ct, &vecForward, &vecRight, &vecUp );
  1397. VectorMA( vecVelocity, vecRandomSpeed.x, vecForward, vecVelocity );
  1398. VectorMA( vecVelocity, -vecRandomSpeed.y, vecRight, vecVelocity );
  1399. VectorMA( vecVelocity, vecRandomSpeed.z, vecUp, vecVelocity );
  1400. }
  1401. if ( m_fSpeedMax > 0.0f )
  1402. {
  1403. Vector vecRandomSpeed;
  1404. pParticles->RandomVector( m_fSpeedMin, m_fSpeedMax, &vecRandomSpeed );
  1405. vecVelocity += vecRandomSpeed;
  1406. }
  1407. vecVelocity *= pParticles->m_flPreviousDt;
  1408. pxyz[0] -= vecVelocity.x;
  1409. pxyz[4] -= vecVelocity.y;
  1410. pxyz[8] -= vecVelocity.z;
  1411. }
  1412. }
  1413. //-----------------------------------------------------------------------------
  1414. // Initial Velocity Noise Operator
  1415. //-----------------------------------------------------------------------------
  1416. class C_INIT_InitialVelocityNoise : public CParticleInitializerOperatorInstance
  1417. {
  1418. DECLARE_PARTICLE_OPERATOR( C_INIT_InitialVelocityNoise );
  1419. uint32 GetWrittenAttributes( void ) const
  1420. {
  1421. return PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  1422. }
  1423. uint32 GetReadAttributes( void ) const
  1424. {
  1425. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_XYZ_MASK;
  1426. }
  1427. virtual uint64 GetReadControlPointMask() const
  1428. {
  1429. return 1ULL << m_nControlPointNumber;
  1430. }
  1431. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  1432. int nParticleCount, int nAttributeWriteMask,
  1433. void *pContext) const;
  1434. void InitNewParticlesBlock( CParticleCollection *pParticles,
  1435. int start_block, int n_blocks, int nAttributeWriteMask,
  1436. void *pContext ) const;
  1437. void InitParams( CParticleSystemDefinition *pDef )
  1438. {
  1439. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  1440. }
  1441. virtual bool InitMultipleOverride() { return true; }
  1442. Vector m_vecAbsVal, m_vecAbsValInv, m_vecOffsetLoc;
  1443. float m_flOffset;
  1444. Vector m_vecOutputMin;
  1445. Vector m_vecOutputMax;
  1446. float m_flNoiseScale, m_flNoiseScaleLoc;
  1447. int nRemainingBlocks, m_nControlPointNumber;
  1448. bool m_bLocalSpace;
  1449. };
  1450. DEFINE_PARTICLE_OPERATOR( C_INIT_InitialVelocityNoise, "Velocity Noise", OPERATOR_GENERIC );
  1451. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_InitialVelocityNoise )
  1452. DMXELEMENT_UNPACK_FIELD( "Control Point Number","0",int,m_nControlPointNumber)
  1453. DMXELEMENT_UNPACK_FIELD( "Time Noise Coordinate Scale","1",float,m_flNoiseScale)
  1454. DMXELEMENT_UNPACK_FIELD( "Spatial Noise Coordinate Scale","0.01",float,m_flNoiseScaleLoc)
  1455. DMXELEMENT_UNPACK_FIELD( "Time Coordinate Offset","0", float, m_flOffset )
  1456. DMXELEMENT_UNPACK_FIELD( "Spatial Coordinate Offset","0 0 0", Vector, m_vecOffsetLoc )
  1457. DMXELEMENT_UNPACK_FIELD( "Absolute Value","0 0 0", Vector, m_vecAbsVal )
  1458. DMXELEMENT_UNPACK_FIELD( "Invert Abs Value","0 0 0", Vector, m_vecAbsValInv )
  1459. DMXELEMENT_UNPACK_FIELD( "output minimum","0 0 0", Vector, m_vecOutputMin )
  1460. DMXELEMENT_UNPACK_FIELD( "output maximum","1 1 1", Vector, m_vecOutputMax )
  1461. DMXELEMENT_UNPACK_FIELD( "Apply Velocity in Local Space (0/1)","0", bool, m_bLocalSpace )
  1462. END_PARTICLE_OPERATOR_UNPACK( C_INIT_InitialVelocityNoise );
  1463. void C_INIT_InitialVelocityNoise::InitNewParticlesBlock( CParticleCollection *pParticles,
  1464. int start_block, int n_blocks, int nAttributeWriteMask,
  1465. void *pContext ) const
  1466. {
  1467. float flAbsScaleX, flAbsScaleY, flAbsScaleZ;
  1468. fltx4 fl4AbsValX, fl4AbsValY, fl4AbsValZ;
  1469. fl4AbsValX = ( fltx4 ) CmpEqSIMD( Four_Zeros, Four_Zeros );
  1470. fl4AbsValY = fl4AbsValX;
  1471. fl4AbsValZ = fl4AbsValX;
  1472. flAbsScaleX = 0.5;
  1473. flAbsScaleY = 0.5;
  1474. flAbsScaleZ = 0.5;
  1475. // Set up single if check for absolute value inversion inside the loop
  1476. bool m_bNoiseAbs = ( m_vecAbsValInv.x != 0.0f ) || ( m_vecAbsValInv.y != 0.0f ) || ( m_vecAbsValInv.z != 0.0f );
  1477. // Set up values for more optimal absolute value calculations inside the loop
  1478. if ( m_vecAbsVal.x != 0.0f )
  1479. {
  1480. fl4AbsValX = LoadAlignedSIMD( (float *) g_SIMD_clear_signmask );
  1481. flAbsScaleX = 1.0;
  1482. }
  1483. if ( m_vecAbsVal.y != 0.0f )
  1484. {
  1485. fl4AbsValY = LoadAlignedSIMD( (float *) g_SIMD_clear_signmask );
  1486. flAbsScaleY = 1.0;
  1487. }
  1488. if ( m_vecAbsVal.z != 0.0f )
  1489. {
  1490. fl4AbsValZ = LoadAlignedSIMD( (float *) g_SIMD_clear_signmask );
  1491. flAbsScaleZ = 1.0;
  1492. }
  1493. float ValueScaleX, ValueScaleY, ValueScaleZ, ValueBaseX, ValueBaseY, ValueBaseZ;
  1494. ValueScaleX = ( flAbsScaleX *(m_vecOutputMax.x-m_vecOutputMin.x ) );
  1495. ValueBaseX = (m_vecOutputMin.x+ ( ( 1.0 - flAbsScaleX ) *( m_vecOutputMax.x-m_vecOutputMin.x ) ) );
  1496. ValueScaleY = ( flAbsScaleY *(m_vecOutputMax.y-m_vecOutputMin.y ) );
  1497. ValueBaseY = (m_vecOutputMin.y+ ( ( 1.0 - flAbsScaleY ) *( m_vecOutputMax.y-m_vecOutputMin.y ) ) );
  1498. ValueScaleZ = ( flAbsScaleZ *(m_vecOutputMax.z-m_vecOutputMin.z ) );
  1499. ValueBaseZ = (m_vecOutputMin.z+ ( ( 1.0 - flAbsScaleZ ) *( m_vecOutputMax.z-m_vecOutputMin.z ) ) );
  1500. fltx4 fl4ValueBaseX = ReplicateX4( ValueBaseX );
  1501. fltx4 fl4ValueBaseY = ReplicateX4( ValueBaseY );
  1502. fltx4 fl4ValueBaseZ = ReplicateX4( ValueBaseZ );
  1503. fltx4 fl4ValueScaleX = ReplicateX4( ValueScaleX );
  1504. fltx4 fl4ValueScaleY = ReplicateX4( ValueScaleY );
  1505. fltx4 fl4ValueScaleZ = ReplicateX4( ValueScaleZ );
  1506. float CoordScale = m_flNoiseScale;
  1507. float CoordScaleLoc = m_flNoiseScaleLoc;
  1508. Vector ofs_y = Vector( 100000.5, 300000.25, 9000000.75 );
  1509. Vector ofs_z = Vector( 110000.25, 310000.75, 9100000.5 );
  1510. size_t attr_stride;
  1511. const FourVectors *xyz = pParticles->Get4VAttributePtr( PARTICLE_ATTRIBUTE_XYZ, &attr_stride );
  1512. xyz += attr_stride * start_block;
  1513. FourVectors *pxyz = pParticles->Get4VAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, &attr_stride );
  1514. pxyz += attr_stride * start_block;
  1515. const fltx4 *pCreationTime = pParticles->GetM128AttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, &attr_stride );
  1516. pCreationTime += attr_stride * start_block;
  1517. // setup
  1518. fltx4 fl4Offset = ReplicateX4( m_flOffset );
  1519. FourVectors fvOffsetLoc;
  1520. fvOffsetLoc.DuplicateVector( m_vecOffsetLoc );
  1521. CParticleSIMDTransformation CPTransform;
  1522. float flCreationTime = SubFloat( *pCreationTime, 0 );
  1523. pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, flCreationTime, &CPTransform );
  1524. while( n_blocks-- )
  1525. {
  1526. FourVectors fvCoordLoc = *xyz;
  1527. fvCoordLoc += fvOffsetLoc;
  1528. FourVectors fvCoord;
  1529. fvCoord.x = AddSIMD(*pCreationTime, fl4Offset);
  1530. fvCoord.y = AddSIMD(*pCreationTime, fl4Offset);
  1531. fvCoord.z = AddSIMD(*pCreationTime, fl4Offset);
  1532. fvCoordLoc *= CoordScaleLoc;
  1533. fvCoord *= CoordScale;
  1534. fvCoord += fvCoordLoc;
  1535. FourVectors fvCoord2 = fvCoord;
  1536. FourVectors fvOffsetTemp;
  1537. fvOffsetTemp.DuplicateVector( ofs_y );
  1538. fvCoord2 += fvOffsetTemp;
  1539. FourVectors fvCoord3 = fvCoord;
  1540. fvOffsetTemp.DuplicateVector( ofs_z );
  1541. fvCoord3 += fvOffsetTemp;
  1542. fltx4 fl4NoiseX;
  1543. fltx4 fl4NoiseY;
  1544. fltx4 fl4NoiseZ;
  1545. fl4NoiseX = NoiseSIMD( fvCoord );
  1546. fl4NoiseY = NoiseSIMD( fvCoord2 );
  1547. fl4NoiseZ = NoiseSIMD( fvCoord3 );
  1548. fl4NoiseX = AndSIMD ( fl4NoiseX, fl4AbsValX );
  1549. fl4NoiseY = AndSIMD ( fl4NoiseY, fl4AbsValY );
  1550. fl4NoiseZ = AndSIMD ( fl4NoiseZ, fl4AbsValZ );
  1551. if ( m_bNoiseAbs )
  1552. {
  1553. if ( m_vecAbsValInv.x != 0.0f )
  1554. {
  1555. fl4NoiseX = SubSIMD( Four_Ones, fl4NoiseX );
  1556. }
  1557. if ( m_vecAbsValInv.y != 0.0f )
  1558. {
  1559. fl4NoiseY = SubSIMD( Four_Ones, fl4NoiseY );
  1560. }
  1561. if ( m_vecAbsValInv.z != 0.0f )
  1562. {
  1563. fl4NoiseZ = SubSIMD( Four_Ones, fl4NoiseZ );
  1564. }
  1565. }
  1566. FourVectors fvOffset;
  1567. fvOffset.x = AddSIMD( fl4ValueBaseX, ( MulSIMD( fl4ValueScaleX , fl4NoiseX ) ) );
  1568. fvOffset.y = AddSIMD( fl4ValueBaseY, ( MulSIMD( fl4ValueScaleY , fl4NoiseY ) ) );
  1569. fvOffset.z = AddSIMD( fl4ValueBaseZ, ( MulSIMD( fl4ValueScaleZ , fl4NoiseZ ) ) );
  1570. fvOffset *= pParticles->m_flPreviousDt;
  1571. if ( m_bLocalSpace )
  1572. {
  1573. CPTransform.VectorRotate( fvOffset );
  1574. }
  1575. *pxyz -= fvOffset;
  1576. xyz += attr_stride;
  1577. pxyz += attr_stride;
  1578. pCreationTime += attr_stride;
  1579. }
  1580. }
  1581. void C_INIT_InitialVelocityNoise::InitNewParticlesScalar(
  1582. CParticleCollection *pParticles, int start_p,
  1583. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1584. {
  1585. float flAbsScaleX, flAbsScaleY, flAbsScaleZ;
  1586. int nAbsValX, nAbsValY, nAbsValZ;
  1587. nAbsValX = 0xffffffff;
  1588. nAbsValY = 0xffffffff;
  1589. nAbsValZ = 0xffffffff;
  1590. flAbsScaleX = 0.5;
  1591. flAbsScaleY = 0.5;
  1592. flAbsScaleZ = 0.5;
  1593. // Set up single if check for absolute value inversion inside the loop
  1594. bool m_bNoiseAbs = ( m_vecAbsValInv.x != 0.0f ) || ( m_vecAbsValInv.y != 0.0f ) || ( m_vecAbsValInv.z != 0.0f );
  1595. // Set up values for more optimal absolute value calculations inside the loop
  1596. if ( m_vecAbsVal.x != 0.0f )
  1597. {
  1598. nAbsValX = 0x7fffffff;
  1599. flAbsScaleX = 1.0;
  1600. }
  1601. if ( m_vecAbsVal.y != 0.0f )
  1602. {
  1603. nAbsValY = 0x7fffffff;
  1604. flAbsScaleY = 1.0;
  1605. }
  1606. if ( m_vecAbsVal.z != 0.0f )
  1607. {
  1608. nAbsValZ = 0x7fffffff;
  1609. flAbsScaleZ = 1.0;
  1610. }
  1611. float ValueScaleX, ValueScaleY, ValueScaleZ, ValueBaseX, ValueBaseY, ValueBaseZ;
  1612. ValueScaleX = ( flAbsScaleX *(m_vecOutputMax.x-m_vecOutputMin.x ) );
  1613. ValueBaseX = (m_vecOutputMin.x+ ( ( 1.0 - flAbsScaleX ) *( m_vecOutputMax.x-m_vecOutputMin.x ) ) );
  1614. ValueScaleY = ( flAbsScaleY *(m_vecOutputMax.y-m_vecOutputMin.y ) );
  1615. ValueBaseY = (m_vecOutputMin.y+ ( ( 1.0 - flAbsScaleY ) *( m_vecOutputMax.y-m_vecOutputMin.y ) ) );
  1616. ValueScaleZ = ( flAbsScaleZ *(m_vecOutputMax.z-m_vecOutputMin.z ) );
  1617. ValueBaseZ = (m_vecOutputMin.z+ ( ( 1.0 - flAbsScaleZ ) *( m_vecOutputMax.z-m_vecOutputMin.z ) ) );
  1618. float CoordScale = m_flNoiseScale;
  1619. float CoordScaleLoc = m_flNoiseScaleLoc;
  1620. Vector ofs_y = Vector( 100000.5, 300000.25, 9000000.75 );
  1621. Vector ofs_z = Vector( 110000.25, 310000.75, 9100000.5 );
  1622. for( ; nParticleCount--; start_p++ )
  1623. {
  1624. const float *xyz = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_XYZ, start_p );
  1625. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  1626. const float *pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  1627. Vector Coord, Coord2, Coord3, CoordLoc;
  1628. SetVectorFromAttribute( CoordLoc, xyz );
  1629. CoordLoc += m_vecOffsetLoc;
  1630. float Offset = m_flOffset;
  1631. Coord = Vector ( (*pCreationTime + Offset), (*pCreationTime + Offset), (*pCreationTime + Offset) );
  1632. Coord *= CoordScale;
  1633. CoordLoc *= CoordScaleLoc;
  1634. Coord += CoordLoc;
  1635. Coord2 = ( Coord );
  1636. Coord3 = ( Coord );
  1637. fltx4 flNoise128;
  1638. FourVectors fvNoise;
  1639. fvNoise.DuplicateVector( Coord );
  1640. flNoise128 = NoiseSIMD( fvNoise );
  1641. float flNoiseX = SubFloat( flNoise128, 0 );
  1642. fvNoise.DuplicateVector( Coord2 + ofs_y );
  1643. flNoise128 = NoiseSIMD( fvNoise );
  1644. float flNoiseY = SubFloat( flNoise128, 0 );
  1645. fvNoise.DuplicateVector( Coord3 + ofs_z );
  1646. flNoise128 = NoiseSIMD( fvNoise );
  1647. float flNoiseZ = SubFloat( flNoise128, 0 );
  1648. *( (int *) &flNoiseX) &= nAbsValX;
  1649. *( (int *) &flNoiseY) &= nAbsValY;
  1650. *( (int *) &flNoiseZ) &= nAbsValZ;
  1651. if ( m_bNoiseAbs )
  1652. {
  1653. if ( m_vecAbsValInv.x != 0.0f )
  1654. {
  1655. flNoiseX = 1.0 - flNoiseX;
  1656. }
  1657. if ( m_vecAbsValInv.y != 0.0f )
  1658. {
  1659. flNoiseY = 1.0 - flNoiseY;
  1660. }
  1661. if ( m_vecAbsValInv.z != 0.0f )
  1662. {
  1663. flNoiseZ = 1.0 - flNoiseZ;
  1664. }
  1665. }
  1666. Vector poffset;
  1667. poffset.x = ( ValueBaseX + ( ValueScaleX * flNoiseX ) );
  1668. poffset.y = ( ValueBaseY + ( ValueScaleY * flNoiseY ) );
  1669. poffset.z = ( ValueBaseZ + ( ValueScaleZ * flNoiseZ ) );
  1670. poffset *= pParticles->m_flPreviousDt;
  1671. if ( m_bLocalSpace )
  1672. {
  1673. matrix3x4_t mat;
  1674. pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, *pCreationTime, &mat );
  1675. Vector vecTransformLocal = vec3_origin;
  1676. VectorRotate( poffset, mat, vecTransformLocal );
  1677. poffset = vecTransformLocal;
  1678. }
  1679. pxyz[0] -= poffset.x;
  1680. pxyz[4] -= poffset.y;
  1681. pxyz[8] -= poffset.z;
  1682. }
  1683. }
  1684. class C_INIT_RandomLifeTime : public CParticleInitializerOperatorInstance
  1685. {
  1686. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomLifeTime );
  1687. float m_fLifetimeMin;
  1688. float m_fLifetimeMax;
  1689. float m_fLifetimeRandExponent;
  1690. uint32 GetWrittenAttributes( void ) const
  1691. {
  1692. return PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
  1693. }
  1694. uint32 GetReadAttributes( void ) const
  1695. {
  1696. return 0;
  1697. }
  1698. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  1699. int nParticleCount, int nAttributeWriteMask, void *pContext ) const;
  1700. void InitNewParticlesBlock( CParticleCollection *pParticles,
  1701. int start_block, int n_blocks, int nAttributeWriteMask,
  1702. void *pContext ) const
  1703. {
  1704. InitScalarAttributeRandomRangeExpBlock( PARTICLE_ATTRIBUTE_LIFE_DURATION,
  1705. m_fLifetimeMin, m_fLifetimeMax, m_fLifetimeRandExponent,
  1706. pParticles, start_block, n_blocks );
  1707. }
  1708. };
  1709. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomLifeTime, "Lifetime Random", OPERATOR_GENERIC );
  1710. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomLifeTime )
  1711. DMXELEMENT_UNPACK_FIELD( "lifetime_min", "0", float, m_fLifetimeMin )
  1712. DMXELEMENT_UNPACK_FIELD( "lifetime_max", "0", float, m_fLifetimeMax )
  1713. DMXELEMENT_UNPACK_FIELD( "lifetime_random_exponent", "1", float, m_fLifetimeRandExponent )
  1714. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomLifeTime )
  1715. void C_INIT_RandomLifeTime::InitNewParticlesScalar(
  1716. CParticleCollection *pParticles, int start_p,
  1717. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1718. {
  1719. InitScalarAttributeRandomRangeExpScalar( PARTICLE_ATTRIBUTE_LIFE_DURATION,
  1720. m_fLifetimeMin, m_fLifetimeMax, m_fLifetimeRandExponent,
  1721. pParticles, start_p, nParticleCount );
  1722. }
  1723. class C_INIT_RandomScalar : public CParticleInitializerOperatorInstance
  1724. {
  1725. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomScalar );
  1726. float m_flMin;
  1727. float m_flMax;
  1728. float m_flExponent;
  1729. int m_nFieldOutput;
  1730. uint32 GetWrittenAttributes( void ) const
  1731. {
  1732. return 1 << m_nFieldOutput;
  1733. }
  1734. uint32 GetReadAttributes( void ) const
  1735. {
  1736. return 0;
  1737. }
  1738. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  1739. int nParticleCount, int nAttributeWriteMask, void *pContext ) const;
  1740. void InitNewParticlesBlock( CParticleCollection *pParticles,
  1741. int start_block, int n_blocks, int nAttributeWriteMask,
  1742. void *pContext ) const
  1743. {
  1744. InitScalarAttributeRandomRangeExpBlock( m_nFieldOutput,
  1745. m_flMin, m_flMax, m_flExponent,
  1746. pParticles, start_block, n_blocks );
  1747. }
  1748. };
  1749. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomScalar, "Scalar Random", OPERATOR_GENERIC );
  1750. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomScalar )
  1751. DMXELEMENT_UNPACK_FIELD( "min", "0", float, m_flMin )
  1752. DMXELEMENT_UNPACK_FIELD( "max", "0", float, m_flMax )
  1753. DMXELEMENT_UNPACK_FIELD( "exponent", "1", float, m_flExponent )
  1754. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "3", int, m_nFieldOutput, "intchoice particlefield_scalar" )
  1755. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomScalar )
  1756. void C_INIT_RandomScalar::InitNewParticlesScalar(
  1757. CParticleCollection *pParticles, int start_p,
  1758. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1759. {
  1760. InitScalarAttributeRandomRangeExpScalar( m_nFieldOutput,
  1761. m_flMin, m_flMax, m_flExponent,
  1762. pParticles, start_p, nParticleCount );
  1763. }
  1764. class C_INIT_RandomVector : public CParticleInitializerOperatorInstance
  1765. {
  1766. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomVector );
  1767. Vector m_vecMin;
  1768. Vector m_vecMax;
  1769. int m_nFieldOutput;
  1770. uint32 GetWrittenAttributes( void ) const
  1771. {
  1772. return 1 << m_nFieldOutput;
  1773. }
  1774. uint32 GetReadAttributes( void ) const
  1775. {
  1776. return 0;
  1777. }
  1778. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  1779. int nParticleCount, int nAttributeWriteMask, void *pContext ) const;
  1780. };
  1781. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomVector, "Vector Random", OPERATOR_GENERIC );
  1782. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomVector )
  1783. DMXELEMENT_UNPACK_FIELD( "min", "0 0 0", Vector, m_vecMin )
  1784. DMXELEMENT_UNPACK_FIELD( "max", "0 0 0", Vector, m_vecMax )
  1785. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "0", int, m_nFieldOutput, "intchoice particlefield_vector" )
  1786. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomVector )
  1787. void C_INIT_RandomVector::InitNewParticlesScalar(
  1788. CParticleCollection *pParticles, int start_p,
  1789. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1790. {
  1791. for( ; nParticleCount--; start_p++ )
  1792. {
  1793. // Setup
  1794. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  1795. Vector vecOutput;
  1796. pParticles->RandomVector( m_vecMin, m_vecMax, &vecOutput );
  1797. SetVectorAttribute( pOutput, vecOutput );
  1798. }
  1799. }
  1800. class C_INIT_RandomVectorComponent : public CParticleInitializerOperatorInstance
  1801. {
  1802. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomVectorComponent );
  1803. float m_flMin;
  1804. float m_flMax;
  1805. int m_nFieldOutput;
  1806. int m_nComponent;
  1807. uint32 GetWrittenAttributes( void ) const
  1808. {
  1809. return 1 << m_nFieldOutput;
  1810. }
  1811. uint32 GetReadAttributes( void ) const
  1812. {
  1813. return 1 << m_nFieldOutput;
  1814. }
  1815. virtual void InitParams( CParticleSystemDefinition *pDef )
  1816. {
  1817. m_nComponent = clamp( m_nComponent, 0, 2 );
  1818. }
  1819. bool InitMultipleOverride ( void ) { return true; }
  1820. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  1821. int nParticleCount, int nAttributeWriteMask, void *pContext ) const;
  1822. };
  1823. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomVectorComponent, "Vector Component Random", OPERATOR_GENERIC );
  1824. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomVectorComponent )
  1825. DMXELEMENT_UNPACK_FIELD( "min", "0", float, m_flMin )
  1826. DMXELEMENT_UNPACK_FIELD( "max", "0", float, m_flMax )
  1827. DMXELEMENT_UNPACK_FIELD( "component 0/1/2 X/Y/Z", "0", int, m_nComponent )
  1828. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "0", int, m_nFieldOutput, "intchoice particlefield_vector" )
  1829. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomVectorComponent )
  1830. void C_INIT_RandomVectorComponent::InitNewParticlesScalar(
  1831. CParticleCollection *pParticles, int start_p,
  1832. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1833. {
  1834. for( ; nParticleCount--; start_p++ )
  1835. {
  1836. const float *pInput = pParticles->GetFloatAttributePtr( m_nFieldOutput, start_p );
  1837. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  1838. Vector vecOutput;
  1839. if ( pInput )
  1840. SetVectorFromAttribute( vecOutput, pInput );
  1841. else
  1842. vecOutput = vec3_origin;
  1843. vecOutput[m_nComponent] = pParticles->RandomFloat( m_flMin, m_flMax );
  1844. SetVectorAttribute( pOutput, vecOutput );
  1845. }
  1846. }
  1847. //-----------------------------------------------------------------------------
  1848. // Random radius
  1849. //-----------------------------------------------------------------------------
  1850. class C_INIT_RandomRadius : public CParticleInitializerOperatorInstance
  1851. {
  1852. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomRadius );
  1853. uint32 GetWrittenAttributes( void ) const
  1854. {
  1855. return PARTICLE_ATTRIBUTE_RADIUS_MASK;
  1856. }
  1857. uint32 GetReadAttributes( void ) const
  1858. {
  1859. return 0;
  1860. }
  1861. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  1862. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1863. {
  1864. InitScalarAttributeRandomRangeExpScalar( PARTICLE_ATTRIBUTE_RADIUS, m_flRadiusMin, m_flRadiusMax,
  1865. m_flRadiusRandExponent, pParticles, start_p, nParticleCount );
  1866. }
  1867. virtual void InitNewParticlesBlock( CParticleCollection *pParticles,
  1868. int start_block, int n_blocks, int nAttributeWriteMask,
  1869. void *pContext ) const
  1870. {
  1871. InitScalarAttributeRandomRangeExpBlock( PARTICLE_ATTRIBUTE_RADIUS,
  1872. m_flRadiusMin, m_flRadiusMax, m_flRadiusRandExponent,
  1873. pParticles, start_block, n_blocks );
  1874. }
  1875. float m_flRadiusMin;
  1876. float m_flRadiusMax;
  1877. float m_flRadiusRandExponent;
  1878. };
  1879. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomRadius, "Radius Random", OPERATOR_PI_RADIUS );
  1880. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomRadius )
  1881. DMXELEMENT_UNPACK_FIELD( "radius_min", "1", float, m_flRadiusMin )
  1882. DMXELEMENT_UNPACK_FIELD( "radius_max", "1", float, m_flRadiusMax )
  1883. DMXELEMENT_UNPACK_FIELD( "radius_random_exponent", "1", float, m_flRadiusRandExponent )
  1884. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomRadius )
  1885. //-----------------------------------------------------------------------------
  1886. // Random alpha
  1887. //-----------------------------------------------------------------------------
  1888. class C_INIT_RandomAlpha : public CParticleInitializerOperatorInstance
  1889. {
  1890. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomAlpha );
  1891. uint32 GetWrittenAttributes( void ) const
  1892. {
  1893. return PARTICLE_ATTRIBUTE_ALPHA_MASK;
  1894. }
  1895. uint32 GetReadAttributes( void ) const
  1896. {
  1897. return 0;
  1898. }
  1899. virtual void InitParams( CParticleSystemDefinition *pDef )
  1900. {
  1901. m_flAlphaMin = m_nAlphaMin / 255.0f;
  1902. m_flAlphaMax = m_nAlphaMax / 255.0f;
  1903. }
  1904. virtual void InitNewParticlesBlock( CParticleCollection *pParticles,
  1905. int start_block, int n_blocks, int nAttributeWriteMask,
  1906. void *pContext ) const
  1907. {
  1908. InitScalarAttributeRandomRangeExpBlock( PARTICLE_ATTRIBUTE_ALPHA,
  1909. m_flAlphaMin, m_flAlphaMax, m_flAlphaRandExponent,
  1910. pParticles, start_block, n_blocks );
  1911. }
  1912. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1913. {
  1914. InitScalarAttributeRandomRangeExpScalar( PARTICLE_ATTRIBUTE_ALPHA,
  1915. m_flAlphaMin, m_flAlphaMax, m_flAlphaRandExponent,
  1916. pParticles, start_p, nParticleCount );
  1917. }
  1918. int m_nAlphaMin;
  1919. int m_nAlphaMax;
  1920. float m_flAlphaMin;
  1921. float m_flAlphaMax;
  1922. float m_flAlphaRandExponent;
  1923. };
  1924. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomAlpha, "Alpha Random", OPERATOR_PI_ALPHA );
  1925. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomAlpha )
  1926. DMXELEMENT_UNPACK_FIELD( "alpha_min", "255", int, m_nAlphaMin )
  1927. DMXELEMENT_UNPACK_FIELD( "alpha_max", "255", int, m_nAlphaMax )
  1928. DMXELEMENT_UNPACK_FIELD( "alpha_random_exponent", "1", float, m_flAlphaRandExponent )
  1929. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomAlpha )
  1930. //-----------------------------------------------------------------------------
  1931. // Random rotation
  1932. //-----------------------------------------------------------------------------
  1933. class CGeneralRandomRotation : public CParticleInitializerOperatorInstance
  1934. {
  1935. protected:
  1936. virtual int GetAttributeToInit( void ) const = 0;
  1937. uint32 GetWrittenAttributes( void ) const
  1938. {
  1939. return (1 << GetAttributeToInit() );
  1940. }
  1941. uint32 GetReadAttributes( void ) const
  1942. {
  1943. return 0;
  1944. }
  1945. virtual void InitParams( CParticleSystemDefinition *pDef )
  1946. {
  1947. m_flRadians = m_flDegrees * ( M_PI / 180.0f );
  1948. m_flRadiansMin = m_flDegreesMin * ( M_PI / 180.0f );
  1949. m_flRadiansMax = m_flDegreesMax * ( M_PI / 180.0f );
  1950. }
  1951. virtual void InitNewParticlesBlock( CParticleCollection *pParticles,
  1952. int start_block, int n_blocks, int nAttributeWriteMask,
  1953. void *pContext ) const
  1954. {
  1955. InitScalarAttributeRandomRangeExpBlock( GetAttributeToInit(),
  1956. m_flRadiansMin, m_flRadiansMax, m_flRotationRandExponent,
  1957. pParticles, start_block, n_blocks, m_bRandomlyFlipDirection );
  1958. }
  1959. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  1960. {
  1961. if ( !m_bRandomlyFlipDirection )
  1962. {
  1963. for( ; nParticleCount--; start_p++ )
  1964. {
  1965. float *pAttr = pParticles->GetFloatAttributePtrForWrite( GetAttributeToInit(), start_p );
  1966. *pAttr = m_flRadians + pParticles->RandomFloatExp( m_flRadiansMin, m_flRadiansMax, m_flRotationRandExponent );
  1967. }
  1968. }
  1969. else
  1970. {
  1971. for( ; nParticleCount--; start_p++ )
  1972. {
  1973. float *pAttr = pParticles->GetFloatAttributePtrForWrite( GetAttributeToInit(), start_p );
  1974. float flRot = m_flRadians + pParticles->RandomFloatExp( m_flRadiansMin, m_flRadiansMax, m_flRotationRandExponent );
  1975. *pAttr = ( pParticles->RandomFloat( -1.0f, 1.0f ) >= 0.0f ) ? +flRot : -flRot;
  1976. }
  1977. }
  1978. }
  1979. // User-specified range
  1980. float m_flDegreesMin;
  1981. float m_flDegreesMax;
  1982. float m_flDegrees;
  1983. // Converted range
  1984. float m_flRadiansMin;
  1985. float m_flRadiansMax;
  1986. float m_flRadians;
  1987. float m_flRotationRandExponent;
  1988. bool m_bRandomlyFlipDirection;
  1989. };
  1990. //-----------------------------------------------------------------------------
  1991. // Random rotation
  1992. //-----------------------------------------------------------------------------
  1993. class C_INIT_RandomRotation : public CGeneralRandomRotation
  1994. {
  1995. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomRotation );
  1996. virtual int GetAttributeToInit( void ) const
  1997. {
  1998. return PARTICLE_ATTRIBUTE_ROTATION;
  1999. }
  2000. };
  2001. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomRotation, "Rotation Random", OPERATOR_PI_ROTATION );
  2002. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomRotation )
  2003. DMXELEMENT_UNPACK_FIELD( "rotation_initial", "0", float, m_flDegrees )
  2004. DMXELEMENT_UNPACK_FIELD( "rotation_offset_min", "0", float, m_flDegreesMin )
  2005. DMXELEMENT_UNPACK_FIELD( "rotation_offset_max", "360", float, m_flDegreesMax )
  2006. DMXELEMENT_UNPACK_FIELD( "rotation_random_exponent", "1", float, m_flRotationRandExponent )
  2007. DMXELEMENT_UNPACK_FIELD( "randomly_flip_direction", "1", bool, m_bRandomlyFlipDirection )
  2008. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomRotation )
  2009. //-----------------------------------------------------------------------------
  2010. // Random rotation speed
  2011. //-----------------------------------------------------------------------------
  2012. class C_INIT_RandomRotationSpeed : public CGeneralRandomRotation
  2013. {
  2014. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomRotationSpeed );
  2015. virtual int GetAttributeToInit( void ) const
  2016. {
  2017. return PARTICLE_ATTRIBUTE_ROTATION_SPEED;
  2018. }
  2019. };
  2020. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomRotationSpeed, "Rotation Speed Random", OPERATOR_GENERIC );
  2021. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomRotationSpeed )
  2022. DMXELEMENT_UNPACK_FIELD( "rotation_speed_constant", "0", float, m_flDegrees )
  2023. DMXELEMENT_UNPACK_FIELD( "rotation_speed_random_min", "0", float, m_flDegreesMin )
  2024. DMXELEMENT_UNPACK_FIELD( "rotation_speed_random_max", "360", float, m_flDegreesMax )
  2025. DMXELEMENT_UNPACK_FIELD( "rotation_speed_random_exponent", "1", float, m_flRotationRandExponent )
  2026. DMXELEMENT_UNPACK_FIELD( "randomly_flip_direction", "1", bool, m_bRandomlyFlipDirection )
  2027. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomRotationSpeed )
  2028. //-----------------------------------------------------------------------------
  2029. // Random yaw
  2030. //-----------------------------------------------------------------------------
  2031. class C_INIT_RandomYaw : public CGeneralRandomRotation
  2032. {
  2033. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomYaw );
  2034. virtual int GetAttributeToInit( void ) const
  2035. {
  2036. return PARTICLE_ATTRIBUTE_YAW;
  2037. }
  2038. };
  2039. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomYaw, "Rotation Yaw Random", OPERATOR_PI_YAW );
  2040. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomYaw )
  2041. DMXELEMENT_UNPACK_FIELD( "yaw_initial", "0", float, m_flDegrees )
  2042. DMXELEMENT_UNPACK_FIELD( "yaw_offset_min", "0", float, m_flDegreesMin )
  2043. DMXELEMENT_UNPACK_FIELD( "yaw_offset_max", "360", float, m_flDegreesMax )
  2044. DMXELEMENT_UNPACK_FIELD( "yaw_random_exponent", "1", float, m_flRotationRandExponent )
  2045. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomYaw )
  2046. static void ComputeLitParticleColor( float *pColorToWrite, const float flBaseColor1[3], const float flBaseColor2[3], float flRandomValue, const Vector &vLightingColor, float flTintFraction, int nBlendMode )
  2047. {
  2048. Vector vLightingTint;
  2049. pColorToWrite[0] = flBaseColor1[0] + ( ( flBaseColor2[0] - flBaseColor1[0] ) * flRandomValue );
  2050. pColorToWrite[4] = flBaseColor1[1] + ( ( flBaseColor2[1] - flBaseColor1[1] ) * flRandomValue );
  2051. pColorToWrite[8] = flBaseColor1[2] + ( ( flBaseColor2[2] - flBaseColor1[2] ) * flRandomValue );
  2052. switch ( nBlendMode )
  2053. {
  2054. case PARTICLEBLEND_DEFAULT:
  2055. {
  2056. vLightingTint = vLightingColor;
  2057. }
  2058. break;
  2059. case PARTICLEBLEND_OVERLAY:
  2060. {
  2061. vLightingTint.x = ( vLightingColor.x < .5 ) ? ( 2 * pColorToWrite[0] * vLightingColor.x ) : ( 1 - ( 2 * ( 1 - pColorToWrite[0] ) * ( 1 - vLightingColor.x ) ) );
  2062. vLightingTint.y = ( vLightingColor.y < .5 ) ? ( 2 * pColorToWrite[4] * vLightingColor.y ) : ( 1 - ( 2 * ( 1 - pColorToWrite[4] ) * ( 1 - vLightingColor.y ) ) );
  2063. vLightingTint.z = ( vLightingColor.z < .5 ) ? ( 2 * pColorToWrite[8] * vLightingColor.z ) : ( 1 - ( 2 * ( 1 - pColorToWrite[8] ) * ( 1 - vLightingColor.z ) ) );
  2064. }
  2065. break;
  2066. case PARTICLEBLEND_DARKEN:
  2067. {
  2068. vLightingTint.x = MIN( pColorToWrite[0], vLightingColor.x );
  2069. vLightingTint.y = MIN( pColorToWrite[4], vLightingColor.y );
  2070. vLightingTint.z = MIN( pColorToWrite[8], vLightingColor.z );
  2071. }
  2072. break;
  2073. case PARTICLEBLEND_LIGHTEN:
  2074. {
  2075. vLightingTint.x = MAX( pColorToWrite[0], vLightingColor.x );
  2076. vLightingTint.y = MAX( pColorToWrite[4], vLightingColor.y );
  2077. vLightingTint.z = MAX( pColorToWrite[8], vLightingColor.z );
  2078. }
  2079. break;
  2080. case PARTICLEBLEND_MULTIPLY:
  2081. {
  2082. vLightingTint.x = pColorToWrite[0] * vLightingColor.x;
  2083. vLightingTint.y = pColorToWrite[4] * vLightingColor.y;
  2084. vLightingTint.z = pColorToWrite[8] * vLightingColor.z;
  2085. }
  2086. break;
  2087. }
  2088. pColorToWrite[0] = Lerp( flTintFraction, pColorToWrite[0], vLightingTint.x );
  2089. pColorToWrite[4] = Lerp( flTintFraction, pColorToWrite[4], vLightingTint.y );
  2090. pColorToWrite[8] = Lerp( flTintFraction, pColorToWrite[8], vLightingTint.z );
  2091. pColorToWrite[0] = MIN( pColorToWrite[0], 1.0f );
  2092. pColorToWrite[4] = MIN( pColorToWrite[4], 1.0f );
  2093. pColorToWrite[8] = MIN( pColorToWrite[8], 1.0f );
  2094. }
  2095. //-----------------------------------------------------------------------------
  2096. // Random color
  2097. //-----------------------------------------------------------------------------
  2098. class C_INIT_RandomColor : public CParticleInitializerOperatorInstance
  2099. {
  2100. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomColor );
  2101. uint32 GetWrittenAttributes( void ) const
  2102. {
  2103. return ( 1 << m_nFieldOutput );
  2104. }
  2105. uint32 GetReadAttributes( void ) const
  2106. {
  2107. return 0;
  2108. }
  2109. struct C_OP_RandomColorContext_t
  2110. {
  2111. Vector m_vPrevPosition;
  2112. };
  2113. size_t GetRequiredContextBytes( void ) const
  2114. {
  2115. return sizeof( C_OP_RandomColorContext_t );
  2116. }
  2117. virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
  2118. {
  2119. C_OP_RandomColorContext_t *pCtx=reinterpret_cast<C_OP_RandomColorContext_t *>( pContext );
  2120. pCtx->m_vPrevPosition = vec3_origin;
  2121. }
  2122. virtual void InitParams( CParticleSystemDefinition *pDef )
  2123. {
  2124. m_flNormColorMin[0] = ( float )m_ColorMin[0] / 255.0f;
  2125. m_flNormColorMin[1] = ( float )m_ColorMin[1] / 255.0f;
  2126. m_flNormColorMin[2] = ( float )m_ColorMin[2] / 255.0f;
  2127. m_flNormColorMax[0] = ( float )m_ColorMax[0] / 255.0f;
  2128. m_flNormColorMax[1] = ( float )m_ColorMax[1] / 255.0f;
  2129. m_flNormColorMax[2] = ( float )m_ColorMax[2] / 255.0f;
  2130. }
  2131. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  2132. {
  2133. C_OP_RandomColorContext_t *pCtx=reinterpret_cast<C_OP_RandomColorContext_t *>( pContext );
  2134. Color tint( 255, 255, 255, 255 );
  2135. float randomPerc;
  2136. float *pColor;
  2137. // If we're factoring in luminosity or tint, then get our lighting info for this position
  2138. if ( m_flTintPerc )
  2139. {
  2140. if ( pParticles->m_pParent && pParticles->m_pParent->m_LocalLightingCP == m_nTintCP )
  2141. {
  2142. tint = pParticles->m_pParent->m_LocalLighting;
  2143. }
  2144. else
  2145. {
  2146. // Get our control point
  2147. Vector vecOrigin;
  2148. pParticles->GetControlPointAtTime( m_nTintCP, pParticles->m_flCurTime, &vecOrigin );
  2149. if ( ( ( pCtx->m_vPrevPosition - vecOrigin ).Length() >= m_flUpdateThreshold ) || ( pParticles->m_LocalLightingCP == -1 ) )
  2150. {
  2151. g_pParticleSystemMgr->Query()->GetLightingAtPoint( vecOrigin, tint );
  2152. pParticles->m_LocalLighting = tint;
  2153. pParticles->m_LocalLightingCP = m_nTintCP;
  2154. pCtx->m_vPrevPosition = vecOrigin;
  2155. }
  2156. else
  2157. tint = pParticles->m_LocalLighting;
  2158. }
  2159. tint[0] = MAX( m_TintMin[0], MIN( tint[0], m_TintMax[0] ) );
  2160. tint[1] = MAX( m_TintMin[1], MIN( tint[1], m_TintMax[1] ) );
  2161. tint[2] = MAX( m_TintMin[2], MIN( tint[2], m_TintMax[2] ) );
  2162. Vector vecLightingColor = Vector ( tint[0], tint[1], tint[2] );
  2163. vecLightingColor *= m_flLightAmplification / 255.0f;
  2164. for ( ; nParticleCount--; start_p++ )
  2165. {
  2166. pColor = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  2167. float flRandomFraction = pParticles->RandomFloat( 0.0f, 1.0f );
  2168. ComputeLitParticleColor( pColor, m_flNormColorMin, m_flNormColorMax, flRandomFraction, vecLightingColor, m_flTintPerc, m_nTintBlendMode );
  2169. }
  2170. }
  2171. else // If we don't tint, keep it simple.
  2172. {
  2173. for( ; nParticleCount--; start_p++ )
  2174. {
  2175. // Setup
  2176. pColor = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  2177. randomPerc = pParticles->RandomFloat( 0.0f, 1.0f );
  2178. // No Blend
  2179. // No Tint
  2180. // Randomly choose a range between the two colors
  2181. pColor[0] = m_flNormColorMin[0] + ( ( m_flNormColorMax[0] - m_flNormColorMin[0] ) * randomPerc );
  2182. pColor[4] = m_flNormColorMin[1] + ( ( m_flNormColorMax[1] - m_flNormColorMin[1] ) * randomPerc );
  2183. pColor[8] = m_flNormColorMin[2] + ( ( m_flNormColorMax[2] - m_flNormColorMin[2] ) * randomPerc );
  2184. }
  2185. }
  2186. }
  2187. virtual void InitNewParticlesBlock( CParticleCollection *pParticles,
  2188. int start_block, int n_blocks, int nAttributeWriteMask,
  2189. void *pContext ) const
  2190. {
  2191. C_OP_RandomColorContext_t *pCtx=reinterpret_cast<C_OP_RandomColorContext_t *>( pContext );
  2192. Color tint( 255, 255, 255, 255 );
  2193. size_t attr_stride;
  2194. FourVectors *pColor = pParticles->Get4VAttributePtrForWrite( m_nFieldOutput, &attr_stride );
  2195. pColor += attr_stride * start_block;
  2196. FourVectors fvColorMin;
  2197. fvColorMin.DuplicateVector( Vector (m_flNormColorMin[0], m_flNormColorMin[1], m_flNormColorMin[2] ) );
  2198. FourVectors fvColorWidth;
  2199. fvColorWidth.DuplicateVector( Vector (m_flNormColorMax[0] - m_flNormColorMin[0], m_flNormColorMax[1] - m_flNormColorMin[1], m_flNormColorMax[2] - m_flNormColorMin[2] ) );
  2200. int nRandContext = GetSIMDRandContext();
  2201. // If we're factoring in luminosity or tint, then get our lighting info for this position
  2202. if ( m_flTintPerc )
  2203. {
  2204. if ( pParticles->m_pParent && pParticles->m_pParent->m_LocalLightingCP == m_nTintCP )
  2205. {
  2206. tint = pParticles->m_pParent->m_LocalLighting;
  2207. }
  2208. else
  2209. {
  2210. // FIXME: Really, we want the emission point for each particle, but for now, we do it more cheaply
  2211. // Get our control point
  2212. Vector vecOrigin;
  2213. pParticles->GetControlPointAtTime( m_nTintCP, pParticles->m_flCurTime, &vecOrigin );
  2214. if ( ( ( pCtx->m_vPrevPosition - vecOrigin ).Length() >= m_flUpdateThreshold ) || ( pParticles->m_LocalLightingCP == -1 ) )
  2215. {
  2216. g_pParticleSystemMgr->Query()->GetLightingAtPoint( vecOrigin, tint );
  2217. pParticles->m_LocalLighting = tint;
  2218. pParticles->m_LocalLightingCP = m_nTintCP;
  2219. pCtx->m_vPrevPosition = vecOrigin;
  2220. }
  2221. else
  2222. tint = pParticles->m_LocalLighting;
  2223. }
  2224. tint[0] = MAX( m_TintMin[0], MIN( tint[0], m_TintMax[0] ) );
  2225. tint[1] = MAX( m_TintMin[1], MIN( tint[1], m_TintMax[1] ) );
  2226. tint[2] = MAX( m_TintMin[2], MIN( tint[2], m_TintMax[2] ) );
  2227. FourVectors fvTint;
  2228. fvTint.DuplicateVector( Vector ( tint[0], tint[1], tint[2] ) );
  2229. fltx4 fl4Divisor = ReplicateX4( m_flLightAmplification / 255.0f );
  2230. fvTint *= fl4Divisor;
  2231. fltx4 fl4TintPrc = ReplicateX4( m_flTintPerc );
  2232. // A little ugly, but we keep the switch outside the loop
  2233. switch( m_nTintBlendMode )
  2234. {
  2235. case PARTICLEBLEND_DEFAULT:
  2236. {
  2237. while( n_blocks-- )
  2238. {
  2239. FourVectors fvColor = fvColorWidth;
  2240. FourVectors fvColor2 = fvTint;
  2241. fvColor *= RandSIMD( nRandContext );
  2242. fvColor += fvColorMin;
  2243. //Do Tint
  2244. fvColor2 -= fvColor;
  2245. fvColor2 *= fl4TintPrc;
  2246. fvColor2 += fvColor;
  2247. *pColor = fvColor2;
  2248. // Clamp color components to 1.0
  2249. pColor->x = MinSIMD( pColor->x, Four_Ones );
  2250. pColor->y = MinSIMD( pColor->y, Four_Ones );
  2251. pColor->z = MinSIMD( pColor->z, Four_Ones );
  2252. pColor += attr_stride;
  2253. }
  2254. break;
  2255. }
  2256. case PARTICLEBLEND_OVERLAY:
  2257. {
  2258. while( n_blocks-- )
  2259. {
  2260. FourVectors fvColor = fvColorWidth;
  2261. FourVectors fvColor2 = fvTint;
  2262. fvColor *= RandSIMD( nRandContext );
  2263. fvColor += fvColorMin;
  2264. //Do Blend
  2265. bi32x4 fl4BlendMaskRed = CmpLtSIMD( fvTint.x, Four_PointFives );
  2266. bi32x4 fl4BlendMaskGreen = CmpLtSIMD( fvTint.y, Four_PointFives );
  2267. bi32x4 fl4BlendMaskBlue = CmpLtSIMD( fvTint.z, Four_PointFives );
  2268. FourVectors fvBlend1;
  2269. fvBlend1.x = MulSIMD( Four_Twos, MulSIMD( fvColor.x, fvColor2.x ) );
  2270. fvBlend1.y = MulSIMD( Four_Twos, MulSIMD( fvColor.y, fvColor2.y ) );
  2271. fvBlend1.z = MulSIMD( Four_Twos, MulSIMD( fvColor.z, fvColor2.z ) );
  2272. FourVectors fvBlend2;
  2273. fvBlend2.x = SubSIMD( Four_Ones, MulSIMD( Four_Twos, MulSIMD( SubSIMD( Four_Ones, fvColor.x), SubSIMD( Four_Ones, fvColor2.x ) ) ) );
  2274. fvBlend2.y = SubSIMD( Four_Ones, MulSIMD( Four_Twos, MulSIMD( SubSIMD( Four_Ones, fvColor.y), SubSIMD( Four_Ones, fvColor2.y ) ) ) );
  2275. fvBlend2.z = SubSIMD( Four_Ones, MulSIMD( Four_Twos, MulSIMD( SubSIMD( Four_Ones, fvColor.z), SubSIMD( Four_Ones, fvColor2.z ) ) ) );
  2276. fvColor2 = fvBlend2;
  2277. fvColor2.x = MaskedAssign( fl4BlendMaskRed, fvBlend1.x, fvColor2.x );
  2278. fvColor2.y = MaskedAssign( fl4BlendMaskGreen, fvBlend1.y, fvColor2.y );
  2279. fvColor2.z = MaskedAssign( fl4BlendMaskBlue, fvBlend1.z, fvColor2.z );
  2280. //Do Tint
  2281. fvColor2 -= fvColor;
  2282. fvColor2 *= fl4TintPrc;
  2283. fvColor2 += fvColor;
  2284. *pColor = fvColor2;
  2285. // Clamp color components to 1.0
  2286. pColor->x = MinSIMD( pColor->x, Four_Ones );
  2287. pColor->y = MinSIMD( pColor->y, Four_Ones );
  2288. pColor->z = MinSIMD( pColor->z, Four_Ones );
  2289. pColor += attr_stride;
  2290. }
  2291. break;
  2292. }
  2293. case PARTICLEBLEND_DARKEN:
  2294. {
  2295. while( n_blocks-- )
  2296. {
  2297. FourVectors fvColor = fvColorWidth;
  2298. FourVectors fvColor2 = fvTint;
  2299. fvColor *= RandSIMD( nRandContext );
  2300. fvColor += fvColorMin;
  2301. //Do Blend
  2302. fvColor2.x = MinSIMD( fvColor.x, fvColor2.x );
  2303. fvColor2.y = MinSIMD( fvColor.y, fvColor2.y );
  2304. fvColor2.z = MinSIMD( fvColor.z, fvColor2.z );
  2305. //Do Tint
  2306. fvColor2 -= fvColor;
  2307. fvColor2 *= fl4TintPrc;
  2308. fvColor2 += fvColor;
  2309. *pColor = fvColor2;
  2310. // Clamp color components to 1.0
  2311. pColor->x = MinSIMD( pColor->x, Four_Ones );
  2312. pColor->y = MinSIMD( pColor->y, Four_Ones );
  2313. pColor->z = MinSIMD( pColor->z, Four_Ones );
  2314. pColor += attr_stride;
  2315. }
  2316. break;
  2317. }
  2318. case PARTICLEBLEND_LIGHTEN:
  2319. {
  2320. while( n_blocks-- )
  2321. {
  2322. FourVectors fvColor = fvColorWidth;
  2323. FourVectors fvColor2 = fvTint;
  2324. fvColor *= RandSIMD( nRandContext );
  2325. fvColor += fvColorMin;
  2326. //Do Blend
  2327. fvColor2.x = MaxSIMD( fvColor.x, fvColor2.x );
  2328. fvColor2.y = MaxSIMD( fvColor.y, fvColor2.y );
  2329. fvColor2.z = MaxSIMD( fvColor.z, fvColor2.z );
  2330. //Do Tint
  2331. fvColor2 -= fvColor;
  2332. fvColor2 *= fl4TintPrc;
  2333. fvColor2 += fvColor;
  2334. *pColor = fvColor2;
  2335. // Clamp color components to 1.0
  2336. pColor->x = MinSIMD( pColor->x, Four_Ones );
  2337. pColor->y = MinSIMD( pColor->y, Four_Ones );
  2338. pColor->z = MinSIMD( pColor->z, Four_Ones );
  2339. pColor += attr_stride;
  2340. }
  2341. break;
  2342. }
  2343. case PARTICLEBLEND_MULTIPLY:
  2344. {
  2345. while( n_blocks-- )
  2346. {
  2347. FourVectors fvColor = fvColorWidth;
  2348. FourVectors fvColor2 = fvTint;
  2349. fvColor *= RandSIMD( nRandContext );
  2350. fvColor += fvColorMin;
  2351. //Do Modulate
  2352. fvColor2 *= fvColor;
  2353. //Do Tint
  2354. fvColor2 -= fvColor;
  2355. fvColor2 *= fl4TintPrc;
  2356. fvColor2 += fvColor;
  2357. *pColor = fvColor2;
  2358. // Clamp color components to 1.0
  2359. pColor->x = MinSIMD( pColor->x, Four_Ones );
  2360. pColor->y = MinSIMD( pColor->y, Four_Ones );
  2361. pColor->z = MinSIMD( pColor->z, Four_Ones );
  2362. pColor += attr_stride;
  2363. }
  2364. break;
  2365. }
  2366. }
  2367. }
  2368. else
  2369. {
  2370. while( n_blocks-- )
  2371. {
  2372. FourVectors fvColor = fvColorWidth;
  2373. fvColor *= RandSIMD( nRandContext );
  2374. fvColor += fvColorMin;
  2375. *pColor = fvColor;
  2376. pColor += attr_stride;
  2377. }
  2378. }
  2379. ReleaseSIMDRandContext( nRandContext );
  2380. }
  2381. virtual uint64 GetReadControlPointMask() const
  2382. {
  2383. return 1ULL << m_nTintCP;
  2384. }
  2385. float m_flNormColorMin[3];
  2386. float m_flNormColorMax[3];
  2387. Color m_ColorMin;
  2388. Color m_ColorMax;
  2389. Color m_TintMin;
  2390. Color m_TintMax;
  2391. float m_flTintPerc;
  2392. float m_flUpdateThreshold;
  2393. int m_nTintCP;
  2394. int m_nFieldOutput;
  2395. int m_nTintBlendMode;
  2396. float m_flLightAmplification;
  2397. };
  2398. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomColor, "Color Random", OPERATOR_GENERIC );
  2399. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomColor )
  2400. DMXELEMENT_UNPACK_FIELD( "color1", "255 255 255 255", Color, m_ColorMin )
  2401. DMXELEMENT_UNPACK_FIELD( "color2", "255 255 255 255", Color, m_ColorMax )
  2402. DMXELEMENT_UNPACK_FIELD( "tint_perc", "0.0", float, m_flTintPerc )
  2403. DMXELEMENT_UNPACK_FIELD( "tint control point", "0", int, m_nTintCP )
  2404. DMXELEMENT_UNPACK_FIELD( "tint clamp min", "0 0 0 0", Color, m_TintMin )
  2405. DMXELEMENT_UNPACK_FIELD( "tint clamp max", "255 255 255 255", Color, m_TintMax )
  2406. DMXELEMENT_UNPACK_FIELD( "tint update movement threshold", "32", float, m_flUpdateThreshold )
  2407. DMXELEMENT_UNPACK_FIELD( "tint blend mode", "0", int, m_nTintBlendMode )
  2408. DMXELEMENT_UNPACK_FIELD( "light amplification amount", "1", float, m_flLightAmplification )
  2409. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "6", int, m_nFieldOutput, "intchoice particlefield_vector" )
  2410. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomColor )
  2411. //-----------------------------------------------------------------------------
  2412. // Random color
  2413. //-----------------------------------------------------------------------------
  2414. class C_INIT_ColorLitPerParticle : public CParticleInitializerOperatorInstance
  2415. {
  2416. DECLARE_PARTICLE_OPERATOR( C_INIT_ColorLitPerParticle );
  2417. uint32 GetWrittenAttributes( void ) const
  2418. {
  2419. return PARTICLE_ATTRIBUTE_TINT_RGB_MASK;
  2420. }
  2421. uint32 GetReadAttributes( void ) const
  2422. {
  2423. return PARTICLE_ATTRIBUTE_XYZ_MASK;
  2424. }
  2425. virtual void InitParams( CParticleSystemDefinition *pDef )
  2426. {
  2427. m_flNormColorMin[0] = ( float )m_ColorMin[0] / 255.0f;
  2428. m_flNormColorMin[1] = ( float )m_ColorMin[1] / 255.0f;
  2429. m_flNormColorMin[2] = ( float )m_ColorMin[2] / 255.0f;
  2430. m_flNormColorMax[0] = ( float )m_ColorMax[0] / 255.0f;
  2431. m_flNormColorMax[1] = ( float )m_ColorMax[1] / 255.0f;
  2432. m_flNormColorMax[2] = ( float )m_ColorMax[2] / 255.0f;
  2433. }
  2434. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  2435. {
  2436. Color tint( 255, 255, 255, 255 );
  2437. float *pColor;
  2438. float *pXYZ;
  2439. for( ; nParticleCount--; start_p++ )
  2440. {
  2441. pColor = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_TINT_RGB, start_p );
  2442. pXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  2443. Vector vecOrigin;
  2444. SetVectorFromAttribute( vecOrigin, pXYZ );
  2445. g_pParticleSystemMgr->Query()->GetLightingAtPoint( vecOrigin, tint );
  2446. tint[0] = MAX( m_TintMin[0], MIN( tint[0], m_TintMax[0] ) );
  2447. tint[1] = MAX( m_TintMin[1], MIN( tint[1], m_TintMax[1] ) );
  2448. tint[2] = MAX( m_TintMin[2], MIN( tint[2], m_TintMax[2] ) );
  2449. Vector vecLightingColor = Vector ( tint[0], tint[1], tint[2] );
  2450. vecLightingColor *= m_flLightAmplification / 255.0f;
  2451. float flRandomFraction = pParticles->RandomFloat( 0.0f, 1.0f );
  2452. ComputeLitParticleColor( pColor, m_flNormColorMin, m_flNormColorMax, flRandomFraction, vecLightingColor, m_flTintPerc, m_nTintBlendMode );
  2453. }
  2454. }
  2455. float m_flNormColorMin[3];
  2456. float m_flNormColorMax[3];
  2457. Color m_ColorMin;
  2458. Color m_ColorMax;
  2459. Color m_TintMin;
  2460. Color m_TintMax;
  2461. float m_flTintPerc;
  2462. int m_nTintBlendMode;
  2463. float m_flLightAmplification;
  2464. };
  2465. DEFINE_PARTICLE_OPERATOR( C_INIT_ColorLitPerParticle, "Color Lit Per Particle", OPERATOR_PI_TINT_RGB );
  2466. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_ColorLitPerParticle )
  2467. DMXELEMENT_UNPACK_FIELD( "color1", "255 255 255 255", Color, m_ColorMin )
  2468. DMXELEMENT_UNPACK_FIELD( "color2", "255 255 255 255", Color, m_ColorMax )
  2469. DMXELEMENT_UNPACK_FIELD( "light bias", "0.0", float, m_flTintPerc )
  2470. DMXELEMENT_UNPACK_FIELD( "tint clamp min", "0 0 0 0", Color, m_TintMin )
  2471. DMXELEMENT_UNPACK_FIELD( "tint clamp max", "255 255 255 255", Color, m_TintMax )
  2472. DMXELEMENT_UNPACK_FIELD( "tint blend mode", "0", int, m_nTintBlendMode )
  2473. DMXELEMENT_UNPACK_FIELD( "light amplification amount", "1", float, m_flLightAmplification )
  2474. END_PARTICLE_OPERATOR_UNPACK( C_INIT_ColorLitPerParticle )
  2475. //-----------------------------------------------------------------------------
  2476. // Trail Length
  2477. //-----------------------------------------------------------------------------
  2478. class C_INIT_RandomTrailLength : public CParticleInitializerOperatorInstance
  2479. {
  2480. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomTrailLength );
  2481. uint32 GetWrittenAttributes( void ) const
  2482. {
  2483. return PARTICLE_ATTRIBUTE_TRAIL_LENGTH_MASK;
  2484. }
  2485. uint32 GetReadAttributes( void ) const
  2486. {
  2487. return 0;
  2488. }
  2489. virtual void InitParams( CParticleSystemDefinition *pDef )
  2490. {
  2491. }
  2492. virtual void InitNewParticlesBlock( CParticleCollection *pParticles,
  2493. int start_block, int n_blocks, int nAttributeWriteMask,
  2494. void *pContext ) const
  2495. {
  2496. InitScalarAttributeRandomRangeExpBlock( PARTICLE_ATTRIBUTE_TRAIL_LENGTH,
  2497. m_flMinLength, m_flMaxLength, m_flLengthRandExponent,
  2498. pParticles, start_block, n_blocks );
  2499. }
  2500. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  2501. {
  2502. InitScalarAttributeRandomRangeExpScalar( PARTICLE_ATTRIBUTE_TRAIL_LENGTH,
  2503. m_flMinLength, m_flMaxLength, m_flLengthRandExponent,
  2504. pParticles, start_p, nParticleCount );
  2505. }
  2506. float m_flMinLength;
  2507. float m_flMaxLength;
  2508. float m_flLengthRandExponent;
  2509. };
  2510. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomTrailLength, "Trail Length Random", OPERATOR_GENERIC );
  2511. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomTrailLength )
  2512. DMXELEMENT_UNPACK_FIELD( "length_min", "0.1", float, m_flMinLength )
  2513. DMXELEMENT_UNPACK_FIELD( "length_max", "0.1", float, m_flMaxLength )
  2514. DMXELEMENT_UNPACK_FIELD( "length_random_exponent", "1", float, m_flLengthRandExponent )
  2515. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomTrailLength )
  2516. //-----------------------------------------------------------------------------
  2517. // Random sequence
  2518. //-----------------------------------------------------------------------------
  2519. class C_INIT_RandomSequence : public CParticleInitializerOperatorInstance
  2520. {
  2521. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomSequence );
  2522. uint32 GetWrittenAttributes( void ) const
  2523. {
  2524. return PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER_MASK;
  2525. }
  2526. uint32 GetReadAttributes( void ) const
  2527. {
  2528. return 0;
  2529. }
  2530. virtual void InitParams( CParticleSystemDefinition *pDef )
  2531. {
  2532. }
  2533. struct SequenceContext_t
  2534. {
  2535. int nCurrent;
  2536. int nList[64];
  2537. };
  2538. virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
  2539. {
  2540. if ( m_bShuffle || m_bLinear )
  2541. {
  2542. SequenceContext_t *pCtx = reinterpret_cast<SequenceContext_t *>( pContext );
  2543. pCtx->nCurrent = m_nSequenceMin;
  2544. for ( int i = m_nSequenceMin; i <= m_nSequenceMax; i++ )
  2545. {
  2546. pCtx->nList[i] = i;
  2547. }
  2548. if ( m_bShuffle )
  2549. {
  2550. for ( int i = m_nSequenceMax; i > m_nSequenceMin; i-- )
  2551. {
  2552. int nShuffle = pParticles->RandomInt( m_nSequenceMin, i );
  2553. int nTemp = pCtx->nList[i];
  2554. pCtx->nList[i] = pCtx->nList[nShuffle];
  2555. pCtx->nList[nShuffle] = nTemp;
  2556. }
  2557. }
  2558. }
  2559. }
  2560. size_t GetRequiredContextBytes( void ) const
  2561. {
  2562. return sizeof( SequenceContext_t );
  2563. }
  2564. /*
  2565. virtual void InitNewParticlesBlock( CParticleCollection *pParticles,
  2566. int start_block, int n_blocks, int nAttributeWriteMask,
  2567. void *pContext ) const
  2568. {
  2569. InitScalarAttributeRandomRangeBlock( PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER,
  2570. m_nSequenceMin, m_nSequenceMax,
  2571. pParticles, start_block, n_blocks );
  2572. }
  2573. */
  2574. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  2575. {
  2576. float *pSequence;
  2577. if ( m_bShuffle || m_bLinear )
  2578. {
  2579. SequenceContext_t *pCtx = reinterpret_cast<SequenceContext_t *>( pContext );
  2580. for( ; nParticleCount--; start_p++ )
  2581. {
  2582. if ( pCtx->nCurrent > m_nSequenceMax )
  2583. {
  2584. if ( m_bShuffle )
  2585. {
  2586. for ( int i = m_nSequenceMax; i > m_nSequenceMin; i-- )
  2587. {
  2588. int nShuffle = pParticles->RandomInt( m_nSequenceMin, i );
  2589. int nTemp = pCtx->nList[i];
  2590. pCtx->nList[i] = pCtx->nList[nShuffle];
  2591. pCtx->nList[nShuffle] = nTemp;
  2592. }
  2593. }
  2594. pCtx->nCurrent = m_nSequenceMin;
  2595. }
  2596. pSequence = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER, start_p );
  2597. *pSequence = pCtx->nList[pCtx->nCurrent];
  2598. pCtx->nCurrent++;
  2599. }
  2600. }
  2601. else
  2602. {
  2603. for( ; nParticleCount--; start_p++ )
  2604. {
  2605. pSequence = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER, start_p );
  2606. *pSequence = pParticles->RandomInt( m_nSequenceMin, m_nSequenceMax );
  2607. }
  2608. }
  2609. }
  2610. int m_nSequenceMin;
  2611. int m_nSequenceMax;
  2612. bool m_bShuffle;
  2613. bool m_bLinear;
  2614. };
  2615. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomSequence, "Sequence Random", OPERATOR_GENERIC );
  2616. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomSequence )
  2617. DMXELEMENT_UNPACK_FIELD_USERDATA( "sequence_min", "0", int, m_nSequenceMin, "sheetsequencepicker" )
  2618. DMXELEMENT_UNPACK_FIELD_USERDATA( "sequence_max", "0", int, m_nSequenceMax, "sheetsequencepicker" )
  2619. DMXELEMENT_UNPACK_FIELD( "shuffle", "0", bool, m_bShuffle )
  2620. DMXELEMENT_UNPACK_FIELD( "linear", "0", bool, m_bLinear )
  2621. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomSequence )
  2622. //-----------------------------------------------------------------------------
  2623. // Random sequence
  2624. //-----------------------------------------------------------------------------
  2625. class C_INIT_SequenceFromCP : public CParticleInitializerOperatorInstance
  2626. {
  2627. DECLARE_PARTICLE_OPERATOR( C_INIT_SequenceFromCP );
  2628. bool m_bKillUnused;
  2629. bool m_bRadiusScale;
  2630. int m_nCP;
  2631. Vector m_vecOffset;
  2632. uint32 GetWrittenAttributes( void ) const
  2633. {
  2634. int ret = PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER_MASK;
  2635. if ( m_vecOffset != vec3_origin )
  2636. ret |= PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  2637. if ( m_bKillUnused == true )
  2638. ret |= PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
  2639. return ret;
  2640. }
  2641. uint32 GetReadAttributes( void ) const
  2642. {
  2643. if ( m_vecOffset != vec3_origin )
  2644. return PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_RADIUS_MASK;
  2645. else
  2646. return 0;
  2647. }
  2648. virtual uint64 GetReadControlPointMask() const
  2649. {
  2650. return 1ULL << m_nCP;
  2651. }
  2652. bool InitMultipleOverride ( void ) { return true; }
  2653. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  2654. int nParticleCount, int nAttributeWriteMask,
  2655. void *pContext) const;
  2656. };
  2657. DEFINE_PARTICLE_OPERATOR( C_INIT_SequenceFromCP, "Sequence From Control Point", OPERATOR_GENERIC );
  2658. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_SequenceFromCP )
  2659. DMXELEMENT_UNPACK_FIELD( "control point", "1", int, m_nCP )
  2660. DMXELEMENT_UNPACK_FIELD( "per particle spatial offset", "0 0 0", Vector, m_vecOffset )
  2661. DMXELEMENT_UNPACK_FIELD( "offset propotional to radius", "0", bool, m_bRadiusScale )
  2662. END_PARTICLE_OPERATOR_UNPACK( C_INIT_SequenceFromCP )
  2663. void C_INIT_SequenceFromCP::InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  2664. {
  2665. float *pSequence;
  2666. Vector vecCP = pParticles->GetControlPointAtCurrentTime( m_nCP );
  2667. int nComponent = 2;
  2668. int nDesiredCount = MAX ( 0, int ( log10( vecCP.x * 10 ) ) );
  2669. nDesiredCount += MAX ( 0, int ( log10( vecCP.y * 10 ) ) );
  2670. nDesiredCount += MAX ( 0, int ( log10( vecCP.z * 10 ) ) );
  2671. nDesiredCount = MIN( nDesiredCount, nParticleCount ) - 1;
  2672. Vector vecOffsetPos = ( m_vecOffset * nDesiredCount ) / 2;
  2673. for( ; nParticleCount--; start_p++ )
  2674. {
  2675. while ( vecCP[nComponent] < 1 && nComponent >= 0 )
  2676. nComponent--;
  2677. if ( vecCP[nComponent] > 0 && nComponent >= 0 )
  2678. {
  2679. pSequence = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER, start_p );
  2680. *pSequence = fmod( vecCP[nComponent], 10 ) + ( 10 * nComponent );
  2681. vecCP[nComponent] = floor( vecCP[nComponent] / 10 );
  2682. MAX ( vecCP[nComponent], 0 );
  2683. if ( m_vecOffset != vec3_origin )
  2684. {
  2685. float flRadius = 1;
  2686. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  2687. float *xyzprev = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  2688. if ( m_bRadiusScale )
  2689. {
  2690. const float *rad = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_RADIUS, start_p );
  2691. flRadius = *rad;
  2692. }
  2693. Vector vecXYZ, vecXYZPrev;
  2694. SetVectorFromAttribute( vecXYZ, xyz );
  2695. SetVectorFromAttribute( vecXYZPrev, xyzprev );
  2696. vecXYZ += vecOffsetPos * flRadius;
  2697. vecXYZPrev += vecOffsetPos * flRadius;
  2698. SetVectorAttribute( xyz, vecXYZ );
  2699. SetVectorAttribute( xyzprev, vecXYZPrev);
  2700. vecOffsetPos -= m_vecOffset;
  2701. }
  2702. }
  2703. else
  2704. {
  2705. float *pLifeDuration = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
  2706. *pLifeDuration = -1;
  2707. }
  2708. }
  2709. };
  2710. //-----------------------------------------------------------------------------
  2711. // Position Warp Initializer
  2712. // Scales initial position and velocity of particles within a random vector range
  2713. //-----------------------------------------------------------------------------
  2714. class C_INIT_PositionWarp : public CParticleInitializerOperatorInstance
  2715. {
  2716. DECLARE_PARTICLE_OPERATOR( C_INIT_PositionOffset );
  2717. Vector m_vecWarpMin;
  2718. Vector m_vecWarpMax;
  2719. int m_nControlPointNumber;
  2720. float m_flWarpTime, m_flWarpStartTime;
  2721. bool m_bInvertWarp;
  2722. bool m_bUseCount;
  2723. struct PositionWarpContext_t
  2724. {
  2725. float m_flStartTime;
  2726. int m_nStartCount;
  2727. unsigned int m_nCurrentCount;
  2728. };
  2729. uint32 GetWrittenAttributes( void ) const
  2730. {
  2731. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  2732. }
  2733. uint32 GetReadAttributes( void ) const
  2734. {
  2735. return PARTICLE_ATTRIBUTE_CREATION_TIME;
  2736. }
  2737. virtual uint64 GetReadControlPointMask() const
  2738. {
  2739. return 1ULL << m_nControlPointNumber;
  2740. }
  2741. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  2742. int nParticleCount, int nAttributeWriteMask,
  2743. void *pContext) const;
  2744. virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
  2745. {
  2746. PositionWarpContext_t *pCtx = reinterpret_cast<PositionWarpContext_t *>( pContext );
  2747. pCtx->m_flStartTime = pParticles->m_flCurTime;
  2748. pCtx->m_nStartCount = 0;
  2749. pCtx->m_nCurrentCount = 0;
  2750. }
  2751. size_t GetRequiredContextBytes( void ) const
  2752. {
  2753. return sizeof( PositionWarpContext_t );
  2754. }
  2755. virtual void Restart( CParticleCollection *pParticles, void *pContext )
  2756. {
  2757. PositionWarpContext_t *pCtx = reinterpret_cast<PositionWarpContext_t *>( pContext );
  2758. pCtx->m_flStartTime = pParticles->m_flCurTime;
  2759. pCtx->m_nStartCount = 0;
  2760. pCtx->m_nCurrentCount = 0;
  2761. }
  2762. void InitParams( CParticleSystemDefinition *pDef )
  2763. {
  2764. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  2765. }
  2766. bool InitMultipleOverride ( void ) { return true; }
  2767. };
  2768. DEFINE_PARTICLE_OPERATOR( C_INIT_PositionWarp, "Position Modify Warp Random", OPERATOR_GENERIC );
  2769. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_PositionWarp )
  2770. DMXELEMENT_UNPACK_FIELD( "control point number", "0", int, m_nControlPointNumber )
  2771. DMXELEMENT_UNPACK_FIELD( "warp min", "1 1 1", Vector, m_vecWarpMin )
  2772. DMXELEMENT_UNPACK_FIELD( "warp max", "1 1 1", Vector, m_vecWarpMax )
  2773. DMXELEMENT_UNPACK_FIELD( "warp transition time (treats min/max as start/end sizes)", "0", float , m_flWarpTime )
  2774. DMXELEMENT_UNPACK_FIELD( "warp transition start time", "0", float , m_flWarpStartTime )
  2775. DMXELEMENT_UNPACK_FIELD( "reverse warp (0/1)", "0", bool , m_bInvertWarp )
  2776. DMXELEMENT_UNPACK_FIELD( "use particle count instead of time", "0", bool , m_bUseCount )
  2777. END_PARTICLE_OPERATOR_UNPACK( C_INIT_PositionWarp )
  2778. void C_INIT_PositionWarp::InitNewParticlesScalar(
  2779. CParticleCollection *pParticles, int start_p,
  2780. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  2781. {
  2782. PositionWarpContext_t *pCtx = reinterpret_cast<PositionWarpContext_t *>( pContext );
  2783. Vector vecWarpStart = m_vecWarpMin;
  2784. Vector vecWarpEnd = m_vecWarpMax;
  2785. if ( m_bInvertWarp )
  2786. {
  2787. vecWarpStart = m_vecWarpMax;
  2788. vecWarpEnd = m_vecWarpMin;
  2789. }
  2790. for( ; nParticleCount--; start_p++ )
  2791. {
  2792. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  2793. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  2794. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  2795. Vector randpos;
  2796. if ( m_flWarpTime != 0.0f )
  2797. {
  2798. if ( m_bUseCount )
  2799. {
  2800. float flWarpEnd = m_flWarpStartTime + m_flWarpTime + pCtx->m_nStartCount;
  2801. float flPercentage = RemapValClamped( pCtx->m_nCurrentCount, m_flWarpStartTime + pCtx->m_nStartCount, flWarpEnd, 0.0, 1.0 );
  2802. VectorLerp( vecWarpStart, vecWarpEnd, flPercentage, randpos );
  2803. pCtx->m_nCurrentCount++;
  2804. }
  2805. else
  2806. {
  2807. float flWarpEnd = m_flWarpStartTime + m_flWarpTime + pCtx->m_flStartTime;
  2808. float flPercentage = RemapValClamped( *ct, m_flWarpStartTime + pCtx->m_flStartTime, flWarpEnd, 0.0, 1.0 );
  2809. VectorLerp( vecWarpStart, vecWarpEnd, flPercentage, randpos );
  2810. }
  2811. }
  2812. else
  2813. {
  2814. pParticles->RandomVector( m_vecWarpMin, m_vecWarpMax, &randpos );
  2815. }
  2816. matrix3x4_t mat;
  2817. pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, *ct, &mat );
  2818. Vector vecTransformLocal = vec3_origin;
  2819. Vector vecParticlePosition, vecParticlePosition_prev ;
  2820. SetVectorFromAttribute( vecParticlePosition, xyz );
  2821. SetVectorFromAttribute( vecParticlePosition_prev, pxyz );
  2822. // rotate particles from world space into local
  2823. VectorITransform( vecParticlePosition, mat, vecTransformLocal );
  2824. // multiply position by desired amount
  2825. vecTransformLocal.x *= randpos.x;
  2826. vecTransformLocal.y *= randpos.y;
  2827. vecTransformLocal.z *= randpos.z;
  2828. // rotate back into world space
  2829. VectorTransform( vecTransformLocal, mat, vecParticlePosition );
  2830. // rinse, repeat
  2831. VectorITransform( vecParticlePosition_prev, mat, vecTransformLocal );
  2832. vecTransformLocal.x *= randpos.x;
  2833. vecTransformLocal.y *= randpos.y;
  2834. vecTransformLocal.z *= randpos.z;
  2835. VectorTransform( vecTransformLocal, mat, vecParticlePosition_prev );
  2836. // set positions into floats
  2837. SetVectorAttribute( xyz, vecParticlePosition );
  2838. SetVectorAttribute( pxyz, vecParticlePosition_prev );
  2839. }
  2840. }
  2841. //-----------------------------------------------------------------------------
  2842. // noise initializer
  2843. //-----------------------------------------------------------------------------
  2844. class C_INIT_CreationNoise : public CParticleInitializerOperatorInstance
  2845. {
  2846. DECLARE_PARTICLE_OPERATOR( C_INIT_CreationNoise );
  2847. uint32 GetWrittenAttributes( void ) const
  2848. {
  2849. return 1 << m_nFieldOutput;
  2850. }
  2851. uint32 GetReadAttributes( void ) const
  2852. {
  2853. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK | PARTICLE_ATTRIBUTE_XYZ_MASK;
  2854. }
  2855. uint32 GetFilter( void ) const
  2856. {
  2857. return FILTER_PARAMETER_REMAPPING_MASK;
  2858. }
  2859. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  2860. int nParticleCount, int nAttributeWriteMask,
  2861. void *pContext) const;
  2862. void InitNewParticlesBlock( CParticleCollection *pParticles,
  2863. int start_block, int n_blocks, int nAttributeWriteMask,
  2864. void *pContext ) const;
  2865. virtual bool IsScrubSafe() { return true; }
  2866. int m_nFieldOutput;
  2867. bool m_bAbsVal, m_bAbsValInv;
  2868. float m_flOffset;
  2869. float m_flOutputMin;
  2870. float m_flOutputMax;
  2871. float m_flNoiseScale, m_flNoiseScaleLoc;
  2872. Vector m_vecOffsetLoc;
  2873. float m_flWorldTimeScale;
  2874. };
  2875. DEFINE_PARTICLE_OPERATOR( C_INIT_CreationNoise, "Remap Noise to Scalar", OPERATOR_GENERIC );
  2876. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_CreationNoise )
  2877. DMXELEMENT_UNPACK_FIELD( "time noise coordinate scale","0.1",float,m_flNoiseScale)
  2878. DMXELEMENT_UNPACK_FIELD( "spatial noise coordinate scale","0.001",float,m_flNoiseScaleLoc)
  2879. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "3", int, m_nFieldOutput, "intchoice particlefield_scalar" )
  2880. DMXELEMENT_UNPACK_FIELD( "time coordinate offset","0", float, m_flOffset )
  2881. DMXELEMENT_UNPACK_FIELD( "spatial coordinate offset","0 0 0", Vector, m_vecOffsetLoc )
  2882. DMXELEMENT_UNPACK_FIELD( "absolute value","0", bool, m_bAbsVal )
  2883. DMXELEMENT_UNPACK_FIELD( "invert absolute value","0", bool, m_bAbsValInv )
  2884. DMXELEMENT_UNPACK_FIELD( "output minimum","0", float, m_flOutputMin )
  2885. DMXELEMENT_UNPACK_FIELD( "output maximum","1", float, m_flOutputMax )
  2886. DMXELEMENT_UNPACK_FIELD( "world time noise coordinate scale","0", float, m_flWorldTimeScale )
  2887. END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreationNoise );
  2888. void C_INIT_CreationNoise::InitNewParticlesBlock( CParticleCollection *pParticles,
  2889. int start_block, int n_blocks, int nAttributeWriteMask,
  2890. void *pContext ) const
  2891. {
  2892. float flAbsScale;
  2893. fltx4 fl4AbsVal;
  2894. fl4AbsVal = ( fltx4 ) CmpEqSIMD( Four_Zeros, Four_Zeros );
  2895. flAbsScale = 0.5;
  2896. // Set up values for more optimal absolute value calculations inside the loop
  2897. if ( m_bAbsVal )
  2898. {
  2899. fl4AbsVal = LoadAlignedSIMD( (float *) g_SIMD_clear_signmask );
  2900. flAbsScale = 1.0;
  2901. }
  2902. float fMin = m_flOutputMin;
  2903. float fMax = m_flOutputMax;
  2904. if ( ATTRIBUTES_WHICH_ARE_ANGLES & (1 << m_nFieldOutput ) )
  2905. {
  2906. fMin *= ( M_PI / 180.0f );
  2907. fMax *= ( M_PI / 180.0f );
  2908. }
  2909. float CoordScale = m_flNoiseScale;
  2910. float CoordScaleLoc = m_flNoiseScaleLoc;
  2911. float ValueScale, ValueBase;
  2912. ValueScale = ( flAbsScale *( fMax - fMin ) );
  2913. ValueBase = ( fMin+ ( ( 1.0 - flAbsScale ) *( fMax - fMin ) ) );
  2914. fltx4 fl4ValueBase = ReplicateX4( ValueBase );
  2915. fltx4 fl4ValueScale = ReplicateX4( ValueScale );
  2916. size_t attr_stride;
  2917. fltx4 *pAttr = pParticles->GetM128AttributePtrForWrite( m_nFieldOutput, &attr_stride );
  2918. pAttr += attr_stride * start_block;
  2919. const FourVectors *pxyz = pParticles->Get4VAttributePtr( PARTICLE_ATTRIBUTE_XYZ, &attr_stride );
  2920. pxyz += attr_stride * start_block;
  2921. const fltx4 *pCreationTime = pParticles->GetM128AttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, &attr_stride );
  2922. pCreationTime += attr_stride * start_block;
  2923. //setup
  2924. fltx4 fl4Offset = ReplicateX4( m_flOffset );
  2925. FourVectors fvOffsetLoc;
  2926. fvOffsetLoc.DuplicateVector( m_vecOffsetLoc );
  2927. FourVectors fvCoordBase;
  2928. fvCoordBase.x = AddSIMD(*pCreationTime, fl4Offset);
  2929. fvCoordBase.y = AddSIMD(*pCreationTime, fl4Offset);
  2930. fvCoordBase.z = AddSIMD(*pCreationTime, fl4Offset);
  2931. fvCoordBase *= CoordScale;
  2932. while( n_blocks-- )
  2933. {
  2934. FourVectors fvCoordLoc = *pxyz;
  2935. fvCoordLoc += fvOffsetLoc;
  2936. FourVectors fvCoord = fvCoordBase;
  2937. fvCoordLoc *= CoordScaleLoc;
  2938. fvCoord += fvCoordLoc;
  2939. fltx4 fl4Noise;
  2940. fl4Noise = NoiseSIMD( fvCoord );
  2941. fl4Noise = AndSIMD ( fl4Noise, fl4AbsVal );
  2942. if ( m_bAbsValInv )
  2943. {
  2944. fl4Noise = SubSIMD( Four_Ones, fl4Noise );
  2945. }
  2946. fltx4 fl4InitialNoise;
  2947. fl4InitialNoise = AddSIMD( fl4ValueBase, ( MulSIMD( fl4ValueScale, fl4Noise ) ) );
  2948. if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & (1 << m_nFieldOutput ) )
  2949. {
  2950. fl4InitialNoise = MinSIMD( Four_Ones, fl4InitialNoise );
  2951. fl4InitialNoise = MaxSIMD( Four_Zeros, fl4InitialNoise );
  2952. }
  2953. *( pAttr ) = fl4InitialNoise;
  2954. pAttr += attr_stride;
  2955. pxyz += attr_stride;
  2956. }
  2957. }
  2958. void C_INIT_CreationNoise::InitNewParticlesScalar(
  2959. CParticleCollection *pParticles, int start_p,
  2960. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  2961. {
  2962. float flAbsScale;
  2963. int nAbsVal;
  2964. nAbsVal = 0xffffffff;
  2965. flAbsScale = 0.5;
  2966. if ( m_bAbsVal )
  2967. {
  2968. nAbsVal = 0x7fffffff;
  2969. flAbsScale = 1.0;
  2970. }
  2971. float fMin = m_flOutputMin;
  2972. float fMax = m_flOutputMax;
  2973. if ( ATTRIBUTES_WHICH_ARE_ANGLES & (1 << m_nFieldOutput ) )
  2974. {
  2975. fMin *= ( M_PI / 180.0f );
  2976. fMax *= ( M_PI / 180.0f );
  2977. }
  2978. float CoordScale = m_flNoiseScale;
  2979. float CoordScaleLoc = m_flNoiseScaleLoc;
  2980. float ValueScale, ValueBase;
  2981. ValueScale = ( flAbsScale *( fMax - fMin ) );
  2982. ValueBase = ( fMin+ ( ( 1.0 - flAbsScale ) *( fMax - fMin ) ) );
  2983. Vector CoordLoc, CoordWorldTime, CoordBase;
  2984. const float *pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  2985. float Offset = m_flOffset;
  2986. CoordBase = Vector ( (*pCreationTime + Offset), (*pCreationTime + Offset), (*pCreationTime + Offset) );
  2987. CoordBase *= CoordScale;
  2988. CoordWorldTime = Vector( (Plat_MSTime() * m_flWorldTimeScale), (Plat_MSTime() * m_flWorldTimeScale), (Plat_MSTime() * m_flWorldTimeScale) );
  2989. CoordBase += CoordWorldTime;
  2990. for( ; nParticleCount--; start_p++ )
  2991. {
  2992. const float *pxyz = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_XYZ, start_p );
  2993. float *pAttr = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  2994. Vector Coord = CoordBase;
  2995. CoordLoc.x = pxyz[0];
  2996. CoordLoc.y = pxyz[4];
  2997. CoordLoc.z = pxyz[8];
  2998. CoordLoc += m_vecOffsetLoc;
  2999. CoordLoc *= CoordScaleLoc;
  3000. Coord += CoordLoc;
  3001. fltx4 flNoise128;
  3002. FourVectors fvNoise;
  3003. fvNoise.DuplicateVector( Coord );
  3004. flNoise128 = NoiseSIMD( fvNoise );
  3005. float flNoise = SubFloat( flNoise128, 0 );
  3006. *( (int *) &flNoise) &= nAbsVal;
  3007. if ( m_bAbsValInv )
  3008. {
  3009. flNoise = 1.0 - flNoise;
  3010. }
  3011. float flInitialNoise = ( ValueBase + ( ValueScale * flNoise ) );
  3012. if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & (1 << m_nFieldOutput ) )
  3013. {
  3014. flInitialNoise = clamp(flInitialNoise, 0.0f, 1.0f );
  3015. }
  3016. *( pAttr ) = flInitialNoise;
  3017. }
  3018. }
  3019. class C_INIT_CreateAlongPath : public CParticleInitializerOperatorInstance
  3020. {
  3021. DECLARE_PARTICLE_OPERATOR( C_INIT_CreateAlongPath );
  3022. float m_fMaxDistance;
  3023. struct CPathParameters m_PathParams;
  3024. bool m_bUseRandomCPs;
  3025. uint32 GetWrittenAttributes( void ) const
  3026. {
  3027. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  3028. }
  3029. uint32 GetReadAttributes( void ) const
  3030. {
  3031. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  3032. }
  3033. virtual uint64 GetReadControlPointMask() const
  3034. {
  3035. uint64 nStartMask = ( 1ULL << m_PathParams.m_nStartControlPointNumber ) - 1;
  3036. uint64 nEndMask = ( 1ULL << ( m_PathParams.m_nEndControlPointNumber + 1 ) ) - 1;
  3037. return nEndMask & (~nStartMask);
  3038. }
  3039. void InitParams( CParticleSystemDefinition *pDef )
  3040. {
  3041. m_PathParams.ClampControlPointIndices();
  3042. }
  3043. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  3044. int nParticleCount, int nAttributeWriteMask,
  3045. void *pContext) const;
  3046. };
  3047. DEFINE_PARTICLE_OPERATOR( C_INIT_CreateAlongPath, "Position Along Path Random", OPERATOR_PI_POSITION );
  3048. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_CreateAlongPath )
  3049. DMXELEMENT_UNPACK_FIELD( "maximum distance", "0", float, m_fMaxDistance )
  3050. DMXELEMENT_UNPACK_FIELD( "bulge", "0", float, m_PathParams.m_flBulge )
  3051. DMXELEMENT_UNPACK_FIELD( "start control point number", "0", int, m_PathParams.m_nStartControlPointNumber )
  3052. DMXELEMENT_UNPACK_FIELD( "end control point number", "0", int, m_PathParams.m_nEndControlPointNumber )
  3053. DMXELEMENT_UNPACK_FIELD( "bulge control 0=random 1=orientation of start pnt 2=orientation of end point", "0", int, m_PathParams.m_nBulgeControl )
  3054. DMXELEMENT_UNPACK_FIELD( "mid point position", "0.5", float, m_PathParams.m_flMidPoint )
  3055. DMXELEMENT_UNPACK_FIELD( "randomly select sequential CP pairs between start and end points", "0", bool, m_bUseRandomCPs )
  3056. END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateAlongPath )
  3057. void C_INIT_CreateAlongPath::InitNewParticlesScalar(
  3058. CParticleCollection *pParticles, int start_p,
  3059. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  3060. {
  3061. for( ; nParticleCount--; start_p++ )
  3062. {
  3063. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  3064. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  3065. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  3066. struct CPathParameters PathParams = m_PathParams;
  3067. if ( m_bUseRandomCPs )
  3068. {
  3069. PathParams.m_nEndControlPointNumber = pParticles->RandomInt( PathParams.m_nStartControlPointNumber + 1, PathParams.m_nEndControlPointNumber );
  3070. PathParams.m_nStartControlPointNumber = PathParams.m_nEndControlPointNumber - 1 ;
  3071. }
  3072. Vector StartPnt, MidP, EndPnt;
  3073. pParticles->CalculatePathValues( PathParams, *ct, &StartPnt, &MidP, &EndPnt);
  3074. float t=pParticles->RandomFloat( 0.0, 1.0 );
  3075. Vector randpos;
  3076. pParticles->RandomVector( -m_fMaxDistance, m_fMaxDistance, &randpos );
  3077. // form delta terms needed for quadratic bezier
  3078. Vector Delta0=MidP-StartPnt;
  3079. Vector Delta1 = EndPnt-MidP;
  3080. Vector L0 = StartPnt+t*Delta0;
  3081. Vector L1 = MidP+t*Delta1;
  3082. Vector Pnt = L0+(L1-L0)*t;
  3083. Pnt+=randpos;
  3084. xyz[0] = Pnt.x;
  3085. xyz[4] = Pnt.y;
  3086. xyz[8] = Pnt.z;
  3087. if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
  3088. {
  3089. pxyz[0] = Pnt.x;
  3090. pxyz[4] = Pnt.y;
  3091. pxyz[8] = Pnt.z;
  3092. }
  3093. }
  3094. }
  3095. class C_INIT_MoveBetweenPoints : public CParticleInitializerOperatorInstance
  3096. {
  3097. DECLARE_PARTICLE_OPERATOR( C_INIT_MoveBetweenPoints );
  3098. float m_flSpeedMin, m_flSpeedMax;
  3099. float m_flEndSpread;
  3100. float m_flStartOffset;
  3101. float m_flEndOffset;
  3102. int m_nEndControlPointNumber;
  3103. bool m_bTrailBias;
  3104. uint32 GetWrittenAttributes( void ) const
  3105. {
  3106. return PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_XYZ_MASK;
  3107. }
  3108. uint32 GetReadAttributes( void ) const
  3109. {
  3110. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_CREATION_TIME_MASK | PARTICLE_ATTRIBUTE_TRAIL_LENGTH_MASK;
  3111. }
  3112. virtual uint64 GetReadControlPointMask() const
  3113. {
  3114. return 1ULL << m_nEndControlPointNumber;
  3115. }
  3116. bool InitMultipleOverride ( void )
  3117. {
  3118. if ( m_bTrailBias )
  3119. return true;
  3120. else
  3121. return false;
  3122. }
  3123. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  3124. int nParticleCount, int nAttributeWriteMask,
  3125. void *pContext) const;
  3126. };
  3127. DEFINE_PARTICLE_OPERATOR( C_INIT_MoveBetweenPoints, "Move Particles Between 2 Control Points", OPERATOR_GENERIC );
  3128. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_MoveBetweenPoints )
  3129. DMXELEMENT_UNPACK_FIELD( "minimum speed", "1", float, m_flSpeedMin )
  3130. DMXELEMENT_UNPACK_FIELD( "maximum speed", "1", float, m_flSpeedMax )
  3131. DMXELEMENT_UNPACK_FIELD( "end spread", "0", float, m_flEndSpread )
  3132. DMXELEMENT_UNPACK_FIELD( "start offset", "0", float, m_flStartOffset )
  3133. DMXELEMENT_UNPACK_FIELD( "end offset", "0", float, m_flEndOffset )
  3134. DMXELEMENT_UNPACK_FIELD( "bias lifetime by trail length", "0", bool, m_bTrailBias )
  3135. DMXELEMENT_UNPACK_FIELD( "end control point", "1", int, m_nEndControlPointNumber )
  3136. END_PARTICLE_OPERATOR_UNPACK( C_INIT_MoveBetweenPoints )
  3137. void C_INIT_MoveBetweenPoints::InitNewParticlesScalar(
  3138. CParticleCollection *pParticles, int start_p,
  3139. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  3140. {
  3141. bool bMoveStartPoint = ( m_flStartOffset != 0.0 );
  3142. bool bMoveEndPoint = ( m_flEndOffset != 0.0 );
  3143. for( ; nParticleCount--; start_p++ )
  3144. {
  3145. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  3146. float *pPrevXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  3147. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  3148. float *dtime = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
  3149. Vector StartPnt( pxyz[0], pxyz[4], pxyz[8] );
  3150. Vector vecEndPoint;
  3151. pParticles->GetControlPointAtTime( m_nEndControlPointNumber, *ct, &vecEndPoint );
  3152. Vector randpos(0,0,0);
  3153. if ( m_flEndSpread > 0.0 )
  3154. {
  3155. pParticles->RandomVectorInUnitSphere( &randpos );
  3156. randpos *= m_flEndSpread;
  3157. }
  3158. vecEndPoint += randpos;
  3159. Vector vDelta = vecEndPoint - StartPnt;
  3160. float flLen = VectorLength( vDelta );
  3161. if ( bMoveEndPoint )
  3162. {
  3163. vecEndPoint += ( m_flEndOffset / ( flLen + FLT_EPSILON ) ) * vDelta;
  3164. vDelta = vecEndPoint - StartPnt;
  3165. flLen = VectorLength( vDelta );
  3166. }
  3167. if ( bMoveStartPoint )
  3168. {
  3169. StartPnt += ( m_flStartOffset/ ( flLen + FLT_EPSILON ) ) * vDelta;
  3170. vDelta = vecEndPoint - StartPnt;
  3171. flLen = VectorLength( vDelta );
  3172. }
  3173. float flVel = pParticles->RandomFloat( m_flSpeedMin, m_flSpeedMax );
  3174. if ( m_bTrailBias )
  3175. {
  3176. const float *pTrailLength = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_TRAIL_LENGTH, start_p );
  3177. *dtime = ( flLen / ( flVel + FLT_EPSILON ) ) + *pTrailLength;
  3178. }
  3179. else
  3180. {
  3181. *dtime = flLen / ( flVel + FLT_EPSILON );
  3182. }
  3183. Vector poffset = vDelta * ( flVel / ( flLen + FLT_EPSILON ) );
  3184. poffset *= pParticles->m_flPreviousDt;
  3185. if ( bMoveStartPoint )
  3186. {
  3187. pxyz[0] = StartPnt.x;
  3188. pxyz[1] = StartPnt.y;
  3189. pxyz[2] = StartPnt.z;
  3190. }
  3191. pPrevXYZ[0] = pxyz[0] - poffset.x;
  3192. pPrevXYZ[4] = pxyz[4] - poffset.y;
  3193. pPrevXYZ[8] = pxyz[8] - poffset.z;
  3194. }
  3195. }
  3196. //-----------------------------------------------------------------------------
  3197. // Remap Scalar Initializer
  3198. //-----------------------------------------------------------------------------
  3199. class C_INIT_RemapScalar : public CParticleInitializerOperatorInstance
  3200. {
  3201. DECLARE_PARTICLE_OPERATOR( C_INIT_RemapScalar );
  3202. uint32 GetWrittenAttributes( void ) const
  3203. {
  3204. return 1 << m_nFieldOutput;
  3205. }
  3206. uint32 GetReadAttributes( void ) const
  3207. {
  3208. return 1 << m_nFieldInput;
  3209. }
  3210. uint32 GetFilter( void ) const
  3211. {
  3212. return FILTER_PARAMETER_REMAPPING_MASK;
  3213. }
  3214. void InitParams( CParticleSystemDefinition *pDef )
  3215. {
  3216. // clamp the result to 0 and 1 if it's alpha
  3217. if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & ( 1 << m_nFieldOutput ) )
  3218. {
  3219. m_flOutputMin = clamp(m_flOutputMin, 0.0f, 1.0f );
  3220. m_flOutputMax = clamp(m_flOutputMax, 0.0f, 1.0f );
  3221. }
  3222. }
  3223. bool InitMultipleOverride ( void ) { return true; }
  3224. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  3225. int nParticleCount, int nAttributeWriteMask,
  3226. void *pContext) const;
  3227. int m_nFieldInput;
  3228. int m_nFieldOutput;
  3229. float m_flInputMin;
  3230. float m_flInputMax;
  3231. float m_flOutputMin;
  3232. float m_flOutputMax;
  3233. float m_flStartTime;
  3234. float m_flEndTime;
  3235. bool m_bScaleInitialRange;
  3236. bool m_bActiveRange;
  3237. };
  3238. DEFINE_PARTICLE_OPERATOR( C_INIT_RemapScalar, "Remap Initial Scalar", OPERATOR_GENERIC );
  3239. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RemapScalar )
  3240. DMXELEMENT_UNPACK_FIELD( "emitter lifetime start time (seconds)", "-1", float, m_flStartTime )
  3241. DMXELEMENT_UNPACK_FIELD( "emitter lifetime end time (seconds)", "-1", float, m_flEndTime )
  3242. DMXELEMENT_UNPACK_FIELD_USERDATA( "input field", "8", int, m_nFieldInput, "intchoice particlefield_scalar" )
  3243. DMXELEMENT_UNPACK_FIELD( "input minimum","0", float, m_flInputMin )
  3244. DMXELEMENT_UNPACK_FIELD( "input maximum","1", float, m_flInputMax )
  3245. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "3", int, m_nFieldOutput, "intchoice particlefield_scalar" )
  3246. DMXELEMENT_UNPACK_FIELD( "output minimum","0", float, m_flOutputMin )
  3247. DMXELEMENT_UNPACK_FIELD( "output maximum","1", float, m_flOutputMax )
  3248. DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
  3249. DMXELEMENT_UNPACK_FIELD( "only active within specified input range","0", bool, m_bActiveRange )
  3250. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapScalar )
  3251. void C_INIT_RemapScalar::InitNewParticlesScalar(
  3252. CParticleCollection *pParticles, int start_p,
  3253. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  3254. {
  3255. const float *pCreationTime;
  3256. // FIXME: SSE-ize
  3257. for( ; nParticleCount--; start_p++ )
  3258. {
  3259. pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  3260. // using raw creation time to map to emitter lifespan
  3261. float flLifeTime = *pCreationTime;
  3262. float flInput;
  3263. if ( ATTRIBUTES_WHICH_ARE_INTS & ( 1 << m_nFieldInput ) )
  3264. {
  3265. const int *pInput = pParticles->GetIntAttributePtr( m_nFieldInput, start_p );
  3266. flInput = float( *pInput );
  3267. }
  3268. else
  3269. {
  3270. const float *pInput = pParticles->GetFloatAttributePtr( m_nFieldInput, start_p );
  3271. flInput = *pInput;
  3272. }
  3273. // only use within start/end time frame and, if set, active input range
  3274. if ( ( ( ( flLifeTime < m_flStartTime ) || ( flLifeTime >= m_flEndTime ) ) && ( ( m_flStartTime != -1.0f) && ( m_flEndTime != -1.0f) ) ) || ( m_bActiveRange && ( flInput < m_flInputMin || flInput > m_flInputMax ) ) )
  3275. continue;
  3276. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  3277. float flOutput = RemapValClamped( flInput, m_flInputMin, m_flInputMax, m_flOutputMin, m_flOutputMax );
  3278. if ( m_bScaleInitialRange )
  3279. {
  3280. float flInitialValue = *pOutput;
  3281. flOutput *= flInitialValue;
  3282. }
  3283. if ( ATTRIBUTES_WHICH_ARE_INTS & ( 1 << m_nFieldOutput ) )
  3284. {
  3285. *pOutput = int ( flOutput );
  3286. }
  3287. else
  3288. {
  3289. *pOutput = flOutput;
  3290. }
  3291. }
  3292. }
  3293. //-----------------------------------------------------------------------------
  3294. // Remap Particle Count to Scalar Initializer
  3295. //-----------------------------------------------------------------------------
  3296. struct RemapCountContext_t
  3297. {
  3298. int m_nCurrentParticleCount;
  3299. };
  3300. class C_INIT_RemapParticleCountToScalar : public CParticleInitializerOperatorInstance
  3301. {
  3302. DECLARE_PARTICLE_OPERATOR( C_INIT_RemapParticleCountToScalar );
  3303. uint32 GetWrittenAttributes( void ) const
  3304. {
  3305. return 1 << m_nFieldOutput;
  3306. }
  3307. uint32 GetReadAttributes( void ) const
  3308. {
  3309. return 0;
  3310. }
  3311. uint32 GetFilter( void ) const
  3312. {
  3313. return FILTER_PARAMETER_REMAPPING_MASK;
  3314. }
  3315. virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
  3316. {
  3317. RemapCountContext_t *pCtx = reinterpret_cast<RemapCountContext_t *>( pContext );
  3318. pCtx->m_nCurrentParticleCount = 0;
  3319. }
  3320. virtual void Restart( CParticleCollection *pParticles, void *pContext )
  3321. {
  3322. RemapCountContext_t *pCtx = reinterpret_cast<RemapCountContext_t *>( pContext );
  3323. pCtx->m_nCurrentParticleCount = 0;
  3324. }
  3325. size_t GetRequiredContextBytes( void ) const
  3326. {
  3327. return sizeof( RemapCountContext_t );
  3328. }
  3329. void InitParams( CParticleSystemDefinition *pDef )
  3330. {
  3331. // clamp the result to 0 and 1 if it's alpha
  3332. if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & ( 1 << m_nFieldOutput ) )
  3333. {
  3334. m_flOutputMin = clamp(m_flOutputMin, 0.0f, 1.0f );
  3335. m_flOutputMax = clamp(m_flOutputMax, 0.0f, 1.0f );
  3336. }
  3337. }
  3338. bool InitMultipleOverride ( void ) { return true; }
  3339. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  3340. int nParticleCount, int nAttributeWriteMask,
  3341. void *pContext) const;
  3342. int m_nFieldInput;
  3343. int m_nFieldOutput;
  3344. int m_nInputMin;
  3345. int m_nInputMax;
  3346. float m_flOutputMin;
  3347. float m_flOutputMax;
  3348. bool m_bScaleInitialRange;
  3349. bool m_bActiveRange;
  3350. };
  3351. DEFINE_PARTICLE_OPERATOR( C_INIT_RemapParticleCountToScalar, "Remap Particle Count to Scalar", OPERATOR_GENERIC );
  3352. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RemapParticleCountToScalar )
  3353. DMXELEMENT_UNPACK_FIELD( "input minimum","0", int, m_nInputMin )
  3354. DMXELEMENT_UNPACK_FIELD( "input maximum","10", int, m_nInputMax )
  3355. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "3", int, m_nFieldOutput, "intchoice particlefield_scalar" )
  3356. DMXELEMENT_UNPACK_FIELD( "output minimum","0", float, m_flOutputMin )
  3357. DMXELEMENT_UNPACK_FIELD( "output maximum","1", float, m_flOutputMax )
  3358. DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
  3359. DMXELEMENT_UNPACK_FIELD( "only active within specified input range","0", bool, m_bActiveRange )
  3360. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapParticleCountToScalar )
  3361. void C_INIT_RemapParticleCountToScalar::InitNewParticlesScalar(
  3362. CParticleCollection *pParticles, int start_p,
  3363. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  3364. {
  3365. RemapCountContext_t *pCtx = reinterpret_cast<RemapCountContext_t *>( pContext );
  3366. // if we're only working on a specified set, and we've exceeded it, early out
  3367. if ( m_bActiveRange && pCtx->m_nCurrentParticleCount > m_nInputMax )
  3368. return;
  3369. // FIXME: SSE-ize
  3370. for( ; nParticleCount--; start_p++ )
  3371. {
  3372. if ( m_bActiveRange && ( pCtx->m_nCurrentParticleCount < m_nInputMin || pCtx->m_nCurrentParticleCount > m_nInputMax ) )
  3373. {
  3374. pCtx->m_nCurrentParticleCount++;
  3375. continue;
  3376. }
  3377. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  3378. float flOutput = RemapValClamped( float( pCtx->m_nCurrentParticleCount ), float( m_nInputMin ), float( m_nInputMax ), m_flOutputMin, m_flOutputMax );
  3379. if ( m_bScaleInitialRange )
  3380. {
  3381. float flInitialValue = *pOutput;
  3382. flOutput *= flInitialValue;
  3383. }
  3384. if ( ATTRIBUTES_WHICH_ARE_INTS & ( 1 << m_nFieldOutput ) )
  3385. {
  3386. *pOutput = int ( flOutput );
  3387. }
  3388. else
  3389. {
  3390. *pOutput = flOutput;
  3391. }
  3392. pCtx->m_nCurrentParticleCount++;
  3393. }
  3394. }
  3395. //-----------------------------------------------------------------------------
  3396. // Inherit Velocity Initializer
  3397. // Causes particles to inherit the velocity of their CP at spawn
  3398. //
  3399. //-----------------------------------------------------------------------------
  3400. class C_INIT_InheritVelocity : public CParticleInitializerOperatorInstance
  3401. {
  3402. DECLARE_PARTICLE_OPERATOR( C_INIT_InheritVelocity );
  3403. int m_nControlPointNumber;
  3404. float m_flVelocityScale;
  3405. uint32 GetWrittenAttributes( void ) const
  3406. {
  3407. return PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  3408. }
  3409. uint32 GetReadAttributes( void ) const
  3410. {
  3411. return PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  3412. }
  3413. virtual uint64 GetReadControlPointMask() const
  3414. {
  3415. return 1ULL << m_nControlPointNumber;
  3416. }
  3417. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  3418. int nParticleCount, int nAttributeWriteMask,
  3419. void *pContext) const;
  3420. void InitParams( CParticleSystemDefinition *pDef )
  3421. {
  3422. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  3423. }
  3424. bool InitMultipleOverride ( void ) { return true; }
  3425. };
  3426. DEFINE_PARTICLE_OPERATOR( C_INIT_InheritVelocity, "Velocity Inherit from Control Point", OPERATOR_GENERIC );
  3427. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_InheritVelocity )
  3428. DMXELEMENT_UNPACK_FIELD( "control point number", "0", int, m_nControlPointNumber )
  3429. DMXELEMENT_UNPACK_FIELD( "velocity scale", "1", float, m_flVelocityScale )
  3430. END_PARTICLE_OPERATOR_UNPACK( C_INIT_InheritVelocity )
  3431. void C_INIT_InheritVelocity::InitNewParticlesScalar(
  3432. CParticleCollection *pParticles, int start_p,
  3433. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  3434. {
  3435. Vector vecControlPointPrev;
  3436. pParticles->GetControlPointAtPrevTime( m_nControlPointNumber, &vecControlPointPrev );
  3437. Vector vecDeltaPos = pParticles->GetControlPointAtCurrentTime( m_nControlPointNumber) - vecControlPointPrev;
  3438. vecDeltaPos *= m_flVelocityScale;
  3439. for( ; nParticleCount--; start_p++ )
  3440. {
  3441. float *prevxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  3442. prevxyz[0] -= vecDeltaPos.x;
  3443. prevxyz[4] -= vecDeltaPos.y;
  3444. prevxyz[8] -= vecDeltaPos.z;
  3445. }
  3446. }
  3447. //-----------------------------------------------------------------------------
  3448. // Velocity From CP Initializer
  3449. // Causes particles to inherit the velocity of their CP at spawn
  3450. //
  3451. //-----------------------------------------------------------------------------
  3452. class C_INIT_VelocityFromCP : public CParticleInitializerOperatorInstance
  3453. {
  3454. DECLARE_PARTICLE_OPERATOR( C_INIT_VelocityFromCP );
  3455. int m_nControlPoint;
  3456. int m_nControlPointCompare;
  3457. int m_nControlPointLocal;
  3458. float m_flVelocityScale;
  3459. bool m_bDirectionOnly;
  3460. uint32 GetWrittenAttributes( void ) const
  3461. {
  3462. return PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  3463. }
  3464. uint32 GetReadAttributes( void ) const
  3465. {
  3466. return PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  3467. }
  3468. virtual uint64 GetReadControlPointMask() const
  3469. {
  3470. uint64 nMask = ( 1ULL << m_nControlPoint );
  3471. if ( m_nControlPointCompare != -1 )
  3472. {
  3473. nMask |= ( 1ULL << m_nControlPointCompare );
  3474. }
  3475. if ( m_nControlPointLocal != -1 )
  3476. {
  3477. nMask |= ( 1ULL << m_nControlPointLocal );
  3478. }
  3479. return nMask;
  3480. }
  3481. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  3482. int nParticleCount, int nAttributeWriteMask,
  3483. void *pContext) const;
  3484. bool InitMultipleOverride ( void ) { return true; }
  3485. };
  3486. DEFINE_PARTICLE_OPERATOR( C_INIT_VelocityFromCP, "Velocity Set from Control Point", OPERATOR_GENERIC );
  3487. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_VelocityFromCP )
  3488. DMXELEMENT_UNPACK_FIELD( "control point number", "0", int, m_nControlPoint )
  3489. DMXELEMENT_UNPACK_FIELD( "velocity scale", "1", float, m_flVelocityScale )
  3490. DMXELEMENT_UNPACK_FIELD( "comparison control point number", "-1", int, m_nControlPointCompare )
  3491. DMXELEMENT_UNPACK_FIELD( "local space control point number", "-1", int, m_nControlPointLocal )
  3492. DMXELEMENT_UNPACK_FIELD( "direction only", "0", bool, m_bDirectionOnly )
  3493. END_PARTICLE_OPERATOR_UNPACK( C_INIT_VelocityFromCP )
  3494. void C_INIT_VelocityFromCP::InitNewParticlesScalar(
  3495. CParticleCollection *pParticles, int start_p,
  3496. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  3497. {
  3498. Vector vecDeltaPos;
  3499. if ( m_nControlPointCompare > -1 )
  3500. vecDeltaPos = ( pParticles->GetControlPointAtCurrentTime( m_nControlPoint ) - pParticles->GetControlPointAtCurrentTime( m_nControlPointCompare ) );
  3501. else
  3502. {
  3503. vecDeltaPos = pParticles->GetControlPointAtCurrentTime( m_nControlPoint );
  3504. }
  3505. if ( m_nControlPointLocal > -1 )
  3506. {
  3507. Vector vecDeltaPosInitial = vecDeltaPos;
  3508. matrix3x4_t mat;
  3509. pParticles->GetControlPointTransformAtCurrentTime( m_nControlPointLocal, &mat );
  3510. VectorRotate( vecDeltaPosInitial, mat, vecDeltaPos );
  3511. }
  3512. if ( m_bDirectionOnly )
  3513. vecDeltaPos.NormalizeInPlace();
  3514. vecDeltaPos *= pParticles->m_flPreviousDt;
  3515. vecDeltaPos.x *= m_flVelocityScale;
  3516. vecDeltaPos.y *= m_flVelocityScale;
  3517. vecDeltaPos.z *= m_flVelocityScale;
  3518. for( ; nParticleCount--; start_p++ )
  3519. {
  3520. float *prevxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  3521. prevxyz[0] -= vecDeltaPos.x;
  3522. prevxyz[4] -= vecDeltaPos.y;
  3523. prevxyz[8] -= vecDeltaPos.z;
  3524. }
  3525. }
  3526. //-----------------------------------------------------------------------------
  3527. // Pre-Age Noise
  3528. // Sets particle creation time back to treat newly spawned particle as if
  3529. // part of its life has already elapsed.
  3530. //-----------------------------------------------------------------------------
  3531. class C_INIT_AgeNoise : public CParticleInitializerOperatorInstance
  3532. {
  3533. DECLARE_PARTICLE_OPERATOR( C_INIT_AgeNoise );
  3534. uint32 GetWrittenAttributes( void ) const
  3535. {
  3536. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  3537. }
  3538. uint32 GetReadAttributes( void ) const
  3539. {
  3540. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK | PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
  3541. }
  3542. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  3543. int nParticleCount, int nAttributeWriteMask,
  3544. void *pContext) const;
  3545. bool InitMultipleOverride ( void ) { return true; }
  3546. bool m_bAbsVal, m_bAbsValInv;
  3547. float m_flOffset;
  3548. float m_flAgeMin;
  3549. float m_flAgeMax;
  3550. float m_flNoiseScale, m_flNoiseScaleLoc;
  3551. Vector m_vecOffsetLoc;
  3552. };
  3553. DEFINE_PARTICLE_OPERATOR( C_INIT_AgeNoise, "Lifetime Pre-Age Noise", OPERATOR_GENERIC );
  3554. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_AgeNoise )
  3555. DMXELEMENT_UNPACK_FIELD( "time noise coordinate scale","1.0",float,m_flNoiseScale)
  3556. DMXELEMENT_UNPACK_FIELD( "spatial noise coordinate scale","1.0",float,m_flNoiseScaleLoc)
  3557. DMXELEMENT_UNPACK_FIELD( "time coordinate offset","0", float, m_flOffset )
  3558. DMXELEMENT_UNPACK_FIELD( "spatial coordinate offset","0 0 0", Vector, m_vecOffsetLoc )
  3559. DMXELEMENT_UNPACK_FIELD( "absolute value","0", bool, m_bAbsVal )
  3560. DMXELEMENT_UNPACK_FIELD( "invert absolute value","0", bool, m_bAbsValInv )
  3561. DMXELEMENT_UNPACK_FIELD( "start age minimum","0", float, m_flAgeMin )
  3562. DMXELEMENT_UNPACK_FIELD( "start age maximum","1", float, m_flAgeMax )
  3563. END_PARTICLE_OPERATOR_UNPACK( C_INIT_AgeNoise );
  3564. void C_INIT_AgeNoise::InitNewParticlesScalar(
  3565. CParticleCollection *pParticles, int start_p,
  3566. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  3567. {
  3568. float flAbsScale;
  3569. int nAbsVal;
  3570. nAbsVal = 0xffffffff;
  3571. flAbsScale = 0.5;
  3572. if ( m_bAbsVal )
  3573. {
  3574. nAbsVal = 0x7fffffff;
  3575. flAbsScale = 1.0;
  3576. }
  3577. float fMin = m_flAgeMin;
  3578. float fMax = m_flAgeMax;
  3579. float CoordScale = m_flNoiseScale;
  3580. float CoordScaleLoc = m_flNoiseScaleLoc;
  3581. for( ; nParticleCount--; start_p++ )
  3582. {
  3583. const float *pxyz = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_XYZ, start_p );
  3584. const float *pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  3585. const float *pLifespan = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
  3586. float *pAttr = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  3587. float ValueScale, ValueBase;
  3588. Vector Coord, CoordLoc;
  3589. CoordLoc.x = pxyz[0];
  3590. CoordLoc.y = pxyz[4];
  3591. CoordLoc.z = pxyz[8];
  3592. CoordLoc += m_vecOffsetLoc;
  3593. float Offset = m_flOffset;
  3594. Coord = Vector ( (*pCreationTime + Offset), (*pCreationTime + Offset), (*pCreationTime + Offset) );
  3595. Coord *= CoordScale;
  3596. CoordLoc *= CoordScaleLoc;
  3597. Coord += CoordLoc;
  3598. fltx4 flNoise128;
  3599. FourVectors fvNoise;
  3600. fvNoise.DuplicateVector( Coord );
  3601. flNoise128 = NoiseSIMD( fvNoise );
  3602. float flNoise = SubFloat( flNoise128, 0 );
  3603. *( (int *) &flNoise) &= nAbsVal;
  3604. ValueScale = ( flAbsScale *( fMax - fMin ) );
  3605. ValueBase = ( fMin+ ( ( 1.0 - flAbsScale ) *( fMax - fMin ) ) );
  3606. if ( m_bAbsValInv )
  3607. {
  3608. flNoise = 1.0 - flNoise;
  3609. }
  3610. float flInitialNoise = ( ValueBase + ( ValueScale * flNoise ) );
  3611. flInitialNoise = clamp(flInitialNoise, 0.0f, 1.0f );
  3612. flInitialNoise *= *pLifespan;
  3613. *( pAttr ) = *pCreationTime - flInitialNoise;
  3614. }
  3615. }
  3616. //-----------------------------------------------------------------------------
  3617. // LifeTime Sequence Length
  3618. //-----------------------------------------------------------------------------
  3619. class C_INIT_SequenceLifeTime : public CParticleInitializerOperatorInstance
  3620. {
  3621. DECLARE_PARTICLE_OPERATOR( C_INIT_SequenceLifeTime );
  3622. float m_flFramerate;
  3623. uint32 GetWrittenAttributes( void ) const
  3624. {
  3625. return PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
  3626. }
  3627. uint32 GetReadAttributes( void ) const
  3628. {
  3629. return PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER_MASK;
  3630. }
  3631. bool InitMultipleOverride ( void ) { return true; }
  3632. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  3633. int nParticleCount, int nAttributeWriteMask, void *pContext ) const;
  3634. };
  3635. DEFINE_PARTICLE_OPERATOR( C_INIT_SequenceLifeTime, "Lifetime From Sequence", OPERATOR_GENERIC );
  3636. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_SequenceLifeTime )
  3637. DMXELEMENT_UNPACK_FIELD( "Frames Per Second", "30", float, m_flFramerate )
  3638. END_PARTICLE_OPERATOR_UNPACK( C_INIT_SequenceLifeTime )
  3639. void C_INIT_SequenceLifeTime::InitNewParticlesScalar(
  3640. CParticleCollection *pParticles, int start_p,
  3641. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  3642. {
  3643. if ( ( m_flFramerate != 0.0f ) && ( pParticles->m_Sheet() ) )
  3644. {
  3645. for( ; nParticleCount--; start_p++ )
  3646. {
  3647. const float *flSequence = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER, start_p );
  3648. float *dtime = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
  3649. int nSequence = *flSequence;
  3650. if ( pParticles->m_Sheet()->m_SheetInfo[nSequence].m_flFrameSpan != 0 )
  3651. {
  3652. *dtime = pParticles->m_Sheet()->m_SheetInfo[nSequence].m_flFrameSpan / m_flFramerate;
  3653. }
  3654. else
  3655. {
  3656. *dtime = 1.0;
  3657. }
  3658. }
  3659. }
  3660. }
  3661. //-----------------------------------------------------------------------------
  3662. // Create In Hierarchy
  3663. //-----------------------------------------------------------------------------
  3664. class C_INIT_CreateInHierarchy : public CParticleInitializerOperatorInstance
  3665. {
  3666. DECLARE_PARTICLE_OPERATOR( C_INIT_CreateInHierarchy );
  3667. float m_fMaxDistance;
  3668. float m_flGrowthTime;
  3669. //float m_flTraceDist;
  3670. float m_flDesiredMidPoint;
  3671. int m_nOrientation;
  3672. float m_flBulgeFactor;
  3673. int m_nDesiredEndPoint;
  3674. int m_nDesiredStartPoint;
  3675. bool m_bUseHighestEndCP;
  3676. Vector m_vecDistanceBias, m_vecDistanceBiasAbs;
  3677. bool m_bDistanceBias, m_bDistanceBiasAbs;
  3678. uint32 GetWrittenAttributes( void ) const
  3679. {
  3680. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  3681. }
  3682. uint32 GetReadAttributes( void ) const
  3683. {
  3684. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  3685. }
  3686. virtual uint64 GetReadControlPointMask() const
  3687. {
  3688. uint64 nStartMask = ( 1ULL << m_nDesiredStartPoint ) - 1;
  3689. uint64 nEndMask = m_bUseHighestEndCP ? 0xFFFFFFFFFFFFFFFFll : ( 1ULL << ( m_nDesiredEndPoint + 1 ) ) - 1;
  3690. return nEndMask & (~nStartMask);
  3691. }
  3692. void InitParams( CParticleSystemDefinition *pDef )
  3693. {
  3694. //fixme - confirm CPs
  3695. // m_PathParams.ClampControlPointIndices();
  3696. m_bDistanceBias = ( m_vecDistanceBias.x != 1.0f ) || ( m_vecDistanceBias.y != 1.0f ) || ( m_vecDistanceBias.z != 1.0f );
  3697. m_bDistanceBiasAbs = ( m_vecDistanceBiasAbs.x != 0.0f ) || ( m_vecDistanceBiasAbs.y != 0.0f ) || ( m_vecDistanceBiasAbs.z != 0.0f );
  3698. }
  3699. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  3700. int nParticleCount, int nAttributeWriteMask,
  3701. void *pContext) const;
  3702. };
  3703. DEFINE_PARTICLE_OPERATOR( C_INIT_CreateInHierarchy, "Position In CP Hierarchy", OPERATOR_PI_POSITION );
  3704. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_CreateInHierarchy )
  3705. DMXELEMENT_UNPACK_FIELD( "maximum distance", "0", float, m_fMaxDistance )
  3706. DMXELEMENT_UNPACK_FIELD( "bulge", "0", float, m_flBulgeFactor )
  3707. DMXELEMENT_UNPACK_FIELD( "start control point number", "0", int, m_nDesiredStartPoint )
  3708. DMXELEMENT_UNPACK_FIELD( "end control point number", "1", int, m_nDesiredEndPoint )
  3709. DMXELEMENT_UNPACK_FIELD( "bulge control 0=random 1=orientation of start pnt 2=orientation of end point", "0", int, m_nOrientation )
  3710. DMXELEMENT_UNPACK_FIELD( "mid point position", "0.5", float, m_flDesiredMidPoint )
  3711. DMXELEMENT_UNPACK_FIELD( "growth time", "0.0", float, m_flGrowthTime )
  3712. //DMXELEMENT_UNPACK_FIELD( "trace distance for optional culling", "0.0", float, m_flTraceDist )
  3713. DMXELEMENT_UNPACK_FIELD( "use highest supplied end point", "0", bool, m_bUseHighestEndCP )
  3714. DMXELEMENT_UNPACK_FIELD( "distance_bias", "1 1 1", Vector, m_vecDistanceBias )
  3715. DMXELEMENT_UNPACK_FIELD( "distance_bias_absolute_value", "0 0 0", Vector, m_vecDistanceBiasAbs )
  3716. END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateInHierarchy )
  3717. void C_INIT_CreateInHierarchy::InitNewParticlesScalar(
  3718. CParticleCollection *pParticles, int start_p,
  3719. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  3720. {
  3721. int nEndCP;
  3722. float flGrowth;
  3723. struct CPathParameters PathParams;
  3724. PathParams.m_flBulge = m_flBulgeFactor;
  3725. PathParams.m_nBulgeControl = m_nOrientation;
  3726. PathParams.m_flMidPoint = m_flDesiredMidPoint;
  3727. int nRealEndPoint;
  3728. if ( m_bUseHighestEndCP )
  3729. {
  3730. nRealEndPoint = pParticles->GetHighestControlPoint();
  3731. }
  3732. else
  3733. {
  3734. nRealEndPoint = m_nDesiredEndPoint;
  3735. }
  3736. for( ; nParticleCount--; start_p++ )
  3737. {
  3738. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  3739. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  3740. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  3741. if ( ( pParticles->m_flCurTime <= m_flGrowthTime ) && ( nRealEndPoint > 0 ) )
  3742. {
  3743. float nCurrentEndCP = RemapValClamped( *ct, 0.0f, m_flGrowthTime, MIN( m_nDesiredStartPoint + 1, nRealEndPoint ), nRealEndPoint );
  3744. nEndCP = pParticles->RandomInt( MIN( m_nDesiredStartPoint + 1, nCurrentEndCP ), nCurrentEndCP );
  3745. // clamp growth to the appropriate values...
  3746. float flEndTime = float(nCurrentEndCP) / float(nRealEndPoint) ;
  3747. flGrowth = RemapValClamped( *ct, 0.0f, m_flGrowthTime, 0.0, flEndTime );
  3748. }
  3749. else
  3750. {
  3751. int nLowestStartPoint = MIN( m_nDesiredStartPoint + 1, nRealEndPoint );
  3752. nEndCP = pParticles->RandomInt( nLowestStartPoint, nRealEndPoint );
  3753. flGrowth = 1.0;
  3754. }
  3755. PathParams.m_nStartControlPointNumber = pParticles->ControlPoint( nEndCP ).m_nParent;
  3756. PathParams.m_nEndControlPointNumber = nEndCP;
  3757. Vector StartPnt, MidP, EndPnt;
  3758. pParticles->CalculatePathValues( PathParams, *ct, &StartPnt, &MidP, &EndPnt);
  3759. EndPnt *= flGrowth;
  3760. float t=pParticles->RandomFloat( 0.0, 1.0 );
  3761. Vector randpos;
  3762. pParticles->RandomVector( -m_fMaxDistance, m_fMaxDistance, &randpos );
  3763. if ( m_bDistanceBiasAbs )
  3764. {
  3765. if ( m_vecDistanceBiasAbs.x != 0.0f )
  3766. {
  3767. randpos.x = fabs(randpos.x);
  3768. }
  3769. if ( m_vecDistanceBiasAbs.y != 0.0f )
  3770. {
  3771. randpos.y = fabs(randpos.y);
  3772. }
  3773. if ( m_vecDistanceBiasAbs.z != 0.0f )
  3774. {
  3775. randpos.z = fabs(randpos.z);
  3776. }
  3777. }
  3778. randpos *= m_vecDistanceBias;
  3779. // form delta terms needed for quadratic bezier
  3780. Vector Delta0=MidP-StartPnt;
  3781. Vector Delta1 = EndPnt-MidP;
  3782. Vector L0 = StartPnt+t*Delta0;
  3783. Vector L1 = MidP+t*Delta1;
  3784. Vector Pnt = L0+(L1-L0)*t;
  3785. Pnt+=randpos;
  3786. // Optional Culling based on configurable trace distance. Failing particle are destroyed
  3787. //disabled for now.
  3788. //if ( m_flTraceDist != 0.0f )
  3789. //{
  3790. // // Trace down
  3791. // Vector TraceDir=Vector(0, 0, -1);
  3792. // // now set the trace distance
  3793. // // note - probably need to offset Pnt upwards for some fudge factor on irregular surfaces
  3794. // CBaseTrace tr;
  3795. // Vector RayStart=Pnt;
  3796. // float flRadius = m_flTraceDist;
  3797. // g_pParticleSystemMgr->Query()->TraceLine( RayStart, ( RayStart + ( TraceDir * flRadius ) ), MASK_SOLID, NULL, COLLISION_GROUP_NONE, &tr );
  3798. // if ( tr.fraction == 1.0 )
  3799. // {
  3800. // //If the trace hit nothing, kill the particle.
  3801. // pParticles->KillParticle( start_p );
  3802. // }
  3803. // else
  3804. // {
  3805. // //If we hit something, set particle position to collision position
  3806. // Pnt += tr.endpos;
  3807. // //FIXME - if we add a concept of a particle normal (for example, aligned quads or decals, set it here)
  3808. // }
  3809. //}
  3810. xyz[0] = Pnt.x;
  3811. xyz[4] = Pnt.y;
  3812. xyz[8] = Pnt.z;
  3813. if ( pxyz && ( nAttributeWriteMask & PARTICLE_ATTRIBUTE_PREV_XYZ_MASK ) )
  3814. {
  3815. pxyz[0] = Pnt.x;
  3816. pxyz[4] = Pnt.y;
  3817. pxyz[8] = Pnt.z;
  3818. }
  3819. }
  3820. }
  3821. //-----------------------------------------------------------------------------
  3822. // Remap initial Scalar to Vector Initializer
  3823. //-----------------------------------------------------------------------------
  3824. class C_INIT_RemapScalarToVector : public CParticleInitializerOperatorInstance
  3825. {
  3826. DECLARE_PARTICLE_OPERATOR( C_INIT_RemapScalarToVector );
  3827. uint32 GetWrittenAttributes( void ) const
  3828. {
  3829. return 1 << m_nFieldOutput | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  3830. }
  3831. uint32 GetReadAttributes( void ) const
  3832. {
  3833. return 1 << m_nFieldInput;
  3834. }
  3835. uint32 GetFilter( void ) const
  3836. {
  3837. return FILTER_PARAMETER_REMAPPING_MASK;
  3838. }
  3839. virtual uint64 GetReadControlPointMask() const
  3840. {
  3841. return 1ULL << m_nControlPointNumber;
  3842. }
  3843. bool InitMultipleOverride ( void ) { return true; }
  3844. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  3845. int nParticleCount, int nAttributeWriteMask,
  3846. void *pContext) const;
  3847. int m_nFieldInput;
  3848. int m_nFieldOutput;
  3849. float m_flInputMin;
  3850. float m_flInputMax;
  3851. Vector m_vecOutputMin;
  3852. Vector m_vecOutputMax;
  3853. float m_flStartTime;
  3854. float m_flEndTime;
  3855. bool m_bScaleInitialRange;
  3856. int m_nControlPointNumber;
  3857. bool m_bLocalCoords;
  3858. };
  3859. DEFINE_PARTICLE_OPERATOR( C_INIT_RemapScalarToVector, "Remap Scalar to Vector", OPERATOR_GENERIC );
  3860. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RemapScalarToVector )
  3861. DMXELEMENT_UNPACK_FIELD( "emitter lifetime start time (seconds)", "-1", float, m_flStartTime )
  3862. DMXELEMENT_UNPACK_FIELD( "emitter lifetime end time (seconds)", "-1", float, m_flEndTime )
  3863. DMXELEMENT_UNPACK_FIELD_USERDATA( "input field", "8", int, m_nFieldInput, "intchoice particlefield_scalar" )
  3864. DMXELEMENT_UNPACK_FIELD( "input minimum","0", float, m_flInputMin )
  3865. DMXELEMENT_UNPACK_FIELD( "input maximum","1", float, m_flInputMax )
  3866. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "0", int, m_nFieldOutput, "intchoice particlefield_vector" )
  3867. DMXELEMENT_UNPACK_FIELD( "output minimum","0 0 0", Vector, m_vecOutputMin )
  3868. DMXELEMENT_UNPACK_FIELD( "output maximum","1 1 1", Vector, m_vecOutputMax )
  3869. DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
  3870. DMXELEMENT_UNPACK_FIELD( "use local system", "1", bool, m_bLocalCoords )
  3871. DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
  3872. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapScalarToVector )
  3873. void C_INIT_RemapScalarToVector::InitNewParticlesScalar(
  3874. CParticleCollection *pParticles, int start_p,
  3875. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  3876. {
  3877. const float *pCreationTime;
  3878. // FIXME: SSE-ize
  3879. for( ; nParticleCount--; start_p++ )
  3880. {
  3881. pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  3882. // using raw creation time to map to emitter lifespan
  3883. float flLifeTime = *pCreationTime;
  3884. // only use within start/end time frame
  3885. if ( ( ( flLifeTime < m_flStartTime ) || ( flLifeTime >= m_flEndTime ) ) && ( ( m_flStartTime != -1.0f) && ( m_flEndTime != -1.0f) ) )
  3886. continue;
  3887. const float *pInput = pParticles->GetFloatAttributePtr( m_nFieldInput, start_p );
  3888. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  3889. Vector vecOutput = vec3_origin;
  3890. vecOutput.x = RemapValClamped( *pInput, m_flInputMin, m_flInputMax, m_vecOutputMin.x, m_vecOutputMax.x );
  3891. vecOutput.y = RemapValClamped( *pInput, m_flInputMin, m_flInputMax, m_vecOutputMin.y, m_vecOutputMax.y );
  3892. vecOutput.z = RemapValClamped( *pInput, m_flInputMin, m_flInputMax, m_vecOutputMin.z, m_vecOutputMax.z );
  3893. if ( m_nFieldOutput == 0 )
  3894. {
  3895. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  3896. if ( !m_bLocalCoords )
  3897. {
  3898. Vector vecControlPoint;
  3899. pParticles->GetControlPointAtTime( m_nControlPointNumber, *pCreationTime, &vecControlPoint );
  3900. vecOutput += vecControlPoint;
  3901. Vector vecOutputPrev = vecOutput;
  3902. if ( m_bScaleInitialRange )
  3903. {
  3904. Vector vecScaleInitial;
  3905. Vector vecScaleInitialPrev;
  3906. SetVectorFromAttribute ( vecScaleInitial, pOutput );
  3907. SetVectorFromAttribute ( vecScaleInitialPrev, pxyz );
  3908. vecOutput *= vecScaleInitial;
  3909. vecOutputPrev *= vecScaleInitialPrev;
  3910. }
  3911. SetVectorAttribute( pOutput, vecOutput );
  3912. SetVectorAttribute( pxyz, vecOutputPrev );
  3913. }
  3914. else
  3915. {
  3916. matrix3x4_t mat;
  3917. pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, *pCreationTime, &mat );
  3918. Vector vecTransformLocal = vec3_origin;
  3919. VectorTransform( vecOutput, mat, vecTransformLocal );
  3920. vecOutput = vecTransformLocal;
  3921. Vector vecOutputPrev = vecOutput;
  3922. if ( m_bScaleInitialRange )
  3923. {
  3924. Vector vecScaleInitial;
  3925. Vector vecScaleInitialPrev;
  3926. SetVectorFromAttribute ( vecScaleInitial, pOutput );
  3927. SetVectorFromAttribute ( vecScaleInitialPrev, pxyz );
  3928. vecOutput *= vecScaleInitial;
  3929. vecOutputPrev *= vecScaleInitialPrev;
  3930. }
  3931. SetVectorAttribute( pOutput, vecOutput );
  3932. SetVectorAttribute( pxyz, vecOutput );
  3933. }
  3934. }
  3935. else
  3936. {
  3937. if ( m_bScaleInitialRange )
  3938. {
  3939. Vector vecScaleInitial;
  3940. SetVectorFromAttribute ( vecScaleInitial, pOutput );
  3941. vecOutput *= vecScaleInitial;
  3942. }
  3943. SetVectorAttribute( pOutput, vecOutput );
  3944. }
  3945. }
  3946. }
  3947. //-----------------------------------------------------------------------------
  3948. // Offset Vector to Vector Initializer
  3949. //-----------------------------------------------------------------------------
  3950. class C_INIT_OffsetVectorToVector : public CParticleInitializerOperatorInstance
  3951. {
  3952. DECLARE_PARTICLE_OPERATOR( C_INIT_OffsetVectorToVector );
  3953. uint32 GetWrittenAttributes( void ) const
  3954. {
  3955. return 1 << m_nFieldOutput;
  3956. }
  3957. uint32 GetReadAttributes( void ) const
  3958. {
  3959. return 1 << m_nFieldInput;
  3960. }
  3961. uint32 GetFilter( void ) const
  3962. {
  3963. return FILTER_PARAMETER_REMAPPING_MASK;
  3964. }
  3965. virtual uint64 GetReadControlPointMask() const
  3966. {
  3967. return 1ULL << m_nControlPointNumber;
  3968. }
  3969. bool InitMultipleOverride ( void ) { return true; }
  3970. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  3971. int nParticleCount, int nAttributeWriteMask,
  3972. void *pContext) const;
  3973. int m_nFieldInput;
  3974. int m_nFieldOutput;
  3975. float m_flInputMin;
  3976. float m_flInputMax;
  3977. Vector m_vecOutputMin;
  3978. Vector m_vecOutputMax;
  3979. float m_flStartTime;
  3980. float m_flEndTime;
  3981. int m_nControlPointNumber;
  3982. };
  3983. DEFINE_PARTICLE_OPERATOR( C_INIT_OffsetVectorToVector, "Offset Vector to Vector", OPERATOR_GENERIC );
  3984. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_OffsetVectorToVector )
  3985. DMXELEMENT_UNPACK_FIELD_USERDATA( "input field", "0", int, m_nFieldInput, "intchoice particlefield_vector" )
  3986. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "0", int, m_nFieldOutput, "intchoice particlefield_vector" )
  3987. DMXELEMENT_UNPACK_FIELD( "output offset minimum","0 0 0", Vector, m_vecOutputMin )
  3988. DMXELEMENT_UNPACK_FIELD( "output offset maximum","1 1 1", Vector, m_vecOutputMax )
  3989. END_PARTICLE_OPERATOR_UNPACK( C_INIT_OffsetVectorToVector )
  3990. void C_INIT_OffsetVectorToVector::InitNewParticlesScalar(
  3991. CParticleCollection *pParticles, int start_p,
  3992. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  3993. {
  3994. // FIXME: SSE-ize
  3995. for( ; nParticleCount--; start_p++ )
  3996. {
  3997. const float *pInput = pParticles->GetFloatAttributePtr( m_nFieldInput, start_p );
  3998. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  3999. Vector vecInput;
  4000. SetVectorFromAttribute( vecInput, pInput );
  4001. Vector vecOutput;
  4002. pParticles->RandomVector( m_vecOutputMin, m_vecOutputMax, &vecOutput );
  4003. vecOutput += vecInput;
  4004. SetVectorAttribute( pOutput, vecOutput );
  4005. }
  4006. }
  4007. //-----------------------------------------------------------------------------
  4008. // Create particles sequentially along a path
  4009. //-----------------------------------------------------------------------------
  4010. struct SequentialPathContext_t
  4011. {
  4012. int m_nCPCountAmount;
  4013. float m_flParticleCount;
  4014. float m_flPerSegmentAmount;
  4015. float m_flCPCount;
  4016. float m_flStep;
  4017. float m_flCPStep;
  4018. };
  4019. class C_INIT_CreateSequentialPath : public CParticleInitializerOperatorInstance
  4020. {
  4021. DECLARE_PARTICLE_OPERATOR( C_INIT_CreateSequentialPath );
  4022. float m_fMaxDistance;
  4023. float m_flNumToAssign;
  4024. bool m_bLoop;
  4025. bool m_bCPPairs;
  4026. bool m_bSaveOffset;
  4027. struct CPathParameters m_PathParams;
  4028. uint32 GetWrittenAttributes( void ) const
  4029. {
  4030. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ_MASK ;
  4031. }
  4032. uint32 GetReadAttributes( void ) const
  4033. {
  4034. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  4035. }
  4036. virtual uint64 GetReadControlPointMask() const
  4037. {
  4038. uint64 nStartMask = ( 1ULL << m_PathParams.m_nStartControlPointNumber ) - 1;
  4039. uint64 nEndMask = m_bCPPairs ? 0xFFFFFFFFFFFFFFFFULL : ( 1ULL << ( m_PathParams.m_nEndControlPointNumber + 1 ) ) - 1;
  4040. return nEndMask & (~nStartMask);
  4041. }
  4042. virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
  4043. {
  4044. SequentialPathContext_t *pCtx = reinterpret_cast<SequentialPathContext_t *>( pContext );
  4045. pCtx->m_flParticleCount = 0;
  4046. if ( m_flNumToAssign > 1.0f )
  4047. {
  4048. pCtx->m_flStep = 1.0f / ( m_flNumToAssign - 1 );
  4049. }
  4050. else
  4051. {
  4052. pCtx->m_flStep = 0.0f;
  4053. }
  4054. pCtx->m_flPerSegmentAmount = -1;
  4055. if ( m_bCPPairs )
  4056. {
  4057. pCtx->m_flCPCount = m_PathParams.m_nStartControlPointNumber;
  4058. if ( m_PathParams.m_nEndControlPointNumber - m_PathParams.m_nStartControlPointNumber > 1 && m_flNumToAssign > 1.0f )
  4059. {
  4060. pCtx->m_flStep *= m_PathParams.m_nEndControlPointNumber - m_PathParams.m_nStartControlPointNumber;
  4061. pCtx->m_flCPStep = pCtx->m_flStep ;
  4062. }
  4063. else
  4064. {
  4065. pCtx->m_flCPStep = 0.0f;
  4066. }
  4067. }
  4068. }
  4069. virtual void Restart( CParticleCollection *pParticles, void *pContext )
  4070. {
  4071. SequentialPathContext_t *pCtx = reinterpret_cast<SequentialPathContext_t *>( pContext );
  4072. pCtx->m_flParticleCount = 0;
  4073. if ( m_flNumToAssign > 1.0f )
  4074. {
  4075. pCtx->m_flStep = 1.0f / ( m_flNumToAssign - 1 );
  4076. }
  4077. else
  4078. {
  4079. pCtx->m_flStep = 0.0f;
  4080. }
  4081. pCtx->m_flPerSegmentAmount = -1;
  4082. if ( m_bCPPairs )
  4083. {
  4084. pCtx->m_flCPCount = m_PathParams.m_nStartControlPointNumber;
  4085. if ( m_PathParams.m_nEndControlPointNumber - m_PathParams.m_nStartControlPointNumber > 1 && m_flNumToAssign > 1.0f )
  4086. {
  4087. pCtx->m_flStep *= m_PathParams.m_nEndControlPointNumber - m_PathParams.m_nStartControlPointNumber;
  4088. pCtx->m_flCPStep = pCtx->m_flStep ;
  4089. }
  4090. else
  4091. {
  4092. pCtx->m_flCPStep = 0.0f;
  4093. }
  4094. }
  4095. }
  4096. void InitParams( CParticleSystemDefinition *pDef )
  4097. {
  4098. m_PathParams.ClampControlPointIndices();
  4099. }
  4100. size_t GetRequiredContextBytes( void ) const
  4101. {
  4102. return sizeof( SequentialPathContext_t );
  4103. }
  4104. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  4105. int nParticleCount, int nAttributeWriteMask,
  4106. void *pContext) const;
  4107. };
  4108. DEFINE_PARTICLE_OPERATOR( C_INIT_CreateSequentialPath, "Position Along Path Sequential", OPERATOR_PI_POSITION );
  4109. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_CreateSequentialPath )
  4110. DMXELEMENT_UNPACK_FIELD( "maximum distance", "0", float, m_fMaxDistance )
  4111. DMXELEMENT_UNPACK_FIELD( "bulge", "0", float, m_PathParams.m_flBulge )
  4112. DMXELEMENT_UNPACK_FIELD( "start control point number", "0", int, m_PathParams.m_nStartControlPointNumber )
  4113. DMXELEMENT_UNPACK_FIELD( "end control point number", "0", int, m_PathParams.m_nEndControlPointNumber )
  4114. DMXELEMENT_UNPACK_FIELD( "bulge control 0=random 1=orientation of start pnt 2=orientation of end point", "0", int, m_PathParams.m_nBulgeControl )
  4115. DMXELEMENT_UNPACK_FIELD( "mid point position", "0.5", float, m_PathParams.m_flMidPoint )
  4116. DMXELEMENT_UNPACK_FIELD( "particles to map from start to end", "100", float, m_flNumToAssign )
  4117. DMXELEMENT_UNPACK_FIELD( "restart behavior (0 = bounce, 1 = loop )", "1", bool, m_bLoop )
  4118. DMXELEMENT_UNPACK_FIELD( "Use sequential CP pairs between start and end point", "0", bool, m_bCPPairs )
  4119. DMXELEMENT_UNPACK_FIELD( "Save Offset", "0", bool, m_bSaveOffset )
  4120. END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateSequentialPath )
  4121. void C_INIT_CreateSequentialPath::InitNewParticlesScalar(
  4122. CParticleCollection *pParticles, int start_p,
  4123. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  4124. {
  4125. // NOTE: Using C_OP_ContinuousEmitter:: avoids a virtual function call
  4126. SequentialPathContext_t *pCtx = reinterpret_cast<SequentialPathContext_t *>( pContext );
  4127. for( ; nParticleCount--; start_p++ )
  4128. {
  4129. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  4130. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  4131. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  4132. struct CPathParameters PathParams = m_PathParams;
  4133. int nTotalCPCount = 1;
  4134. if ( m_bCPPairs )
  4135. {
  4136. nTotalCPCount = m_PathParams.m_nEndControlPointNumber - m_PathParams.m_nStartControlPointNumber;
  4137. if ( pCtx->m_flCPCount > nTotalCPCount || pCtx->m_flCPCount < m_PathParams.m_nStartControlPointNumber )
  4138. {
  4139. if ( m_bLoop )
  4140. {
  4141. pCtx->m_flCPCount -= nTotalCPCount;
  4142. }
  4143. else
  4144. {
  4145. //Flip CP step direction.
  4146. pCtx->m_flCPStep *= -1;
  4147. pCtx->m_flCPCount += 2 * pCtx->m_flCPStep;
  4148. // We flip individual particle step here because we've hit the end of the CP line.
  4149. pCtx->m_flStep *= -1;
  4150. pCtx->m_flPerSegmentAmount *= -1;
  4151. pCtx->m_flParticleCount += 2 * pCtx->m_flStep;
  4152. }
  4153. }
  4154. PathParams.m_nStartControlPointNumber = int ( pCtx->m_flCPCount );
  4155. PathParams.m_nEndControlPointNumber = PathParams.m_nStartControlPointNumber + 1;
  4156. }
  4157. if ( pCtx->m_flParticleCount > 1 + FLT_EPSILON || pCtx->m_flParticleCount < 0 )
  4158. {
  4159. if ( m_bLoop )
  4160. {
  4161. pCtx->m_flParticleCount += pCtx->m_flPerSegmentAmount;
  4162. }
  4163. else
  4164. {
  4165. if ( !m_bCPPairs ) // We're doing an 2 specific CPs and bouncing
  4166. {
  4167. pCtx->m_flStep *= -1;
  4168. pCtx->m_flPerSegmentAmount *= -1;
  4169. pCtx->m_flParticleCount += 2 * pCtx->m_flStep;
  4170. }
  4171. else // Multiple CP sets require bouncing to 0 or 1 + the remaining step amount.
  4172. {
  4173. pCtx->m_flParticleCount += pCtx->m_flPerSegmentAmount;
  4174. }
  4175. }
  4176. }
  4177. float t = pCtx->m_flParticleCount;
  4178. Vector StartPnt, MidP, EndPnt;
  4179. pParticles->CalculatePathValues( PathParams, *ct, &StartPnt, &MidP, &EndPnt);
  4180. Vector randpos;
  4181. pParticles->RandomVector( -m_fMaxDistance, m_fMaxDistance, &randpos );
  4182. // form delta terms needed for quadratic bezier
  4183. Vector Delta0=MidP-StartPnt;
  4184. Vector Delta1 = EndPnt-MidP;
  4185. Vector L0 = StartPnt+t*Delta0;
  4186. Vector L1 = MidP+t*Delta1;
  4187. Vector Pnt = L0+(L1-L0)*t;
  4188. Pnt+=randpos;
  4189. xyz[0] = Pnt.x;
  4190. xyz[4] = Pnt.y;
  4191. xyz[8] = Pnt.z;
  4192. pxyz[0] = Pnt.x;
  4193. pxyz[4] = Pnt.y;
  4194. pxyz[8] = Pnt.z;
  4195. if ( m_bSaveOffset )
  4196. {
  4197. float *pSavedPos = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_HITBOX_RELATIVE_XYZ, start_p );
  4198. Vector vecSavedPos = Vector( t, PathParams.m_nStartControlPointNumber, PathParams.m_nEndControlPointNumber );
  4199. SetVectorAttribute( pSavedPos, vecSavedPos );
  4200. }
  4201. pCtx->m_flParticleCount += pCtx->m_flStep;
  4202. pCtx->m_flCPCount += pCtx->m_flCPStep;
  4203. }
  4204. }
  4205. //-----------------------------------------------------------------------------
  4206. // Initial Repulsion Velocity - repulses the particles from nearby surfaces
  4207. // on spawn
  4208. //-----------------------------------------------------------------------------
  4209. class C_INIT_InitialRepulsionVelocity : public CParticleInitializerOperatorInstance
  4210. {
  4211. DECLARE_PARTICLE_OPERATOR( C_INIT_InitialRepulsionVelocity );
  4212. uint32 GetWrittenAttributes( void ) const
  4213. {
  4214. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  4215. }
  4216. uint32 GetReadAttributes( void ) const
  4217. {
  4218. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK | PARTICLE_ATTRIBUTE_RADIUS_MASK;
  4219. }
  4220. virtual uint64 GetReadControlPointMask() const
  4221. {
  4222. if ( m_bPerParticle )
  4223. return 0;
  4224. if ( m_bInherit )
  4225. return ( 1ULL << m_nControlPointNumber ) | ( 1ULL << ( m_nControlPointNumber + 1 ) );
  4226. return 1ULL << m_nControlPointNumber;
  4227. }
  4228. virtual uint64 GetNonPositionalControlPointMask() const
  4229. {
  4230. if ( m_bPerParticle || !m_bInherit )
  4231. return 0;
  4232. return ( 1ULL << m_nControlPointNumber ) | ( 1ULL << ( m_nControlPointNumber + 1 ) );
  4233. }
  4234. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  4235. int nParticleCount, int nAttributeWriteMask,
  4236. void *pContext) const;
  4237. void InitParams( CParticleSystemDefinition *pDef )
  4238. {
  4239. m_nCollisionGroupNumber = g_pParticleSystemMgr->Query()->GetCollisionGroupFromName( m_CollisionGroupName );
  4240. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  4241. }
  4242. bool InitMultipleOverride ( void ) { return true; }
  4243. char m_CollisionGroupName[128];
  4244. int m_nCollisionGroupNumber;
  4245. Vector m_vecOutputMin;
  4246. Vector m_vecOutputMax;
  4247. int nRemainingBlocks;
  4248. int m_nControlPointNumber;
  4249. bool m_bPerParticle;
  4250. bool m_bTranslate;
  4251. bool m_bProportional;
  4252. float m_flTraceLength;
  4253. bool m_bPerParticleTR;
  4254. bool m_bInherit;
  4255. int m_nChildCP;
  4256. int m_nChildGroupID;
  4257. };
  4258. DEFINE_PARTICLE_OPERATOR( C_INIT_InitialRepulsionVelocity, "Velocity Repulse from World", OPERATOR_GENERIC );
  4259. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_InitialRepulsionVelocity )
  4260. DMXELEMENT_UNPACK_FIELD( "minimum velocity","0 0 0", Vector, m_vecOutputMin )
  4261. DMXELEMENT_UNPACK_FIELD( "maximum velocity","1 1 1", Vector, m_vecOutputMax )
  4262. DMXELEMENT_UNPACK_FIELD_STRING( "collision group", "NONE", m_CollisionGroupName )
  4263. DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
  4264. DMXELEMENT_UNPACK_FIELD( "Per Particle World Collision Tests", "0", bool, m_bPerParticle )
  4265. DMXELEMENT_UNPACK_FIELD( "Use radius for Per Particle Trace Length", "0", bool, m_bPerParticleTR )
  4266. DMXELEMENT_UNPACK_FIELD( "Offset instead of accelerate", "0", bool, m_bTranslate )
  4267. DMXELEMENT_UNPACK_FIELD( "Offset proportional to radius 0/1", "0", bool, m_bProportional )
  4268. DMXELEMENT_UNPACK_FIELD( "Trace Length", "64.0", float, m_flTraceLength )
  4269. DMXELEMENT_UNPACK_FIELD( "Inherit from Parent", "0", bool, m_bInherit )
  4270. DMXELEMENT_UNPACK_FIELD( "control points to broadcast to children (n + 1)", "-1", int, m_nChildCP )
  4271. DMXELEMENT_UNPACK_FIELD( "Child Group ID to affect", "0", int, m_nChildGroupID )
  4272. END_PARTICLE_OPERATOR_UNPACK( C_INIT_InitialRepulsionVelocity );
  4273. void C_INIT_InitialRepulsionVelocity::InitNewParticlesScalar(
  4274. CParticleCollection *pParticles, int start_p,
  4275. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  4276. {
  4277. Vector d[6];
  4278. //All cardinal directions
  4279. d[0] = Vector( 1, 0, 0 );
  4280. d[1] = Vector( -1, 0, 0 );
  4281. d[2] = Vector( 0, 1, 0 );
  4282. d[3] = Vector( 0, -1, 0 );
  4283. d[4] = Vector( 0, 0, 1 );
  4284. d[5] = Vector( 0, 0, -1 );
  4285. //Init the results
  4286. Vector resultDirection;
  4287. float resultForce;
  4288. if ( m_bPerParticle )
  4289. {
  4290. for( ; nParticleCount--; start_p++ )
  4291. {
  4292. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  4293. float *pxyz_prev = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  4294. const float *radius = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_RADIUS, start_p );
  4295. Vector vecCurrentPos;
  4296. SetVectorFromAttribute( vecCurrentPos, pxyz );
  4297. resultDirection.Init();
  4298. resultForce = 0.0f;
  4299. //Get the aggregate force vector
  4300. for ( int i = 0; i < 6; i++ )
  4301. {
  4302. //Press out
  4303. float flTraceDistance = m_flTraceLength;
  4304. if ( m_bPerParticleTR )
  4305. {
  4306. flTraceDistance = *radius;
  4307. }
  4308. Vector endpos = vecCurrentPos + ( d[i] * flTraceDistance );
  4309. //Trace into the world
  4310. CBaseTrace tr;
  4311. g_pParticleSystemMgr->Query()->TraceLine( vecCurrentPos, endpos, CONTENTS_SOLID, NULL, m_nCollisionGroupNumber, &tr );
  4312. //Push back a proportional amount to the probe
  4313. d[i] = -d[i] * (1.0f-tr.fraction);
  4314. assert(( 1.0f - tr.fraction ) >= 0.0f );
  4315. resultForce += 1.0f-tr.fraction;
  4316. resultDirection += d[i];
  4317. }
  4318. //If we've hit nothing, then point up
  4319. if ( resultDirection == vec3_origin )
  4320. {
  4321. resultDirection = Vector( 0, 0, 1 );
  4322. resultForce = 0.0f;
  4323. }
  4324. //Just return the direction
  4325. VectorNormalize( resultDirection );
  4326. resultDirection *= resultForce;
  4327. Vector vecRepulsionAmount;
  4328. vecRepulsionAmount.x = Lerp( resultForce, m_vecOutputMin.x, m_vecOutputMax.x );
  4329. vecRepulsionAmount.y = Lerp( resultForce, m_vecOutputMin.y, m_vecOutputMax.y );
  4330. vecRepulsionAmount.z = Lerp( resultForce, m_vecOutputMin.z, m_vecOutputMax.z );
  4331. vecRepulsionAmount *= resultDirection;
  4332. if ( m_bProportional )
  4333. {
  4334. vecRepulsionAmount *= *radius;
  4335. }
  4336. pxyz[0] += vecRepulsionAmount.x;
  4337. pxyz[4] += vecRepulsionAmount.y;
  4338. pxyz[8] += vecRepulsionAmount.z;
  4339. if ( m_bTranslate )
  4340. {
  4341. pxyz_prev[0] += vecRepulsionAmount.x;
  4342. pxyz_prev[4] += vecRepulsionAmount.y;
  4343. pxyz_prev[8] += vecRepulsionAmount.z;
  4344. }
  4345. }
  4346. }
  4347. else
  4348. {
  4349. Vector vecRepulsionAmount;
  4350. if ( m_bInherit )
  4351. {
  4352. float *ct = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  4353. pParticles->GetControlPointAtTime( m_nControlPointNumber, *ct, &resultDirection );
  4354. Vector vecPassedForce;
  4355. pParticles->GetControlPointAtTime( m_nControlPointNumber+1, *ct, &vecPassedForce );
  4356. vecRepulsionAmount.x = Lerp( vecPassedForce.x, m_vecOutputMin.x, m_vecOutputMax.x );
  4357. vecRepulsionAmount.y = Lerp( vecPassedForce.x, m_vecOutputMin.y, m_vecOutputMax.y );
  4358. vecRepulsionAmount.z = Lerp( vecPassedForce.x, m_vecOutputMin.z, m_vecOutputMax.z );
  4359. vecRepulsionAmount *= resultDirection;
  4360. }
  4361. else
  4362. {
  4363. float *ct = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  4364. Vector vecControlPoint;
  4365. pParticles->GetControlPointAtTime( m_nControlPointNumber, *ct, &vecControlPoint );
  4366. Vector vecCurrentPos = vecControlPoint;
  4367. resultDirection.Init();
  4368. resultForce = 0.0f;
  4369. //Get the aggregate force vector
  4370. for ( int i = 0; i < 6; i++ )
  4371. {
  4372. //Press out
  4373. Vector endpos = vecCurrentPos + ( d[i] * m_flTraceLength );
  4374. //Trace into the world
  4375. CBaseTrace tr;
  4376. g_pParticleSystemMgr->Query()->TraceLine( vecCurrentPos, endpos, CONTENTS_SOLID, NULL, m_nCollisionGroupNumber, &tr );
  4377. //Push back a proportional amount to the probe
  4378. d[i] = -d[i] * (1.0f-tr.fraction);
  4379. assert(( 1.0f - tr.fraction ) >= 0.0f );
  4380. resultForce += 1.0f-tr.fraction;
  4381. resultDirection += d[i];
  4382. }
  4383. //If we've hit nothing, then point up
  4384. if ( resultDirection == vec3_origin )
  4385. {
  4386. resultDirection = Vector( 0, 0, 1 );
  4387. resultForce = 0.0f;
  4388. }
  4389. //Just return the direction
  4390. VectorNormalize( resultDirection );
  4391. resultDirection *= resultForce;
  4392. vecRepulsionAmount.x = Lerp( resultForce, m_vecOutputMin.x, m_vecOutputMax.x );
  4393. vecRepulsionAmount.y = Lerp( resultForce, m_vecOutputMin.y, m_vecOutputMax.y );
  4394. vecRepulsionAmount.z = Lerp( resultForce, m_vecOutputMin.z, m_vecOutputMax.z );
  4395. vecRepulsionAmount *= resultDirection;
  4396. if ( m_nChildCP != -1 )
  4397. {
  4398. for( CParticleCollection *pChild = pParticles->m_Children.m_pHead; pChild; pChild = pChild->m_pNext )
  4399. {
  4400. if ( pChild->GetGroupID() == m_nChildGroupID )
  4401. {
  4402. Vector vecPassForce = Vector(resultForce, 0, 0);
  4403. pChild->SetControlPoint( m_nChildCP, resultDirection );
  4404. pChild->SetControlPoint( m_nChildCP+1, vecPassForce );
  4405. }
  4406. }
  4407. }
  4408. }
  4409. for( ; nParticleCount--; start_p++ )
  4410. {
  4411. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  4412. float *pxyz_prev = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  4413. const float *radius = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_RADIUS, start_p );
  4414. if ( m_bProportional )
  4415. {
  4416. vecRepulsionAmount *= *radius;
  4417. }
  4418. pxyz[0] += vecRepulsionAmount.x;
  4419. pxyz[4] += vecRepulsionAmount.y;
  4420. pxyz[8] += vecRepulsionAmount.z;
  4421. if ( m_bTranslate )
  4422. {
  4423. pxyz_prev[0] += vecRepulsionAmount.x;
  4424. pxyz_prev[4] += vecRepulsionAmount.y;
  4425. pxyz_prev[8] += vecRepulsionAmount.z;
  4426. }
  4427. }
  4428. }
  4429. }
  4430. //-----------------------------------------------------------------------------
  4431. // Random Yaw Flip
  4432. //-----------------------------------------------------------------------------
  4433. class C_INIT_RandomYawFlip : public CParticleInitializerOperatorInstance
  4434. {
  4435. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomYawFlip );
  4436. uint32 GetWrittenAttributes( void ) const
  4437. {
  4438. return PARTICLE_ATTRIBUTE_YAW_MASK;
  4439. }
  4440. uint32 GetReadAttributes( void ) const
  4441. {
  4442. return PARTICLE_ATTRIBUTE_YAW_MASK;
  4443. }
  4444. bool InitMultipleOverride ( void ) { return true; }
  4445. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  4446. int nParticleCount, int nAttributeWriteMask, void *pContext ) const;
  4447. float m_flPercent;
  4448. };
  4449. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomYawFlip, "Rotation Yaw Flip Random", OPERATOR_GENERIC );
  4450. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomYawFlip )
  4451. DMXELEMENT_UNPACK_FIELD( "Flip Percentage", ".5", float, m_flPercent )
  4452. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomYawFlip )
  4453. void C_INIT_RandomYawFlip::InitNewParticlesScalar(
  4454. CParticleCollection *pParticles, int start_p,
  4455. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  4456. {
  4457. for( ; nParticleCount--; start_p++ )
  4458. {
  4459. float flChance = pParticles->RandomFloat( 0.0, 1.0 );
  4460. if ( flChance < m_flPercent )
  4461. {
  4462. float *drot = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_YAW, start_p );
  4463. float flRotation = fmodf( *drot , M_PI );
  4464. *drot = M_PI + flRotation;
  4465. }
  4466. }
  4467. }
  4468. //-----------------------------------------------------------------------------
  4469. // Random second sequence
  4470. //-----------------------------------------------------------------------------
  4471. class C_INIT_RandomSecondSequence : public CParticleInitializerOperatorInstance
  4472. {
  4473. DECLARE_PARTICLE_OPERATOR( C_INIT_RandomSecondSequence );
  4474. uint32 GetWrittenAttributes( void ) const
  4475. {
  4476. return PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER1_MASK;
  4477. }
  4478. uint32 GetReadAttributes( void ) const
  4479. {
  4480. return 0;
  4481. }
  4482. virtual void InitParams( CParticleSystemDefinition *pDef )
  4483. {
  4484. // TODO: Validate the ranges here!
  4485. }
  4486. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p, int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  4487. {
  4488. float *pSequence;
  4489. for( ; nParticleCount--; start_p++ )
  4490. {
  4491. pSequence = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_SEQUENCE_NUMBER1, start_p );
  4492. *pSequence = pParticles->RandomInt( m_nSequenceMin, m_nSequenceMax );
  4493. }
  4494. }
  4495. int m_nSequenceMin;
  4496. int m_nSequenceMax;
  4497. };
  4498. DEFINE_PARTICLE_OPERATOR( C_INIT_RandomSecondSequence, "Sequence Two Random", OPERATOR_GENERIC );
  4499. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RandomSecondSequence )
  4500. DMXELEMENT_UNPACK_FIELD_USERDATA( "sequence_min", "0", int, m_nSequenceMin, "sheetsequencepicker_second" )
  4501. DMXELEMENT_UNPACK_FIELD_USERDATA( "sequence_max", "0", int, m_nSequenceMax, "sheetsequencepicker_second" )
  4502. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RandomSecondSequence )
  4503. //-----------------------------------------------------------------------------
  4504. // Remap CP to Scalar Initializer
  4505. //-----------------------------------------------------------------------------
  4506. class C_INIT_RemapCPtoScalar : public CParticleInitializerOperatorInstance
  4507. {
  4508. DECLARE_PARTICLE_OPERATOR( C_INIT_RemapCPtoScalar );
  4509. uint32 GetWrittenAttributes( void ) const
  4510. {
  4511. return 1 << m_nFieldOutput;
  4512. }
  4513. uint32 GetReadAttributes( void ) const
  4514. {
  4515. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  4516. }
  4517. uint32 GetFilter( void ) const
  4518. {
  4519. return FILTER_PARAMETER_REMAPPING_MASK;
  4520. }
  4521. virtual uint64 GetReadControlPointMask() const
  4522. {
  4523. return 1ULL << m_nCPInput;
  4524. }
  4525. virtual uint64 GetNonPositionalControlPointMask() const
  4526. {
  4527. return 1ULL << m_nCPInput;
  4528. }
  4529. bool InitMultipleOverride ( void ) { return true; }
  4530. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  4531. int nParticleCount, int nAttributeWriteMask,
  4532. void *pContext) const;
  4533. virtual void InitParams( CParticleSystemDefinition *pDef )
  4534. {
  4535. m_nField = int (clamp (m_nField, 0, 2));
  4536. // clamp the result to 0 and 1 if it's alpha
  4537. if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & ( 1 << m_nFieldOutput ) )
  4538. {
  4539. m_flOutputMin = clamp(m_flOutputMin, 0.0f, 1.0f );
  4540. m_flOutputMax = clamp(m_flOutputMax, 0.0f, 1.0f );
  4541. }
  4542. }
  4543. int m_nCPInput;
  4544. int m_nFieldOutput;
  4545. int m_nField;
  4546. float m_flInputMin;
  4547. float m_flInputMax;
  4548. float m_flOutputMin;
  4549. float m_flOutputMax;
  4550. float m_flStartTime;
  4551. float m_flEndTime;
  4552. bool m_bScaleInitialRange;
  4553. };
  4554. DEFINE_PARTICLE_OPERATOR( C_INIT_RemapCPtoScalar, "Remap Control Point to Scalar", OPERATOR_GENERIC );
  4555. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RemapCPtoScalar )
  4556. DMXELEMENT_UNPACK_FIELD( "emitter lifetime start time (seconds)", "-1", float, m_flStartTime )
  4557. DMXELEMENT_UNPACK_FIELD( "emitter lifetime end time (seconds)", "-1", float, m_flEndTime )
  4558. DMXELEMENT_UNPACK_FIELD( "input control point number", "0", int, m_nCPInput )
  4559. DMXELEMENT_UNPACK_FIELD( "input minimum","0", float, m_flInputMin )
  4560. DMXELEMENT_UNPACK_FIELD( "input maximum","1", float, m_flInputMax )
  4561. DMXELEMENT_UNPACK_FIELD( "input field 0-2 X/Y/Z","0", int, m_nField )
  4562. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "3", int, m_nFieldOutput, "intchoice particlefield_scalar" )
  4563. DMXELEMENT_UNPACK_FIELD( "output minimum","0", float, m_flOutputMin )
  4564. DMXELEMENT_UNPACK_FIELD( "output maximum","1", float, m_flOutputMax )
  4565. DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
  4566. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapCPtoScalar )
  4567. void C_INIT_RemapCPtoScalar::InitNewParticlesScalar(
  4568. CParticleCollection *pParticles, int start_p,
  4569. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  4570. {
  4571. const float *pCreationTime;
  4572. Vector vecControlPoint;
  4573. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  4574. pParticles->GetControlPointAtTime( m_nCPInput, *ct, &vecControlPoint );
  4575. float flInput = vecControlPoint[m_nField];
  4576. // FIXME: SSE-ize
  4577. for( ; nParticleCount--; start_p++ )
  4578. {
  4579. pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  4580. // using raw creation time to map to emitter lifespan
  4581. float flLifeTime = *pCreationTime;
  4582. // only use within start/end time frame
  4583. if ( ( ( flLifeTime < m_flStartTime ) || ( flLifeTime >= m_flEndTime ) ) && ( ( m_flStartTime != -1.0f) && ( m_flEndTime != -1.0f) ) )
  4584. continue;
  4585. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  4586. float flOutput = RemapValClamped( flInput, m_flInputMin, m_flInputMax, m_flOutputMin, m_flOutputMax );
  4587. if ( m_bScaleInitialRange )
  4588. {
  4589. flOutput = *pOutput * flOutput;
  4590. }
  4591. if ( ATTRIBUTES_WHICH_ARE_INTS & ( 1 << m_nFieldOutput ) )
  4592. {
  4593. *pOutput = int ( flOutput );
  4594. }
  4595. else
  4596. {
  4597. *pOutput = flOutput;
  4598. }
  4599. }
  4600. }
  4601. //-----------------------------------------------------------------------------
  4602. // Remap CP to Vector Initializer
  4603. //-----------------------------------------------------------------------------
  4604. class C_INIT_RemapCPtoVector : public CParticleInitializerOperatorInstance
  4605. {
  4606. DECLARE_PARTICLE_OPERATOR( C_INIT_RemapCPtoVector );
  4607. uint32 GetWrittenAttributes( void ) const
  4608. {
  4609. return 1 << m_nFieldOutput | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  4610. }
  4611. uint32 GetReadAttributes( void ) const
  4612. {
  4613. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  4614. }
  4615. uint32 GetFilter( void ) const
  4616. {
  4617. return FILTER_PARAMETER_REMAPPING_MASK;
  4618. }
  4619. virtual uint64 GetReadControlPointMask() const
  4620. {
  4621. uint64 nMask = ( 1ULL << m_nCPInput );
  4622. if ( m_nLocalSpaceCP != -1 )
  4623. {
  4624. nMask |= ( 1ULL << m_nLocalSpaceCP );
  4625. }
  4626. return nMask;
  4627. }
  4628. bool InitMultipleOverride ( void ) { return true; }
  4629. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  4630. int nParticleCount, int nAttributeWriteMask,
  4631. void *pContext) const;
  4632. int m_nCPInput;
  4633. int m_nFieldOutput;
  4634. Vector m_vInputMin;
  4635. Vector m_vInputMax;
  4636. Vector m_vOutputMin;
  4637. Vector m_vOutputMax;
  4638. float m_flStartTime;
  4639. float m_flEndTime;
  4640. bool m_bScaleInitialRange;
  4641. bool m_bOffset;
  4642. bool m_bAccelerate;
  4643. int m_nLocalSpaceCP;
  4644. };
  4645. DEFINE_PARTICLE_OPERATOR( C_INIT_RemapCPtoVector, "Remap Control Point to Vector", OPERATOR_GENERIC );
  4646. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RemapCPtoVector )
  4647. DMXELEMENT_UNPACK_FIELD( "emitter lifetime start time (seconds)", "-1", float, m_flStartTime )
  4648. DMXELEMENT_UNPACK_FIELD( "emitter lifetime end time (seconds)", "-1", float, m_flEndTime )
  4649. DMXELEMENT_UNPACK_FIELD( "input control point number", "0", int, m_nCPInput )
  4650. DMXELEMENT_UNPACK_FIELD( "input minimum","0 0 0", Vector, m_vInputMin )
  4651. DMXELEMENT_UNPACK_FIELD( "input maximum","0 0 0", Vector, m_vInputMax )
  4652. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "0", int, m_nFieldOutput, "intchoice particlefield_vector" )
  4653. DMXELEMENT_UNPACK_FIELD( "output minimum","0 0 0", Vector, m_vOutputMin )
  4654. DMXELEMENT_UNPACK_FIELD( "output maximum","0 0 0", Vector, m_vOutputMax )
  4655. DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
  4656. DMXELEMENT_UNPACK_FIELD( "offset position","0", bool, m_bOffset )
  4657. DMXELEMENT_UNPACK_FIELD( "accelerate position","0", bool, m_bAccelerate )
  4658. DMXELEMENT_UNPACK_FIELD( "local space CP","-1", int, m_nLocalSpaceCP )
  4659. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapCPtoVector )
  4660. void C_INIT_RemapCPtoVector::InitNewParticlesScalar(
  4661. CParticleCollection *pParticles, int start_p,
  4662. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  4663. {
  4664. Vector vecControlPoint;
  4665. pParticles->GetControlPointAtTime( m_nCPInput, pParticles->m_flCurTime, &vecControlPoint );
  4666. Vector vOutputMinLocal = m_vOutputMin;
  4667. Vector vOutputMaxLocal = m_vOutputMax;
  4668. if ( m_nLocalSpaceCP != -1 )
  4669. {
  4670. matrix3x4_t mat;
  4671. pParticles->GetControlPointTransformAtTime( m_nLocalSpaceCP, pParticles->m_flCurTime, &mat );
  4672. Vector vecTransformLocal = vec3_origin;
  4673. VectorRotate( vOutputMinLocal, mat, vecTransformLocal );
  4674. vOutputMinLocal = vecTransformLocal;
  4675. VectorRotate( vOutputMaxLocal, mat, vecTransformLocal );
  4676. vOutputMaxLocal = vecTransformLocal;
  4677. }
  4678. // FIXME: SSE-ize
  4679. for( ; nParticleCount--; start_p++ )
  4680. {
  4681. const float *pCreationTime = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  4682. // using raw creation time to map to emitter lifespan
  4683. float flLifeTime = *pCreationTime;
  4684. // only use within start/end time frame
  4685. if ( ( ( flLifeTime < m_flStartTime ) || ( flLifeTime >= m_flEndTime ) ) && ( ( m_flStartTime != -1.0f) && ( m_flEndTime != -1.0f) ) )
  4686. continue;
  4687. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  4688. Vector vOutput;
  4689. vOutput.x = RemapValClamped( vecControlPoint.x, m_vInputMin.x, m_vInputMax.x, vOutputMinLocal.x, vOutputMaxLocal.x );
  4690. vOutput.y = RemapValClamped( vecControlPoint.y, m_vInputMin.y, m_vInputMax.y, vOutputMinLocal.y, vOutputMaxLocal.y );
  4691. vOutput.z = RemapValClamped( vecControlPoint.z, m_vInputMin.z, m_vInputMax.z, vOutputMinLocal.z, vOutputMaxLocal.z );
  4692. if ( m_bScaleInitialRange )
  4693. {
  4694. Vector vOrgValue;
  4695. SetVectorFromAttribute ( vOrgValue, pOutput );
  4696. vOutput *= vOrgValue;
  4697. }
  4698. if ( m_nFieldOutput == ATTRIBUTES_WHICH_ARE_COLOR_AND_OPACITY )
  4699. {
  4700. pOutput[0] = MAX( 0.0f, MIN( vOutput.x, 1.0f) );
  4701. pOutput[4] = MAX( 0.0f, MIN( vOutput.y, 1.0f) );
  4702. pOutput[8] = MAX( 0.0f, MIN( vOutput.z, 1.0f) );
  4703. }
  4704. else
  4705. {
  4706. float *pXYZ_Prev = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  4707. Vector vXYZPrev;
  4708. if ( m_bAccelerate )
  4709. {
  4710. if ( m_bOffset )
  4711. {
  4712. Vector vOrgValue;
  4713. SetVectorFromAttribute ( vOrgValue, pOutput );
  4714. SetVectorFromAttribute ( vXYZPrev, pXYZ_Prev );
  4715. vOutput += vOrgValue;
  4716. vXYZPrev += vOutput;
  4717. vOutput += vOutput * pParticles->m_flDt;
  4718. SetVectorAttribute ( pOutput, vOutput );
  4719. SetVectorAttribute ( pXYZ_Prev, vXYZPrev );
  4720. }
  4721. else
  4722. {
  4723. vOutput *= pParticles->m_flDt;
  4724. SetVectorAttribute ( pOutput, vOutput );
  4725. }
  4726. }
  4727. else
  4728. {
  4729. vXYZPrev = vOutput;
  4730. if ( m_bOffset )
  4731. {
  4732. Vector vOrgValue;
  4733. SetVectorFromAttribute ( vOrgValue, pOutput );
  4734. SetVectorFromAttribute ( vXYZPrev, pXYZ_Prev );
  4735. vOutput += vOrgValue;
  4736. vXYZPrev += vOutput;
  4737. SetVectorAttribute ( pXYZ_Prev, vXYZPrev );
  4738. }
  4739. SetVectorAttribute ( pOutput, vOutput );
  4740. }
  4741. }
  4742. }
  4743. }
  4744. class C_INIT_ChaoticAttractor : public CParticleInitializerOperatorInstance
  4745. {
  4746. DECLARE_PARTICLE_OPERATOR( C_INIT_ChaoticAttractor );
  4747. struct AttractorContext_t
  4748. {
  4749. Vector m_vecLastPosition;
  4750. };
  4751. uint32 GetWrittenAttributes( void ) const
  4752. {
  4753. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  4754. }
  4755. uint32 GetReadAttributes( void ) const
  4756. {
  4757. return 0;
  4758. }
  4759. virtual uint64 GetReadControlPointMask() const
  4760. {
  4761. return 1ULL << m_nBaseCP;
  4762. }
  4763. virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
  4764. {
  4765. AttractorContext_t *pCtx = reinterpret_cast<AttractorContext_t *>( pContext );
  4766. pCtx->m_vecLastPosition.Init();
  4767. }
  4768. size_t GetRequiredContextBytes( void ) const
  4769. {
  4770. return sizeof( AttractorContext_t );
  4771. }
  4772. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  4773. int nParticleCount, int nAttributeWriteMask,
  4774. void *pContext) const;
  4775. float m_flAParm;
  4776. float m_flBParm;
  4777. float m_flCParm;
  4778. float m_flDParm;
  4779. float m_flScale;
  4780. float m_flSpeedMin;
  4781. float m_flSpeedMax;
  4782. int m_nBaseCP;
  4783. bool m_bUniformSpeed;
  4784. };
  4785. DEFINE_PARTICLE_OPERATOR( C_INIT_ChaoticAttractor, "Position From Chaotic Attractor", OPERATOR_PI_POSITION );
  4786. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_ChaoticAttractor )
  4787. DMXELEMENT_UNPACK_FIELD( "Pickover A Parameter", "-0.9629629", float, m_flAParm )
  4788. DMXELEMENT_UNPACK_FIELD( "Pickover B Parameter", "2.791139", float, m_flBParm )
  4789. DMXELEMENT_UNPACK_FIELD( "Pickover C Parameter", "1.85185185", float, m_flCParm )
  4790. DMXELEMENT_UNPACK_FIELD( "Pickover D Parameter", "1.5", float, m_flDParm )
  4791. DMXELEMENT_UNPACK_FIELD( "Speed Min", "0", float, m_flSpeedMin )
  4792. DMXELEMENT_UNPACK_FIELD( "Speed Max", "0", float, m_flSpeedMax )
  4793. DMXELEMENT_UNPACK_FIELD( "Uniform speed", "0", bool, m_bUniformSpeed )
  4794. DMXELEMENT_UNPACK_FIELD( "Relative Control point number", "0", int, m_nBaseCP )
  4795. DMXELEMENT_UNPACK_FIELD( "Scale", "1", float, m_flScale )
  4796. END_PARTICLE_OPERATOR_UNPACK( C_INIT_ChaoticAttractor )
  4797. void C_INIT_ChaoticAttractor::InitNewParticlesScalar(
  4798. CParticleCollection *pParticles, int start_p,
  4799. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  4800. {
  4801. AttractorContext_t *pCtx = reinterpret_cast<AttractorContext_t *>( pContext );
  4802. Vector vecControlPoint1 = pParticles->GetControlPointAtCurrentTime( m_nBaseCP );
  4803. Vector vecFwd, vecRight, vecUp;
  4804. pParticles->GetControlPointOrientationAtCurrentTime( m_nBaseCP, &vecFwd, &vecRight, &vecUp );
  4805. for( ; nParticleCount--; start_p++ )
  4806. {
  4807. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  4808. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  4809. Vector vecScaledOffset = m_flScale * pCtx->m_vecLastPosition;
  4810. Vector vecOut = vecControlPoint1 + vecScaledOffset.x * vecRight + vecScaledOffset.y * vecFwd + vecScaledOffset.z * vecUp;
  4811. SetVectorAttribute( xyz, vecOut );
  4812. Vector vecNewPnt;
  4813. vecNewPnt.x = sin( m_flAParm * pCtx->m_vecLastPosition.y ) - pCtx->m_vecLastPosition.z * cos( m_flBParm * pCtx->m_vecLastPosition.x );
  4814. vecNewPnt.z = sin( pCtx->m_vecLastPosition.x );
  4815. vecNewPnt.y = pCtx->m_vecLastPosition.z * sin ( m_flCParm * pCtx->m_vecLastPosition.x ) - cos( m_flDParm * pCtx->m_vecLastPosition.y );
  4816. if ( fabs( m_flSpeedMax ) > 0 )
  4817. {
  4818. Vector dx = vecNewPnt - pCtx->m_vecLastPosition;
  4819. if (! m_bUniformSpeed )
  4820. {
  4821. float flLen = VectorLength( dx );
  4822. if ( fabs( flLen ) > 0 )
  4823. {
  4824. dx *= 1. / flLen;
  4825. }
  4826. }
  4827. dx *= pParticles->RandomInt( m_flSpeedMin, m_flSpeedMax );
  4828. dx *= pParticles->m_flPreviousDt;
  4829. SetVectorAttribute( pxyz, vecOut + dx );
  4830. }
  4831. else
  4832. SetVectorAttribute( pxyz, vecOut );
  4833. pCtx->m_vecLastPosition = vecNewPnt;
  4834. }
  4835. }
  4836. class C_INIT_CreateFromParentParticles : public CParticleInitializerOperatorInstance
  4837. {
  4838. DECLARE_PARTICLE_OPERATOR( C_INIT_CreateFromParentParticles );
  4839. struct ParentParticlesContext_t
  4840. {
  4841. int m_nCurrentParentParticle;
  4842. };
  4843. uint32 GetWrittenAttributes( void ) const
  4844. {
  4845. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
  4846. }
  4847. uint32 GetReadAttributes( void ) const
  4848. {
  4849. return PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  4850. }
  4851. virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
  4852. {
  4853. ParentParticlesContext_t *pCtx = reinterpret_cast<ParentParticlesContext_t *>( pContext );
  4854. pCtx->m_nCurrentParentParticle = 0;
  4855. }
  4856. size_t GetRequiredContextBytes( void ) const
  4857. {
  4858. return sizeof( ParentParticlesContext_t );
  4859. }
  4860. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  4861. int nParticleCount, int nAttributeWriteMask,
  4862. void *pContext) const;
  4863. float m_flVelocityScale;
  4864. bool m_bRandomDistribution;
  4865. int m_nIncrement;
  4866. };
  4867. DEFINE_PARTICLE_OPERATOR( C_INIT_CreateFromParentParticles, "Position From Parent Particles", OPERATOR_PI_POSITION );
  4868. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_CreateFromParentParticles )
  4869. DMXELEMENT_UNPACK_FIELD( "Inherited Velocity Scale","0", float, m_flVelocityScale )
  4870. DMXELEMENT_UNPACK_FIELD( "Random Parent Particle Distribution","0", bool, m_bRandomDistribution )
  4871. DMXELEMENT_UNPACK_FIELD( "Particle Increment Amount","1", int, m_nIncrement )
  4872. END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateFromParentParticles )
  4873. void C_INIT_CreateFromParentParticles::InitNewParticlesScalar(
  4874. CParticleCollection *pParticles, int start_p,
  4875. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  4876. {
  4877. if ( !pParticles->m_pParent )
  4878. {
  4879. for( ; nParticleCount--; start_p++ )
  4880. {
  4881. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  4882. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  4883. SetVectorAttribute( xyz, vec3_origin );
  4884. SetVectorAttribute( pxyz, vec3_origin );
  4885. }
  4886. return;
  4887. }
  4888. ParentParticlesContext_t *pCtx = reinterpret_cast<ParentParticlesContext_t *>( pContext );
  4889. int nActiveParticles = pParticles->m_pParent->m_nActiveParticles;
  4890. if ( nActiveParticles == 0 )
  4891. {
  4892. while( nParticleCount-- )
  4893. {
  4894. float *lifespan = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
  4895. *lifespan = 0.0f;
  4896. start_p++;
  4897. }
  4898. return;
  4899. }
  4900. nActiveParticles = MAX( 0, nActiveParticles - 1 );
  4901. for( ; nParticleCount--; start_p++ )
  4902. {
  4903. if ( m_bRandomDistribution )
  4904. {
  4905. pCtx->m_nCurrentParentParticle = pParticles->RandomInt( 0, nActiveParticles );
  4906. }
  4907. else if ( pCtx->m_nCurrentParentParticle > nActiveParticles )
  4908. {
  4909. pCtx->m_nCurrentParentParticle = 0;
  4910. }
  4911. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  4912. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  4913. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  4914. const float *pParent_xyz = pParticles->m_pParent->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_XYZ, pCtx->m_nCurrentParentParticle );
  4915. const float *pParent_pxyz = pParticles->m_pParent->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_PREV_XYZ, pCtx->m_nCurrentParentParticle );
  4916. Vector vecParentXYZ;
  4917. Vector vecParentPrevXYZ;
  4918. Vector vecScaledXYZ;
  4919. float flPrevTime = pParticles->m_flCurTime - pParticles->m_flDt;
  4920. float flSubFrame = RemapValClamped( *ct, flPrevTime, pParticles->m_flCurTime, 0, 1 );
  4921. vecParentXYZ.x = pParent_xyz[0];
  4922. vecParentXYZ.y = pParent_xyz[4];
  4923. vecParentXYZ.z = pParent_xyz[8];
  4924. vecParentPrevXYZ.x = pParent_pxyz[0];
  4925. vecParentPrevXYZ.y = pParent_pxyz[4];
  4926. vecParentPrevXYZ.z = pParent_pxyz[8];
  4927. VectorLerp( vecParentPrevXYZ, vecParentXYZ, flSubFrame, vecParentXYZ );
  4928. VectorLerp( vecParentXYZ, vecParentPrevXYZ, m_flVelocityScale, vecScaledXYZ );
  4929. SetVectorAttribute( pxyz, vecScaledXYZ );
  4930. SetVectorAttribute( xyz, vecParentXYZ );
  4931. pCtx->m_nCurrentParentParticle += m_nIncrement;
  4932. }
  4933. }
  4934. class C_INIT_InheritFromParentParticles : public CParticleInitializerOperatorInstance
  4935. {
  4936. DECLARE_PARTICLE_OPERATOR( C_INIT_InheritFromParentParticles );
  4937. struct ParentParticlesContext_t
  4938. {
  4939. int m_nCurrentParentParticle;
  4940. };
  4941. uint32 GetWrittenAttributes( void ) const
  4942. {
  4943. return 1 << m_nFieldOutput;
  4944. }
  4945. uint32 GetReadAttributes( void ) const
  4946. {
  4947. return 0;
  4948. }
  4949. uint32 GetFilter( void ) const
  4950. {
  4951. return FILTER_PARAMETER_REMAPPING_MASK;
  4952. }
  4953. virtual void InitializeContextData( CParticleCollection *pParticles, void *pContext ) const
  4954. {
  4955. ParentParticlesContext_t *pCtx = reinterpret_cast<ParentParticlesContext_t *>( pContext );
  4956. pCtx->m_nCurrentParentParticle = 0;
  4957. }
  4958. size_t GetRequiredContextBytes( void ) const
  4959. {
  4960. return sizeof( ParentParticlesContext_t );
  4961. }
  4962. bool InitMultipleOverride ( void ) { return true; }
  4963. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  4964. int nParticleCount, int nAttributeWriteMask,
  4965. void *pContext) const;
  4966. float m_flScale;
  4967. int m_nFieldOutput;
  4968. int m_nIncrement;
  4969. bool m_bRandomDistribution;
  4970. };
  4971. DEFINE_PARTICLE_OPERATOR( C_INIT_InheritFromParentParticles, "Inherit Initial Value From Parent Particle", OPERATOR_GENERIC );
  4972. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_InheritFromParentParticles )
  4973. DMXELEMENT_UNPACK_FIELD_USERDATA( "Inherited Field", "3", int, m_nFieldOutput, "intchoice particlefield" )
  4974. DMXELEMENT_UNPACK_FIELD( "Scale","1", float, m_flScale )
  4975. DMXELEMENT_UNPACK_FIELD( "Random Parent Particle Distribution","0", bool, m_bRandomDistribution )
  4976. DMXELEMENT_UNPACK_FIELD( "Particle Increment Amount","1", int, m_nIncrement )
  4977. END_PARTICLE_OPERATOR_UNPACK( C_INIT_InheritFromParentParticles )
  4978. void C_INIT_InheritFromParentParticles::InitNewParticlesScalar(
  4979. CParticleCollection *pParticles, int start_p,
  4980. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  4981. {
  4982. if ( !pParticles->m_pParent )
  4983. {
  4984. return;
  4985. }
  4986. ParentParticlesContext_t *pCtx = reinterpret_cast<ParentParticlesContext_t *>( pContext );
  4987. int nActiveParticles = pParticles->m_pParent->m_nActiveParticles;
  4988. if ( nActiveParticles == 0 )
  4989. {
  4990. return;
  4991. }
  4992. nActiveParticles = MAX( 0, nActiveParticles - 1 );
  4993. for( ; nParticleCount--; start_p++ )
  4994. {
  4995. if ( m_bRandomDistribution )
  4996. {
  4997. pCtx->m_nCurrentParentParticle = pParticles->RandomInt( 0, nActiveParticles );
  4998. }
  4999. else if ( pCtx->m_nCurrentParentParticle > nActiveParticles )
  5000. {
  5001. pCtx->m_nCurrentParentParticle = 0;
  5002. }
  5003. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  5004. const float *pParentValue = pParticles->m_pParent->GetFloatAttributePtr( m_nFieldOutput, pCtx->m_nCurrentParentParticle );
  5005. if ( ATTRIBUTES_WHICH_ARE_VEC3S_MASK & ( 1 << m_nFieldOutput ) )
  5006. {
  5007. Vector vecParentValue;
  5008. SetVectorFromAttribute( vecParentValue, pParentValue );
  5009. vecParentValue *= m_flScale;
  5010. // Clamp to 0-1 if color
  5011. if ( ATTRIBUTES_WHICH_ARE_COLOR_AND_OPACITY & ( 1 << m_nFieldOutput ) )
  5012. {
  5013. vecParentValue.Min( Vector( 1, 1, 1 ) );
  5014. vecParentValue.Max( Vector( 0, 0, 0 ) );
  5015. }
  5016. SetVectorAttribute( pOutput, vecParentValue );
  5017. }
  5018. else
  5019. {
  5020. float flOutput = *pParentValue * m_flScale;
  5021. if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & ( 1 << m_nFieldOutput ) )
  5022. {
  5023. flOutput = clamp(flOutput, 0.0f, 1.0f );
  5024. }
  5025. *pOutput = flOutput;
  5026. }
  5027. pCtx->m_nCurrentParentParticle += m_nIncrement;
  5028. }
  5029. }
  5030. //-----------------------------------------------------------------------------
  5031. // Distance to CP Initializer
  5032. //-----------------------------------------------------------------------------
  5033. class C_INIT_DistanceToCPInit : public CParticleInitializerOperatorInstance
  5034. {
  5035. DECLARE_PARTICLE_OPERATOR( C_INIT_DistanceToCPInit );
  5036. uint32 GetWrittenAttributes( void ) const
  5037. {
  5038. return 1 << m_nFieldOutput;
  5039. }
  5040. uint32 GetReadAttributes( void ) const
  5041. {
  5042. return PARTICLE_ATTRIBUTE_XYZ_MASK;
  5043. }
  5044. uint32 GetFilter( void ) const
  5045. {
  5046. return FILTER_PARAMETER_REMAPPING_MASK;
  5047. }
  5048. virtual uint64 GetReadControlPointMask() const
  5049. {
  5050. return 1ULL << m_nStartCP;
  5051. }
  5052. bool InitMultipleOverride ( void ) { return true; }
  5053. void InitParams( CParticleSystemDefinition *pDef )
  5054. {
  5055. m_nCollisionGroupNumber = g_pParticleSystemMgr->Query()->GetCollisionGroupFromName( m_CollisionGroupName );
  5056. m_nStartCP = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nStartCP ) );
  5057. // clamp the result to 0 and 1 if it's alpha
  5058. if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & ( 1 << m_nFieldOutput ) )
  5059. {
  5060. m_flOutputMin = clamp(m_flOutputMin, 0.0f, 1.0f );
  5061. m_flOutputMax = clamp(m_flOutputMax, 0.0f, 1.0f );
  5062. }
  5063. }
  5064. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  5065. int nParticleCount, int nAttributeWriteMask,
  5066. void *pContext) const;
  5067. int m_nFieldOutput;
  5068. float m_flInputMin;
  5069. float m_flInputMax;
  5070. float m_flOutputMin;
  5071. float m_flOutputMax;
  5072. int m_nStartCP;
  5073. bool m_bLOS;
  5074. char m_CollisionGroupName[128];
  5075. int m_nCollisionGroupNumber;
  5076. float m_flMaxTraceLength;
  5077. float m_flLOSScale;
  5078. bool m_bScaleInitialRange;
  5079. bool m_bActiveRange;
  5080. };
  5081. DEFINE_PARTICLE_OPERATOR( C_INIT_DistanceToCPInit, "Remap Initial Distance to Control Point to Scalar", OPERATOR_GENERIC );
  5082. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_DistanceToCPInit )
  5083. DMXELEMENT_UNPACK_FIELD( "distance minimum","0", float, m_flInputMin )
  5084. DMXELEMENT_UNPACK_FIELD( "distance maximum","128", float, m_flInputMax )
  5085. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "3", int, m_nFieldOutput, "intchoice particlefield_scalar" )
  5086. DMXELEMENT_UNPACK_FIELD( "output minimum","0", float, m_flOutputMin )
  5087. DMXELEMENT_UNPACK_FIELD( "output maximum","1", float, m_flOutputMax )
  5088. DMXELEMENT_UNPACK_FIELD( "control point","0", int, m_nStartCP )
  5089. DMXELEMENT_UNPACK_FIELD( "ensure line of sight","0", bool, m_bLOS )
  5090. DMXELEMENT_UNPACK_FIELD_STRING( "LOS collision group", "NONE", m_CollisionGroupName )
  5091. DMXELEMENT_UNPACK_FIELD( "Maximum Trace Length", "-1", float, m_flMaxTraceLength )
  5092. DMXELEMENT_UNPACK_FIELD( "LOS Failure Scalar", "0", float, m_flLOSScale )
  5093. DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
  5094. DMXELEMENT_UNPACK_FIELD( "only active within specified distance","0", bool, m_bActiveRange )
  5095. END_PARTICLE_OPERATOR_UNPACK( C_INIT_DistanceToCPInit )
  5096. void C_INIT_DistanceToCPInit::InitNewParticlesScalar(
  5097. CParticleCollection *pParticles, int start_p,
  5098. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  5099. {
  5100. Vector vecControlPoint1 = pParticles->GetControlPointAtCurrentTime( m_nStartCP );
  5101. // FIXME: SSE-ize
  5102. for( ; nParticleCount--; start_p++ )
  5103. {
  5104. Vector vecPosition2;
  5105. const float *pXYZ = pParticles->GetFloatAttributePtr(PARTICLE_ATTRIBUTE_XYZ, start_p );
  5106. vecPosition2 = Vector(pXYZ[0], pXYZ[4], pXYZ[8]);
  5107. Vector vecDelta = vecControlPoint1 - vecPosition2;
  5108. float flDistance = vecDelta.Length();
  5109. if ( m_bActiveRange && ( flDistance < m_flInputMin || flDistance > m_flInputMax ) )
  5110. {
  5111. continue;
  5112. }
  5113. if ( m_bLOS )
  5114. {
  5115. Vector vecEndPoint = vecPosition2;
  5116. if ( m_flMaxTraceLength != -1.0f && m_flMaxTraceLength < flDistance )
  5117. {
  5118. VectorNormalize(vecEndPoint);
  5119. vecEndPoint *= m_flMaxTraceLength;
  5120. vecEndPoint += vecControlPoint1;
  5121. }
  5122. CBaseTrace tr;
  5123. g_pParticleSystemMgr->Query()->TraceLine( vecControlPoint1, vecEndPoint, MASK_OPAQUE_AND_NPCS, NULL , m_nCollisionGroupNumber, &tr );
  5124. if (tr.fraction != 1.0f)
  5125. {
  5126. flDistance *= tr.fraction * m_flLOSScale;
  5127. }
  5128. }
  5129. float flOutput = RemapValClamped( flDistance, m_flInputMin, m_flInputMax, m_flOutputMin, m_flOutputMax );
  5130. if ( m_bScaleInitialRange )
  5131. {
  5132. const float *pInitialOutput = pParticles->GetFloatAttributePtr( m_nFieldOutput, start_p );
  5133. flOutput = *pInitialOutput * flOutput;
  5134. }
  5135. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  5136. *pOutput = flOutput;
  5137. }
  5138. }
  5139. class C_INIT_LifespanFromVelocity : public CParticleInitializerOperatorInstance
  5140. {
  5141. DECLARE_PARTICLE_OPERATOR( C_INIT_LifespanFromVelocity );
  5142. Vector m_vecComponentScale;
  5143. float m_flTraceOffset;
  5144. float m_flMaxTraceLength;
  5145. float m_flTraceTolerance;
  5146. int m_nCollisionGroupNumber;
  5147. int m_nMaxPlanes;
  5148. int m_nAllowedPlanes;
  5149. char m_CollisionGroupName[128];
  5150. unsigned int m_CollisionMask;
  5151. bool m_bIncludeWater;
  5152. uint32 GetWrittenAttributes( void ) const
  5153. {
  5154. return PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK;
  5155. }
  5156. uint32 GetReadAttributes( void ) const
  5157. {
  5158. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  5159. }
  5160. void InitializeContextData( CParticleCollection *pParticles,
  5161. void *pContext ) const
  5162. {
  5163. }
  5164. size_t GetRequiredContextBytes( ) const
  5165. {
  5166. return sizeof( CWorldCollideContextData );
  5167. }
  5168. bool InitMultipleOverride ( void ) { return true; }
  5169. void InitParams( CParticleSystemDefinition *pDef )
  5170. {
  5171. m_nCollisionGroupNumber = g_pParticleSystemMgr->Query()->GetCollisionGroupFromName( m_CollisionGroupName );
  5172. m_nAllowedPlanes = ( MIN( MAX_WORLD_PLANAR_CONSTRAINTS, m_nMaxPlanes ) - 1 );
  5173. if ( m_bIncludeWater )
  5174. m_CollisionMask = MASK_SHOT_HULL|MASK_SPLITAREAPORTAL;
  5175. else
  5176. m_CollisionMask = MASK_SHOT_HULL;
  5177. }
  5178. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  5179. int nParticleCount, int nAttributeWriteMask,
  5180. void *pContext) const;
  5181. virtual void InitNewParticlesBlock( CParticleCollection *pParticles,
  5182. int start_block, int n_blocks, int nAttributeWriteMask,
  5183. void *pContext ) const;
  5184. };
  5185. DEFINE_PARTICLE_OPERATOR( C_INIT_LifespanFromVelocity, "Lifetime from Time to Impact", OPERATOR_GENERIC );
  5186. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_LifespanFromVelocity )
  5187. DMXELEMENT_UNPACK_FIELD_STRING( "trace collision group", "NONE", m_CollisionGroupName )
  5188. DMXELEMENT_UNPACK_FIELD( "maximum trace length", "1024", float, m_flMaxTraceLength )
  5189. DMXELEMENT_UNPACK_FIELD( "trace offset", "0", float, m_flTraceOffset )
  5190. DMXELEMENT_UNPACK_FIELD( "trace recycle tolerance", "64", float, m_flTraceTolerance )
  5191. DMXELEMENT_UNPACK_FIELD( "maximum points to cache", "16", int, m_nMaxPlanes )
  5192. DMXELEMENT_UNPACK_FIELD( "bias distance", "1 1 1", Vector, m_vecComponentScale )
  5193. DMXELEMENT_UNPACK_FIELD( "collide with water", "1", bool, m_bIncludeWater )
  5194. END_PARTICLE_OPERATOR_UNPACK( C_INIT_LifespanFromVelocity )
  5195. void C_INIT_LifespanFromVelocity::InitNewParticlesScalar(
  5196. CParticleCollection *pParticles, int start_p,
  5197. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  5198. {
  5199. CWorldCollideContextData **ppCtx;
  5200. if ( pParticles->m_pParent )
  5201. ppCtx = &( pParticles->m_pParent->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );
  5202. else
  5203. ppCtx = &( pParticles->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );
  5204. CWorldCollideContextData *pCtx = NULL;
  5205. if ( ! *ppCtx )
  5206. {
  5207. *ppCtx = new CWorldCollideContextData;
  5208. (*ppCtx)->m_nActivePlanes = 0;
  5209. (*ppCtx)->m_nActivePlanes = 0;
  5210. (*ppCtx)->m_nNumFixedPlanes = 0;
  5211. }
  5212. pCtx = *ppCtx;
  5213. float flTol = m_flTraceTolerance * m_flTraceTolerance;
  5214. //Trace length takes the max trace and subtracts the offset to get the actual total.
  5215. float flTotalTraceDist = m_flMaxTraceLength - m_flTraceOffset;
  5216. //Offset percentage to account for if we've hit something within the offset (but not spawn) area
  5217. float flOffsetPct = m_flMaxTraceLength / ( flTotalTraceDist + FLT_EPSILON );
  5218. FourVectors v4ComponentScale;
  5219. v4ComponentScale.DuplicateVector( m_vecComponentScale );
  5220. fltx4 fl4TraceOffset = ReplicateX4( m_flTraceOffset );
  5221. for( ; nParticleCount--; start_p++ )
  5222. {
  5223. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  5224. float *pPrevXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  5225. float *dtime = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
  5226. if ( *dtime >= 0.0f )
  5227. {
  5228. Vector vecXYZ;
  5229. Vector vecXYZ_Prev;
  5230. SetVectorFromAttribute( vecXYZ, pxyz );
  5231. SetVectorFromAttribute( vecXYZ_Prev, pPrevXYZ );
  5232. //Calculate velocity and account for frame delta time
  5233. Vector vDelta = ( vecXYZ - vecXYZ_Prev ) / pParticles->m_flPreviousDt;
  5234. float flVelocity = VectorLength( vDelta );
  5235. //Normalize the delta and get the offset to use from the normalized delta times the offset
  5236. VectorNormalize( vDelta );
  5237. Vector vecOffset = vDelta * m_flTraceOffset;
  5238. Vector vecStartPnt = vecXYZ + vecOffset;
  5239. Vector vecEndPnt = ( vDelta * flTotalTraceDist ) + vecStartPnt;
  5240. // Use SIMD section to interface with plane cache, even though we're not SIMD here
  5241. // Test versus existing Data
  5242. FourVectors fvStartPnt;
  5243. fvStartPnt.DuplicateVector( vecStartPnt );
  5244. FourVectors fvEndPnt;
  5245. fvEndPnt.DuplicateVector( vecEndPnt );
  5246. FourVectors v4PointOnPlane;
  5247. FourVectors v4PlaneNormal;
  5248. FourVectors v4Delta;
  5249. fltx4 fl4ClosestDist = Four_FLT_MAX;
  5250. for( int i = 0 ; i < pCtx->m_nActivePlanes; i++ )
  5251. {
  5252. if ( pCtx->m_bPlaneActive[i] )
  5253. {
  5254. fltx4 fl4TrialDistance = MaxSIMD(
  5255. fvStartPnt.DistToSqr( pCtx->m_TraceStartPnt[i] ),
  5256. fvEndPnt.DistToSqr( pCtx->m_TraceEndPnt[i] ) );
  5257. //fltx4 fl4TrialDistance = MaxSIMD(
  5258. // fvStartPnt.DistSqrToLineSegment( pCtx->m_TraceStartPnt[i], pCtx->m_TraceEndPnt[i] ),
  5259. // fvEndPnt.DistSqrToLineSegment( pCtx->m_TraceStartPnt[i], pCtx->m_TraceEndPnt[i] ) );
  5260. // If the trial distance is closer than the existing closest, replace.
  5261. if ( !IsAllGreaterThan( fl4TrialDistance, fl4ClosestDist ) )
  5262. {
  5263. fl4ClosestDist = fl4TrialDistance;
  5264. v4PointOnPlane = pCtx->m_PointOnPlane[i];
  5265. }
  5266. }
  5267. }
  5268. // If we're outside the tolerance range, do a new trace and store it.
  5269. if ( IsAllGreaterThan( fl4ClosestDist, ReplicateX4( flTol ) ) )
  5270. {
  5271. //replace this with fast raycaster when available
  5272. CBaseTrace tr;
  5273. g_pParticleSystemMgr->Query()->TraceLine( vecStartPnt, vecEndPnt, m_CollisionMask, NULL , m_nCollisionGroupNumber, &tr );
  5274. Vector vNormal = tr.plane.normal;
  5275. Vector vPointOnPlane = tr.endpos;
  5276. // If we hit nothing, invalidate our hit normal
  5277. if ( tr.fraction == 1.0f )
  5278. vNormal = vec3_invalid;
  5279. // If we start solid, our trace distance is 0, or we hit within the offset area
  5280. // cache off anyway, but set our impact and normal to be invalid
  5281. if ( ( tr.fraction <= ( 1 - flOffsetPct ) ) || tr.startsolid || flTotalTraceDist == 0.0f || tr.allsolid )
  5282. {
  5283. vPointOnPlane = vec3_invalid;
  5284. vNormal = vec3_invalid;
  5285. }
  5286. // Cache off trace
  5287. int nIndex = pCtx->m_nNumFixedPlanes;
  5288. pCtx->m_bPlaneActive[nIndex] = true;
  5289. pCtx->m_PointOnPlane[nIndex].DuplicateVector( vPointOnPlane );
  5290. pCtx->m_PlaneNormal[nIndex].DuplicateVector( vNormal );
  5291. pCtx->m_TraceStartPnt[nIndex].DuplicateVector( vecStartPnt );
  5292. pCtx->m_TraceEndPnt[nIndex].DuplicateVector( vecEndPnt );
  5293. fvStartPnt.DuplicateVector( vecStartPnt );
  5294. v4PointOnPlane.DuplicateVector( vPointOnPlane );
  5295. pCtx->m_nNumFixedPlanes = pCtx->m_nNumFixedPlanes + 1;
  5296. if ( pCtx->m_nNumFixedPlanes > m_nAllowedPlanes )
  5297. pCtx->m_nNumFixedPlanes = 0;
  5298. pCtx->m_nActivePlanes = MIN( m_nAllowedPlanes, pCtx->m_nActivePlanes + 1 );
  5299. }
  5300. // If we have an invalid trace, kill us, otherwise do the math.
  5301. if ( v4PointOnPlane.Vec( 0 ) == vec3_invalid )
  5302. {
  5303. *dtime = -1.0f;
  5304. }
  5305. else
  5306. {
  5307. fvStartPnt -= v4PointOnPlane;
  5308. //Scale components to remove undesired axis
  5309. fvStartPnt *= v4ComponentScale;
  5310. //Find the length of the trace
  5311. //Need to use the adjusted value of the trace length and collision point to account for the offset
  5312. fltx4 fl4Dist = AddSIMD ( fvStartPnt.length(), fl4TraceOffset );
  5313. flVelocity += FLT_EPSILON;
  5314. //Divide by Velocity to get Lifespan
  5315. float flLifeUpdate;
  5316. flLifeUpdate = SubFloat( fl4Dist, 0) / flVelocity;
  5317. *dtime = flLifeUpdate;
  5318. }
  5319. }
  5320. }
  5321. }
  5322. void C_INIT_LifespanFromVelocity::InitNewParticlesBlock( CParticleCollection *pParticles,
  5323. int start_block, int n_blocks, int nAttributeWriteMask,
  5324. void *pContext ) const
  5325. {
  5326. CWorldCollideContextData **ppCtx;
  5327. if ( pParticles->m_pParent )
  5328. ppCtx = &( pParticles->m_pParent->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );
  5329. else
  5330. ppCtx = &( pParticles->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );
  5331. CWorldCollideContextData *pCtx = NULL;
  5332. if ( ! *ppCtx )
  5333. {
  5334. *ppCtx = new CWorldCollideContextData;
  5335. (*ppCtx)->m_nActivePlanes = 0;
  5336. (*ppCtx)->m_nActivePlanes = 0;
  5337. (*ppCtx)->m_nNumFixedPlanes = 0;
  5338. }
  5339. pCtx = *ppCtx;
  5340. float flTol = m_flTraceTolerance * m_flTraceTolerance;
  5341. //Trace length takes the max trace and subtracts the offset to get the actual total.
  5342. float flTotalTraceDist = m_flMaxTraceLength - m_flTraceOffset;
  5343. fltx4 fl4TotalTraceDist = ReplicateX4( flTotalTraceDist );
  5344. //Offset percentage to account for if we've hit something within the offset (but not spawn) area
  5345. float flOffsetPct = m_flMaxTraceLength / ( flTotalTraceDist + FLT_EPSILON );
  5346. FourVectors v4ComponentScale;
  5347. v4ComponentScale.DuplicateVector( m_vecComponentScale );
  5348. fltx4 fl4TraceOffset = ReplicateX4( m_flTraceOffset );
  5349. fltx4 fl4PrevDT = ReplicateX4( pParticles->m_flPreviousDt );
  5350. size_t attr_stride;
  5351. const FourVectors *pXYZ = pParticles->Get4VAttributePtr( PARTICLE_ATTRIBUTE_XYZ, &attr_stride );
  5352. pXYZ += attr_stride * start_block;
  5353. const FourVectors *pPrev_XYZ = pParticles->Get4VAttributePtr( PARTICLE_ATTRIBUTE_PREV_XYZ, &attr_stride );
  5354. pPrev_XYZ += attr_stride * start_block;
  5355. fltx4 *pLifespan = pParticles->GetM128AttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, &attr_stride );
  5356. pLifespan += attr_stride * start_block;
  5357. while( n_blocks-- )
  5358. {
  5359. fltx4 fl4Life = *pLifespan;
  5360. bi32x4 fl4PassMask = CmpGeSIMD( fl4Life, Four_Zeros );
  5361. bi32x4 fl4SkipMask = CmpLtSIMD( fl4Life, Four_Zeros );
  5362. if ( IsAnyTrue( fl4PassMask ) )
  5363. {
  5364. // Determine Velocity
  5365. FourVectors fvDelta = *pXYZ;
  5366. fvDelta -= *pPrev_XYZ;
  5367. fvDelta *= ReciprocalSIMD ( fl4PrevDT );
  5368. fltx4 fl4Velocity = fvDelta.length();
  5369. //fl4Velocity = MulSIMD ( fl4Velocity, fl4PrevDT );
  5370. //fl4Velocity = DivSIMD ( fl4Velocity, fl4PrevDT );
  5371. //Normalize the delta and get the offset to use from the normalized delta times the offset
  5372. FourVectors fvDeltaNormalized = fvDelta;
  5373. fvDeltaNormalized.VectorNormalizeFast();
  5374. FourVectors fvOffset = fvDeltaNormalized;
  5375. fvOffset *= m_flTraceOffset;
  5376. //Start/Endpoints for our traces
  5377. FourVectors fvStartPnt = *pXYZ;
  5378. fvStartPnt += fvOffset;
  5379. FourVectors fvEndPnt = fvDeltaNormalized;
  5380. fvEndPnt *= fl4TotalTraceDist;
  5381. fvEndPnt += fvStartPnt;
  5382. // Test versus existing Data
  5383. FourVectors v4PointOnPlane;
  5384. FourVectors v4PlaneNormal;
  5385. fltx4 fl4ClosestDist = Four_FLT_MAX;
  5386. fl4ClosestDist = MaskedAssign( fl4SkipMask, Four_Zeros, fl4ClosestDist );
  5387. for( int i = 0 ; i < pCtx->m_nActivePlanes; i++ )
  5388. {
  5389. if ( pCtx->m_bPlaneActive[i] )
  5390. {
  5391. fltx4 fl4TrialDistance = MaxSIMD(
  5392. fvStartPnt.DistToSqr( pCtx->m_TraceStartPnt[i] ),
  5393. fvEndPnt.DistToSqr( pCtx->m_TraceEndPnt[i] ) );
  5394. //fltx4 fl4TrialDistance = MaxSIMD(
  5395. // fvStartPnt.DistSqrToLineSegment( pCtx->m_TraceStartPnt[i], pCtx->m_TraceEndPnt[i] ),
  5396. // fvEndPnt.DistSqrToLineSegment( pCtx->m_TraceStartPnt[i], pCtx->m_TraceEndPnt[i] ) );
  5397. bi32x4 fl4Nearestmask = CmpLeSIMD( fl4TrialDistance, fl4ClosestDist );
  5398. fl4ClosestDist = MaskedAssign( fl4Nearestmask, fl4TrialDistance, fl4ClosestDist );
  5399. v4PointOnPlane.x = MaskedAssign( fl4Nearestmask, pCtx->m_PointOnPlane[i].x, v4PointOnPlane.x );
  5400. v4PointOnPlane.y = MaskedAssign( fl4Nearestmask, pCtx->m_PointOnPlane[i].y, v4PointOnPlane.y );
  5401. v4PointOnPlane.z = MaskedAssign( fl4Nearestmask, pCtx->m_PointOnPlane[i].z, v4PointOnPlane.z );
  5402. }
  5403. }
  5404. // If we're outside the tolerance range, do a new trace and store it.
  5405. bi32x4 fl4OutOfRange = CmpGtSIMD( fl4ClosestDist, ReplicateX4( flTol ) );
  5406. if ( IsAnyTrue( fl4OutOfRange ) )
  5407. {
  5408. int nMask = TestSignSIMD( fl4OutOfRange );
  5409. for(int i=0; i < 4; i++ )
  5410. {
  5411. if ( nMask & ( 1 << i ) )
  5412. {
  5413. Vector start = fvStartPnt.Vec( i );
  5414. Vector end = fvEndPnt.Vec( i );
  5415. //replace this with fast raycaster when available
  5416. CBaseTrace tr;
  5417. g_pParticleSystemMgr->Query()->TraceLine( start, end, m_CollisionMask, NULL , m_nCollisionGroupNumber, &tr );
  5418. Vector vNormal = tr.plane.normal;
  5419. Vector vPointOnPlane = tr.endpos;
  5420. // If we hit nothing, invalidate our hit normal
  5421. if ( tr.fraction == 1.0f )
  5422. vNormal = vec3_invalid;
  5423. // If we start solid, our trace distance is 0, or we hit within the offset area
  5424. // cache off anyway, but set our impact and normal to be invalid
  5425. if ( ( tr.fraction <= ( 1 - flOffsetPct ) ) || tr.startsolid || flTotalTraceDist == 0.0f || tr.allsolid )
  5426. {
  5427. vPointOnPlane = vec3_invalid;
  5428. vNormal = vec3_invalid;
  5429. }
  5430. SubFloat( v4PointOnPlane.x, i ) = vPointOnPlane.x;
  5431. SubFloat( v4PointOnPlane.y, i ) = vPointOnPlane.y;
  5432. SubFloat( v4PointOnPlane.z, i ) = vPointOnPlane.z;
  5433. // Cache off trace
  5434. int nIndex = pCtx->m_nNumFixedPlanes;
  5435. pCtx->m_bPlaneActive[nIndex] = true;
  5436. pCtx->m_PointOnPlane[nIndex].DuplicateVector( vPointOnPlane );
  5437. pCtx->m_PlaneNormal[nIndex].DuplicateVector( vNormal );
  5438. pCtx->m_TraceStartPnt[nIndex].DuplicateVector( start );
  5439. pCtx->m_TraceEndPnt[nIndex].DuplicateVector( end );
  5440. pCtx->m_nNumFixedPlanes = pCtx->m_nNumFixedPlanes + 1;
  5441. if ( pCtx->m_nNumFixedPlanes > m_nAllowedPlanes )
  5442. pCtx->m_nNumFixedPlanes = 0;
  5443. pCtx->m_nActivePlanes = MIN( m_nAllowedPlanes, pCtx->m_nActivePlanes + 1 );
  5444. }
  5445. }
  5446. }
  5447. FourVectors fvInvalid;
  5448. fvInvalid.DuplicateVector( vec3_invalid );
  5449. bi32x4 fl4InvalidMask = CmpEqSIMD( v4PointOnPlane.x, fvInvalid.x );
  5450. fl4InvalidMask = OrSIMD (fl4InvalidMask, CmpEqSIMD( v4PointOnPlane.y, fvInvalid.y ) );
  5451. fl4InvalidMask = OrSIMD (fl4InvalidMask, CmpEqSIMD( v4PointOnPlane.z, fvInvalid.z ) );
  5452. //Find the length of the trace
  5453. fvStartPnt -= v4PointOnPlane;
  5454. fvStartPnt *= v4ComponentScale;
  5455. //Need to use the adjusted value of the trace length and collision point to account for the offset
  5456. fltx4 fl4Dist = AddSIMD ( fvStartPnt.length(), fl4TraceOffset );
  5457. fl4Velocity = AddSIMD( fl4Velocity, Four_Epsilons );
  5458. //Divide by Velocity to get Lifespan
  5459. fltx4 fl4Life = DivSIMD( fl4Dist, fl4Velocity );
  5460. fl4Life = MaskedAssign( fl4InvalidMask, Four_Zeros, fl4Life );
  5461. *pLifespan = MaskedAssign( fl4PassMask, fl4Life, *pLifespan );
  5462. }
  5463. pXYZ += attr_stride;
  5464. pPrev_XYZ += attr_stride;
  5465. pLifespan += attr_stride;
  5466. }
  5467. }
  5468. class C_INIT_CreateFromPlaneCache : public CParticleInitializerOperatorInstance
  5469. {
  5470. DECLARE_PARTICLE_OPERATOR( C_INIT_CreateFromPlaneCache );
  5471. Vector m_vecOffsetMin;
  5472. Vector m_vecOffsetMax;
  5473. bool bLocalOffset;
  5474. bool m_bUseNormal;
  5475. uint32 GetWrittenAttributes( void ) const
  5476. {
  5477. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_LIFE_DURATION_MASK | PARTICLE_ATTRIBUTE_NORMAL_MASK;
  5478. }
  5479. uint32 GetReadAttributes( void ) const
  5480. {
  5481. return 0;
  5482. }
  5483. size_t GetRequiredContextBytes( ) const
  5484. {
  5485. return sizeof( CWorldCollideContextData );
  5486. }
  5487. void InitParams( CParticleSystemDefinition *pDef )
  5488. {
  5489. bLocalOffset = m_vecOffsetMin != vec3_origin && m_vecOffsetMax != vec3_origin;
  5490. }
  5491. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  5492. int nParticleCount, int nAttributeWriteMask,
  5493. void *pContext) const;
  5494. };
  5495. DEFINE_PARTICLE_OPERATOR( C_INIT_CreateFromPlaneCache, "Position from Parent Cache", OPERATOR_PI_POSITION );
  5496. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_CreateFromPlaneCache )
  5497. DMXELEMENT_UNPACK_FIELD( "Local Offset Min", "0 0 0", Vector, m_vecOffsetMin )
  5498. DMXELEMENT_UNPACK_FIELD( "Local Offset Max", "0 0 0", Vector, m_vecOffsetMax )
  5499. DMXELEMENT_UNPACK_FIELD( "Set Normal", "0", bool, m_bUseNormal )
  5500. END_PARTICLE_OPERATOR_UNPACK( C_INIT_CreateFromPlaneCache )
  5501. void C_INIT_CreateFromPlaneCache::InitNewParticlesScalar(
  5502. CParticleCollection *pParticles, int start_p,
  5503. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  5504. {
  5505. if ( !pParticles->m_pParent )
  5506. {
  5507. for( ; nParticleCount--; start_p++ )
  5508. {
  5509. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  5510. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  5511. SetVectorAttribute( xyz, vec3_origin );
  5512. SetVectorAttribute( pxyz, vec3_origin );
  5513. }
  5514. return;
  5515. }
  5516. CWorldCollideContextData **ppCtx;
  5517. if ( pParticles->m_pParent )
  5518. ppCtx = &( pParticles->m_pParent->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );
  5519. else
  5520. ppCtx = &( pParticles->m_pCollisionCacheData[COLLISION_MODE_INITIAL_TRACE_DOWN] );
  5521. CWorldCollideContextData *pCtx = NULL;
  5522. if ( ! *ppCtx )
  5523. {
  5524. *ppCtx = new CWorldCollideContextData;
  5525. (*ppCtx)->m_nActivePlanes = 0;
  5526. (*ppCtx)->m_nNumFixedPlanes = 0;
  5527. FourVectors fvEmpty;
  5528. fvEmpty.DuplicateVector( vec3_origin );
  5529. (*ppCtx)->m_PointOnPlane[0] = fvEmpty;
  5530. }
  5531. pCtx = *ppCtx;
  5532. if ( pCtx->m_nActivePlanes > 0 )
  5533. {
  5534. for( ; nParticleCount--; start_p++ )
  5535. {
  5536. int nIndex = pParticles->RandomInt( 0, pCtx->m_nActivePlanes - 1 );
  5537. Vector vecNormal = pCtx->m_PlaneNormal[nIndex].Vec( 0 );
  5538. Vector vecPointOnPlane = pCtx->m_PointOnPlane[nIndex].Vec( 0 );
  5539. if ( vecNormal == vec3_invalid || vecPointOnPlane == vec3_invalid )
  5540. {
  5541. float *plifespan = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
  5542. *plifespan = 0.0f;
  5543. }
  5544. else
  5545. {
  5546. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  5547. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  5548. FourVectors fvPoint = pCtx->m_PointOnPlane[nIndex];
  5549. Vector vPoint = fvPoint.Vec( 0 );
  5550. if ( bLocalOffset )
  5551. {
  5552. Vector randpos;
  5553. Vector OutputPos;
  5554. QAngle LocalCoords;
  5555. pParticles->RandomVector( m_vecOffsetMin, m_vecOffsetMax, &randpos );
  5556. randpos = Vector( randpos.z, randpos.y, randpos.x );
  5557. VectorAngles( vecNormal, LocalCoords );
  5558. VectorRotate( randpos, LocalCoords, OutputPos );
  5559. vPoint += OutputPos;
  5560. }
  5561. SetVectorAttribute( xyz, vPoint );
  5562. SetVectorAttribute( pxyz, vPoint );
  5563. if ( m_bUseNormal )
  5564. {
  5565. float *pNormal = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_NORMAL, start_p );
  5566. SetVectorAttribute( pNormal, vecNormal );
  5567. }
  5568. }
  5569. }
  5570. }
  5571. else
  5572. {
  5573. for( ; nParticleCount--; start_p++ )
  5574. {
  5575. float *xyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  5576. float *pxyz = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  5577. SetVectorAttribute( xyz, vec3_origin );
  5578. SetVectorAttribute( pxyz, vec3_origin );
  5579. }
  5580. }
  5581. }
  5582. //-----------------------------------------------------------------------------
  5583. // Model Cull Operator - cull particles inside or outside of a brush/animated model
  5584. //-----------------------------------------------------------------------------
  5585. class C_INIT_ModelCull : public CParticleInitializerOperatorInstance
  5586. {
  5587. DECLARE_PARTICLE_OPERATOR( C_INIT_ModelCull );
  5588. int m_nControlPointNumber;
  5589. bool m_bBoundBox;
  5590. bool m_bCullOutside;
  5591. char m_HitboxSetName[128];
  5592. uint32 GetWrittenAttributes( void ) const
  5593. {
  5594. return PARTICLE_ATTRIBUTE_LIFE_DURATION;
  5595. }
  5596. uint32 GetReadAttributes( void ) const
  5597. {
  5598. return PARTICLE_ATTRIBUTE_XYZ_MASK;
  5599. }
  5600. virtual uint64 GetReadControlPointMask() const
  5601. {
  5602. return 1ULL << m_nControlPointNumber;
  5603. }
  5604. void InitParams( CParticleSystemDefinition *pDef )
  5605. {
  5606. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  5607. }
  5608. bool InitMultipleOverride ( void ) { return true; }
  5609. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  5610. int nParticleCount, int nAttributeWriteMask,
  5611. void *pContext) const;
  5612. };
  5613. DEFINE_PARTICLE_OPERATOR( C_INIT_ModelCull , "Cull relative to model", OPERATOR_GENERIC );
  5614. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_ModelCull )
  5615. DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
  5616. DMXELEMENT_UNPACK_FIELD( "use only bounding box", "0", bool, m_bBoundBox )
  5617. DMXELEMENT_UNPACK_FIELD( "cull outside instead of inside", "0", bool, m_bCullOutside )
  5618. DMXELEMENT_UNPACK_FIELD_STRING( "hitbox set", "effects", m_HitboxSetName )
  5619. END_PARTICLE_OPERATOR_UNPACK( C_INIT_ModelCull )
  5620. void C_INIT_ModelCull::InitNewParticlesScalar(
  5621. CParticleCollection *pParticles, int start_p,
  5622. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  5623. {
  5624. pParticles->UpdateHitBoxInfo( m_nControlPointNumber, m_HitboxSetName );
  5625. if ( pParticles->ControlPointHitBox( m_nControlPointNumber ).CurAndPrevValid() )
  5626. {
  5627. for( ; nParticleCount--; start_p++ )
  5628. {
  5629. float *pXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  5630. float *pLifespan = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
  5631. Vector vecParticlePosition;
  5632. SetVectorFromAttribute( vecParticlePosition, pXYZ );
  5633. bool bInside = g_pParticleSystemMgr->Query()->IsPointInControllingObjectHitBox( pParticles, m_nControlPointNumber, vecParticlePosition, m_bBoundBox );
  5634. if ( ( bInside && m_bCullOutside ) || ( !bInside && !m_bCullOutside ))
  5635. continue;
  5636. *pLifespan = -1.0f;
  5637. }
  5638. }
  5639. }
  5640. //-----------------------------------------------------------------------------
  5641. // RtEnv Cull Operator - cull particles inside or outside of a RayTraceEnvironment
  5642. //-----------------------------------------------------------------------------
  5643. class C_INIT_RtEnvCull : public CParticleInitializerOperatorInstance
  5644. {
  5645. DECLARE_PARTICLE_OPERATOR( C_INIT_RtEnvCull );
  5646. Vector m_vecTestDir;
  5647. Vector m_vecTestNormal;
  5648. int m_nRtEnvNumber;
  5649. bool m_bUseVelocity;
  5650. bool m_bCullOnMiss;
  5651. bool m_bLifeAdjust;
  5652. char m_RtEnvName[128];
  5653. uint32 GetWrittenAttributes( void ) const
  5654. {
  5655. return PARTICLE_ATTRIBUTE_LIFE_DURATION;
  5656. }
  5657. uint32 GetReadAttributes( void ) const
  5658. {
  5659. return PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  5660. }
  5661. void InitParams( CParticleSystemDefinition *pDef )
  5662. {
  5663. m_nRtEnvNumber = g_pParticleSystemMgr->Query()->GetRayTraceEnvironmentFromName( m_RtEnvName );
  5664. }
  5665. bool InitMultipleOverride ( void ) { return true; }
  5666. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  5667. int nParticleCount, int nAttributeWriteMask,
  5668. void *pContext) const;
  5669. virtual void InitNewParticlesBlock( CParticleCollection *pParticles,
  5670. int start_block, int n_blocks, int nAttributeWriteMask,
  5671. void *pContext ) const;
  5672. };
  5673. DEFINE_PARTICLE_OPERATOR( C_INIT_RtEnvCull , "Cull relative to Ray Trace Environment", OPERATOR_GENERIC );
  5674. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RtEnvCull )
  5675. DMXELEMENT_UNPACK_FIELD( "cull on miss", "0", bool, m_bCullOnMiss )
  5676. DMXELEMENT_UNPACK_FIELD( "velocity test adjust lifespan", "0", bool, m_bLifeAdjust )
  5677. DMXELEMENT_UNPACK_FIELD( "use velocity for test direction", "0", bool, m_bUseVelocity )
  5678. DMXELEMENT_UNPACK_FIELD( "test direction", "0 0 1", Vector, m_vecTestDir )
  5679. DMXELEMENT_UNPACK_FIELD( "cull normal", "0 0 0", Vector, m_vecTestNormal )
  5680. DMXELEMENT_UNPACK_FIELD_STRING( "ray trace environment name", "PRECIPITATION", m_RtEnvName )
  5681. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RtEnvCull )
  5682. void C_INIT_RtEnvCull::InitNewParticlesScalar(
  5683. CParticleCollection *pParticles, int start_p,
  5684. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  5685. {
  5686. int32 skipid = NULL;
  5687. bool bUseNormal = m_vecTestNormal != vec3_origin;
  5688. if ( m_bUseVelocity )
  5689. {
  5690. fltx4 fl4PrevDt = ReplicateX4( pParticles->m_flPreviousDt );
  5691. for( ; nParticleCount--; start_p++ )
  5692. {
  5693. float *pXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  5694. float *pXYZPrev = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  5695. float *pLifespan = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
  5696. if ( *pLifespan == -1.0f )
  5697. continue;
  5698. Vector vecParticlePosition;
  5699. Vector vecParticlePositionPrev;
  5700. SetVectorFromAttribute( vecParticlePosition, pXYZ );
  5701. SetVectorFromAttribute( vecParticlePositionPrev, pXYZPrev );
  5702. FourVectors fvDirection;
  5703. fvDirection.DuplicateVector( vecParticlePosition - vecParticlePositionPrev );
  5704. fltx4 fl4Velocity = fvDirection.length();
  5705. fl4Velocity = DivSIMD ( fl4Velocity, fl4PrevDt );
  5706. fvDirection.VectorNormalizeFast();
  5707. FourRays frTr;
  5708. frTr.direction = fvDirection;
  5709. frTr.origin.DuplicateVector( vecParticlePosition );
  5710. fltx4 fl4TMin = Four_Zeros;
  5711. fltx4 fl4Lifespan = ReplicateX4( *pLifespan );
  5712. fltx4 fl4TMax = MulSIMD ( fl4Velocity, fl4Lifespan );
  5713. RayTracingResult Result;
  5714. g_pParticleSystemMgr->Query()->TraceAgainstRayTraceEnv( m_nRtEnvNumber, frTr, fl4TMin, fl4TMax, &Result, skipid );
  5715. bool bHit = Result.HitIds[0] != -1;
  5716. if ( bUseNormal )
  5717. {
  5718. bHit = bHit && Result.surface_normal.Vec( 0 ) == m_vecTestNormal;
  5719. }
  5720. if ( m_bLifeAdjust && bHit )
  5721. {
  5722. fl4Velocity = AddSIMD( fl4Velocity, Four_Epsilons );
  5723. fl4Lifespan = DivSIMD( Result.HitDistance, fl4Velocity );
  5724. //debug
  5725. //if ( SubFloat( fl4Lifespan, 0) != -1.0f && Result.HitIds[0] != -1 )
  5726. //{
  5727. // g_pParticleSystemMgr->Query()->DebugDrawLine( frTr.origin.Vec( 0 ), frTr.origin.Vec( 0 ) + ( frTr.direction.Vec ( 0 ) * SubFloat( Result.HitDistance, 0 ) ), 0, 0, 255, true, -1 );
  5728. // Vector vecEnd = frTr.origin.Vec( 0 ) + ( frTr.direction.Vec( 0 ) * ( SubFloat( fl4Lifespan, 0 ) * SubFloat( fl4Velocity, 0 ) ) ) ;
  5729. // g_pParticleSystemMgr->Query()->DebugDrawLine( frTr.origin.Vec( 0 ), vecEnd, 0, 255, 0, true, -1 );
  5730. //}
  5731. *pLifespan = MIN( SubFloat( fl4Lifespan, 0), *pLifespan );
  5732. continue;
  5733. }
  5734. if ( ( bHit && m_bCullOnMiss ) || ( !bHit && !m_bCullOnMiss ) )
  5735. continue;
  5736. *pLifespan = -1.0f;
  5737. }
  5738. }
  5739. else
  5740. {
  5741. for( ; nParticleCount--; start_p++ )
  5742. {
  5743. float *pXYZ = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_XYZ, start_p );
  5744. float *pLifespan = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, start_p );
  5745. if ( *pLifespan == -1.0f )
  5746. continue;
  5747. Vector vecParticlePosition;
  5748. SetVectorFromAttribute( vecParticlePosition, pXYZ );
  5749. FourRays frTr;
  5750. frTr.direction.DuplicateVector( m_vecTestDir );
  5751. frTr.origin.DuplicateVector( vecParticlePosition );
  5752. fltx4 fl4TMin = Four_Zeros;
  5753. fltx4 fl4TMax = Four_FLT_MAX;
  5754. RayTracingResult Result;
  5755. g_pParticleSystemMgr->Query()->TraceAgainstRayTraceEnv( m_nRtEnvNumber, frTr, fl4TMin, fl4TMax, &Result, skipid );
  5756. bool bHit = Result.HitIds[0] != -1;
  5757. if ( bUseNormal )
  5758. {
  5759. bHit = bHit && Result.surface_normal.Vec( 0 ) == m_vecTestNormal;
  5760. }
  5761. if ( ( bHit && m_bCullOnMiss && !bUseNormal ) || ( !bHit && !m_bCullOnMiss ))
  5762. continue;
  5763. *pLifespan = -1.0f;
  5764. }
  5765. }
  5766. }
  5767. void C_INIT_RtEnvCull::InitNewParticlesBlock( CParticleCollection *pParticles,
  5768. int start_block, int n_blocks, int nAttributeWriteMask,
  5769. void *pContext ) const
  5770. {
  5771. int32 skipid = NULL;
  5772. bool bUseNormal = m_vecTestNormal != vec3_origin;
  5773. size_t attr_stride;
  5774. const FourVectors *pXYZ = pParticles->Get4VAttributePtr( PARTICLE_ATTRIBUTE_XYZ, &attr_stride );
  5775. pXYZ += attr_stride * start_block;
  5776. const FourVectors *pXYZPrev = pParticles->Get4VAttributePtr( PARTICLE_ATTRIBUTE_PREV_XYZ, &attr_stride );
  5777. pXYZPrev += attr_stride * start_block;
  5778. fltx4 *pLifespan = pParticles->GetM128AttributePtrForWrite( PARTICLE_ATTRIBUTE_LIFE_DURATION, &attr_stride );
  5779. pLifespan += attr_stride * start_block;
  5780. bi32x4 fl4HitMask;
  5781. fltx4 fl4Lifespan;
  5782. if ( m_bUseVelocity )
  5783. {
  5784. fltx4 fl4PrevDt = ReplicateX4( pParticles->m_flPreviousDt );
  5785. while( n_blocks-- )
  5786. {
  5787. FourVectors fvDirection = *pXYZ;
  5788. fvDirection -= *pXYZPrev;
  5789. fltx4 fl4Velocity = fvDirection.length();
  5790. fl4Velocity = DivSIMD ( fl4Velocity, fl4PrevDt );
  5791. fl4Lifespan = *pLifespan;
  5792. if ( IsAllEqual( fl4Lifespan, Four_NegativeOnes ) )
  5793. {
  5794. pXYZ += attr_stride;
  5795. pXYZPrev += attr_stride;
  5796. pLifespan += attr_stride;
  5797. continue;
  5798. }
  5799. bi32x4 fl4SkipMask = CmpEqSIMD( fl4Lifespan, Four_NegativeOnes );
  5800. fvDirection.VectorNormalize();
  5801. FourRays frTr;
  5802. frTr.direction = fvDirection;
  5803. frTr.origin = *pXYZ;
  5804. fltx4 fl4TMin = Four_Zeros;
  5805. fltx4 fl4TMax = MulSIMD ( fl4Velocity, fl4Lifespan );
  5806. RayTracingResult Result;
  5807. g_pParticleSystemMgr->Query()->TraceAgainstRayTraceEnv( m_nRtEnvNumber, frTr, fl4TMin, fl4TMax, &Result, skipid );
  5808. i32x4 in4HitIds = LoadAlignedIntSIMD( Result.HitIds );
  5809. fltx4 fl4HitIds = SignedIntConvertToFltSIMD ( in4HitIds );
  5810. if ( m_bCullOnMiss )
  5811. {
  5812. fl4HitMask = CmpLtSIMD( fl4HitIds, Four_Zeros);
  5813. if ( bUseNormal )
  5814. {
  5815. FourVectors fvCullNormal;
  5816. fvCullNormal.DuplicateVector( m_vecTestNormal );
  5817. bi32x4 fl4NormalMask;
  5818. fl4NormalMask = CmpEqSIMD( fvCullNormal.x, Result.surface_normal.x );
  5819. fl4NormalMask = AndSIMD( fl4NormalMask, CmpEqSIMD( fvCullNormal.y, Result.surface_normal.y ) );
  5820. fl4NormalMask = AndSIMD( fl4NormalMask, CmpEqSIMD( fvCullNormal.z, Result.surface_normal.z ) );
  5821. fl4HitMask = OrSIMD( fl4HitMask, fl4NormalMask );
  5822. }
  5823. }
  5824. else
  5825. {
  5826. fl4HitMask = CmpGeSIMD( fl4HitIds, Four_Zeros);
  5827. if ( bUseNormal )
  5828. {
  5829. FourVectors fvCullNormal;
  5830. fvCullNormal.DuplicateVector( m_vecTestNormal );
  5831. bi32x4 fl4NormalMask;
  5832. fl4NormalMask = CmpEqSIMD( fvCullNormal.x, Result.surface_normal.x );
  5833. fl4NormalMask = AndSIMD( fl4NormalMask, CmpEqSIMD( fvCullNormal.y, Result.surface_normal.y ) );
  5834. fl4NormalMask = AndSIMD( fl4NormalMask, CmpEqSIMD( fvCullNormal.z, Result.surface_normal.z ) );
  5835. fl4HitMask = AndSIMD( fl4HitMask, fl4NormalMask );
  5836. }
  5837. }
  5838. if ( m_bLifeAdjust )
  5839. {
  5840. if ( IsAnyTrue( fl4HitMask ) )
  5841. {
  5842. fl4Velocity = AddSIMD( fl4Velocity, Four_Epsilons );
  5843. fl4Lifespan = DivSIMD( Result.HitDistance, fl4Velocity );
  5844. //debug
  5845. //for ( int i=0; i<4; ++i )
  5846. //{
  5847. // if ( SubFloat( fl4Lifespan, i) != -1.0f && Result.HitIds[i] != -1 )
  5848. // {
  5849. // g_pParticleSystemMgr->Query()->DebugDrawLine( frTr.origin.Vec( i ), frTr.origin.Vec( i ) + ( frTr.direction.Vec( i ) * SubFloat( Result.HitDistance, i ) ), 255, 0, 0, true, -1 );
  5850. // Vector vecEnd = frTr.origin.Vec( i ) + ( frTr.direction.Vec( i ) * ( SubFloat( fl4Lifespan, i ) * SubFloat( fl4Velocity, i ) ) ) ;
  5851. // g_pParticleSystemMgr->Query()->DebugDrawLine( frTr.origin.Vec( i ), vecEnd, 0, 255, 0, true, -1 );
  5852. // }
  5853. //}
  5854. //debug
  5855. //Don't Mess with Particles we should skip
  5856. fl4Lifespan = MaskedAssign( fl4SkipMask, Four_NegativeOnes, fl4Lifespan );
  5857. }
  5858. }
  5859. else
  5860. {
  5861. fl4Lifespan = Four_NegativeOnes;
  5862. }
  5863. fltx4 fl4MinLife = *pLifespan;
  5864. *pLifespan = MinSIMD( fl4MinLife, MaskedAssign( fl4HitMask, fl4Lifespan, *pLifespan ) );
  5865. pXYZ += attr_stride;
  5866. pXYZPrev += attr_stride;
  5867. pLifespan += attr_stride;
  5868. }
  5869. }
  5870. else
  5871. {
  5872. while( n_blocks-- )
  5873. {
  5874. FourRays frTr;
  5875. frTr.direction.DuplicateVector( m_vecTestDir );
  5876. frTr.origin = *pXYZ;
  5877. fltx4 fl4TMin = Four_Zeros;
  5878. fltx4 fl4TMax = Four_FLT_MAX;
  5879. fl4Lifespan = *pLifespan;
  5880. if ( IsAllEqual( fl4Lifespan, Four_NegativeOnes ) )
  5881. {
  5882. pXYZ += attr_stride;
  5883. pXYZPrev += attr_stride;
  5884. pLifespan += attr_stride;
  5885. continue;
  5886. }
  5887. RayTracingResult Result;
  5888. g_pParticleSystemMgr->Query()->TraceAgainstRayTraceEnv( m_nRtEnvNumber, frTr, fl4TMin, fl4TMax, &Result, skipid );
  5889. i32x4 in4HitIds = LoadAlignedIntSIMD( Result.HitIds );
  5890. fltx4 fl4HitIds = SignedIntConvertToFltSIMD ( in4HitIds );
  5891. if ( m_bCullOnMiss )
  5892. {
  5893. fl4HitMask = CmpLtSIMD( fl4HitIds, Four_Zeros);
  5894. if ( bUseNormal )
  5895. {
  5896. FourVectors fvCullNormal;
  5897. fvCullNormal.DuplicateVector( m_vecTestNormal );
  5898. bi32x4 fl4NormalMask;
  5899. fl4NormalMask = CmpEqSIMD( fvCullNormal.x, Result.surface_normal.x );
  5900. fl4NormalMask = AndSIMD( fl4NormalMask, CmpEqSIMD( fvCullNormal.y, Result.surface_normal.y ) );
  5901. fl4NormalMask = AndSIMD( fl4NormalMask, CmpEqSIMD( fvCullNormal.z, Result.surface_normal.z ) );
  5902. fl4HitMask = OrSIMD( fl4HitMask, fl4NormalMask );
  5903. }
  5904. }
  5905. else
  5906. {
  5907. fl4HitMask = CmpGeSIMD( fl4HitIds, Four_Zeros);
  5908. if ( bUseNormal )
  5909. {
  5910. FourVectors fvCullNormal;
  5911. fvCullNormal.DuplicateVector( m_vecTestNormal );
  5912. bi32x4 fl4NormalMask;
  5913. fl4NormalMask = CmpEqSIMD( fvCullNormal.x, Result.surface_normal.x );
  5914. fl4NormalMask = AndSIMD( fl4NormalMask, CmpEqSIMD( fvCullNormal.y, Result.surface_normal.y ) );
  5915. fl4NormalMask = AndSIMD( fl4NormalMask, CmpEqSIMD( fvCullNormal.z, Result.surface_normal.z ) );
  5916. fl4HitMask = AndSIMD( fl4HitMask, fl4NormalMask );
  5917. }
  5918. }
  5919. fl4Lifespan = Four_NegativeOnes;
  5920. *pLifespan = MaskedAssign( fl4HitMask, fl4Lifespan, *pLifespan );
  5921. pXYZ += attr_stride;
  5922. pXYZPrev += attr_stride;
  5923. pLifespan += attr_stride;
  5924. }
  5925. }
  5926. }
  5927. //-----------------------------------------------------------------------------
  5928. // Set Normal to CP Forward Vector
  5929. //-----------------------------------------------------------------------------
  5930. class C_INIT_NormalAlignToCP : public CParticleInitializerOperatorInstance
  5931. {
  5932. DECLARE_PARTICLE_OPERATOR( C_INIT_NormalAlignToCP );
  5933. uint32 GetWrittenAttributes( void ) const
  5934. {
  5935. return PARTICLE_ATTRIBUTE_NORMAL_MASK;
  5936. }
  5937. uint32 GetReadAttributes( void ) const
  5938. {
  5939. return 0;
  5940. }
  5941. virtual uint64 GetReadControlPointMask() const
  5942. {
  5943. return 1ULL << m_nControlPointNumber;
  5944. }
  5945. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  5946. int nParticleCount, int nAttributeWriteMask,
  5947. void *pContext) const;
  5948. void InitParams( CParticleSystemDefinition *pDef )
  5949. {
  5950. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  5951. }
  5952. private:
  5953. int m_nControlPointNumber;
  5954. Vector m_vOffsetMin;
  5955. Vector m_vOffsetMax;
  5956. };
  5957. DEFINE_PARTICLE_OPERATOR( C_INIT_NormalAlignToCP, "Normal Align to CP", OPERATOR_GENERIC );
  5958. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_NormalAlignToCP )
  5959. DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
  5960. END_PARTICLE_OPERATOR_UNPACK( C_INIT_NormalAlignToCP )
  5961. void C_INIT_NormalAlignToCP::InitNewParticlesScalar(
  5962. CParticleCollection *pParticles, int start_p,
  5963. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  5964. {
  5965. for( ; nParticleCount--; start_p++ )
  5966. {
  5967. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  5968. float *pNormal = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_NORMAL, start_p );
  5969. Vector vecForward;
  5970. Vector vecRight;
  5971. Vector vecUp;
  5972. pParticles->GetControlPointOrientationAtTime( m_nControlPointNumber, *ct, &vecForward, &vecRight, &vecUp );
  5973. SetVectorAttribute( pNormal, vecForward );
  5974. }
  5975. }
  5976. //-----------------------------------------------------------------------------
  5977. // Normal Offset Initializer
  5978. //-----------------------------------------------------------------------------
  5979. class C_INIT_NormalOffset : public CParticleInitializerOperatorInstance
  5980. {
  5981. DECLARE_PARTICLE_OPERATOR( C_INIT_NormalOffset );
  5982. Vector m_OffsetMin;
  5983. Vector m_OffsetMax;
  5984. int m_nControlPointNumber;
  5985. bool m_bLocalCoords;
  5986. bool m_bProportional;
  5987. bool m_bNormalize;
  5988. uint32 GetWrittenAttributes( void ) const
  5989. {
  5990. return PARTICLE_ATTRIBUTE_NORMAL_MASK | PARTICLE_ATTRIBUTE_CREATION_TIME_MASK;
  5991. }
  5992. uint32 GetReadAttributes( void ) const
  5993. {
  5994. return PARTICLE_ATTRIBUTE_NORMAL_MASK;
  5995. }
  5996. virtual uint64 GetReadControlPointMask() const
  5997. {
  5998. return 1ULL << m_nControlPointNumber;
  5999. }
  6000. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  6001. int nParticleCount, int nAttributeWriteMask,
  6002. void *pContext) const;
  6003. void InitParams( CParticleSystemDefinition *pDef )
  6004. {
  6005. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  6006. }
  6007. bool InitMultipleOverride ( void ) { return true; }
  6008. };
  6009. DEFINE_PARTICLE_OPERATOR( C_INIT_NormalOffset, "Normal Modify Offset Random", OPERATOR_GENERIC );
  6010. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_NormalOffset )
  6011. DMXELEMENT_UNPACK_FIELD( "control_point_number", "0", int, m_nControlPointNumber )
  6012. DMXELEMENT_UNPACK_FIELD( "offset min", "0 0 0", Vector, m_OffsetMin )
  6013. DMXELEMENT_UNPACK_FIELD( "offset max", "0 0 0", Vector, m_OffsetMax )
  6014. DMXELEMENT_UNPACK_FIELD( "offset in local space 0/1", "0", bool, m_bLocalCoords )
  6015. DMXELEMENT_UNPACK_FIELD( "normalize output 0/1", "0", bool, m_bNormalize )
  6016. END_PARTICLE_OPERATOR_UNPACK( C_INIT_NormalOffset )
  6017. void C_INIT_NormalOffset::InitNewParticlesScalar(
  6018. CParticleCollection *pParticles, int start_p,
  6019. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  6020. {
  6021. for( ; nParticleCount--; start_p++ )
  6022. {
  6023. const float *ct = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_CREATION_TIME, start_p );
  6024. float *pNormal = pParticles->GetFloatAttributePtrForWrite( PARTICLE_ATTRIBUTE_NORMAL, start_p );
  6025. Vector randpos;
  6026. pParticles->RandomVector( m_OffsetMin, m_OffsetMax, &randpos );
  6027. if ( m_bLocalCoords )
  6028. {
  6029. matrix3x4_t mat;
  6030. pParticles->GetControlPointTransformAtTime( m_nControlPointNumber, *ct, &mat );
  6031. Vector vecTransformLocal = vec3_origin;
  6032. VectorRotate( randpos, mat, vecTransformLocal );
  6033. randpos = vecTransformLocal;
  6034. }
  6035. Vector vecOffset;
  6036. SetVectorFromAttribute( vecOffset, pNormal );
  6037. vecOffset += randpos;
  6038. if ( m_bNormalize )
  6039. VectorNormalize( vecOffset );
  6040. SetVectorAttribute( pNormal, vecOffset );
  6041. }
  6042. }
  6043. //-----------------------------------------------------------------------------
  6044. // Remap Speed to Scalar Initializer
  6045. //-----------------------------------------------------------------------------
  6046. class C_INIT_RemapSpeedToScalar : public CParticleInitializerOperatorInstance
  6047. {
  6048. DECLARE_PARTICLE_OPERATOR( C_INIT_RemapSpeedToScalar );
  6049. uint32 GetWrittenAttributes( void ) const
  6050. {
  6051. return 1 << m_nFieldOutput;
  6052. }
  6053. uint32 GetReadAttributes( void ) const
  6054. {
  6055. return 1 << m_nFieldOutput | PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK;
  6056. }
  6057. uint32 GetFilter( void ) const
  6058. {
  6059. return FILTER_PARAMETER_REMAPPING_MASK;
  6060. }
  6061. void InitParams(CParticleSystemDefinition *pDef )
  6062. {
  6063. m_flInputMin = MAX(MIN_PARTICLE_SPEED, m_flInputMin);
  6064. m_flInputMax = MAX(MIN_PARTICLE_SPEED, m_flInputMax);
  6065. // clamp the result to 0 and 1 if it's alpha
  6066. if ( ATTRIBUTES_WHICH_ARE_0_TO_1 & ( 1 << m_nFieldOutput ) )
  6067. {
  6068. m_flOutputMin = clamp(m_flOutputMin, 0.0f, 1.0f );
  6069. m_flOutputMax = clamp(m_flOutputMax, 0.0f, 1.0f );
  6070. }
  6071. }
  6072. bool InitMultipleOverride ( void ) { return true; }
  6073. virtual void InitNewParticlesBlock( CParticleCollection *pParticles,
  6074. int start_block, int n_blocks, int nAttributeWriteMask,
  6075. void *pContext ) const;
  6076. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  6077. int nParticleCount, int nAttributeWriteMask,
  6078. void *pContext) const;
  6079. int m_nFieldOutput;
  6080. int m_nControlPointNumber;
  6081. float m_flStartTime;
  6082. float m_flEndTime;
  6083. float m_flInputMin;
  6084. float m_flInputMax;
  6085. float m_flOutputMin;
  6086. float m_flOutputMax;
  6087. bool m_bScaleInitialRange;
  6088. bool m_bPerParticle;
  6089. };
  6090. DEFINE_PARTICLE_OPERATOR( C_INIT_RemapSpeedToScalar, "Remap Speed to Scalar", OPERATOR_GENERIC );
  6091. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RemapSpeedToScalar )
  6092. DMXELEMENT_UNPACK_FIELD( "emitter lifetime start time (seconds)", "-1", float, m_flStartTime )
  6093. DMXELEMENT_UNPACK_FIELD( "emitter lifetime end time (seconds)", "-1", float, m_flEndTime )
  6094. DMXELEMENT_UNPACK_FIELD( "control point number (ignored if per particle)", "0", int, m_nControlPointNumber )
  6095. DMXELEMENT_UNPACK_FIELD( "per particle","0", bool, m_bPerParticle )
  6096. DMXELEMENT_UNPACK_FIELD( "input minimum","0", float, m_flInputMin )
  6097. DMXELEMENT_UNPACK_FIELD( "input maximum","1", float, m_flInputMax )
  6098. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "3", int, m_nFieldOutput, "intchoice particlefield_scalar" )
  6099. DMXELEMENT_UNPACK_FIELD( "output minimum","0", float, m_flOutputMin )
  6100. DMXELEMENT_UNPACK_FIELD( "output maximum","1", float, m_flOutputMax )
  6101. DMXELEMENT_UNPACK_FIELD( "output is scalar of initial random range","0", bool, m_bScaleInitialRange )
  6102. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapSpeedToScalar );
  6103. void C_INIT_RemapSpeedToScalar::InitNewParticlesBlock( CParticleCollection *pParticles,
  6104. int start_block, int n_blocks, int nAttributeWriteMask,
  6105. void *pContext ) const
  6106. {
  6107. if ( ( ( pParticles->m_flCurTime < m_flStartTime ) || ( pParticles->m_flCurTime >= m_flEndTime ) ) && ( ( m_flStartTime != -1.0f) && ( m_flEndTime != -1.0f) ) )
  6108. return;
  6109. size_t attr_stride;
  6110. const FourVectors *pXYZ = pParticles->Get4VAttributePtr( PARTICLE_ATTRIBUTE_XYZ, &attr_stride );
  6111. pXYZ += attr_stride * start_block;
  6112. const FourVectors *pPrevXYZ = pParticles->Get4VAttributePtr( PARTICLE_ATTRIBUTE_PREV_XYZ, &attr_stride );
  6113. pPrevXYZ += attr_stride * start_block;
  6114. fltx4 *pOutput = pParticles->GetM128AttributePtrForWrite( m_nFieldOutput, &attr_stride );
  6115. pOutput += attr_stride * start_block;
  6116. const fltx4 *pInitialOutput = pParticles->GetInitialM128AttributePtr( m_nFieldOutput, &attr_stride );
  6117. pInitialOutput += attr_stride * start_block;
  6118. fltx4 flMin = ReplicateX4( m_flOutputMin );
  6119. fltx4 flMax = ReplicateX4( m_flOutputMax );
  6120. fltx4 fl4Dt = ReplicateX4( pParticles->m_flPreviousDt );
  6121. fltx4 fl4InputMin = ReplicateX4( m_flInputMin );
  6122. fltx4 fl4InputMax = ReplicateX4( m_flInputMax );
  6123. if ( m_bPerParticle )
  6124. {
  6125. while( n_blocks-- )
  6126. {
  6127. fltx4 fl4Speed = DivSIMD ( (*pXYZ - *pPrevXYZ).length(), fl4Dt );
  6128. fltx4 fl4Output = RemapValClampedSIMD( fl4Speed, fl4InputMin, fl4InputMax, flMin, flMax );
  6129. if ( m_bScaleInitialRange )
  6130. {
  6131. fl4Output = MulSIMD( *pInitialOutput, fl4Output );
  6132. }
  6133. *( pOutput ) = fl4Output;
  6134. pXYZ += attr_stride;
  6135. pPrevXYZ += attr_stride;
  6136. pOutput += attr_stride;
  6137. pInitialOutput += attr_stride;
  6138. }
  6139. }
  6140. else
  6141. {
  6142. Vector vecPrevPos;
  6143. pParticles->GetControlPointAtPrevTime( m_nControlPointNumber, &vecPrevPos );
  6144. FourVectors fvDelta;
  6145. fvDelta.DuplicateVector( pParticles->GetControlPointAtCurrentTime( m_nControlPointNumber ) - vecPrevPos );
  6146. fltx4 fl4Speed = DivSIMD ( fvDelta.length(), fl4Dt );
  6147. fltx4 fl4Output = RemapValClampedSIMD( fl4Speed, fl4InputMin, fl4InputMax, flMin, flMax );
  6148. while( n_blocks-- )
  6149. {
  6150. if ( m_bScaleInitialRange )
  6151. {
  6152. fl4Output = MulSIMD( *pInitialOutput, fl4Output );
  6153. }
  6154. *( pOutput ) = fl4Output;
  6155. pOutput += attr_stride;
  6156. pInitialOutput += attr_stride;
  6157. }
  6158. }
  6159. }
  6160. void C_INIT_RemapSpeedToScalar::InitNewParticlesScalar(
  6161. CParticleCollection *pParticles, int start_p,
  6162. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  6163. {
  6164. if ( ( ( pParticles->m_flCurTime < m_flStartTime ) || ( pParticles->m_flCurTime >= m_flEndTime ) ) && ( ( m_flStartTime != -1.0f) && ( m_flEndTime != -1.0f) ) )
  6165. return;
  6166. if ( m_bPerParticle )
  6167. {
  6168. for( ; nParticleCount--; start_p++ )
  6169. {
  6170. const float *pXYZ = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_XYZ, start_p );
  6171. const float *pPrevXYZ = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_PREV_XYZ, start_p );
  6172. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  6173. Vector vecXYZ;
  6174. Vector vecPrevXYZ;
  6175. SetVectorFromAttribute ( vecXYZ, pXYZ );
  6176. SetVectorFromAttribute ( vecPrevXYZ, pPrevXYZ );
  6177. float flSpeed = (vecXYZ - vecPrevXYZ).Length() / pParticles->m_flPreviousDt;
  6178. float flOutput = RemapValClamped( flSpeed, m_flInputMin, m_flInputMax, m_flOutputMin, m_flOutputMax );
  6179. if ( m_bScaleInitialRange )
  6180. {
  6181. const float *pInitialOutput = pParticles->GetInitialFloatAttributePtr( m_nFieldOutput, start_p );
  6182. flOutput = *pInitialOutput * flOutput;
  6183. }
  6184. *pOutput = flOutput;
  6185. }
  6186. }
  6187. else
  6188. {
  6189. Vector vecPrevPos;
  6190. pParticles->GetControlPointAtPrevTime( m_nControlPointNumber, &vecPrevPos );
  6191. Vector vecDelta;
  6192. vecDelta = pParticles->GetControlPointAtCurrentTime( m_nControlPointNumber ) - vecPrevPos;
  6193. float flSpeed = vecDelta.Length() / pParticles->m_flPreviousDt;
  6194. float flOutput = RemapValClamped( flSpeed, m_flInputMin, m_flInputMax, m_flOutputMin, m_flOutputMax );
  6195. for( ; nParticleCount--; start_p++ )
  6196. {
  6197. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  6198. if ( m_bScaleInitialRange )
  6199. {
  6200. const float *pInitialOutput = pParticles->GetInitialFloatAttributePtr( m_nFieldOutput, start_p );
  6201. flOutput = *pInitialOutput * flOutput;
  6202. }
  6203. *pOutput = flOutput;
  6204. }
  6205. }
  6206. }
  6207. //-----------------------------------------------------------------------------
  6208. // CP Snapshot Initializer
  6209. //-----------------------------------------------------------------------------
  6210. class C_INIT_InitFromCPSnapshot : public CParticleInitializerOperatorInstance
  6211. {
  6212. DECLARE_PARTICLE_OPERATOR( C_INIT_InitFromCPSnapshot );
  6213. int m_nControlPointNumber;
  6214. int m_nAttributeToRead;
  6215. int m_nAttributeToWrite;
  6216. int m_nLocalSpaceCP;
  6217. virtual uint32 GetWrittenAttributes( void ) const
  6218. {
  6219. return ( ( m_nAttributeToWrite == -1 ) ? 0 : ( 1 << m_nAttributeToWrite ) );
  6220. }
  6221. virtual uint32 GetReadAttributes( void ) const
  6222. {
  6223. return 0;
  6224. }
  6225. virtual uint64 GetReadControlPointMask() const
  6226. {
  6227. uint64 nMask = 0;
  6228. if ( m_nControlPointNumber != -1 )
  6229. nMask |= ( 1ULL << m_nControlPointNumber );
  6230. if ( m_nLocalSpaceCP != -1 )
  6231. nMask |= ( 1ULL << m_nLocalSpaceCP );
  6232. return nMask;
  6233. }
  6234. virtual void InitParams( CParticleSystemDefinition *pDef, CDmxElement *pElement )
  6235. {
  6236. m_nControlPointNumber = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nControlPointNumber ) );
  6237. m_nLocalSpaceCP = MAX( 0, MIN( MAX_PARTICLE_CONTROL_POINTS-1, m_nLocalSpaceCP ) );
  6238. // If m_nAttributeToRead is not specified, just ask the snapshot for the written attribute
  6239. if ( m_nAttributeToRead == -1 )
  6240. m_nAttributeToRead = m_nAttributeToWrite;
  6241. }
  6242. // NOTE: InitNewParticlesBlock is not implemented, because InitNewParticlesScalar is just as efficient in this case
  6243. virtual void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  6244. int nParticleCount, int nAttributeWriteMask,
  6245. void *pContext) const;
  6246. };
  6247. DEFINE_PARTICLE_OPERATOR( C_INIT_InitFromCPSnapshot, "Init From CP Snapshot", OPERATOR_GENERIC );
  6248. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_InitFromCPSnapshot )
  6249. DMXELEMENT_UNPACK_FIELD( "snapshot control point number", "0", int, m_nControlPointNumber )
  6250. DMXELEMENT_UNPACK_FIELD_USERDATA( "field to write", "0", int, m_nAttributeToWrite, "intchoice particlefield" )
  6251. DMXELEMENT_UNPACK_FIELD_USERDATA( "field to read", "-1", int, m_nAttributeToRead, "intchoice particlefield" )
  6252. DMXELEMENT_UNPACK_FIELD( "local space control point number", "-1", int, m_nLocalSpaceCP )
  6253. END_PARTICLE_OPERATOR_UNPACK( C_INIT_InitFromCPSnapshot )
  6254. void C_INIT_InitFromCPSnapshot::InitNewParticlesScalar(
  6255. CParticleCollection *pParticles, int start_p,
  6256. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  6257. {
  6258. if ( m_nAttributeToWrite == -1 )
  6259. return;
  6260. CParticleSnapshot *pSnapshot = pParticles->GetControlPointSnapshot( m_nControlPointNumber );
  6261. EAttributeDataType nAttributeType = g_pParticleSystemMgr->GetParticleAttributeDataType( m_nAttributeToWrite );
  6262. // NOTE: HasAttribute() returns false if the read/write attributes are not of the same datatype
  6263. int nAttributeMask = ( 1 << m_nAttributeToWrite );
  6264. if ( pSnapshot && ( nAttributeWriteMask & nAttributeMask ) && pSnapshot->HasAttribute( m_nAttributeToRead, nAttributeType ) )
  6265. {
  6266. matrix3x4_t cpTransform;
  6267. int nTransformableAttributesMask = PARTICLE_ATTRIBUTE_XYZ_MASK | PARTICLE_ATTRIBUTE_PREV_XYZ_MASK | PARTICLE_ATTRIBUTE_NORMAL_MASK;
  6268. bool bUseCPTransform = ( m_nLocalSpaceCP != -1 ) && ( nAttributeMask & nTransformableAttributesMask );
  6269. if ( bUseCPTransform )
  6270. {
  6271. // Transform normals/positions as if they are specified in the local space of the given control point
  6272. pParticles->GetControlPointTransformAtCurrentTime( m_nLocalSpaceCP, &cpTransform );
  6273. if ( nAttributeMask == PARTICLE_ATTRIBUTE_NORMAL_MASK )
  6274. cpTransform[0][3] = cpTransform[1][3] = cpTransform[2][3] = 0.0f;
  6275. }
  6276. // Check we can read/write the specified data range:
  6277. Assert( pSnapshot->NumCols() >= ( start_p + nParticleCount ) );
  6278. Assert( pParticles->GetFloatAttributePtrForWrite( m_nAttributeToWrite, 0 ) );
  6279. if ( ( pSnapshot->NumCols() < ( start_p + nParticleCount ) ) ||
  6280. !pParticles->GetFloatAttributePtrForWrite( m_nAttributeToWrite, 0 ) )
  6281. return;
  6282. switch( nAttributeType )
  6283. {
  6284. case ATTRDATATYPE_FLOAT:
  6285. {
  6286. const float *pSrcAttributeData = pSnapshot->ElementPointer<float>( m_nAttributeToRead, start_p );
  6287. float *pDstAttributeData = pParticles->GetFloatAttributePtrForWrite( m_nAttributeToWrite, start_p );
  6288. memcpy( pDstAttributeData, pSrcAttributeData, nParticleCount*sizeof( float ) );
  6289. break;
  6290. }
  6291. case ATTRDATATYPE_INT:
  6292. {
  6293. const int *pSrcAttributeData = pSnapshot->ElementPointer<int>( m_nAttributeToRead, start_p );
  6294. int *pDstAttributeData = pParticles->GetIntAttributePtrForWrite( m_nAttributeToWrite, start_p );
  6295. memcpy( pDstAttributeData, pSrcAttributeData, nParticleCount*sizeof( int ) );
  6296. break;
  6297. }
  6298. case ATTRDATATYPE_4V:
  6299. {
  6300. // The source/dest data type is Vector, interleaved in FourVectors objects
  6301. // TODO: OPTIMIZATION: if the source/dest indices have the same alignment modulo 4, and the range is contiguous, then memcpy the middle N complete FourVectors directly (and apply cpTransform to FourVectors instead of Vector)
  6302. for( ; nParticleCount--; start_p++ )
  6303. {
  6304. const float *pSrcAttributeData = (const float *)pSnapshot->ElementPointer4V( m_nAttributeToRead, start_p );
  6305. float *pDstAttributeData = pParticles->GetFloatAttributePtrForWrite( m_nAttributeToWrite, start_p );
  6306. Vector srcAttributeData, srcAttributeDataTmp;
  6307. SetVectorFromAttribute( srcAttributeData, pSrcAttributeData );
  6308. if ( bUseCPTransform )
  6309. {
  6310. VectorTransform( srcAttributeData, cpTransform, srcAttributeDataTmp );
  6311. srcAttributeData = srcAttributeDataTmp;
  6312. }
  6313. SetVectorAttribute( pDstAttributeData, srcAttributeData );
  6314. }
  6315. break;
  6316. }
  6317. default:
  6318. // Don't support ATTRDATATYPE_POINTER
  6319. ExecuteNTimes( 10, Warning( "C_INIT_InitFromCPSnapshot: Unsupported attribute data type! (%d)\n", nAttributeType) );
  6320. Assert( 0 );
  6321. break;
  6322. }
  6323. }
  6324. }
  6325. //-----------------------------------------------------------------------------
  6326. // Parent Killed Particles Initializer
  6327. //-----------------------------------------------------------------------------
  6328. class C_INIT_InitFromParentKilled : public CParticleInitializerOperatorInstance
  6329. {
  6330. DECLARE_PARTICLE_OPERATOR( C_INIT_InitFromParentKilled );
  6331. int m_nAttributeToCopy;
  6332. char m_AttributeCopyName[128];
  6333. virtual uint32 GetWrittenAttributes( void ) const
  6334. {
  6335. return ( ( m_nAttributeToCopy == -1 ) ? 0 : ( 1 << m_nAttributeToCopy ) );
  6336. }
  6337. virtual uint32 GetReadAttributes( void ) const
  6338. {
  6339. // TODO: this refers to parent attributes.... errr....
  6340. return ( ( m_nAttributeToCopy == -1 ) ? 0 : ( 1 << m_nAttributeToCopy ) );
  6341. }
  6342. virtual bool ShouldRun( bool bApplyingParentKillList ) const
  6343. {
  6344. return bApplyingParentKillList;
  6345. }
  6346. // TODO: implement InitNewParticlesBlock (requires 'gather' memory reads, though)
  6347. virtual void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  6348. int nParticleCount, int nAttributeWriteMask,
  6349. void *pContext) const;
  6350. };
  6351. DEFINE_PARTICLE_OPERATOR( C_INIT_InitFromParentKilled, "Init From Killed Parent Particle", OPERATOR_GENERIC );
  6352. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_InitFromParentKilled )
  6353. DMXELEMENT_UNPACK_FIELD_USERDATA( "field to init", "-1", int, m_nAttributeToCopy, "intchoice particlefield" )
  6354. END_PARTICLE_OPERATOR_UNPACK( C_INIT_InitFromParentKilled )
  6355. void C_INIT_InitFromParentKilled::InitNewParticlesScalar(
  6356. CParticleCollection *pParticles, int start_p,
  6357. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  6358. {
  6359. if ( ( m_nAttributeToCopy == -1 ) || !( nAttributeWriteMask & ( 1 << m_nAttributeToCopy ) ) )
  6360. return;
  6361. int nNumParticlesToKill;
  6362. KillListItem_t *pKillList = GetParentKillList( pParticles, nNumParticlesToKill );
  6363. Assert( pKillList && ( nNumParticlesToKill > 0 ) );
  6364. if ( !pKillList || ( nNumParticlesToKill <= 0 ) )
  6365. return;
  6366. Assert( HasAttribute( pParticles, m_nAttributeToCopy ) && HasAttribute( pParticles->m_pParent, m_nAttributeToCopy ) );
  6367. int nKillListIndex = nNumParticlesToKill + ( start_p - pParticles->m_nActiveParticles );
  6368. Assert( ( nKillListIndex >= 0 ) && ( ( nKillListIndex + nParticleCount ) <= nNumParticlesToKill ) );
  6369. EAttributeDataType nAttributeType = g_pParticleSystemMgr->GetParticleAttributeDataType( m_nAttributeToCopy );
  6370. for ( int nWriteIndex = start_p; nWriteIndex < ( start_p + nParticleCount ) ; nWriteIndex++ )
  6371. {
  6372. unsigned int nReadIndex = pKillList[ nKillListIndex++ ].nIndex;
  6373. switch( nAttributeType )
  6374. {
  6375. case ATTRDATATYPE_FLOAT:
  6376. {
  6377. const float *pSrcAttributeData = pParticles->m_pParent->GetFloatAttributePtr( m_nAttributeToCopy, nReadIndex );
  6378. float *pDstAttributeData = pParticles->GetFloatAttributePtrForWrite( m_nAttributeToCopy, nWriteIndex );
  6379. pDstAttributeData[0] = pSrcAttributeData[0];
  6380. break;
  6381. }
  6382. case ATTRDATATYPE_INT:
  6383. {
  6384. const int *pSrcAttributeData = pParticles->m_pParent->GetIntAttributePtr( m_nAttributeToCopy, nReadIndex );
  6385. int *pDstAttributeData = pParticles->GetIntAttributePtrForWrite( m_nAttributeToCopy, nWriteIndex );
  6386. pDstAttributeData[0] = pSrcAttributeData[0];
  6387. break;
  6388. }
  6389. case ATTRDATATYPE_4V:
  6390. {
  6391. // The source/dest data type is Vector, interleaved in FourVectors objects
  6392. const float *pSrcAttributeData = pParticles->m_pParent->GetFloatAttributePtr( m_nAttributeToCopy, nReadIndex );
  6393. float *pDstAttributeData = pParticles->GetFloatAttributePtrForWrite( m_nAttributeToCopy, nWriteIndex );
  6394. Vector srcAttributeData;
  6395. SetVectorFromAttribute( srcAttributeData, pSrcAttributeData );
  6396. SetVectorAttribute( pDstAttributeData, srcAttributeData );
  6397. break;
  6398. }
  6399. default:
  6400. // Don't support ATTRDATATYPE_POINTER
  6401. ExecuteNTimes( 10, Warning( "C_INIT_InitFromParentKilled: Unsupported attribute data type! (%d)\n", nAttributeType) );
  6402. Assert( 0 );
  6403. break;
  6404. }
  6405. }
  6406. }
  6407. class C_INIT_RemapInitialDirectionToCPToVector : public CParticleInitializerOperatorInstance
  6408. {
  6409. DECLARE_PARTICLE_OPERATOR( C_INIT_RemapInitialDirectionToCPToVector );
  6410. uint32 GetWrittenAttributes( void ) const
  6411. {
  6412. return 1 << m_nFieldOutput;
  6413. }
  6414. uint32 GetReadAttributes( void ) const
  6415. {
  6416. return PARTICLE_ATTRIBUTE_XYZ_MASK;
  6417. }
  6418. virtual uint64 GetReadControlPointMask() const
  6419. {
  6420. return 1ULL << m_nCP;
  6421. }
  6422. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  6423. int nParticleCount, int nAttributeWriteMask,
  6424. void *pContext) const;
  6425. virtual void InitNewParticlesBlock( CParticleCollection *pParticles,
  6426. int start_block, int n_blocks, int nAttributeWriteMask,
  6427. void *pContext ) const;
  6428. int m_nCP;
  6429. int m_nFieldOutput;
  6430. float m_flScale;
  6431. float m_flOffsetRot;
  6432. Vector m_vecOffsetAxis;
  6433. bool m_bNormalize;
  6434. };
  6435. DEFINE_PARTICLE_OPERATOR( C_INIT_RemapInitialDirectionToCPToVector, "Remap Initial Direction to CP to Vector", OPERATOR_GENERIC );
  6436. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RemapInitialDirectionToCPToVector )
  6437. DMXELEMENT_UNPACK_FIELD( "control point","0", int, m_nCP )
  6438. DMXELEMENT_UNPACK_FIELD_USERDATA( "output field", "0", int, m_nFieldOutput, "intchoice particlefield_vector" )
  6439. DMXELEMENT_UNPACK_FIELD( "normalize","0", bool, m_bNormalize )
  6440. DMXELEMENT_UNPACK_FIELD( "offset axis","0 0 0", Vector, m_vecOffsetAxis )
  6441. DMXELEMENT_UNPACK_FIELD( "offset rotation","0", float, m_flOffsetRot )
  6442. DMXELEMENT_UNPACK_FIELD( "scale factor" , "1", float, m_flScale )
  6443. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapInitialDirectionToCPToVector )
  6444. void C_INIT_RemapInitialDirectionToCPToVector::InitNewParticlesScalar(
  6445. CParticleCollection *pParticles, int start_p,
  6446. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  6447. {
  6448. matrix3x4_t matRot;
  6449. MatrixBuildRotationAboutAxis ( m_vecOffsetAxis, m_flOffsetRot, matRot );
  6450. Vector vecCPPos = pParticles->GetControlPointAtCurrentTime( m_nCP );
  6451. for( ; nParticleCount--; start_p++ )
  6452. {
  6453. const float *pXYZ = pParticles->GetFloatAttributePtr( PARTICLE_ATTRIBUTE_XYZ, start_p );
  6454. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  6455. Vector vecXYZ, vecOutput;
  6456. SetVectorFromAttribute ( vecXYZ, pXYZ );
  6457. vecXYZ -= vecCPPos;
  6458. VectorRotate( vecXYZ, matRot, vecOutput );
  6459. if ( m_bNormalize )
  6460. vecOutput.NormalizeInPlace();
  6461. vecOutput *= m_flScale;
  6462. SetVectorAttribute( pOutput, vecOutput );
  6463. }
  6464. }
  6465. void C_INIT_RemapInitialDirectionToCPToVector::InitNewParticlesBlock( CParticleCollection *pParticles,
  6466. int start_block, int n_blocks, int nAttributeWriteMask,
  6467. void *pContext ) const
  6468. {
  6469. C4VAttributeIterator xyz( PARTICLE_ATTRIBUTE_XYZ, pParticles );
  6470. C4VAttributeWriteIterator pOutField( m_nFieldOutput, pParticles) ;
  6471. int nCtr = pParticles->m_nPaddedActiveParticles;
  6472. FourVectors v4CPPosition;
  6473. FourVectors v4Offset;
  6474. matrix3x4_t matRot;
  6475. MatrixBuildRotationAboutAxis ( m_vecOffsetAxis, m_flOffsetRot, matRot );
  6476. v4CPPosition.DuplicateVector( pParticles->GetControlPointAtCurrentTime( m_nCP ) );
  6477. if ( m_bNormalize )
  6478. {
  6479. fltx4 fl4Scale = ReplicateX4( m_flScale );
  6480. do
  6481. {
  6482. FourVectors v4Vel = *xyz;
  6483. v4Vel -= v4CPPosition;
  6484. v4Vel.RotateBy( matRot );
  6485. v4Vel.VectorNormalize();
  6486. v4Vel *= fl4Scale;
  6487. *pOutField = v4Vel;
  6488. ++pOutField;
  6489. ++xyz;
  6490. } while( --nCtr );
  6491. }
  6492. else
  6493. {
  6494. fltx4 fl4Scale = ReplicateX4( m_flScale * 1.0 / ( MAX( 1.0e-20, pParticles->m_flPreviousDt ) ) );
  6495. do
  6496. {
  6497. FourVectors v4Vel = *xyz;
  6498. v4Vel -= v4CPPosition;
  6499. v4Vel += v4Offset;
  6500. v4Vel.RotateBy( matRot );
  6501. v4Vel *= fl4Scale;
  6502. *pOutField = v4Vel;
  6503. ++pOutField;
  6504. ++xyz;
  6505. } while( --nCtr );
  6506. }
  6507. }
  6508. class C_INIT_RemapInitialCPDirectionToRotation : public CParticleInitializerOperatorInstance
  6509. {
  6510. DECLARE_PARTICLE_OPERATOR( C_INIT_RemapInitialCPDirectionToRotation );
  6511. uint32 GetWrittenAttributes( void ) const
  6512. {
  6513. return 1 << m_nFieldOutput;
  6514. }
  6515. uint32 GetReadAttributes( void ) const
  6516. {
  6517. return PARTICLE_ATTRIBUTE_XYZ_MASK;
  6518. }
  6519. virtual void InitParams( CParticleSystemDefinition *pDef )
  6520. {
  6521. m_nAxis = MAX( 0, MIN( 2, m_nAxis ) );
  6522. if ( m_nAxis == 0 )
  6523. {
  6524. m_nComponent1 = 1;
  6525. m_nComponent2 = 0;
  6526. }
  6527. else if ( m_nAxis == 1 )
  6528. {
  6529. m_nComponent1 = 2;
  6530. m_nComponent2 = 0;
  6531. }
  6532. else if ( m_nAxis == 2 )
  6533. {
  6534. m_nComponent1 = 2;
  6535. m_nComponent2 = 1;
  6536. }
  6537. }
  6538. void InitNewParticlesScalar( CParticleCollection *pParticles, int start_p,
  6539. int nParticleCount, int nAttributeWriteMask,
  6540. void *pContext) const;
  6541. int m_nCP;
  6542. int m_nFieldOutput;
  6543. int m_nAxis;
  6544. float m_flScale;
  6545. float m_flOffsetRot;
  6546. int m_nComponent1;
  6547. int m_nComponent2;
  6548. };
  6549. DEFINE_PARTICLE_OPERATOR( C_INIT_RemapInitialCPDirectionToRotation, "Remap CP Orientation to Rotation", OPERATOR_GENERIC );
  6550. BEGIN_PARTICLE_INITIALIZER_OPERATOR_UNPACK( C_INIT_RemapInitialCPDirectionToRotation )
  6551. DMXELEMENT_UNPACK_FIELD( "control point","0", int, m_nCP )
  6552. DMXELEMENT_UNPACK_FIELD_USERDATA( "rotation field", "0", int, m_nFieldOutput, "intchoice particlefield_rotation" )
  6553. DMXELEMENT_UNPACK_FIELD( "axis","0", int, m_nAxis )
  6554. DMXELEMENT_UNPACK_FIELD( "offset rotation","0", float, m_flOffsetRot )
  6555. END_PARTICLE_OPERATOR_UNPACK( C_INIT_RemapInitialCPDirectionToRotation )
  6556. void C_INIT_RemapInitialCPDirectionToRotation::InitNewParticlesScalar(
  6557. CParticleCollection *pParticles, int start_p,
  6558. int nParticleCount, int nAttributeWriteMask, void *pContext ) const
  6559. {
  6560. float flRotOffset = m_flOffsetRot * ( M_PI / 180.0f );
  6561. Vector vecCPRotation[3];
  6562. pParticles->GetControlPointOrientationAtCurrentTime( m_nCP, &vecCPRotation[0], &vecCPRotation[1], &vecCPRotation[2] );
  6563. float flRot = atan2(vecCPRotation[m_nAxis][m_nComponent1], vecCPRotation[m_nAxis][m_nComponent2] ) + M_PI;
  6564. flRot += flRotOffset;
  6565. for( ; nParticleCount--; start_p++ )
  6566. {
  6567. float *pOutput = pParticles->GetFloatAttributePtrForWrite( m_nFieldOutput, start_p );
  6568. *pOutput = flRot;
  6569. }
  6570. }
  6571. //
  6572. //
  6573. //
  6574. //
  6575. //-----------------------------------------------------------------------------
  6576. // Purpose: Add all operators to be considered active, here
  6577. //-----------------------------------------------------------------------------
  6578. void AddBuiltInParticleInitializers( void )
  6579. {
  6580. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RingWave );
  6581. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateAlongPath );
  6582. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_MoveBetweenPoints );
  6583. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateWithinSphere );
  6584. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_VelocityRandom );
  6585. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateOnModel );
  6586. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateWithinBox );
  6587. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomRotationSpeed );
  6588. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomLifeTime );
  6589. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomAlpha );
  6590. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomRadius );
  6591. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomRotation );
  6592. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomYaw );
  6593. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomColor );
  6594. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_ColorLitPerParticle );
  6595. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomTrailLength );
  6596. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomSequence );
  6597. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_PositionOffset );
  6598. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_PositionWarp );
  6599. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_PositionPlaceOnGround );
  6600. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreationNoise );
  6601. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_InitialVelocityNoise );
  6602. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapScalar );
  6603. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapParticleCountToScalar );
  6604. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_InheritVelocity );
  6605. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_AgeNoise );
  6606. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_SequenceLifeTime );
  6607. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateInHierarchy );
  6608. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapScalarToVector );
  6609. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_OffsetVectorToVector );
  6610. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateSequentialPath );
  6611. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_InitialRepulsionVelocity );
  6612. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomYawFlip );
  6613. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomSecondSequence );
  6614. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapCPtoScalar );
  6615. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapCPtoVector );
  6616. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapSpeedToScalar );
  6617. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateFromParentParticles );
  6618. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_DistanceToCPInit );
  6619. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_LifespanFromVelocity );
  6620. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateFromPlaneCache );
  6621. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_ChaoticAttractor );
  6622. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_ModelCull );
  6623. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RtEnvCull );
  6624. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_NormalAlignToCP );
  6625. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_NormalOffset );
  6626. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_VelocityFromCP );
  6627. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_SequenceFromCP );
  6628. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_InitFromCPSnapshot );
  6629. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_InitFromParentKilled );
  6630. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_SetHitboxToModel );
  6631. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_InheritFromParentParticles );
  6632. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomVector );
  6633. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomVectorComponent );
  6634. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RandomScalar );
  6635. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_CreateInEpitrochoid );
  6636. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_SetHitboxToClosest );
  6637. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapInitialDirectionToCPToVector );
  6638. REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_RemapInitialCPDirectionToRotation );
  6639. // REGISTER_PARTICLE_OPERATOR( FUNCTION_INITIALIZER, C_INIT_OffsetToCP );
  6640. }