@@ -2,28 +2,18 @@ import {
2
2
ACESFilmicToneMapping ,
3
3
CustomBlending ,
4
4
Scene ,
5
- Box3 ,
6
- Mesh ,
7
- CylinderGeometry ,
8
- MeshPhysicalMaterial ,
9
5
WebGLRenderer ,
10
- EquirectangularReflectionMapping ,
11
- PerspectiveCamera ,
12
- Color ,
6
+ Vector3 ,
13
7
} from 'three' ;
14
8
import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js' ;
15
- import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js' ;
16
9
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' ;
17
10
import { DenoiseMaterial , WebGLPathTracer } from '../src/index.js' ;
18
- import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader.js' ;
19
- import { MeshoptDecoder } from 'three/examples/jsm/libs/meshopt_decoder.module.js' ;
20
11
import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js' ;
21
12
import { LoaderElement } from './utils/LoaderElement.js' ;
13
+ import { MaterialOrbSceneLoader } from './utils/MaterialOrbSceneLoader.js' ;
14
+ import { RectAreaLightUniformsLib } from 'three/examples/jsm/lights/RectAreaLightUniformsLib.js' ;
22
15
23
- const ENV_URL = 'https://raw.githubusercontent.com/gkjohnson/3d-demo-data/master/hdri/autoshop_01_1k.hdr' ;
24
- const MODEL_URL = 'https://raw.githubusercontent.com/gkjohnson/3d-demo-data/main/models/material-balls/material_ball_v2.glb' ;
25
-
26
- let pathTracer , renderer , controls , denoiseQuad , shellMaterial ;
16
+ let pathTracer , renderer , controls , denoiseQuad , material ;
27
17
let camera , scene , loader ;
28
18
29
19
const params = {
@@ -113,17 +103,21 @@ init();
113
103
114
104
async function init ( ) {
115
105
106
+ RectAreaLightUniformsLib . init ( ) ;
107
+
116
108
loader = new LoaderElement ( ) ;
117
109
loader . attach ( document . body ) ;
118
110
119
111
// renderer
120
112
renderer = new WebGLRenderer ( { antialias : true } ) ;
121
113
renderer . toneMapping = ACESFilmicToneMapping ;
114
+ renderer . toneMappingExposure = 0.02 ;
122
115
document . body . appendChild ( renderer . domElement ) ;
123
116
124
117
// path tracer
125
118
pathTracer = new WebGLPathTracer ( renderer ) ;
126
119
pathTracer . tiles . set ( params . tiles , params . tiles ) ;
120
+ pathTracer . textureSize . set ( 2048 , 2048 ) ;
127
121
pathTracer . renderToCanvasCallback = ( target , renderer , quad ) => {
128
122
129
123
denoiseQuad . material . sigma = params . denoiseSigma ;
@@ -147,76 +141,30 @@ async function init() {
147
141
premultipliedAlpha : renderer . getContextAttributes ( ) . premultipliedAlpha ,
148
142
} ) ) ;
149
143
150
- // camera
151
- const aspect = window . innerWidth / window . innerHeight ;
152
- camera = new PerspectiveCamera ( 75 , aspect , 0.025 , 500 ) ;
153
- camera . position . set ( - 4 , 2 , 3 ) ;
154
-
155
- // controls
156
- controls = new OrbitControls ( camera , renderer . domElement ) ;
157
- controls . addEventListener ( 'change' , ( ) => pathTracer . updateCamera ( ) ) ;
158
-
159
144
scene = new Scene ( ) ;
160
- scene . backgroundBlurriness = 0.05 ;
161
-
162
- // load assets
163
- const [ envTexture , gltf ] = await Promise . all ( [
164
- new RGBELoader ( ) . loadAsync ( ENV_URL ) ,
165
- new GLTFLoader ( ) . setMeshoptDecoder ( MeshoptDecoder ) . loadAsync ( MODEL_URL )
166
- ] ) ;
167
-
168
- // set environment
169
- envTexture . mapping = EquirectangularReflectionMapping ;
170
- scene . background = envTexture ;
171
- scene . environment = envTexture ;
172
-
173
- // set up scene
174
- gltf . scene . scale . setScalar ( 0.01 ) ;
175
- gltf . scene . updateMatrixWorld ( ) ;
176
- scene . add ( gltf . scene ) ;
177
-
178
- const box = new Box3 ( ) ;
179
- box . setFromObject ( gltf . scene ) ;
180
-
181
- const floor = new Mesh (
182
- new CylinderGeometry ( 3 , 3 , 0.05 , 200 ) ,
183
- new MeshPhysicalMaterial ( { color : 0xffffff , roughness : 0.1 , metalness : 0.9 } ) ,
184
- ) ;
185
- floor . geometry = floor . geometry . toNonIndexed ( ) ;
186
- floor . geometry . clearGroups ( ) ;
187
- floor . position . y = box . min . y - 0.03 ;
188
- scene . add ( floor ) ;
189
145
190
- const coreMaterial = new MeshPhysicalMaterial ( { color : new Color ( 0.5 , 0.5 , 0.5 ) } ) ;
191
- shellMaterial = new MeshPhysicalMaterial ( ) ;
146
+ window . SCENE = scene ;
192
147
193
- gltf . scene . traverse ( c => {
194
-
195
- // the vertex normals on the material ball are off...
196
- // TODO: precompute the vertex normals so they are correct on load
197
- if ( c . geometry ) {
198
-
199
- c . geometry . computeVertexNormals ( ) ;
200
-
201
- }
202
-
203
- if ( c . name === 'Sphere_1' ) {
204
-
205
- c . material = coreMaterial ;
206
-
207
- } else {
208
-
209
- c . material = shellMaterial ;
210
-
211
- }
148
+ // load assets
149
+ const orb = await new MaterialOrbSceneLoader ( ) . loadAsync ( ) ;
212
150
213
- if ( c . name === 'subsphere_1' ) {
151
+ // scene initialization
152
+ scene . add ( orb . scene ) ;
153
+ camera = orb . camera ;
154
+ material = orb . material ;
214
155
215
- c . material = coreMaterial ;
156
+ // move camera to the scene
157
+ scene . attach ( camera ) ;
158
+ camera . removeFromParent ( ) ;
216
159
217
- }
160
+ // controls
161
+ controls = new OrbitControls ( camera , renderer . domElement ) ;
162
+ controls . addEventListener ( 'change' , ( ) => pathTracer . updateCamera ( ) ) ;
218
163
219
- } ) ;
164
+ // shift target
165
+ const fwd = new Vector3 ( 0 , 0 , - 1 ) . transformDirection ( camera . matrixWorld ) . normalize ( ) ;
166
+ controls . target . copy ( camera . position ) . addScaledVector ( fwd , 25 ) ;
167
+ controls . update ( ) ;
220
168
221
169
loader . setPercentage ( 1 ) ;
222
170
onParamsChange ( ) ;
@@ -287,27 +235,27 @@ function onResize() {
287
235
function onParamsChange ( ) {
288
236
289
237
const materialProperties = params . materialProperties ;
290
- shellMaterial . color . set ( materialProperties . color ) ;
291
- shellMaterial . emissive . set ( materialProperties . emissive ) ;
292
- shellMaterial . emissiveIntensity = materialProperties . emissiveIntensity ;
293
- shellMaterial . metalness = materialProperties . metalness ;
294
- shellMaterial . roughness = materialProperties . roughness ;
295
- shellMaterial . transmission = materialProperties . transmission ;
296
- shellMaterial . attenuationDistance = materialProperties . thinFilm ? Infinity : materialProperties . attenuationDistance ;
297
- shellMaterial . attenuationColor . set ( materialProperties . attenuationColor ) ;
298
- shellMaterial . ior = materialProperties . ior ;
299
- shellMaterial . opacity = materialProperties . opacity ;
300
- shellMaterial . clearcoat = materialProperties . clearcoat ;
301
- shellMaterial . clearcoatRoughness = materialProperties . clearcoatRoughness ;
302
- shellMaterial . sheenColor . set ( materialProperties . sheenColor ) ;
303
- shellMaterial . sheenRoughness = materialProperties . sheenRoughness ;
304
- shellMaterial . iridescence = materialProperties . iridescence ;
305
- shellMaterial . iridescenceIOR = materialProperties . iridescenceIOR ;
306
- shellMaterial . iridescenceThicknessRange = [ 0 , materialProperties . iridescenceThickness ] ;
307
- shellMaterial . specularColor . set ( materialProperties . specularColor ) ;
308
- shellMaterial . specularIntensity = materialProperties . specularIntensity ;
309
- shellMaterial . transparent = shellMaterial . opacity < 1 ;
310
- shellMaterial . flatShading = materialProperties . flatShading ;
238
+ material . color . set ( materialProperties . color ) ;
239
+ material . emissive . set ( materialProperties . emissive ) ;
240
+ material . emissiveIntensity = materialProperties . emissiveIntensity ;
241
+ material . metalness = materialProperties . metalness ;
242
+ material . roughness = materialProperties . roughness ;
243
+ material . transmission = materialProperties . transmission ;
244
+ material . attenuationDistance = materialProperties . thinFilm ? Infinity : materialProperties . attenuationDistance ;
245
+ material . attenuationColor . set ( materialProperties . attenuationColor ) ;
246
+ material . ior = materialProperties . ior ;
247
+ material . opacity = materialProperties . opacity ;
248
+ material . clearcoat = materialProperties . clearcoat ;
249
+ material . clearcoatRoughness = materialProperties . clearcoatRoughness ;
250
+ material . sheenColor . set ( materialProperties . sheenColor ) ;
251
+ material . sheenRoughness = materialProperties . sheenRoughness ;
252
+ material . iridescence = materialProperties . iridescence ;
253
+ material . iridescenceIOR = materialProperties . iridescenceIOR ;
254
+ material . iridescenceThicknessRange = [ 0 , materialProperties . iridescenceThickness ] ;
255
+ material . specularColor . set ( materialProperties . specularColor ) ;
256
+ material . specularIntensity = materialProperties . specularIntensity ;
257
+ material . transparent = material . opacity < 1 ;
258
+ material . flatShading = materialProperties . flatShading ;
311
259
312
260
pathTracer . transmissiveBounces = params . transmissiveBounces ;
313
261
pathTracer . multipleImportanceSampling = params . multipleImportanceSampling ;
@@ -316,8 +264,8 @@ function onParamsChange() {
316
264
pathTracer . renderScale = params . renderScale ;
317
265
318
266
// note: custom properties
319
- shellMaterial . matte = materialProperties . matte ;
320
- shellMaterial . castShadow = materialProperties . castShadow ;
267
+ material . matte = materialProperties . matte ;
268
+ material . castShadow = materialProperties . castShadow ;
321
269
322
270
pathTracer . updateMaterials ( ) ;
323
271
pathTracer . setScene ( scene , camera ) ;
0 commit comments