@ifc-lite/renderer 3.2.0 → 4.2.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/dist/aabb-edges.d.ts +10 -0
- package/dist/aabb-edges.d.ts.map +1 -1
- package/dist/aabb-edges.js +13 -0
- package/dist/aabb-edges.js.map +1 -1
- package/dist/ao-params.d.ts +85 -0
- package/dist/ao-params.d.ts.map +1 -0
- package/dist/ao-params.js +109 -0
- package/dist/ao-params.js.map +1 -0
- package/dist/ao-pass.d.ts +47 -0
- package/dist/ao-pass.d.ts.map +1 -0
- package/dist/ao-pass.js +157 -0
- package/dist/ao-pass.js.map +1 -0
- package/dist/camera-matrices.d.ts +3 -0
- package/dist/camera-matrices.d.ts.map +1 -1
- package/dist/camera-matrices.js +12 -1
- package/dist/camera-matrices.js.map +1 -1
- package/dist/camera-orientation.d.ts +62 -0
- package/dist/camera-orientation.d.ts.map +1 -0
- package/dist/camera-orientation.js +155 -0
- package/dist/camera-orientation.js.map +1 -0
- package/dist/camera-projection.d.ts +3 -1
- package/dist/camera-projection.d.ts.map +1 -1
- package/dist/camera-projection.js +26 -20
- package/dist/camera-projection.js.map +1 -1
- package/dist/camera-surface-zoom.d.ts +32 -0
- package/dist/camera-surface-zoom.d.ts.map +1 -0
- package/dist/camera-surface-zoom.js +37 -0
- package/dist/camera-surface-zoom.js.map +1 -0
- package/dist/camera.d.ts +32 -61
- package/dist/camera.d.ts.map +1 -1
- package/dist/camera.js +71 -167
- package/dist/camera.js.map +1 -1
- package/dist/clash-solid-pipeline.d.ts +9 -3
- package/dist/clash-solid-pipeline.d.ts.map +1 -1
- package/dist/clash-solid-pipeline.js +31 -8
- package/dist/clash-solid-pipeline.js.map +1 -1
- package/dist/constants.d.ts +1 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +4 -3
- package/dist/constants.js.map +1 -1
- package/dist/depth-reconstruct.d.ts +43 -0
- package/dist/depth-reconstruct.d.ts.map +1 -0
- package/dist/depth-reconstruct.js +35 -0
- package/dist/depth-reconstruct.js.map +1 -0
- package/dist/device.js +1 -1
- package/dist/device.js.map +1 -1
- package/dist/environment.d.ts +26 -15
- package/dist/environment.d.ts.map +1 -1
- package/dist/environment.js +24 -13
- package/dist/environment.js.map +1 -1
- package/dist/federation-registry.d.ts +16 -0
- package/dist/federation-registry.d.ts.map +1 -1
- package/dist/federation-registry.js +80 -9
- package/dist/federation-registry.js.map +1 -1
- package/dist/gpu-static-upload.d.ts +39 -0
- package/dist/gpu-static-upload.d.ts.map +1 -0
- package/dist/gpu-static-upload.js +70 -0
- package/dist/gpu-static-upload.js.map +1 -0
- package/dist/gpu-upload-guard.d.ts +12 -23
- package/dist/gpu-upload-guard.d.ts.map +1 -1
- package/dist/gpu-upload-guard.js +12 -26
- package/dist/gpu-upload-guard.js.map +1 -1
- package/dist/index.d.ts +29 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +201 -252
- package/dist/index.js.map +1 -1
- package/dist/instanced-render.d.ts +22 -2
- package/dist/instanced-render.d.ts.map +1 -1
- package/dist/instanced-render.js +47 -10
- package/dist/instanced-render.js.map +1 -1
- package/dist/instanced-rte.d.ts +17 -0
- package/dist/instanced-rte.d.ts.map +1 -0
- package/dist/instanced-rte.js +32 -0
- package/dist/instanced-rte.js.map +1 -0
- package/dist/mesh-rte-uniforms.d.ts +46 -0
- package/dist/mesh-rte-uniforms.d.ts.map +1 -0
- package/dist/mesh-rte-uniforms.js +59 -0
- package/dist/mesh-rte-uniforms.js.map +1 -0
- package/dist/model-bounds-tracker.d.ts +7 -0
- package/dist/model-bounds-tracker.d.ts.map +1 -1
- package/dist/model-bounds-tracker.js +16 -5
- package/dist/model-bounds-tracker.js.map +1 -1
- package/dist/model-translation.d.ts +1 -1
- package/dist/model-translation.d.ts.map +1 -1
- package/dist/model-translation.js +50 -2
- package/dist/model-translation.js.map +1 -1
- package/dist/pick-rte-snapshot.d.ts +7 -0
- package/dist/pick-rte-snapshot.d.ts.map +1 -0
- package/dist/pick-rte-snapshot.js +15 -0
- package/dist/pick-rte-snapshot.js.map +1 -0
- package/dist/pick-types.d.ts +17 -0
- package/dist/pick-types.d.ts.map +1 -0
- package/dist/pick-types.js +5 -0
- package/dist/pick-types.js.map +1 -0
- package/dist/pick-uniforms.d.ts +6 -0
- package/dist/pick-uniforms.d.ts.map +1 -1
- package/dist/pick-uniforms.js +22 -18
- package/dist/pick-uniforms.js.map +1 -1
- package/dist/pick-world-position.d.ts +18 -0
- package/dist/pick-world-position.d.ts.map +1 -0
- package/dist/pick-world-position.js +21 -0
- package/dist/pick-world-position.js.map +1 -0
- package/dist/picker-readbacks.d.ts +6 -0
- package/dist/picker-readbacks.d.ts.map +1 -0
- package/dist/picker-readbacks.js +20 -0
- package/dist/picker-readbacks.js.map +1 -0
- package/dist/picker-rte-uniforms.d.ts +6 -0
- package/dist/picker-rte-uniforms.d.ts.map +1 -0
- package/dist/picker-rte-uniforms.js +41 -0
- package/dist/picker-rte-uniforms.js.map +1 -0
- package/dist/picker.d.ts +6 -7
- package/dist/picker.d.ts.map +1 -1
- package/dist/picker.js +88 -115
- package/dist/picker.js.map +1 -1
- package/dist/picking-manager.d.ts +8 -0
- package/dist/picking-manager.d.ts.map +1 -1
- package/dist/picking-manager.js +46 -19
- package/dist/picking-manager.js.map +1 -1
- package/dist/pipeline.d.ts +1 -7
- package/dist/pipeline.d.ts.map +1 -1
- package/dist/pipeline.js +17 -20
- package/dist/pipeline.js.map +1 -1
- package/dist/point-picker.d.ts +5 -1
- package/dist/point-picker.d.ts.map +1 -1
- package/dist/point-picker.js +49 -9
- package/dist/point-picker.js.map +1 -1
- package/dist/pointcloud/point-cloud-handle-ids.d.ts +22 -0
- package/dist/pointcloud/point-cloud-handle-ids.d.ts.map +1 -0
- package/dist/pointcloud/point-cloud-handle-ids.js +31 -0
- package/dist/pointcloud/point-cloud-handle-ids.js.map +1 -0
- package/dist/pointcloud/point-cloud-node.d.ts +14 -6
- package/dist/pointcloud/point-cloud-node.d.ts.map +1 -1
- package/dist/pointcloud/point-cloud-node.js.map +1 -1
- package/dist/pointcloud/point-cloud-pick-sources.d.ts +47 -0
- package/dist/pointcloud/point-cloud-pick-sources.d.ts.map +1 -0
- package/dist/pointcloud/point-cloud-pick-sources.js +32 -0
- package/dist/pointcloud/point-cloud-pick-sources.js.map +1 -0
- package/dist/pointcloud/point-cloud-placement.d.ts.map +1 -1
- package/dist/pointcloud/point-cloud-placement.js +20 -5
- package/dist/pointcloud/point-cloud-placement.js.map +1 -1
- package/dist/pointcloud/point-cloud-ray-transform.d.ts +78 -4
- package/dist/pointcloud/point-cloud-ray-transform.d.ts.map +1 -1
- package/dist/pointcloud/point-cloud-ray-transform.js +31 -84
- package/dist/pointcloud/point-cloud-ray-transform.js.map +1 -1
- package/dist/pointcloud/point-cloud-renderer.d.ts +21 -19
- package/dist/pointcloud/point-cloud-renderer.d.ts.map +1 -1
- package/dist/pointcloud/point-cloud-renderer.js +46 -52
- package/dist/pointcloud/point-cloud-renderer.js.map +1 -1
- package/dist/pointcloud/point-cloud-stream-lifecycle.d.ts +44 -0
- package/dist/pointcloud/point-cloud-stream-lifecycle.d.ts.map +1 -0
- package/dist/pointcloud/point-cloud-stream-lifecycle.js +44 -0
- package/dist/pointcloud/point-cloud-stream-lifecycle.js.map +1 -0
- package/dist/pointcloud/point-cloud-uniforms.d.ts +5 -0
- package/dist/pointcloud/point-cloud-uniforms.d.ts.map +1 -1
- package/dist/pointcloud/point-cloud-uniforms.js +55 -44
- package/dist/pointcloud/point-cloud-uniforms.js.map +1 -1
- package/dist/pointcloud/point-cloud-visibility.d.ts +10 -0
- package/dist/pointcloud/point-cloud-visibility.d.ts.map +1 -0
- package/dist/pointcloud/point-cloud-visibility.js +18 -0
- package/dist/pointcloud/point-cloud-visibility.js.map +1 -0
- package/dist/pointcloud/point-pipeline.d.ts +4 -9
- package/dist/pointcloud/point-pipeline.d.ts.map +1 -1
- package/dist/pointcloud/point-pipeline.js +4 -9
- package/dist/pointcloud/point-pipeline.js.map +1 -1
- package/dist/pointcloud/point-shader.wgsl.d.ts +1 -21
- package/dist/pointcloud/point-shader.wgsl.d.ts.map +1 -1
- package/dist/pointcloud/point-shader.wgsl.js +25 -5
- package/dist/pointcloud/point-shader.wgsl.js.map +1 -1
- package/dist/post-pass-chain.d.ts +38 -0
- package/dist/post-pass-chain.d.ts.map +1 -0
- package/dist/post-pass-chain.js +99 -0
- package/dist/post-pass-chain.js.map +1 -0
- package/dist/post-processor.d.ts +12 -26
- package/dist/post-processor.d.ts.map +1 -1
- package/dist/post-processor.js +14 -72
- package/dist/post-processor.js.map +1 -1
- package/dist/raycast-engine.d.ts +7 -5
- package/dist/raycast-engine.d.ts.map +1 -1
- package/dist/raycast-engine.js +17 -20
- package/dist/raycast-engine.js.map +1 -1
- package/dist/raycast-point-cloud-query.d.ts +1 -1
- package/dist/raycast-point-cloud-query.d.ts.map +1 -1
- package/dist/reference-image-pipeline.d.ts +1 -1
- package/dist/reference-image-pipeline.d.ts.map +1 -1
- package/dist/reference-image-pipeline.js +25 -7
- package/dist/reference-image-pipeline.js.map +1 -1
- package/dist/reference-images.d.ts +1 -1
- package/dist/reference-images.d.ts.map +1 -1
- package/dist/reference-images.js +13 -5
- package/dist/reference-images.js.map +1 -1
- package/dist/relative-to-eye.d.ts +146 -0
- package/dist/relative-to-eye.d.ts.map +1 -0
- package/dist/relative-to-eye.js +326 -0
- package/dist/relative-to-eye.js.map +1 -0
- package/dist/render-section-draw.d.ts +11 -0
- package/dist/render-section-draw.d.ts.map +1 -1
- package/dist/render-section-draw.js +6 -3
- package/dist/render-section-draw.js.map +1 -1
- package/dist/renderer-color-readback.d.ts +50 -0
- package/dist/renderer-color-readback.d.ts.map +1 -0
- package/dist/renderer-color-readback.js +145 -0
- package/dist/renderer-color-readback.js.map +1 -0
- package/dist/renderer-overlays.d.ts +12 -3
- package/dist/renderer-overlays.d.ts.map +1 -1
- package/dist/renderer-overlays.js +33 -20
- package/dist/renderer-overlays.js.map +1 -1
- package/dist/renderer-screenshot.d.ts +4 -0
- package/dist/renderer-screenshot.d.ts.map +1 -0
- package/dist/renderer-screenshot.js +17 -0
- package/dist/renderer-screenshot.js.map +1 -0
- package/dist/renderer-symbolic-overlays.d.ts +4 -2
- package/dist/renderer-symbolic-overlays.d.ts.map +1 -1
- package/dist/renderer-symbolic-overlays.js +33 -13
- package/dist/renderer-symbolic-overlays.js.map +1 -1
- package/dist/renderer-viewport.d.ts +45 -0
- package/dist/renderer-viewport.d.ts.map +1 -1
- package/dist/renderer-viewport.js +44 -0
- package/dist/renderer-viewport.js.map +1 -1
- package/dist/rte-clip-space.d.ts +12 -0
- package/dist/rte-clip-space.d.ts.map +1 -0
- package/dist/rte-clip-space.js +22 -0
- package/dist/rte-clip-space.js.map +1 -0
- package/dist/rte-frustum.d.ts +24 -0
- package/dist/rte-frustum.d.ts.map +1 -0
- package/dist/rte-frustum.js +45 -0
- package/dist/rte-frustum.js.map +1 -0
- package/dist/scene-batch-upload.d.ts.map +1 -1
- package/dist/scene-batch-upload.js +14 -38
- package/dist/scene-batch-upload.js.map +1 -1
- package/dist/scene-device-recovery.d.ts.map +1 -1
- package/dist/scene-device-recovery.js +5 -9
- package/dist/scene-device-recovery.js.map +1 -1
- package/dist/scene-finalize-regroup.d.ts +54 -0
- package/dist/scene-finalize-regroup.d.ts.map +1 -0
- package/dist/scene-finalize-regroup.js +136 -0
- package/dist/scene-finalize-regroup.js.map +1 -0
- package/dist/scene-instance-bounds.d.ts +12 -0
- package/dist/scene-instance-bounds.d.ts.map +1 -0
- package/dist/scene-instance-bounds.js +48 -0
- package/dist/scene-instance-bounds.js.map +1 -0
- package/dist/scene-instance-materialization.d.ts +4 -1
- package/dist/scene-instance-materialization.d.ts.map +1 -1
- package/dist/scene-instance-materialization.js +21 -6
- package/dist/scene-instance-materialization.js.map +1 -1
- package/dist/scene-instance-translation.d.ts +17 -0
- package/dist/scene-instance-translation.d.ts.map +1 -0
- package/dist/scene-instance-translation.js +50 -0
- package/dist/scene-instance-translation.js.map +1 -0
- package/dist/scene-instance-types.d.ts +6 -0
- package/dist/scene-instance-types.d.ts.map +1 -1
- package/dist/scene-model-translation.d.ts +2 -0
- package/dist/scene-model-translation.d.ts.map +1 -1
- package/dist/scene-model-translation.js +1 -1
- package/dist/scene-model-translation.js.map +1 -1
- package/dist/scene-rect-select.d.ts +11 -1
- package/dist/scene-rect-select.d.ts.map +1 -1
- package/dist/scene-rect-select.js +7 -4
- package/dist/scene-rect-select.js.map +1 -1
- package/dist/scene.d.ts +17 -19
- package/dist/scene.d.ts.map +1 -1
- package/dist/scene.js +109 -238
- package/dist/scene.js.map +1 -1
- package/dist/section-2d-lift.d.ts +10 -2
- package/dist/section-2d-lift.d.ts.map +1 -1
- package/dist/section-2d-lift.js +14 -6
- package/dist/section-2d-lift.js.map +1 -1
- package/dist/section-2d-line-buffer.d.ts +12 -4
- package/dist/section-2d-line-buffer.d.ts.map +1 -1
- package/dist/section-2d-line-buffer.js +59 -33
- package/dist/section-2d-line-buffer.js.map +1 -1
- package/dist/section-2d-overlay.d.ts +18 -72
- package/dist/section-2d-overlay.d.ts.map +1 -1
- package/dist/section-2d-overlay.js +72 -103
- package/dist/section-2d-overlay.js.map +1 -1
- package/dist/section-2d-types.d.ts +38 -0
- package/dist/section-2d-types.d.ts.map +1 -0
- package/dist/section-2d-types.js +6 -0
- package/dist/section-2d-types.js.map +1 -0
- package/dist/section-cap-pipelines.d.ts +6 -0
- package/dist/section-cap-pipelines.d.ts.map +1 -0
- package/dist/section-cap-pipelines.js +87 -0
- package/dist/section-cap-pipelines.js.map +1 -0
- package/dist/section-plane-geometry.d.ts +24 -0
- package/dist/section-plane-geometry.d.ts.map +1 -0
- package/dist/section-plane-geometry.js +93 -0
- package/dist/section-plane-geometry.js.map +1 -0
- package/dist/section-plane.d.ts +23 -24
- package/dist/section-plane.d.ts.map +1 -1
- package/dist/section-plane.js +58 -174
- package/dist/section-plane.js.map +1 -1
- package/dist/shaders/ao.wgsl.d.ts +2 -0
- package/dist/shaders/ao.wgsl.d.ts.map +1 -0
- package/dist/shaders/ao.wgsl.js +234 -0
- package/dist/shaders/ao.wgsl.js.map +1 -0
- package/dist/shaders/color-transfer.wgsl.d.ts +26 -0
- package/dist/shaders/color-transfer.wgsl.d.ts.map +1 -0
- package/dist/shaders/color-transfer.wgsl.js +53 -0
- package/dist/shaders/color-transfer.wgsl.js.map +1 -0
- package/dist/shaders/depth-reconstruct.wgsl.d.ts +16 -0
- package/dist/shaders/depth-reconstruct.wgsl.d.ts.map +1 -0
- package/dist/shaders/depth-reconstruct.wgsl.js +69 -0
- package/dist/shaders/depth-reconstruct.wgsl.js.map +1 -0
- package/dist/shaders/main-rte-shadow.wgsl.d.ts +4 -0
- package/dist/shaders/main-rte-shadow.wgsl.d.ts.map +1 -0
- package/dist/shaders/main-rte-shadow.wgsl.js +54 -0
- package/dist/shaders/main-rte-shadow.wgsl.js.map +1 -0
- package/dist/shaders/main.wgsl.d.ts +1 -6
- package/dist/shaders/main.wgsl.d.ts.map +1 -1
- package/dist/shaders/main.wgsl.js +110 -159
- package/dist/shaders/main.wgsl.js.map +1 -1
- package/dist/shaders/relative-to-eye.wgsl.d.ts +12 -0
- package/dist/shaders/relative-to-eye.wgsl.d.ts.map +1 -0
- package/dist/shaders/relative-to-eye.wgsl.js +40 -0
- package/dist/shaders/relative-to-eye.wgsl.js.map +1 -0
- package/dist/shaders/section-2d-overlay.wgsl.d.ts +28 -12
- package/dist/shaders/section-2d-overlay.wgsl.d.ts.map +1 -1
- package/dist/shaders/section-2d-overlay.wgsl.js +40 -13
- package/dist/shaders/section-2d-overlay.wgsl.js.map +1 -1
- package/dist/shaders/shadow.wgsl.d.ts +1 -1
- package/dist/shaders/shadow.wgsl.d.ts.map +1 -1
- package/dist/shaders/shadow.wgsl.js +25 -7
- package/dist/shaders/shadow.wgsl.js.map +1 -1
- package/dist/shaders/sky.wgsl.d.ts +1 -11
- package/dist/shaders/sky.wgsl.d.ts.map +1 -1
- package/dist/shaders/sky.wgsl.js +17 -19
- package/dist/shaders/sky.wgsl.js.map +1 -1
- package/dist/shaders/symbolic-overlay.wgsl.d.ts +2 -9
- package/dist/shaders/symbolic-overlay.wgsl.d.ts.map +1 -1
- package/dist/shaders/symbolic-overlay.wgsl.js +88 -17
- package/dist/shaders/symbolic-overlay.wgsl.js.map +1 -1
- package/dist/shaders/textured.wgsl.d.ts.map +1 -1
- package/dist/shaders/textured.wgsl.js +11 -9
- package/dist/shaders/textured.wgsl.js.map +1 -1
- package/dist/shadow-light-matrix.d.ts +7 -0
- package/dist/shadow-light-matrix.d.ts.map +1 -1
- package/dist/shadow-light-matrix.js +11 -0
- package/dist/shadow-light-matrix.js.map +1 -1
- package/dist/shadow-occluder-batches.d.ts +10 -0
- package/dist/shadow-occluder-batches.d.ts.map +1 -0
- package/dist/shadow-occluder-batches.js +39 -0
- package/dist/shadow-occluder-batches.js.map +1 -0
- package/dist/shadow-occluders.d.ts.map +1 -1
- package/dist/shadow-occluders.js +4 -0
- package/dist/shadow-occluders.js.map +1 -1
- package/dist/shadow-pass.d.ts +5 -51
- package/dist/shadow-pass.d.ts.map +1 -1
- package/dist/shadow-pass.js +49 -45
- package/dist/shadow-pass.js.map +1 -1
- package/dist/shadow-types.d.ts +39 -0
- package/dist/shadow-types.d.ts.map +1 -0
- package/dist/shadow-types.js +15 -0
- package/dist/shadow-types.js.map +1 -0
- package/dist/symbolic-overlay-geometry.d.ts +7 -0
- package/dist/symbolic-overlay-geometry.d.ts.map +1 -0
- package/dist/symbolic-overlay-geometry.js +43 -0
- package/dist/symbolic-overlay-geometry.js.map +1 -0
- package/dist/symbolic-overlay-pipelines.d.ts +24 -36
- package/dist/symbolic-overlay-pipelines.d.ts.map +1 -1
- package/dist/symbolic-overlay-pipelines.js +175 -182
- package/dist/symbolic-overlay-pipelines.js.map +1 -1
- package/dist/symbolic-text-atlas.d.ts +21 -0
- package/dist/symbolic-text-atlas.d.ts.map +1 -1
- package/dist/symbolic-text-atlas.js +29 -1
- package/dist/symbolic-text-atlas.js.map +1 -1
- package/dist/types.d.ts +20 -26
- package/dist/types.d.ts.map +1 -1
- package/dist/visual-enhancement.d.ts +23 -1
- package/dist/visual-enhancement.d.ts.map +1 -1
- package/dist/visual-enhancement.js +38 -12
- package/dist/visual-enhancement.js.map +1 -1
- package/package.json +3 -3
|
@@ -1,7 +1,2 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Main PBR rendering shader for IFC geometry.
|
|
3
|
-
* Features: PBR lighting, section plane clipping, selection highlight,
|
|
4
|
-
* glass fresnel, ACES tone mapping, screen-space edge enhancement.
|
|
5
|
-
*/
|
|
6
|
-
export declare const mainShaderSource = "\n struct Uniforms {\n viewProj: mat4x4<f32>,\n model: mat4x4<f32>,\n baseColor: vec4<f32>,\n metallicRoughness: vec2<f32>, // x = metallic, y = roughness\n _padding1: vec2<f32>,\n sectionPlane: vec4<f32>, // xyz = plane normal, w = plane distance\n flags: vec4<u32>, // x = isSelected, y = section/clip bits, z = edgeEnabled, w = edgeIntensityMilli\n clipBoxMin: vec4<f32>, // xyz = clip-box min corner (world), w = pad\n clipBoxMax: vec4<f32>, // xyz = clip-box max corner (world), w = pad\n // Quantized-vertex dequantization (issue #1682 phase 6):\n // xyz = lattice-aligned quantMin (batch-origin-relative), w = step.\n // Only read by vs_main_quantized; zero elsewhere.\n quantParams: vec4<f32>,\n }\n @binding(0) @group(0) var<uniform> uniforms: Uniforms;\n\n // Global lighting environment \u2014 one buffer shared by every mesh in\n // the pass (bound once per frame at group(1)). Field packing must\n // match packEnvironmentUniforms() in environment.ts.\n struct Environment {\n sunDirection: vec3<f32>, // unit vector TOWARD the sun\n sunIntensity: f32,\n sunColor: vec3<f32>,\n ambientIntensity: f32,\n skyColor: vec3<f32>, // hemisphere-ambient sky tint\n exposure: f32,\n groundColor: vec3<f32>, // hemisphere-ambient ground tint\n fillIntensity: f32,\n rimIntensity: f32,\n sunSoftness: f32,\n _pad1: f32,\n _pad2: f32,\n }\n @binding(0) @group(1) var<uniform> env: Environment;\n\n // Sun shadow map (#2670, Phase 2b) at group(1). The depth map, a\n // comparison sampler, and the light matrix + params. Bound on every\n // main-family pipeline; sampling is gated by shadowU.params.y (enabled),\n // so when shadows are off this reads the 1\u00D71 dummy and returns 1.0.\n @binding(1) @group(1) var shadowMap: texture_depth_2d;\n @binding(2) @group(1) var shadowCmp: sampler_comparison;\n struct Shadow {\n lightViewProj: mat4x4<f32>,\n // x = texelSize (1/resolution), y = enabled (0/1),\n // z = normalBias (world units), w = pcfRadius (texels).\n params: vec4<f32>,\n // x = depthBias (reverse-Z clip units, nudges toward lit).\n params2: vec4<f32>,\n }\n @binding(3) @group(1) var<uniform> shadowU: Shadow;\n\n // Fraction of the sun reaching this surface point (1 = lit, 0 = fully\n // shadowed). Normal-offset + slope-scaled bias defeats acne without\n // peter-panning; the penumbra is sampled with a 12-tap Poisson disk\n // ROTATED per pixel (interleaved gradient noise). A fixed grid kernel\n // undersamples a wide penumbra and breaks into discrete bands (the\n // \"tripled shadow\" at high softness); a rotated disk turns that banding\n // into fine dither that reads as smooth at any softness.\n // textureSampleCompareLevel is used (not ...Compare) so it is legal in\n // this non-uniform control flow.\n const SHADOW_POISSON = array<vec2<f32>, 12>(\n vec2<f32>(-0.326, -0.406), vec2<f32>(-0.840, -0.074), vec2<f32>(-0.696, 0.457),\n vec2<f32>(-0.203, 0.621), vec2<f32>( 0.962, -0.195), vec2<f32>( 0.473, -0.480),\n vec2<f32>( 0.519, 0.767), vec2<f32>( 0.185, -0.893), vec2<f32>( 0.507, 0.064),\n vec2<f32>( 0.896, 0.412), vec2<f32>(-0.322, -0.933), vec2<f32>(-0.792, -0.598),\n );\n\n fn sunShadowFactor(worldPos: vec3<f32>, N: vec3<f32>, fragCoord: vec2<f32>) -> f32 {\n if (shadowU.params.y < 0.5) { return 1.0; }\n // The diffuse sun term is TWO-SIDED (abs(dot(N, sun)) in the shading\n // below), so a face whose stabilized normal points away from the sun is\n // still lit. Orient the normal toward the sun before biasing: otherwise\n // the normal-offset push (params.z) moves the sample AWAY from the light\n // (deeper behind the surface), and the slope term below collapses to its\n // max (NdotL\u21920), together biasing the compare toward \"lit\" \u2014 which leaks\n // direct sun onto interior faces the roof occludes (#2670 review).\n let L = normalize(env.sunDirection);\n let Ns = N * select(-1.0, 1.0, dot(N, L) >= 0.0);\n let biased = worldPos + Ns * shadowU.params.z;\n let clip = shadowU.lightViewProj * vec4<f32>(biased, 1.0);\n let ndc = clip.xyz / clip.w;\n let uv = vec2<f32>(ndc.x * 0.5 + 0.5, ndc.y * -0.5 + 0.5);\n let inBounds = uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0 && ndc.z > 0.0;\n if (!inBounds) { return 1.0; }\n // Slope-scaled depth bias: at a grazing sun the receiver's light-space\n // depth changes fast across the kernel, so a constant bias can't clear\n // the whole footprint and the surface rings with moir\u00E9. Grow the bias\n // as the surface tilts away from the sun (NdotL \u2192 0) and with the\n // kernel width. The hardware slope bias in the depth pass covers the\n // occluder side; this covers the receiver side.\n let NdotL = max(dot(Ns, L), 0.0);\n let slope = clamp(sqrt(max(1.0 - NdotL * NdotL, 0.0)) / max(NdotL, 0.1), 1.0, 12.0);\n let refDepth = ndc.z + shadowU.params2.x * slope * (1.0 + shadowU.params.w);\n let radius = shadowU.params.x * shadowU.params.w; // penumbra, uv units\n // Per-pixel rotation (interleaved gradient noise) dithers the disk so\n // the discrete taps never line up into bands.\n let ign = fract(52.9829189 * fract(dot(fragCoord, vec2<f32>(0.06711056, 0.00583715))));\n let ang = ign * 6.2831853;\n let cr = cos(ang);\n let sr = sin(ang);\n var sum = 0.0;\n for (var i = 0; i < 12; i = i + 1) {\n let p = SHADOW_POISSON[i];\n let off = vec2<f32>(p.x * cr - p.y * sr, p.x * sr + p.y * cr) * radius;\n sum = sum + textureSampleCompareLevel(shadowMap, shadowCmp, uv + off, refDepth);\n }\n let pcf = sum / 12.0;\n // Terminator fade. On a surface nearly PARALLEL to the sun rays\n // (NdotL \u2192 0, e.g. a vertical wall under a midday sun) the receiver\n // straddles the shadow threshold, so the rotated-disk taps randomly\n // pass/fail and the wall breaks into salt-and-pepper speckle. The\n // direct sun term is near zero there anyway, so fade the cast shadow\n // smoothly toward lit as the surface goes grazing: a clean gradient\n // replaces the ripple (#2670).\n let graze = smoothstep(0.0, 0.3, NdotL);\n return mix(1.0, pcf, graze);\n }\n\n struct VertexInput {\n @location(0) position: vec3<f32>,\n @location(1) normal: vec3<f32>,\n @location(2) entityId: u32,\n }\n\n struct VertexOutput {\n @builtin(position) position: vec4<f32>,\n @location(0) worldPos: vec3<f32>,\n @location(1) normal: vec3<f32>,\n @location(2) @interpolate(flat) entityId: u32,\n @location(3) viewPos: vec3<f32>, // For edge detection\n // Per-draw albedo carried from the vertex stage so the fragment shader\n // is shared by the flat path (vs_main writes uniforms.baseColor \u2014 the\n // per-batch / overlay-override colour) AND the instanced path\n // (vs_instanced writes the per-occurrence colour from the instance\n // buffer). For the flat path this is identical to reading\n // uniforms.baseColor directly (the value is constant across the draw).\n @location(4) color: vec4<f32>,\n // Per-occurrence selection flag for the instanced path (bit 0 = selected).\n // vs_main writes 0 (the flat path selects via uniforms.flags.x instead);\n // vs_instanced writes the per-instance flag from the instance buffer, so a\n // single selected occurrence highlights without re-drawing.\n @location(5) @interpolate(flat) instSelected: u32,\n }\n\n // Per-instance vertex-buffer inputs (slot 1, stepMode 'instance') used by\n // vs_instanced. The mat4 arrives as four COLUMN vec4s (WGSL mat4x4 is\n // column-major), matching composeInstanceMatrix's column-major output +\n // the pipeline's slot-1 attribute offsets (0/16/32/48).\n //\n // Location namespaces: vertex-INPUT @location (this struct + VertexInput)\n // and inter-stage @location (VertexOutput) are INDEPENDENT in WGSL, so\n // InstanceInput.m1 @location(4) does NOT collide with VertexOutput.color\n // @location(4) \u2014 exactly as VertexInput.entityId and VertexOutput.entityId\n // already BOTH use @location(2). Within the INPUT namespace the per-vertex\n // inputs (0..2) and per-instance inputs (3..8) stay distinct.\n struct InstanceInput {\n @location(3) m0: vec4<f32>,\n @location(4) m1: vec4<f32>,\n @location(5) m2: vec4<f32>,\n @location(6) m3: vec4<f32>,\n @location(7) instEntityId: u32,\n @location(8) instColor: vec4<f32>,\n @location(9) instSelected: u32,\n }\n\n // 12-byte quantized vertex (issue #1682 phase 6): uint16x4 (lattice\n // position xyz + packed octahedral normal in w as (u8x << 8 | u8y))\n // followed by the u32 entityId lane. Dequantization is BIT-EXACT for\n // the 2^-10 lattice: quantMin and q*step are both (integer)\u00B72^-10,\n // so coincident points across batches stay coincident (the shared-\n // origin z-fight guarantee survives quantization).\n struct QuantizedVertexInput {\n @location(0) q: vec4<u32>, // uint16x4: x, y, z, packedOct\n @location(2) entityId: u32,\n }\n\n fn octDecodeN(packed: u32) -> vec3<f32> {\n let ox = (f32((packed >> 8u) & 255u) / 255.0) * 2.0 - 1.0;\n let oy = (f32(packed & 255u) / 255.0) * 2.0 - 1.0;\n var n = vec3<f32>(ox, oy, 1.0 - abs(ox) - abs(oy));\n if (n.z < 0.0) {\n let tx = (1.0 - abs(oy)) * select(-1.0, 1.0, ox >= 0.0);\n let ty = (1.0 - abs(ox)) * select(-1.0, 1.0, oy >= 0.0);\n n = vec3<f32>(tx, ty, n.z);\n }\n return normalize(n);\n }\n\n // Shared flat-path vertex shading \u2014 vs_main and vs_main_quantized\n // differ ONLY in how position/normal are sourced.\n fn shadeFlatVertex(localPos: vec3<f32>, localNormal: vec3<f32>, entityId: u32) -> VertexOutput {\n var output: VertexOutput;\n let worldPos = uniforms.model * vec4<f32>(localPos, 1.0);\n output.position = uniforms.viewProj * worldPos;\n // Anti z-fighting depth nudge \u2014 see vs_main's comment.\n let colorSalt = (entityId >> 24u) * 2654435761u;\n let zHash = (((entityId & 0x00FFFFFFu) ^ colorSalt) * 2654435761u) & 255u;\n output.position.z *= 1.0 + f32(zHash) * 1e-6;\n output.worldPos = worldPos.xyz;\n output.normal = normalize((uniforms.model * vec4<f32>(localNormal, 0.0)).xyz);\n output.entityId = entityId;\n output.color = uniforms.baseColor;\n output.instSelected = 0u;\n output.viewPos = (uniforms.viewProj * worldPos).xyz;\n return output;\n }\n\n @vertex\n fn vs_main_quantized(input: QuantizedVertexInput) -> VertexOutput {\n let p = uniforms.quantParams.xyz\n + vec3<f32>(f32(input.q.x), f32(input.q.y), f32(input.q.z)) * uniforms.quantParams.w;\n return shadeFlatVertex(p, octDecodeN(input.q.w), input.entityId);\n }\n\n @vertex\n fn vs_main(input: VertexInput, @builtin(instance_index) instanceIndex: u32) -> VertexOutput {\n var output: VertexOutput;\n let worldPos = uniforms.model * vec4<f32>(input.position, 1.0);\n output.position = uniforms.viewProj * worldPos;\n // Anti z-fighting: deterministic depth nudge.\n // Knuth multiplicative hash spreads sequential IDs across 0-255 so\n // coplanar faces from different entities always get distinct depths.\n // Material-layer walls slice into one closed solid per layer, all\n // sharing the PARENT wall's expressId, so adjacent layers' coincident\n // interface caps would get the same entity nudge and z-fight into a\n // flickering comb (\"see inside the wall\"). To separate them we fold in\n // an 8-bit MATERIAL-COLOUR salt that mergeGeometry/interleaveTextured\n // baked into the HIGH 8 bits of the entityId lane (low 24 = picking id,\n // masked off by encodeId24). Crucially the salt comes from the mesh's\n // OWN colour, NOT the per-draw baseColor uniform \u2014 so the base opaque\n // pass and the lens/IDS/compare/4D OVERLAY pass (which redraws the same\n // geometry with a DIFFERENT draw colour) compute the SAME nudge, and\n // the overlay pipeline's depthCompare:'equal' matches instead of\n // rejecting every fragment. At 1e-6 per step the max world-space offset\n // is <3mm at 10m \u2014 invisible.\n let colorSalt = (input.entityId >> 24u) * 2654435761u;\n let zHash = (((input.entityId & 0x00FFFFFFu) ^ colorSalt) * 2654435761u) & 255u;\n output.position.z *= 1.0 + f32(zHash) * 1e-6;\n output.worldPos = worldPos.xyz;\n output.normal = normalize((uniforms.model * vec4<f32>(input.normal, 0.0)).xyz);\n output.entityId = input.entityId;\n output.color = uniforms.baseColor;\n output.instSelected = 0u; // flat path selects via uniforms.flags.x\n // Store view-space position for edge detection\n output.viewPos = (uniforms.viewProj * worldPos).xyz;\n return output;\n }\n\n // Instanced vertex entry \u2014 one template's geometry drawn once per\n // occurrence. The per-instance mat4 already folds SWAP * rel_k * T(origin)\n // (composed CPU-side, see instanced-render.ts), so it maps the template's\n // LOCAL vertex straight to WebGL Y-up world space \u2014 no uniforms.model.\n // rel_k and SWAP are rigid (no scale), so the same matrix transforms\n // normals. entityId + colour come per-occurrence from the instance buffer.\n @vertex\n fn vs_instanced(input: VertexInput, inst: InstanceInput) -> VertexOutput {\n var output: VertexOutput;\n let instMat = mat4x4<f32>(inst.m0, inst.m1, inst.m2, inst.m3);\n let worldPos = instMat * vec4<f32>(input.position, 1.0);\n output.position = uniforms.viewProj * worldPos;\n // Same per-entity depth nudge as vs_main. No colour salt here: the\n // instanced path has no base-vs-overlay coincident redraw (yet), so the\n // raw picking id is enough to separate coplanar entities.\n let zHash = ((inst.instEntityId & 0x00FFFFFFu) * 2654435761u) & 255u;\n output.position.z *= 1.0 + f32(zHash) * 1e-6;\n output.worldPos = worldPos.xyz;\n output.normal = normalize((instMat * vec4<f32>(input.normal, 0.0)).xyz);\n output.entityId = inst.instEntityId;\n output.color = inst.instColor;\n output.instSelected = inst.instSelected;\n output.viewPos = (uniforms.viewProj * worldPos).xyz;\n return output;\n }\n\n // PBR helper functions\n fn fresnelSchlick(cosTheta: f32, F0: vec3<f32>) -> vec3<f32> {\n return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);\n }\n\n fn distributionGGX(NdotH: f32, roughness: f32) -> f32 {\n let a = roughness * roughness;\n let a2 = a * a;\n let NdotH2 = NdotH * NdotH;\n let num = a2;\n let denomBase = (NdotH2 * (a2 - 1.0) + 1.0);\n let denom = 3.14159265 * denomBase * denomBase;\n return num / max(denom, 0.0000001);\n }\n\n fn geometrySchlickGGX(NdotV: f32, roughness: f32) -> f32 {\n let r = (roughness + 1.0);\n let k = (r * r) / 8.0;\n let num = NdotV;\n let denom = NdotV * (1.0 - k) + k;\n return num / max(denom, 0.0000001);\n }\n\n fn geometrySmith(NdotV: f32, NdotL: f32, roughness: f32) -> f32 {\n let ggx2 = geometrySchlickGGX(NdotV, roughness);\n let ggx1 = geometrySchlickGGX(NdotL, roughness);\n return ggx1 * ggx2;\n }\n\n fn encodeId24(id: u32) -> vec4<f32> {\n let r = f32((id >> 16u) & 255u) / 255.0;\n let g = f32((id >> 8u) & 255u) / 255.0;\n let b = f32(id & 255u) / 255.0;\n return vec4<f32>(r, g, b, 1.0);\n }\n\n struct FragmentOutput {\n @location(0) color: vec4<f32>,\n @location(1) objectIdEncoded: vec4<f32>,\n }\n\n @fragment\n fn fs_main(input: VertexOutput) -> FragmentOutput {\n // Per-instance hide/isolate: bit 1 of the instance flags lane marks a hidden\n // occurrence. Discard it so it neither draws nor writes depth (and the pick\n // pass applies the same discard, so it isn't pickable). vs_main writes\n // instSelected=0u for flat geometry, so this never affects the flat path.\n if ((input.instSelected & 2u) != 0u) {\n discard;\n }\n // Per-instance opacity routing (instanced passes only \u2014 flags.x bit 2). The\n // opaque instanced pass draws fully-opaque (or selected) occurrences; the\n // transparent instanced sub-pass (bit 3, alpha-blended) draws the rest. Discard\n // the occurrences belonging to the OTHER pass so each is drawn exactly once.\n // Lens-ghost / x-ray / compare write a low per-instance alpha into input.color.a.\n if ((uniforms.flags.x & 4u) != 0u) {\n let occOpaque = input.color.a >= 0.99 || (input.instSelected & 1u) != 0u;\n let transparentPass = (uniforms.flags.x & 8u) != 0u;\n if (transparentPass) {\n if (occOpaque) { discard; }\n } else {\n if (!occOpaque) { discard; }\n }\n }\n // Section plane clipping - discard fragments ABOVE the plane.\n // flags.y packs two bits: bit 0 = enabled, bit 1 = flipped.\n let sectionEnabled = (uniforms.flags.y & 1u) == 1u;\n if (sectionEnabled) {\n let planeNormal = uniforms.sectionPlane.xyz;\n let planeDistance = uniforms.sectionPlane.w;\n let flipped = (uniforms.flags.y & 2u) == 2u;\n let side = select(1.0, -1.0, flipped);\n let distToPlane = (dot(input.worldPos, planeNormal) - planeDistance) * side;\n if (distToPlane > 0.0) {\n discard;\n }\n }\n // Clip box (section / crop box): discard fragments OUTSIDE the AABB.\n // flags.y bit 2 = clip-box enabled.\n if ((uniforms.flags.y & 4u) != 0u) {\n let p = input.worldPos;\n if (any(p < uniforms.clipBoxMin.xyz) || any(p > uniforms.clipBoxMax.xyz)) {\n discard;\n }\n }\n\n // Compute normal via derivative-based flat shading.\n //\n // Industry-standard solution for BIM/CAD viewers \u2014 what\n // Three.js (material.flatShading = true), Autodesk Forge,\n // Speckle, and xeokit all do for opaque surfaces. Rationale:\n //\n // * BIM geometry is overwhelmingly flat surfaces (walls,\n // slabs, roofs, beams), and CSG operations (opening\n // subtraction, layer slicing) emit those surfaces as\n // dense strips of coplanar triangles. Per-vertex normal\n // averaging gives a SLIGHTLY-different normal at each\n // vertex due to f32 noise from boolean output; the\n // boundary between strips then reads as a visible darker/\n // brighter scar line \u2014 the horizontal striations on\n // walls, stripes on roofs, visible triangulation reports\n // across every CSG kernel we have tried (legacy BSP,\n // Manifold).\n // * cross(dpdx, dpdy) of world position evaluates to the\n // EXACT face normal in the fragment shader. Every\n // fragment on a flat face \u2014 across an arbitrarily-fine\n // triangulation \u2014 gets the IDENTICAL normal, so coplanar\n // splits become invisible by construction. No CPU-side\n // welding, smooth-grouping, or coplanar-face merging\n // fixes the symptom as cleanly.\n //\n // Trade-off: genuinely curved surfaces (cylinder tessellations,\n // BSpline approximations) shade with visible facets. For BIM\n // that's acceptable \u2014 curved surfaces are < 5 % of typical\n // model triangle count and the faceting matches CAD-tool\n // (Revit, ArchiCAD) on-screen behaviour at default quality.\n //\n // We still fall back to the vertex normal when derivatives\n // are unavailable (extreme polygon degeneracy where dpdx /\n // dpdy collapse to zero \u2014 practically never on real geometry).\n let faceN = cross(dpdx(input.worldPos), dpdy(input.worldPos));\n let fLen2 = dot(faceN, faceN);\n var N: vec3<f32>;\n if (fLen2 > 1e-10) {\n N = faceN * inverseSqrt(fLen2);\n } else {\n // Degenerate derivative \u2014 fall back to the vertex normal\n // if it's populated, else +Y.\n N = input.normal;\n let nLen2 = dot(N, N);\n if (nLen2 > 1e-6) {\n N = N * inverseSqrt(nLen2);\n } else {\n N = vec3<f32>(0.0, 1.0, 0.0);\n }\n }\n\n // Stabilize the SIGN of the derivative face normal with the vertex\n // normal. The screen-space cross product gives the exact face\n // normal DIRECTION for coplanar strips (the scar-line fix), but at\n // grazing angles its SIGN becomes numerically unstable per quad \u2014\n // hemisphere/rim lighting then band-flips across large regions of\n // flat walls/slabs (diagonal lighter/darker bands). The interpolated\n // vertex normal is quad-noise-free, so use it only to orient N.\n // Guard: skip when the vertex normal is missing or nearly\n // perpendicular to the face normal (unreliable witness).\n let vN = input.normal;\n let alignDot = dot(N, vN);\n if (alignDot * alignDot > 0.03 * dot(vN, vN)) {\n N = N * sign(alignDot);\n }\n\n // Lighting environment \u2014 sun/hemisphere/exposure come from the\n // global env uniform (defaults reproduce the historic hardcoded\n // values); fill + rim directions stay fixed in view-agnostic\n // world space as stylistic shaping lights.\n let sunLight = env.sunDirection;\n let fillLight = normalize(vec3<f32>(-0.5, 0.3, -0.3)); // Fill light\n let rimLight = normalize(vec3<f32>(0.0, 0.2, -1.0)); // Rim light for edge definition\n\n // Hemisphere ambient\n let hemisphereFactor = N.y * 0.5 + 0.5;\n let ambient = mix(env.groundColor, env.skyColor, hemisphereFactor) * env.ambientIntensity;\n\n // Two-sided sun light so inner faces (I-beam channels) stay visible.\n // sunSoftness is the diffuse wrap (env uniform): 0 = crisp\n // terminator (hard shadows), larger = softer wrap-around (overcast).\n let NdotL = abs(dot(N, sunLight));\n let wrap = env.sunSoftness;\n let diffuseSun = max((NdotL + wrap) / (1.0 + wrap), 0.0) * env.sunIntensity;\n\n // Fill light - two-sided\n let NdotFill = abs(dot(N, fillLight));\n let diffuseFill = NdotFill * env.fillIntensity;\n\n // Rim light for edge definition\n let NdotRim = max(dot(N, rimLight), 0.0);\n let rim = pow(NdotRim, 4.0) * env.rimIntensity;\n\n var baseColor = input.color.rgb;\n\n // Detect if the color is close to white/gray (low saturation)\n let baseGray = dot(baseColor, vec3<f32>(0.299, 0.587, 0.114));\n let baseSaturation = length(baseColor - vec3<f32>(baseGray)) / max(baseGray, 0.001);\n let isWhiteish = 1.0 - smoothstep(0.0, 0.3, baseSaturation);\n\n // Darken whites/grays more to reduce washed-out appearance\n baseColor = mix(baseColor, baseColor * 0.7, isWhiteish * 0.4);\n\n // Combine all lighting. Only the DIRECT sun term is occluded by cast\n // shadows (#2670); ambient/fill/rim are indirect and stay unshadowed.\n let sunShadow = sunShadowFactor(input.worldPos, N, input.position.xy);\n let lightTerm = ambient + env.sunColor * (diffuseSun * sunShadow) + vec3<f32>(diffuseFill + rim);\n var color = baseColor * lightTerm;\n\n // flags.x is a bitfield:\n // bit 0 (value 1) = isSelected \u2192 selection-highlight + force opaque\n // bit 1 (value 2) = isOverlay \u2192 color-override pass; preserve\n // baseColor.a (overlay pipeline has\n // src-alpha blending) AND skip the\n // glass-fresnel branch so low-alpha\n // ghost tints don't pick up the\n // near-white reflection tint meant\n // for real glass materials.\n // Selected via the per-draw flag (flat path) OR the per-occurrence flag\n // (instanced path \u2014 vs_instanced reads it from the instance buffer).\n let isSelected = ((uniforms.flags.x & 1u) == 1u) || ((input.instSelected & 1u) == 1u);\n let isOverlay = (uniforms.flags.x & 2u) == 2u;\n\n // Selection highlight \u2014 a blue albedo RE-LIT by the scene lighting.\n //\n // We override the material albedo with selection-blue and re-light\n // it with the SAME lightTerm used for unselected surfaces, then\n // discard the view-dependent (fresnel) term below. Two requirements\n // are in tension and this satisfies both:\n //\n // * No base-material bleed-through. The old fresnel-glow mix left\n // ~80 % of the lit object colour visible at face centres (the\n // green-site / red-roof wash-out). Here the base colour never\n // enters the result \u2014 only lightTerm (geometry/light, colour-\n // independent) modulates the constant blue albedo.\n // * Facet/crease structure must survive. A single FLAT colour\n // (the previous fix) collapsed every face to the same blue, so\n // internal edges \u2014 which read as the per-face shading STEP, not\n // just the faint screen-space edge line \u2014 disappeared on\n // selection. Re-lighting keeps that per-face brightness step, so\n // creases read on the highlight exactly as they do unselected.\n //\n // The luminance of lightTerm is remapped by a multiplicative gain\n // (which preserves the per-face brightness RATIOS, so creases read\n // as strongly as on the unselected surface) calibrated so a sunlit\n // face hits full selection-blue, with a floor/ceiling clamp so\n // shadowed faces only dim and bright scenes never wash out.\n if (isSelected) {\n let shadeLum = dot(lightTerm, vec3<f32>(0.299, 0.587, 0.114));\n let shade = clamp(shadeLum * 1.55, 0.45, 1.2);\n color = vec3<f32>(0.3, 0.6, 1.0) * shade;\n }\n\n // flags.x bit 5 (value 32) = EMPHASIZE overlay: render the colour\n // override FULLY UNLIT and saturated (no lighting attenuation, no\n // wash-to-white) so the focused clash pair reads as a solid, vivid,\n // distinct colour that pops against the lit model \u2014 like a clash tool.\n // A faint normal-based shade keeps the silhouette from going flat. (#1277)\n let emphasizedOverlay = isOverlay && (uniforms.flags.x & 32u) != 0u;\n if (emphasizedOverlay) {\n let facet = 0.85 + 0.15 * abs(dot(N, normalize(vec3<f32>(0.3, 1.0, 0.2))));\n color = baseColor * facet;\n }\n\n // Beautiful fresnel effect for transparent materials (glass)\n // Skip when selected \u2014 the glass shine and desaturation wash out the\n // blue highlight, making it appear white instead of blue.\n // Also force alpha to 1.0 for selected objects so the highlight is\n // fully opaque (the selection pipeline has no alpha blending).\n // Emphasized clash overlay paints a SOLID vivid fill (force opaque) so\n // it isn't blended down to a pale tint against the geometry beneath.\n var finalAlpha = select(input.color.a, 1.0, isSelected || emphasizedOverlay);\n if (finalAlpha < 0.99 && !isSelected && !isOverlay) {\n // Calculate view direction for fresnel\n let V = normalize(-input.worldPos);\n let NdotV = max(dot(N, V), 0.0);\n\n // Enhanced fresnel effect - stronger at edges (grazing angles)\n // Using Schlick's approximation for realistic glass reflection\n let fresnelPower = 1.5; // Higher = softer edge reflections\n let fresnel = pow(1.0 - NdotV, fresnelPower);\n\n // Glass reflection tint (sky/environment reflection at edges)\n let reflectionTint = vec3<f32>(0.92, 0.96, 1.0); // Cool sky reflection\n let reflectionStrength = fresnel * 0.6; // Strong edge reflections\n\n // Mix in reflection tint at edges\n color = mix(color, color * reflectionTint, reflectionStrength);\n\n // Add realistic glass shine - brighter at edges where light reflects\n let glassShine = fresnel * 0.12;\n color += glassShine;\n\n // Slight desaturation at edges (glass reflects environment, not just color)\n let edgeDesaturation = fresnel * 0.25;\n let gray = dot(color, vec3<f32>(0.299, 0.587, 0.114));\n color = mix(color, vec3<f32>(gray), edgeDesaturation);\n\n // Make glass more transparent (reduce opacity by 30%)\n finalAlpha = finalAlpha * 0.7;\n }\n\n // Exposure adjustment (historic default 0.85 darkens overall)\n color *= env.exposure;\n\n // Contrast enhancement\n color = (color - 0.5) * 1.15 + 0.5;\n color = max(color, vec3<f32>(0.0));\n\n // Saturation boost - stronger for colored surfaces, less for whites\n let gray = dot(color, vec3<f32>(0.299, 0.587, 0.114));\n // More saturation for colored surfaces. isWhiteish is derived from\n // the base material colour, so for a SELECTED object it would leak a\n // material dependence into the highlight (breaking the no-bleed-\n // through contract). The selection blue is a fully-saturated colour,\n // so force the colored-surface boost (1.4) when selected \u2014 keeping\n // the highlight identical regardless of the underlying material.\n let satBoost = select(mix(1.4, 1.1, isWhiteish), 1.4, isSelected);\n color = mix(vec3<f32>(gray), color, satBoost);\n\n // ACES filmic tone mapping\n let a = 2.51;\n let b = 0.03;\n let c = 2.43;\n let d = 0.59;\n let e = 0.14;\n color = clamp((color * (a * color + b)) / (color * (c * color + d) + e), vec3<f32>(0.0), vec3<f32>(1.0));\n\n // Subtle edge enhancement using screen-space derivatives.\n //\n // Use the SHADED normal (face normal from dpdx/dpdy above)\n // for the normal-gradient term, not the interpolated vertex\n // normal \u2014 otherwise we get spurious dark stripes on flat\n // surfaces whose vertex normals carry numerical noise from\n // CSG output (the visible scar-line symptom would just\n // resurface here even after the lit-normal fix). With the\n // face normal, coplanar adjacent triangles agree exactly \u2192\n // zero normal gradient \u2192 no false edge; only the genuine\n // creases between perpendicular faces produce a real\n // gradient and get the intended outline.\n let depthGradient = length(vec2<f32>(\n dpdx(input.viewPos.z),\n dpdy(input.viewPos.z)\n ));\n let normalGradient = length(vec2<f32>(\n length(dpdx(N)),\n length(dpdy(N))\n ));\n\n if (uniforms.flags.z == 1u) {\n // Threshold filters subtle normal discontinuities at internal\n // triangle edges between coplanar entities in the same batch.\n let edgeFactor = smoothstep(0.02, 0.12, depthGradient * 10.0 + normalGradient * 5.0);\n let edgeIntensity = f32(uniforms.flags.w) / 1000.0;\n let edgeDarkenStrength = clamp(0.25 * edgeIntensity, 0.0, 0.85);\n let edgeDarken = mix(1.0, 1.0 - edgeDarkenStrength, edgeFactor);\n color *= edgeDarken;\n }\n\n // Gamma correction\n color = pow(color, vec3<f32>(1.0 / 2.2));\n\n var out: FragmentOutput;\n out.color = vec4<f32>(color, finalAlpha);\n out.objectIdEncoded = encodeId24(input.entityId);\n return out;\n }\n ";
|
|
1
|
+
export declare const mainShaderSource = "\n struct Uniforms {\n viewProj: mat4x4<f32>,\n model: mat4x4<f32>,\n baseColor: vec4<f32>,\n metallicRoughness: vec2<f32>, // x = metallic, y = roughness\n _padding1: vec2<f32>,\n sectionPlane: vec4<f32>, // xyz = plane normal, w = plane distance\n flags: vec4<u32>, // x = isSelected, y = section/clip bits, z = edgeEnabled, w = edgeIntensityMilli\n clipBoxMin: vec4<f32>, // xyz = clip-box min corner (world), w = pad\n clipBoxMax: vec4<f32>, // xyz = clip-box max corner (world), w = pad\n quantParams: vec4<f32>, // local min xyz, lattice step w\n rteViewProj: mat4x4<f32>, // appended frame; bit 16 selects it\n drawableDeltaHigh: vec4<f32>,\n drawableDeltaLow: vec4<f32>,\n rteCameraHigh: vec4<f32>,\n rteCameraLow: vec4<f32>,\n }\n @binding(0) @group(0) var<uniform> uniforms: Uniforms;\n const RTE_DRAWABLE_FLAG: u32 = 65536u;\n \nstruct RteFrameUniform {\n // Camera translation is intentionally absent. This projects positions\n // returned by rteWorldPosition, which are already relative to the eye.\n viewProj: mat4x4<f32>,\n}\n\nstruct RteDrawableUniform {\n // CPU computes (drawable origin - camera origin) in f64 and splits it.\n // This data must be refreshed whenever the RTE camera frame changes.\n drawableDeltaHigh: vec4<f32>,\n drawableDeltaLow: vec4<f32>,\n}\n\nfn rteWorldPosition(\n local: vec3<f32>,\n drawable: RteDrawableUniform,\n) -> vec4<f32> {\n // Do not subtract a camera origin here: this delta was formed in CPU f64.\n let highDelta = drawable.drawableDeltaHigh.xyz;\n let lowDelta = drawable.drawableDeltaLow.xyz;\n // A drawable template can span kilometres. Keep local with high first;\n // The local-plus-(high-plus-low) association is not equivalent in f32.\n return vec4<f32>((local + highDelta) + lowDelta, 1.0);\n}\n\n \n fn rtePosition(local: vec3<f32>) -> vec4<f32> {\n let linear = (uniforms.model * vec4<f32>(local, 0.0)).xyz;\n return rteWorldPosition(linear, RteDrawableUniform(\n uniforms.drawableDeltaHigh,\n uniforms.drawableDeltaLow,\n ));\n }\n fn rteInstancePosition(local: vec3<f32>, anchorHigh: vec3<f32>, anchorLow: vec3<f32>) -> vec4<f32> {\n return rteWorldPosition(local, RteDrawableUniform(\n vec4<f32>(anchorHigh, 0.0),\n vec4<f32>(anchorLow, 0.0),\n ));\n }\n // Shared group(1) lighting; packing matches packEnvironmentUniforms().\n struct Environment {\n sunDirection: vec3<f32>, // unit vector TOWARD the sun\n sunIntensity: f32,\n sunColor: vec3<f32>,\n ambientIntensity: f32,\n skyColor: vec3<f32>, // hemisphere-ambient sky tint\n exposure: f32,\n groundColor: vec3<f32>, // hemisphere-ambient ground tint\n fillIntensity: f32,\n rimIntensity: f32,\n sunSoftness: f32,\n _pad1: f32,\n _pad2: f32,\n }\n @binding(0) @group(1) var<uniform> env: Environment;\n const IRRADIANCE_CALIBRATION: f32 = 1.82;\n \n fn srgbToLinear(c: vec3<f32>) -> vec3<f32> {\n let lo = c / 12.92;\n let hi = pow((max(c, vec3<f32>(0.0)) + 0.055) / 1.055, vec3<f32>(2.4));\n return select(hi, lo, c <= vec3<f32>(0.04045));\n }\n\n fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {\n let lo = c * 12.92;\n let hi = 1.055 * pow(max(c, vec3<f32>(0.0)), vec3<f32>(1.0 / 2.4)) - 0.055;\n return select(hi, lo, c <= vec3<f32>(0.0031308));\n }\n\n fn neutralCompress(cIn: vec3<f32>) -> vec3<f32> {\n let startCompression = 0.76;\n let desaturation = 0.15;\n let c = max(cIn, vec3<f32>(0.0));\n let peak = max(c.r, max(c.g, c.b));\n if (peak < startCompression) { return c; }\n let d = 1.0 - startCompression;\n let newPeak = 1.0 - d * d / (peak + d - startCompression);\n let g = 1.0 - 1.0 / (desaturation * (peak - newPeak) + 1.0);\n return mix(c * (newPeak / peak), vec3<f32>(newPeak), g);\n }\n\n\n \n @binding(1) @group(1) var shadowMap: texture_depth_2d;\n @binding(2) @group(1) var shadowCmp: sampler_comparison;\n struct Shadow { lightViewProj: mat4x4<f32>, params: vec4<f32>, params2: vec4<f32>, }\n @binding(3) @group(1) var<uniform> shadowU: Shadow;\n const SHADOW_POISSON = array<vec2<f32>, 12>(\n vec2<f32>(-0.326, -0.406), vec2<f32>(-0.840, -0.074), vec2<f32>(-0.696, 0.457),\n vec2<f32>(-0.203, 0.621), vec2<f32>( 0.962, -0.195), vec2<f32>( 0.473, -0.480),\n vec2<f32>( 0.519, 0.767), vec2<f32>( 0.185, -0.893), vec2<f32>( 0.507, 0.064),\n vec2<f32>( 0.896, 0.412), vec2<f32>(-0.322, -0.933), vec2<f32>(-0.792, -0.598),\n );\n fn sunShadowFactor(eyePos: vec3<f32>, N: vec3<f32>, fragCoord: vec2<f32>) -> f32 {\n if (shadowU.params.y < 0.5) { return 1.0; }\n let L = normalize(env.sunDirection);\n let Ns = N * select(-1.0, 1.0, dot(N, L) >= 0.0);\n let biased = eyePos + Ns * shadowU.params.z;\n let clip = shadowU.lightViewProj * vec4<f32>(biased, 1.0);\n let ndc = clip.xyz / clip.w;\n let uv = vec2<f32>(ndc.x * 0.5 + 0.5, ndc.y * -0.5 + 0.5);\n let inBounds = uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0 && ndc.z > 0.0;\n if (!inBounds) { return 1.0; }\n let NdotL = max(dot(Ns, L), 0.0);\n let slope = clamp(sqrt(max(1.0 - NdotL * NdotL, 0.0)) / max(NdotL, 0.1), 1.0, 12.0);\n let refDepth = ndc.z + shadowU.params2.x * slope * (1.0 + shadowU.params.w);\n let radius = shadowU.params.x * shadowU.params.w;\n let ign = fract(52.9829189 * fract(dot(fragCoord, vec2<f32>(0.06711056, 0.00583715))));\n let ang = ign * 6.2831853;\n let cr = cos(ang); let sr = sin(ang); var sum = 0.0;\n for (var i = 0; i < 12; i = i + 1) {\n let p = SHADOW_POISSON[i];\n let off = vec2<f32>(p.x * cr - p.y * sr, p.x * sr + p.y * cr) * radius;\n sum = sum + textureSampleCompareLevel(shadowMap, shadowCmp, uv + off, refDepth);\n }\n return mix(1.0, sum / 12.0, smoothstep(0.0, 0.3, NdotL));\n }\n\n struct VertexInput {\n @location(0) position: vec3<f32>,\n @location(1) normal: vec3<f32>,\n @location(2) entityId: u32,\n }\n\n struct VertexOutput {\n @builtin(position) position: vec4<f32>,\n @location(0) worldPos: vec3<f32>,\n @location(1) normal: vec3<f32>,\n @location(2) @interpolate(flat) entityId: u32,\n @location(3) viewPos: vec3<f32>, // For edge detection\n // Per-draw albedo carried from the vertex stage so the fragment shader\n // is shared by the flat path (vs_main writes uniforms.baseColor \u2014 the\n // per-batch / overlay-override colour) AND the instanced path\n // (vs_instanced writes the per-occurrence colour from the instance\n // buffer). For the flat path this is identical to reading\n // uniforms.baseColor directly (the value is constant across the draw).\n @location(4) color: vec4<f32>,\n // Per-occurrence selection flag for the instanced path (bit 0 = selected).\n // vs_main writes 0 (the flat path selects via uniforms.flags.x instead);\n // vs_instanced writes the per-instance flag from the instance buffer, so a\n // single selected occurrence highlights without re-drawing.\n @location(5) @interpolate(flat) instSelected: u32,\n // Camera-relative position for fragment operations that compare\n // geometry to section/crop boundaries or take derivatives. This is\n // intentionally separate from worldPos: shadow maps still consume\n // their established world-space light matrix until their depth pass\n // is migrated as one transaction.\n @location(6) eyePos: vec3<f32>,\n }\n\n // Per-instance vertex-buffer inputs (slot 1, stepMode 'instance') used by\n // vs_instanced. The mat4 arrives as four COLUMN vec4s (WGSL mat4x4 is\n // column-major), matching composeInstanceMatrix's column-major output +\n // the pipeline's slot-1 attribute offsets (0/16/32/48).\n //\n // Location namespaces: vertex-INPUT @location (this struct + VertexInput)\n // and inter-stage @location (VertexOutput) are INDEPENDENT in WGSL, so\n // InstanceInput.m1 @location(4) does NOT collide with VertexOutput.color\n // @location(4) \u2014 exactly as VertexInput.entityId and VertexOutput.entityId\n // already BOTH use @location(2). Within the INPUT namespace the per-vertex\n // inputs (0..2) and per-instance inputs (3..8) stay distinct.\n struct InstanceInput {\n @location(3) m0: vec4<f32>,\n @location(4) m1: vec4<f32>,\n @location(5) m2: vec4<f32>,\n @location(6) m3: vec4<f32>,\n @location(7) instEntityId: u32,\n @location(8) instColor: vec4<f32>,\n @location(9) instSelected: u32,\n @location(10) anchorHigh: vec4<f32>,\n @location(11) anchorLow: vec4<f32>,\n }\n\n // 12-byte quantized vertex (issue #1682 phase 6): uint16x4 (lattice\n // position xyz + packed octahedral normal in w as (u8x << 8 | u8y))\n // followed by the u32 entityId lane. Dequantization is BIT-EXACT for\n // the 2^-10 lattice: quantMin and q*step are both (integer)\u00B72^-10,\n // so coincident points across batches stay coincident (the shared-\n // origin z-fight guarantee survives quantization).\n struct QuantizedVertexInput {\n @location(0) q: vec4<u32>, // uint16x4: x, y, z, packedOct\n @location(2) entityId: u32,\n }\n\n fn octDecodeN(packed: u32) -> vec3<f32> {\n let ox = (f32((packed >> 8u) & 255u) / 255.0) * 2.0 - 1.0;\n let oy = (f32(packed & 255u) / 255.0) * 2.0 - 1.0;\n var n = vec3<f32>(ox, oy, 1.0 - abs(ox) - abs(oy));\n if (n.z < 0.0) {\n let tx = (1.0 - abs(oy)) * select(-1.0, 1.0, ox >= 0.0);\n let ty = (1.0 - abs(ox)) * select(-1.0, 1.0, oy >= 0.0);\n n = vec3<f32>(tx, ty, n.z);\n }\n return normalize(n);\n }\n\n // Shared flat-path vertex shading \u2014 vs_main and vs_main_quantized\n // differ ONLY in how position/normal are sourced.\n fn shadeFlatVertex(localPos: vec3<f32>, localNormal: vec3<f32>, entityId: u32) -> VertexOutput {\n var output: VertexOutput;\n let worldPos = uniforms.model * vec4<f32>(localPos, 1.0);\n let eyePos = rtePosition(localPos).xyz;\n let rte = (uniforms.flags.x & RTE_DRAWABLE_FLAG) != 0u;\n output.position = select(uniforms.viewProj * worldPos, uniforms.rteViewProj * vec4<f32>(eyePos, 1.0), rte);\n // Anti z-fighting depth nudge \u2014 see vs_main's comment.\n let colorSalt = (entityId >> 24u) * 2654435761u;\n let zHash = (((entityId & 0x00FFFFFFu) ^ colorSalt) * 2654435761u) & 255u;\n output.position.z *= 1.0 + f32(zHash) * 1e-6;\n output.worldPos = worldPos.xyz;\n output.normal = normalize((uniforms.model * vec4<f32>(localNormal, 0.0)).xyz);\n output.entityId = entityId;\n output.color = uniforms.baseColor;\n output.instSelected = 0u;\n output.eyePos = eyePos;\n output.viewPos = select((uniforms.viewProj * worldPos).xyz, (uniforms.rteViewProj * vec4<f32>(eyePos, 1.0)).xyz, rte);\n return output;\n }\n\n @vertex\n fn vs_main_quantized(input: QuantizedVertexInput) -> VertexOutput {\n let p = uniforms.quantParams.xyz\n + vec3<f32>(f32(input.q.x), f32(input.q.y), f32(input.q.z)) * uniforms.quantParams.w;\n return shadeFlatVertex(p, octDecodeN(input.q.w), input.entityId);\n }\n\n @vertex\n fn vs_main(input: VertexInput, @builtin(instance_index) instanceIndex: u32) -> VertexOutput {\n var output: VertexOutput;\n let worldPos = uniforms.model * vec4<f32>(input.position, 1.0);\n let eyePos = rtePosition(input.position).xyz;\n let rte = (uniforms.flags.x & RTE_DRAWABLE_FLAG) != 0u;\n output.position = select(uniforms.viewProj * worldPos, uniforms.rteViewProj * vec4<f32>(eyePos, 1.0), rte);\n // Anti z-fighting: deterministic depth nudge.\n // Knuth multiplicative hash spreads sequential IDs across 0-255 so\n // coplanar faces from different entities always get distinct depths.\n // Material-layer walls slice into one closed solid per layer, all\n // sharing the PARENT wall's expressId, so adjacent layers' coincident\n // interface caps would get the same entity nudge and z-fight into a\n // flickering comb (\"see inside the wall\"). To separate them we fold in\n // an 8-bit MATERIAL-COLOUR salt that mergeGeometry/interleaveTextured\n // baked into the HIGH 8 bits of the entityId lane (low 24 = picking id,\n // masked off by encodeId24). Crucially the salt comes from the mesh's\n // OWN colour, NOT the per-draw baseColor uniform \u2014 so the base opaque\n // pass and the lens/IDS/compare/4D OVERLAY pass (which redraws the same\n // geometry with a DIFFERENT draw colour) compute the SAME nudge, and\n // the overlay pipeline's depthCompare:'equal' matches instead of\n // rejecting every fragment. At 1e-6 per step the max world-space offset\n // is <3mm at 10m \u2014 invisible.\n let colorSalt = (input.entityId >> 24u) * 2654435761u;\n let zHash = (((input.entityId & 0x00FFFFFFu) ^ colorSalt) * 2654435761u) & 255u;\n output.position.z *= 1.0 + f32(zHash) * 1e-6;\n output.worldPos = worldPos.xyz;\n output.normal = normalize((uniforms.model * vec4<f32>(input.normal, 0.0)).xyz);\n output.entityId = input.entityId;\n output.color = uniforms.baseColor;\n output.instSelected = 0u; // flat path selects via uniforms.flags.x\n output.eyePos = eyePos;\n // Store view-space position for edge detection\n output.viewPos = select((uniforms.viewProj * worldPos).xyz, (uniforms.rteViewProj * vec4<f32>(eyePos, 1.0)).xyz, rte);\n return output;\n }\n\n // Instanced vertex entry \u2014 one template's geometry drawn once per\n // occurrence. The per-instance mat4 already folds SWAP * rel_k * T(origin)\n // (composed CPU-side, see instanced-render.ts), so it maps the template's\n // LOCAL vertex straight to WebGL Y-up world space \u2014 no uniforms.model.\n // rel_k and SWAP are rigid (no scale), so the same matrix transforms\n // normals. entityId + colour come per-occurrence from the instance buffer.\n @vertex\n fn vs_instanced(input: VertexInput, inst: InstanceInput) -> VertexOutput {\n var output: VertexOutput;\n let instMat = mat4x4<f32>(inst.m0, inst.m1, inst.m2, inst.m3);\n let worldPos = instMat * vec4<f32>(input.position, 1.0);\n let linearLocal = (instMat * vec4<f32>(input.position, 0.0)).xyz;\n let eyePos = rteInstancePosition(linearLocal, inst.anchorHigh.xyz, inst.anchorLow.xyz).xyz;\n output.position = uniforms.rteViewProj * vec4<f32>(eyePos, 1.0);\n // Same per-entity depth nudge as vs_main. No colour salt here: the\n // instanced path has no base-vs-overlay coincident redraw (yet), so the\n // raw picking id is enough to separate coplanar entities.\n let zHash = ((inst.instEntityId & 0x00FFFFFFu) * 2654435761u) & 255u;\n output.position.z *= 1.0 + f32(zHash) * 1e-6;\n output.worldPos = worldPos.xyz;\n output.normal = normalize((instMat * vec4<f32>(input.normal, 0.0)).xyz);\n output.entityId = inst.instEntityId;\n output.color = inst.instColor;\n output.instSelected = inst.instSelected;\n output.eyePos = eyePos;\n output.viewPos = (uniforms.rteViewProj * vec4<f32>(eyePos, 1.0)).xyz;\n return output;\n }\n\n // PBR helper functions\n fn fresnelSchlick(cosTheta: f32, F0: vec3<f32>) -> vec3<f32> {\n return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);\n }\n\n fn distributionGGX(NdotH: f32, roughness: f32) -> f32 {\n let a = roughness * roughness;\n let a2 = a * a;\n let NdotH2 = NdotH * NdotH;\n let num = a2;\n let denomBase = (NdotH2 * (a2 - 1.0) + 1.0);\n let denom = 3.14159265 * denomBase * denomBase;\n return num / max(denom, 0.0000001);\n }\n\n fn geometrySchlickGGX(NdotV: f32, roughness: f32) -> f32 {\n let r = (roughness + 1.0);\n let k = (r * r) / 8.0;\n let num = NdotV;\n let denom = NdotV * (1.0 - k) + k;\n return num / max(denom, 0.0000001);\n }\n\n fn geometrySmith(NdotV: f32, NdotL: f32, roughness: f32) -> f32 {\n let ggx2 = geometrySchlickGGX(NdotV, roughness);\n let ggx1 = geometrySchlickGGX(NdotL, roughness);\n return ggx1 * ggx2;\n }\n\n fn encodeId24(id: u32) -> vec4<f32> {\n let r = f32((id >> 16u) & 255u) / 255.0;\n let g = f32((id >> 8u) & 255u) / 255.0;\n let b = f32(id & 255u) / 255.0;\n return vec4<f32>(r, g, b, 1.0);\n }\n\n struct FragmentOutput {\n @location(0) color: vec4<f32>,\n @location(1) objectIdEncoded: vec4<f32>,\n }\n\n @fragment\n fn fs_main(input: VertexOutput) -> FragmentOutput {\n // The flat/quantized/textured mesh paths submit this in one\n // camera-relative frame. Do all camera-local fragment arithmetic in\n // that frame; otherwise the RTE vertex precision is thrown away at\n // section/crop/derivative ingress. Instanced geometry is not yet\n // anchored, so its worldPos remains the authoritative input.\n let fragmentPos = select(input.worldPos, input.eyePos, (uniforms.flags.x & RTE_DRAWABLE_FLAG) != 0u);\n // Per-instance hide/isolate: bit 1 of the instance flags lane marks a hidden\n // occurrence. Discard it so it neither draws nor writes depth (and the pick\n // pass applies the same discard, so it isn't pickable). vs_main writes\n // instSelected=0u for flat geometry, so this never affects the flat path.\n if ((input.instSelected & 2u) != 0u) {\n discard;\n }\n // Per-instance opacity routing (instanced passes only \u2014 flags.x bit 2). The\n // opaque instanced pass draws fully-opaque (or selected) occurrences; the\n // transparent instanced sub-pass (bit 3, alpha-blended) draws the rest. Discard\n // the occurrences belonging to the OTHER pass so each is drawn exactly once.\n // Lens-ghost / x-ray / compare write a low per-instance alpha into input.color.a.\n if ((uniforms.flags.x & 4u) != 0u) {\n let occOpaque = input.color.a >= 0.99 || (input.instSelected & 1u) != 0u;\n let transparentPass = (uniforms.flags.x & 8u) != 0u;\n if (transparentPass) {\n if (occOpaque) { discard; }\n } else {\n if (!occOpaque) { discard; }\n }\n }\n // Section plane clipping - discard fragments ABOVE the plane.\n // flags.y packs two bits: bit 0 = enabled, bit 1 = flipped.\n let sectionEnabled = (uniforms.flags.y & 1u) == 1u;\n if (sectionEnabled) {\n let planeNormal = uniforms.sectionPlane.xyz;\n let planeDistance = uniforms.sectionPlane.w;\n let flipped = (uniforms.flags.y & 2u) == 2u;\n let side = select(1.0, -1.0, flipped);\n let distToPlane = (dot(fragmentPos, planeNormal) - planeDistance) * side;\n if (distToPlane > 0.0) {\n discard;\n }\n }\n // Clip box (section / crop box): discard fragments OUTSIDE the AABB.\n // flags.y bit 2 = clip-box enabled.\n if ((uniforms.flags.y & 4u) != 0u) {\n let p = fragmentPos;\n if (any(p < uniforms.clipBoxMin.xyz) || any(p > uniforms.clipBoxMax.xyz)) {\n discard;\n }\n }\n\n // Compute normal via derivative-based flat shading.\n //\n // Industry-standard solution for BIM/CAD viewers \u2014 what\n // Three.js (material.flatShading = true), Autodesk Forge,\n // Speckle, and xeokit all do for opaque surfaces. Rationale:\n //\n // * BIM geometry is overwhelmingly flat surfaces (walls,\n // slabs, roofs, beams), and CSG operations (opening\n // subtraction, layer slicing) emit those surfaces as\n // dense strips of coplanar triangles. Per-vertex normal\n // averaging gives a SLIGHTLY-different normal at each\n // vertex due to f32 noise from boolean output; the\n // boundary between strips then reads as a visible darker/\n // brighter scar line \u2014 the horizontal striations on\n // walls, stripes on roofs, visible triangulation reports\n // across every CSG kernel we have tried (legacy BSP,\n // Manifold).\n // * cross(dpdx, dpdy) of world position evaluates to the\n // EXACT face normal in the fragment shader. Every\n // fragment on a flat face \u2014 across an arbitrarily-fine\n // triangulation \u2014 gets the IDENTICAL normal, so coplanar\n // splits become invisible by construction. No CPU-side\n // welding, smooth-grouping, or coplanar-face merging\n // fixes the symptom as cleanly.\n //\n // Trade-off: genuinely curved surfaces (cylinder tessellations,\n // BSpline approximations) shade with visible facets. For BIM\n // that's acceptable \u2014 curved surfaces are < 5 % of typical\n // model triangle count and the faceting matches CAD-tool\n // (Revit, ArchiCAD) on-screen behaviour at default quality.\n //\n // We still fall back to the vertex normal when derivatives\n // are unavailable (extreme polygon degeneracy where dpdx /\n // dpdy collapse to zero \u2014 practically never on real geometry).\n let faceN = cross(dpdx(fragmentPos), dpdy(fragmentPos));\n let fLen2 = dot(faceN, faceN);\n var N: vec3<f32>;\n if (fLen2 > 1e-10) {\n N = faceN * inverseSqrt(fLen2);\n } else {\n // Degenerate derivative \u2014 fall back to the vertex normal\n // if it's populated, else +Y.\n N = input.normal;\n let nLen2 = dot(N, N);\n if (nLen2 > 1e-6) {\n N = N * inverseSqrt(nLen2);\n } else {\n N = vec3<f32>(0.0, 1.0, 0.0);\n }\n }\n\n // Stabilize the SIGN of the derivative face normal with the vertex\n // normal. The screen-space cross product gives the exact face\n // normal DIRECTION for coplanar strips (the scar-line fix), but at\n // grazing angles its SIGN becomes numerically unstable per quad \u2014\n // hemisphere/rim lighting then band-flips across large regions of\n // flat walls/slabs (diagonal lighter/darker bands). The interpolated\n // vertex normal is quad-noise-free, so use it only to orient N.\n // Guard: skip when the vertex normal is missing or nearly\n // perpendicular to the face normal (unreliable witness).\n let vN = input.normal;\n let alignDot = dot(N, vN);\n if (alignDot * alignDot > 0.03 * dot(vN, vN)) {\n N = N * sign(alignDot);\n }\n\n // Lighting environment \u2014 sun/hemisphere/exposure come from the\n // global env uniform. The fill follows the sun (it bounces in from\n // the opposite side); the rim stays fixed in world space as a\n // stylistic shaping light.\n let sunLight = env.sunDirection;\n // Horizontal mirror of the sun, lifted slightly. An overhead sun\n // leaves (0, 0.25, 0), which normalizes to straight up.\n let fillLight = normalize(vec3<f32>(-sunLight.x, 0.25, -sunLight.z));\n let rimLight = normalize(vec3<f32>(0.0, 0.2, -1.0)); // Rim light for edge definition\n\n // Hemisphere ambient. This, not the sun, keeps faces turned away\n // from the sun readable (I-beam webs and flange undersides, #5382).\n let hemisphereFactor = N.y * 0.5 + 0.5;\n let ambient = mix(env.groundColor, env.skyColor, hemisphereFactor) * env.ambientIntensity;\n\n // One-sided sun, so a building has a lit side and a shaded side.\n // sunSoftness is the diffuse wrap (env uniform): 0 = crisp\n // terminator (hard shadows), larger = softer wrap-around (overcast).\n let NdotL = dot(N, sunLight);\n let wrap = env.sunSoftness;\n let diffuseSun = max((NdotL + wrap) / (1.0 + wrap), 0.0) * env.sunIntensity;\n\n // Fill light, one-sided like the sun.\n let NdotFill = max(dot(N, fillLight), 0.0);\n let diffuseFill = NdotFill * env.fillIntensity;\n\n // Rim light for edge definition\n let NdotRim = max(dot(N, rimLight), 0.0);\n let rim = pow(NdotRim, 4.0) * env.rimIntensity;\n\n // The authored colour is display-referred sRGB; light it in linear.\n // (textured.wgsl.ts anchors on the first line to multiply in the texel.)\n var baseColor = input.color.rgb;\n baseColor = srgbToLinear(baseColor);\n\n // Combine all lighting. Only the DIRECT sun term is occluded by cast\n // shadows (#2670); ambient/fill/rim are indirect and stay unshadowed.\n // Exposure scales the light, so the selection shade below and every\n // later stage see one exposed irradiance.\n let sunShadow = sunShadowFactor(input.eyePos, N, input.position.xy);\n let lightTerm = ambient + env.sunColor * (diffuseSun * sunShadow) + vec3<f32>(diffuseFill + rim);\n let irradiance = lightTerm * (env.exposure * IRRADIANCE_CALIBRATION);\n var color = baseColor * irradiance;\n\n // flags.x is a bitfield:\n // bit 0 (value 1) = isSelected \u2192 selection-highlight + force opaque\n // bit 1 (value 2) = isOverlay \u2192 color-override pass; preserve\n // baseColor.a (overlay pipeline has\n // src-alpha blending) AND skip the\n // glass-fresnel branch so low-alpha\n // ghost tints don't pick up the\n // near-white reflection tint meant\n // for real glass materials.\n // Selected via the per-draw flag (flat path) OR the per-occurrence flag\n // (instanced path \u2014 vs_instanced reads it from the instance buffer).\n let isSelected = ((uniforms.flags.x & 1u) == 1u) || ((input.instSelected & 1u) == 1u);\n let isOverlay = (uniforms.flags.x & 2u) == 2u;\n\n // Selection highlight \u2014 a blue albedo RE-LIT by the scene lighting.\n //\n // We override the material albedo with selection-blue and re-light\n // it with the SAME lightTerm used for unselected surfaces, then\n // discard the view-dependent (fresnel) term below. Two requirements\n // are in tension and this satisfies both:\n //\n // * No base-material bleed-through. The old fresnel-glow mix left\n // ~80 % of the lit object colour visible at face centres (the\n // green-site / red-roof wash-out). Here the base colour never\n // enters the result \u2014 only lightTerm (geometry/light, colour-\n // independent) modulates the constant blue albedo.\n // * Facet/crease structure must survive. A single FLAT colour\n // (the previous fix) collapsed every face to the same blue, so\n // internal edges \u2014 which read as the per-face shading STEP, not\n // just the faint screen-space edge line \u2014 disappeared on\n // selection. Re-lighting keeps that per-face brightness step, so\n // creases read on the highlight exactly as they do unselected.\n //\n // The luminance of the exposed irradiance is used as a multiplicative\n // gain (which preserves the per-face brightness RATIOS, so creases\n // read as strongly as on the unselected surface). The calibration\n // puts a sunlit face at 1.0, i.e. full selection-blue, and the\n // floor/ceiling clamp keeps shadowed faces only dimmed and bright\n // scenes from washing out.\n if (isSelected) {\n let shadeLum = dot(irradiance, vec3<f32>(0.299, 0.587, 0.114));\n let shade = clamp(shadeLum, 0.45, 1.2);\n color = srgbToLinear(vec3<f32>(0.3, 0.6, 1.0)) * shade;\n }\n\n // flags.x bit 5 (value 32) = EMPHASIZE overlay: render the colour\n // override FULLY UNLIT and saturated (no lighting attenuation, no\n // wash-to-white) so the focused clash pair reads as a solid, vivid,\n // distinct colour that pops against the lit model \u2014 like a clash tool.\n // A faint normal-based shade keeps the silhouette from going flat. (#1277)\n let emphasizedOverlay = isOverlay && (uniforms.flags.x & 32u) != 0u;\n if (emphasizedOverlay) {\n let facet = 0.85 + 0.15 * abs(dot(N, normalize(vec3<f32>(0.3, 1.0, 0.2))));\n color = baseColor * facet;\n }\n\n // Beautiful fresnel effect for transparent materials (glass)\n // Skip when selected \u2014 the glass shine and desaturation wash out the\n // blue highlight, making it appear white instead of blue.\n // Also force alpha to 1.0 for selected objects so the highlight is\n // fully opaque (the selection pipeline has no alpha blending).\n // Emphasized clash overlay paints a SOLID vivid fill (force opaque) so\n // it isn't blended down to a pale tint against the geometry beneath.\n var finalAlpha = select(input.color.a, 1.0, isSelected || emphasizedOverlay);\n if (finalAlpha < 0.99 && !isSelected && !isOverlay) {\n // Calculate view direction for fresnel\n let V = normalize(-fragmentPos);\n let NdotV = max(dot(N, V), 0.0);\n\n // Enhanced fresnel effect - stronger at edges (grazing angles)\n // Using Schlick's approximation for realistic glass reflection\n let fresnelPower = 1.5; // Higher = softer edge reflections\n let fresnel = pow(1.0 - NdotV, fresnelPower);\n\n // Glass reflection tint (sky/environment reflection at edges)\n let reflectionTint = vec3<f32>(0.92, 0.96, 1.0); // Cool sky reflection\n let reflectionStrength = fresnel * 0.6; // Strong edge reflections\n\n // Mix in reflection tint at edges\n color = mix(color, color * reflectionTint, reflectionStrength);\n\n // Add realistic glass shine - brighter at edges where light reflects.\n // Linear amount: 0.06 lifts a mid-tone about as much as the 0.12\n // the pre-linear pipeline added in display space.\n let glassShine = fresnel * 0.06;\n color += glassShine;\n\n // Slight desaturation at edges (glass reflects environment, not just color)\n let edgeDesaturation = fresnel * 0.25;\n let gray = dot(color, vec3<f32>(0.299, 0.587, 0.114));\n color = mix(color, vec3<f32>(gray), edgeDesaturation);\n\n // Make glass more transparent (reduce opacity by 30%)\n finalAlpha = finalAlpha * 0.7;\n }\n\n // Hue-preserving highlight roll-off (color-transfer.wgsl.ts). No\n // contrast curve and no saturation boost: an authored colour lit at\n // unit irradiance leaves here unchanged unless it is brighter than\n // the roll-off threshold.\n color = neutralCompress(color);\n\n // Subtle edge enhancement using screen-space derivatives.\n //\n // Use the SHADED normal (face normal from dpdx/dpdy above)\n // for the normal-gradient term, not the interpolated vertex\n // normal \u2014 otherwise we get spurious dark stripes on flat\n // surfaces whose vertex normals carry numerical noise from\n // CSG output (the visible scar-line symptom would just\n // resurface here even after the lit-normal fix). With the\n // face normal, coplanar adjacent triangles agree exactly \u2192\n // zero normal gradient \u2192 no false edge; only the genuine\n // creases between perpendicular faces produce a real\n // gradient and get the intended outline.\n let depthGradient = length(vec2<f32>(\n dpdx(input.viewPos.z),\n dpdy(input.viewPos.z)\n ));\n let normalGradient = length(vec2<f32>(\n length(dpdx(N)),\n length(dpdy(N))\n ));\n\n if (uniforms.flags.z == 1u) {\n // Threshold filters subtle normal discontinuities at internal\n // triangle edges between coplanar entities in the same batch.\n let edgeFactor = smoothstep(0.02, 0.12, depthGradient * 10.0 + normalGradient * 5.0);\n let edgeIntensity = f32(uniforms.flags.w) / 1000.0;\n let edgeDarkenStrength = clamp(0.25 * edgeIntensity, 0.0, 0.85);\n let edgeDarken = mix(1.0, 1.0 - edgeDarkenStrength, edgeFactor);\n color *= edgeDarken;\n }\n\n color = linearToSrgb(clamp(color, vec3<f32>(0.0), vec3<f32>(1.0)));\n\n var out: FragmentOutput;\n out.color = vec4<f32>(color, finalAlpha);\n out.objectIdEncoded = encodeId24(input.entityId);\n return out;\n }\n ";
|
|
7
2
|
//# sourceMappingURL=main.wgsl.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.wgsl.d.ts","sourceRoot":"","sources":["../../src/shaders/main.wgsl.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.wgsl.d.ts","sourceRoot":"","sources":["../../src/shaders/main.wgsl.ts"],"names":[],"mappings":"AAiCA,eAAO,MAAM,gBAAgB,y5kCA8iBtB,CAAC"}
|
|
@@ -2,10 +2,32 @@
|
|
|
2
2
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
3
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
4
4
|
/**
|
|
5
|
-
* Main
|
|
6
|
-
* Features:
|
|
7
|
-
*
|
|
5
|
+
* Main rendering shader for IFC geometry.
|
|
6
|
+
* Features: linear-space diffuse lighting of sRGB-authored colours, section
|
|
7
|
+
* plane clipping, selection highlight, glass fresnel, hue-preserving highlight
|
|
8
|
+
* roll-off, screen-space edge enhancement.
|
|
8
9
|
*/
|
|
10
|
+
import { MESH_FLAG_RTE_DRAWABLE } from '../mesh-rte-uniforms.js';
|
|
11
|
+
import { colorTransferWgsl } from './color-transfer.wgsl.js';
|
|
12
|
+
import { mainRteWgsl, mainShadowWgsl } from './main-rte-shadow.wgsl.js';
|
|
13
|
+
import { relativeToEyeWgsl } from './relative-to-eye.wgsl.js';
|
|
14
|
+
/**
|
|
15
|
+
* Converts the environment's light intensities to linear irradiance.
|
|
16
|
+
*
|
|
17
|
+
* The intensity scale predates the linear pipeline (#5381), which lights in
|
|
18
|
+
* linear rather than lighting sRGB values and brightening them with a 2.2
|
|
19
|
+
* gamma; one factor maps it instead of restating every preset. The default
|
|
20
|
+
* rig delivers 0.6464 (luma) to a sun-facing horizontal surface and the
|
|
21
|
+
* default exposure is 0.85, so this factor puts that surface at unit
|
|
22
|
+
* irradiance by luma. The default sky tint leaves the channels within about
|
|
23
|
+
* 2% of that, and colours brighter than the highlight roll-off's 0.76
|
|
24
|
+
* threshold compress (see color-transfer.wgsl.ts), so mid-tones render
|
|
25
|
+
* within about 2% of authored and pure white lands near 241/255. Every
|
|
26
|
+
* preset and user exposure is scaled by the same factor, so their relative
|
|
27
|
+
* brightness holds.
|
|
28
|
+
* `light-rig.test.ts` re-derives it from the default rig.
|
|
29
|
+
*/
|
|
30
|
+
const IRRADIANCE_CALIBRATION = 1.82;
|
|
9
31
|
export const mainShaderSource = `
|
|
10
32
|
struct Uniforms {
|
|
11
33
|
viewProj: mat4x4<f32>,
|
|
@@ -17,16 +39,18 @@ export const mainShaderSource = `
|
|
|
17
39
|
flags: vec4<u32>, // x = isSelected, y = section/clip bits, z = edgeEnabled, w = edgeIntensityMilli
|
|
18
40
|
clipBoxMin: vec4<f32>, // xyz = clip-box min corner (world), w = pad
|
|
19
41
|
clipBoxMax: vec4<f32>, // xyz = clip-box max corner (world), w = pad
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
42
|
+
quantParams: vec4<f32>, // local min xyz, lattice step w
|
|
43
|
+
rteViewProj: mat4x4<f32>, // appended frame; bit 16 selects it
|
|
44
|
+
drawableDeltaHigh: vec4<f32>,
|
|
45
|
+
drawableDeltaLow: vec4<f32>,
|
|
46
|
+
rteCameraHigh: vec4<f32>,
|
|
47
|
+
rteCameraLow: vec4<f32>,
|
|
24
48
|
}
|
|
25
49
|
@binding(0) @group(0) var<uniform> uniforms: Uniforms;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
//
|
|
50
|
+
const RTE_DRAWABLE_FLAG: u32 = ${MESH_FLAG_RTE_DRAWABLE}u;
|
|
51
|
+
${relativeToEyeWgsl}
|
|
52
|
+
${mainRteWgsl}
|
|
53
|
+
// Shared group(1) lighting; packing matches packEnvironmentUniforms().
|
|
30
54
|
struct Environment {
|
|
31
55
|
sunDirection: vec3<f32>, // unit vector TOWARD the sun
|
|
32
56
|
sunIntensity: f32,
|
|
@@ -42,89 +66,10 @@ export const mainShaderSource = `
|
|
|
42
66
|
_pad2: f32,
|
|
43
67
|
}
|
|
44
68
|
@binding(0) @group(1) var<uniform> env: Environment;
|
|
69
|
+
const IRRADIANCE_CALIBRATION: f32 = ${IRRADIANCE_CALIBRATION};
|
|
70
|
+
${colorTransferWgsl}
|
|
45
71
|
|
|
46
|
-
|
|
47
|
-
// comparison sampler, and the light matrix + params. Bound on every
|
|
48
|
-
// main-family pipeline; sampling is gated by shadowU.params.y (enabled),
|
|
49
|
-
// so when shadows are off this reads the 1×1 dummy and returns 1.0.
|
|
50
|
-
@binding(1) @group(1) var shadowMap: texture_depth_2d;
|
|
51
|
-
@binding(2) @group(1) var shadowCmp: sampler_comparison;
|
|
52
|
-
struct Shadow {
|
|
53
|
-
lightViewProj: mat4x4<f32>,
|
|
54
|
-
// x = texelSize (1/resolution), y = enabled (0/1),
|
|
55
|
-
// z = normalBias (world units), w = pcfRadius (texels).
|
|
56
|
-
params: vec4<f32>,
|
|
57
|
-
// x = depthBias (reverse-Z clip units, nudges toward lit).
|
|
58
|
-
params2: vec4<f32>,
|
|
59
|
-
}
|
|
60
|
-
@binding(3) @group(1) var<uniform> shadowU: Shadow;
|
|
61
|
-
|
|
62
|
-
// Fraction of the sun reaching this surface point (1 = lit, 0 = fully
|
|
63
|
-
// shadowed). Normal-offset + slope-scaled bias defeats acne without
|
|
64
|
-
// peter-panning; the penumbra is sampled with a 12-tap Poisson disk
|
|
65
|
-
// ROTATED per pixel (interleaved gradient noise). A fixed grid kernel
|
|
66
|
-
// undersamples a wide penumbra and breaks into discrete bands (the
|
|
67
|
-
// "tripled shadow" at high softness); a rotated disk turns that banding
|
|
68
|
-
// into fine dither that reads as smooth at any softness.
|
|
69
|
-
// textureSampleCompareLevel is used (not ...Compare) so it is legal in
|
|
70
|
-
// this non-uniform control flow.
|
|
71
|
-
const SHADOW_POISSON = array<vec2<f32>, 12>(
|
|
72
|
-
vec2<f32>(-0.326, -0.406), vec2<f32>(-0.840, -0.074), vec2<f32>(-0.696, 0.457),
|
|
73
|
-
vec2<f32>(-0.203, 0.621), vec2<f32>( 0.962, -0.195), vec2<f32>( 0.473, -0.480),
|
|
74
|
-
vec2<f32>( 0.519, 0.767), vec2<f32>( 0.185, -0.893), vec2<f32>( 0.507, 0.064),
|
|
75
|
-
vec2<f32>( 0.896, 0.412), vec2<f32>(-0.322, -0.933), vec2<f32>(-0.792, -0.598),
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
fn sunShadowFactor(worldPos: vec3<f32>, N: vec3<f32>, fragCoord: vec2<f32>) -> f32 {
|
|
79
|
-
if (shadowU.params.y < 0.5) { return 1.0; }
|
|
80
|
-
// The diffuse sun term is TWO-SIDED (abs(dot(N, sun)) in the shading
|
|
81
|
-
// below), so a face whose stabilized normal points away from the sun is
|
|
82
|
-
// still lit. Orient the normal toward the sun before biasing: otherwise
|
|
83
|
-
// the normal-offset push (params.z) moves the sample AWAY from the light
|
|
84
|
-
// (deeper behind the surface), and the slope term below collapses to its
|
|
85
|
-
// max (NdotL→0), together biasing the compare toward "lit" — which leaks
|
|
86
|
-
// direct sun onto interior faces the roof occludes (#2670 review).
|
|
87
|
-
let L = normalize(env.sunDirection);
|
|
88
|
-
let Ns = N * select(-1.0, 1.0, dot(N, L) >= 0.0);
|
|
89
|
-
let biased = worldPos + Ns * shadowU.params.z;
|
|
90
|
-
let clip = shadowU.lightViewProj * vec4<f32>(biased, 1.0);
|
|
91
|
-
let ndc = clip.xyz / clip.w;
|
|
92
|
-
let uv = vec2<f32>(ndc.x * 0.5 + 0.5, ndc.y * -0.5 + 0.5);
|
|
93
|
-
let inBounds = uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0 && ndc.z > 0.0;
|
|
94
|
-
if (!inBounds) { return 1.0; }
|
|
95
|
-
// Slope-scaled depth bias: at a grazing sun the receiver's light-space
|
|
96
|
-
// depth changes fast across the kernel, so a constant bias can't clear
|
|
97
|
-
// the whole footprint and the surface rings with moiré. Grow the bias
|
|
98
|
-
// as the surface tilts away from the sun (NdotL → 0) and with the
|
|
99
|
-
// kernel width. The hardware slope bias in the depth pass covers the
|
|
100
|
-
// occluder side; this covers the receiver side.
|
|
101
|
-
let NdotL = max(dot(Ns, L), 0.0);
|
|
102
|
-
let slope = clamp(sqrt(max(1.0 - NdotL * NdotL, 0.0)) / max(NdotL, 0.1), 1.0, 12.0);
|
|
103
|
-
let refDepth = ndc.z + shadowU.params2.x * slope * (1.0 + shadowU.params.w);
|
|
104
|
-
let radius = shadowU.params.x * shadowU.params.w; // penumbra, uv units
|
|
105
|
-
// Per-pixel rotation (interleaved gradient noise) dithers the disk so
|
|
106
|
-
// the discrete taps never line up into bands.
|
|
107
|
-
let ign = fract(52.9829189 * fract(dot(fragCoord, vec2<f32>(0.06711056, 0.00583715))));
|
|
108
|
-
let ang = ign * 6.2831853;
|
|
109
|
-
let cr = cos(ang);
|
|
110
|
-
let sr = sin(ang);
|
|
111
|
-
var sum = 0.0;
|
|
112
|
-
for (var i = 0; i < 12; i = i + 1) {
|
|
113
|
-
let p = SHADOW_POISSON[i];
|
|
114
|
-
let off = vec2<f32>(p.x * cr - p.y * sr, p.x * sr + p.y * cr) * radius;
|
|
115
|
-
sum = sum + textureSampleCompareLevel(shadowMap, shadowCmp, uv + off, refDepth);
|
|
116
|
-
}
|
|
117
|
-
let pcf = sum / 12.0;
|
|
118
|
-
// Terminator fade. On a surface nearly PARALLEL to the sun rays
|
|
119
|
-
// (NdotL → 0, e.g. a vertical wall under a midday sun) the receiver
|
|
120
|
-
// straddles the shadow threshold, so the rotated-disk taps randomly
|
|
121
|
-
// pass/fail and the wall breaks into salt-and-pepper speckle. The
|
|
122
|
-
// direct sun term is near zero there anyway, so fade the cast shadow
|
|
123
|
-
// smoothly toward lit as the surface goes grazing: a clean gradient
|
|
124
|
-
// replaces the ripple (#2670).
|
|
125
|
-
let graze = smoothstep(0.0, 0.3, NdotL);
|
|
126
|
-
return mix(1.0, pcf, graze);
|
|
127
|
-
}
|
|
72
|
+
${mainShadowWgsl}
|
|
128
73
|
|
|
129
74
|
struct VertexInput {
|
|
130
75
|
@location(0) position: vec3<f32>,
|
|
@@ -150,6 +95,12 @@ export const mainShaderSource = `
|
|
|
150
95
|
// vs_instanced writes the per-instance flag from the instance buffer, so a
|
|
151
96
|
// single selected occurrence highlights without re-drawing.
|
|
152
97
|
@location(5) @interpolate(flat) instSelected: u32,
|
|
98
|
+
// Camera-relative position for fragment operations that compare
|
|
99
|
+
// geometry to section/crop boundaries or take derivatives. This is
|
|
100
|
+
// intentionally separate from worldPos: shadow maps still consume
|
|
101
|
+
// their established world-space light matrix until their depth pass
|
|
102
|
+
// is migrated as one transaction.
|
|
103
|
+
@location(6) eyePos: vec3<f32>,
|
|
153
104
|
}
|
|
154
105
|
|
|
155
106
|
// Per-instance vertex-buffer inputs (slot 1, stepMode 'instance') used by
|
|
@@ -171,6 +122,8 @@ export const mainShaderSource = `
|
|
|
171
122
|
@location(7) instEntityId: u32,
|
|
172
123
|
@location(8) instColor: vec4<f32>,
|
|
173
124
|
@location(9) instSelected: u32,
|
|
125
|
+
@location(10) anchorHigh: vec4<f32>,
|
|
126
|
+
@location(11) anchorLow: vec4<f32>,
|
|
174
127
|
}
|
|
175
128
|
|
|
176
129
|
// 12-byte quantized vertex (issue #1682 phase 6): uint16x4 (lattice
|
|
@@ -201,7 +154,9 @@ export const mainShaderSource = `
|
|
|
201
154
|
fn shadeFlatVertex(localPos: vec3<f32>, localNormal: vec3<f32>, entityId: u32) -> VertexOutput {
|
|
202
155
|
var output: VertexOutput;
|
|
203
156
|
let worldPos = uniforms.model * vec4<f32>(localPos, 1.0);
|
|
204
|
-
|
|
157
|
+
let eyePos = rtePosition(localPos).xyz;
|
|
158
|
+
let rte = (uniforms.flags.x & RTE_DRAWABLE_FLAG) != 0u;
|
|
159
|
+
output.position = select(uniforms.viewProj * worldPos, uniforms.rteViewProj * vec4<f32>(eyePos, 1.0), rte);
|
|
205
160
|
// Anti z-fighting depth nudge — see vs_main's comment.
|
|
206
161
|
let colorSalt = (entityId >> 24u) * 2654435761u;
|
|
207
162
|
let zHash = (((entityId & 0x00FFFFFFu) ^ colorSalt) * 2654435761u) & 255u;
|
|
@@ -211,7 +166,8 @@ export const mainShaderSource = `
|
|
|
211
166
|
output.entityId = entityId;
|
|
212
167
|
output.color = uniforms.baseColor;
|
|
213
168
|
output.instSelected = 0u;
|
|
214
|
-
output.
|
|
169
|
+
output.eyePos = eyePos;
|
|
170
|
+
output.viewPos = select((uniforms.viewProj * worldPos).xyz, (uniforms.rteViewProj * vec4<f32>(eyePos, 1.0)).xyz, rte);
|
|
215
171
|
return output;
|
|
216
172
|
}
|
|
217
173
|
|
|
@@ -226,7 +182,9 @@ export const mainShaderSource = `
|
|
|
226
182
|
fn vs_main(input: VertexInput, @builtin(instance_index) instanceIndex: u32) -> VertexOutput {
|
|
227
183
|
var output: VertexOutput;
|
|
228
184
|
let worldPos = uniforms.model * vec4<f32>(input.position, 1.0);
|
|
229
|
-
|
|
185
|
+
let eyePos = rtePosition(input.position).xyz;
|
|
186
|
+
let rte = (uniforms.flags.x & RTE_DRAWABLE_FLAG) != 0u;
|
|
187
|
+
output.position = select(uniforms.viewProj * worldPos, uniforms.rteViewProj * vec4<f32>(eyePos, 1.0), rte);
|
|
230
188
|
// Anti z-fighting: deterministic depth nudge.
|
|
231
189
|
// Knuth multiplicative hash spreads sequential IDs across 0-255 so
|
|
232
190
|
// coplanar faces from different entities always get distinct depths.
|
|
@@ -251,8 +209,9 @@ export const mainShaderSource = `
|
|
|
251
209
|
output.entityId = input.entityId;
|
|
252
210
|
output.color = uniforms.baseColor;
|
|
253
211
|
output.instSelected = 0u; // flat path selects via uniforms.flags.x
|
|
212
|
+
output.eyePos = eyePos;
|
|
254
213
|
// Store view-space position for edge detection
|
|
255
|
-
output.viewPos = (uniforms.viewProj * worldPos).xyz;
|
|
214
|
+
output.viewPos = select((uniforms.viewProj * worldPos).xyz, (uniforms.rteViewProj * vec4<f32>(eyePos, 1.0)).xyz, rte);
|
|
256
215
|
return output;
|
|
257
216
|
}
|
|
258
217
|
|
|
@@ -267,7 +226,9 @@ export const mainShaderSource = `
|
|
|
267
226
|
var output: VertexOutput;
|
|
268
227
|
let instMat = mat4x4<f32>(inst.m0, inst.m1, inst.m2, inst.m3);
|
|
269
228
|
let worldPos = instMat * vec4<f32>(input.position, 1.0);
|
|
270
|
-
|
|
229
|
+
let linearLocal = (instMat * vec4<f32>(input.position, 0.0)).xyz;
|
|
230
|
+
let eyePos = rteInstancePosition(linearLocal, inst.anchorHigh.xyz, inst.anchorLow.xyz).xyz;
|
|
231
|
+
output.position = uniforms.rteViewProj * vec4<f32>(eyePos, 1.0);
|
|
271
232
|
// Same per-entity depth nudge as vs_main. No colour salt here: the
|
|
272
233
|
// instanced path has no base-vs-overlay coincident redraw (yet), so the
|
|
273
234
|
// raw picking id is enough to separate coplanar entities.
|
|
@@ -278,7 +239,8 @@ export const mainShaderSource = `
|
|
|
278
239
|
output.entityId = inst.instEntityId;
|
|
279
240
|
output.color = inst.instColor;
|
|
280
241
|
output.instSelected = inst.instSelected;
|
|
281
|
-
output.
|
|
242
|
+
output.eyePos = eyePos;
|
|
243
|
+
output.viewPos = (uniforms.rteViewProj * vec4<f32>(eyePos, 1.0)).xyz;
|
|
282
244
|
return output;
|
|
283
245
|
}
|
|
284
246
|
|
|
@@ -325,6 +287,12 @@ export const mainShaderSource = `
|
|
|
325
287
|
|
|
326
288
|
@fragment
|
|
327
289
|
fn fs_main(input: VertexOutput) -> FragmentOutput {
|
|
290
|
+
// The flat/quantized/textured mesh paths submit this in one
|
|
291
|
+
// camera-relative frame. Do all camera-local fragment arithmetic in
|
|
292
|
+
// that frame; otherwise the RTE vertex precision is thrown away at
|
|
293
|
+
// section/crop/derivative ingress. Instanced geometry is not yet
|
|
294
|
+
// anchored, so its worldPos remains the authoritative input.
|
|
295
|
+
let fragmentPos = select(input.worldPos, input.eyePos, (uniforms.flags.x & RTE_DRAWABLE_FLAG) != 0u);
|
|
328
296
|
// Per-instance hide/isolate: bit 1 of the instance flags lane marks a hidden
|
|
329
297
|
// occurrence. Discard it so it neither draws nor writes depth (and the pick
|
|
330
298
|
// pass applies the same discard, so it isn't pickable). vs_main writes
|
|
@@ -354,7 +322,7 @@ export const mainShaderSource = `
|
|
|
354
322
|
let planeDistance = uniforms.sectionPlane.w;
|
|
355
323
|
let flipped = (uniforms.flags.y & 2u) == 2u;
|
|
356
324
|
let side = select(1.0, -1.0, flipped);
|
|
357
|
-
let distToPlane = (dot(
|
|
325
|
+
let distToPlane = (dot(fragmentPos, planeNormal) - planeDistance) * side;
|
|
358
326
|
if (distToPlane > 0.0) {
|
|
359
327
|
discard;
|
|
360
328
|
}
|
|
@@ -362,7 +330,7 @@ export const mainShaderSource = `
|
|
|
362
330
|
// Clip box (section / crop box): discard fragments OUTSIDE the AABB.
|
|
363
331
|
// flags.y bit 2 = clip-box enabled.
|
|
364
332
|
if ((uniforms.flags.y & 4u) != 0u) {
|
|
365
|
-
let p =
|
|
333
|
+
let p = fragmentPos;
|
|
366
334
|
if (any(p < uniforms.clipBoxMin.xyz) || any(p > uniforms.clipBoxMax.xyz)) {
|
|
367
335
|
discard;
|
|
368
336
|
}
|
|
@@ -402,7 +370,7 @@ export const mainShaderSource = `
|
|
|
402
370
|
// We still fall back to the vertex normal when derivatives
|
|
403
371
|
// are unavailable (extreme polygon degeneracy where dpdx /
|
|
404
372
|
// dpdy collapse to zero — practically never on real geometry).
|
|
405
|
-
let faceN = cross(dpdx(
|
|
373
|
+
let faceN = cross(dpdx(fragmentPos), dpdy(fragmentPos));
|
|
406
374
|
let fLen2 = dot(faceN, faceN);
|
|
407
375
|
var N: vec3<f32>;
|
|
408
376
|
if (fLen2 > 1e-10) {
|
|
@@ -435,47 +403,48 @@ export const mainShaderSource = `
|
|
|
435
403
|
}
|
|
436
404
|
|
|
437
405
|
// Lighting environment — sun/hemisphere/exposure come from the
|
|
438
|
-
// global env uniform
|
|
439
|
-
//
|
|
440
|
-
//
|
|
406
|
+
// global env uniform. The fill follows the sun (it bounces in from
|
|
407
|
+
// the opposite side); the rim stays fixed in world space as a
|
|
408
|
+
// stylistic shaping light.
|
|
441
409
|
let sunLight = env.sunDirection;
|
|
442
|
-
|
|
410
|
+
// Horizontal mirror of the sun, lifted slightly. An overhead sun
|
|
411
|
+
// leaves (0, 0.25, 0), which normalizes to straight up.
|
|
412
|
+
let fillLight = normalize(vec3<f32>(-sunLight.x, 0.25, -sunLight.z));
|
|
443
413
|
let rimLight = normalize(vec3<f32>(0.0, 0.2, -1.0)); // Rim light for edge definition
|
|
444
414
|
|
|
445
|
-
// Hemisphere ambient
|
|
415
|
+
// Hemisphere ambient. This, not the sun, keeps faces turned away
|
|
416
|
+
// from the sun readable (I-beam webs and flange undersides, #5382).
|
|
446
417
|
let hemisphereFactor = N.y * 0.5 + 0.5;
|
|
447
418
|
let ambient = mix(env.groundColor, env.skyColor, hemisphereFactor) * env.ambientIntensity;
|
|
448
419
|
|
|
449
|
-
//
|
|
420
|
+
// One-sided sun, so a building has a lit side and a shaded side.
|
|
450
421
|
// sunSoftness is the diffuse wrap (env uniform): 0 = crisp
|
|
451
422
|
// terminator (hard shadows), larger = softer wrap-around (overcast).
|
|
452
|
-
let NdotL =
|
|
423
|
+
let NdotL = dot(N, sunLight);
|
|
453
424
|
let wrap = env.sunSoftness;
|
|
454
425
|
let diffuseSun = max((NdotL + wrap) / (1.0 + wrap), 0.0) * env.sunIntensity;
|
|
455
426
|
|
|
456
|
-
// Fill light -
|
|
457
|
-
let NdotFill =
|
|
427
|
+
// Fill light, one-sided like the sun.
|
|
428
|
+
let NdotFill = max(dot(N, fillLight), 0.0);
|
|
458
429
|
let diffuseFill = NdotFill * env.fillIntensity;
|
|
459
430
|
|
|
460
431
|
// Rim light for edge definition
|
|
461
432
|
let NdotRim = max(dot(N, rimLight), 0.0);
|
|
462
433
|
let rim = pow(NdotRim, 4.0) * env.rimIntensity;
|
|
463
434
|
|
|
435
|
+
// The authored colour is display-referred sRGB; light it in linear.
|
|
436
|
+
// (textured.wgsl.ts anchors on the first line to multiply in the texel.)
|
|
464
437
|
var baseColor = input.color.rgb;
|
|
465
|
-
|
|
466
|
-
// Detect if the color is close to white/gray (low saturation)
|
|
467
|
-
let baseGray = dot(baseColor, vec3<f32>(0.299, 0.587, 0.114));
|
|
468
|
-
let baseSaturation = length(baseColor - vec3<f32>(baseGray)) / max(baseGray, 0.001);
|
|
469
|
-
let isWhiteish = 1.0 - smoothstep(0.0, 0.3, baseSaturation);
|
|
470
|
-
|
|
471
|
-
// Darken whites/grays more to reduce washed-out appearance
|
|
472
|
-
baseColor = mix(baseColor, baseColor * 0.7, isWhiteish * 0.4);
|
|
438
|
+
baseColor = srgbToLinear(baseColor);
|
|
473
439
|
|
|
474
440
|
// Combine all lighting. Only the DIRECT sun term is occluded by cast
|
|
475
441
|
// shadows (#2670); ambient/fill/rim are indirect and stay unshadowed.
|
|
476
|
-
|
|
442
|
+
// Exposure scales the light, so the selection shade below and every
|
|
443
|
+
// later stage see one exposed irradiance.
|
|
444
|
+
let sunShadow = sunShadowFactor(input.eyePos, N, input.position.xy);
|
|
477
445
|
let lightTerm = ambient + env.sunColor * (diffuseSun * sunShadow) + vec3<f32>(diffuseFill + rim);
|
|
478
|
-
|
|
446
|
+
let irradiance = lightTerm * (env.exposure * IRRADIANCE_CALIBRATION);
|
|
447
|
+
var color = baseColor * irradiance;
|
|
479
448
|
|
|
480
449
|
// flags.x is a bitfield:
|
|
481
450
|
// bit 0 (value 1) = isSelected → selection-highlight + force opaque
|
|
@@ -510,15 +479,16 @@ export const mainShaderSource = `
|
|
|
510
479
|
// selection. Re-lighting keeps that per-face brightness step, so
|
|
511
480
|
// creases read on the highlight exactly as they do unselected.
|
|
512
481
|
//
|
|
513
|
-
// The luminance of
|
|
514
|
-
// (which preserves the per-face brightness RATIOS, so creases
|
|
515
|
-
// as strongly as on the unselected surface)
|
|
516
|
-
// face
|
|
517
|
-
// shadowed faces only
|
|
482
|
+
// The luminance of the exposed irradiance is used as a multiplicative
|
|
483
|
+
// gain (which preserves the per-face brightness RATIOS, so creases
|
|
484
|
+
// read as strongly as on the unselected surface). The calibration
|
|
485
|
+
// puts a sunlit face at 1.0, i.e. full selection-blue, and the
|
|
486
|
+
// floor/ceiling clamp keeps shadowed faces only dimmed and bright
|
|
487
|
+
// scenes from washing out.
|
|
518
488
|
if (isSelected) {
|
|
519
|
-
let shadeLum = dot(
|
|
520
|
-
let shade = clamp(shadeLum
|
|
521
|
-
color = vec3<f32>(0.3, 0.6, 1.0) * shade;
|
|
489
|
+
let shadeLum = dot(irradiance, vec3<f32>(0.299, 0.587, 0.114));
|
|
490
|
+
let shade = clamp(shadeLum, 0.45, 1.2);
|
|
491
|
+
color = srgbToLinear(vec3<f32>(0.3, 0.6, 1.0)) * shade;
|
|
522
492
|
}
|
|
523
493
|
|
|
524
494
|
// flags.x bit 5 (value 32) = EMPHASIZE overlay: render the colour
|
|
@@ -542,7 +512,7 @@ export const mainShaderSource = `
|
|
|
542
512
|
var finalAlpha = select(input.color.a, 1.0, isSelected || emphasizedOverlay);
|
|
543
513
|
if (finalAlpha < 0.99 && !isSelected && !isOverlay) {
|
|
544
514
|
// Calculate view direction for fresnel
|
|
545
|
-
let V = normalize(-
|
|
515
|
+
let V = normalize(-fragmentPos);
|
|
546
516
|
let NdotV = max(dot(N, V), 0.0);
|
|
547
517
|
|
|
548
518
|
// Enhanced fresnel effect - stronger at edges (grazing angles)
|
|
@@ -557,8 +527,10 @@ export const mainShaderSource = `
|
|
|
557
527
|
// Mix in reflection tint at edges
|
|
558
528
|
color = mix(color, color * reflectionTint, reflectionStrength);
|
|
559
529
|
|
|
560
|
-
// Add realistic glass shine - brighter at edges where light reflects
|
|
561
|
-
|
|
530
|
+
// Add realistic glass shine - brighter at edges where light reflects.
|
|
531
|
+
// Linear amount: 0.06 lifts a mid-tone about as much as the 0.12
|
|
532
|
+
// the pre-linear pipeline added in display space.
|
|
533
|
+
let glassShine = fresnel * 0.06;
|
|
562
534
|
color += glassShine;
|
|
563
535
|
|
|
564
536
|
// Slight desaturation at edges (glass reflects environment, not just color)
|
|
@@ -570,31 +542,11 @@ export const mainShaderSource = `
|
|
|
570
542
|
finalAlpha = finalAlpha * 0.7;
|
|
571
543
|
}
|
|
572
544
|
|
|
573
|
-
//
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
//
|
|
577
|
-
color = (color
|
|
578
|
-
color = max(color, vec3<f32>(0.0));
|
|
579
|
-
|
|
580
|
-
// Saturation boost - stronger for colored surfaces, less for whites
|
|
581
|
-
let gray = dot(color, vec3<f32>(0.299, 0.587, 0.114));
|
|
582
|
-
// More saturation for colored surfaces. isWhiteish is derived from
|
|
583
|
-
// the base material colour, so for a SELECTED object it would leak a
|
|
584
|
-
// material dependence into the highlight (breaking the no-bleed-
|
|
585
|
-
// through contract). The selection blue is a fully-saturated colour,
|
|
586
|
-
// so force the colored-surface boost (1.4) when selected — keeping
|
|
587
|
-
// the highlight identical regardless of the underlying material.
|
|
588
|
-
let satBoost = select(mix(1.4, 1.1, isWhiteish), 1.4, isSelected);
|
|
589
|
-
color = mix(vec3<f32>(gray), color, satBoost);
|
|
590
|
-
|
|
591
|
-
// ACES filmic tone mapping
|
|
592
|
-
let a = 2.51;
|
|
593
|
-
let b = 0.03;
|
|
594
|
-
let c = 2.43;
|
|
595
|
-
let d = 0.59;
|
|
596
|
-
let e = 0.14;
|
|
597
|
-
color = clamp((color * (a * color + b)) / (color * (c * color + d) + e), vec3<f32>(0.0), vec3<f32>(1.0));
|
|
545
|
+
// Hue-preserving highlight roll-off (color-transfer.wgsl.ts). No
|
|
546
|
+
// contrast curve and no saturation boost: an authored colour lit at
|
|
547
|
+
// unit irradiance leaves here unchanged unless it is brighter than
|
|
548
|
+
// the roll-off threshold.
|
|
549
|
+
color = neutralCompress(color);
|
|
598
550
|
|
|
599
551
|
// Subtle edge enhancement using screen-space derivatives.
|
|
600
552
|
//
|
|
@@ -627,8 +579,7 @@ export const mainShaderSource = `
|
|
|
627
579
|
color *= edgeDarken;
|
|
628
580
|
}
|
|
629
581
|
|
|
630
|
-
|
|
631
|
-
color = pow(color, vec3<f32>(1.0 / 2.2));
|
|
582
|
+
color = linearToSrgb(clamp(color, vec3<f32>(0.0), vec3<f32>(1.0)));
|
|
632
583
|
|
|
633
584
|
var out: FragmentOutput;
|
|
634
585
|
out.color = vec4<f32>(color, finalAlpha);
|