webgl_lightningstrike.html 22 KB

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