forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebgl_lensflares.html
189 lines (126 loc) · 4.81 KB
/
webgl_lensflares.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!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://www.ro.me" target="_blank" rel="noopener">ro.me</a><br/>
fly with WASD/RF/QE + mouse
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/libs/stats.module.js';
import { FlyControls } from 'three/addons/controls/FlyControls.js';
import { Lensflare, LensflareElement } from 'three/addons/objects/Lensflare.js';
let container, stats;
let camera, scene, renderer;
let controls;
const 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
const s = 250;
const geometry = new THREE.BoxGeometry( s, s, s );
const material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 50 } );
for ( let i = 0; i < 3000; i ++ ) {
const 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
const 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
const textureLoader = new THREE.TextureLoader();
const textureFlare0 = textureLoader.load( 'textures/lensflare/lensflare0.png' );
const 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 ) {
const light = new THREE.PointLight( 0xffffff, 1.5, 2000 );
light.color.setHSL( h, s, l );
light.position.set( x, y, z );
scene.add( light );
const 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 } );
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 );
}
//
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() {
const delta = clock.getDelta();
controls.update( delta );
renderer.render( scene, camera );
}
</script>
</body>
</html>