Skip to content

Commit 6b40e53

Browse files
committed
feat(cloud-picture): fix "double side" glsl
fix get_hit_shading_data.glsl: fix revert nw; add revert tw, bw;
1 parent a14a6cd commit 6b40e53

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

src/run/domain_layer/domain/shader/ray_tracing/get_hit_shading_data.glsl

+17-20
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,12 @@ vec2 _computeUVByWrapData(vec2 wrapData, vec2 uv) {
199199
// return uv;
200200
}
201201

202-
bool _isBackFace(vec3 rayDir, vec3 normal) {
202+
bool _isFrontFace(vec3 rayDir, vec3 normal) {
203203
return dot(rayDir, normal) <= 0.0;
204204
}
205205

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);
216208
}
217209

218210
HitShadingData getHitShadingData(uint instanceIndex, uint primitiveIndex) {
@@ -241,16 +233,21 @@ HitShadingData getHitShadingData(uint instanceIndex, uint primitiveIndex) {
241233
vec3 no = _blerp(attribs.xy, n0.xyz, n1.xyz, n2.xyz);
242234
const vec3 ta = _blerp(attribs.xy, t0.xyz, t1.xyz, t2.xyz);
243235

244-
BSDFMaterial mat = _getMaterial(materialIndex);
236+
mat3 normalMatrix = _getNormalMatrix(instanceData);
245237

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);
248241

249-
mat3 normalMatrix = _getNormalMatrix(instanceData);
242+
BSDFMaterial mat = _getMaterial(materialIndex);
250243

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+
}
254251

255252
uint diffuseMapLayerIndex = uint(mat.diffuseMapLayerIndex);
256253
uint normalMapLayerIndex = uint(mat.normalMapLayerIndex);
@@ -299,7 +296,7 @@ HitShadingData getHitShadingData(uint instanceIndex, uint primitiveIndex) {
299296
}
300297

301298
if (_hasMap(normalMapLayerIndex)) {
302-
data.worldNormal =
299+
data.worldNormal = normalize(
303300
mat3(tw, bw, nw) *
304301
normalize(
305302
(texture(sampler2DArray(textureArray, textureSampler),
@@ -309,7 +306,7 @@ HitShadingData getHitShadingData(uint instanceIndex, uint primitiveIndex) {
309306
.rgb) *
310307
2.0 -
311308
1.0)
312-
.xyz;
309+
.xyz);
313310
} else {
314311
data.worldNormal = nw;
315312
}

0 commit comments

Comments
 (0)