LightningStorm.js 7.6 KB

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