webgl_lightningstrike.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lightning strike</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - lightning strike</div>
  12. <script type="module">
  13. import * as THREE from '../build/three.module.js';
  14. import Stats from './jsm/libs/stats.module.js';
  15. import { GUI } from './jsm/libs/dat.gui.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { LightningStrike } from './jsm/geometries/LightningStrike.js';
  18. import { LightningStorm } from './jsm/objects/LightningStorm.js';
  19. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  20. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  21. import { OutlinePass } from './jsm/postprocessing/OutlinePass.js';
  22. var container, stats;
  23. var scene, renderer, composer, gui;
  24. var currentSceneIndex = 0;
  25. var currentTime = 0;
  26. var sceneCreators = [
  27. createConesScene,
  28. createPlasmaBallScene,
  29. createStormScene
  30. ];
  31. var clock = new THREE.Clock();
  32. var raycaster = new THREE.Raycaster();
  33. var mouse = new THREE.Vector2();
  34. init();
  35. animate();
  36. function init() {
  37. container = document.getElementById( 'container' );
  38. renderer = new THREE.WebGLRenderer();
  39. renderer.setPixelRatio( window.devicePixelRatio );
  40. renderer.setSize( window.innerWidth, window.innerHeight );
  41. renderer.outputEncoding = THREE.sRGBEncoding;
  42. container.appendChild( renderer.domElement );
  43. composer = new EffectComposer( renderer );
  44. stats = new Stats();
  45. container.appendChild( stats.dom );
  46. window.addEventListener( 'resize', onWindowResize, false );
  47. createScene();
  48. }
  49. function createScene() {
  50. scene = sceneCreators[ currentSceneIndex ]();
  51. createGUI();
  52. }
  53. function onWindowResize() {
  54. scene.userData.camera.aspect = window.innerWidth / window.innerHeight;
  55. scene.userData.camera.updateProjectionMatrix();
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. composer.setSize( window.innerWidth, window.innerHeight );
  58. }
  59. //
  60. function createGUI() {
  61. if ( gui ) {
  62. gui.destroy();
  63. }
  64. gui = new GUI( { width: 350 } );
  65. var sceneFolder = gui.addFolder( "Scene" );
  66. scene.userData.sceneIndex = currentSceneIndex;
  67. sceneFolder.add( scene.userData, 'sceneIndex', { "Electric Cones": 0, "Plasma Ball": 1, "Storm": 2 } ).name( 'Scene' ).onChange( function ( value ) {
  68. currentSceneIndex = value;
  69. createScene();
  70. } );
  71. scene.userData.timeRate = 1;
  72. sceneFolder.add( scene.userData, 'timeRate', scene.userData.canGoBackwardsInTime ? - 1 : 0, 1 ).name( 'Time rate' );
  73. sceneFolder.open();
  74. var graphicsFolder = gui.addFolder( "Graphics" );
  75. graphicsFolder.add( scene.userData, "outlineEnabled" ).name( "Glow enabled" );
  76. scene.userData.lightningColorRGB = [
  77. scene.userData.lightningColor.r * 255,
  78. scene.userData.lightningColor.g * 255,
  79. scene.userData.lightningColor.b * 255
  80. ];
  81. graphicsFolder.addColor( scene.userData, 'lightningColorRGB' ).name( 'Color' ).onChange( function ( value ) {
  82. scene.userData.lightningMaterial.color.setRGB( value[ 0 ], value[ 1 ], value[ 2 ] ).multiplyScalar( 1 / 255 );
  83. } );
  84. scene.userData.outlineColorRGB = [
  85. scene.userData.outlineColor.r * 255,
  86. scene.userData.outlineColor.g * 255,
  87. scene.userData.outlineColor.b * 255
  88. ];
  89. graphicsFolder.addColor( scene.userData, 'outlineColorRGB' ).name( 'Glow color' ).onChange( function ( value ) {
  90. scene.userData.outlineColor.setRGB( value[ 0 ], value[ 1 ], value[ 2 ] ).multiplyScalar( 1 / 255 );
  91. } );
  92. graphicsFolder.open();
  93. var rayFolder = gui.addFolder( "Ray parameters" );
  94. rayFolder.add( scene.userData.rayParams, 'straightness', 0, 1 ).name( 'Straightness' );
  95. rayFolder.add( scene.userData.rayParams, 'roughness', 0, 1 ).name( 'Roughness' );
  96. rayFolder.add( scene.userData.rayParams, 'radius0', 0.1, 10 ).name( 'Initial radius' );
  97. rayFolder.add( scene.userData.rayParams, 'radius1', 0.1, 10 ).name( 'Final radius' );
  98. rayFolder.add( scene.userData.rayParams, 'radius0Factor', 0, 1 ).name( 'Subray initial radius' );
  99. rayFolder.add( scene.userData.rayParams, 'radius1Factor', 0, 1 ).name( 'Subray final radius' );
  100. rayFolder.add( scene.userData.rayParams, 'timeScale', 0, 5 ).name( 'Ray time scale' );
  101. rayFolder.add( scene.userData.rayParams, 'subrayPeriod', 0.1, 10 ).name( 'Subray period (s)' );
  102. rayFolder.add( scene.userData.rayParams, 'subrayDutyCycle', 0, 1 ).name( 'Subray duty cycle' );
  103. if ( scene.userData.recreateRay ) {
  104. // Parameters which need to recreate the ray after modification
  105. var raySlowFolder = gui.addFolder( "Ray parameters (slow)" );
  106. raySlowFolder.add( scene.userData.rayParams, 'ramification', 0, 15 ).step( 1 ).name( 'Ramification' ).onFinishChange( function () {
  107. scene.userData.recreateRay();
  108. } );
  109. raySlowFolder.add( scene.userData.rayParams, 'maxSubrayRecursion', 0, 5 ).step( 1 ).name( 'Recursion' ).onFinishChange( function () {
  110. scene.userData.recreateRay();
  111. } );
  112. raySlowFolder.add( scene.userData.rayParams, 'recursionProbability', 0, 1 ).name( 'Rec. probability' ).onFinishChange( function () {
  113. scene.userData.recreateRay();
  114. } );
  115. raySlowFolder.open();
  116. }
  117. rayFolder.open();
  118. }
  119. //
  120. function animate() {
  121. requestAnimationFrame( animate );
  122. render();
  123. stats.update();
  124. }
  125. function render() {
  126. currentTime += scene.userData.timeRate * clock.getDelta();
  127. if ( currentTime < 0 ) {
  128. currentTime = 0;
  129. }
  130. scene.userData.render( currentTime );
  131. }
  132. function createOutline( scene, objectsArray, visibleColor ) {
  133. var outlinePass = new OutlinePass( new THREE.Vector2( window.innerWidth, window.innerHeight ), scene, scene.userData.camera, objectsArray );
  134. outlinePass.edgeStrength = 2.5;
  135. outlinePass.edgeGlow = 0.7;
  136. outlinePass.edgeThickness = 2.8;
  137. outlinePass.visibleEdgeColor = visibleColor;
  138. outlinePass.hiddenEdgeColor.set( 0 );
  139. composer.addPass( outlinePass );
  140. scene.userData.outlineEnabled = true;
  141. return outlinePass;
  142. }
  143. //
  144. function createConesScene() {
  145. var scene = new THREE.Scene();
  146. scene.background = new THREE.Color( 0x050505 );
  147. scene.userData.canGoBackwardsInTime = true;
  148. scene.userData.camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 200, 100000 );
  149. // Lights
  150. scene.userData.lightningColor = new THREE.Color( 0xB0FFFF );
  151. scene.userData.outlineColor = new THREE.Color( 0x00FFFF );
  152. var posLight = new THREE.PointLight( 0x00ffff, 1, 5000, 2 );
  153. scene.add( posLight );
  154. // Ground
  155. var ground = new THREE.Mesh( new THREE.PlaneBufferGeometry( 200000, 200000 ), new THREE.MeshPhongMaterial( { color: 0xC0C0C0, shininess: 0 } ) );
  156. ground.rotation.x = - Math.PI * 0.5;
  157. scene.add( ground );
  158. // Cones
  159. var conesDistance = 1000;
  160. var coneHeight = 200;
  161. var coneHeightHalf = coneHeight * 0.5;
  162. posLight.position.set( 0, ( conesDistance + coneHeight ) * 0.5, 0 );
  163. posLight.color = scene.userData.outlineColor;
  164. scene.userData.camera.position.set( 5 * coneHeight, 4 * coneHeight, 18 * coneHeight );
  165. var coneMesh1 = new THREE.Mesh( new THREE.ConeBufferGeometry( coneHeight, coneHeight, 30, 1, false ), new THREE.MeshPhongMaterial( { color: 0xFFFF00, emissive: 0x1F1F00 } ) );
  166. coneMesh1.rotation.x = Math.PI;
  167. coneMesh1.position.y = conesDistance + coneHeight;
  168. scene.add( coneMesh1 );
  169. var coneMesh2 = new THREE.Mesh( coneMesh1.geometry.clone(), new THREE.MeshPhongMaterial( { color: 0xFF2020, emissive: 0x1F0202 } ) );
  170. coneMesh2.position.y = coneHeightHalf;
  171. scene.add( coneMesh2 );
  172. // Lightning strike
  173. scene.userData.lightningMaterial = new THREE.MeshBasicMaterial( { color: scene.userData.lightningColor } );
  174. scene.userData.rayParams = {
  175. sourceOffset: new THREE.Vector3(),
  176. destOffset: new THREE.Vector3(),
  177. radius0: 4,
  178. radius1: 4,
  179. minRadius: 2.5,
  180. maxIterations: 7,
  181. isEternal: true,
  182. timeScale: 0.7,
  183. propagationTimeFactor: 0.05,
  184. vanishingTimeFactor: 0.95,
  185. subrayPeriod: 3.5,
  186. subrayDutyCycle: 0.6,
  187. maxSubrayRecursion: 3,
  188. ramification: 7,
  189. recursionProbability: 0.6,
  190. roughness: 0.85,
  191. straightness: 0.6
  192. };
  193. var lightningStrike;
  194. var lightningStrikeMesh;
  195. var outlineMeshArray = [];
  196. scene.userData.recreateRay = function () {
  197. if ( lightningStrikeMesh ) {
  198. scene.remove( lightningStrikeMesh );
  199. }
  200. lightningStrike = new LightningStrike( scene.userData.rayParams );
  201. lightningStrikeMesh = new THREE.Mesh( lightningStrike, scene.userData.lightningMaterial );
  202. outlineMeshArray.length = 0;
  203. outlineMeshArray.push( lightningStrikeMesh );
  204. scene.add( lightningStrikeMesh );
  205. };
  206. scene.userData.recreateRay();
  207. // Compose rendering
  208. composer.passes = [];
  209. composer.addPass( new RenderPass( scene, scene.userData.camera ) );
  210. createOutline( scene, outlineMeshArray, scene.userData.outlineColor );
  211. // Controls
  212. var controls = new OrbitControls( scene.userData.camera, renderer.domElement );
  213. controls.target.y = ( conesDistance + coneHeight ) * 0.5;
  214. controls.enableDamping = true;
  215. controls.dampingFactor = 0.05;
  216. scene.userData.render = function ( time ) {
  217. // Move cones and Update ray position
  218. coneMesh1.position.set( Math.sin( 0.5 * time ) * conesDistance * 0.6, conesDistance + coneHeight, Math.cos( 0.5 * time ) * conesDistance * 0.6 );
  219. coneMesh2.position.set( Math.sin( 0.9 * time ) * conesDistance, coneHeightHalf, 0 );
  220. lightningStrike.rayParameters.sourceOffset.copy( coneMesh1.position );
  221. lightningStrike.rayParameters.sourceOffset.y -= coneHeightHalf;
  222. lightningStrike.rayParameters.destOffset.copy( coneMesh2.position );
  223. lightningStrike.rayParameters.destOffset.y += coneHeightHalf;
  224. lightningStrike.update( time );
  225. controls.update();
  226. // Update point light position to the middle of the ray
  227. posLight.position.lerpVectors( lightningStrike.rayParameters.sourceOffset, lightningStrike.rayParameters.destOffset, 0.5 );
  228. if ( scene.userData.outlineEnabled ) {
  229. composer.render();
  230. } else {
  231. renderer.render( scene, scene.userData.camera );
  232. }
  233. };
  234. return scene;
  235. }
  236. //
  237. function createPlasmaBallScene() {
  238. var scene = new THREE.Scene();
  239. scene.userData.canGoBackwardsInTime = true;
  240. scene.userData.camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 100, 50000 );
  241. var ballScene = new THREE.Scene();
  242. ballScene.background = new THREE.Color( 0x454545 );
  243. // Lights
  244. var ambientLight = new THREE.AmbientLight( 0x444444 );
  245. ballScene.add( ambientLight );
  246. scene.add( ambientLight );
  247. var light1 = new THREE.DirectionalLight( 0xffffff, 0.5 );
  248. light1.position.set( 1, 1, 1 );
  249. ballScene.add( light1 );
  250. scene.add( light1 );
  251. var light2 = new THREE.DirectionalLight( 0xffffff, 1.5 );
  252. light2.position.set( - 0.5, 1, 0.2 );
  253. ballScene.add( light2 );
  254. scene.add( light2 );
  255. // Plasma ball
  256. scene.userData.lightningColor = new THREE.Color( 0xFFB0FF );
  257. scene.userData.outlineColor = new THREE.Color( 0xFF00FF );
  258. scene.userData.lightningMaterial = new THREE.MeshBasicMaterial( { color: scene.userData.lightningColor, side: THREE.DoubleSide } );
  259. var r = "textures/cube/Bridge2/";
  260. var urls = [ r + "posx.jpg", r + "negx.jpg",
  261. r + "posy.jpg", r + "negy.jpg",
  262. r + "posz.jpg", r + "negz.jpg" ];
  263. var textureCube = new THREE.CubeTextureLoader().load( urls );
  264. textureCube.format = THREE.RGBFormat;
  265. textureCube.mapping = THREE.CubeReflectionMapping;
  266. textureCube.encoding = THREE.sRGBEncoding;
  267. var sphereMaterial = new THREE.MeshPhysicalMaterial( {
  268. transparent: true,
  269. transparency: .96,
  270. depthWrite: false,
  271. color: 'white',
  272. metalness: 0,
  273. roughness: 0,
  274. envMap: textureCube
  275. } );
  276. var sphereHeight = 300;
  277. var sphereRadius = 200;
  278. scene.userData.camera.position.set( 5 * sphereRadius, 2 * sphereHeight, 6 * sphereRadius );
  279. var sphereMesh = new THREE.Mesh( new THREE.SphereBufferGeometry( sphereRadius, 80, 40 ), sphereMaterial );
  280. sphereMesh.position.set( 0, sphereHeight, 0 );
  281. ballScene.add( sphereMesh );
  282. var sphere = new THREE.Sphere( sphereMesh.position, sphereRadius );
  283. var spherePlasma = new THREE.Mesh( new THREE.SphereBufferGeometry( sphereRadius * 0.05, 24, 12 ), scene.userData.lightningMaterial );
  284. spherePlasma.position.copy( sphereMesh.position );
  285. spherePlasma.scale.y = 0.6;
  286. scene.add( spherePlasma );
  287. var post = new THREE.Mesh(
  288. new THREE.CylinderBufferGeometry( sphereRadius * 0.06, sphereRadius * 0.06, sphereHeight, 6, 1, true ),
  289. new THREE.MeshLambertMaterial( { color: 0x020202 } )
  290. );
  291. post.position.y = sphereHeight * 0.5 - sphereRadius * 0.05 * 1.2;
  292. scene.add( post );
  293. var box = new THREE.Mesh( new THREE.BoxBufferGeometry( sphereHeight * 0.5, sphereHeight * 0.1, sphereHeight * 0.5 ), post.material );
  294. box.position.y = sphereHeight * 0.05 * 0.5;
  295. scene.add( box );
  296. var rayDirection = new THREE.Vector3();
  297. var rayLength = 0;
  298. var vec1 = new THREE.Vector3();
  299. var vec2 = new THREE.Vector3();
  300. scene.userData.rayParams = {
  301. sourceOffset: sphereMesh.position,
  302. destOffset: new THREE.Vector3( sphereRadius, 0, 0 ).add( sphereMesh.position ),
  303. radius0: 4,
  304. radius1: 4,
  305. radius0Factor: 0.82,
  306. minRadius: 2.5,
  307. maxIterations: 6,
  308. isEternal: true,
  309. timeScale: 0.6,
  310. propagationTimeFactor: 0.15,
  311. vanishingTimeFactor: 0.87,
  312. subrayPeriod: 0.8,
  313. ramification: 5,
  314. recursionProbability: 0.8,
  315. roughness: 0.85,
  316. straightness: 0.7,
  317. onSubrayCreation: function ( segment, parentSubray, childSubray, lightningStrike ) {
  318. lightningStrike.subrayConePosition( segment, parentSubray, childSubray, 0.6, 0.9, 0.7 );
  319. // THREE.Sphere projection
  320. vec1.subVectors( childSubray.pos1, lightningStrike.rayParameters.sourceOffset );
  321. vec2.set( 0, 0, 0 );
  322. if ( lightningStrike.randomGenerator.random() < 0.7 ) {
  323. vec2.copy( rayDirection ).multiplyScalar( rayLength * 1.0865 );
  324. }
  325. vec1.add( vec2 ).setLength( rayLength );
  326. childSubray.pos1.addVectors( vec1, lightningStrike.rayParameters.sourceOffset );
  327. }
  328. };
  329. var lightningStrike;
  330. var lightningStrikeMesh;
  331. var outlineMeshArray = [];
  332. scene.userData.recreateRay = function () {
  333. if ( lightningStrikeMesh ) {
  334. scene.remove( lightningStrikeMesh );
  335. }
  336. lightningStrike = new LightningStrike( scene.userData.rayParams );
  337. lightningStrikeMesh = new THREE.Mesh( lightningStrike, scene.userData.lightningMaterial );
  338. outlineMeshArray.length = 0;
  339. outlineMeshArray.push( lightningStrikeMesh );
  340. outlineMeshArray.push( spherePlasma );
  341. scene.add( lightningStrikeMesh );
  342. };
  343. scene.userData.recreateRay();
  344. // Compose rendering
  345. composer.passes = [];
  346. composer.addPass( new RenderPass( ballScene, scene.userData.camera ) );
  347. var rayPass = new RenderPass( scene, scene.userData.camera );
  348. rayPass.clear = false;
  349. composer.addPass( rayPass );
  350. var outlinePass = createOutline( scene, outlineMeshArray, scene.userData.outlineColor );
  351. scene.userData.render = function ( time ) {
  352. rayDirection.subVectors( lightningStrike.rayParameters.destOffset, lightningStrike.rayParameters.sourceOffset );
  353. rayLength = rayDirection.length();
  354. rayDirection.normalize();
  355. lightningStrike.update( time );
  356. controls.update();
  357. outlinePass.enabled = scene.userData.outlineEnabled;
  358. composer.render();
  359. };
  360. // Controls
  361. var controls = new OrbitControls( scene.userData.camera, renderer.domElement );
  362. controls.target.copy( sphereMesh.position );
  363. controls.enableDamping = true;
  364. controls.dampingFactor = 0.05;
  365. // THREE.Sphere mouse raycasting
  366. window.addEventListener( 'mousemove', onTouchMove );
  367. window.addEventListener( 'touchmove', onTouchMove );
  368. function onTouchMove( event ) {
  369. var x, y;
  370. if ( event.changedTouches ) {
  371. x = event.changedTouches[ 0 ].pageX;
  372. y = event.changedTouches[ 0 ].pageY;
  373. } else {
  374. x = event.clientX;
  375. y = event.clientY;
  376. }
  377. mouse.x = ( x / window.innerWidth ) * 2 - 1;
  378. mouse.y = - ( y / window.innerHeight ) * 2 + 1;
  379. checkIntersection();
  380. }
  381. var intersection = new THREE.Vector3();
  382. function checkIntersection() {
  383. raycaster.setFromCamera( mouse, scene.userData.camera );
  384. var result = raycaster.ray.intersectSphere( sphere, intersection );
  385. if ( result !== null ) {
  386. lightningStrike.rayParameters.destOffset.copy( intersection );
  387. }
  388. }
  389. return scene;
  390. }
  391. //
  392. function createStormScene() {
  393. var scene = new THREE.Scene();
  394. scene.background = new THREE.Color( 0x050505 );
  395. scene.userData.canGoBackwardsInTime = false;
  396. scene.userData.camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 20, 10000 );
  397. // Lights
  398. scene.add( new THREE.AmbientLight( 0x444444 ) );
  399. var light1 = new THREE.DirectionalLight( 0xffffff, 0.5 );
  400. light1.position.set( 1, 1, 1 );
  401. scene.add( light1 );
  402. var posLight = new THREE.PointLight( 0x00ffff );
  403. posLight.position.set( 0, 100, 0 );
  404. scene.add( posLight );
  405. // Ground
  406. var GROUND_SIZE = 1000;
  407. scene.userData.camera.position.set( 0, 0.2, 1.6 ).multiplyScalar( GROUND_SIZE * 0.5 );
  408. var ground = new THREE.Mesh( new THREE.PlaneBufferGeometry( GROUND_SIZE, GROUND_SIZE ), new THREE.MeshLambertMaterial( { color: 0x072302 } ) );
  409. ground.rotation.x = - Math.PI * 0.5;
  410. scene.add( ground );
  411. // Storm
  412. scene.userData.lightningColor = new THREE.Color( 0xB0FFFF );
  413. scene.userData.outlineColor = new THREE.Color( 0x00FFFF );
  414. scene.userData.lightningMaterial = new THREE.MeshBasicMaterial( { color: scene.userData.lightningColor } );
  415. var rayDirection = new THREE.Vector3( 0, - 1, 0 );
  416. var rayLength = 0;
  417. var vec1 = new THREE.Vector3();
  418. var vec2 = new THREE.Vector3();
  419. scene.userData.rayParams = {
  420. radius0: 1,
  421. radius1: 0.5,
  422. minRadius: 0.3,
  423. maxIterations: 7,
  424. timeScale: 0.15,
  425. propagationTimeFactor: 0.2,
  426. vanishingTimeFactor: 0.9,
  427. subrayPeriod: 4,
  428. subrayDutyCycle: 0.6,
  429. maxSubrayRecursion: 3,
  430. ramification: 3,
  431. recursionProbability: 0.4,
  432. roughness: 0.85,
  433. straightness: 0.65,
  434. onSubrayCreation: function ( segment, parentSubray, childSubray, lightningStrike ) {
  435. lightningStrike.subrayConePosition( segment, parentSubray, childSubray, 0.6, 0.6, 0.5 );
  436. // Plane projection
  437. rayLength = lightningStrike.rayParameters.sourceOffset.y;
  438. vec1.subVectors( childSubray.pos1, lightningStrike.rayParameters.sourceOffset );
  439. var proj = rayDirection.dot( vec1 );
  440. vec2.copy( rayDirection ).multiplyScalar( proj );
  441. vec1.sub( vec2 );
  442. var scale = proj / rayLength > 0.5 ? rayLength / proj : 1;
  443. vec2.multiplyScalar( scale );
  444. vec1.add( vec2 );
  445. childSubray.pos1.addVectors( vec1, lightningStrike.rayParameters.sourceOffset );
  446. }
  447. };
  448. // Black star mark
  449. var starVertices = [];
  450. var prevPoint = new THREE.Vector3( 0, 0, 1 );
  451. var currPoint = new THREE.Vector3();
  452. for ( var i = 1; i <= 16; i ++ ) {
  453. currPoint.set( Math.sin( 2 * Math.PI * i / 16 ), 0, Math.cos( 2 * Math.PI * i / 16 ) );
  454. if ( i % 2 === 1 ) {
  455. currPoint.multiplyScalar( 0.3 );
  456. }
  457. starVertices.push( 0, 0, 0 );
  458. starVertices.push( prevPoint.x, prevPoint.y, prevPoint.z );
  459. starVertices.push( currPoint.x, currPoint.y, currPoint.z );
  460. prevPoint.copy( currPoint );
  461. }
  462. var starGeometry = new THREE.BufferGeometry();
  463. starGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( starVertices, 3 ) );
  464. var starMesh = new THREE.Mesh( starGeometry, new THREE.MeshBasicMaterial( { color: 0x020900 } ) );
  465. starMesh.scale.multiplyScalar( 6 );
  466. //
  467. var storm = new LightningStorm( {
  468. size: GROUND_SIZE,
  469. minHeight: 90,
  470. maxHeight: 200,
  471. maxSlope: 0.6,
  472. maxLightnings: 8,
  473. lightningParameters: scene.userData.rayParams,
  474. lightningMaterial: scene.userData.lightningMaterial,
  475. onLightningDown: function ( lightning ) {
  476. // Add black star mark at ray strike
  477. var star1 = starMesh.clone();
  478. star1.position.copy( lightning.rayParameters.destOffset );
  479. star1.position.y = 0.05;
  480. star1.rotation.y = 2 * Math.PI * Math.random();
  481. scene.add( star1 );
  482. }
  483. } );
  484. scene.add( storm );
  485. // Compose rendering
  486. composer.passes = [];
  487. composer.addPass( new RenderPass( scene, scene.userData.camera ) );
  488. createOutline( scene, storm.lightningsMeshes, scene.userData.outlineColor );
  489. // Controls
  490. var controls = new OrbitControls( scene.userData.camera, renderer.domElement );
  491. controls.target.y = GROUND_SIZE * 0.05;
  492. controls.enableDamping = true;
  493. controls.dampingFactor = 0.05;
  494. scene.userData.render = function ( time ) {
  495. storm.update( time );
  496. controls.update();
  497. if ( scene.userData.outlineEnabled ) {
  498. composer.render();
  499. } else {
  500. renderer.render( scene, scene.userData.camera );
  501. }
  502. };
  503. return scene;
  504. }
  505. </script>
  506. </body>
  507. </html>