|
@@ -109,910 +109,909 @@ import { SimplexNoise } from '../math/SimplexNoise.js';
|
|
*
|
|
*
|
|
*/
|
|
*/
|
|
|
|
|
|
-var LightningStrike = function ( rayParameters ) {
|
|
|
|
|
|
+class LightningStrike extends BufferGeometry {
|
|
|
|
|
|
- BufferGeometry.call( this );
|
|
|
|
|
|
+ constructor( rayParameters = {} ) {
|
|
|
|
|
|
- this.type = 'LightningStrike';
|
|
|
|
|
|
+ super();
|
|
|
|
|
|
- // Set parameters, and set undefined parameters to default values
|
|
|
|
- rayParameters = rayParameters || {};
|
|
|
|
- this.init( LightningStrike.copyParameters( rayParameters, rayParameters ) );
|
|
|
|
|
|
+ this.type = 'LightningStrike';
|
|
|
|
|
|
- // Creates and populates the mesh
|
|
|
|
- this.createMesh();
|
|
|
|
|
|
+ // Set parameters, and set undefined parameters to default values
|
|
|
|
+ this.init( LightningStrike.copyParameters( rayParameters, rayParameters ) );
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ // Creates and populates the mesh
|
|
|
|
+ this.createMesh();
|
|
|
|
|
|
-LightningStrike.prototype = Object.create( BufferGeometry.prototype );
|
|
|
|
-
|
|
|
|
-LightningStrike.prototype.constructor = LightningStrike;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
-LightningStrike.prototype.isLightningStrike = true;
|
|
|
|
|
|
+ static createRandomGenerator() {
|
|
|
|
|
|
-// Ray states
|
|
|
|
-LightningStrike.RAY_INITIALIZED = 0;
|
|
|
|
-LightningStrike.RAY_UNBORN = 1;
|
|
|
|
-LightningStrike.RAY_PROPAGATING = 2;
|
|
|
|
-LightningStrike.RAY_STEADY = 3;
|
|
|
|
-LightningStrike.RAY_VANISHING = 4;
|
|
|
|
-LightningStrike.RAY_EXTINGUISHED = 5;
|
|
|
|
|
|
+ const numSeeds = 2053;
|
|
|
|
+ const seeds = [];
|
|
|
|
|
|
-LightningStrike.COS30DEG = Math.cos( 30 * Math.PI / 180 );
|
|
|
|
-LightningStrike.SIN30DEG = Math.sin( 30 * Math.PI / 180 );
|
|
|
|
|
|
+ for ( let i = 0; i < numSeeds; i ++ ) {
|
|
|
|
|
|
-LightningStrike.createRandomGenerator = function () {
|
|
|
|
|
|
+ seeds.push( Math.random() );
|
|
|
|
|
|
- var numSeeds = 2053;
|
|
|
|
- var seeds = [];
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- for ( var i = 0; i < numSeeds; i ++ ) {
|
|
|
|
|
|
+ const generator = {
|
|
|
|
|
|
- seeds.push( Math.random() );
|
|
|
|
|
|
+ currentSeed: 0,
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ random: function () {
|
|
|
|
|
|
- var generator = {
|
|
|
|
|
|
+ const value = seeds[ generator.currentSeed ];
|
|
|
|
|
|
- currentSeed: 0,
|
|
|
|
|
|
+ generator.currentSeed = ( generator.currentSeed + 1 ) % numSeeds;
|
|
|
|
|
|
- random: function () {
|
|
|
|
|
|
+ return value;
|
|
|
|
|
|
- var value = seeds[ generator.currentSeed ];
|
|
|
|
|
|
+ },
|
|
|
|
|
|
- generator.currentSeed = ( generator.currentSeed + 1 ) % numSeeds;
|
|
|
|
|
|
+ getSeed: function () {
|
|
|
|
|
|
- return value;
|
|
|
|
|
|
+ return generator.currentSeed / numSeeds;
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ },
|
|
|
|
|
|
- getSeed: function () {
|
|
|
|
|
|
+ setSeed: function ( seed ) {
|
|
|
|
|
|
- return generator.currentSeed / numSeeds;
|
|
|
|
|
|
+ generator.currentSeed = Math.floor( seed * numSeeds ) % numSeeds;
|
|
|
|
|
|
- },
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- setSeed: function ( seed ) {
|
|
|
|
|
|
+ };
|
|
|
|
|
|
- generator.currentSeed = Math.floor( seed * numSeeds ) % numSeeds;
|
|
|
|
|
|
+ return generator;
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- };
|
|
|
|
|
|
+ static copyParameters( dest = {}, source = {} ) {
|
|
|
|
|
|
- return generator;
|
|
|
|
|
|
+ const vecCopy = function ( v ) {
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ if ( source === dest ) {
|
|
|
|
|
|
-LightningStrike.copyParameters = function ( dest, source ) {
|
|
|
|
|
|
+ return v;
|
|
|
|
|
|
- source = source || {};
|
|
|
|
- dest = dest || {};
|
|
|
|
|
|
+ } else {
|
|
|
|
|
|
- var vecCopy = function ( v ) {
|
|
|
|
|
|
+ return v.clone();
|
|
|
|
|
|
- if ( source === dest ) {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- return v;
|
|
|
|
|
|
+ };
|
|
|
|
|
|
- } else {
|
|
|
|
|
|
+ dest.sourceOffset = source.sourceOffset !== undefined ? vecCopy( source.sourceOffset ) : new Vector3( 0, 100, 0 ),
|
|
|
|
+ dest.destOffset = source.destOffset !== undefined ? vecCopy( source.destOffset ) : new Vector3( 0, 0, 0 ),
|
|
|
|
|
|
- return v.clone();
|
|
|
|
|
|
+ dest.timeScale = source.timeScale !== undefined ? source.timeScale : 1,
|
|
|
|
+ dest.roughness = source.roughness !== undefined ? source.roughness : 0.9,
|
|
|
|
+ dest.straightness = source.straightness !== undefined ? source.straightness : 0.7,
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ dest.up0 = source.up0 !== undefined ? vecCopy( source.up0 ) : new Vector3( 0, 0, 1 );
|
|
|
|
+ dest.up1 = source.up1 !== undefined ? vecCopy( source.up1 ) : new Vector3( 0, 0, 1 ),
|
|
|
|
+ dest.radius0 = source.radius0 !== undefined ? source.radius0 : 1,
|
|
|
|
+ dest.radius1 = source.radius1 !== undefined ? source.radius1 : 1,
|
|
|
|
+ dest.radius0Factor = source.radius0Factor !== undefined ? source.radius0Factor : 0.5,
|
|
|
|
+ dest.radius1Factor = source.radius1Factor !== undefined ? source.radius1Factor : 0.2,
|
|
|
|
+ dest.minRadius = source.minRadius !== undefined ? source.minRadius : 0.2,
|
|
|
|
|
|
- };
|
|
|
|
|
|
+ // These parameters should not be changed after lightning creation. They can be changed but the ray will change its form abruptly:
|
|
|
|
|
|
- dest.sourceOffset = source.sourceOffset !== undefined ? vecCopy( source.sourceOffset ) : new Vector3( 0, 100, 0 ),
|
|
|
|
- dest.destOffset = source.destOffset !== undefined ? vecCopy( source.destOffset ) : new Vector3( 0, 0, 0 ),
|
|
|
|
|
|
+ dest.isEternal = source.isEternal !== undefined ? source.isEternal : ( source.birthTime === undefined || source.deathTime === undefined ),
|
|
|
|
+ dest.birthTime = source.birthTime,
|
|
|
|
+ dest.deathTime = source.deathTime,
|
|
|
|
+ dest.propagationTimeFactor = source.propagationTimeFactor !== undefined ? source.propagationTimeFactor : 0.1,
|
|
|
|
+ dest.vanishingTimeFactor = source.vanishingTimeFactor !== undefined ? source.vanishingTimeFactor : 0.9,
|
|
|
|
+ dest.subrayPeriod = source.subrayPeriod !== undefined ? source.subrayPeriod : 4,
|
|
|
|
+ dest.subrayDutyCycle = source.subrayDutyCycle !== undefined ? source.subrayDutyCycle : 0.6;
|
|
|
|
|
|
- dest.timeScale = source.timeScale !== undefined ? source.timeScale : 1,
|
|
|
|
- dest.roughness = source.roughness !== undefined ? source.roughness : 0.9,
|
|
|
|
- dest.straightness = source.straightness !== undefined ? source.straightness : 0.7,
|
|
|
|
|
|
+ // These parameters cannot change after lightning creation:
|
|
|
|
|
|
- dest.up0 = source.up0 !== undefined ? vecCopy( source.up0 ) : new Vector3( 0, 0, 1 );
|
|
|
|
- dest.up1 = source.up1 !== undefined ? vecCopy( source.up1 ) : new Vector3( 0, 0, 1 ),
|
|
|
|
- dest.radius0 = source.radius0 !== undefined ? source.radius0 : 1,
|
|
|
|
- dest.radius1 = source.radius1 !== undefined ? source.radius1 : 1,
|
|
|
|
- dest.radius0Factor = source.radius0Factor !== undefined ? source.radius0Factor : 0.5,
|
|
|
|
- dest.radius1Factor = source.radius1Factor !== undefined ? source.radius1Factor : 0.2,
|
|
|
|
- dest.minRadius = source.minRadius !== undefined ? source.minRadius : 0.2,
|
|
|
|
|
|
+ dest.maxIterations = source.maxIterations !== undefined ? source.maxIterations : 9;
|
|
|
|
+ dest.isStatic = source.isStatic !== undefined ? source.isStatic : false;
|
|
|
|
+ dest.ramification = source.ramification !== undefined ? source.ramification : 5;
|
|
|
|
+ dest.maxSubrayRecursion = source.maxSubrayRecursion !== undefined ? source.maxSubrayRecursion : 3;
|
|
|
|
+ dest.recursionProbability = source.recursionProbability !== undefined ? source.recursionProbability : 0.6;
|
|
|
|
+ dest.generateUVs = source.generateUVs !== undefined ? source.generateUVs : false;
|
|
|
|
+ dest.randomGenerator = source.randomGenerator,
|
|
|
|
+ dest.noiseSeed = source.noiseSeed,
|
|
|
|
+ dest.onDecideSubrayCreation = source.onDecideSubrayCreation,
|
|
|
|
+ dest.onSubrayCreation = source.onSubrayCreation;
|
|
|
|
|
|
- // These parameters should not be changed after lightning creation. They can be changed but the ray will change its form abruptly:
|
|
|
|
|
|
+ return dest;
|
|
|
|
|
|
- dest.isEternal = source.isEternal !== undefined ? source.isEternal : ( source.birthTime === undefined || source.deathTime === undefined ),
|
|
|
|
- dest.birthTime = source.birthTime,
|
|
|
|
- dest.deathTime = source.deathTime,
|
|
|
|
- dest.propagationTimeFactor = source.propagationTimeFactor !== undefined ? source.propagationTimeFactor : 0.1,
|
|
|
|
- dest.vanishingTimeFactor = source.vanishingTimeFactor !== undefined ? source.vanishingTimeFactor : 0.9,
|
|
|
|
- dest.subrayPeriod = source.subrayPeriod !== undefined ? source.subrayPeriod : 4,
|
|
|
|
- dest.subrayDutyCycle = source.subrayDutyCycle !== undefined ? source.subrayDutyCycle : 0.6;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- // These parameters cannot change after lightning creation:
|
|
|
|
|
|
+ update( time ) {
|
|
|
|
|
|
- dest.maxIterations = source.maxIterations !== undefined ? source.maxIterations : 9;
|
|
|
|
- dest.isStatic = source.isStatic !== undefined ? source.isStatic : false;
|
|
|
|
- dest.ramification = source.ramification !== undefined ? source.ramification : 5;
|
|
|
|
- dest.maxSubrayRecursion = source.maxSubrayRecursion !== undefined ? source.maxSubrayRecursion : 3;
|
|
|
|
- dest.recursionProbability = source.recursionProbability !== undefined ? source.recursionProbability : 0.6;
|
|
|
|
- dest.generateUVs = source.generateUVs !== undefined ? source.generateUVs : false;
|
|
|
|
- dest.randomGenerator = source.randomGenerator,
|
|
|
|
- dest.noiseSeed = source.noiseSeed,
|
|
|
|
- dest.onDecideSubrayCreation = source.onDecideSubrayCreation,
|
|
|
|
- dest.onSubrayCreation = source.onSubrayCreation;
|
|
|
|
|
|
+ if ( this.isStatic ) return;
|
|
|
|
|
|
- return dest;
|
|
|
|
|
|
+ if ( this.rayParameters.isEternal || ( this.rayParameters.birthTime <= time && time <= this.rayParameters.deathTime ) ) {
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ this.updateMesh( time );
|
|
|
|
|
|
-LightningStrike.prototype.update = function ( time ) {
|
|
|
|
|
|
+ if ( time < this.subrays[ 0 ].endPropagationTime ) {
|
|
|
|
|
|
- if ( this.isStatic ) return;
|
|
|
|
|
|
+ this.state = LightningStrike.RAY_PROPAGATING;
|
|
|
|
|
|
- if ( this.rayParameters.isEternal || ( this.rayParameters.birthTime <= time && time <= this.rayParameters.deathTime ) ) {
|
|
|
|
|
|
+ } else if ( time > this.subrays[ 0 ].beginVanishingTime ) {
|
|
|
|
|
|
- this.updateMesh( time );
|
|
|
|
|
|
+ this.state = LightningStrike.RAY_VANISHING;
|
|
|
|
|
|
- if ( time < this.subrays[ 0 ].endPropagationTime ) {
|
|
|
|
|
|
+ } else {
|
|
|
|
|
|
- this.state = LightningStrike.RAY_PROPAGATING;
|
|
|
|
|
|
+ this.state = LightningStrike.RAY_STEADY;
|
|
|
|
|
|
- } else if ( time > this.subrays[ 0 ].beginVanishingTime ) {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- this.state = LightningStrike.RAY_VANISHING;
|
|
|
|
|
|
+ this.visible = true;
|
|
|
|
|
|
} else {
|
|
} else {
|
|
|
|
|
|
- this.state = LightningStrike.RAY_STEADY;
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- this.visible = true;
|
|
|
|
|
|
+ this.visible = false;
|
|
|
|
|
|
- } else {
|
|
|
|
|
|
+ if ( time < this.rayParameters.birthTime ) {
|
|
|
|
|
|
- this.visible = false;
|
|
|
|
|
|
+ this.state = LightningStrike.RAY_UNBORN;
|
|
|
|
|
|
- if ( time < this.rayParameters.birthTime ) {
|
|
|
|
|
|
+ } else {
|
|
|
|
|
|
- this.state = LightningStrike.RAY_UNBORN;
|
|
|
|
|
|
+ this.state = LightningStrike.RAY_EXTINGUISHED;
|
|
|
|
|
|
- } else {
|
|
|
|
-
|
|
|
|
- this.state = LightningStrike.RAY_EXTINGUISHED;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ init( rayParameters ) {
|
|
|
|
|
|
-LightningStrike.prototype.init = function ( rayParameters ) {
|
|
|
|
|
|
+ // Init all the state from the parameters
|
|
|
|
|
|
- // Init all the state from the parameters
|
|
|
|
|
|
+ this.rayParameters = rayParameters;
|
|
|
|
|
|
- this.rayParameters = rayParameters;
|
|
|
|
|
|
+ // These parameters cannot change after lightning creation:
|
|
|
|
|
|
- // These parameters cannot change after lightning creation:
|
|
|
|
|
|
+ this.maxIterations = rayParameters.maxIterations !== undefined ? Math.floor( rayParameters.maxIterations ) : 9;
|
|
|
|
+ rayParameters.maxIterations = this.maxIterations;
|
|
|
|
+ this.isStatic = rayParameters.isStatic !== undefined ? rayParameters.isStatic : false;
|
|
|
|
+ rayParameters.isStatic = this.isStatic;
|
|
|
|
+ this.ramification = rayParameters.ramification !== undefined ? Math.floor( rayParameters.ramification ) : 5;
|
|
|
|
+ rayParameters.ramification = this.ramification;
|
|
|
|
+ this.maxSubrayRecursion = rayParameters.maxSubrayRecursion !== undefined ? Math.floor( rayParameters.maxSubrayRecursion ) : 3;
|
|
|
|
+ rayParameters.maxSubrayRecursion = this.maxSubrayRecursion;
|
|
|
|
+ this.recursionProbability = rayParameters.recursionProbability !== undefined ? rayParameters.recursionProbability : 0.6;
|
|
|
|
+ rayParameters.recursionProbability = this.recursionProbability;
|
|
|
|
+ this.generateUVs = rayParameters.generateUVs !== undefined ? rayParameters.generateUVs : false;
|
|
|
|
+ rayParameters.generateUVs = this.generateUVs;
|
|
|
|
|
|
- this.maxIterations = rayParameters.maxIterations !== undefined ? Math.floor( rayParameters.maxIterations ) : 9;
|
|
|
|
- rayParameters.maxIterations = this.maxIterations;
|
|
|
|
- this.isStatic = rayParameters.isStatic !== undefined ? rayParameters.isStatic : false;
|
|
|
|
- rayParameters.isStatic = this.isStatic;
|
|
|
|
- this.ramification = rayParameters.ramification !== undefined ? Math.floor( rayParameters.ramification ) : 5;
|
|
|
|
- rayParameters.ramification = this.ramification;
|
|
|
|
- this.maxSubrayRecursion = rayParameters.maxSubrayRecursion !== undefined ? Math.floor( rayParameters.maxSubrayRecursion ) : 3;
|
|
|
|
- rayParameters.maxSubrayRecursion = this.maxSubrayRecursion;
|
|
|
|
- this.recursionProbability = rayParameters.recursionProbability !== undefined ? rayParameters.recursionProbability : 0.6;
|
|
|
|
- rayParameters.recursionProbability = this.recursionProbability;
|
|
|
|
- this.generateUVs = rayParameters.generateUVs !== undefined ? rayParameters.generateUVs : false;
|
|
|
|
- rayParameters.generateUVs = this.generateUVs;
|
|
|
|
|
|
+ // Random generator
|
|
|
|
+ if ( rayParameters.randomGenerator !== undefined ) {
|
|
|
|
|
|
- // Random generator
|
|
|
|
- if ( rayParameters.randomGenerator !== undefined ) {
|
|
|
|
|
|
+ this.randomGenerator = rayParameters.randomGenerator;
|
|
|
|
+ this.seedGenerator = rayParameters.randomGenerator;
|
|
|
|
|
|
- this.randomGenerator = rayParameters.randomGenerator;
|
|
|
|
- this.seedGenerator = rayParameters.randomGenerator;
|
|
|
|
|
|
+ if ( rayParameters.noiseSeed !== undefined ) {
|
|
|
|
|
|
- if ( rayParameters.noiseSeed !== undefined ) {
|
|
|
|
|
|
+ this.seedGenerator.setSeed( rayParameters.noiseSeed );
|
|
|
|
|
|
- this.seedGenerator.setSeed( rayParameters.noiseSeed );
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ } else {
|
|
|
|
|
|
- } else {
|
|
|
|
|
|
+ this.randomGenerator = LightningStrike.createRandomGenerator();
|
|
|
|
+ this.seedGenerator = Math;
|
|
|
|
|
|
- this.randomGenerator = LightningStrike.createRandomGenerator();
|
|
|
|
- this.seedGenerator = Math;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ // Ray creation callbacks
|
|
|
|
+ if ( rayParameters.onDecideSubrayCreation !== undefined ) {
|
|
|
|
|
|
- // Ray creation callbacks
|
|
|
|
- if ( rayParameters.onDecideSubrayCreation !== undefined ) {
|
|
|
|
|
|
+ this.onDecideSubrayCreation = rayParameters.onDecideSubrayCreation;
|
|
|
|
|
|
- this.onDecideSubrayCreation = rayParameters.onDecideSubrayCreation;
|
|
|
|
|
|
+ } else {
|
|
|
|
|
|
- } else {
|
|
|
|
|
|
+ this.createDefaultSubrayCreationCallbacks();
|
|
|
|
|
|
- this.createDefaultSubrayCreationCallbacks();
|
|
|
|
|
|
+ if ( rayParameters.onSubrayCreation !== undefined ) {
|
|
|
|
|
|
- if ( rayParameters.onSubrayCreation !== undefined ) {
|
|
|
|
|
|
+ this.onSubrayCreation = rayParameters.onSubrayCreation;
|
|
|
|
|
|
- this.onSubrayCreation = rayParameters.onSubrayCreation;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ // Internal state
|
|
|
|
|
|
- // Internal state
|
|
|
|
|
|
+ this.state = LightningStrike.RAY_INITIALIZED;
|
|
|
|
|
|
- this.state = LightningStrike.RAY_INITIALIZED;
|
|
|
|
|
|
+ this.maxSubrays = Math.ceil( 1 + Math.pow( this.ramification, Math.max( 0, this.maxSubrayRecursion - 1 ) ) );
|
|
|
|
+ rayParameters.maxSubrays = this.maxSubrays;
|
|
|
|
|
|
- this.maxSubrays = Math.ceil( 1 + Math.pow( this.ramification, Math.max( 0, this.maxSubrayRecursion - 1 ) ) );
|
|
|
|
- rayParameters.maxSubrays = this.maxSubrays;
|
|
|
|
|
|
+ this.maxRaySegments = 2 * ( 1 << this.maxIterations );
|
|
|
|
|
|
- this.maxRaySegments = 2 * ( 1 << this.maxIterations );
|
|
|
|
|
|
+ this.subrays = [];
|
|
|
|
|
|
- this.subrays = [];
|
|
|
|
|
|
+ for ( let i = 0; i < this.maxSubrays; i ++ ) {
|
|
|
|
|
|
- for ( var i = 0; i < this.maxSubrays; i ++ ) {
|
|
|
|
|
|
+ this.subrays.push( this.createSubray() );
|
|
|
|
|
|
- this.subrays.push( this.createSubray() );
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ this.raySegments = [];
|
|
|
|
|
|
- this.raySegments = [];
|
|
|
|
|
|
+ for ( let i = 0; i < this.maxRaySegments; i ++ ) {
|
|
|
|
|
|
- for ( var i = 0; i < this.maxRaySegments; i ++ ) {
|
|
|
|
|
|
+ this.raySegments.push( this.createSegment() );
|
|
|
|
|
|
- this.raySegments.push( this.createSegment() );
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.time = 0;
|
|
|
|
+ this.timeFraction = 0;
|
|
|
|
+ this.currentSegmentCallback = null;
|
|
|
|
+ this.currentCreateTriangleVertices = this.generateUVs ? this.createTriangleVerticesWithUVs : this.createTriangleVerticesWithoutUVs;
|
|
|
|
+ this.numSubrays = 0;
|
|
|
|
+ this.currentSubray = null;
|
|
|
|
+ this.currentSegmentIndex = 0;
|
|
|
|
+ this.isInitialSegment = false;
|
|
|
|
+ this.subrayProbability = 0;
|
|
|
|
+
|
|
|
|
+ this.currentVertex = 0;
|
|
|
|
+ this.currentIndex = 0;
|
|
|
|
+ this.currentCoordinate = 0;
|
|
|
|
+ this.currentUVCoordinate = 0;
|
|
|
|
+ this.vertices = null;
|
|
|
|
+ this.uvs = null;
|
|
|
|
+ this.indices = null;
|
|
|
|
+ this.positionAttribute = null;
|
|
|
|
+ this.uvsAttribute = null;
|
|
|
|
+
|
|
|
|
+ this.simplexX = new SimplexNoise( this.seedGenerator );
|
|
|
|
+ this.simplexY = new SimplexNoise( this.seedGenerator );
|
|
|
|
+ this.simplexZ = new SimplexNoise( this.seedGenerator );
|
|
|
|
+
|
|
|
|
+ // Temp vectors
|
|
|
|
+ this.forwards = new Vector3();
|
|
|
|
+ this.forwardsFill = new Vector3();
|
|
|
|
+ this.side = new Vector3();
|
|
|
|
+ this.down = new Vector3();
|
|
|
|
+ this.middlePos = new Vector3();
|
|
|
|
+ this.middleLinPos = new Vector3();
|
|
|
|
+ this.newPos = new Vector3();
|
|
|
|
+ this.vPos = new Vector3();
|
|
|
|
+ this.cross1 = new Vector3();
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- this.time = 0;
|
|
|
|
- this.timeFraction = 0;
|
|
|
|
- this.currentSegmentCallback = null;
|
|
|
|
- this.currentCreateTriangleVertices = this.generateUVs ? this.createTriangleVerticesWithUVs : this.createTriangleVerticesWithoutUVs;
|
|
|
|
- this.numSubrays = 0;
|
|
|
|
- this.currentSubray = null;
|
|
|
|
- this.currentSegmentIndex = 0;
|
|
|
|
- this.isInitialSegment = false;
|
|
|
|
- this.subrayProbability = 0;
|
|
|
|
-
|
|
|
|
- this.currentVertex = 0;
|
|
|
|
- this.currentIndex = 0;
|
|
|
|
- this.currentCoordinate = 0;
|
|
|
|
- this.currentUVCoordinate = 0;
|
|
|
|
- this.vertices = null;
|
|
|
|
- this.uvs = null;
|
|
|
|
- this.indices = null;
|
|
|
|
- this.positionAttribute = null;
|
|
|
|
- this.uvsAttribute = null;
|
|
|
|
-
|
|
|
|
- this.simplexX = new SimplexNoise( this.seedGenerator );
|
|
|
|
- this.simplexY = new SimplexNoise( this.seedGenerator );
|
|
|
|
- this.simplexZ = new SimplexNoise( this.seedGenerator );
|
|
|
|
-
|
|
|
|
- // Temp vectors
|
|
|
|
- this.forwards = new Vector3();
|
|
|
|
- this.forwardsFill = new Vector3();
|
|
|
|
- this.side = new Vector3();
|
|
|
|
- this.down = new Vector3();
|
|
|
|
- this.middlePos = new Vector3();
|
|
|
|
- this.middleLinPos = new Vector3();
|
|
|
|
- this.newPos = new Vector3();
|
|
|
|
- this.vPos = new Vector3();
|
|
|
|
- this.cross1 = new Vector3();
|
|
|
|
-
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-LightningStrike.prototype.createMesh = function () {
|
|
|
|
-
|
|
|
|
- var maxDrawableSegmentsPerSubRay = 1 << this.maxIterations;
|
|
|
|
-
|
|
|
|
- var maxVerts = 3 * ( maxDrawableSegmentsPerSubRay + 1 ) * this.maxSubrays;
|
|
|
|
- var maxIndices = 18 * maxDrawableSegmentsPerSubRay * this.maxSubrays;
|
|
|
|
-
|
|
|
|
- this.vertices = new Float32Array( maxVerts * 3 );
|
|
|
|
- this.indices = new Uint32Array( maxIndices );
|
|
|
|
- if ( this.generateUVs ) {
|
|
|
|
-
|
|
|
|
- this.uvs = new Float32Array( maxVerts * 2 );
|
|
|
|
|
|
+ createMesh() {
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ const maxDrawableSegmentsPerSubRay = 1 << this.maxIterations;
|
|
|
|
|
|
- // Populate the mesh
|
|
|
|
- this.fillMesh( 0 );
|
|
|
|
|
|
+ const maxVerts = 3 * ( maxDrawableSegmentsPerSubRay + 1 ) * this.maxSubrays;
|
|
|
|
+ const maxIndices = 18 * maxDrawableSegmentsPerSubRay * this.maxSubrays;
|
|
|
|
|
|
- this.setIndex( new Uint32BufferAttribute( this.indices, 1 ) );
|
|
|
|
|
|
+ this.vertices = new Float32Array( maxVerts * 3 );
|
|
|
|
+ this.indices = new Uint32Array( maxIndices );
|
|
|
|
|
|
- this.positionAttribute = new Float32BufferAttribute( this.vertices, 3 );
|
|
|
|
- this.setAttribute( 'position', this.positionAttribute );
|
|
|
|
|
|
+ if ( this.generateUVs ) {
|
|
|
|
|
|
- if ( this.generateUVs ) {
|
|
|
|
|
|
+ this.uvs = new Float32Array( maxVerts * 2 );
|
|
|
|
|
|
- this.uvsAttribute = new Float32BufferAttribute( new Float32Array( this.uvs ), 2 );
|
|
|
|
- this.setAttribute( 'uv', this.uvsAttribute );
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ // Populate the mesh
|
|
|
|
+ this.fillMesh( 0 );
|
|
|
|
|
|
- if ( ! this.isStatic ) {
|
|
|
|
|
|
+ this.setIndex( new Uint32BufferAttribute( this.indices, 1 ) );
|
|
|
|
+
|
|
|
|
+ this.positionAttribute = new Float32BufferAttribute( this.vertices, 3 );
|
|
|
|
+ this.setAttribute( 'position', this.positionAttribute );
|
|
|
|
|
|
- this.index.usage = DynamicDrawUsage;
|
|
|
|
- this.positionAttribute.usage = DynamicDrawUsage;
|
|
|
|
if ( this.generateUVs ) {
|
|
if ( this.generateUVs ) {
|
|
|
|
|
|
- this.uvsAttribute.usage = DynamicDrawUsage;
|
|
|
|
|
|
+ this.uvsAttribute = new Float32BufferAttribute( new Float32Array( this.uvs ), 2 );
|
|
|
|
+ this.setAttribute( 'uv', this.uvsAttribute );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ if ( ! this.isStatic ) {
|
|
|
|
|
|
- // Store buffers for later modification
|
|
|
|
- this.vertices = this.positionAttribute.array;
|
|
|
|
- this.indices = this.index.array;
|
|
|
|
- if ( this.generateUVs ) {
|
|
|
|
|
|
+ this.index.usage = DynamicDrawUsage;
|
|
|
|
+ this.positionAttribute.usage = DynamicDrawUsage;
|
|
|
|
|
|
- this.uvs = this.uvsAttribute.array;
|
|
|
|
|
|
+ if ( this.generateUVs ) {
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ this.uvsAttribute.usage = DynamicDrawUsage;
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ }
|
|
|
|
|
|
-LightningStrike.prototype.updateMesh = function ( time ) {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- this.fillMesh( time );
|
|
|
|
|
|
+ // Store buffers for later modification
|
|
|
|
+ this.vertices = this.positionAttribute.array;
|
|
|
|
+ this.indices = this.index.array;
|
|
|
|
|
|
- this.drawRange.count = this.currentIndex;
|
|
|
|
|
|
+ if ( this.generateUVs ) {
|
|
|
|
|
|
- this.index.needsUpdate = true;
|
|
|
|
|
|
+ this.uvs = this.uvsAttribute.array;
|
|
|
|
|
|
- this.positionAttribute.needsUpdate = true;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- if ( this.generateUVs ) {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- this.uvsAttribute.needsUpdate = true;
|
|
|
|
|
|
+ updateMesh( time ) {
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ this.fillMesh( time );
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ this.drawRange.count = this.currentIndex;
|
|
|
|
|
|
-LightningStrike.prototype.fillMesh = function ( time ) {
|
|
|
|
|
|
+ this.index.needsUpdate = true;
|
|
|
|
|
|
- var scope = this;
|
|
|
|
|
|
+ this.positionAttribute.needsUpdate = true;
|
|
|
|
|
|
- this.currentVertex = 0;
|
|
|
|
- this.currentIndex = 0;
|
|
|
|
- this.currentCoordinate = 0;
|
|
|
|
- this.currentUVCoordinate = 0;
|
|
|
|
|
|
+ if ( this.generateUVs ) {
|
|
|
|
|
|
- this.fractalRay( time, function fillVertices( segment ) {
|
|
|
|
|
|
+ this.uvsAttribute.needsUpdate = true;
|
|
|
|
|
|
- var subray = scope.currentSubray;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- if ( time < subray.birthTime ) { //&& ( ! this.rayParameters.isEternal || scope.currentSubray.recursion > 0 ) ) {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- return;
|
|
|
|
|
|
+ fillMesh( time ) {
|
|
|
|
|
|
- } else if ( this.rayParameters.isEternal && scope.currentSubray.recursion == 0 ) {
|
|
|
|
|
|
+ const scope = this;
|
|
|
|
|
|
- // Eternal rays don't propagate nor vanish, but its subrays do
|
|
|
|
|
|
+ this.currentVertex = 0;
|
|
|
|
+ this.currentIndex = 0;
|
|
|
|
+ this.currentCoordinate = 0;
|
|
|
|
+ this.currentUVCoordinate = 0;
|
|
|
|
|
|
- scope.createPrism( segment );
|
|
|
|
|
|
+ this.fractalRay( time, function fillVertices( segment ) {
|
|
|
|
|
|
- scope.onDecideSubrayCreation( segment, scope );
|
|
|
|
|
|
+ const subray = scope.currentSubray;
|
|
|
|
|
|
- } else if ( time < subray.endPropagationTime ) {
|
|
|
|
|
|
+ if ( time < subray.birthTime ) { //&& ( ! this.rayParameters.isEternal || scope.currentSubray.recursion > 0 ) ) {
|
|
|
|
|
|
- if ( scope.timeFraction >= segment.fraction0 * subray.propagationTimeFactor ) {
|
|
|
|
|
|
+ return;
|
|
|
|
|
|
- // Ray propagation has arrived to this segment
|
|
|
|
|
|
+ } else if ( this.rayParameters.isEternal && scope.currentSubray.recursion == 0 ) {
|
|
|
|
+
|
|
|
|
+ // Eternal rays don't propagate nor vanish, but its subrays do
|
|
|
|
|
|
scope.createPrism( segment );
|
|
scope.createPrism( segment );
|
|
|
|
|
|
scope.onDecideSubrayCreation( segment, scope );
|
|
scope.onDecideSubrayCreation( segment, scope );
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ } else if ( time < subray.endPropagationTime ) {
|
|
|
|
|
|
- } else if ( time < subray.beginVanishingTime ) {
|
|
|
|
|
|
+ if ( scope.timeFraction >= segment.fraction0 * subray.propagationTimeFactor ) {
|
|
|
|
|
|
- // Ray is steady (nor propagating nor vanishing)
|
|
|
|
|
|
+ // Ray propagation has arrived to this segment
|
|
|
|
|
|
- scope.createPrism( segment );
|
|
|
|
|
|
+ scope.createPrism( segment );
|
|
|
|
|
|
- scope.onDecideSubrayCreation( segment, scope );
|
|
|
|
|
|
+ scope.onDecideSubrayCreation( segment, scope );
|
|
|
|
|
|
- } else {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- if ( scope.timeFraction <= subray.vanishingTimeFactor + segment.fraction1 * ( 1 - subray.vanishingTimeFactor ) ) {
|
|
|
|
|
|
+ } else if ( time < subray.beginVanishingTime ) {
|
|
|
|
|
|
- // Segment has not yet vanished
|
|
|
|
|
|
+ // Ray is steady (nor propagating nor vanishing)
|
|
|
|
|
|
scope.createPrism( segment );
|
|
scope.createPrism( segment );
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ scope.onDecideSubrayCreation( segment, scope );
|
|
|
|
|
|
- scope.onDecideSubrayCreation( segment, scope );
|
|
|
|
|
|
+ } else {
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ if ( scope.timeFraction <= subray.vanishingTimeFactor + segment.fraction1 * ( 1 - subray.vanishingTimeFactor ) ) {
|
|
|
|
|
|
- } );
|
|
|
|
|
|
+ // Segment has not yet vanished
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ scope.createPrism( segment );
|
|
|
|
|
|
-LightningStrike.prototype.addNewSubray = function ( /*rayParameters*/ ) {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- return this.subrays[ this.numSubrays ++ ];
|
|
|
|
|
|
+ scope.onDecideSubrayCreation( segment, scope );
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ }
|
|
|
|
|
|
-LightningStrike.prototype.initSubray = function ( subray, rayParameters ) {
|
|
|
|
|
|
+ } );
|
|
|
|
|
|
- subray.pos0.copy( rayParameters.sourceOffset );
|
|
|
|
- subray.pos1.copy( rayParameters.destOffset );
|
|
|
|
- subray.up0.copy( rayParameters.up0 );
|
|
|
|
- subray.up1.copy( rayParameters.up1 );
|
|
|
|
- subray.radius0 = rayParameters.radius0;
|
|
|
|
- subray.radius1 = rayParameters.radius1;
|
|
|
|
- subray.birthTime = rayParameters.birthTime;
|
|
|
|
- subray.deathTime = rayParameters.deathTime;
|
|
|
|
- subray.timeScale = rayParameters.timeScale;
|
|
|
|
- subray.roughness = rayParameters.roughness;
|
|
|
|
- subray.straightness = rayParameters.straightness;
|
|
|
|
- subray.propagationTimeFactor = rayParameters.propagationTimeFactor;
|
|
|
|
- subray.vanishingTimeFactor = rayParameters.vanishingTimeFactor;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- subray.maxIterations = this.maxIterations;
|
|
|
|
- subray.seed = rayParameters.noiseSeed !== undefined ? rayParameters.noiseSeed : 0;
|
|
|
|
- subray.recursion = 0;
|
|
|
|
|
|
+ addNewSubray( /*rayParameters*/ ) {
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ return this.subrays[ this.numSubrays ++ ];
|
|
|
|
|
|
-LightningStrike.prototype.fractalRay = function ( time, segmentCallback ) {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- this.time = time;
|
|
|
|
- this.currentSegmentCallback = segmentCallback;
|
|
|
|
- this.numSubrays = 0;
|
|
|
|
|
|
+ initSubray( subray, rayParameters ) {
|
|
|
|
+
|
|
|
|
+ subray.pos0.copy( rayParameters.sourceOffset );
|
|
|
|
+ subray.pos1.copy( rayParameters.destOffset );
|
|
|
|
+ subray.up0.copy( rayParameters.up0 );
|
|
|
|
+ subray.up1.copy( rayParameters.up1 );
|
|
|
|
+ subray.radius0 = rayParameters.radius0;
|
|
|
|
+ subray.radius1 = rayParameters.radius1;
|
|
|
|
+ subray.birthTime = rayParameters.birthTime;
|
|
|
|
+ subray.deathTime = rayParameters.deathTime;
|
|
|
|
+ subray.timeScale = rayParameters.timeScale;
|
|
|
|
+ subray.roughness = rayParameters.roughness;
|
|
|
|
+ subray.straightness = rayParameters.straightness;
|
|
|
|
+ subray.propagationTimeFactor = rayParameters.propagationTimeFactor;
|
|
|
|
+ subray.vanishingTimeFactor = rayParameters.vanishingTimeFactor;
|
|
|
|
+
|
|
|
|
+ subray.maxIterations = this.maxIterations;
|
|
|
|
+ subray.seed = rayParameters.noiseSeed !== undefined ? rayParameters.noiseSeed : 0;
|
|
|
|
+ subray.recursion = 0;
|
|
|
|
|
|
- // Add the top level subray
|
|
|
|
- this.initSubray( this.addNewSubray(), this.rayParameters );
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- // Process all subrays that are being generated until consuming all of them
|
|
|
|
- for ( var subrayIndex = 0; subrayIndex < this.numSubrays; subrayIndex ++ ) {
|
|
|
|
|
|
+ fractalRay( time, segmentCallback ) {
|
|
|
|
|
|
- var subray = this.subrays[ subrayIndex ];
|
|
|
|
- this.currentSubray = subray;
|
|
|
|
|
|
+ this.time = time;
|
|
|
|
+ this.currentSegmentCallback = segmentCallback;
|
|
|
|
+ this.numSubrays = 0;
|
|
|
|
|
|
- this.randomGenerator.setSeed( subray.seed );
|
|
|
|
|
|
+ // Add the top level subray
|
|
|
|
+ this.initSubray( this.addNewSubray(), this.rayParameters );
|
|
|
|
|
|
- subray.endPropagationTime = MathUtils.lerp( subray.birthTime, subray.deathTime, subray.propagationTimeFactor );
|
|
|
|
- subray.beginVanishingTime = MathUtils.lerp( subray.deathTime, subray.birthTime, 1 - subray.vanishingTimeFactor );
|
|
|
|
|
|
+ // Process all subrays that are being generated until consuming all of them
|
|
|
|
+ for ( let subrayIndex = 0; subrayIndex < this.numSubrays; subrayIndex ++ ) {
|
|
|
|
|
|
- var random1 = this.randomGenerator.random;
|
|
|
|
- subray.linPos0.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
|
|
|
|
- subray.linPos1.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
|
|
|
|
|
|
+ const subray = this.subrays[ subrayIndex ];
|
|
|
|
+ this.currentSubray = subray;
|
|
|
|
|
|
- this.timeFraction = ( time - subray.birthTime ) / ( subray.deathTime - subray.birthTime );
|
|
|
|
|
|
+ this.randomGenerator.setSeed( subray.seed );
|
|
|
|
|
|
- this.currentSegmentIndex = 0;
|
|
|
|
- this.isInitialSegment = true;
|
|
|
|
-
|
|
|
|
- var segment = this.getNewSegment();
|
|
|
|
- segment.iteration = 0;
|
|
|
|
- segment.pos0.copy( subray.pos0 );
|
|
|
|
- segment.pos1.copy( subray.pos1 );
|
|
|
|
- segment.linPos0.copy( subray.linPos0 );
|
|
|
|
- segment.linPos1.copy( subray.linPos1 );
|
|
|
|
- segment.up0.copy( subray.up0 );
|
|
|
|
- segment.up1.copy( subray.up1 );
|
|
|
|
- segment.radius0 = subray.radius0;
|
|
|
|
- segment.radius1 = subray.radius1;
|
|
|
|
- segment.fraction0 = 0;
|
|
|
|
- segment.fraction1 = 1;
|
|
|
|
- segment.positionVariationFactor = 1 - subray.straightness;
|
|
|
|
-
|
|
|
|
- this.subrayProbability = this.ramification * Math.pow( this.recursionProbability, subray.recursion ) / ( 1 << subray.maxIterations );
|
|
|
|
-
|
|
|
|
- this.fractalRayRecursive( segment );
|
|
|
|
|
|
+ subray.endPropagationTime = MathUtils.lerp( subray.birthTime, subray.deathTime, subray.propagationTimeFactor );
|
|
|
|
+ subray.beginVanishingTime = MathUtils.lerp( subray.deathTime, subray.birthTime, 1 - subray.vanishingTimeFactor );
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ const random1 = this.randomGenerator.random;
|
|
|
|
+ subray.linPos0.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
|
|
|
|
+ subray.linPos1.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
|
|
|
|
|
|
- this.currentSegmentCallback = null;
|
|
|
|
- this.currentSubray = null;
|
|
|
|
|
|
+ this.timeFraction = ( time - subray.birthTime ) / ( subray.deathTime - subray.birthTime );
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ this.currentSegmentIndex = 0;
|
|
|
|
+ this.isInitialSegment = true;
|
|
|
|
|
|
-LightningStrike.prototype.fractalRayRecursive = function ( segment ) {
|
|
|
|
|
|
+ const segment = this.getNewSegment();
|
|
|
|
+ segment.iteration = 0;
|
|
|
|
+ segment.pos0.copy( subray.pos0 );
|
|
|
|
+ segment.pos1.copy( subray.pos1 );
|
|
|
|
+ segment.linPos0.copy( subray.linPos0 );
|
|
|
|
+ segment.linPos1.copy( subray.linPos1 );
|
|
|
|
+ segment.up0.copy( subray.up0 );
|
|
|
|
+ segment.up1.copy( subray.up1 );
|
|
|
|
+ segment.radius0 = subray.radius0;
|
|
|
|
+ segment.radius1 = subray.radius1;
|
|
|
|
+ segment.fraction0 = 0;
|
|
|
|
+ segment.fraction1 = 1;
|
|
|
|
+ segment.positionVariationFactor = 1 - subray.straightness;
|
|
|
|
|
|
- // Leave recursion condition
|
|
|
|
- if ( segment.iteration >= this.currentSubray.maxIterations ) {
|
|
|
|
|
|
+ this.subrayProbability = this.ramification * Math.pow( this.recursionProbability, subray.recursion ) / ( 1 << subray.maxIterations );
|
|
|
|
|
|
- this.currentSegmentCallback( segment );
|
|
|
|
|
|
+ this.fractalRayRecursive( segment );
|
|
|
|
|
|
- return;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ this.currentSegmentCallback = null;
|
|
|
|
+ this.currentSubray = null;
|
|
|
|
|
|
- // Interpolation
|
|
|
|
- this.forwards.subVectors( segment.pos1, segment.pos0 );
|
|
|
|
- var lForwards = this.forwards.length();
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- if ( lForwards < 0.000001 ) {
|
|
|
|
|
|
+ fractalRayRecursive( segment ) {
|
|
|
|
|
|
- this.forwards.set( 0, 0, 0.01 );
|
|
|
|
- lForwards = this.forwards.length();
|
|
|
|
|
|
+ // Leave recursion condition
|
|
|
|
+ if ( segment.iteration >= this.currentSubray.maxIterations ) {
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ this.currentSegmentCallback( segment );
|
|
|
|
|
|
- var middleRadius = ( segment.radius0 + segment.radius1 ) * 0.5;
|
|
|
|
- var middleFraction = ( segment.fraction0 + segment.fraction1 ) * 0.5;
|
|
|
|
|
|
+ return;
|
|
|
|
|
|
- var timeDimension = this.time * this.currentSubray.timeScale * Math.pow( 2, segment.iteration );
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- this.middlePos.lerpVectors( segment.pos0, segment.pos1, 0.5 );
|
|
|
|
- this.middleLinPos.lerpVectors( segment.linPos0, segment.linPos1, 0.5 );
|
|
|
|
- var p = this.middleLinPos;
|
|
|
|
|
|
+ // Interpolation
|
|
|
|
+ this.forwards.subVectors( segment.pos1, segment.pos0 );
|
|
|
|
+ let lForwards = this.forwards.length();
|
|
|
|
|
|
- // Noise
|
|
|
|
- this.newPos.set( this.simplexX.noise4d( p.x, p.y, p.z, timeDimension ),
|
|
|
|
- this.simplexY.noise4d( p.x, p.y, p.z, timeDimension ),
|
|
|
|
- this.simplexZ.noise4d( p.x, p.y, p.z, timeDimension ) );
|
|
|
|
|
|
+ if ( lForwards < 0.000001 ) {
|
|
|
|
|
|
- this.newPos.multiplyScalar( segment.positionVariationFactor * lForwards );
|
|
|
|
- this.newPos.add( this.middlePos );
|
|
|
|
|
|
+ this.forwards.set( 0, 0, 0.01 );
|
|
|
|
+ lForwards = this.forwards.length();
|
|
|
|
|
|
- // Recursion
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- var newSegment1 = this.getNewSegment();
|
|
|
|
- newSegment1.pos0.copy( segment.pos0 );
|
|
|
|
- newSegment1.pos1.copy( this.newPos );
|
|
|
|
- newSegment1.linPos0.copy( segment.linPos0 );
|
|
|
|
- newSegment1.linPos1.copy( this.middleLinPos );
|
|
|
|
- newSegment1.up0.copy( segment.up0 );
|
|
|
|
- newSegment1.up1.copy( segment.up1 );
|
|
|
|
- newSegment1.radius0 = segment.radius0;
|
|
|
|
- newSegment1.radius1 = middleRadius;
|
|
|
|
- newSegment1.fraction0 = segment.fraction0;
|
|
|
|
- newSegment1.fraction1 = middleFraction;
|
|
|
|
- newSegment1.positionVariationFactor = segment.positionVariationFactor * this.currentSubray.roughness;
|
|
|
|
- newSegment1.iteration = segment.iteration + 1;
|
|
|
|
|
|
+ const middleRadius = ( segment.radius0 + segment.radius1 ) * 0.5;
|
|
|
|
+ const middleFraction = ( segment.fraction0 + segment.fraction1 ) * 0.5;
|
|
|
|
+
|
|
|
|
+ const timeDimension = this.time * this.currentSubray.timeScale * Math.pow( 2, segment.iteration );
|
|
|
|
+
|
|
|
|
+ this.middlePos.lerpVectors( segment.pos0, segment.pos1, 0.5 );
|
|
|
|
+ this.middleLinPos.lerpVectors( segment.linPos0, segment.linPos1, 0.5 );
|
|
|
|
+ const p = this.middleLinPos;
|
|
|
|
+
|
|
|
|
+ // Noise
|
|
|
|
+ this.newPos.set( this.simplexX.noise4d( p.x, p.y, p.z, timeDimension ),
|
|
|
|
+ this.simplexY.noise4d( p.x, p.y, p.z, timeDimension ),
|
|
|
|
+ this.simplexZ.noise4d( p.x, p.y, p.z, timeDimension ) );
|
|
|
|
+
|
|
|
|
+ this.newPos.multiplyScalar( segment.positionVariationFactor * lForwards );
|
|
|
|
+ this.newPos.add( this.middlePos );
|
|
|
|
+
|
|
|
|
+ // Recursion
|
|
|
|
+
|
|
|
|
+ const newSegment1 = this.getNewSegment();
|
|
|
|
+ newSegment1.pos0.copy( segment.pos0 );
|
|
|
|
+ newSegment1.pos1.copy( this.newPos );
|
|
|
|
+ newSegment1.linPos0.copy( segment.linPos0 );
|
|
|
|
+ newSegment1.linPos1.copy( this.middleLinPos );
|
|
|
|
+ newSegment1.up0.copy( segment.up0 );
|
|
|
|
+ newSegment1.up1.copy( segment.up1 );
|
|
|
|
+ newSegment1.radius0 = segment.radius0;
|
|
|
|
+ newSegment1.radius1 = middleRadius;
|
|
|
|
+ newSegment1.fraction0 = segment.fraction0;
|
|
|
|
+ newSegment1.fraction1 = middleFraction;
|
|
|
|
+ newSegment1.positionVariationFactor = segment.positionVariationFactor * this.currentSubray.roughness;
|
|
|
|
+ newSegment1.iteration = segment.iteration + 1;
|
|
|
|
+
|
|
|
|
+ const newSegment2 = this.getNewSegment();
|
|
|
|
+ newSegment2.pos0.copy( this.newPos );
|
|
|
|
+ newSegment2.pos1.copy( segment.pos1 );
|
|
|
|
+ newSegment2.linPos0.copy( this.middleLinPos );
|
|
|
|
+ newSegment2.linPos1.copy( segment.linPos1 );
|
|
|
|
+ this.cross1.crossVectors( segment.up0, this.forwards.normalize() );
|
|
|
|
+ newSegment2.up0.crossVectors( this.forwards, this.cross1 ).normalize();
|
|
|
|
+ newSegment2.up1.copy( segment.up1 );
|
|
|
|
+ newSegment2.radius0 = middleRadius;
|
|
|
|
+ newSegment2.radius1 = segment.radius1;
|
|
|
|
+ newSegment2.fraction0 = middleFraction;
|
|
|
|
+ newSegment2.fraction1 = segment.fraction1;
|
|
|
|
+ newSegment2.positionVariationFactor = segment.positionVariationFactor * this.currentSubray.roughness;
|
|
|
|
+ newSegment2.iteration = segment.iteration + 1;
|
|
|
|
+
|
|
|
|
+ this.fractalRayRecursive( newSegment1 );
|
|
|
|
+
|
|
|
|
+ this.fractalRayRecursive( newSegment2 );
|
|
|
|
|
|
- var newSegment2 = this.getNewSegment();
|
|
|
|
- newSegment2.pos0.copy( this.newPos );
|
|
|
|
- newSegment2.pos1.copy( segment.pos1 );
|
|
|
|
- newSegment2.linPos0.copy( this.middleLinPos );
|
|
|
|
- newSegment2.linPos1.copy( segment.linPos1 );
|
|
|
|
- this.cross1.crossVectors( segment.up0, this.forwards.normalize() );
|
|
|
|
- newSegment2.up0.crossVectors( this.forwards, this.cross1 ).normalize();
|
|
|
|
- newSegment2.up1.copy( segment.up1 );
|
|
|
|
- newSegment2.radius0 = middleRadius;
|
|
|
|
- newSegment2.radius1 = segment.radius1;
|
|
|
|
- newSegment2.fraction0 = middleFraction;
|
|
|
|
- newSegment2.fraction1 = segment.fraction1;
|
|
|
|
- newSegment2.positionVariationFactor = segment.positionVariationFactor * this.currentSubray.roughness;
|
|
|
|
- newSegment2.iteration = segment.iteration + 1;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- this.fractalRayRecursive( newSegment1 );
|
|
|
|
|
|
+ createPrism( segment ) {
|
|
|
|
|
|
- this.fractalRayRecursive( newSegment2 );
|
|
|
|
|
|
+ // Creates one triangular prism and its vertices at the segment
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ this.forwardsFill.subVectors( segment.pos1, segment.pos0 ).normalize();
|
|
|
|
|
|
-LightningStrike.prototype.createPrism = function ( segment ) {
|
|
|
|
|
|
+ if ( this.isInitialSegment ) {
|
|
|
|
|
|
- // Creates one triangular prism and its vertices at the segment
|
|
|
|
|
|
+ this.currentCreateTriangleVertices( segment.pos0, segment.up0, this.forwardsFill, segment.radius0, 0 );
|
|
|
|
|
|
- this.forwardsFill.subVectors( segment.pos1, segment.pos0 ).normalize();
|
|
|
|
|
|
+ this.isInitialSegment = false;
|
|
|
|
|
|
- if ( this.isInitialSegment ) {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- this.currentCreateTriangleVertices( segment.pos0, segment.up0, this.forwardsFill, segment.radius0, 0 );
|
|
|
|
|
|
+ this.currentCreateTriangleVertices( segment.pos1, segment.up0, this.forwardsFill, segment.radius1, segment.fraction1 );
|
|
|
|
|
|
- this.isInitialSegment = false;
|
|
|
|
|
|
+ this.createPrismFaces();
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- this.currentCreateTriangleVertices( segment.pos1, segment.up0, this.forwardsFill, segment.radius1, segment.fraction1 );
|
|
|
|
|
|
+ createTriangleVerticesWithoutUVs( pos, up, forwards, radius ) {
|
|
|
|
|
|
- this.createPrismFaces();
|
|
|
|
|
|
+ // Create an equilateral triangle (only vertices)
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ this.side.crossVectors( up, forwards ).multiplyScalar( radius * LightningStrike.COS30DEG );
|
|
|
|
+ this.down.copy( up ).multiplyScalar( - radius * LightningStrike.SIN30DEG );
|
|
|
|
|
|
-LightningStrike.prototype.createTriangleVerticesWithoutUVs = function ( pos, up, forwards, radius ) {
|
|
|
|
|
|
+ const p = this.vPos;
|
|
|
|
+ const v = this.vertices;
|
|
|
|
|
|
- // Create an equilateral triangle (only vertices)
|
|
|
|
|
|
+ p.copy( pos ).sub( this.side ).add( this.down );
|
|
|
|
|
|
- this.side.crossVectors( up, forwards ).multiplyScalar( radius * LightningStrike.COS30DEG );
|
|
|
|
- this.down.copy( up ).multiplyScalar( - radius * LightningStrike.SIN30DEG );
|
|
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
- var p = this.vPos;
|
|
|
|
- var v = this.vertices;
|
|
|
|
|
|
+ p.copy( pos ).add( this.side ).add( this.down );
|
|
|
|
|
|
- p.copy( pos ).sub( this.side ).add( this.down );
|
|
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
+ p.copy( up ).multiplyScalar( radius ).add( pos );
|
|
|
|
|
|
- p.copy( pos ).add( this.side ).add( this.down );
|
|
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
+ this.currentVertex += 3;
|
|
|
|
|
|
- p.copy( up ).multiplyScalar( radius ).add( pos );
|
|
|
|
-
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- this.currentVertex += 3;
|
|
|
|
|
|
+ createTriangleVerticesWithUVs( pos, up, forwards, radius, u ) {
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ // Create an equilateral triangle (only vertices)
|
|
|
|
|
|
-LightningStrike.prototype.createTriangleVerticesWithUVs = function ( pos, up, forwards, radius, u ) {
|
|
|
|
|
|
+ this.side.crossVectors( up, forwards ).multiplyScalar( radius * LightningStrike.COS30DEG );
|
|
|
|
+ this.down.copy( up ).multiplyScalar( - radius * LightningStrike.SIN30DEG );
|
|
|
|
|
|
- // Create an equilateral triangle (only vertices)
|
|
|
|
|
|
+ const p = this.vPos;
|
|
|
|
+ const v = this.vertices;
|
|
|
|
+ const uv = this.uvs;
|
|
|
|
|
|
- this.side.crossVectors( up, forwards ).multiplyScalar( radius * LightningStrike.COS30DEG );
|
|
|
|
- this.down.copy( up ).multiplyScalar( - radius * LightningStrike.SIN30DEG );
|
|
|
|
|
|
+ p.copy( pos ).sub( this.side ).add( this.down );
|
|
|
|
|
|
- var p = this.vPos;
|
|
|
|
- var v = this.vertices;
|
|
|
|
- var uv = this.uvs;
|
|
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
- p.copy( pos ).sub( this.side ).add( this.down );
|
|
|
|
|
|
+ uv[ this.currentUVCoordinate ++ ] = u;
|
|
|
|
+ uv[ this.currentUVCoordinate ++ ] = 0;
|
|
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
+ p.copy( pos ).add( this.side ).add( this.down );
|
|
|
|
|
|
- uv[ this.currentUVCoordinate ++ ] = u;
|
|
|
|
- uv[ this.currentUVCoordinate ++ ] = 0;
|
|
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
- p.copy( pos ).add( this.side ).add( this.down );
|
|
|
|
|
|
+ uv[ this.currentUVCoordinate ++ ] = u;
|
|
|
|
+ uv[ this.currentUVCoordinate ++ ] = 0.5;
|
|
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
+ p.copy( up ).multiplyScalar( radius ).add( pos );
|
|
|
|
|
|
- uv[ this.currentUVCoordinate ++ ] = u;
|
|
|
|
- uv[ this.currentUVCoordinate ++ ] = 0.5;
|
|
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
+ v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
- p.copy( up ).multiplyScalar( radius ).add( pos );
|
|
|
|
|
|
+ uv[ this.currentUVCoordinate ++ ] = u;
|
|
|
|
+ uv[ this.currentUVCoordinate ++ ] = 1;
|
|
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.x;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.y;
|
|
|
|
- v[ this.currentCoordinate ++ ] = p.z;
|
|
|
|
|
|
+ this.currentVertex += 3;
|
|
|
|
|
|
- uv[ this.currentUVCoordinate ++ ] = u;
|
|
|
|
- uv[ this.currentUVCoordinate ++ ] = 1;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- this.currentVertex += 3;
|
|
|
|
|
|
+ createPrismFaces( vertex/*, index*/ ) {
|
|
|
|
+
|
|
|
|
+ const indices = this.indices;
|
|
|
|
+ vertex = this.currentVertex - 6;
|
|
|
|
+
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 1;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 2;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 5;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 1;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 5;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 4;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 0;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 1;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 4;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 0;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 4;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 3;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 2;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 0;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 3;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 2;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 3;
|
|
|
|
+ indices[ this.currentIndex ++ ] = vertex + 5;
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ }
|
|
|
|
|
|
-LightningStrike.prototype.createPrismFaces = function ( vertex/*, index*/ ) {
|
|
|
|
|
|
+ createDefaultSubrayCreationCallbacks() {
|
|
|
|
|
|
- var indices = this.indices;
|
|
|
|
- var vertex = this.currentVertex - 6;
|
|
|
|
|
|
+ const random1 = this.randomGenerator.random;
|
|
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 1;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 2;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 5;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 1;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 5;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 4;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 0;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 1;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 4;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 0;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 4;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 3;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 2;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 0;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 3;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 2;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 3;
|
|
|
|
- indices[ this.currentIndex ++ ] = vertex + 5;
|
|
|
|
|
|
+ this.onDecideSubrayCreation = function ( segment, lightningStrike ) {
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ // Decide subrays creation at parent (sub)ray segment
|
|
|
|
|
|
-LightningStrike.prototype.createDefaultSubrayCreationCallbacks = function () {
|
|
|
|
|
|
+ const subray = lightningStrike.currentSubray;
|
|
|
|
|
|
- var random1 = this.randomGenerator.random;
|
|
|
|
|
|
+ const period = lightningStrike.rayParameters.subrayPeriod;
|
|
|
|
+ const dutyCycle = lightningStrike.rayParameters.subrayDutyCycle;
|
|
|
|
|
|
- this.onDecideSubrayCreation = function ( segment, lightningStrike ) {
|
|
|
|
|
|
+ const phase0 = ( lightningStrike.rayParameters.isEternal && subray.recursion == 0 ) ? - random1() * period : MathUtils.lerp( subray.birthTime, subray.endPropagationTime, segment.fraction0 ) - random1() * period;
|
|
|
|
|
|
- // Decide subrays creation at parent (sub)ray segment
|
|
|
|
|
|
+ const phase = lightningStrike.time - phase0;
|
|
|
|
+ const currentCycle = Math.floor( phase / period );
|
|
|
|
|
|
- var subray = lightningStrike.currentSubray;
|
|
|
|
|
|
+ const childSubraySeed = random1() * ( currentCycle + 1 );
|
|
|
|
|
|
- var period = lightningStrike.rayParameters.subrayPeriod;
|
|
|
|
- var dutyCycle = lightningStrike.rayParameters.subrayDutyCycle;
|
|
|
|
|
|
+ const isActive = phase % period <= dutyCycle * period;
|
|
|
|
|
|
- var phase0 = ( lightningStrike.rayParameters.isEternal && subray.recursion == 0 ) ? - random1() * period : MathUtils.lerp( subray.birthTime, subray.endPropagationTime, segment.fraction0 ) - random1() * period;
|
|
|
|
|
|
+ let probability = 0;
|
|
|
|
|
|
- var phase = lightningStrike.time - phase0;
|
|
|
|
- var currentCycle = Math.floor( phase / period );
|
|
|
|
|
|
+ if ( isActive ) {
|
|
|
|
|
|
- var childSubraySeed = random1() * ( currentCycle + 1 );
|
|
|
|
|
|
+ probability = lightningStrike.subrayProbability;
|
|
|
|
+ // Distribution test: probability *= segment.fraction0 > 0.5 && segment.fraction0 < 0.9 ? 1 / 0.4 : 0;
|
|
|
|
|
|
- var isActive = phase % period <= dutyCycle * period;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- var probability = 0;
|
|
|
|
|
|
+ if ( subray.recursion < lightningStrike.maxSubrayRecursion && lightningStrike.numSubrays < lightningStrike.maxSubrays && random1() < probability ) {
|
|
|
|
|
|
- if ( isActive ) {
|
|
|
|
|
|
+ const childSubray = lightningStrike.addNewSubray();
|
|
|
|
|
|
- probability = lightningStrike.subrayProbability;
|
|
|
|
- // Distribution test: probability *= segment.fraction0 > 0.5 && segment.fraction0 < 0.9 ? 1 / 0.4 : 0;
|
|
|
|
|
|
+ const parentSeed = lightningStrike.randomGenerator.getSeed();
|
|
|
|
+ childSubray.seed = childSubraySeed;
|
|
|
|
+ lightningStrike.randomGenerator.setSeed( childSubraySeed );
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ childSubray.recursion = subray.recursion + 1;
|
|
|
|
+ childSubray.maxIterations = Math.max( 1, subray.maxIterations - 1 );
|
|
|
|
|
|
- if ( subray.recursion < lightningStrike.maxSubrayRecursion && lightningStrike.numSubrays < lightningStrike.maxSubrays && random1() < probability ) {
|
|
|
|
|
|
+ childSubray.linPos0.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
|
|
|
|
+ childSubray.linPos1.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
|
|
|
|
+ childSubray.up0.copy( subray.up0 );
|
|
|
|
+ childSubray.up1.copy( subray.up1 );
|
|
|
|
+ childSubray.radius0 = segment.radius0 * lightningStrike.rayParameters.radius0Factor;
|
|
|
|
+ childSubray.radius1 = Math.min( lightningStrike.rayParameters.minRadius, segment.radius1 * lightningStrike.rayParameters.radius1Factor );
|
|
|
|
|
|
- var childSubray = lightningStrike.addNewSubray();
|
|
|
|
|
|
+ childSubray.birthTime = phase0 + ( currentCycle ) * period;
|
|
|
|
+ childSubray.deathTime = childSubray.birthTime + period * dutyCycle;
|
|
|
|
|
|
- var parentSeed = lightningStrike.randomGenerator.getSeed();
|
|
|
|
- childSubray.seed = childSubraySeed;
|
|
|
|
- lightningStrike.randomGenerator.setSeed( childSubraySeed );
|
|
|
|
|
|
+ if ( ! lightningStrike.rayParameters.isEternal && subray.recursion == 0 ) {
|
|
|
|
|
|
- childSubray.recursion = subray.recursion + 1;
|
|
|
|
- childSubray.maxIterations = Math.max( 1, subray.maxIterations - 1 );
|
|
|
|
|
|
+ childSubray.birthTime = Math.max( childSubray.birthTime, subray.birthTime );
|
|
|
|
+ childSubray.deathTime = Math.min( childSubray.deathTime, subray.deathTime );
|
|
|
|
|
|
- childSubray.linPos0.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
|
|
|
|
- childSubray.linPos1.set( random1(), random1(), random1() ).multiplyScalar( 1000 );
|
|
|
|
- childSubray.up0.copy( subray.up0 );
|
|
|
|
- childSubray.up1.copy( subray.up1 );
|
|
|
|
- childSubray.radius0 = segment.radius0 * lightningStrike.rayParameters.radius0Factor;
|
|
|
|
- childSubray.radius1 = Math.min( lightningStrike.rayParameters.minRadius, segment.radius1 * lightningStrike.rayParameters.radius1Factor );
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- childSubray.birthTime = phase0 + ( currentCycle ) * period;
|
|
|
|
- childSubray.deathTime = childSubray.birthTime + period * dutyCycle;
|
|
|
|
|
|
+ childSubray.timeScale = subray.timeScale * 2;
|
|
|
|
+ childSubray.roughness = subray.roughness;
|
|
|
|
+ childSubray.straightness = subray.straightness;
|
|
|
|
+ childSubray.propagationTimeFactor = subray.propagationTimeFactor;
|
|
|
|
+ childSubray.vanishingTimeFactor = subray.vanishingTimeFactor;
|
|
|
|
|
|
- if ( ! lightningStrike.rayParameters.isEternal && subray.recursion == 0 ) {
|
|
|
|
|
|
+ lightningStrike.onSubrayCreation( segment, subray, childSubray, lightningStrike );
|
|
|
|
|
|
- childSubray.birthTime = Math.max( childSubray.birthTime, subray.birthTime );
|
|
|
|
- childSubray.deathTime = Math.min( childSubray.deathTime, subray.deathTime );
|
|
|
|
|
|
+ lightningStrike.randomGenerator.setSeed( parentSeed );
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- childSubray.timeScale = subray.timeScale * 2;
|
|
|
|
- childSubray.roughness = subray.roughness;
|
|
|
|
- childSubray.straightness = subray.straightness;
|
|
|
|
- childSubray.propagationTimeFactor = subray.propagationTimeFactor;
|
|
|
|
- childSubray.vanishingTimeFactor = subray.vanishingTimeFactor;
|
|
|
|
-
|
|
|
|
- lightningStrike.onSubrayCreation( segment, subray, childSubray, lightningStrike );
|
|
|
|
|
|
+ };
|
|
|
|
|
|
- lightningStrike.randomGenerator.setSeed( parentSeed );
|
|
|
|
|
|
+ const vec1Pos = new Vector3();
|
|
|
|
+ const vec2Forward = new Vector3();
|
|
|
|
+ const vec3Side = new Vector3();
|
|
|
|
+ const vec4Up = new Vector3();
|
|
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- var vec1Pos = new Vector3();
|
|
|
|
- var vec2Forward = new Vector3();
|
|
|
|
- var vec3Side = new Vector3();
|
|
|
|
- var vec4Up = new Vector3();
|
|
|
|
-
|
|
|
|
- this.onSubrayCreation = function ( segment, parentSubray, childSubray, lightningStrike ) {
|
|
|
|
|
|
+ this.onSubrayCreation = function ( segment, parentSubray, childSubray, lightningStrike ) {
|
|
|
|
|
|
- // Decide childSubray origin and destination positions (pos0 and pos1) and possibly other properties of childSubray
|
|
|
|
|
|
+ // Decide childSubray origin and destination positions (pos0 and pos1) and possibly other properties of childSubray
|
|
|
|
|
|
- // Just use the default cone position generator
|
|
|
|
- lightningStrike.subrayCylinderPosition( segment, parentSubray, childSubray, 0.5, 0.6, 0.2 );
|
|
|
|
|
|
+ // Just use the default cone position generator
|
|
|
|
+ lightningStrike.subrayCylinderPosition( segment, parentSubray, childSubray, 0.5, 0.6, 0.2 );
|
|
|
|
|
|
- };
|
|
|
|
|
|
+ };
|
|
|
|
|
|
- this.subrayConePosition = function ( segment, parentSubray, childSubray, heightFactor, sideWidthFactor, minSideWidthFactor ) {
|
|
|
|
|
|
+ this.subrayConePosition = function ( segment, parentSubray, childSubray, heightFactor, sideWidthFactor, minSideWidthFactor ) {
|
|
|
|
|
|
- // Sets childSubray pos0 and pos1 in a cone
|
|
|
|
|
|
+ // Sets childSubray pos0 and pos1 in a cone
|
|
|
|
|
|
- childSubray.pos0.copy( segment.pos0 );
|
|
|
|
|
|
+ childSubray.pos0.copy( segment.pos0 );
|
|
|
|
|
|
- vec1Pos.subVectors( parentSubray.pos1, parentSubray.pos0 );
|
|
|
|
- vec2Forward.copy( vec1Pos ).normalize();
|
|
|
|
- vec1Pos.multiplyScalar( segment.fraction0 + ( 1 - segment.fraction0 ) * ( random1() * heightFactor ) );
|
|
|
|
- var length = vec1Pos.length();
|
|
|
|
- vec3Side.crossVectors( parentSubray.up0, vec2Forward );
|
|
|
|
- var angle = 2 * Math.PI * random1();
|
|
|
|
- vec3Side.multiplyScalar( Math.cos( angle ) );
|
|
|
|
- vec4Up.copy( parentSubray.up0 ).multiplyScalar( Math.sin( angle ) );
|
|
|
|
|
|
+ vec1Pos.subVectors( parentSubray.pos1, parentSubray.pos0 );
|
|
|
|
+ vec2Forward.copy( vec1Pos ).normalize();
|
|
|
|
+ vec1Pos.multiplyScalar( segment.fraction0 + ( 1 - segment.fraction0 ) * ( random1() * heightFactor ) );
|
|
|
|
+ const length = vec1Pos.length();
|
|
|
|
+ vec3Side.crossVectors( parentSubray.up0, vec2Forward );
|
|
|
|
+ const angle = 2 * Math.PI * random1();
|
|
|
|
+ vec3Side.multiplyScalar( Math.cos( angle ) );
|
|
|
|
+ vec4Up.copy( parentSubray.up0 ).multiplyScalar( Math.sin( angle ) );
|
|
|
|
|
|
- childSubray.pos1.copy( vec3Side ).add( vec4Up ).multiplyScalar( length * sideWidthFactor * ( minSideWidthFactor + random1() * ( 1 - minSideWidthFactor ) ) ).add( vec1Pos ).add( parentSubray.pos0 );
|
|
|
|
|
|
+ childSubray.pos1.copy( vec3Side ).add( vec4Up ).multiplyScalar( length * sideWidthFactor * ( minSideWidthFactor + random1() * ( 1 - minSideWidthFactor ) ) ).add( vec1Pos ).add( parentSubray.pos0 );
|
|
|
|
|
|
- };
|
|
|
|
|
|
+ };
|
|
|
|
|
|
- this.subrayCylinderPosition = function ( segment, parentSubray, childSubray, heightFactor, sideWidthFactor, minSideWidthFactor ) {
|
|
|
|
|
|
+ this.subrayCylinderPosition = function ( segment, parentSubray, childSubray, heightFactor, sideWidthFactor, minSideWidthFactor ) {
|
|
|
|
|
|
- // Sets childSubray pos0 and pos1 in a cylinder
|
|
|
|
|
|
+ // Sets childSubray pos0 and pos1 in a cylinder
|
|
|
|
|
|
- childSubray.pos0.copy( segment.pos0 );
|
|
|
|
|
|
+ childSubray.pos0.copy( segment.pos0 );
|
|
|
|
|
|
- vec1Pos.subVectors( parentSubray.pos1, parentSubray.pos0 );
|
|
|
|
- vec2Forward.copy( vec1Pos ).normalize();
|
|
|
|
- vec1Pos.multiplyScalar( segment.fraction0 + ( 1 - segment.fraction0 ) * ( ( 2 * random1() - 1 ) * heightFactor ) );
|
|
|
|
- var length = vec1Pos.length();
|
|
|
|
- vec3Side.crossVectors( parentSubray.up0, vec2Forward );
|
|
|
|
- var angle = 2 * Math.PI * random1();
|
|
|
|
- vec3Side.multiplyScalar( Math.cos( angle ) );
|
|
|
|
- vec4Up.copy( parentSubray.up0 ).multiplyScalar( Math.sin( angle ) );
|
|
|
|
|
|
+ vec1Pos.subVectors( parentSubray.pos1, parentSubray.pos0 );
|
|
|
|
+ vec2Forward.copy( vec1Pos ).normalize();
|
|
|
|
+ vec1Pos.multiplyScalar( segment.fraction0 + ( 1 - segment.fraction0 ) * ( ( 2 * random1() - 1 ) * heightFactor ) );
|
|
|
|
+ const length = vec1Pos.length();
|
|
|
|
+ vec3Side.crossVectors( parentSubray.up0, vec2Forward );
|
|
|
|
+ const angle = 2 * Math.PI * random1();
|
|
|
|
+ vec3Side.multiplyScalar( Math.cos( angle ) );
|
|
|
|
+ vec4Up.copy( parentSubray.up0 ).multiplyScalar( Math.sin( angle ) );
|
|
|
|
|
|
- childSubray.pos1.copy( vec3Side ).add( vec4Up ).multiplyScalar( length * sideWidthFactor * ( minSideWidthFactor + random1() * ( 1 - minSideWidthFactor ) ) ).add( vec1Pos ).add( parentSubray.pos0 );
|
|
|
|
|
|
+ childSubray.pos1.copy( vec3Side ).add( vec4Up ).multiplyScalar( length * sideWidthFactor * ( minSideWidthFactor + random1() * ( 1 - minSideWidthFactor ) ) ).add( vec1Pos ).add( parentSubray.pos0 );
|
|
|
|
|
|
- };
|
|
|
|
|
|
+ };
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ }
|
|
|
|
|
|
-LightningStrike.prototype.createSubray = function () {
|
|
|
|
|
|
+ createSubray() {
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+
|
|
|
|
+ seed: 0,
|
|
|
|
+ maxIterations: 0,
|
|
|
|
+ recursion: 0,
|
|
|
|
+ pos0: new Vector3(),
|
|
|
|
+ pos1: new Vector3(),
|
|
|
|
+ linPos0: new Vector3(),
|
|
|
|
+ linPos1: new Vector3(),
|
|
|
|
+ up0: new Vector3(),
|
|
|
|
+ up1: new Vector3(),
|
|
|
|
+ radius0: 0,
|
|
|
|
+ radius1: 0,
|
|
|
|
+ birthTime: 0,
|
|
|
|
+ deathTime: 0,
|
|
|
|
+ timeScale: 0,
|
|
|
|
+ roughness: 0,
|
|
|
|
+ straightness: 0,
|
|
|
|
+ propagationTimeFactor: 0,
|
|
|
|
+ vanishingTimeFactor: 0,
|
|
|
|
+ endPropagationTime: 0,
|
|
|
|
+ beginVanishingTime: 0
|
|
|
|
+
|
|
|
|
+ };
|
|
|
|
|
|
- return {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- seed: 0,
|
|
|
|
- maxIterations: 0,
|
|
|
|
- recursion: 0,
|
|
|
|
- pos0: new Vector3(),
|
|
|
|
- pos1: new Vector3(),
|
|
|
|
- linPos0: new Vector3(),
|
|
|
|
- linPos1: new Vector3(),
|
|
|
|
- up0: new Vector3(),
|
|
|
|
- up1: new Vector3(),
|
|
|
|
- radius0: 0,
|
|
|
|
- radius1: 0,
|
|
|
|
- birthTime: 0,
|
|
|
|
- deathTime: 0,
|
|
|
|
- timeScale: 0,
|
|
|
|
- roughness: 0,
|
|
|
|
- straightness: 0,
|
|
|
|
- propagationTimeFactor: 0,
|
|
|
|
- vanishingTimeFactor: 0,
|
|
|
|
- endPropagationTime: 0,
|
|
|
|
- beginVanishingTime: 0
|
|
|
|
|
|
+ createSegment() {
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ iteration: 0,
|
|
|
|
+ pos0: new Vector3(),
|
|
|
|
+ pos1: new Vector3(),
|
|
|
|
+ linPos0: new Vector3(),
|
|
|
|
+ linPos1: new Vector3(),
|
|
|
|
+ up0: new Vector3(),
|
|
|
|
+ up1: new Vector3(),
|
|
|
|
+ radius0: 0,
|
|
|
|
+ radius1: 0,
|
|
|
|
+ fraction0: 0,
|
|
|
|
+ fraction1: 0,
|
|
|
|
+ positionVariationFactor: 0
|
|
|
|
+ };
|
|
|
|
|
|
- };
|
|
|
|
|
|
+ }
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ getNewSegment() {
|
|
|
|
|
|
-LightningStrike.prototype.createSegment = function () {
|
|
|
|
|
|
+ return this.raySegments[ this.currentSegmentIndex ++ ];
|
|
|
|
|
|
- return {
|
|
|
|
- iteration: 0,
|
|
|
|
- pos0: new Vector3(),
|
|
|
|
- pos1: new Vector3(),
|
|
|
|
- linPos0: new Vector3(),
|
|
|
|
- linPos1: new Vector3(),
|
|
|
|
- up0: new Vector3(),
|
|
|
|
- up1: new Vector3(),
|
|
|
|
- radius0: 0,
|
|
|
|
- radius1: 0,
|
|
|
|
- fraction0: 0,
|
|
|
|
- fraction1: 0,
|
|
|
|
- positionVariationFactor: 0
|
|
|
|
- };
|
|
|
|
|
|
+ }
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ copy( source ) {
|
|
|
|
|
|
-LightningStrike.prototype.getNewSegment = function () {
|
|
|
|
|
|
+ super.copy( source );
|
|
|
|
|
|
- return this.raySegments[ this.currentSegmentIndex ++ ];
|
|
|
|
|
|
+ this.init( LightningStrike.copyParameters( {}, source.rayParameters ) );
|
|
|
|
|
|
-};
|
|
|
|
|
|
+ return this;
|
|
|
|
|
|
-LightningStrike.prototype.copy = function ( source ) {
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- BufferGeometry.prototype.copy.call( this, source );
|
|
|
|
|
|
+ clone() {
|
|
|
|
|
|
- this.init( LightningStrike.copyParameters( {}, source.rayParameters ) );
|
|
|
|
|
|
+ return new this.constructor( LightningStrike.copyParameters( {}, this.rayParameters ) );
|
|
|
|
|
|
- return this;
|
|
|
|
|
|
+ }
|
|
|
|
|
|
-};
|
|
|
|
|
|
+}
|
|
|
|
|
|
-LightningStrike.prototype.clone = function () {
|
|
|
|
|
|
+LightningStrike.prototype.isLightningStrike = true;
|
|
|
|
|
|
- return new this.constructor( LightningStrike.copyParameters( {}, this.rayParameters ) );
|
|
|
|
|
|
+// Ray states
|
|
|
|
+LightningStrike.RAY_INITIALIZED = 0;
|
|
|
|
+LightningStrike.RAY_UNBORN = 1;
|
|
|
|
+LightningStrike.RAY_PROPAGATING = 2;
|
|
|
|
+LightningStrike.RAY_STEADY = 3;
|
|
|
|
+LightningStrike.RAY_VANISHING = 4;
|
|
|
|
+LightningStrike.RAY_EXTINGUISHED = 5;
|
|
|
|
|
|
-};
|
|
|
|
|
|
+LightningStrike.COS30DEG = Math.cos( 30 * Math.PI / 180 );
|
|
|
|
+LightningStrike.SIN30DEG = Math.sin( 30 * Math.PI / 180 );
|
|
|
|
|
|
export { LightningStrike };
|
|
export { LightningStrike };
|