Skip to content

Commit 1e02e8a

Browse files
committed
Simplify part of the code, increase samples.
1 parent bba41e5 commit 1e02e8a

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

base/glprogs/interaction.glprog

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ glprog interaction
119119
uniform sampler2D lightImage;
120120
uniform sampler2D diffuseImage;
121121
uniform sampler2D specularImage;
122-
uniform sampler2D specularTableImage;
123122
uniform sampler2D shadowMaps[6];
124123
uniform float maxPOMDistance; // Add this uniform to control POM distance
125124

@@ -161,8 +160,7 @@ glprog interaction
161160
vec2(-0.24188840, 0.99706507),
162161
vec2(-0.81409955, 0.91437590),
163162
vec2(0.19984126, 0.78641367),
164-
vec2(0.14383161, -0.14100790)
165-
);
163+
vec2(0.14383161, -0.14100790));
166164

167165
// Generate a random rotation based on screen position to reduce banding
168166
float screenSize = 1024.0;
@@ -176,15 +174,14 @@ glprog interaction
176174

177175
float factor = 0.0;
178176

179-
// Could increase this to 16 or 32 for better quality
180-
int samples = 8;
177+
// Could increase this to 16 or 32 for better quality
178+
int samples = 16;
181179

182180
for (int i = 0; i < samples; i++) {
183181
// Rotate the sample position
184182
vec2 rotatedOffset = vec2(
185183
poissonDisk[i].x * c - poissonDisk[i].y * s,
186-
poissonDisk[i].x * s + poissonDisk[i].y * c
187-
);
184+
poissonDisk[i].x * s + poissonDisk[i].y * c);
188185

189186
// Apply scaled offset
190187
vec2 sampleCoord = coord.xy + rotatedOffset * filterSize;
@@ -211,25 +208,30 @@ glprog interaction
211208
float viewDistance = length(lightVector);
212209
float pomQualityFactor = clamp(1.0 - viewDistance / maxPOMDistance, 0.0, 1.0);
213210

211+
// Only perform POM if the quality factor is significant
214212
if (pomQualityFactor > 0.01) {
215-
// Only do POM if we're close enough
216-
float numLayers = mix(8.0, 32.0, pomQualityFactor); // Fewer layers for distant objects
217-
float layerHeight = 0.01 * pomQualityFactor; // Less height effect for distant objects
213+
// Fewer layers for distant objects
214+
float numLayers = mix(5.0, 16.0, pomQualityFactor);
215+
// Less height effect for distant objects
216+
float layerHeight = 0.01 * pomQualityFactor;
218217

219218
// Initialize parallax mapping
220219
float layerDepth = 1.0 / numLayers;
221-
vec2 P = V.xy * layerHeight / max(V.z, 0.05); // Prevent division by near-zero
220+
// Prevent division by near-zero, use a small epsilon
221+
vec2 P = V.xy * layerHeight / max(V.z, 0.05);
222222
float currentLayerDepth = 0.0;
223223
vec2 deltaTexCoords = P / numLayers;
224224

225-
// Binary search refinement instead of linear search
226-
// First do a coarse linear search
227-
float numSearches = 8.0; // Fewer iterations for better performance
228-
for (int i = 0; i < int(numSearches); i++) {
229-
float height = normalize(texture2D(bumpImage, bumpedST).xyz * 2.0 - 1.0).z;
225+
int numSearches = int(mix(4.0, 8.0, pomQualityFactor));
226+
for (int i = 0; i < numSearches; i++) {
227+
// Sample height from the normal map's alpha/blue channel (assuming height is stored there)
228+
// Or derive from normal length if appropriate for your encoding
229+
float height = texture(bumpImage, bumpedST).b;
230+
230231
if (currentLayerDepth >= height) {
231232
break;
232233
}
234+
233235
bumpedST -= deltaTexCoords;
234236
diffuseST -= deltaTexCoords;
235237
specularST -= deltaTexCoords;
@@ -243,28 +245,30 @@ glprog interaction
243245
vec2 prevSpecular = specularST + deltaTexCoords;
244246

245247
// Binary search to refine the depth
246-
for (int i = 0; i < 4; i++) { // Just a few iterations
248+
for (int i = 0; i < 4; i++) {
247249
deltaTexCoords *= 0.5;
248250
currentLayerDepth -= layerDepth * 0.5;
249251

250-
float height = normalize(texture2D(bumpImage, bumpedST).xyz * 2.0 - 1.0).z;
252+
float height = texture(bumpImage, bumpedST).b;
251253

254+
// Check if current depth is below or above the surface
252255
if (currentLayerDepth > height) {
253-
bumpedST -= deltaTexCoords;
254-
diffuseST -= deltaTexCoords;
255-
specularST -= deltaTexCoords;
256-
currentLayerDepth += layerDepth * 0.5;
257-
} else {
258256
bumpedST += deltaTexCoords;
259257
diffuseST += deltaTexCoords;
260258
specularST += deltaTexCoords;
259+
currentLayerDepth += layerDepth * 0.5;
260+
} else {
261+
bumpedST -= deltaTexCoords;
262+
diffuseST -= deltaTexCoords;
263+
specularST -= deltaTexCoords;
261264
currentLayerDepth -= layerDepth * 0.5;
262265
}
266+
layerDepth *= 0.5;
263267
}
264268
}
265269
}
266270

267-
// Sample the bump map and adjust range to [-1, 1]
271+
// Sample the bump map *once* after POM and adjust range to [-1, 1]
268272
vec3 N = normalize(texture(bumpImage, bumpedST).xyz * 2.0 - 1.0);
269273

270274
// Sample the light vector and normalize it

0 commit comments

Comments
 (0)