forked from portsmouth/OpenPBR-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
1193 lines (1020 loc) · 54.5 KB
/
main.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Vector2, Vector3, Matrix4, Box3,
Mesh, MeshBasicMaterial, ShaderMaterial, Scene, PerspectiveCamera, OrthographicCamera,
DirectionalLight, AmbientLight,
sRGBEncoding, RGBAFormat, FloatType,
WebGLRenderer, WebGLRenderTarget, TextureLoader } from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { FullScreenQuad } from 'three/addons/postprocessing/Pass.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { mergeGeometries } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
import Stats from 'stats.js';
import {
MeshBVH, MeshBVHUniformStruct, FloatVertexAttributeTexture,
shaderStructs, shaderIntersectFunction, SAH, StaticGeometryGenerator
} from 'three-mesh-bvh';
import { GUI } from 'lil-gui';
import glsl_main from './glsl/main.glsl?raw'
import glsl_fuzz_brdf from './glsl/fuzz_brdf.glsl?raw'
import glsl_coat_brdf from './glsl/coat_brdf.glsl?raw'
import glsl_thin_film from './glsl/thin-film.glsl?raw'
import glsl_metal_brdf from './glsl/metal_brdf.glsl?raw'
import glsl_specular_brdf from './glsl/specular_brdf.glsl?raw'
import glsl_specular_btdf from './glsl/specular_btdf.glsl?raw'
import glsl_diffuse_brdf from './glsl/diffuse_brdf.glsl?raw'
import glsl_openpbr_surface from './glsl/openpbr_surface.glsl?raw'
import glsl_pathtracer from './glsl/pathtracer.glsl?raw'
import { Circle } from 'progressbar.js'
class MeshLoader
{
constructor()
{
this.result = null;
this.loader = new GLTFLoader();
}
reset()
{
this.result = null;
}
async load(path)
{
if (this.result) Promise.resolve(this.result);
let gltf = await this.loader.loadAsync(path);
let S = Array.isArray( gltf.scene ) ? gltf.scene : [ gltf.scene ];
const meshes = [];
for ( let i = 0, l = S.length; i < l; i++ )
{
S[i].traverseVisible( c =>
{
if (c.isMesh)
{
meshes.push(c);
}
}
)
}
if (meshes.length > 0)
{
const generator = new StaticGeometryGenerator(meshes);
generator.attributes = [ 'position', 'color', 'normal', 'tangent', 'uv', 'uv2' ];
generator.applyWorldTransforms = false;
let merged_mesh = new Mesh(generator.generate(), new MeshBasicMaterial());
let bvh = new MeshBVH( merged_mesh.geometry, { strategy: SAH, maxLeafTris: 1 } );
this.result = {scene:gltf.scene, bvh:bvh, mesh:merged_mesh};
console.log("==> loaded mesh ", path);
}
return this.result;
}
}
function array_to_vector3(array)
{
return new Vector3(array[0], array[1], array[2]);
}
const params =
{
//////////////////////////////////////////////////////
// renderer params
//////////////////////////////////////////////////////
scene_name: 'standard-shader-ball',
smooth_normals: true,
bounces: 6,
max_samples: 1024,
max_volume_steps: 8,
wireframe: false,
neutral_color: [0.5, 0.5, 0.5],
//////////////////////////////////////////////////////
// lighting params
//////////////////////////////////////////////////////
skyPower: 0.5,
skyColor: [0.8, 0.8, 1.0],
sunPower: 0.35,
sunAngularSize: 40.0,
sunLatitude: 40.0,
sunLongitude: 180.0,
sunColor: [1.0, 1.0, 0.8],
//////////////////////////////////////////////////////
// OpenPBR surface params
//////////////////////////////////////////////////////
base_weight: 1.0,
base_color: [0.8, 0.8, 0.8],
base_roughness: 0.0,
base_metalness: 0.0,
diffuse_mode: 8,
specular_weight: 1.0,
specular_color: [1.0, 1.0, 1.0],
specular_roughness: 0.1,
specular_anisotropy: 0.0,
specular_rotation: 0.0,
specular_ior: 1.6,
transmission_weight: 0.0,
transmission_color: [1.0, 1.0, 1.0],
transmission_depth: 0.0,
transmission_scatter: [0.0, 0.0, 0.0],
transmission_scatter_anisotropy: 0.0,
transmission_dispersion_abbe_number: 20.0,
transmission_dispersion_scale: 0.0,
subsurface_weight: 0.0,
subsurface_color: [0.8, 0.8, 0.8],
subsurface_radius: 0.2,
subsurface_radius_scale: [1.0, 0.5, 0.25],
subsurface_anisotropy: 0.0,
subsurface_mode: 0, // FOR TESTING
coat_weight: 0.0,
coat_color: [1.0, 1.0, 1.0],
coat_roughness: 0.0,
coat_anisotropy: 0.0,
coat_rotation: 0.0,
coat_ior: 1.3,
coat_darkening: 1.0,
fuzz_weight: 0.0,
fuzz_color: [1.0, 1.0, 1.0],
fuzz_roughness: 0.5,
emission_luminance: 0.0,
emission_color: [1.0, 1.0, 1.0],
thin_film_weight: 0.0,
thin_film_thickness: 1000.0,
thin_film_ior: 1.4,
geometry_opacity: 1.0,
geometry_thin_walled: false,
reset_camera: function() { reset_camera(params.scene_name); }
};
let renderer, camera, orbitControls, scene, gui, stats;
let rtQuad, rtMaterial, finalQuad, renderTarget;
let samples = 0;
let MESH_SURFACE;
let MESH_PROPS;
let BVH_SURFACE;
let BVH_PROPS;
var progress_bar;
var progress_finished_timer;
var LOADED;
var COMPILING;
var scene_names = {
'Standard Shader Ball': 'standard-shader-ball',
'Glavenus': 'glavenus',
'Terrain': 'terrain',
'Bearded Man': 'bearded-man'
}
var subsurface_mode_names = {
'OpenPBR (orig, 3-float)': 0,
'OpenPBR (luminace)': 1,
'OpenPBR (average)': 2,
'OpenPBR (max value)': 3,
'OpenPBR (weighted average)': 4,
'SPI / Arnold v1': 5,
'Arnold v2': 6,
'Uniform scattering': 7
}
var diffuse_mode_names = {
'Lambert': 0,
'ON Full (Mitsuba)': 1,
'ON Qualitative (QON)': 2,
'ON Qualitative - Energy Conserving (EQON exact)': 3,
'ON Qualitative - Energy Conserving (EQON approx)': 4,
'Fujii - Qualitative (FON)': 5,
'Fujii - Energy Conserving (EFON exact)': 6,
'Fujii - Energy Conserving (EFON approx)': 7,
'Fujii - Energy Conserving (EFON exact, LTC sampling)': 8,
'Fujii - Energy Conserving (EFON approx, LTC sampling)': 9,
'Fujii - MaterialX': 10,
'Chan Diffuse (Unreal)': 11,
'd\'Eon sphere model': 12
}
init();
render();
function updateSunDir()
{
let latTheta = (90.0-params.sunLatitude) * Math.PI/180.0;
let lonPhi = params.sunLongitude * Math.PI/180.0;
let costheta = Math.cos(latTheta);
let sintheta = Math.sin(latTheta);
let cosphi = Math.cos(lonPhi);
let sinphi = Math.sin(lonPhi);
let x = sintheta * cosphi;
let z = sintheta * sinphi;
let y = costheta;
params.sunDir = [x, y, z];
}
function init_three_scene()
{
// dummy scene setup
scene = new Scene();
// dummy light for preview while shaders compiling
const light = new DirectionalLight( 0xffffff, 1 );
light.position.set( 1, 1, 1 );
scene.add( light );
scene.add( new AmbientLight( 0xb0bec5, 0.5 ) );
return scene;
}
function init()
{
// Setup progress bar spinner
progress_bar = new Circle('#progress_overlay',
{
color: 'rgba(255, 128, 64, 0.75)',
strokeWidth: 5.0,
trailColor: 'rgba(255, 128, 64, 0.333)',
trailWidth: 3.0,
svgStyle: {
display: 'block',
width: '100%'
},
text: {
value: '',
className: 'progressbar__label',
style: {
color: 'rgba(169, 85, 42, 1.0)',
position: 'absolute',
fontWeight: 'bold',
left: '50%',
top: '50%',
padding: 0,
margin: 0,
transform: {
prefix: true,
value: 'translate(-50%, -50%)'
}
},
autoStyleContainer: true,
alignToBottom: true
},
fill: null,
duration: 2000.0,
easing: 'linear',
from: { color: 'rgba( 0, 0, 0, 0.0)' },
to: { color: 'rgba(32, 255, 32, 1.0)' },
warnings: true
});
progress_bar.set(0.0);
progress_bar.setText('');
LOADED = false;
MESH_SURFACE = null;
MESH_PROPS = null;
BVH_SURFACE = null;
BVH_PROPS = null;
// renderer setup
renderer = new WebGLRenderer( { antialias: true, preserveDrawingBuffer: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setClearColor( 0x09141a );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.outputEncoding = sRGBEncoding;
document.body.appendChild( renderer.domElement );
// dummy three.js scene
scene = init_three_scene();
// stats setup
stats = new Stats();
document.body.appendChild( stats.dom );
// Samples count text
let samples_txt = document.getElementById('samples');
samples_txt.style.visibility = 'visible';
// Info text
let info_txt = document.getElementById('info');
info_txt.style.visibility = 'visible';
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// OpenPBR surface params
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
rtMaterial = new ShaderMaterial( {
defines:
{
BOUNCES: params.bounces,
MAX_VOLUME_STEPS: params.max_volume_steps,
COAT_ENABLED: false,
TRANSMISSION_ENABLED: false,
VOLUME_ENABLED: false,
DISPERSION_ENABLED: false,
THIN_FILM_ENABLED: false,
},
uniforms:
{
bvh_surface: { value: new MeshBVHUniformStruct() },
normalAttribute_surface: { value: new FloatVertexAttributeTexture() },
tangentAttribute_surface:{ value: new FloatVertexAttributeTexture() },
has_normals_surface: { value: 1 },
has_tangents_surface: { value: 0 },
bvh_props: { value: new MeshBVHUniformStruct() },
normalAttribute_props: { value: new FloatVertexAttributeTexture() },
tangentAttribute_props:{ value: new FloatVertexAttributeTexture() },
has_normals_props: { value: 1 },
has_tangents_props: { value: 0 },
cameraWorldMatrix: { value: new Matrix4() },
invProjectionMatrix: { value: new Matrix4() },
invModelMatrix: { value: new Matrix4() },
resolution: { value: new Vector2() },
samples: { value: 0 },
accumulation_weight: { value: 1 },
//////////////////////////////////////////////////////
// renderer
//////////////////////////////////////////////////////
wireframe: { value: params.wireframe, },
neutral_color: { value: new Vector3().fromArray(params.neutral_color) },
smooth_normals: { value: params.smooth_normals, },
//////////////////////////////////////////////////////
// lighting
//////////////////////////////////////////////////////
skyPower: { value: params.skyPower, },
skyColor: { value: array_to_vector3(params.skyColor) },
sunPower: { value: Math.pow(10.0,params.sunPower), },
sunAngularSize: { value: params.sunAngularSize, },
sunColor: { value: array_to_vector3(params.sunColor) },
sunDir: { value: array_to_vector3([0,0,0]) },
//////////////////////////////////////////////////////
// material
//////////////////////////////////////////////////////
diffuse_mode: { value: params.diffuse_mode },
base_weight: { value: params.base_weight },
base_color: { value: array_to_vector3(params.base_color) },
base_roughness: { value: params.base_roughness },
base_metalness: { value: params.base_metalness },
specular_weight: { value: params.specular_weight, },
specular_color: { value: array_to_vector3(params.specular_color) },
specular_roughness: { value: params.specular_roughness },
specular_anisotropy: { value: params.specular_anisotropy },
specular_rotation: { value: params.specular_rotation },
specular_ior: { value: params.specular_ior },
transmission_weight: { value: params.transmission_weight, },
transmission_color: { value: array_to_vector3(params.transmission_color) },
transmission_depth: { value: params.transmission_depth },
transmission_scatter: { value: array_to_vector3(params.transmission_scatter) },
transmission_scatter_anisotropy: { value: params.transmission_scatter_anisotropy },
transmission_dispersion_abbe_number: { value: params.transmission_dispersion_abbe_number },
transmission_dispersion_scale: { value: params.transmission_dispersion_scale },
subsurface_weight: { value: params.subsurface_weight },
subsurface_color: { value: array_to_vector3(params.subsurface_color) },
subsurface_radius: { value: params.subsurface_radius },
subsurface_radius_scale: { value: array_to_vector3(params.subsurface_radius_scale) },
subsurface_anisotropy: { value: params.subsurface_anisotropy },
subsurface_mode: { value: params.subsurface_mode },
coat_weight: { value: params.coat_weight },
coat_color: { value: array_to_vector3(params.coat_color) },
coat_roughness: { value: params.coat_roughness },
coat_anisotropy: { value: params.coat_anisotropy },
coat_rotation: { value: params.coat_rotation },
coat_ior: { value: params.coat_ior },
coat_darkening: { value: params.coat_darkening },
fuzz_weight: { value: params.fuzz_weight },
fuzz_color: { value: array_to_vector3(params.fuzz_color) },
fuzz_roughness: { value: params.fuzz_roughness },
emission_luminance: { value: params.emission_luminance },
emission_color: { value: array_to_vector3(params.emission_color) },
thin_film_weight: { value: params.thin_film_weight },
thin_film_thickness: { value: params.thin_film_thickness },
thin_film_ior: { value: params.thin_film_ior },
geometry_opacity: { value: params.geometry_opacity },
geometry_thin_walled: { value: params.geometry_thin_walled }
},
vertexShader: `
varying vec2 vUv;
void main()
{
vec4 mvPosition = vec4( position, 1.0 );
mvPosition = modelViewMatrix * mvPosition;
gl_Position = projectionMatrix * mvPosition;
vUv = uv;
}
`,
fragmentShader: `precision highp isampler2D;
precision highp usampler2D;
precision highp int;
${ shaderStructs }
${ shaderIntersectFunction }
`
+ glsl_main
+ glsl_fuzz_brdf
+ glsl_coat_brdf
+ glsl_thin_film
+ glsl_specular_brdf
+ glsl_specular_btdf
+ glsl_metal_brdf
+ glsl_diffuse_brdf
+ glsl_openpbr_surface
+ glsl_pathtracer
} );
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// initialize the scene and update the material properties with the bvh, materials, etc
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
load_scene(params.scene_name);
}
function load_scene(scene_name)
{
const loader = new MeshLoader();
// @todo:
// - clear current THREE.js scene
LOADED = false;
progress_bar.setText('loading meshes...');
progress_bar.animate(0.0);
loader.load(scene_name + '/neutral_objects.glb').then( () => {
scene.add(loader.result.scene);
MESH_PROPS = loader.result.mesh;
BVH_PROPS = loader.result.bvh;
rtMaterial.uniforms.bvh_props.value.updateFrom( BVH_PROPS );
rtMaterial.uniforms.has_normals_props.value = false;
rtMaterial.uniforms.has_tangents_props.value = false;
if (MESH_PROPS.geometry.attributes.normal)
{
rtMaterial.uniforms.normalAttribute_props.value.updateFrom( MESH_PROPS.geometry.attributes.normal );
rtMaterial.uniforms.has_normals_props.value = true;
}
if (MESH_PROPS.geometry.attributes.tangent)
{
rtMaterial.uniforms.tangentAttribute_props.value.updateFrom( MESH_PROPS.geometry.attributes.tangent );
rtMaterial.uniforms.has_tangents_props.value = true;
}
console.log(" has_normals_scene: ", rtMaterial.uniforms.has_normals_props);
console.log(" has_tangents_scene: ", rtMaterial.uniforms.has_tangents_props);
progress_bar.animate(0.5);
loader.reset();
loader.load(scene_name + '/openpbr_objects.glb').then( () => {
scene.add(loader.result.scene);
MESH_SURFACE = loader.result.mesh;
BVH_SURFACE = loader.result.bvh;
rtMaterial.uniforms.bvh_surface.value.updateFrom( BVH_SURFACE );
rtMaterial.uniforms.has_normals_surface.value = false;
rtMaterial.uniforms.has_tangents_surface.value = false;
if (MESH_SURFACE.geometry.attributes.normal)
{
rtMaterial.uniforms.normalAttribute_surface.value.updateFrom( MESH_SURFACE.geometry.attributes.normal );
rtMaterial.uniforms.has_normals_surface.value = true;
}
if (MESH_SURFACE.geometry.attributes.tangent)
{
rtMaterial.uniforms.tangentAttribute_surface.value.updateFrom( MESH_SURFACE.geometry.attributes.tangent );
rtMaterial.uniforms.has_tangents_surface.value = true;
}
console.log(" has_normals_surface: ", rtMaterial.uniforms.has_normals_surface);
console.log(" has_tangents_surface: ", rtMaterial.uniforms.has_tangents_surface);
console.log("===> LOADED");
LOADED = true;
post_load_setup();
progress_bar.animate(1.0);
let progress_overlay = document.getElementById('progress_overlay');
progress_finished_timer = performance.now();
} )
} );
}
function reset_camera(scene_name)
{
let camera_fov = 23.6701655;
let camera_near = 0.01;
let camera_far = 1000.0;
camera = new PerspectiveCamera( camera_fov, window.innerWidth / window.innerHeight, camera_near, camera_far );
orbitControls = new OrbitControls( camera, renderer.domElement );
orbitControls.addEventListener( 'change', () => { resetSamples(); } );
let matrixWorld = new Matrix4();
if (scene_name == 'standard-shader-ball')
{
// Set camera default orientation according to the Standard Shader Ball USD asset description:
matrixWorld.set( 0.9396926207859084, 0, -0.3420201433256687, 0,
-0.2203032561704394, 0.7649214009184319, -0.6052782217606094, 0,
0.26161852717499334, 0.6441236297613865, 0.7187909959242699, 0,
6.531538924716362, 19.5, 17.948521838355774, 1 );
}
else if (scene_name == 'glavenus')
{
matrixWorld.set( 0.4848291963218869, -6.938893903907228e-18, -0.8746088556571293, 0,
-0.07533009256065425, 0.9962839037303908, -0.041758356319859954, 0,
0.8713587249512548, 0.08613003638530015, 0.4830275243540376, 0,
23.076273094000275, 6.7653774216248, 14.822630983786677, 1);
}
else if (scene_name == 'terrain')
{
matrixWorld.set( 0.7242953632536803, -1.1102230246251565e-16, -0.6894898307946385, 0,
-0.4511571209928634, 0.7562050657737049, -0.4739315886028461, 0,
0.5213957028463604, 0.6543346991396579, 0.5477158228088388, 0,
8.561709328489492, 11.460860759783042, 8.95672568146927, 1);
}
else if (scene_name == 'bearded-man')
{
matrixWorld.set(0.6586894440882616, -1.3877787807814457e-17, 0.752414922929295, 0,
0.13367205033823076, 0.9840924050751759, -0.11702102901499911, 0,
-0.7404458111199431, 0.17765736200156684, 0.648211279230448, 0,
-20.089277049402824, 9.131027464916848, 18.02162149148976, 1);
}
matrixWorld.transpose();
camera.matrixAutoUpdate = false;
camera.applyMatrix4(matrixWorld);
camera.matrixAutoUpdate = true;
camera.updateMatrixWorld();
let dir = new Vector3();
camera.getWorldDirection(dir);
let cam_target = camera.position.clone();
cam_target.addScaledVector(dir, 23.39613);
orbitControls.target.copy(cam_target);
orbitControls.zoomSpeed = 1.5;
orbitControls.flySpeed = 0.01;
orbitControls.update();
}
function coat_enabled()
{
if (params.coat_weight == 0.0)
return false;
return true;
}
function volume_enabled()
{
if (params.base_metalness == 1.0)
return false;
if (params.transmission_weight > 0.0 &&
params.transmission_depth > 0.0)
return true;
if (params.subsurface_weight > 0.0)
return true;
return false;
}
function transmission_enabled()
{
if (params.transmission_weight > 0.0)
return true;
if (params.subsurface_weight > 0.0)
return true;
return false;
}
function dispersion_enabled()
{
if (params.transmission_dispersion_scale > 0.0)
return true;
return false;
}
function thin_film_enabled()
{
if (params.thin_film_weight > 0.0)
return true;
return false;
}
function post_load_setup()
{
//////////////////////////////////////////////////////////
// Setup framebuffers
//////////////////////////////////////////////////////////
rtQuad = new FullScreenQuad( rtMaterial );
rtMaterial.transparent = true;
rtMaterial.depthWrite = false;
renderTarget = new WebGLRenderTarget(1, 1, {format: RGBAFormat, type: FloatType});
finalQuad = new FullScreenQuad( new MeshBasicMaterial({map: renderTarget.texture}) );
// Trigger initial shader compile
trigger_recompile();
//////////////////////////////////////////////////////////
// Setup camera
//////////////////////////////////////////////////////////
reset_camera(params.scene_name);
//////////////////////////////////////////////////////////
// Setup GUI
//////////////////////////////////////////////////////////
gui = new GUI({ width: 300 });
///// Material folder /////////////////////////////////////
const material_folder = gui.addFolder('Material');
// Base folder
const base_folder = material_folder.addFolder('Base');
base_folder.add(params, 'base_weight', 0.0, 1.0).onChange( v => { resetSamples(); });
base_folder.addColor(params, 'base_color').onChange( v => { resetSamples(); });
base_folder.add(params, 'base_roughness', 0.0, 1.0).onChange( v => { resetSamples(); });
base_folder.add(params, 'base_metalness', 0.0, 1.0).onChange( v => { resetSamples();
if (volume_enabled() != rtMaterial.defines.VOLUME_ENABLED)
{
rtMaterial.defines.VOLUME_ENABLED = volume_enabled();
trigger_recompile();
}});
base_folder.add(params, 'diffuse_mode', diffuse_mode_names).onChange( v => { resetSamples(); });
// Specular folder
const specular_folder = material_folder.addFolder('Specular');
specular_folder.add(params, 'specular_weight', 0.0, 1.0).onChange( v => { resetSamples(); });
specular_folder.addColor(params, 'specular_color').onChange( v => { resetSamples(); });
specular_folder.add(params, 'specular_roughness', 0.0, 1.0).onChange( v => { resetSamples(); });
specular_folder.add(params, 'specular_ior', 1.0, 5.0).onChange( v => { resetSamples(); });
specular_folder.add(params, 'specular_anisotropy', 0.0, 1.0).onChange( v => { resetSamples(); });
specular_folder.add(params, 'specular_rotation', 0.0, 1.0).onChange( v => { resetSamples(); });
// Transmission folder
const transmission_folder = material_folder.addFolder('Transmission');
transmission_folder.add(params, 'transmission_weight', 0.0, 1.0).onChange( v => { resetSamples();
if (volume_enabled() != rtMaterial.defines.VOLUME_ENABLED)
{
rtMaterial.defines.VOLUME_ENABLED = volume_enabled();
trigger_recompile();
}
if (transmission_enabled() != rtMaterial.defines.TRANSMISSION_ENABLED)
{
rtMaterial.defines.TRANSMISSION_ENABLED = transmission_enabled();
trigger_recompile();
}
});
transmission_folder.addColor(params, 'transmission_color').onChange( v => { resetSamples(); });
transmission_folder.add(params, 'transmission_depth', 0.0, 1.0).onChange( v => { resetSamples();
if (volume_enabled() != rtMaterial.defines.VOLUME_ENABLED)
{
rtMaterial.defines.VOLUME_ENABLED = volume_enabled();
trigger_recompile();
}});
transmission_folder.addColor(params, 'transmission_scatter').onChange( v => { resetSamples(); });
transmission_folder.add(params, 'transmission_scatter_anisotropy', -1.0, 1.0).onChange( v => { resetSamples(); });
transmission_folder.add(params, 'transmission_dispersion_abbe_number', 9.0, 91.0).onChange( v => { resetSamples(); });
transmission_folder.add(params, 'transmission_dispersion_scale', 0.0, 1.0).onChange( v => { resetSamples();
if (dispersion_enabled() != rtMaterial.defines.DISPERSION_ENABLED)
{
rtMaterial.defines.DISPERSION_ENABLED = dispersion_enabled();
trigger_recompile();
}});
transmission_folder.close();
// Subsurface folder
const subsurface_folder = material_folder.addFolder('Subsurface');
subsurface_folder.add(params, 'subsurface_weight', 0.0, 1.0).onChange( v => { resetSamples();
if (volume_enabled() != rtMaterial.defines.VOLUME_ENABLED)
{
rtMaterial.defines.VOLUME_ENABLED = volume_enabled();
trigger_recompile();
}
if (transmission_enabled() != rtMaterial.defines.TRANSMISSION_ENABLED)
{
rtMaterial.defines.TRANSMISSION_ENABLED = transmission_enabled();
trigger_recompile();
}
});
subsurface_folder.addColor(params, 'subsurface_color').onChange( v => { resetSamples(); });
subsurface_folder.add(params, 'subsurface_radius', 0.0, 1.0).onChange( v => { resetSamples(); });
subsurface_folder.addColor(params, 'subsurface_radius_scale').onChange( v => { resetSamples(); });
subsurface_folder.add(params, 'subsurface_anisotropy', -1.0, 1.0).onChange( v => { resetSamples(); });
subsurface_folder.add(params, 'subsurface_mode', subsurface_mode_names).onChange( v => { resetSamples(); });
subsurface_folder.close();
// Coat folder
const coat_folder = material_folder.addFolder('Coat');
coat_folder.add(params, 'coat_weight', 0.0, 1.0).onChange( v => { resetSamples();
if (coat_enabled() != rtMaterial.defines.COAT_ENABLED)
{
rtMaterial.defines.COAT_ENABLED = coat_enabled();
trigger_recompile();
}});
coat_folder.addColor(params, 'coat_color').onChange( v => { resetSamples(); });
coat_folder.add(params, 'coat_roughness', 0.0, 1.0).onChange( v => { resetSamples(); });
coat_folder.add(params, 'coat_ior', 1.0, 3.0).onChange( v => { resetSamples(); });
coat_folder.add(params, 'coat_anisotropy', 0.0, 1.0).onChange( v => { resetSamples(); });
coat_folder.add(params, 'coat_rotation', 0.0, 1.0).onChange( v => { resetSamples(); });
coat_folder.add(params, 'coat_darkening', 0.0, 1.0).onChange( v => { resetSamples(); });
coat_folder.close();
// Fuzz folder
const fuzz_folder = material_folder.addFolder('Fuzz');
fuzz_folder.add(params, 'fuzz_weight', 0.0, 1.0).onChange( v => { resetSamples(); });
fuzz_folder.addColor(params, 'fuzz_color').onChange( v => { resetSamples(); });
fuzz_folder.add(params, 'fuzz_roughness', 0.0, 1.0).onChange( v => { resetSamples(); });
fuzz_folder.close();
// Emission folder
const emission_folder = material_folder.addFolder('Emission');
emission_folder.add(params, 'emission_luminance', 0.0, 10.0).onChange( v => { resetSamples(); });
emission_folder.addColor(params, 'emission_color').onChange( v => { resetSamples(); });
emission_folder.close();
// Thin-film folder
const thin_film_folder = material_folder.addFolder('Thin Film');
thin_film_folder.add(params, 'thin_film_weight', 0.0, 1.0).onChange( v => { resetSamples();
if (thin_film_enabled() != rtMaterial.defines.THIN_FILM_ENABLED)
{
rtMaterial.defines.THIN_FILM_ENABLED = thin_film_enabled();
trigger_recompile();
}});
thin_film_folder.add(params, 'thin_film_thickness', 0.0, 2000.0).onChange( v => { resetSamples(); });
thin_film_folder.add(params, 'thin_film_ior', 1.0, 3.0).onChange( v => { resetSamples(); });
thin_film_folder.close();
// geometry folder
const geometry_folder = material_folder.addFolder('Geometry');
geometry_folder.add(params, 'geometry_opacity', 0.0, 1.0).onChange( v => { resetSamples(); });
geometry_folder.add(params, 'geometry_thin_walled').onChange( v => { resetSamples(); });
geometry_folder.close();
///// Lighting folder /////////////////////////////////////
const lighting_folder = gui.addFolder('Lighting');
lighting_folder.add(params, 'skyPower', 0.0, 2.0).onChange( v => { resetSamples(); });
lighting_folder.addColor(params, 'skyColor').onChange( v => { resetSamples(); });
lighting_folder.add(params, 'sunPower', -4.0, 4.0).onChange( v => { resetSamples(); });
lighting_folder.add(params, 'sunAngularSize', 0.0, 40.0).onChange( v => { resetSamples(); });
lighting_folder.add(params, 'sunLatitude', 0.0, 90.0).onChange( v => { resetSamples(); });
lighting_folder.add(params, 'sunLongitude', 0.0, 360.0).onChange( v => { resetSamples(); });
lighting_folder.addColor(params, 'sunColor').onChange( v => { resetSamples(); });
lighting_folder.close();
///// Renderer folder /////////////////////////////////////
const renderer_folder = gui.addFolder('Renderer');
renderer_folder.add(params, 'scene_name', scene_names).onChange( v => { console.log(v);
load_scene(v);
resetSamples(); });
renderer_folder.add( params, 'smooth_normals' ).onChange( v => { resetSamples(); });
renderer_folder.add( params, 'wireframe' ).onChange( v => { resetSamples(); });
renderer_folder.addColor(params, 'neutral_color').onChange( v => { resetSamples(); });
renderer_folder.add( params, 'bounces', 0, 100, 1 ).onChange( v => { rtMaterial.defines.BOUNCES = parseInt( v );
resetSamples();
trigger_recompile(); });
renderer_folder.add( params, 'max_samples' ).onChange( v => { resetSamples(); });
renderer_folder.add( params, 'max_volume_steps', 1, 100, 1 ).onChange( v => { rtMaterial.defines.MAX_VOLUME_STEPS = parseInt( v );
resetSamples();
trigger_recompile(); });
renderer_folder.close();
gui.add( params, 'reset_camera' );
gui.open();
//////////////////////////////////////////////////////////
// Setup window
//////////////////////////////////////////////////////////
window.addEventListener( 'resize', resize, false );
resize();
}
function trigger_recompile()
{
renderer.setRenderTarget( renderTarget );
let tmp_cam = new OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
startCompilationProgress();
let compile_promise = renderer.compileAsync(rtQuad._mesh, tmp_cam);
compile_promise.then((val) => {
console.log('shaders successfully compiled.');
finishCompilationProgress();
}).catch((err) => {
console.log('shader compilation error: ' + err);
}).finally(() => {});
}
function startCompilationProgress()
{
let progress_overlay = document.getElementById('progress_overlay');
progress_overlay.style.display = 'block';
progress_overlay.style.opacity = 1;
progress_bar.set(0.0);
progress_bar.setText('shaders compiling...');
COMPILING = true;
}
function finishCompilationProgress()
{
progress_bar.set(1.0);
progress_finished_timer = performance.now();
COMPILING = false;
}
function resize()
{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
const w = window.innerWidth;
const h = window.innerHeight;
renderer.setSize( w, h );
renderer.setPixelRatio(1.0);
renderTarget.setSize(w, h);
resetSamples();
}
function get_vector3(array3)
{
return new Vector3(array3[0], array3[1], array3[2]);
}
function resetSamples()
{
if (rtMaterial)
rtMaterial.needsUpdate = true;
samples = 0;
}
function fadeOutProgressBar(time_ms)
{
let progress_overlay = document.getElementById('progress_overlay');
var fadeOutEffect = setInterval(function () {
if (!progress_overlay.style.opacity) {
progress_overlay.style.opacity = 1;
}
if (progress_overlay.style.opacity > 0) {
progress_overlay.style.opacity -= 0.025;
} else {
progress_overlay.style.display = 'none';
progress_overlay.style.opacity = 0;
clearInterval(fadeOutEffect);
}
}, time_ms);
}
function render()
{
if (!LOADED)
{
console.log('not LOADED')
requestAnimationFrame( render );
return;
}
renderer.domElement.style.imageRendering = 'auto';
if (samples >= params.max_samples)
{
requestAnimationFrame( render );
return;
}
if (!COMPILING && LOADED)
{
camera.updateMatrixWorld();
//////////////////////////////////////////////////////
// sync shader uniforms
//////////////////////////////////////////////////////
const uniforms = rtQuad.material.uniforms;
const w = window.innerWidth;
const h = window.innerHeight;
// sync camera
uniforms.cameraWorldMatrix.value.copy( camera.matrixWorld );
uniforms.invProjectionMatrix.value.copy( camera.projectionMatrixInverse );
uniforms.invModelMatrix.value.copy( scene.matrixWorld ).invert();
// sync renderer params
let resolution = new Vector2(w, h);
uniforms.resolution.value.copy(resolution);
uniforms.accumulation_weight.value = 1.0 / (samples + 1.0); // implements Monte-Carlo accumulation
uniforms.samples.value = samples;
uniforms.wireframe.value = params.wireframe;
uniforms.neutral_color.value.copy(get_vector3( params.neutral_color));
uniforms.smooth_normals.value = params.smooth_normals;
// sync material params
uniforms.base_weight.value = params.base_weight;
uniforms.base_color.value.copy(get_vector3( params.base_color));
uniforms.base_roughness.value = params.base_roughness;
uniforms.base_metalness.value = params.base_metalness;
uniforms.diffuse_mode.value = params.diffuse_mode;
uniforms.specular_weight.value = params.specular_weight;
uniforms.specular_color.value.copy(get_vector3( params.specular_color));
uniforms.specular_roughness.value = params.specular_roughness;
uniforms.specular_anisotropy.value = params.specular_anisotropy;
uniforms.specular_rotation.value = params.specular_rotation;
uniforms.specular_ior.value = params.specular_ior;
uniforms.transmission_weight.value = params.transmission_weight;
uniforms.transmission_color.value.copy(get_vector3( params.transmission_color));
uniforms.transmission_depth.value = params.transmission_depth;
uniforms.transmission_scatter.value.copy(get_vector3( params.transmission_scatter));
uniforms.transmission_scatter_anisotropy.value = params.transmission_scatter_anisotropy;
uniforms.transmission_dispersion_abbe_number.value = params.transmission_dispersion_abbe_number;
uniforms.transmission_dispersion_scale.value = params.transmission_dispersion_scale;
uniforms.subsurface_weight.value = params.subsurface_weight;
uniforms.subsurface_color.value.copy(get_vector3( params.subsurface_color));
uniforms.subsurface_radius.value = params.subsurface_radius;
uniforms.subsurface_radius_scale.value.copy(get_vector3(params.subsurface_radius_scale));
uniforms.subsurface_anisotropy.value = params.subsurface_anisotropy;
uniforms.subsurface_mode.value = params.subsurface_mode;
uniforms.coat_weight.value = params.coat_weight;
uniforms.coat_color.value.copy(get_vector3( params.coat_color));
uniforms.coat_roughness.value = params.coat_roughness;
uniforms.coat_anisotropy.value = params.coat_anisotropy;
uniforms.coat_rotation.value = params.coat_rotation;
uniforms.coat_ior.value = params.coat_ior;
uniforms.coat_darkening .value = params.coat_darkening;
uniforms.fuzz_weight.value = params.fuzz_weight;