webgl_lensflares.html 4.9 KB

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