123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - lensflares</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - lensflares<br/>
- textures from <a href="http://ro.me">ro.me</a><br/>
- fly with WASD/RF/QE + mouse
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { FlyControls } from './jsm/controls/FlyControls.js';
- import { Lensflare, LensflareElement } from './jsm/objects/Lensflare.js';
- var container, stats;
- var camera, scene, renderer;
- var controls;
- var clock = new THREE.Clock();
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- // camera
- camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 15000 );
- camera.position.z = 250;
- // scene
- scene = new THREE.Scene();
- scene.background = new THREE.Color().setHSL( 0.51, 0.4, 0.01 );
- scene.fog = new THREE.Fog( scene.background, 3500, 15000 );
- // world
- var s = 250;
- var geometry = new THREE.BoxBufferGeometry( s, s, s );
- var material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 50 } );
- for ( var i = 0; i < 3000; i ++ ) {
- var mesh = new THREE.Mesh( geometry, material );
- mesh.position.x = 8000 * ( 2.0 * Math.random() - 1.0 );
- mesh.position.y = 8000 * ( 2.0 * Math.random() - 1.0 );
- mesh.position.z = 8000 * ( 2.0 * Math.random() - 1.0 );
- mesh.rotation.x = Math.random() * Math.PI;
- mesh.rotation.y = Math.random() * Math.PI;
- mesh.rotation.z = Math.random() * Math.PI;
- mesh.matrixAutoUpdate = false;
- mesh.updateMatrix();
- scene.add( mesh );
- }
- // lights
- var dirLight = new THREE.DirectionalLight( 0xffffff, 0.05 );
- dirLight.position.set( 0, - 1, 0 ).normalize();
- dirLight.color.setHSL( 0.1, 0.7, 0.5 );
- scene.add( dirLight );
- // lensflares
- var textureLoader = new THREE.TextureLoader();
- var textureFlare0 = textureLoader.load( 'textures/lensflare/lensflare0.png' );
- var textureFlare3 = textureLoader.load( 'textures/lensflare/lensflare3.png' );
- addLight( 0.55, 0.9, 0.5, 5000, 0, - 1000 );
- addLight( 0.08, 0.8, 0.5, 0, 0, - 1000 );
- addLight( 0.995, 0.5, 0.9, 5000, 5000, - 1000 );
- function addLight( h, s, l, x, y, z ) {
- var light = new THREE.PointLight( 0xffffff, 1.5, 2000 );
- light.color.setHSL( h, s, l );
- light.position.set( x, y, z );
- scene.add( light );
- var lensflare = new Lensflare();
- lensflare.addElement( new LensflareElement( textureFlare0, 700, 0, light.color ) );
- lensflare.addElement( new LensflareElement( textureFlare3, 60, 0.6 ) );
- lensflare.addElement( new LensflareElement( textureFlare3, 70, 0.7 ) );
- lensflare.addElement( new LensflareElement( textureFlare3, 120, 0.9 ) );
- lensflare.addElement( new LensflareElement( textureFlare3, 70, 1 ) );
- light.add( lensflare );
- }
- // renderer
- renderer = new THREE.WebGLRenderer( { antialias: true, alpha: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.outputEncoding = THREE.sRGBEncoding;
- container.appendChild( renderer.domElement );
- //
- controls = new FlyControls( camera, renderer.domElement );
- controls.movementSpeed = 2500;
- controls.domElement = container;
- controls.rollSpeed = Math.PI / 6;
- controls.autoForward = false;
- controls.dragToLook = false;
- // stats
- stats = new Stats();
- container.appendChild( stats.dom );
- // events
- window.addEventListener( 'resize', onWindowResize, false );
- }
- //
- function onWindowResize() {
- renderer.setSize( window.innerWidth, window.innerHeight );
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- var delta = clock.getDelta();
- controls.update( delta );
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|