@@ -2,28 +2,18 @@ import {
22 ACESFilmicToneMapping ,
33 CustomBlending ,
44 Scene ,
5- Box3 ,
6- Mesh ,
7- CylinderGeometry ,
8- MeshPhysicalMaterial ,
95 WebGLRenderer ,
10- EquirectangularReflectionMapping ,
11- PerspectiveCamera ,
12- Color ,
6+ Vector3 ,
137} from 'three' ;
148import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js' ;
15- import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js' ;
169import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' ;
1710import { 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' ;
2011import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js' ;
2112import { LoaderElement } from './utils/LoaderElement.js' ;
13+ import { MaterialOrbSceneLoader } from './utils/MaterialOrbSceneLoader.js' ;
14+ import { RectAreaLightUniformsLib } from 'three/examples/jsm/lights/RectAreaLightUniformsLib.js' ;
2215
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 ;
2717let camera , scene , loader ;
2818
2919const params = {
@@ -113,17 +103,21 @@ init();
113103
114104async function init ( ) {
115105
106+ RectAreaLightUniformsLib . init ( ) ;
107+
116108 loader = new LoaderElement ( ) ;
117109 loader . attach ( document . body ) ;
118110
119111 // renderer
120112 renderer = new WebGLRenderer ( { antialias : true } ) ;
121113 renderer . toneMapping = ACESFilmicToneMapping ;
114+ renderer . toneMappingExposure = 0.02 ;
122115 document . body . appendChild ( renderer . domElement ) ;
123116
124117 // path tracer
125118 pathTracer = new WebGLPathTracer ( renderer ) ;
126119 pathTracer . tiles . set ( params . tiles , params . tiles ) ;
120+ pathTracer . textureSize . set ( 2048 , 2048 ) ;
127121 pathTracer . renderToCanvasCallback = ( target , renderer , quad ) => {
128122
129123 denoiseQuad . material . sigma = params . denoiseSigma ;
@@ -147,76 +141,30 @@ async function init() {
147141 premultipliedAlpha : renderer . getContextAttributes ( ) . premultipliedAlpha ,
148142 } ) ) ;
149143
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-
159144 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 ) ;
189145
190- const coreMaterial = new MeshPhysicalMaterial ( { color : new Color ( 0.5 , 0.5 , 0.5 ) } ) ;
191- shellMaterial = new MeshPhysicalMaterial ( ) ;
146+ window . SCENE = scene ;
192147
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 ( ) ;
212150
213- if ( c . name === 'subsphere_1' ) {
151+ // scene initialization
152+ scene . add ( orb . scene ) ;
153+ camera = orb . camera ;
154+ material = orb . material ;
214155
215- c . material = coreMaterial ;
156+ // move camera to the scene
157+ scene . attach ( camera ) ;
158+ camera . removeFromParent ( ) ;
216159
217- }
160+ // controls
161+ controls = new OrbitControls ( camera , renderer . domElement ) ;
162+ controls . addEventListener ( 'change' , ( ) => pathTracer . updateCamera ( ) ) ;
218163
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 ( ) ;
220168
221169 loader . setPercentage ( 1 ) ;
222170 onParamsChange ( ) ;
@@ -287,27 +235,27 @@ function onResize() {
287235function onParamsChange ( ) {
288236
289237 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 ;
311259
312260 pathTracer . transmissiveBounces = params . transmissiveBounces ;
313261 pathTracer . multipleImportanceSampling = params . multipleImportanceSampling ;
@@ -316,8 +264,8 @@ function onParamsChange() {
316264 pathTracer . renderScale = params . renderScale ;
317265
318266 // note: custom properties
319- shellMaterial . matte = materialProperties . matte ;
320- shellMaterial . castShadow = materialProperties . castShadow ;
267+ material . matte = materialProperties . matte ;
268+ material . castShadow = materialProperties . castShadow ;
321269
322270 pathTracer . updateMaterials ( ) ;
323271 pathTracer . setScene ( scene , camera ) ;
0 commit comments