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="http://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.gammaInput = true;
  42. renderer.gammaOutput = true;
  43. container.appendChild( renderer.domElement );
  44. composer = new EffectComposer( renderer );
  45. stats = new Stats();
  46. container.appendChild( stats.dom );
  47. window.addEventListener( 'resize', onWindowResize, false );
  48. createScene();
  49. }
  50. function createScene() {
  51. scene = sceneCreators[ currentSceneIndex ]();
  52. createGUI();
  53. }
  54. function onWindowResize() {
  55. scene.userData.camera.aspect = window.innerWidth / window.innerHeight;
  56. scene.userData.camera.updateProjectionMatrix();
  57. renderer.setSize( window.innerWidth, window.innerHeight );
  58. composer.setSize( window.innerWidth, window.innerHeight );
  59. }
  60. //
  61. function createGUI() {
  62. if ( gui ) {
  63. gui.destroy();
  64. }
  65. gui = new GUI( { width: 350 } );
  66. var sceneFolder = gui.addFolder( "Scene" );
  67. scene.userData.sceneIndex = currentSceneIndex;
  68. sceneFolder.add( scene.userData, 'sceneIndex', { "Electric Cones": 0, "Plasma Ball": 1, "Storm": 2 } ).name( 'Scene' ).onChange( function ( value ) {
  69. currentSceneIndex = value;
  70. createScene();
  71. } );
  72. scene.userData.timeRate = 1;
  73. sceneFolder.add( scene.userData, 'timeRate', scene.userData.canGoBackwardsInTime ? - 1 : 0, 1 ).name( 'Time rate' );
  74. sceneFolder.open();
  75. var graphicsFolder = gui.addFolder( "Graphics" );
  76. graphicsFolder.add( scene.userData, "outlineEnabled" ).name( "Glow enabled" );
  77. scene.userData.lightningColorRGB = [
  78. scene.userData.lightningColor.r * 255,
  79. scene.userData.lightningColor.g * 255,
  80. scene.userData.lightningColor.b * 255
  81. ];
  82. graphicsFolder.addColor( scene.userData, 'lightningColorRGB' ).name( 'Color' ).onChange( function ( value ) {
  83. scene.userData.lightningMaterial.color.setRGB( value[ 0 ], value[ 1 ], value[ 2 ] ).multiplyScalar( 1 / 255 );
  84. } );
  85. scene.userData.outlineColorRGB = [
  86. scene.userData.outlineColor.r * 255,
  87. scene.userData.outlineColor.g * 255,
  88. scene.userData.outlineColor.b * 255
  89. ];
  90. graphicsFolder.addColor( scene.userData, 'outlineColorRGB' ).name( 'Glow color' ).onChange( function ( value ) {
  91. scene.userData.outlineColor.setRGB( value[ 0 ], value[ 1 ], value[ 2 ] ).multiplyScalar( 1 / 255 );
  92. } );
  93. graphicsFolder.open();
  94. var rayFolder = gui.addFolder( "Ray parameters" );
  95. rayFolder.add( scene.userData.rayParams, 'straightness', 0, 1 ).name( 'Straightness' );
  96. rayFolder.add( scene.userData.rayParams, 'roughness', 0, 1 ).name( 'Roughness' );
  97. rayFolder.add( scene.userData.rayParams, 'radius0', 0.1, 10 ).name( 'Initial radius' );
  98. rayFolder.add( scene.userData.rayParams, 'radius1', 0.1, 10 ).name( 'Final radius' );
  99. rayFolder.add( scene.userData.rayParams, 'radius0Factor', 0, 1 ).name( 'Subray initial radius' );
  100. rayFolder.add( scene.userData.rayParams, 'radius1Factor', 0, 1 ).name( 'Subray final radius' );
  101. rayFolder.add( scene.userData.rayParams, 'timeScale', 0, 5 ).name( 'Ray time scale' );
  102. rayFolder.add( scene.userData.rayParams, 'subrayPeriod', 0.1, 10 ).name( 'Subray period (s)' );
  103. rayFolder.add( scene.userData.rayParams, 'subrayDutyCycle', 0, 1 ).name( 'Subray duty cycle' );
  104. if ( scene.userData.recreateRay ) {
  105. // Parameters which need to recreate the ray after modification
  106. var raySlowFolder = gui.addFolder( "Ray parameters (slow)" );
  107. raySlowFolder.add( scene.userData.rayParams, 'ramification', 0, 15 ).step( 1 ).name( 'Ramification' ).onFinishChange( function () {
  108. scene.userData.recreateRay();
  109. } );
  110. raySlowFolder.add( scene.userData.rayParams, 'maxSubrayRecursion', 0, 5 ).step( 1 ).name( 'Recursion' ).onFinishChange( function () {
  111. scene.userData.recreateRay();
  112. } );
  113. raySlowFolder.add( scene.userData.rayParams, 'recursionProbability', 0, 1 ).name( 'Rec. probability' ).onFinishChange( function () {
  114. scene.userData.recreateRay();
  115. } );
  116. raySlowFolder.open();
  117. }
  118. rayFolder.open();
  119. }
  120. //
  121. function animate() {
  122. requestAnimationFrame( animate );
  123. render();
  124. stats.update();
  125. }
  126. function render() {
  127. currentTime += scene.userData.timeRate * clock.getDelta();
  128. if ( currentTime < 0 ) {
  129. currentTime = 0;
  130. }
  131. scene.userData.render( currentTime );
  132. }
  133. function createOutline( scene, objectsArray, visibleColor ) {
  134. var outlinePass = new OutlinePass( new THREE.Vector2( window.innerWidth, window.innerHeight ), scene, scene.userData.camera, objectsArray );
  135. outlinePass.edgeStrength = 2.5;
  136. outlinePass.edgeGlow = 0.7;
  137. outlinePass.edgeThickness = 2.8;
  138. outlinePass.visibleEdgeColor = visibleColor;
  139. outlinePass.hiddenEdgeColor.set( 0 );
  140. composer.addPass( outlinePass );
  141. scene.userData.outlineEnabled = true;
  142. return outlinePass;
  143. }
  144. //
  145. function createConesScene() {
  146. var scene = new THREE.Scene();
  147. scene.background = new THREE.Color( 0x050505 );
  148. scene.userData.canGoBackwardsInTime = true;
  149. scene.userData.camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 200, 100000 );
  150. // Lights
  151. scene.userData.lightningColor = new THREE.Color( 0xB0FFFF );
  152. scene.userData.outlineColor = new THREE.Color( 0x00FFFF );
  153. var posLight = new THREE.PointLight( 0x00ffff, 1, 5000, 2 );
  154. scene.add( posLight );
  155. // Ground
  156. var ground = new THREE.Mesh( new THREE.PlaneBufferGeometry( 200000, 200000 ), new THREE.MeshPhongMaterial( { color: 0xC0C0C0, shininess: 0 } ) );
  157. ground.rotation.x = - Math.PI * 0.5;
  158. scene.add( ground );
  159. // Cones
  160. var conesDistance = 1000;
  161. var coneHeight = 200;
  162. var coneHeightHalf = coneHeight * 0.5;
  163. posLight.position.set( 0, ( conesDistance + coneHeight ) * 0.5, 0 );
  164. posLight.color = scene.userData.outlineColor;
  165. scene.userData.camera.position.set( 5 * coneHeight, 4 * coneHeight, 18 * coneHeight );
  166. var coneMesh1 = new THREE.Mesh( new THREE.ConeBufferGeometry( coneHeight, coneHeight, 30, 1, false ), new THREE.MeshPhongMaterial( { color: 0xFFFF00, emissive: 0x1F1F00 } ) );
  167. coneMesh1.rotation.x = Math.PI;
  168. coneMesh1.position.y = conesDistance + coneHeight;
  169. scene.add( coneMesh1 );
  170. var coneMesh2 = new THREE.Mesh( coneMesh1.geometry.clone(), new THREE.MeshPhongMaterial( { color: 0xFF2020, emissive: 0x1F0202 } ) );
  171. coneMesh2.position.y = coneHeightHalf;
  172. scene.add( coneMesh2 );
  173. // Lightning strike
  174. scene.userData.lightningMaterial = new THREE.MeshBasicMaterial( { color: scene.userData.lightningColor } );
  175. scene.userData.rayParams = {
  176. sourceOffset: new THREE.Vector3(),
  177. destOffset: new THREE.Vector3(),
  178. radius0: 4,
  179. radius1: 4,
  180. minRadius: 2.5,
  181. maxIterations: 7,
  182. isEternal: true,
  183. timeScale: 0.7,
  184. propagationTimeFactor: 0.05,
  185. vanishingTimeFactor: 0.95,
  186. subrayPeriod: 3.5,
  187. subrayDutyCycle: 0.6,
  188. maxSubrayRecursion: 3,
  189. ramification: 7,
  190. recursionProbability: 0.6,
  191. roughness: 0.85,
  192. straightness: 0.6
  193. };
  194. var lightningStrike;
  195. var lightningStrikeMesh;
  196. var outlineMeshArray = [];
  197. scene.userData.recreateRay = function () {
  198. if ( lightningStrikeMesh ) {
  199. scene.remove( lightningStrikeMesh );
  200. }
  201. lightningStrike = new LightningStrike( scene.userData.rayParams );
  202. lightningStrikeMesh = new THREE.Mesh( lightningStrike, scene.userData.lightningMaterial );
  203. outlineMeshArray.length = 0;
  204. outlineMeshArray.push( lightningStrikeMesh );
  205. scene.add( lightningStrikeMesh );
  206. };
  207. scene.userData.recreateRay();
  208. // Compose rendering
  209. composer.passes = [];
  210. composer.addPass( new RenderPass( scene, scene.userData.camera ) );
  211. createOutline( scene, outlineMeshArray, scene.userData.outlineColor );
  212. // Controls
  213. var controls = new OrbitControls( scene.userData.camera, renderer.domElement );
  214. controls.target.y = ( conesDistance + coneHeight ) * 0.5;
  215. controls.enableDamping = true;
  216. controls.dampingFactor = 0.05;
  217. scene.userData.render = function ( time ) {
  218. // Move cones and Update ray position
  219. coneMesh1.position.set( Math.sin( 0.5 * time ) * conesDistance * 0.6, conesDistance + coneHeight, Math.cos( 0.5 * time ) * conesDistance * 0.6 );
  220. coneMesh2.position.set( Math.sin( 0.9 * time ) * conesDistance, coneHeightHalf, 0 );
  221. lightningStrike.rayParameters.sourceOffset.copy( coneMesh1.position );
  222. lightningStrike.rayParameters.sourceOffset.y -= coneHeightHalf;
  223. lightningStrike.rayParameters.destOffset.copy( coneMesh2.position );
  224. lightningStrike.rayParameters.destOffset.y += coneHeightHalf;
  225. lightningStrike.update( time );
  226. controls.update();
  227. // Update point light position to the middle of the ray
  228. posLight.position.lerpVectors( lightningStrike.rayParameters.sourceOffset, lightningStrike.rayParameters.destOffset, 0.5 );
  229. if ( scene.userData.outlineEnabled ) {
  230. composer.render();
  231. } else {
  232. renderer.render( scene, scene.userData.camera );
  233. }
  234. };
  235. return scene;
  236. }
  237. //
  238. function createPlasmaBallScene() {
  239. var scene = new THREE.Scene();
  240. scene.userData.canGoBackwardsInTime = true;
  241. scene.userData.camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 100, 50000 );
  242. var ballScene = new THREE.Scene();
  243. ballScene.background = new THREE.Color( 0x454545 );
  244. // Lights
  245. var ambientLight = new THREE.AmbientLight( 0x444444 );
  246. ballScene.add( ambientLight );
  247. scene.add( ambientLight );
  248. var light1 = new THREE.DirectionalLight( 0xffffff, 0.5 );
  249. light1.position.set( 1, 1, 1 );
  250. ballScene.add( light1 );
  251. scene.add( light1 );
  252. var light2 = new THREE.DirectionalLight( 0xffffff, 1.5 );
  253. light2.position.set( - 0.5, 1, 0.2 );
  254. ballScene.add( light2 );
  255. scene.add( light2 );
  256. // Plasma ball
  257. scene.userData.lightningColor = new THREE.Color( 0xFFB0FF );
  258. scene.userData.outlineColor = new THREE.Color( 0xFF00FF );
  259. scene.userData.lightningMaterial = new THREE.MeshBasicMaterial( { color: scene.userData.lightningColor, side: THREE.DoubleSide } );
  260. var r = "textures/cube/Bridge2/";
  261. var urls = [ r + "posx.jpg", r + "negx.jpg",
  262. r + "posy.jpg", r + "negy.jpg",
  263. r + "posz.jpg", r + "negz.jpg" ];
  264. var textureCube = new THREE.CubeTextureLoader().load( urls );
  265. textureCube.format = THREE.RGBFormat;
  266. textureCube.mapping = THREE.CubeReflectionMapping;
  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>