3D engine that AI agents drive
Fix mesh blend seam normals, 1px MSAA edges, and receiver filtering (#510)
Nguyen Xuan Anf6450c86a32c
diff --git a/changes/510.size.md b/changes/510.size.mdnew file mode 100644index 00000000..d23e0cc7--- /dev/null+++ b/changes/510.size.md@@ -0,0 +1 @@+- entry:mesh-blend: +277 B gzip to keep normal reconstruction on the same part across a seam, blend 1-pixel MSAA seam edges, and skip foliage, grass and water IDs plus other blend parts so rocks and trunks blend cleanly into the ground without a dark speckled rim (issue #510).diff --git a/src/mesh-blend.js b/src/mesh-blend.jsindex edee6296..740ed850 100644--- a/src/mesh-blend.js+++ b/src/mesh-blend.js@@ -90,9 +90,13 @@ ivec2 lastPixel;int partAt(ivec2 pixel) {ivec2 clamped = clamp(pixel, ivec2(0), lastPixel);ivec4 bytes = ivec4(texelFetch(partTexture, clamped, 0) * 255.0 + 0.5);- if (bytes.w != (bytes.x * 3 + bytes.y * 5 + bytes.z * 11) % 251 + 1) return 0;+ if (bytes.w == 0) return 0;+ if (bytes.w != (bytes.x * 3 + bytes.y * 5 + bytes.z * 11) % 251 + 1) return -1;return bytes.x + bytes.y * 256 + bytes.z * 65536;}+float radiusOf(int part) {+ return part > 0 ? texelFetch(radiusTexture, ivec2(part & ${TABLE_WIDTH - 1}, part / ${TABLE_WIDTH}), 0).r : 0.0;+}vec3 positionAt(ivec2 pixel) {ivec2 clamped = clamp(pixel, ivec2(0), lastPixel);float depth = texelFetch(sceneDepth, clamped, 0).r;@@ -100,18 +104,22 @@ vec3 positionAt(ivec2 pixel) {vec4 world = inverseViewProjection * ndc;return world.xyz / world.w;}-// The shorter step of the two sides, skipping a zero step (the frame edge clamps a neighbour onto the pixel itself).+// The shorter step of the two sides, skipping a zero step and staying on the same part where a neighbour crosses a seam.vec3 shorterStep(vec3 first, vec3 second) {float firstLength = dot(first, first);float secondLength = dot(second, second);return (secondLength < 1e-14 || (firstLength >= 1e-14 && firstLength < secondLength)) ? first : second;}-vec3 normalAt(ivec2 pixel, vec3 center) {- vec3 right = positionAt(pixel + ivec2(1, 0)) - center;- vec3 left = center - positionAt(pixel - ivec2(1, 0));- vec3 up = positionAt(pixel + ivec2(0, 1)) - center;- vec3 down = center - positionAt(pixel - ivec2(0, 1));- vec3 raw = cross(shorterStep(right, left), shorterStep(up, down));+vec3 stepAlong(ivec2 pixel, ivec2 delta, vec3 center, int part) {+ vec3 forward = positionAt(pixel + delta) - center;+ vec3 backward = center - positionAt(pixel - delta);+ bool forwardSame = partAt(pixel + delta) == part;+ bool backwardSame = partAt(pixel - delta) == part;+ if (forwardSame != backwardSame) return forwardSame ? forward : backward;+ return shorterStep(forward, backward);+}+vec3 normalAt(ivec2 pixel, vec3 center, int part) {+ vec3 raw = cross(stepAlong(pixel, ivec2(1, 0), center, part), stepAlong(pixel, ivec2(0, 1), center, part));vec3 toEye = normalize(eye - center);if (dot(raw, raw) < 1e-20) return toEye;vec3 normal = normalize(raw);@@ -124,9 +132,23 @@ void main() {outputColor = source;int part = partAt(pixel);if (part == 0) return;- float radius = texelFetch(radiusTexture, ivec2(part & ${TABLE_WIDTH - 1}, part / ${TABLE_WIDTH}), 0).r;+ ivec2 normalPixel = pixel;+ float radius = radiusOf(part);+ if (part < 0) {+ const ivec2 neighbours[4] = ivec2[4](ivec2(1, 0), ivec2(-1, 0), ivec2(0, 1), ivec2(0, -1));+ for (int side = 0; side < 4; side++) {+ ivec2 neighbour = pixel + neighbours[side];+ int candidate = partAt(neighbour);+ float candidateRadius = radiusOf(candidate);+ if (candidateRadius > radius) {+ part = candidate;+ radius = candidateRadius;+ normalPixel = neighbour;+ }+ }+ }if (radius <= 0.0) return;- vec3 center = positionAt(pixel);+ vec3 center = positionAt(normalPixel);float pixelRadius = radius * pixelsPerMetre / (orthographic > 0.5 ? 1.0 : length(center - eye));pixelRadius = clamp(pixelRadius, 1.0, ${MAXIMUM_PIXEL_RADIUS}.0);float reach = radius * 3.0;@@ -134,24 +156,26 @@ void main() {float otherWeight = 0.0;ivec2 nearestTap = pixel;vec3 nearestPosition = center;+ int nearestPart = 0;bool found = false;for (int index = 0; index < ${TAP_COUNT}; index++) {vec3 tapOffset = tapOffsets[index];ivec2 tap = pixel + ivec2(round(tapOffset.xy * pixelRadius));int tapPart = partAt(tap);- if (tapPart == 0 || tapPart == part) continue;+ if (tapPart <= 0 || tapPart == part || radiusOf(tapPart) > 0.0) continue;vec3 tapPosition = positionAt(tap);if (distance(tapPosition, center) > reach) continue;if (!found) {nearestTap = tap;nearestPosition = tapPosition;+ nearestPart = tapPart;}found = true;colorSum += tapOffset.z * texelFetch(sourceTexture, clamp(tap, ivec2(0), lastPixel), 0).rgb;otherWeight += tapOffset.z;}if (!found) return;- float creased = smoothstep(0.05, 0.4, 1.0 - dot(normalAt(pixel, center), normalAt(nearestTap, nearestPosition)));+ float creased = smoothstep(0.05, 0.4, 1.0 - dot(normalAt(normalPixel, center, part), normalAt(nearestTap, nearestPosition, nearestPart)));float amount = smoothstep(0.0, 0.5, otherWeight / ${TAP_TOTAL_WEIGHT}) * creased;outputColor.rgb = mix(source.rgb, colorSum / otherWeight, amount);}`;@@ -204,7 +228,7 @@ function sameTable(first, second) {// integer target, so the resolve averages; see partAt). Blended parts write 0: they draw over the opaque pixel without writing depth.registerShaderFeatureCode('meshBlend', Object.freeze({declarations: 'layout(location = 1) out highp vec4 partIdentifier;\n',- surfaceHook: ` uint partNumber = alphaMode > 1.5 ? 0u : uint(renderPassObjectBase + instanceEmission.w + 1.0);+ surfaceHook: ` uint partNumber = canopyShaded || TAG_WATER || alphaMode > 1.5 ? 0u : uint(renderPassObjectBase + instanceEmission.w + 1.0);uvec3 partBytes = uvec3(partNumber & 255u, (partNumber >> 8) & 255u, partNumber >> 16);partIdentifier = vec4(vec3(partBytes), float((partBytes.x * 3u + partBytes.y * 5u + partBytes.z * 11u) % 251u + 1u)) / 255.0;`,diff --git a/src/nature.js b/src/nature.jsindex 89010dc2..cd5eea53 100644--- a/src/nature.js+++ b/src/nature.js@@ -21,8 +21,8 @@ function randomGenerator(seed) {// Seam softening where a rock or a trunk meets the ground (mesh-blend.js), in metres. One radius per group keeps the// instanced batches culled.-const ROCK_BLEND = Object.freeze({ radius: 0.1 });-const TRUNK_BLEND = Object.freeze({ radius: 0.08 });+const ROCK_BLEND = Object.freeze({ radius: 0.16 });+const TRUNK_BLEND = Object.freeze({ radius: 0.12 });export const FLAG_SIZE = Object.freeze({ width: 1.2, height: 0.75, segments: 12 });@@ -367,8 +367,9 @@ export function createNatureWorld({ seed = 67, detail = 'balanced' } = {}) {add(`River stone ${index}`, rockMesh, [horizontal, ground + radius * 0.19, depth], [radius * 1.3, radius * 0.64, radius], stones[index % stones.length], [random() * 0.3, random() * FULL_TURN, random() * 0.4], ROCK_BLEND);}add('Glacial boulder', rockMesh, [-7.8, terrainHeight(-7.8, 12) + 0.6, 12], [2.3, 1.25, 1.65], stones[1], [0.1, 0.4, 0.2], ROCK_BLEND);- add('Moss on glacial boulder', rockMesh, [-7.85, terrainHeight(-7.8, 12) + 1.56, 12], [1.65, 0.25, 1.14], foliage[0], [0.1, 0.4, 0.2]);+ add('Moss on glacial boulder', organicSphere(12, 8, 4, 0.11), [-7.85, terrainHeight(-7.8, 12) + 1.56, 12], [1.65, 0.25, 1.14], foliage[0], [0.1, 0.4, 0.2]);+ const stemMesh = cylinder(10);const flowerPatches = [[-7, 16], [8, 4], [-11, -3], [4, -18]];for (let index = 0; index < settings.flowers; index++) {const patch = flowerPatches[index % flowerPatches.length];@@ -377,7 +378,7 @@ export function createNatureWorld({ seed = 67, detail = 'balanced' } = {}) {if (Math.abs(horizontal - streamCenter(depth)) < streamWidth(depth) + 0.7) continue;const ground = terrainHeight(horizontal, depth);const height = 0.3 + random() * 0.36;- add(`Wildflower ${index} stem`, trunkMesh, [horizontal, ground + height / 2, depth], [0.013, height, 0.013], meadow[2]);+ add(`Wildflower ${index} stem`, stemMesh, [horizontal, ground + height / 2, depth], [0.013, height, 0.013], meadow[2]);add(`Wildflower ${index} blossom`, blossomMesh, [horizontal, ground + height, depth], [1, 1, 1], flowerColors[index % flowerColors.length], [0.1, random() * FULL_TURN, 0.12]);}diff --git a/tests/support/mesh-blend-reference.js b/tests/support/mesh-blend-reference.jsindex ad6e8860..4e973857 100644--- a/tests/support/mesh-blend-reference.js+++ b/tests/support/mesh-blend-reference.js@@ -38,12 +38,16 @@ export function blendReference(buffers, options) {const clampY = y => Math.min(height - 1, Math.max(0, y));const indexOf = (x, y) => clampY(y) * width + clampX(x);const positionAt = (x, y) => Array.from(positions.subarray(indexOf(x, y) * 3, indexOf(x, y) * 3 + 3));- const normalAt = (x, y, center) => {- const right = subtract(positionAt(x + 1, y), center);- const left = subtract(center, positionAt(x - 1, y));- const up = subtract(positionAt(x, y + 1), center);- const down = subtract(center, positionAt(x, y - 1));- const raw = cross(shorterStep(right, left), shorterStep(up, down));+ const stepAlong = (x, y, dx, dy, center, part) => {+ const forward = subtract(positionAt(x + dx, y + dy), center);+ const backward = subtract(center, positionAt(x - dx, y - dy));+ const forwardSame = ids[indexOf(x + dx, y + dy)] === part;+ const backwardSame = ids[indexOf(x - dx, y - dy)] === part;+ if (forwardSame !== backwardSame) return forwardSame ? forward : backward;+ return shorterStep(forward, backward);+ };+ const normalAt = (x, y, center, part) => {+ const raw = cross(stepAlong(x, y, 1, 0, center, part), stepAlong(x, y, 0, 1, center, part));const toEyeRaw = subtract(options.eye, center);const toEye = toEyeRaw.map(component => component / Math.hypot(...toEyeRaw));if (dot(raw, raw) < 1e-20) return toEye;@@ -71,7 +75,7 @@ export function blendReference(buffers, options) {const weight = 1 - fraction;totalWeight += weight;const tapPart = ids[indexOf(tapX, tapY)];- if (tapPart === 0 || tapPart === part) continue;+ if (tapPart <= 0 || tapPart === part || (options.radii.get(tapPart) ?? 0) > 0) continue;if (Math.hypot(...subtract(positionAt(tapX, tapY), center)) > reach) continue;nearestTap ??= [tapX, tapY];for (let channel = 0; channel < 3; channel++) colorSum[channel] += weight * colors[indexOf(tapX, tapY) * 3 + channel];@@ -79,7 +83,7 @@ export function blendReference(buffers, options) {}if (!nearestTap) continue;const nearestPosition = positionAt(...nearestTap);- const creased = smoothstep(0.05, 0.4, 1 - dot(normalAt(x, y, center), normalAt(nearestTap[0], nearestTap[1], nearestPosition)));+ const creased = smoothstep(0.05, 0.4, 1 - dot(normalAt(x, y, center, part), normalAt(nearestTap[0], nearestTap[1], nearestPosition, ids[indexOf(nearestTap[0], nearestTap[1])])));const amount = smoothstep(0, 0.5, otherWeight / totalWeight) * creased;for (let channel = 0; channel < 3; channel++) {const source = colors[indexOf(x, y) * 3 + channel];