webgl_lensflares.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lensflares</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="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - lensflares<br/>
  12. textures from <a href="http://ro.me">ro.me</a><br/>
  13. fly with WASD/RF/QE + mouse
  14. </div>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import Stats from './jsm/libs/stats.module.js';
  18. import { FlyControls } from './jsm/controls/FlyControls.js';
  19. import { Lensflare, LensflareElement } from './jsm/objects/Lensflare.js';
  20. var container, stats;
  21. var camera, scene, renderer;
  22. var controls;
  23. var clock = new THREE.Clock();
  24. init();
  25. animate();
  26. function init() {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. // camera
  30. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 15000 );
  31. camera.position.z = 250;
  32. // scene
  33. scene = new THREE.Scene();
  34. scene.background = new THREE.Color().setHSL( 0.51, 0.4, 0.01 );
  35. scene.fog = new THREE.Fog( scene.background, 3500, 15000 );
  36. // world
  37. var s = 250;
  38. var geometry = new THREE.BoxBufferGeometry( s, s, s );
  39. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 50 } );
  40. for ( var i = 0; i < 3000; i ++ ) {
  41. var mesh = new THREE.Mesh( geometry, material );
  42. mesh.position.x = 8000 * ( 2.0 * Math.random() - 1.0 );
  43. mesh.position.y = 8000 * ( 2.0 * Math.random() - 1.0 );
  44. mesh.position.z = 8000 * ( 2.0 * Math.random() - 1.0 );
  45. mesh.rotation.x = Math.random() * Math.PI;
  46. mesh.rotation.y = Math.random() * Math.PI;
  47. mesh.rotation.z = Math.random() * Math.PI;
  48. mesh.matrixAutoUpdate = false;
  49. mesh.updateMatrix();
  50. scene.add( mesh );
  51. }
  52. // lights
  53. var dirLight = new THREE.DirectionalLight( 0xffffff, 0.05 );
  54. dirLight.position.set( 0, - 1, 0 ).normalize();
  55. dirLight.color.setHSL( 0.1, 0.7, 0.5 );
  56. scene.add( dirLight );
  57. // lensflares
  58. var textureLoader = new THREE.TextureLoader();
  59. var textureFlare0 = textureLoader.load( 'textures/lensflare/lensflare0.png' );
  60. var textureFlare3 = textureLoader.load( 'textures/lensflare/lensflare3.png' );
  61. addLight( 0.55, 0.9, 0.5, 5000, 0, - 1000 );
  62. addLight( 0.08, 0.8, 0.5, 0, 0, - 1000 );
  63. addLight( 0.995, 0.5, 0.9, 5000, 5000, - 1000 );
  64. function addLight( h, s, l, x, y, z ) {
  65. var light = new THREE.PointLight( 0xffffff, 1.5, 2000 );
  66. light.color.setHSL( h, s, l );
  67. light.position.set( x, y, z );
  68. scene.add( light );
  69. var lensflare = new Lensflare();
  70. lensflare.addElement( new LensflareElement( textureFlare0, 700, 0, light.color ) );
  71. lensflare.addElement( new LensflareElement( textureFlare3, 60, 0.6 ) );
  72. lensflare.addElement( new LensflareElement( textureFlare3, 70, 0.7 ) );
  73. lensflare.addElement( new LensflareElement( textureFlare3, 120, 0.9 ) );
  74. lensflare.addElement( new LensflareElement( textureFlare3, 70, 1 ) );
  75. light.add( lensflare );
  76. }
  77. // renderer
  78. renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
  79. renderer.setPixelRatio( window.devicePixelRatio );
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. renderer.outputEncoding = THREE.sRGBEncoding;
  82. container.appendChild( renderer.domElement );
  83. //
  84. controls = new FlyControls( camera, renderer.domElement );
  85. controls.movementSpeed = 2500;
  86. controls.domElement = container;
  87. controls.rollSpeed = Math.PI / 6;
  88. controls.autoForward = false;
  89. controls.dragToLook = false;
  90. // stats
  91. stats = new Stats();
  92. container.appendChild( stats.dom );
  93. // events
  94. window.addEventListener( 'resize', onWindowResize, false );
  95. }
  96. //
  97. function onWindowResize() {
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. }
  102. //
  103. function animate() {
  104. requestAnimationFrame( animate );
  105. render();
  106. stats.update();
  107. }
  108. function render() {
  109. var delta = clock.getDelta();
  110. controls.update( delta );
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>