webgl_lightningstrike.html 22 KB

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