LightningStorm.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import {
  2. MathUtils,
  3. Mesh,
  4. MeshBasicMaterial,
  5. Object3D
  6. } from 'three';
  7. import { LightningStrike } from '../geometries/LightningStrike.js';
  8. /**
  9. * @fileoverview Lightning strike object generator
  10. *
  11. *
  12. * Usage
  13. *
  14. * const myStorm = new LightningStorm( paramsObject );
  15. * myStorm.position.set( ... );
  16. * scene.add( myStorm );
  17. * ...
  18. * myStorm.update( currentTime );
  19. *
  20. * The "currentTime" can only go forwards or be stopped.
  21. *
  22. *
  23. * LightningStorm parameters:
  24. *
  25. * @param {double} size Size of the storm. If no 'onRayPosition' parameter is defined, it means the side of the rectangle the storm covers.
  26. *
  27. * @param {double} minHeight Minimum height a ray can start at. If no 'onRayPosition' parameter is defined, it means the height above plane y = 0.
  28. *
  29. * @param {double} maxHeight Maximum height a ray can start at. If no 'onRayPosition' parameter is defined, it means the height above plane y = 0.
  30. *
  31. * @param {double} maxSlope The maximum inclination slope of a ray. If no 'onRayPosition' parameter is defined, it means the slope relative to plane y = 0.
  32. *
  33. * @param {integer} maxLightnings Greater than 0. The maximum number of simultaneous rays.
  34. *
  35. * @param {double} lightningMinPeriod minimum time between two consecutive rays.
  36. *
  37. * @param {double} lightningMaxPeriod maximum time between two consecutive rays.
  38. *
  39. * @param {double} lightningMinDuration The minimum time a ray can last.
  40. *
  41. * @param {double} lightningMaxDuration The maximum time a ray can last.
  42. *
  43. * @param {Object} lightningParameters The parameters for created rays. See LightningStrike (geometry)
  44. *
  45. * @param {Material} lightningMaterial The THREE.Material used for the created rays.
  46. *
  47. * @param {function} onRayPosition Optional callback with two Vector3 parameters (source, dest). You can set here the start and end points for each created ray, using the standard size, minHeight, etc parameters and other values in your algorithm.
  48. *
  49. * @param {function} onLightningDown This optional callback is called with one parameter (lightningStrike) when a ray ends propagating, so it has hit the ground.
  50. *
  51. *
  52. */
  53. class LightningStorm extends Object3D {
  54. constructor( stormParams = {} ) {
  55. super();
  56. this.isLightningStorm = true;
  57. // Parameters
  58. this.stormParams = stormParams;
  59. stormParams.size = stormParams.size !== undefined ? stormParams.size : 1000.0;
  60. stormParams.minHeight = stormParams.minHeight !== undefined ? stormParams.minHeight : 80.0;
  61. stormParams.maxHeight = stormParams.maxHeight !== undefined ? stormParams.maxHeight : 100.0;
  62. stormParams.maxSlope = stormParams.maxSlope !== undefined ? stormParams.maxSlope : 1.1;
  63. stormParams.maxLightnings = stormParams.maxLightnings !== undefined ? stormParams.maxLightnings : 3;
  64. stormParams.lightningMinPeriod = stormParams.lightningMinPeriod !== undefined ? stormParams.lightningMinPeriod : 3.0;
  65. stormParams.lightningMaxPeriod = stormParams.lightningMaxPeriod !== undefined ? stormParams.lightningMaxPeriod : 7.0;
  66. stormParams.lightningMinDuration = stormParams.lightningMinDuration !== undefined ? stormParams.lightningMinDuration : 1.0;
  67. stormParams.lightningMaxDuration = stormParams.lightningMaxDuration !== undefined ? stormParams.lightningMaxDuration : 2.5;
  68. this.lightningParameters = LightningStrike.copyParameters( stormParams.lightningParameters, stormParams.lightningParameters );
  69. this.lightningParameters.isEternal = false;
  70. this.lightningMaterial = stormParams.lightningMaterial !== undefined ? stormParams.lightningMaterial : new MeshBasicMaterial( { color: 0xB0FFFF } );
  71. if ( stormParams.onRayPosition !== undefined ) {
  72. this.onRayPosition = stormParams.onRayPosition;
  73. } else {
  74. this.onRayPosition = function ( source, dest ) {
  75. dest.set( ( Math.random() - 0.5 ) * stormParams.size, 0, ( Math.random() - 0.5 ) * stormParams.size );
  76. const height = MathUtils.lerp( stormParams.minHeight, stormParams.maxHeight, Math.random() );
  77. source.set( stormParams.maxSlope * ( 2 * Math.random() - 1 ), 1, stormParams.maxSlope * ( 2 * Math.random() - 1 ) ).multiplyScalar( height ).add( dest );
  78. };
  79. }
  80. this.onLightningDown = stormParams.onLightningDown;
  81. // Internal state
  82. this.inited = false;
  83. this.nextLightningTime = 0;
  84. this.lightningsMeshes = [];
  85. this.deadLightningsMeshes = [];
  86. for ( let i = 0; i < this.stormParams.maxLightnings; i ++ ) {
  87. const lightning = new LightningStrike( LightningStrike.copyParameters( {}, this.lightningParameters ) );
  88. const mesh = new Mesh( lightning, this.lightningMaterial );
  89. this.deadLightningsMeshes.push( mesh );
  90. }
  91. }
  92. update( time ) {
  93. if ( ! this.inited ) {
  94. this.nextLightningTime = this.getNextLightningTime( time ) * Math.random();
  95. this.inited = true;
  96. }
  97. if ( time >= this.nextLightningTime ) {
  98. // Lightning creation
  99. const lightningMesh = this.deadLightningsMeshes.pop();
  100. if ( lightningMesh ) {
  101. const lightningParams1 = LightningStrike.copyParameters( lightningMesh.geometry.rayParameters, this.lightningParameters );
  102. lightningParams1.birthTime = time;
  103. lightningParams1.deathTime = time + MathUtils.lerp( this.stormParams.lightningMinDuration, this.stormParams.lightningMaxDuration, Math.random() );
  104. this.onRayPosition( lightningParams1.sourceOffset, lightningParams1.destOffset );
  105. lightningParams1.noiseSeed = Math.random();
  106. this.add( lightningMesh );
  107. this.lightningsMeshes.push( lightningMesh );
  108. }
  109. // Schedule next lightning
  110. this.nextLightningTime = this.getNextLightningTime( time );
  111. }
  112. let i = 0, il = this.lightningsMeshes.length;
  113. while ( i < il ) {
  114. const mesh = this.lightningsMeshes[ i ];
  115. const lightning = mesh.geometry;
  116. const prevState = lightning.state;
  117. lightning.update( time );
  118. if ( prevState === LightningStrike.RAY_PROPAGATING && lightning.state > prevState ) {
  119. if ( this.onLightningDown ) {
  120. this.onLightningDown( lightning );
  121. }
  122. }
  123. if ( lightning.state === LightningStrike.RAY_EXTINGUISHED ) {
  124. // Lightning is to be destroyed
  125. this.lightningsMeshes.splice( this.lightningsMeshes.indexOf( mesh ), 1 );
  126. this.deadLightningsMeshes.push( mesh );
  127. this.remove( mesh );
  128. il --;
  129. } else {
  130. i ++;
  131. }
  132. }
  133. }
  134. getNextLightningTime( currentTime ) {
  135. return currentTime + MathUtils.lerp( this.stormParams.lightningMinPeriod, this.stormParams.lightningMaxPeriod, Math.random() ) / ( this.stormParams.maxLightnings + 1 );
  136. }
  137. copy( source, recursive ) {
  138. super.copy( source, recursive );
  139. this.stormParams.size = source.stormParams.size;
  140. this.stormParams.minHeight = source.stormParams.minHeight;
  141. this.stormParams.maxHeight = source.stormParams.maxHeight;
  142. this.stormParams.maxSlope = source.stormParams.maxSlope;
  143. this.stormParams.maxLightnings = source.stormParams.maxLightnings;
  144. this.stormParams.lightningMinPeriod = source.stormParams.lightningMinPeriod;
  145. this.stormParams.lightningMaxPeriod = source.stormParams.lightningMaxPeriod;
  146. this.stormParams.lightningMinDuration = source.stormParams.lightningMinDuration;
  147. this.stormParams.lightningMaxDuration = source.stormParams.lightningMaxDuration;
  148. this.lightningParameters = LightningStrike.copyParameters( {}, source.lightningParameters );
  149. this.lightningMaterial = source.stormParams.lightningMaterial;
  150. this.onLightningDown = source.onLightningDown;
  151. return this;
  152. }
  153. clone() {
  154. return new this.constructor( this.stormParams ).copy( this );
  155. }
  156. }
  157. export { LightningStorm };