forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebgl_shadowmap_progressive.html
271 lines (203 loc) · 8.23 KB
/
webgl_shadowmap_progressive.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - progressive lightmap accumulation</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="container"></div>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Progressive Lightmaps by <a href="https://github.com/zalo" target="_blank" rel="noopener">zalo</a><br/>
[Inspired by <a href="http://madebyevan.com/shaders/lightmap/" target="_blank" rel="noopener">evanw's Lightmap Generation</a>]
</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 { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { TransformControls } from 'three/addons/controls/TransformControls.js';
import { ProgressiveLightMap } from 'three/addons/misc/ProgressiveLightMap.js';
// ShadowMap + LightMap Res and Number of Directional Lights
const shadowMapRes = 512, lightMapRes = 1024, lightCount = 8;
let camera, scene, renderer, controls, control, control2,
object = new THREE.Mesh(), lightOrigin = null, progressiveSurfacemap;
const dirLights = [], lightmapObjects = [];
const params = { 'Enable': true, 'Blur Edges': true, 'Blend Window': 200,
'Light Radius': 50, 'Ambient Weight': 0.5, 'Debug Lightmap': false };
init();
createGUI();
animate();
function init() {
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
document.body.appendChild( renderer.domElement );
// camera
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 0, 100, 200 );
camera.name = 'Camera';
// scene
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x949494 );
scene.fog = new THREE.Fog( 0x949494, 1000, 3000 );
// progressive lightmap
progressiveSurfacemap = new ProgressiveLightMap( renderer, lightMapRes );
// directional lighting "origin"
lightOrigin = new THREE.Group();
lightOrigin.position.set( 60, 150, 100 );
scene.add( lightOrigin );
// transform gizmo
control = new TransformControls( camera, renderer.domElement );
control.addEventListener( 'dragging-changed', ( event ) => {
controls.enabled = ! event.value;
} );
control.attach( lightOrigin );
scene.add( control );
// create 8 directional lights to speed up the convergence
for ( let l = 0; l < lightCount; l ++ ) {
const dirLight = new THREE.DirectionalLight( 0xffffff, 1.0 / lightCount );
dirLight.name = 'Dir. Light ' + l;
dirLight.position.set( 200, 200, 200 );
dirLight.castShadow = true;
dirLight.shadow.camera.near = 100;
dirLight.shadow.camera.far = 5000;
dirLight.shadow.camera.right = 150;
dirLight.shadow.camera.left = - 150;
dirLight.shadow.camera.top = 150;
dirLight.shadow.camera.bottom = - 150;
dirLight.shadow.mapSize.width = shadowMapRes;
dirLight.shadow.mapSize.height = shadowMapRes;
lightmapObjects.push( dirLight );
dirLights.push( dirLight );
}
// ground
const groundMesh = new THREE.Mesh(
new THREE.PlaneGeometry( 600, 600 ),
new THREE.MeshPhongMaterial( { color: 0xffffff, depthWrite: true } )
);
groundMesh.position.y = - 0.1;
groundMesh.rotation.x = - Math.PI / 2;
groundMesh.name = 'Ground Mesh';
lightmapObjects.push( groundMesh );
scene.add( groundMesh );
// model
function loadModel() {
object.traverse( function ( child ) {
if ( child.isMesh ) {
child.name = 'Loaded Mesh';
child.castShadow = true;
child.receiveShadow = true;
child.material = new THREE.MeshPhongMaterial();
// This adds the model to the lightmap
lightmapObjects.push( child );
progressiveSurfacemap.addObjectsToLightMap( lightmapObjects );
} else {
child.layers.disableAll(); // Disable Rendering for this
}
} );
scene.add( object );
object.scale.set( 2, 2, 2 );
object.position.set( 0, - 16, 0 );
control2 = new TransformControls( camera, renderer.domElement );
control2.addEventListener( 'dragging-changed', ( event ) => {
controls.enabled = ! event.value;
} );
control2.attach( object );
scene.add( control2 );
const lightTarget = new THREE.Group();
lightTarget.position.set( 0, 20, 0 );
for ( let l = 0; l < dirLights.length; l ++ ) {
dirLights[ l ].target = lightTarget;
}
object.add( lightTarget );
if ( typeof TESTING !== 'undefined' ) {
for ( let i = 0; i < 300; i ++ ) {
render();
}
}
}
const manager = new THREE.LoadingManager( loadModel );
const loader = new GLTFLoader( manager );
loader.load( 'models/gltf/ShadowmappableMesh.glb', function ( obj ) {
object = obj.scene.children[ 0 ];
} );
// controls
controls = new OrbitControls( camera, renderer.domElement );
controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
controls.dampingFactor = 0.05;
controls.screenSpacePanning = true;
controls.minDistance = 100;
controls.maxDistance = 500;
controls.maxPolarAngle = Math.PI / 1.5;
controls.target.set( 0, 100, 0 );
window.addEventListener( 'resize', onWindowResize );
}
function createGUI() {
const gui = new GUI( { name: 'Accumulation Settings' } );
gui.add( params, 'Enable' );
gui.add( params, 'Blur Edges' );
gui.add( params, 'Blend Window', 1, 500 ).step( 1 );
gui.add( params, 'Light Radius', 0, 200 ).step( 10 );
gui.add( params, 'Ambient Weight', 0, 1 ).step( 0.1 );
gui.add( params, 'Debug Lightmap' );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function render() {
// Update the inertia on the orbit controls
controls.update();
// Accumulate Surface Maps
if ( params[ 'Enable' ] ) {
progressiveSurfacemap.update( camera, params[ 'Blend Window' ], params[ 'Blur Edges' ] );
if ( ! progressiveSurfacemap.firstUpdate ) {
progressiveSurfacemap.showDebugLightmap( params[ 'Debug Lightmap' ] );
}
}
// Manually Update the Directional Lights
for ( let l = 0; l < dirLights.length; l ++ ) {
// Sometimes they will be sampled from the target direction
// Sometimes they will be uniformly sampled from the upper hemisphere
if ( Math.random() > params[ 'Ambient Weight' ] ) {
dirLights[ l ].position.set(
lightOrigin.position.x + ( Math.random() * params[ 'Light Radius' ] ),
lightOrigin.position.y + ( Math.random() * params[ 'Light Radius' ] ),
lightOrigin.position.z + ( Math.random() * params[ 'Light Radius' ] ) );
} else {
// Uniform Hemispherical Surface Distribution for Ambient Occlusion
const lambda = Math.acos( 2 * Math.random() - 1 ) - ( 3.14159 / 2.0 );
const phi = 2 * 3.14159 * Math.random();
dirLights[ l ].position.set(
( ( Math.cos( lambda ) * Math.cos( phi ) ) * 300 ) + object.position.x,
Math.abs( ( Math.cos( lambda ) * Math.sin( phi ) ) * 300 ) + object.position.y + 20,
( Math.sin( lambda ) * 300 ) + object.position.z
);
}
}
// Render Scene
renderer.render( scene, camera );
}
function animate() {
requestAnimationFrame( animate );
render();
}
</script>
</body>
</html>