rayzee 7.10.2 → 7.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/TSL/Common.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { Fn, wgslFn, float, vec2, vec3, vec4, int, mat3, If, max, dot, clamp, bool as tslBool } from 'three/tsl';
2
2
 
3
3
  import {
4
+ AnisoFrame,
4
5
  DotProducts,
5
6
  MaterialClassification,
6
7
  MISStrategy,
@@ -155,6 +156,58 @@ export const computeDotProducts = Fn( ( [ N, V, L ] ) => {
155
156
  NoH: max( dot( N, H ), 0.001 ),
156
157
  VoH: max( dot( V, H ), 0.001 ),
157
158
  LoH: max( dot( L, H ), 0.001 ),
159
+ ToH: float( 0.0 ), BoH: float( 0.0 ),
160
+ ToV: float( 0.0 ), BoV: float( 0.0 ),
161
+ ToL: float( 0.0 ), BoL: float( 0.0 ),
162
+ } );
163
+
164
+ } );
165
+
166
+ // Anisotropy tangent frame: arbitrary ONB from N (matching constructTBN + normal mapping),
167
+ // rotated by anisotropyRotation. Single source of truth for sampler AND eval/PDF so their
168
+ // frames are bit-identical (required for MIS consistency).
169
+ export const anisoTangentFrame = Fn( ( [ N, rotation ] ) => {
170
+
171
+ const majorAxis = N.x.abs().lessThan( 0.999 ).select( vec3( 1.0, 0.0, 0.0 ), vec3( 0.0, 1.0, 0.0 ) );
172
+ const T0 = N.cross( majorAxis ).normalize();
173
+ const B0 = N.cross( T0 ).normalize();
174
+ const c = rotation.cos();
175
+ const s = rotation.sin();
176
+ return AnisoFrame( { Ta: T0.mul( c ).add( B0.mul( s ) ), Ba: B0.mul( c ).sub( T0.mul( s ) ) } );
177
+
178
+ } );
179
+
180
+ // Anisotropy-aware dot products: computeDotProducts + tangent-frame projections of H/V/L
181
+ // (0 for isotropic materials, so aniso branches gated on anisotropy>0 are a no-op).
182
+ export const computeDotProductsAniso = Fn( ( [ N, V, L, material ] ) => {
183
+
184
+ const H = V.add( L ).toVar();
185
+ const lenSq = dot( H, H ).toVar();
186
+ H.assign( lenSq.greaterThan( EPSILON ).select( H.div( lenSq.sqrt() ), vec3( 0.0, 0.0, 1.0 ) ) );
187
+
188
+ const ToH = float( 0.0 ).toVar();
189
+ const BoH = float( 0.0 ).toVar();
190
+ const ToV = float( 0.0 ).toVar();
191
+ const BoV = float( 0.0 ).toVar();
192
+ const ToL = float( 0.0 ).toVar();
193
+ const BoL = float( 0.0 ).toVar();
194
+
195
+ If( material.anisotropy.greaterThan( 0.0 ), () => {
196
+
197
+ const f = AnisoFrame.wrap( anisoTangentFrame( N, material.anisotropyRotation ) );
198
+ ToH.assign( dot( f.Ta, H ) ); BoH.assign( dot( f.Ba, H ) );
199
+ ToV.assign( dot( f.Ta, V ) ); BoV.assign( dot( f.Ba, V ) );
200
+ ToL.assign( dot( f.Ta, L ) ); BoL.assign( dot( f.Ba, L ) );
201
+
202
+ } );
203
+
204
+ return DotProducts( {
205
+ NoL: max( dot( N, L ), 0.001 ),
206
+ NoV: max( dot( N, V ), 0.001 ),
207
+ NoH: max( dot( N, H ), 0.001 ),
208
+ VoH: max( dot( V, H ), 0.001 ),
209
+ LoH: max( dot( L, H ), 0.001 ),
210
+ ToH, BoH, ToV, BoV, ToL, BoL,
158
211
  } );
159
212
 
160
213
  } );
@@ -345,6 +398,9 @@ export const getMaterial = Fn( ( [ materialIndex, materialBuffer ] ) => {
345
398
  const data27 = getDatafromStorageBuffer( materialBuffer, materialIndex, int( S.SUBSURFACE_A ), int( MATERIAL_SLOTS ) ).toVar();
346
399
  const data28 = getDatafromStorageBuffer( materialBuffer, materialIndex, int( S.SUBSURFACE_B ), int( MATERIAL_SLOTS ) ).toVar();
347
400
  const data29 = getDatafromStorageBuffer( materialBuffer, materialIndex, int( S.SUBSURFACE_C ), int( MATERIAL_SLOTS ) ).toVar();
401
+ const data30 = getDatafromStorageBuffer( materialBuffer, materialIndex, int( S.EXT_MAP_INDICES_A ), int( MATERIAL_SLOTS ) ).toVar();
402
+ const data31 = getDatafromStorageBuffer( materialBuffer, materialIndex, int( S.EXT_MAP_INDICES_B ), int( MATERIAL_SLOTS ) ).toVar();
403
+ const data32 = getDatafromStorageBuffer( materialBuffer, materialIndex, int( S.EXT_MAP_INDICES_C ), int( MATERIAL_SLOTS ) ).toVar();
348
404
 
349
405
  return RayTracingMaterial( {
350
406
  color: vec4( data0.rgb, 1.0 ),
@@ -371,6 +427,18 @@ export const getMaterial = Fn( ( [ materialIndex, materialBuffer ] ) => {
371
427
  subsurfaceRadius: data28.rgb,
372
428
  subsurfaceRadiusScale: data28.a,
373
429
  subsurfaceAnisotropy: data29.r,
430
+ anisotropy: data29.g,
431
+ anisotropyRotation: data29.b,
432
+ anisotropyMapIndex: int( data29.a ),
433
+ transmissionMapIndex: int( data30.r ),
434
+ clearcoatMapIndex: int( data30.g ),
435
+ clearcoatRoughnessMapIndex: int( data30.b ),
436
+ sheenColorMapIndex: int( data30.a ),
437
+ sheenRoughnessMapIndex: int( data31.r ),
438
+ iridescenceMapIndex: int( data31.g ),
439
+ iridescenceThicknessMapIndex: int( data31.b ),
440
+ specularIntensityMapIndex: int( data31.a ),
441
+ specularColorMapIndex: int( data32.r ),
374
442
  albedoMapIndex: int( data8.r ),
375
443
  normalMapIndex: int( data8.g ),
376
444
  roughnessMapIndex: int( data8.b ),
@@ -458,6 +526,18 @@ export const diffuseGroundMaterial = Fn( () => {
458
526
  subsurfaceRadius: vec3( 1.0, 0.2, 0.1 ),
459
527
  subsurfaceRadiusScale: float( 1.0 ),
460
528
  subsurfaceAnisotropy: float( 0.0 ),
529
+ anisotropy: float( 0.0 ),
530
+ anisotropyRotation: float( 0.0 ),
531
+ anisotropyMapIndex: int( - 1 ),
532
+ transmissionMapIndex: int( - 1 ),
533
+ clearcoatMapIndex: int( - 1 ),
534
+ clearcoatRoughnessMapIndex: int( - 1 ),
535
+ sheenColorMapIndex: int( - 1 ),
536
+ sheenRoughnessMapIndex: int( - 1 ),
537
+ iridescenceMapIndex: int( - 1 ),
538
+ iridescenceThicknessMapIndex: int( - 1 ),
539
+ specularIntensityMapIndex: int( - 1 ),
540
+ specularColorMapIndex: int( - 1 ),
461
541
  } );
462
542
 
463
543
  } );
@@ -26,7 +26,7 @@ import {
26
26
  } from 'three/tsl';
27
27
 
28
28
  import { struct } from './patches.js';
29
- import { MIN_PDF, getDatafromStorageBuffer, powerHeuristic, MATERIAL_SLOTS, MATERIAL_SLOT, computeDotProducts } from './Common.js';
29
+ import { MIN_PDF, getDatafromStorageBuffer, powerHeuristic, MATERIAL_SLOTS, MATERIAL_SLOT, computeDotProductsAniso } from './Common.js';
30
30
  import { RandomValue } from './Random.js';
31
31
  import { calculateMaterialPDFFromDots } from './LightsSampling.js';
32
32
  import { evaluateMaterialResponseFromDots } from './MaterialEvaluation.js';
@@ -573,7 +573,7 @@ export const calculateEmissiveTriangleContributionDebug = Fn( ( [
573
573
 
574
574
  // Share H + dot products between BRDF eval and PDF (computeDotProducts
575
575
  // would otherwise run twice with identical inputs).
576
- const dots = DotProducts.wrap( computeDotProducts( normal, viewDir, emissiveSample.direction ) );
576
+ const dots = DotProducts.wrap( computeDotProductsAniso( normal, viewDir, emissiveSample.direction, material ) );
577
577
  const brdfValue = evaluateMaterialResponseFromDots( material, dots );
578
578
  const brdfPdf = calculateMaterialPDFFromDots( material, dots );
579
579
 
@@ -144,8 +144,9 @@ export const traceShadowRay = Fn( ( [
144
144
 
145
145
  If( shadowMaterial.alphaMode.equal( int( 1 ) ), () => {
146
146
 
147
- // MASK mode: binary alpha cutout
148
- const effectiveAlpha = shadowMaterial.color.a.mul( texAlpha );
147
+ // MASK mode: binary alpha cutout. Include opacity (=baseColorFactor.a) so
148
+ // factor-only MASK cuts out; mirrors the BLEND branch and the camera path.
149
+ const effectiveAlpha = shadowMaterial.color.a.mul( texAlpha ).mul( shadowMaterial.opacity );
149
150
  const cutoff = select( shadowMaterial.alphaTest.greaterThan( 0.0 ), shadowMaterial.alphaTest, float( 0.5 ) );
150
151
  If( effectiveAlpha.lessThan( cutoff ), () => {
151
152
 
@@ -25,7 +25,6 @@ import {
25
25
  abs,
26
26
  sqrt,
27
27
  dot,
28
- normalize,
29
28
  If,
30
29
  select,
31
30
  } from 'three/tsl';
@@ -41,7 +40,7 @@ import {
41
40
  EPSILON,
42
41
  MIN_PDF,
43
42
  } from './Common.js';
44
- import { DistributionGGX, calculateVNDFPDF } from './MaterialProperties.js';
43
+ import { DistributionGGX, calculateVNDFPDF, specularVNDFPDFForDir } from './MaterialProperties.js';
45
44
  import { evaluateMaterialResponse } from './MaterialEvaluation.js';
46
45
  import { RandomValue } from './Random.js';
47
46
  import { sampleMicrofacetTransmission, MicrofacetTransmissionResult } from './MaterialTransmission.js';
@@ -342,11 +341,9 @@ export const calculateIndirectLighting = Fn( ( [
342
341
 
343
342
  If( weights.useSpecular, () => {
344
343
 
345
- // Evaluate specular PDF at sampleDir (not brdfSampleDirection which may differ)
346
- const H_spec = normalize( V.add( sampleDir ) );
347
- const NoH_spec = max( dot( N, H_spec ), 0.001 );
348
- const NoV_spec = max( dot( N, V ), 0.001 );
349
- const specPdfAtSampleDir = calculateVNDFPDF( NoH_spec, NoV_spec, material.roughness );
344
+ // Evaluate specular PDF at sampleDir (not brdfSampleDirection which may differ);
345
+ // auto-selects iso/aniso to stay consistent with the BRDF sampler.
346
+ const specPdfAtSampleDir = specularVNDFPDFForDir( material, N, V, sampleDir );
350
347
  combinedPdf.addAssign( weights.specularWeight.mul( specPdfAtSampleDir ) );
351
348
 
352
349
  } );
@@ -72,7 +72,7 @@ import {
72
72
 
73
73
  import { traverseBVHShadow } from './BVHTraversal.js';
74
74
  import { evaluateMaterialResponseFromDots } from './MaterialEvaluation.js';
75
- import { calculateVNDFPDF } from './MaterialProperties.js';
75
+ import { calculateVNDFPDF, calculateVNDFPDFAniso, computeAnisoAlphas } from './MaterialProperties.js';
76
76
  import { RandomValue } from './Random.js';
77
77
  import {
78
78
  selectOptimalMISStrategy,
@@ -81,7 +81,7 @@ import {
81
81
  EPSILON,
82
82
  powerHeuristic,
83
83
  balanceHeuristic,
84
- computeDotProducts,
84
+ computeDotProductsAniso,
85
85
  } from './Common.js';
86
86
  import {
87
87
  sampleEquirectProbability,
@@ -737,7 +737,18 @@ export const calculateMaterialPDFFromDots = Fn( ( [ material, dots ] ) => {
737
737
  If( specularWeight.greaterThan( 0.0 ).and( NoL.greaterThan( 0.0 ) ), () => {
738
738
 
739
739
  const roughness = max( material.roughness, 0.02 );
740
- pdf.addAssign( specularWeight.mul( calculateVNDFPDF( NoH, NoV, roughness ) ) );
740
+ const specPdf = float( 0.0 ).toVar();
741
+ If( material.anisotropy.greaterThan( 0.0 ), () => {
742
+
743
+ const a = computeAnisoAlphas( material.roughness, material.anisotropy );
744
+ specPdf.assign( calculateVNDFPDFAniso( a.x, a.y, dots.NoH, dots.ToH, dots.BoH, dots.NoV, dots.ToV, dots.BoV ) );
745
+
746
+ } ).Else( () => {
747
+
748
+ specPdf.assign( calculateVNDFPDF( NoH, NoV, roughness ) );
749
+
750
+ } );
751
+ pdf.addAssign( specularWeight.mul( specPdf ) );
741
752
 
742
753
  } );
743
754
 
@@ -751,7 +762,7 @@ export const calculateMaterialPDFFromDots = Fn( ( [ material, dots ] ) => {
751
762
  // already have dots; otherwise prefer calculateMaterialPDFFromDots.
752
763
  export const calculateMaterialPDF = Fn( ( [ viewDir, lightDir, normal, material ] ) => {
753
764
 
754
- const dots = DotProducts.wrap( computeDotProducts( normal, viewDir, lightDir ) );
765
+ const dots = DotProducts.wrap( computeDotProductsAniso( normal, viewDir, lightDir, material ) );
755
766
  return calculateMaterialPDFFromDots( material, dots );
756
767
 
757
768
  } );
@@ -985,7 +996,7 @@ export const calculateDirectLightingUnified = Fn( ( [
985
996
 
986
997
  // Share H + dot products between BRDF eval and PDF — otherwise each
987
998
  // would recompute normalize(V+L) + 5 dot products independently.
988
- const sharedDots = DotProducts.wrap( computeDotProducts( hitNormal, viewDir, lightSample.direction ) );
999
+ const sharedDots = DotProducts.wrap( computeDotProductsAniso( hitNormal, viewDir, lightSample.direction, material ) );
989
1000
  const brdfValue = evaluateMaterialResponseFromDots( material, sharedDots );
990
1001
  const bPdf = calculateMaterialPDFFromDots( material, sharedDots ).toVar();
991
1002
 
@@ -1187,7 +1198,7 @@ export const calculateDirectLightingUnified = Fn( ( [
1187
1198
 
1188
1199
  // Share H + dots between env BRDF/PDF — same redundancy fix as the
1189
1200
  // discrete-light path above.
1190
- const envDots = DotProducts.wrap( computeDotProducts( hitNormal, viewDir, envDirection ) );
1201
+ const envDots = DotProducts.wrap( computeDotProductsAniso( hitNormal, viewDir, envDirection, material ) );
1191
1202
  const brdfValue = evaluateMaterialResponseFromDots( material, envDots );
1192
1203
  const bPdf = calculateMaterialPDFFromDots( material, envDots ).toVar();
1193
1204
 
@@ -6,11 +6,12 @@ import {
6
6
  import { DotProducts, DFGResult } from './Struct.js';
7
7
  import {
8
8
  PI, PI_INV, EPSILON, MIN_CLEARCOAT_ROUGHNESS,
9
- computeDotProducts,
9
+ computeDotProductsAniso,
10
10
  } from './Common.js';
11
11
  import { fresnelSchlick, fresnelSchlickFloat, dielectricF0 } from './Fresnel.js';
12
12
  import {
13
13
  DistributionGGX, SheenDistribution, GeometrySmith, evaluateDFG,
14
+ computeAnisoAlphas, DistributionGGXAniso, VisibilityGGXAniso,
14
15
  } from './MaterialProperties.js';
15
16
  import { evalIridescence } from './MaterialProperties.js';
16
17
 
@@ -75,12 +76,25 @@ export const evaluateMaterialResponseFromDots = Fn( ( [ material, dots ] ) => {
75
76
  } );
76
77
 
77
78
  // Precalculate shared terms
78
- const D = DistributionGGX( dots.NoH, material.roughness );
79
- const G = GeometrySmith( dots.NoV, dots.NoL, material.roughness );
80
79
  const F = fresnelSchlick( dots.VoH, F0 );
81
80
 
82
- // Single-scatter specular BRDF
83
- const specularSS = D.mul( G ).mul( F ).div( max( float( 4.0 ).mul( dots.NoV ).mul( dots.NoL ), EPSILON ) );
81
+ // Single-scatter specular BRDF (anisotropic when material.anisotropy > 0; the aniso
82
+ // visibility term already carries the 1/(4·NoV·NoL) denominator)
83
+ const specularSS = vec3( 0.0 ).toVar();
84
+ If( material.anisotropy.greaterThan( 0.0 ), () => {
85
+
86
+ const a = computeAnisoAlphas( material.roughness, material.anisotropy );
87
+ const Da = DistributionGGXAniso( a.x, a.y, dots.NoH, dots.ToH, dots.BoH );
88
+ const Va = VisibilityGGXAniso( a.x, a.y, dots.ToV, dots.BoV, dots.ToL, dots.BoL, dots.NoV, dots.NoL );
89
+ specularSS.assign( F.mul( Da.mul( Va ) ) );
90
+
91
+ } ).Else( () => {
92
+
93
+ const D = DistributionGGX( dots.NoH, material.roughness );
94
+ const G = GeometrySmith( dots.NoV, dots.NoL, material.roughness );
95
+ specularSS.assign( D.mul( G ).mul( F ).div( max( float( 4.0 ).mul( dots.NoV ).mul( dots.NoL ), EPSILON ) ) );
96
+
97
+ } );
84
98
 
85
99
  // Shared DFG evaluation — compensation factor and total directional albedo
86
100
  // come from the same polynomial.
@@ -123,7 +137,7 @@ export const evaluateMaterialResponseFromDots = Fn( ( [ material, dots ] ) => {
123
137
  // have dots; otherwise prefer evaluateMaterialResponseFromDots to share the work.
124
138
  export const evaluateMaterialResponse = Fn( ( [ V, L, N, material ] ) => {
125
139
 
126
- const dots = DotProducts.wrap( computeDotProducts( N, V, L ) );
140
+ const dots = DotProducts.wrap( computeDotProductsAniso( N, V, L, material ) );
127
141
  return evaluateMaterialResponseFromDots( material, dots );
128
142
 
129
143
  } );
@@ -143,10 +157,24 @@ export const evaluateLayeredBRDF = Fn( ( [ dots, material ] ) => {
143
157
  vec3( 0.0 ), vec3( 1.0 )
144
158
  ).toVar();
145
159
 
146
- const D = DistributionGGX( dots.NoH, material.roughness );
147
- const G = GeometrySmith( dots.NoV, dots.NoL, material.roughness );
148
160
  const F = fresnelSchlick( dots.VoH, F0 );
149
- const baseBRDFSS = D.mul( G ).mul( F ).div( max( float( 4.0 ).mul( dots.NoV ).mul( dots.NoL ), EPSILON ) );
161
+
162
+ // Base specular (anisotropic when anisotropy > 0; aniso V term carries 1/(4·NoV·NoL))
163
+ const baseBRDFSS = vec3( 0.0 ).toVar();
164
+ If( material.anisotropy.greaterThan( 0.0 ), () => {
165
+
166
+ const a = computeAnisoAlphas( material.roughness, material.anisotropy );
167
+ const Da = DistributionGGXAniso( a.x, a.y, dots.NoH, dots.ToH, dots.BoH );
168
+ const Va = VisibilityGGXAniso( a.x, a.y, dots.ToV, dots.BoV, dots.ToL, dots.BoL, dots.NoV, dots.NoL );
169
+ baseBRDFSS.assign( F.mul( Da.mul( Va ) ) );
170
+
171
+ } ).Else( () => {
172
+
173
+ const D = DistributionGGX( dots.NoH, material.roughness );
174
+ const G = GeometrySmith( dots.NoV, dots.NoL, material.roughness );
175
+ baseBRDFSS.assign( D.mul( G ).mul( F ).div( max( float( 4.0 ).mul( dots.NoV ).mul( dots.NoL ), EPSILON ) ) );
176
+
177
+ } );
150
178
 
151
179
  // Shared DFG evaluation — compensation factor and total directional albedo
152
180
  // come from the same polynomial.
@@ -1,16 +1,17 @@
1
- import { Fn, float, vec3, int, If, dot, max, min, sqrt, cos, exp, mix, clamp, smoothstep } from 'three/tsl';
1
+ import { Fn, float, vec2, vec3, int, If, dot, max, min, sqrt, cos, exp, mix, clamp, smoothstep } from 'three/tsl';
2
2
 
3
3
  import {
4
4
  BRDFWeights,
5
5
  MaterialCache,
6
6
  ImportanceSamplingInfo,
7
7
  DFGResult,
8
+ DotProducts,
8
9
 
9
10
  } from './Struct.js';
10
11
 
11
12
  import {
12
13
  PI, TWO_PI, EPSILON, MIN_ROUGHNESS,
13
- XYZ_TO_REC709, square,
14
+ XYZ_TO_REC709, square, computeDotProductsAniso,
14
15
  } from './Common.js';
15
16
 
16
17
  import {
@@ -124,6 +125,81 @@ export const calculateVNDFPDF = Fn( ( [ NoH, NoV, roughness ] ) => {
124
125
 
125
126
  } );
126
127
 
128
+ // -----------------------------------------------------------------------------
129
+ // Anisotropic GGX (Filament / three.js convention — matches WebGLRenderer/glTF)
130
+ // -----------------------------------------------------------------------------
131
+ // alphaB = roughness² (bitangent axis), alphaT = mix(roughness², 1, anisotropy²) (tangent axis).
132
+ // Returns vec2( alphaT, alphaB ). Both floored so smooth anisotropic surfaces stay stable.
133
+ export const computeAnisoAlphas = Fn( ( [ roughness, anisotropy ] ) => {
134
+
135
+ const alphaB = max( roughness.mul( roughness ), 1e-4 ).toVar();
136
+ const alphaT = max( mix( alphaB, float( 1.0 ), anisotropy.mul( anisotropy ) ), 1e-4 );
137
+ return vec2( alphaT, alphaB );
138
+
139
+ } );
140
+
141
+ // Anisotropic GGX normal distribution (Filament D_GGX_Anisotropic). Reduces exactly to
142
+ // DistributionGGX when alphaT == alphaB.
143
+ export const DistributionGGXAniso = Fn( ( [ alphaT, alphaB, NoH, ToH, BoH ] ) => {
144
+
145
+ const a2 = alphaT.mul( alphaB );
146
+ const v = vec3( alphaB.mul( ToH ), alphaT.mul( BoH ), a2.mul( NoH ) );
147
+ const v2 = max( dot( v, v ), EPSILON );
148
+ const w2 = a2.div( v2 );
149
+ return a2.mul( w2.mul( w2 ) ).div( PI );
150
+
151
+ } );
152
+
153
+ // Anisotropic Smith height-correlated visibility (three.js V_GGX_SmithCorrelated_Anisotropic).
154
+ // Already includes the 1/(4·NoV·NoL) denominator — multiply directly by D and F.
155
+ export const VisibilityGGXAniso = Fn( ( [ alphaT, alphaB, ToV, BoV, ToL, BoL, NoV, NoL ] ) => {
156
+
157
+ const gv = NoL.mul( vec3( alphaT.mul( ToV ), alphaB.mul( BoV ), NoV ).length() );
158
+ const gl = NoV.mul( vec3( alphaT.mul( ToL ), alphaB.mul( BoL ), NoL ).length() );
159
+ return float( 0.5 ).div( max( gv.add( gl ), EPSILON ) );
160
+
161
+ } );
162
+
163
+ // Anisotropic Smith masking G1 (for the VNDF pdf). G1 = 1 / (1 + Λ).
164
+ export const smithG1Aniso = Fn( ( [ alphaT, alphaB, ToV, BoV, NoV ] ) => {
165
+
166
+ const t = alphaT.mul( ToV );
167
+ const b = alphaB.mul( BoV );
168
+ const num = t.mul( t ).add( b.mul( b ) );
169
+ const nov2 = max( NoV.mul( NoV ), EPSILON );
170
+ const lambda = sqrt( float( 1.0 ).add( num.div( nov2 ) ) ).sub( 1.0 ).mul( 0.5 );
171
+ return float( 1.0 ).div( lambda.add( 1.0 ) );
172
+
173
+ } );
174
+
175
+ // Anisotropic VNDF pdf over the reflected direction: G1(V)·D(H) / (4·NoV).
176
+ export const calculateVNDFPDFAniso = Fn( ( [ alphaT, alphaB, NoH, ToH, BoH, NoV, ToV, BoV ] ) => {
177
+
178
+ const D = DistributionGGXAniso( alphaT, alphaB, NoH, ToH, BoH );
179
+ const G1 = smithG1Aniso( alphaT, alphaB, ToV, BoV, NoV );
180
+ return D.mul( G1 ).div( max( NoV.mul( 4.0 ), EPSILON ) );
181
+
182
+ } );
183
+
184
+ // Specular VNDF pdf (iso/aniso auto-select) for MIS sites that only have (N, V, L).
185
+ export const specularVNDFPDFForDir = Fn( ( [ material, N, V, L ] ) => {
186
+
187
+ const dots = DotProducts.wrap( computeDotProductsAniso( N, V, L, material ) );
188
+ const pdf = float( 0.0 ).toVar();
189
+ If( material.anisotropy.greaterThan( 0.0 ), () => {
190
+
191
+ const a = computeAnisoAlphas( material.roughness, material.anisotropy );
192
+ pdf.assign( calculateVNDFPDFAniso( a.x, a.y, dots.NoH, dots.ToH, dots.BoH, dots.NoV, dots.ToV, dots.BoV ) );
193
+
194
+ } ).Else( () => {
195
+
196
+ pdf.assign( calculateVNDFPDF( dots.NoH, dots.NoV, max( material.roughness, 0.02 ) ) );
197
+
198
+ } );
199
+ return pdf;
200
+
201
+ } );
202
+
127
203
  // -----------------------------------------------------------------------------
128
204
  // Iridescence Evaluation
129
205
  // -----------------------------------------------------------------------------
@@ -74,3 +74,22 @@ export const sampleGGXVNDF = /*@__PURE__*/ wgslFn( `
74
74
  return normalize( vec3f( alpha * Nh.x, alpha * Nh.y, max( 0.0f, Nh.z ) ) );
75
75
  }
76
76
  ` );
77
+
78
+ // Anisotropic VNDF: identical to sampleGGXVNDF but stretches x/y by separate alphas.
79
+ // V is in the anisotropy tangent frame (x=tangent, y=bitangent, z=normal); returns local H.
80
+ export const sampleGGXVNDFAniso = /*@__PURE__*/ wgslFn( `
81
+ fn sampleGGXVNDFAniso( V: vec3f, alphaX: f32, alphaY: f32, Xi: vec2f ) -> vec3f {
82
+ let Vh = normalize( vec3f( alphaX * V.x, alphaY * V.y, V.z ) );
83
+ let lensq = Vh.x * Vh.x + Vh.y * Vh.y;
84
+ let T1 = select( vec3f( 1.0f, 0.0f, 0.0f ), vec3f( -Vh.y, Vh.x, 0.0f ) / sqrt( lensq ), lensq > 1e-8f );
85
+ let T2 = cross( Vh, T1 );
86
+ let r = sqrt( Xi.x );
87
+ let phi = 6.28318530717958647692f * Xi.y;
88
+ let t1 = r * cos( phi );
89
+ let t2tmp = r * sin( phi );
90
+ let s = 0.5f * ( 1.0f + Vh.z );
91
+ let t2 = ( 1.0f - s ) * sqrt( 1.0f - t1 * t1 ) + s * t2tmp;
92
+ let Nh = T1 * t1 + T2 * t2 + Vh * sqrt( max( 0.0f, 1.0f - t1 * t1 - t2 * t2 ) );
93
+ return normalize( vec3f( alphaX * Nh.x, alphaY * Nh.y, max( 0.0f, Nh.z ) ) );
94
+ }
95
+ ` );
@@ -546,7 +546,9 @@ export const handleMaterialTransparency = Fn( ( [
546
546
 
547
547
  const cutoff = select( material.alphaTest.greaterThan( 0.0 ), material.alphaTest, float( 0.5 ) );
548
548
 
549
- If( material.color.a.lessThan( cutoff ), () => {
549
+ // Effective alpha = textureAlpha (in color.a) × baseColorFactor.a (opacity).
550
+ // color.a base is 1.0, so factor-only MASK (no albedo map) relies on opacity.
551
+ If( material.color.a.mul( material.opacity ).lessThan( cutoff ), () => {
550
552
 
551
553
  result.continueRay.assign( true );
552
554
  result.direction.assign( ray.direction );
@@ -33,15 +33,18 @@ import {
33
33
  MIN_CLEARCOAT_ROUGHNESS,
34
34
  MIN_PDF,
35
35
  constructTBN,
36
+ anisoTangentFrame,
36
37
  calculateFireflyThreshold,
37
38
  applySoftSuppressionRGB,
38
39
  } from './Common.js';
39
- import { DirectionSample, MaterialCache } from './Struct.js';
40
+ import { AnisoFrame, DirectionSample, MaterialCache } from './Struct.js';
40
41
  import { RandomValue } from './Random.js';
41
42
  import { sampleMicrofacetTransmission, MicrofacetTransmissionResult } from './MaterialTransmission.js';
42
43
  import {
43
44
  SheenDistribution,
44
45
  calculateVNDFPDF,
46
+ calculateVNDFPDFAniso,
47
+ computeAnisoAlphas,
45
48
  calculateBRDFWeights,
46
49
  } from './MaterialProperties.js';
47
50
  import { evaluateMaterialResponse } from './MaterialEvaluation.js';
@@ -50,6 +53,7 @@ import {
50
53
  ImportanceSampleCosine,
51
54
  ImportanceSampleGGX,
52
55
  sampleGGXVNDF,
56
+ sampleGGXVNDFAniso,
53
57
  } from './MaterialSampling.js';
54
58
 
55
59
  // =============================================================================
@@ -125,18 +129,39 @@ export const generateSampledDirection = Fn( ( [
125
129
 
126
130
  } ).ElseIf( rand.lessThan( cumulativeSpecular ), () => {
127
131
 
128
- const TBN = constructTBN( { N } );
129
- const localV = TBN.transpose().mul( V );
132
+ If( material.anisotropy.greaterThan( 0.0 ), () => {
130
133
 
131
- // VNDF sampling
132
- const localH = sampleGGXVNDF( { V: localV, roughness: material.roughness, Xi: xi } );
133
- H.assign( TBN.mul( localH ) );
134
+ // Shared frame → sampler and eval/PDF stay bit-identical (MIS consistency)
135
+ const f = AnisoFrame.wrap( anisoTangentFrame( N, material.anisotropyRotation ) );
136
+ const Ta = f.Ta;
137
+ const Ba = f.Ba;
134
138
 
135
- const NoH = clamp( dot( N, H ), 0.001, 1.0 );
139
+ const localV = vec3( dot( V, Ta ), dot( V, Ba ), dot( V, N ) );
140
+ const a = computeAnisoAlphas( material.roughness, material.anisotropy );
141
+ const localH = sampleGGXVNDFAniso( { V: localV, alphaX: a.x, alphaY: a.y, Xi: xi } );
142
+ H.assign( Ta.mul( localH.x ).add( Ba.mul( localH.y ) ).add( N.mul( localH.z ) ) );
136
143
 
137
- resultDirection.assign( reflect( V.negate(), H ) );
138
- resultPdf.assign( calculateVNDFPDF( NoH, NoV, material.roughness ) );
139
- resultValue.assign( evaluateMaterialResponse( V, resultDirection, N, material ) );
144
+ const NoH = clamp( dot( N, H ), 0.001, 1.0 );
145
+ resultDirection.assign( reflect( V.negate(), H ) );
146
+ resultPdf.assign( calculateVNDFPDFAniso( a.x, a.y, NoH, dot( Ta, H ), dot( Ba, H ), NoV, dot( Ta, V ), dot( Ba, V ) ) );
147
+ resultValue.assign( evaluateMaterialResponse( V, resultDirection, N, material ) );
148
+
149
+ } ).Else( () => {
150
+
151
+ const TBN = constructTBN( { N } );
152
+ const localV = TBN.transpose().mul( V );
153
+
154
+ // VNDF sampling
155
+ const localH = sampleGGXVNDF( { V: localV, roughness: material.roughness, Xi: xi } );
156
+ H.assign( TBN.mul( localH ) );
157
+
158
+ const NoH = clamp( dot( N, H ), 0.001, 1.0 );
159
+
160
+ resultDirection.assign( reflect( V.negate(), H ) );
161
+ resultPdf.assign( calculateVNDFPDF( NoH, NoV, material.roughness ) );
162
+ resultValue.assign( evaluateMaterialResponse( V, resultDirection, N, material ) );
163
+
164
+ } );
140
165
 
141
166
  } ).ElseIf( rand.lessThan( cumulativeSheen ), () => {
142
167
 
@@ -17,7 +17,7 @@ import {
17
17
  import { sampleEnvironment, sampleEquirectProbability, sampleEquirect, groundProjectedEnvDir } from './Environment.js';
18
18
  import { getMaterial, powerHeuristic, balanceHeuristic, classifyMaterial, REC709_LUMINANCE_COEFFICIENTS, PI_INV, EPSILON, diffuseGroundMaterial } from './Common.js';
19
19
  import { cosineWeightedSample } from './MaterialSampling.js';
20
- import { sampleAllMaterialTextures } from './TextureSampling.js';
20
+ import { sampleAllMaterialTextures, processAnisotropyMap, applyExtensionMaps, getTransformedUV } from './TextureSampling.js';
21
21
  import { evaluateMaterialResponse } from './MaterialEvaluation.js';
22
22
  import { calculateDirectLightingUnified, calculateMaterialPDF } from './LightsSampling.js';
23
23
  import { traceShadowRay, calculateRayOffset } from './LightsDirect.js';
@@ -37,6 +37,7 @@ import {
37
37
  HitInfo,
38
38
  RayTracingMaterial,
39
39
  MaterialSamples,
40
+ ExtMapResult,
40
41
  DirectionSample,
41
42
  ImportanceSamplingInfo,
42
43
  MaterialClassification,
@@ -549,6 +550,35 @@ export function buildShadeKernel( params ) {
549
550
  material.roughness.assign( matSamples.roughness.clamp( 0.05, 1.0 ) );
550
551
  material.sheenRoughness.assign( material.sheenRoughness.clamp( 0.05, 1.0 ) ); // megakernel parity (PathTracerCore.js:1060): sample/PDF mismatch at sheenRoughness~0
551
552
 
553
+ // Anisotropy + extension maps carry no per-slot transform of their own, so reuse the material's
554
+ // albedo UV-transform (KHR_texture_transform). Extension/aniso textures nearly always share the
555
+ // base UV set + transform, so this aligns tiled/offset maps (e.g. SheenCloth's 30× sheen weave)
556
+ // instead of sampling at raw 1× UV. Identity albedo transform → same as raw UV (safe no-op).
557
+ const extUV = getTransformedUV( { uv: samplingUV, transform: material.albedoTransform } ).toVar();
558
+
559
+ // Fold the anisotropy texture (if any) into the scalar anisotropy/rotation used by the BRDF
560
+ If( material.anisotropyMapIndex.greaterThanEqual( int( 0 ) ), () => {
561
+
562
+ const aniso = processAnisotropyMap( material, extUV ).toVar();
563
+ material.anisotropy.assign( aniso.x );
564
+ material.anisotropyRotation.assign( aniso.y );
565
+
566
+ } );
567
+
568
+ // Fold the glTF extension textures (transmission/clearcoat/sheen/iridescence/specular) into
569
+ // their scalar factors. `material` is the shared mutable struct that flows into BOTH the
570
+ // BRDF-sample path and the NEE evaluator, so modulating here covers every consumer at once.
571
+ const extMaps = ExtMapResult.wrap( applyExtensionMaps( material, extUV ) ).toVar();
572
+ material.transmission.assign( extMaps.transmission );
573
+ material.clearcoat.assign( extMaps.clearcoat );
574
+ material.clearcoatRoughness.assign( extMaps.clearcoatRoughness );
575
+ material.sheenColor.assign( extMaps.sheenColor );
576
+ material.sheenRoughness.assign( extMaps.sheenRoughness );
577
+ material.iridescence.assign( extMaps.iridescence );
578
+ material.iridescenceThicknessRange.assign( vec2( material.iridescenceThicknessRange.x, extMaps.iridescenceThickness ) );
579
+ material.specularIntensity.assign( extMaps.specularIntensity );
580
+ material.specularColor.assign( extMaps.specularColor );
581
+
552
582
  const albedo = matSamples.albedo.toVar();
553
583
  If(
554
584
  material.displacementMapIndex.greaterThanEqual( int( 0 ) )
package/src/TSL/Struct.js CHANGED
@@ -55,6 +55,33 @@ export const RayTracingMaterial = struct( {
55
55
  subsurfaceRadius: 'vec3', // per-channel mean free path
56
56
  subsurfaceRadiusScale: 'float', // scalar multiplier on radius
57
57
  subsurfaceAnisotropy: 'float', // Henyey-Greenstein g (-1..1)
58
+ anisotropy: 'float', // surface specular anisotropy strength (0..1)
59
+ anisotropyRotation: 'float', // anisotropy tangent rotation (radians)
60
+ anisotropyMapIndex: 'int', // packed linear-bucket index, -1 if none
61
+ // Extension-texture map indices (packed bucket index, -1 if none). Fold applied in ShadeKernel.
62
+ transmissionMapIndex: 'int',
63
+ clearcoatMapIndex: 'int',
64
+ clearcoatRoughnessMapIndex: 'int',
65
+ sheenColorMapIndex: 'int',
66
+ sheenRoughnessMapIndex: 'int',
67
+ iridescenceMapIndex: 'int',
68
+ iridescenceThicknessMapIndex: 'int',
69
+ specularIntensityMapIndex: 'int',
70
+ specularColorMapIndex: 'int',
71
+ } );
72
+
73
+ // Result of folding the glTF extension textures into their scalar factors (applyExtensionMaps).
74
+ // Each field is the material's scalar × the sampled map channel (or the scalar unchanged if no map).
75
+ export const ExtMapResult = struct( {
76
+ transmission: 'float',
77
+ clearcoat: 'float',
78
+ clearcoatRoughness: 'float',
79
+ sheenColor: 'vec3',
80
+ sheenRoughness: 'float',
81
+ iridescence: 'float',
82
+ iridescenceThickness: 'float', // resolved thin-film thickness → written into iridescenceThicknessRange.y
83
+ specularIntensity: 'float',
84
+ specularColor: 'vec3',
58
85
  } );
59
86
 
60
87
  // Lightweight material for shadow ray evaluation — only the fields needed
@@ -142,12 +169,24 @@ export const ImportanceSamplingInfo = struct( {
142
169
  clearcoatImportance: 'float',
143
170
  } );
144
171
 
172
+ // Anisotropy tangent frame in world space (rotated ONB). Shared by the sampler and
173
+ // the eval/PDF so both derive the identical frame — see anisoTangentFrame().
174
+ export const AnisoFrame = struct( {
175
+ Ta: 'vec3', // anisotropy tangent
176
+ Ba: 'vec3', // anisotropy bitangent
177
+ } );
178
+
145
179
  export const DotProducts = struct( {
146
180
  NoL: 'float', // Normal • Light
147
181
  NoV: 'float', // Normal • View
148
182
  NoH: 'float', // Normal • Half
149
183
  VoH: 'float', // View • Half
150
184
  LoH: 'float', // Light • Half
185
+ // Anisotropy tangent-frame projections (0 unless computed via computeDotProductsAniso).
186
+ // T = anisotropy tangent, B = anisotropy bitangent.
187
+ ToH: 'float', BoH: 'float',
188
+ ToV: 'float', BoV: 'float',
189
+ ToL: 'float', BoL: 'float',
151
190
  } );
152
191
 
153
192
  // Kulla-Conty DFG approximation outputs (computed once, consumed by both