-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathmtoon.frag
719 lines (518 loc) · 20.8 KB
/
mtoon.frag
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
// #define PHONG
uniform vec3 litFactor;
uniform float opacity;
uniform vec3 shadeColorFactor;
#ifdef USE_SHADEMULTIPLYTEXTURE
uniform sampler2D shadeMultiplyTexture;
uniform mat3 shadeMultiplyTextureUvTransform;
#endif
uniform float shadingShiftFactor;
uniform float shadingToonyFactor;
#ifdef USE_SHADINGSHIFTTEXTURE
uniform sampler2D shadingShiftTexture;
uniform mat3 shadingShiftTextureUvTransform;
uniform float shadingShiftTextureScale;
#endif
uniform float giEqualizationFactor;
uniform vec3 parametricRimColorFactor;
#ifdef USE_RIMMULTIPLYTEXTURE
uniform sampler2D rimMultiplyTexture;
uniform mat3 rimMultiplyTextureUvTransform;
#endif
uniform float rimLightingMixFactor;
uniform float parametricRimFresnelPowerFactor;
uniform float parametricRimLiftFactor;
#ifdef USE_MATCAPTEXTURE
uniform vec3 matcapFactor;
uniform sampler2D matcapTexture;
uniform mat3 matcapTextureUvTransform;
#endif
uniform vec3 emissive;
uniform float emissiveIntensity;
uniform vec3 outlineColorFactor;
uniform float outlineLightingMixFactor;
#ifdef USE_UVANIMATIONMASKTEXTURE
uniform sampler2D uvAnimationMaskTexture;
uniform mat3 uvAnimationMaskTextureUvTransform;
#endif
uniform float uvAnimationScrollXOffset;
uniform float uvAnimationScrollYOffset;
uniform float uvAnimationRotationPhase;
#include <common>
#include <packing>
#include <dithering_pars_fragment>
#include <color_pars_fragment>
// #include <uv_pars_fragment>
#if ( defined( MTOON_USE_UV ) && !defined( MTOON_UVS_VERTEX_ONLY ) )
varying vec2 vUv;
#endif
#include <uv2_pars_fragment>
#include <map_pars_fragment>
#ifdef USE_MAP
uniform mat3 mapUvTransform;
#endif
// #include <alphamap_pars_fragment>
#if THREE_VRM_THREE_REVISION >= 132
#include <alphatest_pars_fragment>
#endif
#include <aomap_pars_fragment>
// #include <lightmap_pars_fragment>
#include <emissivemap_pars_fragment>
#ifdef USE_EMISSIVEMAP
uniform mat3 emissiveMapUvTransform;
#endif
// #include <envmap_common_pars_fragment>
// #include <envmap_pars_fragment>
// #include <cube_uv_reflection_fragment>
#include <fog_pars_fragment>
// #include <bsdfs>
vec3 BRDF_Lambert( const in vec3 diffuseColor ) {
return RECIPROCAL_PI * diffuseColor;
}
#include <lights_pars_begin>
#if THREE_VRM_THREE_REVISION >= 132
#include <normal_pars_fragment>
#endif
// #include <lights_phong_pars_fragment>
varying vec3 vViewPosition;
#if THREE_VRM_THREE_REVISION < 132
#ifndef FLAT_SHADED
varying vec3 vNormal;
#endif
#endif
struct MToonMaterial {
vec3 diffuseColor;
vec3 shadeColor;
float shadingShift;
};
float linearstep( float a, float b, float t ) {
return clamp( ( t - a ) / ( b - a ), 0.0, 1.0 );
}
/**
* Convert NdotL into toon shading factor using shadingShift and shadingToony
*/
float getShading(
const in float dotNL,
const in float shadow,
const in float shadingShift
) {
float shading = dotNL;
shading = shading + shadingShift;
shading = linearstep( -1.0 + shadingToonyFactor, 1.0 - shadingToonyFactor, shading );
shading *= shadow;
return shading;
}
/**
* Mix diffuseColor and shadeColor using shading factor and light color
*/
vec3 getDiffuse(
const in MToonMaterial material,
const in float shading,
in vec3 lightColor
) {
#ifdef DEBUG_LITSHADERATE
return vec3( BRDF_Lambert( shading * lightColor ) );
#endif
#if THREE_VRM_THREE_REVISION < 132
#ifndef PHYSICALLY_CORRECT_LIGHTS
lightColor *= PI;
#endif
#endif
vec3 col = lightColor * BRDF_Lambert( mix( material.shadeColor, material.diffuseColor, shading ) );
// The "comment out if you want to PBR absolutely" line
#ifdef V0_COMPAT_SHADE
col = min( col, material.diffuseColor );
#endif
return col;
}
void RE_Direct_MToon( const in IncidentLight directLight, const in GeometricContext geometry, const in MToonMaterial material, const in float shadow, inout ReflectedLight reflectedLight ) {
float dotNL = saturate( dot( geometry.normal, directLight.direction ) );
vec3 irradiance = directLight.color;
#if THREE_VRM_THREE_REVISION < 132
#ifndef PHYSICALLY_CORRECT_LIGHTS
irradiance *= PI;
#endif
#endif
// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;
irradiance *= dotNL;
float shading = getShading( dotNL, shadow, material.shadingShift );
// toon shaded diffuse
reflectedLight.directDiffuse += getDiffuse( material, shading, directLight.color );
}
void RE_IndirectDiffuse_MToon( const in vec3 irradiance, const in GeometricContext geometry, const in MToonMaterial material, inout ReflectedLight reflectedLight ) {
// indirect diffuse will use diffuseColor, no shadeColor involved
reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;
}
#define RE_Direct RE_Direct_MToon
#define RE_IndirectDiffuse RE_IndirectDiffuse_MToon
#define Material_LightProbeLOD( material ) (0)
#include <shadowmap_pars_fragment>
// #include <bumpmap_pars_fragment>
// #include <normalmap_pars_fragment>
#ifdef USE_NORMALMAP
uniform sampler2D normalMap;
uniform mat3 normalMapUvTransform;
uniform vec2 normalScale;
#endif
#ifdef OBJECTSPACE_NORMALMAP
uniform mat3 normalMatrix;
#endif
#if ! defined ( USE_TANGENT ) && defined ( TANGENTSPACE_NORMALMAP )
// Per-Pixel Tangent Space Normal Mapping
// http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
// three-vrm specific change: it requires `uv` as an input in order to support uv scrolls
// Temporary compat against shader change @ Three.js r126
// See: #21205, #21307, #21299
#if THREE_VRM_THREE_REVISION >= 126
vec3 perturbNormal2Arb( vec2 uv, vec3 eye_pos, vec3 surf_norm, vec3 mapN, float faceDirection ) {
vec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );
vec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );
vec2 st0 = dFdx( uv.st );
vec2 st1 = dFdy( uv.st );
vec3 N = normalize( surf_norm );
vec3 q1perp = cross( q1, N );
vec3 q0perp = cross( N, q0 );
vec3 T = q1perp * st0.x + q0perp * st1.x;
vec3 B = q1perp * st0.y + q0perp * st1.y;
// three-vrm specific change: Workaround for the issue that happens when delta of uv = 0.0
// TODO: Is this still required? Or shall I make a PR about it?
if ( length( T ) == 0.0 || length( B ) == 0.0 ) {
return surf_norm;
}
float det = max( dot( T, T ), dot( B, B ) );
float scale = ( det == 0.0 ) ? 0.0 : faceDirection * inversesqrt( det );
return normalize( T * ( mapN.x * scale ) + B * ( mapN.y * scale ) + N * mapN.z );
}
#else
vec3 perturbNormal2Arb( vec2 uv, vec3 eye_pos, vec3 surf_norm, vec3 mapN ) {
// Workaround for Adreno 3XX dFd*( vec3 ) bug. See #9988
vec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );
vec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );
vec2 st0 = dFdx( uv.st );
vec2 st1 = dFdy( uv.st );
float scale = sign( st1.t * st0.s - st0.t * st1.s ); // we do not care about the magnitude
vec3 S = ( q0 * st1.t - q1 * st0.t ) * scale;
vec3 T = ( - q0 * st1.s + q1 * st0.s ) * scale;
// three-vrm specific change: Workaround for the issue that happens when delta of uv = 0.0
// TODO: Is this still required? Or shall I make a PR about it?
if ( length( S ) == 0.0 || length( T ) == 0.0 ) {
return surf_norm;
}
S = normalize( S );
T = normalize( T );
vec3 N = normalize( surf_norm );
#ifdef DOUBLE_SIDED
// Workaround for Adreno GPUs gl_FrontFacing bug. See #15850 and #10331
bool frontFacing = dot( cross( S, T ), N ) > 0.0;
mapN.xy *= ( float( frontFacing ) * 2.0 - 1.0 );
#else
mapN.xy *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );
#endif
mat3 tsn = mat3( S, T, N );
return normalize( tsn * mapN );
}
#endif
#endif
// #include <specularmap_pars_fragment>
#include <logdepthbuf_pars_fragment>
#include <clipping_planes_pars_fragment>
// == post correction ==========================================================
void postCorrection() {
#include <tonemapping_fragment>
#include <encodings_fragment>
#include <fog_fragment>
#include <premultiplied_alpha_fragment>
#include <dithering_fragment>
}
// == main procedure ===========================================================
void main() {
#include <clipping_planes_fragment>
vec2 uv = vec2(0.5, 0.5);
#if ( defined( MTOON_USE_UV ) && !defined( MTOON_UVS_VERTEX_ONLY ) )
uv = vUv;
float uvAnimMask = 1.0;
#ifdef USE_UVANIMATIONMASKTEXTURE
vec2 uvAnimationMaskTextureUv = ( uvAnimationMaskTextureUvTransform * vec3( uv, 1 ) ).xy;
uvAnimMask = texture2D( uvAnimationMaskTexture, uvAnimationMaskTextureUv ).b;
#endif
uv = uv + vec2( uvAnimationScrollXOffset, uvAnimationScrollYOffset ) * uvAnimMask;
float uvRotCos = cos( uvAnimationRotationPhase * uvAnimMask );
float uvRotSin = sin( uvAnimationRotationPhase * uvAnimMask );
uv = mat2( uvRotCos, -uvRotSin, uvRotSin, uvRotCos ) * ( uv - 0.5 ) + 0.5;
#endif
#ifdef DEBUG_UV
gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 );
#if ( defined( MTOON_USE_UV ) && !defined( MTOON_UVS_VERTEX_ONLY ) )
gl_FragColor = vec4( uv, 0.0, 1.0 );
#endif
return;
#endif
vec4 diffuseColor = vec4( litFactor, opacity );
ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
vec3 totalEmissiveRadiance = emissive * emissiveIntensity;
#include <logdepthbuf_fragment>
// #include <map_fragment>
#ifdef USE_MAP
vec2 mapUv = ( mapUvTransform * vec3( uv, 1 ) ).xy;
#if THREE_VRM_THREE_REVISION >= 137
vec4 sampledDiffuseColor = texture2D( map, mapUv );
#ifdef DECODE_VIDEO_TEXTURE
sampledDiffuseColor = vec4( mix( pow( sampledDiffuseColor.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), sampledDiffuseColor.rgb * 0.0773993808, vec3( lessThanEqual( sampledDiffuseColor.rgb, vec3( 0.04045 ) ) ) ), sampledDiffuseColor.w );
#endif
diffuseColor *= sampledDiffuseColor;
#else
// COMPAT: pre-r137
vec4 texelColor = texture2D( map, mapUv );
texelColor = mapTexelToLinear( texelColor );
diffuseColor *= texelColor;
#endif
#endif
// #include <color_fragment>
#if ( defined( USE_COLOR ) && !defined( IGNORE_VERTEX_COLOR ) )
diffuseColor.rgb *= vColor;
#endif
// #include <alphamap_fragment>
#include <alphatest_fragment>
// #include <specularmap_fragment>
#include <normal_fragment_begin>
#ifdef OUTLINE
normal *= -1.0;
#endif
// #include <normal_fragment_maps>
#ifdef OBJECTSPACE_NORMALMAP
vec2 normalMapUv = ( normalMapUvTransform * vec3( uv, 1 ) ).xy;
normal = texture2D( normalMap, normalMapUv ).xyz * 2.0 - 1.0; // overrides both flatShading and attribute normals
#ifdef FLIP_SIDED
normal = - normal;
#endif
#ifdef DOUBLE_SIDED
// Temporary compat against shader change @ Three.js r126
// See: #21205, #21307, #21299
#if THREE_VRM_THREE_REVISION >= 126
normal = normal * faceDirection;
#else
normal = normal * ( float( gl_FrontFacing ) * 2.0 - 1.0 );
#endif
#endif
normal = normalize( normalMatrix * normal );
#elif defined( TANGENTSPACE_NORMALMAP )
vec2 normalMapUv = ( normalMapUvTransform * vec3( uv, 1 ) ).xy;
vec3 mapN = texture2D( normalMap, normalMapUv ).xyz * 2.0 - 1.0;
mapN.xy *= normalScale;
#ifdef USE_TANGENT
normal = normalize( vTBN * mapN );
#else
// Temporary compat against shader change @ Three.js r126
// See: #21205, #21307, #21299
#if THREE_VRM_THREE_REVISION >= 126
normal = perturbNormal2Arb( uv, -vViewPosition, normal, mapN, faceDirection );
#else
normal = perturbNormal2Arb( uv, -vViewPosition, normal, mapN );
#endif
#endif
#endif
// #include <emissivemap_fragment>
#ifdef USE_EMISSIVEMAP
vec2 emissiveMapUv = ( emissiveMapUvTransform * vec3( uv, 1 ) ).xy;
#if THREE_VRM_THREE_REVISION >= 137
totalEmissiveRadiance *= texture2D( emissiveMap, emissiveMapUv ).rgb;
#else
// COMPAT: pre-r137
totalEmissiveRadiance *= emissiveMapTexelToLinear( texture2D( emissiveMap, emissiveMapUv ) ).rgb;
#endif
#endif
#ifdef DEBUG_NORMAL
gl_FragColor = vec4( 0.5 + 0.5 * normal, 1.0 );
return;
#endif
// -- MToon: lighting --------------------------------------------------------
// accumulation
// #include <lights_phong_fragment>
MToonMaterial material;
material.diffuseColor = diffuseColor.rgb;
material.shadeColor = shadeColorFactor;
#ifdef USE_SHADEMULTIPLYTEXTURE
vec2 shadeMultiplyTextureUv = ( shadeMultiplyTextureUvTransform * vec3( uv, 1 ) ).xy;
#if THREE_VRM_THREE_REVISION >= 137
material.shadeColor *= texture2D( shadeMultiplyTexture, shadeMultiplyTextureUv ).rgb;
#else
// COMPAT: pre-r137
material.shadeColor *= shadeMultiplyTextureTexelToLinear( texture2D( shadeMultiplyTexture, shadeMultiplyTextureUv) ).rgb;
#endif
#endif
#if ( defined( USE_COLOR ) && !defined( IGNORE_VERTEX_COLOR ) )
material.shadeColor.rgb *= vColor;
#endif
material.shadingShift = shadingShiftFactor;
#ifdef USE_SHADINGSHIFTTEXTURE
vec2 shadingShiftTextureUv = ( shadingShiftTextureUvTransform * vec3( uv, 1 ) ).xy;
material.shadingShift += texture2D( shadingShiftTexture, shadingShiftTextureUv ).r * shadingShiftTextureScale;
#endif
// #include <lights_fragment_begin>
// MToon Specific changes:
// Since we want to take shadows into account of shading instead of irradiance,
// we had to modify the codes that multiplies the results of shadowmap into color of direct lights.
GeometricContext geometry;
geometry.position = - vViewPosition;
geometry.normal = normal;
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
#ifdef CLEARCOAT
geometry.clearcoatNormal = clearcoatNormal;
#endif
IncidentLight directLight;
// since these variables will be used in unrolled loop, we have to define in prior
float shadow;
#if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )
PointLight pointLight;
#if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0
PointLightShadow pointLightShadow;
#endif
#pragma unroll_loop_start
for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
pointLight = pointLights[ i ];
#if THREE_VRM_THREE_REVISION >= 132
getPointLightInfo( pointLight, geometry, directLight );
#else
getPointDirectLightIrradiance( pointLight, geometry, directLight );
#endif
shadow = 1.0;
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )
pointLightShadow = pointLightShadows[ i ];
shadow = all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
#endif
RE_Direct( directLight, geometry, material, shadow, reflectedLight );
}
#pragma unroll_loop_end
#endif
#if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct )
SpotLight spotLight;
#if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0
SpotLightShadow spotLightShadow;
#endif
#pragma unroll_loop_start
for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
spotLight = spotLights[ i ];
#if THREE_VRM_THREE_REVISION >= 132
getSpotLightInfo( spotLight, geometry, directLight );
#else
getSpotDirectLightIrradiance( spotLight, geometry, directLight );
#endif
shadow = 1.0;
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )
spotLightShadow = spotLightShadows[ i ];
shadow = all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
#endif
RE_Direct( directLight, geometry, material, shadow, reflectedLight );
}
#pragma unroll_loop_end
#endif
#if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct )
DirectionalLight directionalLight;
#if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
DirectionalLightShadow directionalLightShadow;
#endif
#pragma unroll_loop_start
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
directionalLight = directionalLights[ i ];
#if THREE_VRM_THREE_REVISION >= 132
getDirectionalLightInfo( directionalLight, geometry, directLight );
#else
getDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );
#endif
shadow = 1.0;
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
directionalLightShadow = directionalLightShadows[ i ];
shadow = all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
#endif
RE_Direct( directLight, geometry, material, shadow, reflectedLight );
}
#pragma unroll_loop_end
#endif
// #if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )
// RectAreaLight rectAreaLight;
// #pragma unroll_loop_start
// for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {
// rectAreaLight = rectAreaLights[ i ];
// RE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );
// }
// #pragma unroll_loop_end
// #endif
#if defined( RE_IndirectDiffuse )
vec3 iblIrradiance = vec3( 0.0 );
vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
#if THREE_VRM_THREE_REVISION >= 133
irradiance += getLightProbeIrradiance( lightProbe, geometry.normal );
#else
irradiance += getLightProbeIrradiance( lightProbe, geometry );
#endif
#if ( NUM_HEMI_LIGHTS > 0 )
#pragma unroll_loop_start
for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
#if THREE_VRM_THREE_REVISION >= 133
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
#else
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );
#endif
}
#pragma unroll_loop_end
#endif
#endif
// #if defined( RE_IndirectSpecular )
// vec3 radiance = vec3( 0.0 );
// vec3 clearcoatRadiance = vec3( 0.0 );
// #endif
#include <lights_fragment_maps>
#include <lights_fragment_end>
// modulation
#include <aomap_fragment>
vec3 col = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse;
#ifdef DEBUG_LITSHADERATE
gl_FragColor = vec4( col, diffuseColor.a );
postCorrection();
return;
#endif
// -- MToon: rim lighting -----------------------------------------
vec3 viewDir = normalize( vViewPosition );
#ifndef PHYSICALLY_CORRECT_LIGHTS
reflectedLight.directSpecular /= PI;
#endif
vec3 rimMix = mix( vec3( 1.0 ), reflectedLight.directSpecular, 1.0 );
vec3 rim = parametricRimColorFactor * pow( saturate( 1.0 - dot( viewDir, normal ) + parametricRimLiftFactor ), parametricRimFresnelPowerFactor );
#ifdef USE_MATCAPTEXTURE
{
vec3 x = normalize( vec3( viewDir.z, 0.0, -viewDir.x ) );
vec3 y = cross( viewDir, x ); // guaranteed to be normalized
vec2 sphereUv = 0.5 + 0.5 * vec2( dot( x, normal ), -dot( y, normal ) );
sphereUv = ( matcapTextureUvTransform * vec3( sphereUv, 1 ) ).xy;
#if THREE_VRM_THREE_REVISION >= 137
vec3 matcap = texture2D( matcapTexture, sphereUv ).rgb;
#else
// COMPAT: pre-r137
vec3 matcap = matcapTextureTexelToLinear( texture2D( matcapTexture, sphereUv ) ).rgb;
#endif
rim += matcapFactor * matcap;
}
#endif
#ifdef USE_RIMMULTIPLYTEXTURE
vec2 rimMultiplyTextureUv = ( rimMultiplyTextureUvTransform * vec3( uv, 1 ) ).xy;
#if THREE_VRM_THREE_REVISION >= 137
rim *= texture2D( rimMultiplyTexture, rimMultiplyTextureUv ).rgb;
#else
// COMPAT: pre-r137
rim *= rimMultiplyTextureTexelToLinear( texture2D( rimMultiplyTexture, rimMultiplyTextureUv ) ).rgb;
#endif
#endif
col += rimMix * rim;
// -- MToon: Emission --------------------------------------------------------
col += totalEmissiveRadiance;
// #include <envmap_fragment>
// -- Almost done! -----------------------------------------------------------
#if defined( OUTLINE )
col = outlineColorFactor.rgb * mix( vec3( 1.0 ), col, outlineLightingMixFactor );
#endif
gl_FragColor = vec4( col, diffuseColor.a );
postCorrection();
}