webgl_lightningstrike.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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. outlinePass.renderToScreen = true;
  171. composer.addPass( outlinePass );
  172. scene.userData.outlineEnabled = true;
  173. return outlinePass;
  174. }
  175. //
  176. function createConesScene() {
  177. var scene = new THREE.Scene();
  178. scene.background = new THREE.Color( 0x050505 );
  179. scene.userData.canGoBackwardsInTime = true;
  180. scene.userData.camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 200, 100000 );
  181. // Lights
  182. scene.userData.lightningColor = new THREE.Color( 0xB0FFFF );
  183. scene.userData.outlineColor = new THREE.Color( 0x00FFFF );
  184. var posLight = new THREE.PointLight( 0x00ffff, 1, 5000, 2 );
  185. scene.add( posLight );
  186. // Ground
  187. var ground = new THREE.Mesh( new THREE.PlaneBufferGeometry( 200000, 200000 ), new THREE.MeshPhongMaterial( { color: 0xC0C0C0, shininess: 0 } ) );
  188. ground.rotation.x = - Math.PI * 0.5;
  189. scene.add( ground );
  190. // Cones
  191. var conesDistance = 1000;
  192. var coneHeight = 200;
  193. var coneHeightHalf = coneHeight * 0.5;
  194. posLight.position.set( 0, ( conesDistance + coneHeight ) * 0.5, 0 );
  195. posLight.color = scene.userData.outlineColor;
  196. scene.userData.camera.position.set( 5 * coneHeight, 4 * coneHeight, 18 * coneHeight );
  197. var coneMesh1 = new THREE.Mesh( new THREE.ConeBufferGeometry( coneHeight, coneHeight, 30, 1, false ), new THREE.MeshPhongMaterial( { color: 0xFFFF00, emissive: 0x1F1F00 } ) );
  198. coneMesh1.rotation.x = Math.PI;
  199. coneMesh1.position.y = conesDistance + coneHeight;
  200. scene.add( coneMesh1 );
  201. var coneMesh2 = new THREE.Mesh( coneMesh1.geometry.clone(), new THREE.MeshPhongMaterial( { color: 0xFF2020, emissive: 0x1F0202 } ) );
  202. coneMesh2.position.y = coneHeightHalf;
  203. scene.add( coneMesh2 );
  204. // Lightning strike
  205. scene.userData.lightningMaterial = new THREE.MeshBasicMaterial( { color: scene.userData.lightningColor } );
  206. scene.userData.rayParams = {
  207. sourceOffset: new THREE.Vector3(),
  208. destOffset: new THREE.Vector3(),
  209. radius0: 4,
  210. radius1: 4,
  211. minRadius: 2.5,
  212. maxIterations: 7,
  213. isEternal: true,
  214. timeScale: 0.7,
  215. propagationTimeFactor: 0.05,
  216. vanishingTimeFactor: 0.95,
  217. subrayPeriod: 3.5,
  218. subrayDutyCycle: 0.6,
  219. maxSubrayRecursion: 3,
  220. ramification: 7,
  221. recursionProbability: 0.6,
  222. roughness: 0.85,
  223. straightness: 0.6
  224. };
  225. var lightningStrike;
  226. var lightningStrikeMesh;
  227. var outlineMeshArray = [];
  228. scene.userData.recreateRay = function () {
  229. if ( lightningStrikeMesh ) {
  230. scene.remove( lightningStrikeMesh );
  231. }
  232. lightningStrike = new THREE.LightningStrike( scene.userData.rayParams );
  233. lightningStrikeMesh = new THREE.Mesh( lightningStrike, scene.userData.lightningMaterial );
  234. outlineMeshArray.length = 0;
  235. outlineMeshArray.push( lightningStrikeMesh );
  236. scene.add( lightningStrikeMesh );
  237. }
  238. scene.userData.recreateRay();
  239. // Compose rendering
  240. composer.passes = [];
  241. composer.addPass( new THREE.RenderPass( scene, scene.userData.camera ) );
  242. createOutline( scene, outlineMeshArray, scene.userData.outlineColor );
  243. // Controls
  244. var controls = new THREE.OrbitControls( scene.userData.camera, renderer.domElement );
  245. controls.target.y = ( conesDistance + coneHeight ) * 0.5
  246. controls.enableDamping = true;
  247. controls.dampingFactor = 0.25;
  248. scene.userData.render = function ( time ) {
  249. // Move cones and Update ray position
  250. coneMesh1.position.set( Math.sin( 0.5 * time ) * conesDistance * 0.6, conesDistance + coneHeight, Math.cos( 0.5 * time ) * conesDistance * 0.6 );
  251. coneMesh2.position.set( Math.sin( 0.9 * time ) * conesDistance, coneHeightHalf, 0 );
  252. lightningStrike.rayParameters.sourceOffset.copy( coneMesh1.position );
  253. lightningStrike.rayParameters.sourceOffset.y -= coneHeightHalf;
  254. lightningStrike.rayParameters.destOffset.copy( coneMesh2.position );
  255. lightningStrike.rayParameters.destOffset.y += coneHeightHalf;
  256. lightningStrike.update( time );
  257. controls.update();
  258. // Update point light position to the middle of the ray
  259. posLight.position.lerpVectors( lightningStrike.rayParameters.sourceOffset, lightningStrike.rayParameters.destOffset, 0.5 );
  260. if ( scene.userData.outlineEnabled ) {
  261. composer.render();
  262. }
  263. else {
  264. renderer.render( scene, scene.userData.camera );
  265. }
  266. };
  267. return scene;
  268. }
  269. //
  270. function createPlasmaBallScene() {
  271. var scene = new THREE.Scene();
  272. scene.userData.canGoBackwardsInTime = true;
  273. scene.userData.camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 100, 50000 );
  274. var ballScene = new THREE.Scene();
  275. ballScene.background = new THREE.Color( 0x454545 );
  276. // Lights
  277. var ambientLight = new THREE.AmbientLight( 0x444444 );
  278. ballScene.add( ambientLight );
  279. scene.add( ambientLight );
  280. var light1 = new THREE.DirectionalLight( 0xffffff, 0.5 );
  281. light1.position.set( 1, 1, 1 );
  282. ballScene.add( light1 );
  283. scene.add( light1 );
  284. var light2 = new THREE.DirectionalLight( 0xffffff, 1.5 );
  285. light2.position.set( -0.5, 1, 0.2 );
  286. ballScene.add( light2 );
  287. scene.add( light2 );
  288. // Plasma ball
  289. scene.userData.lightningColor = new THREE.Color( 0xFFB0FF );
  290. scene.userData.outlineColor = new THREE.Color( 0xFF00FF );
  291. scene.userData.lightningMaterial = new THREE.MeshBasicMaterial( { color: scene.userData.lightningColor, side: THREE.DoubleSide } );
  292. var r = "textures/cube/Bridge2/";
  293. var urls = [ r + "posx.jpg", r + "negx.jpg",
  294. r + "posy.jpg", r + "negy.jpg",
  295. r + "posz.jpg", r + "negz.jpg" ];
  296. var textureCube = new THREE.CubeTextureLoader().load( urls );
  297. textureCube.format = THREE.RGBFormat;
  298. textureCube.mapping = THREE.CubeReflectionMapping;
  299. var sphereMaterial = new THREE.MeshPhysicalMaterial( {
  300. transparent: true,
  301. depthWrite: false,
  302. opacity: 0.15,
  303. color: 0,
  304. metalness: 1,
  305. roughness: 0,
  306. reflectivity: 0,
  307. envMap: textureCube
  308. } );
  309. var sphereHeight = 300;
  310. var sphereRadius = 200;
  311. scene.userData.camera.position.set( 5 * sphereRadius, 2 * sphereHeight, 6 * sphereRadius );
  312. var sphereMesh = new THREE.Mesh( new THREE.SphereBufferGeometry( sphereRadius, 80, 40 ), sphereMaterial );
  313. sphereMesh.position.set( 0, sphereHeight, 0 );
  314. ballScene.add( sphereMesh );
  315. var sphere = new THREE.Sphere( sphereMesh.position, sphereRadius );
  316. var spherePlasma = new THREE.Mesh( new THREE.SphereBufferGeometry( sphereRadius * 0.05, 24, 12 ), scene.userData.lightningMaterial );
  317. spherePlasma.position.copy( sphereMesh.position );
  318. spherePlasma.scale.y = 0.6;
  319. scene.add( spherePlasma );
  320. var post = new THREE.Mesh(
  321. new THREE.CylinderBufferGeometry( sphereRadius * 0.06, sphereRadius * 0.06, sphereHeight, 6, 1, true ),
  322. new THREE.MeshLambertMaterial( { color: 0x020202 } )
  323. );
  324. post.position.y = sphereHeight * 0.5 - sphereRadius * 0.05 * 1.2;
  325. scene.add( post );
  326. var box = new THREE.Mesh( new THREE.BoxBufferGeometry( sphereHeight * 0.5, sphereHeight * 0.1, sphereHeight * 0.5 ), post.material );
  327. box.position.y = sphereHeight * 0.05 * 0.5;
  328. scene.add( box );
  329. var rayDirection = new THREE.Vector3();
  330. var rayLength = 0;
  331. var vec1 = new THREE.Vector3();
  332. var vec2 = new THREE.Vector3();
  333. scene.userData.rayParams = {
  334. sourceOffset: sphereMesh.position,
  335. destOffset: new THREE.Vector3( sphereRadius, 0, 0 ).add( sphereMesh.position ),
  336. radius0: 4,
  337. radius1: 4,
  338. radius0Factor: 0.82,
  339. minRadius: 2.5,
  340. maxIterations: 6,
  341. isEternal: true,
  342. timeScale: 0.6,
  343. propagationTimeFactor: 0.15,
  344. vanishingTimeFactor: 0.87,
  345. subrayPeriod: 0.8,
  346. ramification: 5,
  347. recursionProbability: 0.8,
  348. roughness: 0.85,
  349. straightness: 0.7,
  350. onSubrayCreation: function ( segment, parentSubray, childSubray, lightningStrike ) {
  351. lightningStrike.subrayConePosition( segment, parentSubray, childSubray, 0.6, 0.9, 0.7 );
  352. // Sphere projection
  353. vec1.subVectors( childSubray.pos1, lightningStrike.rayParameters.sourceOffset );
  354. vec2.set( 0, 0, 0 );
  355. if ( lightningStrike.randomGenerator.random() < 0.7 ) {
  356. vec2.copy( rayDirection ).multiplyScalar( rayLength * 1.0865 );
  357. }
  358. vec1.add( vec2 ).setLength( rayLength );
  359. childSubray.pos1.addVectors( vec1, lightningStrike.rayParameters.sourceOffset );
  360. }
  361. };
  362. var lightningStrike;
  363. var lightningStrikeMesh;
  364. var outlineMeshArray = [];
  365. scene.userData.recreateRay = function () {
  366. if ( lightningStrikeMesh ) {
  367. scene.remove( lightningStrikeMesh );
  368. }
  369. lightningStrike = new THREE.LightningStrike( scene.userData.rayParams );
  370. lightningStrikeMesh = new THREE.Mesh( lightningStrike, scene.userData.lightningMaterial );
  371. outlineMeshArray.length = 0;
  372. outlineMeshArray.push( lightningStrikeMesh );
  373. outlineMeshArray.push( spherePlasma );
  374. scene.add( lightningStrikeMesh );
  375. }
  376. scene.userData.recreateRay();
  377. // Compose rendering
  378. composer.passes = [];
  379. composer.addPass( new THREE.RenderPass( ballScene, scene.userData.camera ) );
  380. var rayPass = new THREE.RenderPass( scene, scene.userData.camera );
  381. rayPass.clear = false;
  382. composer.addPass( rayPass );
  383. var outlinePass = createOutline( scene, outlineMeshArray, scene.userData.outlineColor );
  384. scene.userData.render = function ( time ) {
  385. rayDirection.subVectors( lightningStrike.rayParameters.destOffset, lightningStrike.rayParameters.sourceOffset );
  386. rayLength = rayDirection.length();
  387. rayDirection.normalize();
  388. lightningStrike.update( time );
  389. controls.update();
  390. if ( scene.userData.outlineEnabled ) {
  391. outlinePass.enabled = true;
  392. rayPass.renderToScreen = false;
  393. }
  394. else {
  395. outlinePass.enabled = false;
  396. rayPass.renderToScreen = true;
  397. }
  398. composer.render();
  399. };
  400. // Controls
  401. var controls = new THREE.OrbitControls( scene.userData.camera, renderer.domElement );
  402. controls.target.copy( sphereMesh.position );
  403. controls.enableDamping = true;
  404. controls.dampingFactor = 0.25;
  405. // Sphere mouse raycasting
  406. window.addEventListener( 'mousemove', onTouchMove );
  407. window.addEventListener( 'touchmove', onTouchMove );
  408. function onTouchMove( event ) {
  409. var x, y;
  410. if ( event.changedTouches ) {
  411. x = event.changedTouches[ 0 ].pageX;
  412. y = event.changedTouches[ 0 ].pageY;
  413. } else {
  414. x = event.clientX;
  415. y = event.clientY;
  416. }
  417. mouse.x = ( x / window.innerWidth ) * 2 - 1;
  418. mouse.y = - ( y / window.innerHeight ) * 2 + 1;
  419. checkIntersection();
  420. }
  421. var intersection = new THREE.Vector3();
  422. function checkIntersection() {
  423. raycaster.setFromCamera( mouse, scene.userData.camera );
  424. var result = raycaster.ray.intersectSphere( sphere, intersection );
  425. if ( result !== null ) {
  426. lightningStrike.rayParameters.destOffset.copy( intersection );
  427. }
  428. }
  429. return scene;
  430. }
  431. //
  432. function createStormScene() {
  433. var scene = new THREE.Scene();
  434. scene.background = new THREE.Color( 0x050505 );
  435. scene.userData.canGoBackwardsInTime = false;
  436. scene.userData.camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 20, 10000 );
  437. // Lights
  438. scene.add( new THREE.AmbientLight( 0x444444 ) );
  439. var light1 = new THREE.DirectionalLight( 0xffffff, 0.5 );
  440. light1.position.set( 1, 1, 1 );
  441. scene.add( light1 );
  442. var posLight = new THREE.PointLight( 0x00ffff );
  443. posLight.position.set( 0, 100, 0 );
  444. scene.add( posLight );
  445. // Ground
  446. var GROUND_SIZE = 1000;
  447. scene.userData.camera.position.set( 0, 0.2, 1.6 ).multiplyScalar( GROUND_SIZE * 0.5 );
  448. var ground = new THREE.Mesh( new THREE.PlaneBufferGeometry( GROUND_SIZE, GROUND_SIZE ), new THREE.MeshLambertMaterial( { color: 0x072302 } ) );
  449. ground.rotation.x = - Math.PI * 0.5;
  450. scene.add( ground );
  451. // Storm
  452. scene.userData.lightningColor = new THREE.Color( 0xB0FFFF );
  453. scene.userData.outlineColor = new THREE.Color( 0x00FFFF );
  454. scene.userData.lightningMaterial = new THREE.MeshBasicMaterial( { color: scene.userData.lightningColor } );
  455. var rayDirection = new THREE.Vector3( 0, -1, 0 );
  456. var vec1 = new THREE.Vector3();
  457. var vec2 = new THREE.Vector3();
  458. scene.userData.rayParams = {
  459. radius0: 1,
  460. radius1: 0.5,
  461. minRadius: 0.3,
  462. maxIterations: 7,
  463. timeScale: 0.15,
  464. propagationTimeFactor: 0.2,
  465. vanishingTimeFactor: 0.9,
  466. subrayPeriod: 4,
  467. subrayDutyCycle: 0.6,
  468. maxSubrayRecursion: 3,
  469. ramification: 3,
  470. recursionProbability: 0.4,
  471. roughness: 0.85,
  472. straightness: 0.65,
  473. onSubrayCreation: function ( segment, parentSubray, childSubray, lightningStrike ) {
  474. lightningStrike.subrayConePosition( segment, parentSubray, childSubray, 0.6, 0.6, 0.5 );
  475. // Plane projection
  476. rayLength = lightningStrike.rayParameters.sourceOffset.y;
  477. vec1.subVectors( childSubray.pos1, lightningStrike.rayParameters.sourceOffset );
  478. var proj = rayDirection.dot( vec1 );
  479. vec2.copy( rayDirection ).multiplyScalar( proj );
  480. vec1.sub( vec2 );
  481. var scale = proj / rayLength > 0.5 ? rayLength / proj : 1;
  482. vec2.multiplyScalar( scale );
  483. vec1.add( vec2 );
  484. childSubray.pos1.addVectors( vec1, lightningStrike.rayParameters.sourceOffset );
  485. }
  486. };
  487. // Black star mark
  488. var starVertices = [];
  489. var prevPoint = new THREE.Vector3( 0, 0, 1 );
  490. var currPoint = new THREE.Vector3();
  491. for ( var i = 1; i <= 16; i++ ) {
  492. currPoint.set( Math.sin( 2 * Math.PI * i / 16 ), 0, Math.cos( 2 * Math.PI * i / 16 ) );
  493. if ( i % 2 === 1 ) {
  494. currPoint.multiplyScalar( 0.3 );
  495. }
  496. starVertices.push( 0, 0, 0 );
  497. starVertices.push( prevPoint.x, prevPoint.y, prevPoint.z );
  498. starVertices.push( currPoint.x, currPoint.y, currPoint.z );
  499. prevPoint.copy( currPoint );
  500. }
  501. var starGeometry = new THREE.BufferGeometry();
  502. starGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( starVertices, 3 ) );
  503. var starMesh = new THREE.Mesh( starGeometry, new THREE.MeshBasicMaterial( { color: 0x020900 } ) );
  504. starMesh.scale.multiplyScalar( 6 );
  505. //
  506. var storm = new THREE.LightningStorm( {
  507. size: GROUND_SIZE,
  508. minHeight: 90,
  509. maxHeight: 200,
  510. maxSlope: 0.6,
  511. maxLightnings: 8,
  512. lightningParameters: scene.userData.rayParams,
  513. lightningMaterial: scene.userData.lightningMaterial,
  514. onLightningDown: function ( lightning ) {
  515. // Add black star mark at ray strike
  516. var star1 = starMesh.clone();
  517. star1.position.copy( lightning.rayParameters.destOffset );
  518. star1.position.y = 0.05;
  519. star1.rotation.y = 2 * Math.PI * Math.random();
  520. scene.add( star1 );
  521. }
  522. } );
  523. scene.add( storm );
  524. // Compose rendering
  525. composer.passes = [];
  526. composer.addPass( new THREE.RenderPass( scene, scene.userData.camera ) );
  527. createOutline( scene, storm.lightningsMeshes, scene.userData.outlineColor );
  528. // Controls
  529. var controls = new THREE.OrbitControls( scene.userData.camera, renderer.domElement );
  530. controls.target.y = GROUND_SIZE * 0.05;
  531. controls.enableDamping = true;
  532. controls.dampingFactor = 0.25;
  533. scene.userData.render = function ( time ) {
  534. storm.update( time );
  535. controls.update();
  536. if ( scene.userData.outlineEnabled ) {
  537. composer.render();
  538. }
  539. else {
  540. renderer.render( scene, scene.userData.camera );
  541. }
  542. };
  543. return scene;
  544. }
  545. </script>
  546. </body>
  547. </html>