@@ -199,20 +199,12 @@ vec2 _computeUVByWrapData(vec2 wrapData, vec2 uv) {
199
199
// return uv;
200
200
}
201
201
202
- bool _isBackFace (vec3 rayDir, vec3 normal) {
202
+ bool _isFrontFace (vec3 rayDir, vec3 normal) {
203
203
return dot (rayDir, normal) <= 0.0 ;
204
204
}
205
205
206
- vec3 _revertNormal(vec3 normal, vec3 rayDir) {
207
- return normal * (float (_isBackFace(rayDir, normal))) * 2.0 - 1.0 ;
208
- }
209
-
210
- vec3 _handleNormalForDoubleSide(vec3 normal, vec3 rayDir, bool isDoubleSide) {
211
- if (isDoubleSide) {
212
- return _revertNormal(normal, rayDir);
213
- }
214
-
215
- return normal;
206
+ vec3 _revertForBackFace(vec3 normalRelatedData, bool isFrontFace) {
207
+ return normalRelatedData * (float (isFrontFace) * 2.0 - 1.0 );
216
208
}
217
209
218
210
HitShadingData getHitShadingData(uint instanceIndex, uint primitiveIndex) {
@@ -241,16 +233,21 @@ HitShadingData getHitShadingData(uint instanceIndex, uint primitiveIndex) {
241
233
vec3 no = _blerp(attribs.xy, n0.xyz, n1.xyz, n2.xyz);
242
234
const vec3 ta = _blerp(attribs.xy, t0.xyz, t1.xyz, t2.xyz);
243
235
244
- BSDFMaterial mat = _getMaterial(materialIndex );
236
+ mat3 normalMatrix = _getNormalMatrix(instanceData );
245
237
246
- no = _handleNormalForDoubleSide(no, gl_WorldRayDirectionEXT,
247
- bool (mat.isDoubleSide));
238
+ vec3 nw = normalize (normalMatrix * no);
239
+ vec3 tw = normalize (normalMatrix * ta);
240
+ vec3 bw = cross (nw, tw);
248
241
249
- mat3 normalMatrix = _getNormalMatrix(instanceData );
242
+ BSDFMaterial mat = _getMaterial(materialIndex );
250
243
251
- const vec3 nw = normalize (normalMatrix * no);
252
- const vec3 tw = normalize (normalMatrix * ta);
253
- const vec3 bw = cross (nw, tw);
244
+ if (bool (mat.isDoubleSide)) {
245
+ bool isFrontFace = _isFrontFace(gl_WorldRayDirectionEXT, nw);
246
+
247
+ nw = _revertForBackFace(nw, isFrontFace);
248
+ tw = _revertForBackFace(tw, isFrontFace);
249
+ bw = _revertForBackFace(bw, isFrontFace);
250
+ }
254
251
255
252
uint diffuseMapLayerIndex = uint (mat.diffuseMapLayerIndex);
256
253
uint normalMapLayerIndex = uint (mat.normalMapLayerIndex);
@@ -299,7 +296,7 @@ HitShadingData getHitShadingData(uint instanceIndex, uint primitiveIndex) {
299
296
}
300
297
301
298
if (_hasMap(normalMapLayerIndex)) {
302
- data.worldNormal =
299
+ data.worldNormal = normalize (
303
300
mat3 (tw, bw, nw) *
304
301
normalize (
305
302
(texture(sampler2DArray (textureArray, textureSampler),
@@ -309,7 +306,7 @@ HitShadingData getHitShadingData(uint instanceIndex, uint primitiveIndex) {
309
306
.rgb) *
310
307
2.0 -
311
308
1.0 )
312
- .xyz;
309
+ .xyz) ;
313
310
} else {
314
311
data.worldNormal = nw;
315
312
}
0 commit comments