webgl_lightningstrike.html 22 KB

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