forked from gkjohnson/three-gpu-pathtracer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathareaLight.js
211 lines (162 loc) · 6.16 KB
/
areaLight.js
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
import {
ACESFilmicToneMapping,
Box3,
Color,
CylinderGeometry,
EquirectangularReflectionMapping,
Mesh,
MeshStandardMaterial,
PerspectiveCamera,
Scene,
WebGLRenderer,
} from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { ShapedAreaLight, WebGLPathTracer } from '../src/index.js';
import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader.js';
import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import { MeshoptDecoder } from 'three/examples/jsm/libs/meshopt_decoder.module.js';
import { ParallelMeshBVHWorker } from 'three-mesh-bvh/src/workers/ParallelMeshBVHWorker.js';
import { getScaledSettings } from './utils/getScaledSettings.js';
import { LoaderElement } from './utils/LoaderElement.js';
const ENV_URL = 'https://raw.githubusercontent.com/gkjohnson/3d-demo-data/master/hdri/leadenhall_market_1k.hdr';
const MODEL_URL = 'https://raw.githubusercontent.com/gkjohnson/3d-demo-data/main/models/mercury-about-to-kill-argos/scene.glb';
const CREDITS = 'Model courtesy of Virtual Museums of Małopolska';
let pathTracer, renderer, controls, areaLight, scene, camera;
let loader;
const params = {
// area light settings
enabled: true,
isCircular: false,
intensity: 2,
color: '#ffffff',
width: 1,
height: 1,
// path tracer settings
bounces: 5,
renderScale: 1 / window.devicePixelRatio,
filterGlossyFactor: 0.5,
tiles: 1,
multipleImportanceSampling: true,
...getScaledSettings(),
};
init();
async function init() {
loader = new LoaderElement();
loader.attach( document.body );
// renderer
renderer = new WebGLRenderer( { antialias: true } );
renderer.toneMapping = ACESFilmicToneMapping;
document.body.appendChild( renderer.domElement );
// path tracer
pathTracer = new WebGLPathTracer( renderer );
pathTracer.tiles.set( params.tiles, params.tiles );
pathTracer.setBVHWorker( new ParallelMeshBVHWorker() );
// camera
camera = new PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.025, 500 );
camera.position.set( 0.0, 0.6, 2.65 );
// controls
controls = new OrbitControls( camera, renderer.domElement );
controls.target.set( 0, 0.33, - 0.08 );
controls.addEventListener( 'change', () => pathTracer.updateCamera() );
controls.update();
// init scene
scene = new Scene();
scene.environmentIntensity = 0.03;
scene.backgroundIntensity = 0.03;
// load the assets
const [ envTexture, gltf ] = await Promise.all( [
new RGBELoader().loadAsync( ENV_URL ),
new GLTFLoader().setMeshoptDecoder( MeshoptDecoder ).loadAsync( MODEL_URL ),
] );
// update the env map
envTexture.mapping = EquirectangularReflectionMapping;
scene.background = envTexture;
scene.environment = envTexture;
// position the model
const box = new Box3();
gltf.scene.traverse( c => {
if ( c.material ) c.material.map = null;
} );
gltf.scene.scale.setScalar( 0.01 );
gltf.scene.position.x = 0.05;
gltf.scene.updateMatrixWorld( true );
box.setFromObject( gltf.scene );
gltf.scene.position.y -= box.min.y;
scene.add( gltf.scene );
// set the floor
const floorGeom = new CylinderGeometry( 3.5, 3.5, 0.05, 60 );
const floorMat = new MeshStandardMaterial( { color: new Color( 0x999999 ), metalness: 0.2, roughness: 0.02 } );
const floor = new Mesh( floorGeom, floorMat );
floor.position.y = - 0.025;
scene.add( floor );
// initialize lights
areaLight = new ShapedAreaLight( new Color( 0xffffff ), 5.0, 1.0, 1.0 );
areaLight.position.x = 1.5;
areaLight.position.y = 1.0;
areaLight.position.z = - 0.5;
areaLight.rotateZ( - Math.PI / 4 );
areaLight.rotateX( - Math.PI / 2 );
areaLight.isCircular = false;
scene.add( areaLight );
const redLight = new ShapedAreaLight( new Color( 0xff0000 ), 15.0, 1.25, 2.75 );
redLight.position.y = 1.25;
redLight.position.z = - 1.5;
redLight.rotateX( Math.PI );
redLight.isCircular = false;
scene.add( redLight );
// initialize scene
await pathTracer.setSceneAsync( scene, camera, {
onProgress: v => {
loader.setPercentage( v );
}
} );
loader.setCredits( CREDITS );
// gui
const gui = new GUI();
const ptFolder = gui.addFolder( 'Path Tracer' );
ptFolder.add( params, 'tiles', 1, 4, 1 ).onChange( value => {
pathTracer.tiles.set( value, value );
} );
ptFolder.add( params, 'filterGlossyFactor', 0, 1 ).onChange( onParamsChange );
ptFolder.add( params, 'bounces', 1, 15, 1 ).onChange( onParamsChange );
ptFolder.add( params, 'renderScale', 0.1, 1 ).onChange( onParamsChange );
ptFolder.add( params, 'multipleImportanceSampling' ).onChange( onParamsChange );
ptFolder.close();
const areaLightFolder = gui.addFolder( 'Area Light' );
areaLightFolder.add( params, 'enabled' ).name( 'enable' ).onChange( onParamsChange );
areaLightFolder.add( params, 'isCircular' ).name( 'isCircular' ).onChange( onParamsChange );
areaLightFolder.add( params, 'intensity', 0, 200 ).name( 'intensity' ).onChange( onParamsChange );
areaLightFolder.addColor( params, 'color' ).name( 'color' ).onChange( onParamsChange );
areaLightFolder.add( params, 'width', 0, 5 ).name( 'width' ).onChange( onParamsChange );
areaLightFolder.add( params, 'height', 0, 5 ).name( 'height' ).onChange( onParamsChange );
onParamsChange();
onResize();
window.addEventListener( 'resize', onResize );
animate();
}
function onParamsChange() {
areaLight.isCircular = params.isCircular;
areaLight.intensity = params.intensity;
areaLight.width = params.width;
areaLight.height = params.height;
areaLight.color.set( params.color ).convertSRGBToLinear();
areaLight.visible = params.enabled;
pathTracer.filterGlossyFactor = params.filterGlossyFactor;
pathTracer.bounces = params.bounces;
pathTracer.renderScale = params.renderScale;
pathTracer.multipleImportanceSampling = params.multipleImportanceSampling;
pathTracer.updateLights();
}
function onResize() {
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setPixelRatio( window.devicePixelRatio );
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
pathTracer.updateCamera();
}
function animate() {
requestAnimationFrame( animate );
pathTracer.renderSample();
loader.setSamples( pathTracer.samples, pathTracer.isCompiling );
}