@playcanvas/supersplat-viewer 1.14.2 → 1.15.1
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/index.js +3 -3
- package/package.json +6 -6
- package/public/index.css +12 -0
- package/public/index.html +22 -0
- package/public/index.js +1658 -61
- package/public/index.js.map +1 -1
package/public/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const TRACEID_GPU_TIMINGS = 'GpuTimings';
|
|
2
2
|
|
|
3
|
-
const version$1 = '2.16.
|
|
4
|
-
const revision = '
|
|
3
|
+
const version$1 = '2.16.2';
|
|
4
|
+
const revision = '5c8389e';
|
|
5
5
|
function extend(target, ex) {
|
|
6
6
|
for(const prop in ex){
|
|
7
7
|
const copy = ex[prop];
|
|
@@ -8725,8 +8725,12 @@ class WebgpuShaderProcessorWGSL {
|
|
|
8725
8725
|
}
|
|
8726
8726
|
static generateFragmentOutputStruct(src, numRenderTargets) {
|
|
8727
8727
|
let structCode = 'struct FragmentOutput {\n';
|
|
8728
|
+
const colorName = (i)=>`color${i > 0 ? i : ''}`;
|
|
8728
8729
|
for(let i = 0; i < numRenderTargets; i++){
|
|
8729
|
-
|
|
8730
|
+
const name = colorName(i);
|
|
8731
|
+
if (src.search(new RegExp(`\\.${name}\\s*=`)) !== -1) {
|
|
8732
|
+
structCode += ` @location(${i}) ${name} : pcOutType${i},\n`;
|
|
8733
|
+
}
|
|
8730
8734
|
}
|
|
8731
8735
|
const needsFragDepth = src.search(/\.fragDepth\s*=/) !== -1;
|
|
8732
8736
|
if (needsFragDepth) {
|
|
@@ -14264,7 +14268,8 @@ class WebglGraphicsDevice extends GraphicsDevice {
|
|
|
14264
14268
|
});
|
|
14265
14269
|
const rgbaChannels = getPixelFormatChannelsForRgbaReadback(texture._format);
|
|
14266
14270
|
const needsRgbaReadback = rgbaChannels > 0;
|
|
14267
|
-
const
|
|
14271
|
+
const ArrayType = getPixelFormatArrayType(texture._format);
|
|
14272
|
+
const outputData = options.data ?? new ArrayType(TextureUtils.calcLevelGpuSize(width, height, 1, texture._format) / ArrayType.BYTES_PER_ELEMENT);
|
|
14268
14273
|
const readBuffer = needsRgbaReadback ? new Uint8Array(width * height * 4) : outputData;
|
|
14269
14274
|
this.setRenderTarget(renderTarget);
|
|
14270
14275
|
this.initRenderTarget(renderTarget);
|
|
@@ -31623,7 +31628,7 @@ class GSplatResourceBase {
|
|
|
31623
31628
|
return this._format;
|
|
31624
31629
|
}
|
|
31625
31630
|
getTexture(name) {
|
|
31626
|
-
return this.streams.getTexture(name);
|
|
31631
|
+
return this.streams.getTexture(name) ?? null;
|
|
31627
31632
|
}
|
|
31628
31633
|
get textureDimensions() {
|
|
31629
31634
|
return this.streams.textureDimensions;
|
|
@@ -32650,8 +32655,20 @@ fn fragmentMain(input: FragmentInput) -> FragmentOutput {
|
|
|
32650
32655
|
let sh0 = pack111110(resolve(uniform.sh0Mins.xyz, uniform.sh0Maxs.xyz, sh0Sample.xyz));
|
|
32651
32656
|
let alpha = sigmoid(mix(uniform.sh0Mins.w, uniform.sh0Maxs.w, sh0Sample.w));
|
|
32652
32657
|
#else
|
|
32653
|
-
let
|
|
32654
|
-
let
|
|
32658
|
+
let scalesIdx = vec3u(scalesSample * 255.0);
|
|
32659
|
+
let scalesV = vec3f(
|
|
32660
|
+
uniform.scales_codebook[scalesIdx.x >> 2u][scalesIdx.x & 3u],
|
|
32661
|
+
uniform.scales_codebook[scalesIdx.y >> 2u][scalesIdx.y & 3u],
|
|
32662
|
+
uniform.scales_codebook[scalesIdx.z >> 2u][scalesIdx.z & 3u]
|
|
32663
|
+
);
|
|
32664
|
+
let scale = pack101010((scalesV - uniform.scales_codebook[0].x) / (uniform.scales_codebook[63].w - uniform.scales_codebook[0].x));
|
|
32665
|
+
let sh0Idx = vec3u(sh0Sample.xyz * 255.0);
|
|
32666
|
+
let sh0V = vec3f(
|
|
32667
|
+
uniform.sh0_codebook[sh0Idx.x >> 2u][sh0Idx.x & 3u],
|
|
32668
|
+
uniform.sh0_codebook[sh0Idx.y >> 2u][sh0Idx.y & 3u],
|
|
32669
|
+
uniform.sh0_codebook[sh0Idx.z >> 2u][sh0Idx.z & 3u]
|
|
32670
|
+
);
|
|
32671
|
+
let sh0 = pack111110((sh0V - uniform.sh0_codebook[0].x) / (uniform.sh0_codebook[63].w - uniform.sh0_codebook[0].x));
|
|
32655
32672
|
let alpha = sh0Sample.w;
|
|
32656
32673
|
#endif
|
|
32657
32674
|
let qmode = u32(quatsSample.w * 255.0) - 252u;
|
|
@@ -32726,7 +32743,13 @@ fn fragmentMain(input: FragmentInput) -> FragmentOutput {
|
|
|
32726
32743
|
#ifdef REORDER_V1
|
|
32727
32744
|
output.color = unpack8888(pack111110(shNSample));
|
|
32728
32745
|
#else
|
|
32729
|
-
|
|
32746
|
+
let shNIdx = vec3u(shNSample * 255.0);
|
|
32747
|
+
let shNV = vec3f(
|
|
32748
|
+
uniform.shN_codebook[shNIdx.x >> 2u][shNIdx.x & 3u],
|
|
32749
|
+
uniform.shN_codebook[shNIdx.y >> 2u][shNIdx.y & 3u],
|
|
32750
|
+
uniform.shN_codebook[shNIdx.z >> 2u][shNIdx.z & 3u]
|
|
32751
|
+
);
|
|
32752
|
+
output.color = unpack8888(pack111110((shNV - uniform.shN_codebook[0].x) / (uniform.shN_codebook[63].w - uniform.shN_codebook[0].x)));
|
|
32730
32753
|
#endif
|
|
32731
32754
|
return output;
|
|
32732
32755
|
}
|
|
@@ -32753,17 +32776,7 @@ fn unpack101010(v: u32) -> vec3f {
|
|
|
32753
32776
|
}
|
|
32754
32777
|
fn unpack111110(v: u32) -> vec3f {
|
|
32755
32778
|
return vec3f((vec3u(v) >> vec3u(21u, 10u, 0u)) & vec3u(0x7ffu, 0x7ffu, 0x3ffu)) / vec3f(2047.0, 2047.0, 1023.0);
|
|
32756
|
-
}
|
|
32757
|
-
fn resolveCodebook(s: vec3f, codebook: ptr<uniform, array<vec4f, 64>>) -> vec3f {
|
|
32758
|
-
let idx = vec3u(s * 255.0);
|
|
32759
|
-
let v = vec3f(
|
|
32760
|
-
codebook[idx.x >> 2u][idx.x & 3u],
|
|
32761
|
-
codebook[idx.y >> 2u][idx.y & 3u],
|
|
32762
|
-
codebook[idx.z >> 2u][idx.z & 3u]
|
|
32763
|
-
);
|
|
32764
|
-
return (v - codebook[0].x) / (codebook[63].w - codebook[0].x);
|
|
32765
|
-
}
|
|
32766
|
-
`;
|
|
32779
|
+
}`;
|
|
32767
32780
|
|
|
32768
32781
|
var glslSogCentersPS = `
|
|
32769
32782
|
#include "gsplatPackingPS"
|
|
@@ -36688,7 +36701,6 @@ uniform ivec3 clusterCellsDot;
|
|
|
36688
36701
|
uniform ivec3 clusterCellsMax;
|
|
36689
36702
|
uniform vec2 shadowAtlasParams;
|
|
36690
36703
|
struct ClusterLightData {
|
|
36691
|
-
uint flags;
|
|
36692
36704
|
vec3 halfWidth;
|
|
36693
36705
|
bool isSpot;
|
|
36694
36706
|
vec3 halfHeight;
|
|
@@ -36703,10 +36715,8 @@ struct ClusterLightData {
|
|
|
36703
36715
|
float range;
|
|
36704
36716
|
vec4 cookieChannelMask;
|
|
36705
36717
|
float biasesData;
|
|
36706
|
-
uint colorBFlagsData;
|
|
36707
36718
|
float shadowBias;
|
|
36708
36719
|
float shadowNormalBias;
|
|
36709
|
-
float anglesData;
|
|
36710
36720
|
float innerConeAngleCos;
|
|
36711
36721
|
float outerConeAngleCos;
|
|
36712
36722
|
float cookieIntensity;
|
|
@@ -36714,35 +36724,38 @@ struct ClusterLightData {
|
|
|
36714
36724
|
bool isLightmapped;
|
|
36715
36725
|
};
|
|
36716
36726
|
mat4 lightProjectionMatrix;
|
|
36727
|
+
uint clusterLightData_flags;
|
|
36728
|
+
float clusterLightData_anglesData;
|
|
36729
|
+
uint clusterLightData_colorBFlagsData;
|
|
36717
36730
|
vec4 sampleLightTextureF(const ClusterLightData clusterLightData, int index) {
|
|
36718
36731
|
return texelFetch(lightsTexture, ivec2(index, clusterLightData.lightIndex), 0);
|
|
36719
36732
|
}
|
|
36720
36733
|
void decodeClusterLightCore(inout ClusterLightData clusterLightData, int lightIndex) {
|
|
36721
36734
|
clusterLightData.lightIndex = lightIndex;
|
|
36722
36735
|
vec4 halfData = sampleLightTextureF(clusterLightData, {CLUSTER_TEXTURE_COLOR_ANGLES_BIAS});
|
|
36723
|
-
|
|
36736
|
+
clusterLightData_anglesData = halfData.z;
|
|
36724
36737
|
clusterLightData.biasesData = halfData.w;
|
|
36725
|
-
|
|
36738
|
+
clusterLightData_colorBFlagsData = floatBitsToUint(halfData.y);
|
|
36726
36739
|
vec2 colorRG = unpackHalf2x16(floatBitsToUint(halfData.x));
|
|
36727
|
-
vec2 colorB_flags = unpackHalf2x16(
|
|
36740
|
+
vec2 colorB_flags = unpackHalf2x16(clusterLightData_colorBFlagsData);
|
|
36728
36741
|
clusterLightData.color = vec3(colorRG, colorB_flags.x) * {LIGHT_COLOR_DIVIDER};
|
|
36729
36742
|
vec4 lightPosRange = sampleLightTextureF(clusterLightData, {CLUSTER_TEXTURE_POSITION_RANGE});
|
|
36730
36743
|
clusterLightData.position = lightPosRange.xyz;
|
|
36731
36744
|
clusterLightData.range = lightPosRange.w;
|
|
36732
36745
|
vec4 lightDir_Flags = sampleLightTextureF(clusterLightData, {CLUSTER_TEXTURE_DIRECTION_FLAGS});
|
|
36733
36746
|
clusterLightData.direction = lightDir_Flags.xyz;
|
|
36734
|
-
|
|
36735
|
-
clusterLightData.isSpot = (
|
|
36736
|
-
clusterLightData.shape = (
|
|
36737
|
-
clusterLightData.falloffModeLinear = (
|
|
36738
|
-
clusterLightData.shadowIntensity = float((
|
|
36739
|
-
clusterLightData.cookieIntensity = float((
|
|
36740
|
-
clusterLightData.isDynamic = (
|
|
36741
|
-
clusterLightData.isLightmapped = (
|
|
36747
|
+
clusterLightData_flags = floatBitsToUint(lightDir_Flags.w);
|
|
36748
|
+
clusterLightData.isSpot = (clusterLightData_flags & (1u << 30u)) != 0u;
|
|
36749
|
+
clusterLightData.shape = (clusterLightData_flags >> 28u) & 0x3u;
|
|
36750
|
+
clusterLightData.falloffModeLinear = (clusterLightData_flags & (1u << 27u)) == 0u;
|
|
36751
|
+
clusterLightData.shadowIntensity = float((clusterLightData_flags >> 0u) & 0xFFu) / 255.0;
|
|
36752
|
+
clusterLightData.cookieIntensity = float((clusterLightData_flags >> 8u) & 0xFFu) / 255.0;
|
|
36753
|
+
clusterLightData.isDynamic = (clusterLightData_flags & (1u << 22u)) != 0u;
|
|
36754
|
+
clusterLightData.isLightmapped = (clusterLightData_flags & (1u << 21u)) != 0u;
|
|
36742
36755
|
}
|
|
36743
36756
|
void decodeClusterLightSpot(inout ClusterLightData clusterLightData) {
|
|
36744
|
-
uint angleFlags = (
|
|
36745
|
-
vec2 angleValues = unpackHalf2x16(floatBitsToUint(
|
|
36757
|
+
uint angleFlags = (clusterLightData_colorBFlagsData >> 16u) & 0xFFFFu;
|
|
36758
|
+
vec2 angleValues = unpackHalf2x16(floatBitsToUint(clusterLightData_anglesData));
|
|
36746
36759
|
float innerVal = angleValues.x;
|
|
36747
36760
|
float outerVal = angleValues.y;
|
|
36748
36761
|
float innerIsVersine = float(angleFlags & 1u);
|
|
@@ -36772,7 +36785,7 @@ void decodeClusterLightShadowData(inout ClusterLightData clusterLightData) {
|
|
|
36772
36785
|
clusterLightData.shadowNormalBias = biases.y;
|
|
36773
36786
|
}
|
|
36774
36787
|
void decodeClusterLightCookieData(inout ClusterLightData clusterLightData) {
|
|
36775
|
-
uint cookieFlags = (
|
|
36788
|
+
uint cookieFlags = (clusterLightData_flags >> 23u) & 0x0Fu;
|
|
36776
36789
|
clusterLightData.cookieChannelMask = vec4(uvec4(cookieFlags) & uvec4(1u, 2u, 4u, 8u));
|
|
36777
36790
|
clusterLightData.cookieChannelMask = step(1.0, clusterLightData.cookieChannelMask);
|
|
36778
36791
|
}
|
|
@@ -54690,9 +54703,9 @@ class GSplatComponent extends Component {
|
|
|
54690
54703
|
}
|
|
54691
54704
|
getInstanceTexture(name) {
|
|
54692
54705
|
if (!this._placement) {
|
|
54693
|
-
return
|
|
54706
|
+
return null;
|
|
54694
54707
|
}
|
|
54695
|
-
return this._placement.getInstanceTexture(name, this.system.app.graphicsDevice);
|
|
54708
|
+
return this._placement.getInstanceTexture(name, this.system.app.graphicsDevice) ?? null;
|
|
54696
54709
|
}
|
|
54697
54710
|
_onGSplatAssetAdded() {
|
|
54698
54711
|
if (!this._assetReference.asset) {
|
|
@@ -69079,7 +69092,7 @@ class GamepadSource extends InputSource {
|
|
|
69079
69092
|
}
|
|
69080
69093
|
GamepadSource.buttonCode = BUTTON_CODES;
|
|
69081
69094
|
|
|
69082
|
-
const damp = (damping, dt)=>1 - Math.pow(damping, dt * 1000);
|
|
69095
|
+
const damp$1 = (damping, dt)=>1 - Math.pow(damping, dt * 1000);
|
|
69083
69096
|
|
|
69084
69097
|
const offset$1 = new Vec3();
|
|
69085
69098
|
const angles$1 = new Vec3();
|
|
@@ -69123,7 +69136,7 @@ let FlyController$1 = class FlyController extends InputController$1 {
|
|
|
69123
69136
|
offset$1.add(right.mulScalar(move[0]));
|
|
69124
69137
|
offset$1.add(up.mulScalar(move[1]));
|
|
69125
69138
|
this._targetPose.move(offset$1);
|
|
69126
|
-
return this._pose.lerp(this._pose, this._targetPose, damp(this.moveDamping, dt), damp(this.rotateDamping, dt));
|
|
69139
|
+
return this._pose.lerp(this._pose, this._targetPose, damp$1(this.moveDamping, dt), damp$1(this.rotateDamping, dt));
|
|
69127
69140
|
}
|
|
69128
69141
|
destroy() {
|
|
69129
69142
|
this.detach();
|
|
@@ -69179,8 +69192,8 @@ let OrbitController$1 = class OrbitController extends InputController$1 {
|
|
|
69179
69192
|
const { z: dist } = this._targetChildPose.position;
|
|
69180
69193
|
this._targetChildPose.move(offset.set(0, 0, dist * (1 + move[2]) - dist));
|
|
69181
69194
|
this._targetRootPose.rotate(angles.set(-rotate[1], -rotate[0], 0));
|
|
69182
|
-
this._rootPose.lerp(this._rootPose, this._targetRootPose, damp(this.moveDamping, dt), damp(this.rotateDamping, dt), 1);
|
|
69183
|
-
this._childPose.lerp(this._childPose, this._targetChildPose, damp(this.zoomDamping, dt), 1, 1);
|
|
69195
|
+
this._rootPose.lerp(this._rootPose, this._targetRootPose, damp$1(this.moveDamping, dt), damp$1(this.rotateDamping, dt), 1);
|
|
69196
|
+
this._childPose.lerp(this._childPose, this._targetChildPose, damp$1(this.zoomDamping, dt), 1, 1);
|
|
69184
69197
|
rotation$1.setFromEulerAngles(this._rootPose.angles).transformVector(this._childPose.position, offset).add(this._rootPose.position);
|
|
69185
69198
|
return this._pose.set(offset, this._rootPose.angles, this._childPose.position.z);
|
|
69186
69199
|
}
|
|
@@ -69465,7 +69478,7 @@ const initJoystick = (dom, events, state) => {
|
|
|
69465
69478
|
let lastTapTime = 0;
|
|
69466
69479
|
// Update joystick visibility based on camera mode and input mode
|
|
69467
69480
|
const updateJoystickVisibility = () => {
|
|
69468
|
-
if (state.cameraMode === 'fly' && state.inputMode === 'touch') {
|
|
69481
|
+
if ((state.cameraMode === 'fly' || state.cameraMode === 'fps') && state.inputMode === 'touch') {
|
|
69469
69482
|
dom.joystickBase.classList.remove('hidden');
|
|
69470
69483
|
dom.joystickBase.classList.toggle('mode-2d', joystickMode === '2d');
|
|
69471
69484
|
dom.joystickBase.style.left = `${joystickFixedX}px`;
|
|
@@ -69633,11 +69646,12 @@ const initUI = (global) => {
|
|
|
69633
69646
|
'buttonContainer',
|
|
69634
69647
|
'play', 'pause',
|
|
69635
69648
|
'settings', 'settingsPanel',
|
|
69636
|
-
'orbitCamera', 'flyCamera',
|
|
69649
|
+
'orbitCamera', 'flyCamera', 'fpsCamera',
|
|
69637
69650
|
'hqCheck', 'hqOption', 'lqCheck', 'lqOption',
|
|
69638
69651
|
'reset', 'frame',
|
|
69639
69652
|
'loadingText', 'loadingBar',
|
|
69640
69653
|
'joystickBase', 'joystick',
|
|
69654
|
+
'showVoxels',
|
|
69641
69655
|
'tooltip',
|
|
69642
69656
|
'annotationNav', 'annotationPrev', 'annotationNext', 'annotationInfo', 'annotationNavTitle',
|
|
69643
69657
|
'supersplatBranding'
|
|
@@ -69645,6 +69659,10 @@ const initUI = (global) => {
|
|
|
69645
69659
|
acc[id] = document.getElementById(id);
|
|
69646
69660
|
return acc;
|
|
69647
69661
|
}, {});
|
|
69662
|
+
// Remove focus from buttons after click so keyboard input isn't captured by the UI
|
|
69663
|
+
dom.ui.addEventListener('click', () => {
|
|
69664
|
+
document.activeElement?.blur();
|
|
69665
|
+
});
|
|
69648
69666
|
// Forward wheel events from UI overlays to the canvas so the camera zooms
|
|
69649
69667
|
// instead of the page scrolling (e.g. annotation nav, tooltips, hotspots)
|
|
69650
69668
|
const canvas = global.app.graphicsDevice.canvas;
|
|
@@ -69778,7 +69796,8 @@ const initUI = (global) => {
|
|
|
69778
69796
|
});
|
|
69779
69797
|
// fade ui controls after 5 seconds of inactivity
|
|
69780
69798
|
events.on('controlsHidden:changed', (value) => {
|
|
69781
|
-
dom.controlsWrap.
|
|
69799
|
+
dom.controlsWrap.classList.toggle('faded-out', value);
|
|
69800
|
+
dom.controlsWrap.classList.toggle('faded-in', !value);
|
|
69782
69801
|
});
|
|
69783
69802
|
// show the ui and start a timer to hide it again
|
|
69784
69803
|
let uiTimeout = null;
|
|
@@ -69880,9 +69899,28 @@ const initUI = (global) => {
|
|
|
69880
69899
|
});
|
|
69881
69900
|
});
|
|
69882
69901
|
// Camera mode UI
|
|
69883
|
-
|
|
69884
|
-
dom.orbitCamera.classList
|
|
69885
|
-
dom.flyCamera.classList
|
|
69902
|
+
const updateCameraModeUI = () => {
|
|
69903
|
+
dom.orbitCamera.classList.toggle('active', state.cameraMode === 'orbit');
|
|
69904
|
+
dom.flyCamera.classList.toggle('active', state.cameraMode === 'fly');
|
|
69905
|
+
dom.fpsCamera.classList.toggle('active', state.cameraMode === 'fps');
|
|
69906
|
+
};
|
|
69907
|
+
events.on('cameraMode:changed', updateCameraModeUI);
|
|
69908
|
+
// show/hide the FPS button based on voxel data availability
|
|
69909
|
+
events.on('hasCollision:changed', (value) => {
|
|
69910
|
+
dom.fpsCamera.classList.toggle('hidden', !value);
|
|
69911
|
+
// adjust fly button shape: middle when FPS is visible, right when hidden
|
|
69912
|
+
dom.flyCamera.classList.toggle('middle', value);
|
|
69913
|
+
dom.flyCamera.classList.toggle('right', !value);
|
|
69914
|
+
});
|
|
69915
|
+
// Voxel overlay toggle (only visible when overlay is available)
|
|
69916
|
+
events.on('hasVoxelOverlay:changed', (value) => {
|
|
69917
|
+
dom.showVoxels.classList.toggle('hidden', !value);
|
|
69918
|
+
});
|
|
69919
|
+
dom.showVoxels.addEventListener('click', () => {
|
|
69920
|
+
state.voxelOverlayEnabled = !state.voxelOverlayEnabled;
|
|
69921
|
+
});
|
|
69922
|
+
events.on('voxelOverlayEnabled:changed', (value) => {
|
|
69923
|
+
dom.showVoxels.classList.toggle('active', value);
|
|
69886
69924
|
});
|
|
69887
69925
|
dom.settings.addEventListener('click', () => {
|
|
69888
69926
|
dom.settingsPanel.classList.toggle('hidden');
|
|
@@ -69893,6 +69931,9 @@ const initUI = (global) => {
|
|
|
69893
69931
|
dom.flyCamera.addEventListener('click', () => {
|
|
69894
69932
|
state.cameraMode = 'fly';
|
|
69895
69933
|
});
|
|
69934
|
+
dom.fpsCamera.addEventListener('click', () => {
|
|
69935
|
+
events.fire('inputEvent', 'toggleFps');
|
|
69936
|
+
});
|
|
69896
69937
|
dom.reset.addEventListener('click', (event) => {
|
|
69897
69938
|
events.fire('inputEvent', 'reset', event);
|
|
69898
69939
|
});
|
|
@@ -69913,8 +69954,10 @@ const initUI = (global) => {
|
|
|
69913
69954
|
tooltip.register(dom.pause, 'Pause', 'top');
|
|
69914
69955
|
tooltip.register(dom.orbitCamera, 'Orbit Camera', 'top');
|
|
69915
69956
|
tooltip.register(dom.flyCamera, 'Fly Camera', 'top');
|
|
69957
|
+
tooltip.register(dom.fpsCamera, 'Walk Mode', 'top');
|
|
69916
69958
|
tooltip.register(dom.reset, 'Reset Camera', 'bottom');
|
|
69917
69959
|
tooltip.register(dom.frame, 'Frame Scene', 'bottom');
|
|
69960
|
+
tooltip.register(dom.showVoxels, 'Show Voxels', 'top');
|
|
69918
69961
|
tooltip.register(dom.settings, 'Settings', 'top');
|
|
69919
69962
|
tooltip.register(dom.info, 'Help', 'top');
|
|
69920
69963
|
tooltip.register(dom.arMode, 'Enter AR', 'top');
|
|
@@ -70686,6 +70729,7 @@ class CubicSpline {
|
|
|
70686
70729
|
* @param dt - Delta time in seconds.
|
|
70687
70730
|
* @returns - Damping factor adjusted for the delta time.
|
|
70688
70731
|
*/
|
|
70732
|
+
const damp = (damping, dt) => 1 - Math.pow(damping, dt * 1000);
|
|
70689
70733
|
/**
|
|
70690
70734
|
* Easing function for smooth transitions.
|
|
70691
70735
|
*
|
|
@@ -70859,9 +70903,15 @@ class Camera {
|
|
|
70859
70903
|
}
|
|
70860
70904
|
}
|
|
70861
70905
|
|
|
70906
|
+
/** Radius of the camera collision sphere (meters) */
|
|
70907
|
+
const CAMERA_RADIUS = 0.2;
|
|
70862
70908
|
const p$1 = new Pose();
|
|
70909
|
+
/** Pre-allocated push-out vector for sphere collision */
|
|
70910
|
+
const pushOut$1 = { x: 0, y: 0, z: 0 };
|
|
70863
70911
|
class FlyController {
|
|
70864
70912
|
controller;
|
|
70913
|
+
/** Optional voxel collider for sphere collision with sliding */
|
|
70914
|
+
collider = null;
|
|
70865
70915
|
constructor() {
|
|
70866
70916
|
this.controller = new FlyController$1();
|
|
70867
70917
|
this.controller.pitchRange = new Vec2(-90, 90);
|
|
@@ -70876,9 +70926,33 @@ class FlyController {
|
|
|
70876
70926
|
}
|
|
70877
70927
|
update(deltaTime, inputFrame, camera) {
|
|
70878
70928
|
const pose = this.controller.update(inputFrame, deltaTime);
|
|
70879
|
-
camera.position.copy(pose.position);
|
|
70880
70929
|
camera.angles.copy(pose.angles);
|
|
70881
70930
|
camera.distance = pose.distance;
|
|
70931
|
+
if (this.collider) {
|
|
70932
|
+
// Resolve collision on _targetPose first. The engine's update() already
|
|
70933
|
+
// applied input to _targetPose and lerped _pose toward it. By correcting
|
|
70934
|
+
// _targetPose now, we ensure next frame's lerp interpolates toward a safe
|
|
70935
|
+
// position, preventing the camera from overshooting into the wall.
|
|
70936
|
+
const target = this.controller._targetPose;
|
|
70937
|
+
const tvx = -target.position.x;
|
|
70938
|
+
const tvy = -target.position.y;
|
|
70939
|
+
const tvz = target.position.z;
|
|
70940
|
+
if (this.collider.querySphere(tvx, tvy, tvz, CAMERA_RADIUS, pushOut$1)) {
|
|
70941
|
+
target.position.x += -pushOut$1.x;
|
|
70942
|
+
target.position.y += -pushOut$1.y;
|
|
70943
|
+
target.position.z += pushOut$1.z;
|
|
70944
|
+
}
|
|
70945
|
+
// Now resolve collision on the interpolated pose (_pose).
|
|
70946
|
+
const vx = -pose.position.x;
|
|
70947
|
+
const vy = -pose.position.y;
|
|
70948
|
+
const vz = pose.position.z;
|
|
70949
|
+
if (this.collider.querySphere(vx, vy, vz, CAMERA_RADIUS, pushOut$1)) {
|
|
70950
|
+
pose.position.x += -pushOut$1.x;
|
|
70951
|
+
pose.position.y += -pushOut$1.y;
|
|
70952
|
+
pose.position.z += pushOut$1.z;
|
|
70953
|
+
}
|
|
70954
|
+
}
|
|
70955
|
+
camera.position.copy(pose.position);
|
|
70882
70956
|
}
|
|
70883
70957
|
onExit(camera) {
|
|
70884
70958
|
}
|
|
@@ -70887,6 +70961,168 @@ class FlyController {
|
|
|
70887
70961
|
}
|
|
70888
70962
|
}
|
|
70889
70963
|
|
|
70964
|
+
/** Pre-allocated push-out vector for capsule collision */
|
|
70965
|
+
const pushOut = { x: 0, y: 0, z: 0 };
|
|
70966
|
+
/**
|
|
70967
|
+
* First-person shooter style camera controller with gravity and capsule collision.
|
|
70968
|
+
*
|
|
70969
|
+
* Movement is constrained to the horizontal plane (XZ) relative to the camera yaw.
|
|
70970
|
+
* Vertical motion is driven by gravity and resolved by capsule collision with the
|
|
70971
|
+
* voxel grid. The camera is positioned at eye height within the capsule.
|
|
70972
|
+
*/
|
|
70973
|
+
class FpsController {
|
|
70974
|
+
/** Optional voxel collider for capsule collision with sliding */
|
|
70975
|
+
collider = null;
|
|
70976
|
+
/** Total capsule height in meters (default: human proportion) */
|
|
70977
|
+
capsuleHeight = 1.8;
|
|
70978
|
+
/** Capsule radius in meters */
|
|
70979
|
+
capsuleRadius = 0.3;
|
|
70980
|
+
/** Camera height from the bottom of the capsule in meters */
|
|
70981
|
+
eyeHeight = 1.6;
|
|
70982
|
+
/** Gravity acceleration in m/s^2 */
|
|
70983
|
+
gravity = 9.8;
|
|
70984
|
+
/** Jump velocity in m/s */
|
|
70985
|
+
jumpSpeed = 5;
|
|
70986
|
+
/** Movement damping factor (0 = no damping, 1 = full damping) */
|
|
70987
|
+
moveDamping = 0.97;
|
|
70988
|
+
/** Rotation damping factor (0 = no damping, 1 = full damping) */
|
|
70989
|
+
rotateDamping = 0.97;
|
|
70990
|
+
// Target state (where input drives)
|
|
70991
|
+
_targetPosition = new Vec3();
|
|
70992
|
+
_targetYaw = 0;
|
|
70993
|
+
_targetPitch = 0;
|
|
70994
|
+
// Smoothed state (lerps toward target)
|
|
70995
|
+
_position = new Vec3();
|
|
70996
|
+
_yaw = 0;
|
|
70997
|
+
_pitch = 0;
|
|
70998
|
+
// Vertical velocity for gravity
|
|
70999
|
+
_velocityY = 0;
|
|
71000
|
+
// Whether the capsule is resting on the ground
|
|
71001
|
+
_onGround = false;
|
|
71002
|
+
onEnter(camera) {
|
|
71003
|
+
this._position.copy(camera.position);
|
|
71004
|
+
this._targetPosition.copy(camera.position);
|
|
71005
|
+
// angles.x = pitch, angles.y = yaw
|
|
71006
|
+
this._pitch = camera.angles.x;
|
|
71007
|
+
this._targetPitch = camera.angles.x;
|
|
71008
|
+
this._yaw = camera.angles.y;
|
|
71009
|
+
this._targetYaw = camera.angles.y;
|
|
71010
|
+
this._velocityY = 0;
|
|
71011
|
+
this._onGround = false;
|
|
71012
|
+
}
|
|
71013
|
+
update(deltaTime, inputFrame, camera) {
|
|
71014
|
+
const { move, rotate } = inputFrame.read();
|
|
71015
|
+
// --- Rotation ---
|
|
71016
|
+
this._targetYaw -= rotate[0];
|
|
71017
|
+
this._targetPitch = math.clamp(this._targetPitch - rotate[1], -90, 90);
|
|
71018
|
+
const rotateLerp = damp(this.rotateDamping, deltaTime);
|
|
71019
|
+
this._yaw = math.lerpAngle(this._yaw, this._targetYaw, rotateLerp);
|
|
71020
|
+
this._pitch = math.lerp(this._pitch, this._targetPitch, rotateLerp);
|
|
71021
|
+
// Normalize yaw to prevent floating-point drift over long sessions.
|
|
71022
|
+
// Shift both values by the same multiple of 360 to preserve their delta.
|
|
71023
|
+
const yawOffset = Math.round(this._yaw / 360) * 360;
|
|
71024
|
+
if (yawOffset !== 0) {
|
|
71025
|
+
this._yaw -= yawOffset;
|
|
71026
|
+
this._targetYaw -= yawOffset;
|
|
71027
|
+
}
|
|
71028
|
+
// --- Horizontal movement ---
|
|
71029
|
+
// Compute forward/right from yaw only (ignore pitch for movement)
|
|
71030
|
+
const yawRad = this._targetYaw * math.DEG_TO_RAD;
|
|
71031
|
+
const sinYaw = Math.sin(yawRad);
|
|
71032
|
+
const cosYaw = Math.cos(yawRad);
|
|
71033
|
+
// PlayCanvas convention: forward = -Z, right = +X
|
|
71034
|
+
const forwardX = -sinYaw;
|
|
71035
|
+
const forwardZ = -cosYaw;
|
|
71036
|
+
const rightX = cosYaw;
|
|
71037
|
+
const rightZ = -sinYaw;
|
|
71038
|
+
const moveLerp = damp(this.moveDamping, deltaTime);
|
|
71039
|
+
this._targetPosition.x += rightX * move[0] + forwardX * move[2];
|
|
71040
|
+
this._targetPosition.z += rightZ * move[0] + forwardZ * move[2];
|
|
71041
|
+
// --- Jump ---
|
|
71042
|
+
if (move[1] > 0 && this._onGround) {
|
|
71043
|
+
this._velocityY = this.jumpSpeed;
|
|
71044
|
+
this._onGround = false;
|
|
71045
|
+
}
|
|
71046
|
+
// --- Gravity ---
|
|
71047
|
+
this._velocityY -= this.gravity * deltaTime;
|
|
71048
|
+
this._targetPosition.y += this._velocityY * deltaTime;
|
|
71049
|
+
// --- Capsule collision on target position ---
|
|
71050
|
+
if (this.collider) {
|
|
71051
|
+
this._resolveCapsuleCollision(this._targetPosition, true);
|
|
71052
|
+
}
|
|
71053
|
+
// --- Smooth horizontal position toward target ---
|
|
71054
|
+
this._position.x = math.lerp(this._position.x, this._targetPosition.x, moveLerp);
|
|
71055
|
+
this._position.z = math.lerp(this._position.z, this._targetPosition.z, moveLerp);
|
|
71056
|
+
// No smoothing on Y for crisp ground contact
|
|
71057
|
+
this._position.y = this._targetPosition.y;
|
|
71058
|
+
// --- Capsule collision on smoothed position ---
|
|
71059
|
+
if (this.collider) {
|
|
71060
|
+
this._resolveCapsuleCollision(this._position, false);
|
|
71061
|
+
}
|
|
71062
|
+
// --- Output to camera ---
|
|
71063
|
+
camera.position.copy(this._position);
|
|
71064
|
+
camera.angles.set(this._pitch, this._yaw, 0);
|
|
71065
|
+
}
|
|
71066
|
+
onExit(_camera) {
|
|
71067
|
+
// nothing to clean up
|
|
71068
|
+
}
|
|
71069
|
+
/**
|
|
71070
|
+
* Teleport the controller to a given camera state (used for transitions).
|
|
71071
|
+
*
|
|
71072
|
+
* @param camera - The camera state to jump to.
|
|
71073
|
+
*/
|
|
71074
|
+
goto(camera) {
|
|
71075
|
+
this._position.copy(camera.position);
|
|
71076
|
+
this._targetPosition.copy(camera.position);
|
|
71077
|
+
this._pitch = camera.angles.x;
|
|
71078
|
+
this._targetPitch = camera.angles.x;
|
|
71079
|
+
this._yaw = camera.angles.y;
|
|
71080
|
+
this._targetYaw = camera.angles.y;
|
|
71081
|
+
this._velocityY = 0;
|
|
71082
|
+
this._onGround = false;
|
|
71083
|
+
}
|
|
71084
|
+
/**
|
|
71085
|
+
* Resolve capsule collision for a given eye position, modifying it in place.
|
|
71086
|
+
* Converts from PlayCanvas space to voxel space, queries the capsule, and
|
|
71087
|
+
* applies the push-out vector back to PlayCanvas space.
|
|
71088
|
+
*
|
|
71089
|
+
* @param eyePos - Eye position to resolve (modified in place).
|
|
71090
|
+
* @param isTarget - Whether this is the target position (updates velocity on ground contact).
|
|
71091
|
+
*/
|
|
71092
|
+
_resolveCapsuleCollision(eyePos, isTarget) {
|
|
71093
|
+
// Derive capsule center from eye position in PlayCanvas space:
|
|
71094
|
+
// bottom of capsule = eyePos.y - eyeHeight
|
|
71095
|
+
// capsule center = eyePos.y - eyeHeight + capsuleHeight / 2
|
|
71096
|
+
const capsuleCenterPCY = eyePos.y - this.eyeHeight + this.capsuleHeight * 0.5;
|
|
71097
|
+
const halfHeight = this.capsuleHeight * 0.5 - this.capsuleRadius;
|
|
71098
|
+
// Convert to voxel space (negate X, negate Y, keep Z)
|
|
71099
|
+
const vx = -eyePos.x;
|
|
71100
|
+
const vy = -capsuleCenterPCY;
|
|
71101
|
+
const vz = eyePos.z;
|
|
71102
|
+
if (this.collider.queryCapsule(vx, vy, vz, halfHeight, this.capsuleRadius, pushOut)) {
|
|
71103
|
+
// Convert push-out back to PlayCanvas space
|
|
71104
|
+
const pushPCX = -pushOut.x;
|
|
71105
|
+
const pushPCY = -pushOut.y;
|
|
71106
|
+
const pushPCZ = pushOut.z;
|
|
71107
|
+
eyePos.x += pushPCX;
|
|
71108
|
+
// Push-out applies to capsule center; eye moves by the same amount
|
|
71109
|
+
eyePos.y += pushPCY;
|
|
71110
|
+
eyePos.z += pushPCZ;
|
|
71111
|
+
// Ground detection: if pushed upward and falling, cancel downward velocity
|
|
71112
|
+
if (isTarget) {
|
|
71113
|
+
if (pushPCY > 0 && this._velocityY < 0) {
|
|
71114
|
+
this._velocityY = 0;
|
|
71115
|
+
this._onGround = true;
|
|
71116
|
+
}
|
|
71117
|
+
// Ceiling detection: if pushed downward and rising, cancel upward velocity
|
|
71118
|
+
if (pushPCY < 0 && this._velocityY > 0) {
|
|
71119
|
+
this._velocityY = 0;
|
|
71120
|
+
}
|
|
71121
|
+
}
|
|
71122
|
+
}
|
|
71123
|
+
}
|
|
71124
|
+
}
|
|
71125
|
+
|
|
70890
71126
|
const p = new Pose();
|
|
70891
71127
|
class OrbitController {
|
|
70892
71128
|
controller;
|
|
@@ -70937,7 +71173,7 @@ class CameraManager {
|
|
|
70937
71173
|
update;
|
|
70938
71174
|
// holds the camera state
|
|
70939
71175
|
camera = new Camera();
|
|
70940
|
-
constructor(global, bbox) {
|
|
71176
|
+
constructor(global, bbox, collider = null) {
|
|
70941
71177
|
const { events, settings, state } = global;
|
|
70942
71178
|
const camera0 = settings.cameras[0].initial;
|
|
70943
71179
|
const frameCamera = createFrameCamera(bbox, camera0.fov);
|
|
@@ -70962,8 +71198,11 @@ class CameraManager {
|
|
|
70962
71198
|
const controllers = {
|
|
70963
71199
|
orbit: new OrbitController(),
|
|
70964
71200
|
fly: new FlyController(),
|
|
71201
|
+
fps: new FpsController(),
|
|
70965
71202
|
anim: animTrack ? new AnimController(animTrack) : null
|
|
70966
71203
|
};
|
|
71204
|
+
controllers.fly.collider = collider;
|
|
71205
|
+
controllers.fps.collider = collider;
|
|
70967
71206
|
const getController = (cameraMode) => {
|
|
70968
71207
|
return controllers[cameraMode];
|
|
70969
71208
|
};
|
|
@@ -70976,6 +71215,8 @@ class CameraManager {
|
|
|
70976
71215
|
const target = new Camera(this.camera); // the active controller updates this
|
|
70977
71216
|
const from = new Camera(this.camera); // stores the previous camera state during transition
|
|
70978
71217
|
let fromMode = isObjectExperience ? 'orbit' : 'fly';
|
|
71218
|
+
// tracks the mode to restore when exiting FPS
|
|
71219
|
+
let preFpsMode = 'fly';
|
|
70979
71220
|
// enter the initial controller
|
|
70980
71221
|
getController(state.cameraMode).onEnter(this.camera);
|
|
70981
71222
|
// transition state
|
|
@@ -71030,7 +71271,28 @@ class CameraManager {
|
|
|
71030
71271
|
}
|
|
71031
71272
|
}
|
|
71032
71273
|
break;
|
|
71274
|
+
case 'requestFirstPerson':
|
|
71275
|
+
state.cameraMode = 'fly';
|
|
71276
|
+
break;
|
|
71277
|
+
case 'toggleFps':
|
|
71278
|
+
if (collider) {
|
|
71279
|
+
if (state.cameraMode === 'fps') {
|
|
71280
|
+
state.cameraMode = preFpsMode;
|
|
71281
|
+
}
|
|
71282
|
+
else {
|
|
71283
|
+
preFpsMode = state.cameraMode;
|
|
71284
|
+
state.cameraMode = 'fps';
|
|
71285
|
+
}
|
|
71286
|
+
}
|
|
71287
|
+
break;
|
|
71033
71288
|
case 'cancel':
|
|
71289
|
+
if (state.cameraMode === 'anim') {
|
|
71290
|
+
state.cameraMode = fromMode;
|
|
71291
|
+
}
|
|
71292
|
+
else if (state.cameraMode === 'fps') {
|
|
71293
|
+
state.cameraMode = preFpsMode;
|
|
71294
|
+
}
|
|
71295
|
+
break;
|
|
71034
71296
|
case 'interrupt':
|
|
71035
71297
|
if (state.cameraMode === 'anim') {
|
|
71036
71298
|
state.cameraMode = fromMode;
|
|
@@ -71317,6 +71579,7 @@ class InputController {
|
|
|
71317
71579
|
mouse: [0, 0, 0],
|
|
71318
71580
|
shift: 0,
|
|
71319
71581
|
ctrl: 0,
|
|
71582
|
+
jump: 0,
|
|
71320
71583
|
touches: 0
|
|
71321
71584
|
};
|
|
71322
71585
|
_desktopInput = new KeyboardMouseSource();
|
|
@@ -71400,7 +71663,7 @@ class InputController {
|
|
|
71400
71663
|
if (event.key === 'Escape') {
|
|
71401
71664
|
events.fire('inputEvent', 'cancel', event);
|
|
71402
71665
|
}
|
|
71403
|
-
else if (!event.ctrlKey && !event.altKey && !event.metaKey) {
|
|
71666
|
+
else if (state.cameraMode !== 'fps' && !event.ctrlKey && !event.altKey && !event.metaKey) {
|
|
71404
71667
|
switch (event.key) {
|
|
71405
71668
|
case 'f':
|
|
71406
71669
|
events.fire('inputEvent', 'frame', event);
|
|
@@ -71414,6 +71677,30 @@ class InputController {
|
|
|
71414
71677
|
}
|
|
71415
71678
|
}
|
|
71416
71679
|
});
|
|
71680
|
+
// Pointer lock management for FPS mode on desktop
|
|
71681
|
+
events.on('cameraMode:changed', (value, prev) => {
|
|
71682
|
+
if (value === 'fps' && state.inputMode === 'desktop') {
|
|
71683
|
+
this._desktopInput._pointerLock = true;
|
|
71684
|
+
canvas.requestPointerLock();
|
|
71685
|
+
}
|
|
71686
|
+
else if (prev === 'fps') {
|
|
71687
|
+
this._desktopInput._pointerLock = false;
|
|
71688
|
+
if (document.pointerLockElement === canvas) {
|
|
71689
|
+
document.exitPointerLock();
|
|
71690
|
+
}
|
|
71691
|
+
}
|
|
71692
|
+
});
|
|
71693
|
+
document.addEventListener('pointerlockchange', () => {
|
|
71694
|
+
if (!document.pointerLockElement && state.cameraMode === 'fps') {
|
|
71695
|
+
events.fire('inputEvent', 'cancel');
|
|
71696
|
+
}
|
|
71697
|
+
});
|
|
71698
|
+
// Pointer lock request rejected (e.g., no user gesture, document hidden).
|
|
71699
|
+
// Revert to avoid being stuck in FPS mode without mouse capture.
|
|
71700
|
+
document.addEventListener('pointerlockerror', () => {
|
|
71701
|
+
this._desktopInput._pointerLock = false;
|
|
71702
|
+
events.fire('inputEvent', 'cancel');
|
|
71703
|
+
});
|
|
71417
71704
|
}
|
|
71418
71705
|
/**
|
|
71419
71706
|
* @param dt - delta time in seconds
|
|
@@ -71426,29 +71713,41 @@ class InputController {
|
|
|
71426
71713
|
const { key, button, mouse, wheel } = this._desktopInput.read();
|
|
71427
71714
|
const { touch, pinch, count } = this._orbitInput.read();
|
|
71428
71715
|
const { leftStick, rightStick } = this._gamepadInput.read();
|
|
71429
|
-
const { state } = this.global;
|
|
71716
|
+
const { state, events } = this.global;
|
|
71430
71717
|
const { camera } = this.global.camera;
|
|
71431
71718
|
// update state
|
|
71432
71719
|
this._state.axis.add(tmpV1.set((key[keyCode.D] - key[keyCode.A]) + (key[keyCode.RIGHT] - key[keyCode.LEFT]), (key[keyCode.E] - key[keyCode.Q]), (key[keyCode.W] - key[keyCode.S]) + (key[keyCode.UP] - key[keyCode.DOWN])));
|
|
71720
|
+
this._state.jump += key[keyCode.SPACE];
|
|
71433
71721
|
this._state.touches += count[0];
|
|
71434
71722
|
for (let i = 0; i < button.length; i++) {
|
|
71435
71723
|
this._state.mouse[i] += button[i];
|
|
71436
71724
|
}
|
|
71437
71725
|
this._state.shift += key[keyCode.SHIFT];
|
|
71438
71726
|
this._state.ctrl += key[keyCode.CTRL];
|
|
71439
|
-
|
|
71440
|
-
|
|
71727
|
+
const isFps = state.cameraMode === 'fps';
|
|
71728
|
+
const isFirstPerson = state.cameraMode === 'fly' || isFps;
|
|
71729
|
+
if (!isFirstPerson && this._state.axis.length() > 0) {
|
|
71730
|
+
events.fire('inputEvent', 'requestFirstPerson');
|
|
71441
71731
|
}
|
|
71442
71732
|
const orbit = +(state.cameraMode === 'orbit');
|
|
71443
|
-
const fly = +
|
|
71733
|
+
const fly = +isFirstPerson;
|
|
71444
71734
|
const double = +(this._state.touches > 1);
|
|
71445
71735
|
const pan = this._state.mouse[2] || +(button[2] === -1) || double;
|
|
71446
71736
|
const orbitFactor = fly ? camera.fov / 120 : 1;
|
|
71447
71737
|
const { deltas } = this.frame;
|
|
71448
71738
|
// desktop move
|
|
71449
71739
|
const v = tmpV1.set(0, 0, 0);
|
|
71450
|
-
const keyMove = this._state.axis.clone()
|
|
71740
|
+
const keyMove = this._state.axis.clone();
|
|
71741
|
+
if (isFps) {
|
|
71742
|
+
// In FPS mode, normalize only horizontal axes so jump doesn't reduce speed
|
|
71743
|
+
keyMove.y = 0;
|
|
71744
|
+
}
|
|
71745
|
+
keyMove.normalize();
|
|
71451
71746
|
v.add(keyMove.mulScalar(fly * this.moveSpeed * (this._state.shift ? 4 : this._state.ctrl ? 0.25 : 1) * dt));
|
|
71747
|
+
if (isFps) {
|
|
71748
|
+
// Pass jump signal as raw Y; FPS controller uses move[1] > 0 as boolean trigger
|
|
71749
|
+
v.y = this._state.jump > 0 ? 1 : 0;
|
|
71750
|
+
}
|
|
71452
71751
|
const panMove = screenToWorld(camera, mouse[0], mouse[1], distance);
|
|
71453
71752
|
v.add(panMove.mulScalar(pan));
|
|
71454
71753
|
const wheelMove = new Vec3(0, 0, -wheel[0]);
|
|
@@ -71493,6 +71792,606 @@ class InputController {
|
|
|
71493
71792
|
}
|
|
71494
71793
|
}
|
|
71495
71794
|
|
|
71795
|
+
// ---------------------------------------------------------------------------
|
|
71796
|
+
// WGSL compute shader: ray-march through the sparse voxel octree per pixel
|
|
71797
|
+
// ---------------------------------------------------------------------------
|
|
71798
|
+
const voxelOverlayWGSL = /* wgsl */ `
|
|
71799
|
+
|
|
71800
|
+
// Solid leaf sentinel: childMask=0xFF, baseOffset=0
|
|
71801
|
+
const SOLID_LEAF_MARKER: u32 = 0xFF000000u;
|
|
71802
|
+
|
|
71803
|
+
// Maximum DDA steps to prevent infinite loops
|
|
71804
|
+
const MAX_STEPS: u32 = 512u;
|
|
71805
|
+
|
|
71806
|
+
// Target wireframe edge width in pixels
|
|
71807
|
+
const EDGE_PIXELS: f32 = 1.5;
|
|
71808
|
+
|
|
71809
|
+
// Wireframe edge alpha
|
|
71810
|
+
const EDGE_ALPHA: f32 = 0.85;
|
|
71811
|
+
|
|
71812
|
+
// Interior fill alpha (subtle orientation tint)
|
|
71813
|
+
const FILL_ALPHA: f32 = 0.12;
|
|
71814
|
+
|
|
71815
|
+
struct Uniforms {
|
|
71816
|
+
invVP: mat4x4<f32>,
|
|
71817
|
+
screenWidth: u32,
|
|
71818
|
+
screenHeight: u32,
|
|
71819
|
+
gridMinX: f32,
|
|
71820
|
+
gridMinY: f32,
|
|
71821
|
+
gridMinZ: f32,
|
|
71822
|
+
voxelRes: f32,
|
|
71823
|
+
numVoxelsX: u32,
|
|
71824
|
+
numVoxelsY: u32,
|
|
71825
|
+
numVoxelsZ: u32,
|
|
71826
|
+
leafSize: u32,
|
|
71827
|
+
treeDepth: u32,
|
|
71828
|
+
projScaleY: f32,
|
|
71829
|
+
displayMode: u32,
|
|
71830
|
+
pad2: u32
|
|
71831
|
+
};
|
|
71832
|
+
|
|
71833
|
+
@group(0) @binding(0) var<uniform> uniforms: Uniforms;
|
|
71834
|
+
@group(0) @binding(1) var<storage, read> nodes: array<u32>;
|
|
71835
|
+
@group(0) @binding(2) var<storage, read> leafData: array<u32>;
|
|
71836
|
+
@group(0) @binding(3) var outputTexture: texture_storage_2d<rgba8unorm, write>;
|
|
71837
|
+
|
|
71838
|
+
// ---- helpers ----
|
|
71839
|
+
|
|
71840
|
+
// Traverse the octree for block (bx, by, bz). Returns vec2u(result, emptyLevel):
|
|
71841
|
+
// result: 0 = empty, 1 = solid, 2+ = mixed leaf (2 + leafDataIndex)
|
|
71842
|
+
// emptyLevel: octree level at which emptiness was detected (only meaningful when result == 0)
|
|
71843
|
+
fn queryBlock(bx: i32, by: i32, bz: i32) -> vec2u {
|
|
71844
|
+
let depth = uniforms.treeDepth;
|
|
71845
|
+
var nodeIndex: u32 = 0u;
|
|
71846
|
+
|
|
71847
|
+
for (var level: u32 = depth - 1u; ; ) {
|
|
71848
|
+
let node = nodes[nodeIndex];
|
|
71849
|
+
|
|
71850
|
+
// Solid leaf sentinel
|
|
71851
|
+
if (node == SOLID_LEAF_MARKER) {
|
|
71852
|
+
return vec2u(1u, 0u);
|
|
71853
|
+
}
|
|
71854
|
+
|
|
71855
|
+
let childMask = (node >> 24u) & 0xFFu;
|
|
71856
|
+
|
|
71857
|
+
// childMask == 0 means this is a mixed leaf node
|
|
71858
|
+
if (childMask == 0u) {
|
|
71859
|
+
let leafIdx = node & 0x00FFFFFFu;
|
|
71860
|
+
return vec2u(2u + leafIdx, 0u);
|
|
71861
|
+
}
|
|
71862
|
+
|
|
71863
|
+
// Determine octant at this level
|
|
71864
|
+
let bitX = (u32(bx) >> level) & 1u;
|
|
71865
|
+
let bitY = (u32(by) >> level) & 1u;
|
|
71866
|
+
let bitZ = (u32(bz) >> level) & 1u;
|
|
71867
|
+
let octant = (bitZ << 2u) | (bitY << 1u) | bitX;
|
|
71868
|
+
|
|
71869
|
+
// Check if child exists
|
|
71870
|
+
if ((childMask & (1u << octant)) == 0u) {
|
|
71871
|
+
return vec2u(0u, level);
|
|
71872
|
+
}
|
|
71873
|
+
|
|
71874
|
+
// Compute child index
|
|
71875
|
+
let baseOffset = node & 0x00FFFFFFu;
|
|
71876
|
+
let prefix = (1u << octant) - 1u;
|
|
71877
|
+
let childOffset = countOneBits(childMask & prefix);
|
|
71878
|
+
nodeIndex = baseOffset + childOffset;
|
|
71879
|
+
|
|
71880
|
+
if (level == 0u) { break; }
|
|
71881
|
+
level -= 1u;
|
|
71882
|
+
}
|
|
71883
|
+
|
|
71884
|
+
// Reached leaf level
|
|
71885
|
+
let node = nodes[nodeIndex];
|
|
71886
|
+
if (node == SOLID_LEAF_MARKER) {
|
|
71887
|
+
return vec2u(1u, 0u);
|
|
71888
|
+
}
|
|
71889
|
+
let leafIdx = node & 0x00FFFFFFu;
|
|
71890
|
+
return vec2u(2u + leafIdx, 0u);
|
|
71891
|
+
}
|
|
71892
|
+
|
|
71893
|
+
// Ray-AABB intersection returning (tNear, tFar). If tNear > tFar → miss.
|
|
71894
|
+
fn intersectAABB(ro: vec3f, invDir: vec3f, bmin: vec3f, bmax: vec3f) -> vec2f {
|
|
71895
|
+
let t1 = (bmin - ro) * invDir;
|
|
71896
|
+
let t2 = (bmax - ro) * invDir;
|
|
71897
|
+
let tmin = min(t1, t2);
|
|
71898
|
+
let tmax = max(t1, t2);
|
|
71899
|
+
let tNear = max(max(tmin.x, tmin.y), tmin.z);
|
|
71900
|
+
let tFar = min(min(tmax.x, tmax.y), tmax.z);
|
|
71901
|
+
return vec2f(tNear, tFar);
|
|
71902
|
+
}
|
|
71903
|
+
|
|
71904
|
+
// Compute wireframe edge factor (0 = interior, 1 = on edge) for a hit point on a voxel cube.
|
|
71905
|
+
// Uses the median of the three per-axis face distances so it works on ANY face.
|
|
71906
|
+
fn edgeFactor(hitPos: vec3f, voxMin: vec3f, voxSize: f32, edgeWidth: f32) -> f32 {
|
|
71907
|
+
let local = (hitPos - voxMin) / voxSize;
|
|
71908
|
+
|
|
71909
|
+
// Distance to nearest face boundary for each axis
|
|
71910
|
+
let fx = min(local.x, 1.0 - local.x);
|
|
71911
|
+
let fy = min(local.y, 1.0 - local.y);
|
|
71912
|
+
let fz = min(local.z, 1.0 - local.z);
|
|
71913
|
+
|
|
71914
|
+
// Median of three values = second smallest = edge distance.
|
|
71915
|
+
// On a face, one of fx/fy/fz is ~0 (the face normal axis).
|
|
71916
|
+
// The median gives the smaller of the other two = distance to nearest edge.
|
|
71917
|
+
let edgeDist = max(min(fx, fy), min(max(fx, fy), fz));
|
|
71918
|
+
|
|
71919
|
+
return 1.0 - smoothstep(0.0, edgeWidth, edgeDist);
|
|
71920
|
+
}
|
|
71921
|
+
|
|
71922
|
+
// Shade a voxel hit, returning premultiplied RGBA
|
|
71923
|
+
fn shadeVoxelHit(hitPos: vec3f, voxMin: vec3f, voxelRes: f32, ro: vec3f, isSolid: bool) -> vec4f {
|
|
71924
|
+
let dist = length(hitPos - ro);
|
|
71925
|
+
let pixelWorld = 2.0 * dist / (f32(uniforms.screenHeight) * uniforms.projScaleY);
|
|
71926
|
+
let ew = clamp(EDGE_PIXELS * pixelWorld / voxelRes, 0.01, 0.5);
|
|
71927
|
+
|
|
71928
|
+
let ef = edgeFactor(hitPos, voxMin, voxelRes, ew);
|
|
71929
|
+
let distFade = clamp(1.0 - dist * 0.01, 0.2, 1.0);
|
|
71930
|
+
|
|
71931
|
+
let local = (hitPos - voxMin) / voxelRes;
|
|
71932
|
+
let fx = min(local.x, 1.0 - local.x);
|
|
71933
|
+
let fy = min(local.y, 1.0 - local.y);
|
|
71934
|
+
let fz = min(local.z, 1.0 - local.z);
|
|
71935
|
+
|
|
71936
|
+
var faceAxis: u32 = 0u;
|
|
71937
|
+
if (fy <= fx && fy <= fz) {
|
|
71938
|
+
faceAxis = 1u;
|
|
71939
|
+
} else if (fz <= fx) {
|
|
71940
|
+
faceAxis = 2u;
|
|
71941
|
+
}
|
|
71942
|
+
|
|
71943
|
+
var baseColor: vec3f;
|
|
71944
|
+
if (isSolid) {
|
|
71945
|
+
if (faceAxis == 0u) { baseColor = vec3f(1.0, 0.25, 0.2); }
|
|
71946
|
+
else if (faceAxis == 1u) { baseColor = vec3f(0.8, 0.15, 0.1); }
|
|
71947
|
+
else { baseColor = vec3f(0.55, 0.08, 0.05); }
|
|
71948
|
+
} else {
|
|
71949
|
+
if (faceAxis == 0u) { baseColor = vec3f(0.7, 0.7, 0.72); }
|
|
71950
|
+
else if (faceAxis == 1u) { baseColor = vec3f(0.5, 0.5, 0.52); }
|
|
71951
|
+
else { baseColor = vec3f(0.33, 0.33, 0.35); }
|
|
71952
|
+
}
|
|
71953
|
+
|
|
71954
|
+
let alpha = mix(FILL_ALPHA, EDGE_ALPHA, ef) * distFade;
|
|
71955
|
+
|
|
71956
|
+
return vec4f(mix(baseColor, vec3f(0.0), alpha) * alpha, alpha);
|
|
71957
|
+
}
|
|
71958
|
+
|
|
71959
|
+
// Blue (0) -> Cyan (0.25) -> Green (0.5) -> Yellow (0.75) -> Red (1.0)
|
|
71960
|
+
fn heatmap(t: f32) -> vec3f {
|
|
71961
|
+
let c = clamp(t, 0.0, 1.0);
|
|
71962
|
+
let r = clamp(min(c - 0.5, 1.0) * 2.0, 0.0, 1.0);
|
|
71963
|
+
let g = select(clamp(c * 4.0, 0.0, 1.0), clamp((1.0 - c) * 4.0, 0.0, 1.0), c > 0.5);
|
|
71964
|
+
let b = clamp(1.0 - c * 2.0, 0.0, 1.0);
|
|
71965
|
+
return vec3f(r, g, b);
|
|
71966
|
+
}
|
|
71967
|
+
|
|
71968
|
+
// ---- main ----
|
|
71969
|
+
|
|
71970
|
+
@compute @workgroup_size(8, 8, 1)
|
|
71971
|
+
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
71972
|
+
let px = i32(gid.x);
|
|
71973
|
+
let py = i32(gid.y);
|
|
71974
|
+
let sw = i32(uniforms.screenWidth);
|
|
71975
|
+
let sh = i32(uniforms.screenHeight);
|
|
71976
|
+
|
|
71977
|
+
if (px >= sw || py >= sh) {
|
|
71978
|
+
return;
|
|
71979
|
+
}
|
|
71980
|
+
|
|
71981
|
+
// Reconstruct world-space ray from pixel coordinates
|
|
71982
|
+
let ndcX = (f32(px) + 0.5) / f32(sw) * 2.0 - 1.0;
|
|
71983
|
+
let ndcY = -((f32(py) + 0.5) / f32(sh) * 2.0 - 1.0);
|
|
71984
|
+
|
|
71985
|
+
let clipNear = vec4f(ndcX, ndcY, 0.0, 1.0);
|
|
71986
|
+
let clipFar = vec4f(ndcX, ndcY, 1.0, 1.0);
|
|
71987
|
+
|
|
71988
|
+
var worldNear = uniforms.invVP * clipNear;
|
|
71989
|
+
worldNear = worldNear / worldNear.w;
|
|
71990
|
+
var worldFar = uniforms.invVP * clipFar;
|
|
71991
|
+
worldFar = worldFar / worldFar.w;
|
|
71992
|
+
|
|
71993
|
+
// Convert from PlayCanvas world space to voxel space (negate X and Y)
|
|
71994
|
+
let ro = vec3f(-worldNear.x, -worldNear.y, worldNear.z);
|
|
71995
|
+
let rd = normalize(vec3f(-(worldFar.x - worldNear.x), -(worldFar.y - worldNear.y), worldFar.z - worldNear.z));
|
|
71996
|
+
|
|
71997
|
+
// Grid AABB
|
|
71998
|
+
let gridMin = vec3f(uniforms.gridMinX, uniforms.gridMinY, uniforms.gridMinZ);
|
|
71999
|
+
let gridMax = gridMin + vec3f(
|
|
72000
|
+
f32(uniforms.numVoxelsX),
|
|
72001
|
+
f32(uniforms.numVoxelsY),
|
|
72002
|
+
f32(uniforms.numVoxelsZ)
|
|
72003
|
+
) * uniforms.voxelRes;
|
|
72004
|
+
|
|
72005
|
+
let invDir = 1.0 / rd;
|
|
72006
|
+
let gridHit = intersectAABB(ro, invDir, gridMin, gridMax);
|
|
72007
|
+
|
|
72008
|
+
if (gridHit.x > gridHit.y) {
|
|
72009
|
+
textureStore(outputTexture, vec2i(px, py), vec4f(0.0));
|
|
72010
|
+
return;
|
|
72011
|
+
}
|
|
72012
|
+
|
|
72013
|
+
let tEntry = max(gridHit.x, 0.0) + 0.0001;
|
|
72014
|
+
|
|
72015
|
+
// Entry point in voxel-index space
|
|
72016
|
+
let entryWorld = ro + rd * tEntry;
|
|
72017
|
+
let voxelRes = uniforms.voxelRes;
|
|
72018
|
+
let lsf = f32(uniforms.leafSize);
|
|
72019
|
+
let blockRes = voxelRes * lsf;
|
|
72020
|
+
let leafSz = i32(uniforms.leafSize);
|
|
72021
|
+
|
|
72022
|
+
// Block-level DDA setup
|
|
72023
|
+
let entryBlock = (entryWorld - gridMin) / blockRes;
|
|
72024
|
+
let numBlocksX = i32(uniforms.numVoxelsX / uniforms.leafSize);
|
|
72025
|
+
let numBlocksY = i32(uniforms.numVoxelsY / uniforms.leafSize);
|
|
72026
|
+
let numBlocksZ = i32(uniforms.numVoxelsZ / uniforms.leafSize);
|
|
72027
|
+
|
|
72028
|
+
var bx = clamp(i32(floor(entryBlock.x)), 0, numBlocksX - 1);
|
|
72029
|
+
var by = clamp(i32(floor(entryBlock.y)), 0, numBlocksY - 1);
|
|
72030
|
+
var bz = clamp(i32(floor(entryBlock.z)), 0, numBlocksZ - 1);
|
|
72031
|
+
|
|
72032
|
+
let stepX = select(-1, 1, rd.x >= 0.0);
|
|
72033
|
+
let stepY = select(-1, 1, rd.y >= 0.0);
|
|
72034
|
+
let stepZ = select(-1, 1, rd.z >= 0.0);
|
|
72035
|
+
|
|
72036
|
+
let tDeltaX = abs(blockRes / rd.x);
|
|
72037
|
+
let tDeltaY = abs(blockRes / rd.y);
|
|
72038
|
+
let tDeltaZ = abs(blockRes / rd.z);
|
|
72039
|
+
|
|
72040
|
+
// tMax: t value to reach next block boundary along each axis
|
|
72041
|
+
let blockMinWorld = gridMin + vec3f(f32(bx), f32(by), f32(bz)) * blockRes;
|
|
72042
|
+
let nextBoundX = select(blockMinWorld.x, blockMinWorld.x + blockRes, rd.x >= 0.0);
|
|
72043
|
+
let nextBoundY = select(blockMinWorld.y, blockMinWorld.y + blockRes, rd.y >= 0.0);
|
|
72044
|
+
let nextBoundZ = select(blockMinWorld.z, blockMinWorld.z + blockRes, rd.z >= 0.0);
|
|
72045
|
+
|
|
72046
|
+
var tMaxX = (nextBoundX - ro.x) / rd.x;
|
|
72047
|
+
var tMaxY = (nextBoundY - ro.y) / rd.y;
|
|
72048
|
+
var tMaxZ = (nextBoundZ - ro.z) / rd.z;
|
|
72049
|
+
|
|
72050
|
+
var totalWork: u32 = 0u;
|
|
72051
|
+
|
|
72052
|
+
for (var step: u32 = 0u; step < MAX_STEPS; step++) {
|
|
72053
|
+
totalWork += 1u;
|
|
72054
|
+
|
|
72055
|
+
let qResult = queryBlock(bx, by, bz);
|
|
72056
|
+
let blockResult = qResult.x;
|
|
72057
|
+
let emptyLevel = qResult.y;
|
|
72058
|
+
|
|
72059
|
+
if (blockResult == 0u && emptyLevel >= 1u) {
|
|
72060
|
+
// Large empty region: advance the block DDA past the empty cell
|
|
72061
|
+
let cellBlocks = i32(1u << emptyLevel);
|
|
72062
|
+
let cellMask = ~(cellBlocks - 1);
|
|
72063
|
+
let cellXMin = bx & cellMask;
|
|
72064
|
+
let cellYMin = by & cellMask;
|
|
72065
|
+
let cellZMin = bz & cellMask;
|
|
72066
|
+
|
|
72067
|
+
for (var skip: u32 = 0u; skip < 128u; skip++) {
|
|
72068
|
+
totalWork += 1u;
|
|
72069
|
+
|
|
72070
|
+
if (tMaxX < tMaxY && tMaxX < tMaxZ) {
|
|
72071
|
+
bx += stepX;
|
|
72072
|
+
tMaxX += tDeltaX;
|
|
72073
|
+
if (bx < cellXMin || bx >= cellXMin + cellBlocks) { break; }
|
|
72074
|
+
} else if (tMaxY < tMaxZ) {
|
|
72075
|
+
by += stepY;
|
|
72076
|
+
tMaxY += tDeltaY;
|
|
72077
|
+
if (by < cellYMin || by >= cellYMin + cellBlocks) { break; }
|
|
72078
|
+
} else {
|
|
72079
|
+
bz += stepZ;
|
|
72080
|
+
tMaxZ += tDeltaZ;
|
|
72081
|
+
if (bz < cellZMin || bz >= cellZMin + cellBlocks) { break; }
|
|
72082
|
+
}
|
|
72083
|
+
}
|
|
72084
|
+
} else {
|
|
72085
|
+
if (blockResult != 0u) {
|
|
72086
|
+
let blockOrigin = gridMin + vec3f(f32(bx), f32(by), f32(bz)) * blockRes;
|
|
72087
|
+
|
|
72088
|
+
let blockMax = blockOrigin + vec3f(blockRes);
|
|
72089
|
+
let bHit = intersectAABB(ro, invDir, blockOrigin, blockMax);
|
|
72090
|
+
let tBlockEntry = max(bHit.x, 0.0);
|
|
72091
|
+
|
|
72092
|
+
// Voxel-level DDA within the block
|
|
72093
|
+
let entryVoxWorld = ro + rd * (tBlockEntry + 0.0001);
|
|
72094
|
+
let entryLocal = (entryVoxWorld - blockOrigin) / voxelRes;
|
|
72095
|
+
var vx = clamp(i32(floor(entryLocal.x)), 0, leafSz - 1);
|
|
72096
|
+
var vy = clamp(i32(floor(entryLocal.y)), 0, leafSz - 1);
|
|
72097
|
+
var vz = clamp(i32(floor(entryLocal.z)), 0, leafSz - 1);
|
|
72098
|
+
|
|
72099
|
+
let vTDeltaX = abs(voxelRes / rd.x);
|
|
72100
|
+
let vTDeltaY = abs(voxelRes / rd.y);
|
|
72101
|
+
let vTDeltaZ = abs(voxelRes / rd.z);
|
|
72102
|
+
|
|
72103
|
+
let voxOrigin = blockOrigin + vec3f(f32(vx), f32(vy), f32(vz)) * voxelRes;
|
|
72104
|
+
let vNextX = select(voxOrigin.x, voxOrigin.x + voxelRes, rd.x >= 0.0);
|
|
72105
|
+
let vNextY = select(voxOrigin.y, voxOrigin.y + voxelRes, rd.y >= 0.0);
|
|
72106
|
+
let vNextZ = select(voxOrigin.z, voxOrigin.z + voxelRes, rd.z >= 0.0);
|
|
72107
|
+
|
|
72108
|
+
var vTMaxX = (vNextX - ro.x) / rd.x;
|
|
72109
|
+
var vTMaxY = (vNextY - ro.y) / rd.y;
|
|
72110
|
+
var vTMaxZ = (vNextZ - ro.z) / rd.z;
|
|
72111
|
+
|
|
72112
|
+
var maskLo: u32 = 0u;
|
|
72113
|
+
var maskHi: u32 = 0u;
|
|
72114
|
+
if (blockResult > 1u) {
|
|
72115
|
+
let leafIdx = blockResult - 2u;
|
|
72116
|
+
maskLo = leafData[leafIdx * 2u];
|
|
72117
|
+
maskHi = leafData[leafIdx * 2u + 1u];
|
|
72118
|
+
}
|
|
72119
|
+
|
|
72120
|
+
for (var vStep: u32 = 0u; vStep < 12u; vStep++) {
|
|
72121
|
+
totalWork += 1u;
|
|
72122
|
+
|
|
72123
|
+
var isSolid = false;
|
|
72124
|
+
|
|
72125
|
+
if (blockResult == 1u) {
|
|
72126
|
+
isSolid = true;
|
|
72127
|
+
} else {
|
|
72128
|
+
let bitIndex = u32(vz) * 16u + u32(vy) * 4u + u32(vx);
|
|
72129
|
+
isSolid = select(
|
|
72130
|
+
(maskHi & (1u << (bitIndex - 32u))) != 0u,
|
|
72131
|
+
(maskLo & (1u << bitIndex)) != 0u,
|
|
72132
|
+
bitIndex < 32u
|
|
72133
|
+
);
|
|
72134
|
+
}
|
|
72135
|
+
|
|
72136
|
+
if (isSolid) {
|
|
72137
|
+
if (uniforms.displayMode == 0u) {
|
|
72138
|
+
let voxMin = blockOrigin + vec3f(f32(vx), f32(vy), f32(vz)) * voxelRes;
|
|
72139
|
+
let vHit = intersectAABB(ro, invDir, voxMin, voxMin + vec3f(voxelRes));
|
|
72140
|
+
let hitPos = ro + rd * max(vHit.x, 0.0);
|
|
72141
|
+
let result = shadeVoxelHit(hitPos, voxMin, voxelRes, ro, blockResult == 1u);
|
|
72142
|
+
textureStore(outputTexture, vec2i(px, py), result);
|
|
72143
|
+
} else {
|
|
72144
|
+
let effort = f32(totalWork) / 256.0;
|
|
72145
|
+
let color = heatmap(effort);
|
|
72146
|
+
textureStore(outputTexture, vec2i(px, py), vec4f(color, 1.0));
|
|
72147
|
+
}
|
|
72148
|
+
return;
|
|
72149
|
+
}
|
|
72150
|
+
|
|
72151
|
+
// Advance voxel DDA
|
|
72152
|
+
if (vTMaxX < vTMaxY && vTMaxX < vTMaxZ) {
|
|
72153
|
+
vx += stepX;
|
|
72154
|
+
vTMaxX += vTDeltaX;
|
|
72155
|
+
if (vx < 0 || vx >= leafSz) { break; }
|
|
72156
|
+
} else if (vTMaxY < vTMaxZ) {
|
|
72157
|
+
vy += stepY;
|
|
72158
|
+
vTMaxY += vTDeltaY;
|
|
72159
|
+
if (vy < 0 || vy >= leafSz) { break; }
|
|
72160
|
+
} else {
|
|
72161
|
+
vz += stepZ;
|
|
72162
|
+
vTMaxZ += vTDeltaZ;
|
|
72163
|
+
if (vz < 0 || vz >= leafSz) { break; }
|
|
72164
|
+
}
|
|
72165
|
+
}
|
|
72166
|
+
}
|
|
72167
|
+
|
|
72168
|
+
// Advance block DDA
|
|
72169
|
+
if (tMaxX < tMaxY && tMaxX < tMaxZ) {
|
|
72170
|
+
bx += stepX;
|
|
72171
|
+
tMaxX += tDeltaX;
|
|
72172
|
+
} else if (tMaxY < tMaxZ) {
|
|
72173
|
+
by += stepY;
|
|
72174
|
+
tMaxY += tDeltaY;
|
|
72175
|
+
} else {
|
|
72176
|
+
bz += stepZ;
|
|
72177
|
+
tMaxZ += tDeltaZ;
|
|
72178
|
+
}
|
|
72179
|
+
}
|
|
72180
|
+
|
|
72181
|
+
if (bx < 0 || by < 0 || bz < 0 ||
|
|
72182
|
+
bx >= numBlocksX || by >= numBlocksY || bz >= numBlocksZ) {
|
|
72183
|
+
break;
|
|
72184
|
+
}
|
|
72185
|
+
}
|
|
72186
|
+
|
|
72187
|
+
if (uniforms.displayMode == 0u) {
|
|
72188
|
+
textureStore(outputTexture, vec2i(px, py), vec4f(0.0));
|
|
72189
|
+
} else {
|
|
72190
|
+
let effort = f32(totalWork) / 256.0;
|
|
72191
|
+
let color = heatmap(effort);
|
|
72192
|
+
textureStore(outputTexture, vec2i(px, py), vec4f(color, 1.0));
|
|
72193
|
+
}
|
|
72194
|
+
}
|
|
72195
|
+
`;
|
|
72196
|
+
// ---------------------------------------------------------------------------
|
|
72197
|
+
// VoxelDebugOverlay class
|
|
72198
|
+
// ---------------------------------------------------------------------------
|
|
72199
|
+
class VoxelDebugOverlay {
|
|
72200
|
+
app;
|
|
72201
|
+
camera;
|
|
72202
|
+
compute;
|
|
72203
|
+
storageTexture;
|
|
72204
|
+
overlayMaterial;
|
|
72205
|
+
nodesBuffer;
|
|
72206
|
+
leafDataBuffer;
|
|
72207
|
+
collider;
|
|
72208
|
+
currentWidth = 0;
|
|
72209
|
+
currentHeight = 0;
|
|
72210
|
+
invVP = new Mat4();
|
|
72211
|
+
vpTemp = new Mat4();
|
|
72212
|
+
/** Whether the overlay is currently rendering. */
|
|
72213
|
+
enabled = false;
|
|
72214
|
+
/** Display mode: 'overlay' for wireframe debug, 'heatmap' for effort visualization. */
|
|
72215
|
+
mode = 'overlay';
|
|
72216
|
+
constructor(app, collider, camera) {
|
|
72217
|
+
this.app = app;
|
|
72218
|
+
this.camera = camera;
|
|
72219
|
+
this.collider = collider;
|
|
72220
|
+
const device = app.graphicsDevice;
|
|
72221
|
+
// Upload SVO node array as a read-only storage buffer
|
|
72222
|
+
const nodesData = collider.nodes;
|
|
72223
|
+
const nodesByteSize = Math.max(nodesData.byteLength, 4);
|
|
72224
|
+
this.nodesBuffer = new StorageBuffer(device, nodesByteSize, BUFFERUSAGE_COPY_DST);
|
|
72225
|
+
if (nodesData.byteLength > 0) {
|
|
72226
|
+
this.nodesBuffer.write(0, nodesData, 0, nodesData.length);
|
|
72227
|
+
}
|
|
72228
|
+
// Upload leaf data as a read-only storage buffer
|
|
72229
|
+
const leafDataArr = collider.leafData;
|
|
72230
|
+
const leafByteSize = Math.max(leafDataArr.byteLength, 4);
|
|
72231
|
+
this.leafDataBuffer = new StorageBuffer(device, leafByteSize, BUFFERUSAGE_COPY_DST);
|
|
72232
|
+
if (leafDataArr.byteLength > 0) {
|
|
72233
|
+
this.leafDataBuffer.write(0, leafDataArr, 0, leafDataArr.length);
|
|
72234
|
+
}
|
|
72235
|
+
// Create the initial storage texture (will be resized on first update)
|
|
72236
|
+
this.currentWidth = Math.max(device.width, 1);
|
|
72237
|
+
this.currentHeight = Math.max(device.height, 1);
|
|
72238
|
+
this.storageTexture = this.createStorageTexture(this.currentWidth, this.currentHeight);
|
|
72239
|
+
// Create compute shader
|
|
72240
|
+
const shaderDefinition = {
|
|
72241
|
+
name: 'VoxelDebugOverlay',
|
|
72242
|
+
shaderLanguage: SHADERLANGUAGE_WGSL,
|
|
72243
|
+
cshader: voxelOverlayWGSL,
|
|
72244
|
+
computeUniformBufferFormats: {
|
|
72245
|
+
uniforms: new UniformBufferFormat(device, [
|
|
72246
|
+
new UniformFormat('invVP', UNIFORMTYPE_MAT4),
|
|
72247
|
+
new UniformFormat('screenWidth', UNIFORMTYPE_UINT),
|
|
72248
|
+
new UniformFormat('screenHeight', UNIFORMTYPE_UINT),
|
|
72249
|
+
new UniformFormat('gridMinX', UNIFORMTYPE_FLOAT),
|
|
72250
|
+
new UniformFormat('gridMinY', UNIFORMTYPE_FLOAT),
|
|
72251
|
+
new UniformFormat('gridMinZ', UNIFORMTYPE_FLOAT),
|
|
72252
|
+
new UniformFormat('voxelRes', UNIFORMTYPE_FLOAT),
|
|
72253
|
+
new UniformFormat('numVoxelsX', UNIFORMTYPE_UINT),
|
|
72254
|
+
new UniformFormat('numVoxelsY', UNIFORMTYPE_UINT),
|
|
72255
|
+
new UniformFormat('numVoxelsZ', UNIFORMTYPE_UINT),
|
|
72256
|
+
new UniformFormat('leafSize', UNIFORMTYPE_UINT),
|
|
72257
|
+
new UniformFormat('treeDepth', UNIFORMTYPE_UINT),
|
|
72258
|
+
new UniformFormat('projScaleY', UNIFORMTYPE_FLOAT),
|
|
72259
|
+
new UniformFormat('displayMode', UNIFORMTYPE_UINT),
|
|
72260
|
+
new UniformFormat('pad2', UNIFORMTYPE_UINT)
|
|
72261
|
+
])
|
|
72262
|
+
},
|
|
72263
|
+
computeBindGroupFormat: new BindGroupFormat(device, [
|
|
72264
|
+
new BindUniformBufferFormat('uniforms', SHADERSTAGE_COMPUTE),
|
|
72265
|
+
new BindStorageBufferFormat('nodes', SHADERSTAGE_COMPUTE, true),
|
|
72266
|
+
new BindStorageBufferFormat('leafData', SHADERSTAGE_COMPUTE, true),
|
|
72267
|
+
new BindStorageTextureFormat('outputTexture', PIXELFORMAT_RGBA8, TEXTUREDIMENSION_2D)
|
|
72268
|
+
])
|
|
72269
|
+
};
|
|
72270
|
+
const shader = new Shader(device, shaderDefinition);
|
|
72271
|
+
// Create compute instance
|
|
72272
|
+
this.compute = new Compute(device, shader, 'VoxelDebugOverlay');
|
|
72273
|
+
// Create overlay material with premultiplied alpha blending and a custom
|
|
72274
|
+
// fragment shader that preserves the texture's alpha channel (the built-in
|
|
72275
|
+
// getTextureShaderDesc hardcodes alpha = 1.0, which prevents blending).
|
|
72276
|
+
this.overlayMaterial = new ShaderMaterial();
|
|
72277
|
+
this.overlayMaterial.cull = CULLFACE_NONE;
|
|
72278
|
+
this.overlayMaterial.blendType = BLEND_PREMULTIPLIED;
|
|
72279
|
+
this.overlayMaterial.depthTest = false;
|
|
72280
|
+
this.overlayMaterial.depthWrite = false;
|
|
72281
|
+
this.overlayMaterial.setParameter('colorMap', this.storageTexture);
|
|
72282
|
+
this.overlayMaterial.shaderDesc = {
|
|
72283
|
+
uniqueName: 'VoxelOverlayComposite',
|
|
72284
|
+
vertexGLSL: /* glsl */ `
|
|
72285
|
+
attribute vec2 vertex_position;
|
|
72286
|
+
uniform mat4 matrix_model;
|
|
72287
|
+
varying vec2 uv0;
|
|
72288
|
+
void main(void) {
|
|
72289
|
+
gl_Position = matrix_model * vec4(vertex_position, 0, 1);
|
|
72290
|
+
uv0 = vertex_position.xy + 0.5;
|
|
72291
|
+
}
|
|
72292
|
+
`,
|
|
72293
|
+
vertexWGSL: /* wgsl */ `
|
|
72294
|
+
attribute vertex_position: vec2f;
|
|
72295
|
+
uniform matrix_model: mat4x4f;
|
|
72296
|
+
varying uv0: vec2f;
|
|
72297
|
+
@vertex fn vertexMain(input: VertexInput) -> VertexOutput {
|
|
72298
|
+
var output: VertexOutput;
|
|
72299
|
+
output.position = uniform.matrix_model * vec4f(input.vertex_position, 0.0, 1.0);
|
|
72300
|
+
output.uv0 = input.vertex_position.xy + vec2f(0.5);
|
|
72301
|
+
return output;
|
|
72302
|
+
}
|
|
72303
|
+
`,
|
|
72304
|
+
fragmentGLSL: /* glsl */ `
|
|
72305
|
+
varying vec2 uv0;
|
|
72306
|
+
uniform sampler2D colorMap;
|
|
72307
|
+
void main(void) {
|
|
72308
|
+
gl_FragColor = texture2D(colorMap, uv0);
|
|
72309
|
+
}
|
|
72310
|
+
`,
|
|
72311
|
+
fragmentWGSL: /* wgsl */ `
|
|
72312
|
+
varying uv0: vec2f;
|
|
72313
|
+
var colorMap: texture_2d<f32>;
|
|
72314
|
+
var colorMapSampler: sampler;
|
|
72315
|
+
@fragment fn fragmentMain(input: FragmentInput) -> FragmentOutput {
|
|
72316
|
+
var output: FragmentOutput;
|
|
72317
|
+
output.color = textureSample(colorMap, colorMapSampler, input.uv0);
|
|
72318
|
+
return output;
|
|
72319
|
+
}
|
|
72320
|
+
`,
|
|
72321
|
+
attributes: { vertex_position: SEMANTIC_POSITION }
|
|
72322
|
+
};
|
|
72323
|
+
this.overlayMaterial.update();
|
|
72324
|
+
}
|
|
72325
|
+
createStorageTexture(width, height) {
|
|
72326
|
+
return new Texture(this.app.graphicsDevice, {
|
|
72327
|
+
name: 'VoxelOverlay-Storage',
|
|
72328
|
+
width,
|
|
72329
|
+
height,
|
|
72330
|
+
format: PIXELFORMAT_RGBA8,
|
|
72331
|
+
mipmaps: false,
|
|
72332
|
+
addressU: 3, // ADDRESS_CLAMP_TO_EDGE
|
|
72333
|
+
addressV: 3, // ADDRESS_CLAMP_TO_EDGE
|
|
72334
|
+
storage: true
|
|
72335
|
+
});
|
|
72336
|
+
}
|
|
72337
|
+
update() {
|
|
72338
|
+
if (!this.enabled)
|
|
72339
|
+
return;
|
|
72340
|
+
const { app, camera, compute, collider } = this;
|
|
72341
|
+
const device = app.graphicsDevice;
|
|
72342
|
+
const width = device.width;
|
|
72343
|
+
const height = device.height;
|
|
72344
|
+
if (width <= 0 || height <= 0)
|
|
72345
|
+
return;
|
|
72346
|
+
// Resize storage texture if screen dimensions changed
|
|
72347
|
+
if (width !== this.currentWidth || height !== this.currentHeight) {
|
|
72348
|
+
this.storageTexture.destroy();
|
|
72349
|
+
this.currentWidth = width;
|
|
72350
|
+
this.currentHeight = height;
|
|
72351
|
+
this.storageTexture = this.createStorageTexture(width, height);
|
|
72352
|
+
// Update the overlay material to reference the new texture
|
|
72353
|
+
this.overlayMaterial.setParameter('colorMap', this.storageTexture);
|
|
72354
|
+
this.overlayMaterial.update();
|
|
72355
|
+
}
|
|
72356
|
+
// Compute inverse view-projection matrix
|
|
72357
|
+
const cam = camera.camera;
|
|
72358
|
+
this.vpTemp.mul2(cam.projectionMatrix, cam.viewMatrix);
|
|
72359
|
+
this.invVP.copy(this.vpTemp).invert();
|
|
72360
|
+
// Set compute uniforms
|
|
72361
|
+
compute.setParameter('invVP', this.invVP.data);
|
|
72362
|
+
compute.setParameter('screenWidth', width);
|
|
72363
|
+
compute.setParameter('screenHeight', height);
|
|
72364
|
+
compute.setParameter('gridMinX', collider.gridMinX);
|
|
72365
|
+
compute.setParameter('gridMinY', collider.gridMinY);
|
|
72366
|
+
compute.setParameter('gridMinZ', collider.gridMinZ);
|
|
72367
|
+
compute.setParameter('voxelRes', collider.voxelResolution);
|
|
72368
|
+
compute.setParameter('numVoxelsX', collider.numVoxelsX);
|
|
72369
|
+
compute.setParameter('numVoxelsY', collider.numVoxelsY);
|
|
72370
|
+
compute.setParameter('numVoxelsZ', collider.numVoxelsZ);
|
|
72371
|
+
compute.setParameter('leafSize', collider.leafSize);
|
|
72372
|
+
compute.setParameter('treeDepth', collider.treeDepth);
|
|
72373
|
+
compute.setParameter('projScaleY', cam.projectionMatrix.data[5]);
|
|
72374
|
+
compute.setParameter('displayMode', this.mode === 'heatmap' ? 1 : 0);
|
|
72375
|
+
compute.setParameter('pad2', 0);
|
|
72376
|
+
// Set storage buffers and output texture
|
|
72377
|
+
compute.setParameter('nodes', this.nodesBuffer);
|
|
72378
|
+
compute.setParameter('leafData', this.leafDataBuffer);
|
|
72379
|
+
compute.setParameter('outputTexture', this.storageTexture);
|
|
72380
|
+
// Dispatch compute shader
|
|
72381
|
+
const workgroupsX = Math.ceil(width / 8);
|
|
72382
|
+
const workgroupsY = Math.ceil(height / 8);
|
|
72383
|
+
compute.setupDispatch(workgroupsX, workgroupsY, 1);
|
|
72384
|
+
device.computeDispatch([compute], 'VoxelDebugOverlay');
|
|
72385
|
+
// Composite overlay on top of the scene
|
|
72386
|
+
app.drawTexture(0, 0, 2, 2, null, this.overlayMaterial);
|
|
72387
|
+
}
|
|
72388
|
+
destroy() {
|
|
72389
|
+
this.nodesBuffer?.destroy();
|
|
72390
|
+
this.leafDataBuffer?.destroy();
|
|
72391
|
+
this.storageTexture?.destroy();
|
|
72392
|
+
}
|
|
72393
|
+
}
|
|
72394
|
+
|
|
71496
72395
|
const gammaChunkGlsl = `
|
|
71497
72396
|
vec3 prepareOutputFromGamma(vec3 gammaColor) {
|
|
71498
72397
|
return gammaColor;
|
|
@@ -71573,8 +72472,9 @@ class Viewer {
|
|
|
71573
72472
|
cameraManager;
|
|
71574
72473
|
annotations;
|
|
71575
72474
|
forceRenderNextFrame = false;
|
|
72475
|
+
voxelOverlay = null;
|
|
71576
72476
|
origChunks;
|
|
71577
|
-
constructor(global, gsplatLoad, skyboxLoad) {
|
|
72477
|
+
constructor(global, gsplatLoad, skyboxLoad, voxelLoad) {
|
|
71578
72478
|
this.global = global;
|
|
71579
72479
|
const { app, settings, config, events, state, camera } = global;
|
|
71580
72480
|
const { graphicsDevice } = app;
|
|
@@ -71688,14 +72588,19 @@ class Viewer {
|
|
|
71688
72588
|
applyCamera(this.cameraManager.camera);
|
|
71689
72589
|
}
|
|
71690
72590
|
});
|
|
72591
|
+
// Render voxel debug overlay
|
|
72592
|
+
app.on('prerender', () => {
|
|
72593
|
+
this.voxelOverlay?.update();
|
|
72594
|
+
});
|
|
71691
72595
|
// update state on first frame
|
|
71692
72596
|
events.on('firstFrame', () => {
|
|
71693
72597
|
state.loaded = true;
|
|
71694
72598
|
state.animationPaused = !!config.noanim;
|
|
71695
72599
|
});
|
|
71696
72600
|
// wait for the model to load
|
|
71697
|
-
Promise.all([gsplatLoad, skyboxLoad]).then((results) => {
|
|
72601
|
+
Promise.all([gsplatLoad, skyboxLoad, voxelLoad]).then((results) => {
|
|
71698
72602
|
const gsplat = results[0].gsplat;
|
|
72603
|
+
const collider = results[2];
|
|
71699
72604
|
// get scene bounding box
|
|
71700
72605
|
const gsplatBbox = gsplat.customAabb;
|
|
71701
72606
|
if (gsplatBbox) {
|
|
@@ -71705,7 +72610,18 @@ class Viewer {
|
|
|
71705
72610
|
this.annotations = new Annotations(global, this.cameraFrame != null);
|
|
71706
72611
|
}
|
|
71707
72612
|
this.inputController = new InputController(global);
|
|
71708
|
-
|
|
72613
|
+
state.hasCollision = !!collider;
|
|
72614
|
+
// Create voxel debug overlay in WebGPU only
|
|
72615
|
+
if (collider && config.webgpu) {
|
|
72616
|
+
this.voxelOverlay = new VoxelDebugOverlay(app, collider, camera);
|
|
72617
|
+
this.voxelOverlay.mode = config.heatmap ? 'heatmap' : 'overlay';
|
|
72618
|
+
state.hasVoxelOverlay = true;
|
|
72619
|
+
events.on('voxelOverlayEnabled:changed', (value) => {
|
|
72620
|
+
this.voxelOverlay.enabled = value;
|
|
72621
|
+
app.renderNextFrame = true;
|
|
72622
|
+
});
|
|
72623
|
+
}
|
|
72624
|
+
this.cameraManager = new CameraManager(global, sceneBound, collider);
|
|
71709
72625
|
applyCamera(this.cameraManager.camera);
|
|
71710
72626
|
const { instance } = gsplat;
|
|
71711
72627
|
if (instance) {
|
|
@@ -71834,6 +72750,678 @@ class Viewer {
|
|
|
71834
72750
|
}
|
|
71835
72751
|
}
|
|
71836
72752
|
|
|
72753
|
+
/**
|
|
72754
|
+
* Solid leaf node marker: childMask = 0xFF, baseOffset = 0.
|
|
72755
|
+
* Unambiguous because BFS layout guarantees children always come after their parent,
|
|
72756
|
+
* so baseOffset = 0 is never valid for an interior node.
|
|
72757
|
+
*/
|
|
72758
|
+
const SOLID_LEAF_MARKER = 0xFF000000 >>> 0;
|
|
72759
|
+
/** Minimum penetration depth to report a collision (avoids floating-point noise at corners) */
|
|
72760
|
+
const PENETRATION_EPSILON = 1e-4;
|
|
72761
|
+
/**
|
|
72762
|
+
* Count the number of set bits in a 32-bit integer.
|
|
72763
|
+
*
|
|
72764
|
+
* @param n - 32-bit integer.
|
|
72765
|
+
* @returns Number of bits set to 1.
|
|
72766
|
+
*/
|
|
72767
|
+
function popcount(n) {
|
|
72768
|
+
n >>>= 0;
|
|
72769
|
+
n -= ((n >>> 1) & 0x55555555);
|
|
72770
|
+
n = (n & 0x33333333) + ((n >>> 2) & 0x33333333);
|
|
72771
|
+
return (((n + (n >>> 4)) & 0x0F0F0F0F) * 0x01010101) >>> 24;
|
|
72772
|
+
}
|
|
72773
|
+
/**
|
|
72774
|
+
* Runtime sparse voxel octree collider.
|
|
72775
|
+
*
|
|
72776
|
+
* Loads the two-file format (.voxel.json + .voxel.bin) produced by
|
|
72777
|
+
* splat-transform's writeVoxel and provides point and sphere collision queries.
|
|
72778
|
+
*/
|
|
72779
|
+
class VoxelCollider {
|
|
72780
|
+
/** Grid-aligned bounds (min xyz) */
|
|
72781
|
+
_gridMinX;
|
|
72782
|
+
_gridMinY;
|
|
72783
|
+
_gridMinZ;
|
|
72784
|
+
/** Number of voxels along each axis */
|
|
72785
|
+
_numVoxelsX;
|
|
72786
|
+
_numVoxelsY;
|
|
72787
|
+
_numVoxelsZ;
|
|
72788
|
+
/** Size of each voxel in world units */
|
|
72789
|
+
_voxelResolution;
|
|
72790
|
+
/** Voxels per leaf dimension (always 4) */
|
|
72791
|
+
_leafSize;
|
|
72792
|
+
/** Maximum tree depth (number of octree levels above the leaf level) */
|
|
72793
|
+
_treeDepth;
|
|
72794
|
+
/** Flat Laine-Karras node array */
|
|
72795
|
+
_nodes;
|
|
72796
|
+
/** Leaf voxel masks: pairs of (lo, hi) Uint32 per mixed leaf */
|
|
72797
|
+
_leafData;
|
|
72798
|
+
/** Pre-allocated scratch push-out vector to avoid per-frame allocations */
|
|
72799
|
+
_push = { x: 0, y: 0, z: 0 };
|
|
72800
|
+
/** Pre-allocated constraint normals for iterative corner resolution (max 3 walls) */
|
|
72801
|
+
_constraintNormals = [
|
|
72802
|
+
{ x: 0, y: 0, z: 0 },
|
|
72803
|
+
{ x: 0, y: 0, z: 0 },
|
|
72804
|
+
{ x: 0, y: 0, z: 0 }
|
|
72805
|
+
];
|
|
72806
|
+
constructor(metadata, nodes, leafData) {
|
|
72807
|
+
this._gridMinX = metadata.gridBounds.min[0];
|
|
72808
|
+
this._gridMinY = metadata.gridBounds.min[1];
|
|
72809
|
+
this._gridMinZ = metadata.gridBounds.min[2];
|
|
72810
|
+
const res = metadata.voxelResolution;
|
|
72811
|
+
this._numVoxelsX = Math.round((metadata.gridBounds.max[0] - metadata.gridBounds.min[0]) / res);
|
|
72812
|
+
this._numVoxelsY = Math.round((metadata.gridBounds.max[1] - metadata.gridBounds.min[1]) / res);
|
|
72813
|
+
this._numVoxelsZ = Math.round((metadata.gridBounds.max[2] - metadata.gridBounds.min[2]) / res);
|
|
72814
|
+
this._voxelResolution = res;
|
|
72815
|
+
this._leafSize = metadata.leafSize;
|
|
72816
|
+
this._treeDepth = metadata.treeDepth;
|
|
72817
|
+
this._nodes = nodes;
|
|
72818
|
+
this._leafData = leafData;
|
|
72819
|
+
}
|
|
72820
|
+
/**
|
|
72821
|
+
* Grid-aligned bounds minimum X in world units.
|
|
72822
|
+
*
|
|
72823
|
+
* @returns {number} The minimum X coordinate.
|
|
72824
|
+
*/
|
|
72825
|
+
get gridMinX() {
|
|
72826
|
+
return this._gridMinX;
|
|
72827
|
+
}
|
|
72828
|
+
/**
|
|
72829
|
+
* Grid-aligned bounds minimum Y in world units.
|
|
72830
|
+
*
|
|
72831
|
+
* @returns {number} The minimum Y coordinate.
|
|
72832
|
+
*/
|
|
72833
|
+
get gridMinY() {
|
|
72834
|
+
return this._gridMinY;
|
|
72835
|
+
}
|
|
72836
|
+
/**
|
|
72837
|
+
* Grid-aligned bounds minimum Z in world units.
|
|
72838
|
+
*
|
|
72839
|
+
* @returns {number} The minimum Z coordinate.
|
|
72840
|
+
*/
|
|
72841
|
+
get gridMinZ() {
|
|
72842
|
+
return this._gridMinZ;
|
|
72843
|
+
}
|
|
72844
|
+
/**
|
|
72845
|
+
* Number of voxels along the X axis.
|
|
72846
|
+
*
|
|
72847
|
+
* @returns {number} The voxel count on X.
|
|
72848
|
+
*/
|
|
72849
|
+
get numVoxelsX() {
|
|
72850
|
+
return this._numVoxelsX;
|
|
72851
|
+
}
|
|
72852
|
+
/**
|
|
72853
|
+
* Number of voxels along the Y axis.
|
|
72854
|
+
*
|
|
72855
|
+
* @returns {number} The voxel count on Y.
|
|
72856
|
+
*/
|
|
72857
|
+
get numVoxelsY() {
|
|
72858
|
+
return this._numVoxelsY;
|
|
72859
|
+
}
|
|
72860
|
+
/**
|
|
72861
|
+
* Number of voxels along the Z axis.
|
|
72862
|
+
*
|
|
72863
|
+
* @returns {number} The voxel count on Z.
|
|
72864
|
+
*/
|
|
72865
|
+
get numVoxelsZ() {
|
|
72866
|
+
return this._numVoxelsZ;
|
|
72867
|
+
}
|
|
72868
|
+
/**
|
|
72869
|
+
* Size of each voxel in world units.
|
|
72870
|
+
*
|
|
72871
|
+
* @returns {number} The voxel resolution.
|
|
72872
|
+
*/
|
|
72873
|
+
get voxelResolution() {
|
|
72874
|
+
return this._voxelResolution;
|
|
72875
|
+
}
|
|
72876
|
+
/**
|
|
72877
|
+
* Voxels per leaf dimension (always 4).
|
|
72878
|
+
*
|
|
72879
|
+
* @returns {number} The leaf size.
|
|
72880
|
+
*/
|
|
72881
|
+
get leafSize() {
|
|
72882
|
+
return this._leafSize;
|
|
72883
|
+
}
|
|
72884
|
+
/**
|
|
72885
|
+
* Maximum tree depth (number of octree levels above the leaf level).
|
|
72886
|
+
*
|
|
72887
|
+
* @returns {number} The tree depth.
|
|
72888
|
+
*/
|
|
72889
|
+
get treeDepth() {
|
|
72890
|
+
return this._treeDepth;
|
|
72891
|
+
}
|
|
72892
|
+
/**
|
|
72893
|
+
* Flat Laine-Karras node array (read-only access for GPU upload).
|
|
72894
|
+
*
|
|
72895
|
+
* @returns {Uint32Array} The node array.
|
|
72896
|
+
*/
|
|
72897
|
+
get nodes() {
|
|
72898
|
+
return this._nodes;
|
|
72899
|
+
}
|
|
72900
|
+
/**
|
|
72901
|
+
* Leaf voxel masks: pairs of (lo, hi) Uint32 per mixed leaf (read-only access for GPU upload).
|
|
72902
|
+
*
|
|
72903
|
+
* @returns {Uint32Array} The leaf data array.
|
|
72904
|
+
*/
|
|
72905
|
+
get leafData() {
|
|
72906
|
+
return this._leafData;
|
|
72907
|
+
}
|
|
72908
|
+
/**
|
|
72909
|
+
* Load a VoxelCollider from a .voxel.json URL.
|
|
72910
|
+
* The corresponding .voxel.bin is inferred by replacing the extension.
|
|
72911
|
+
*
|
|
72912
|
+
* @param jsonUrl - URL to the .voxel.json metadata file.
|
|
72913
|
+
* @returns A promise resolving to a VoxelCollider instance.
|
|
72914
|
+
*/
|
|
72915
|
+
static async load(jsonUrl) {
|
|
72916
|
+
// Fetch metadata
|
|
72917
|
+
const metaResponse = await fetch(jsonUrl);
|
|
72918
|
+
if (!metaResponse.ok) {
|
|
72919
|
+
throw new Error(`Failed to fetch voxel metadata: ${metaResponse.statusText}`);
|
|
72920
|
+
}
|
|
72921
|
+
const metadata = await metaResponse.json();
|
|
72922
|
+
// Fetch binary data
|
|
72923
|
+
const binUrl = jsonUrl.replace('.voxel.json', '.voxel.bin');
|
|
72924
|
+
const binResponse = await fetch(binUrl);
|
|
72925
|
+
if (!binResponse.ok) {
|
|
72926
|
+
throw new Error(`Failed to fetch voxel binary: ${binResponse.statusText}`);
|
|
72927
|
+
}
|
|
72928
|
+
const buffer = await binResponse.arrayBuffer();
|
|
72929
|
+
const view = new Uint32Array(buffer);
|
|
72930
|
+
const nodes = view.slice(0, metadata.nodeCount);
|
|
72931
|
+
const leafData = view.slice(metadata.nodeCount, metadata.nodeCount + metadata.leafDataCount);
|
|
72932
|
+
return new VoxelCollider(metadata, nodes, leafData);
|
|
72933
|
+
}
|
|
72934
|
+
/**
|
|
72935
|
+
* Query whether a world-space point lies inside a solid voxel.
|
|
72936
|
+
*
|
|
72937
|
+
* @param x - World X coordinate.
|
|
72938
|
+
* @param y - World Y coordinate.
|
|
72939
|
+
* @param z - World Z coordinate.
|
|
72940
|
+
* @returns True if the point is inside a solid voxel.
|
|
72941
|
+
*/
|
|
72942
|
+
queryPoint(x, y, z) {
|
|
72943
|
+
const ix = Math.floor((x - this.gridMinX) / this.voxelResolution);
|
|
72944
|
+
const iy = Math.floor((y - this.gridMinY) / this.voxelResolution);
|
|
72945
|
+
const iz = Math.floor((z - this.gridMinZ) / this.voxelResolution);
|
|
72946
|
+
return this.isVoxelSolid(ix, iy, iz);
|
|
72947
|
+
}
|
|
72948
|
+
/**
|
|
72949
|
+
* Query a sphere against the voxel grid and write a push-out vector to resolve penetration.
|
|
72950
|
+
* Uses iterative single-voxel resolution: each iteration finds the deepest penetrating voxel,
|
|
72951
|
+
* resolves it, then re-checks. This avoids over-push from summing multiple voxels and
|
|
72952
|
+
* naturally handles corners (2 iterations) and flat walls (1 iteration).
|
|
72953
|
+
*
|
|
72954
|
+
* @param cx - Sphere center X in world units.
|
|
72955
|
+
* @param cy - Sphere center Y in world units.
|
|
72956
|
+
* @param cz - Sphere center Z in world units.
|
|
72957
|
+
* @param radius - Sphere radius in world units.
|
|
72958
|
+
* @param out - Object to receive the push-out vector.
|
|
72959
|
+
* @returns True if a collision was detected and out was written.
|
|
72960
|
+
*/
|
|
72961
|
+
querySphere(cx, cy, cz, radius, out) {
|
|
72962
|
+
if (this.nodes.length === 0) {
|
|
72963
|
+
return false;
|
|
72964
|
+
}
|
|
72965
|
+
const maxIterations = 4;
|
|
72966
|
+
let resolvedX = cx;
|
|
72967
|
+
let resolvedY = cy;
|
|
72968
|
+
let resolvedZ = cz;
|
|
72969
|
+
let totalPushX = 0;
|
|
72970
|
+
let totalPushY = 0;
|
|
72971
|
+
let totalPushZ = 0;
|
|
72972
|
+
let hadCollision = false;
|
|
72973
|
+
const push = this._push;
|
|
72974
|
+
// Constraint normals from previous iterations - prevents oscillation at corners
|
|
72975
|
+
// by ensuring subsequent pushes don't undo previous ones
|
|
72976
|
+
const normals = this._constraintNormals;
|
|
72977
|
+
let numNormals = 0;
|
|
72978
|
+
for (let iter = 0; iter < maxIterations; iter++) {
|
|
72979
|
+
if (!this.resolveDeepestPenetration(resolvedX, resolvedY, resolvedZ, radius)) {
|
|
72980
|
+
break;
|
|
72981
|
+
}
|
|
72982
|
+
hadCollision = true;
|
|
72983
|
+
let px = push.x;
|
|
72984
|
+
let py = push.y;
|
|
72985
|
+
let pz = push.z;
|
|
72986
|
+
// Project out components that contradict previous constraint normals
|
|
72987
|
+
for (let i = 0; i < numNormals; i++) {
|
|
72988
|
+
const n = normals[i];
|
|
72989
|
+
const dot = px * n.x + py * n.y + pz * n.z;
|
|
72990
|
+
if (dot < 0) {
|
|
72991
|
+
px -= dot * n.x;
|
|
72992
|
+
py -= dot * n.y;
|
|
72993
|
+
pz -= dot * n.z;
|
|
72994
|
+
}
|
|
72995
|
+
}
|
|
72996
|
+
// Record this push direction as a constraint normal
|
|
72997
|
+
const len = Math.sqrt(push.x * push.x + push.y * push.y + push.z * push.z);
|
|
72998
|
+
if (len > PENETRATION_EPSILON && numNormals < 3) {
|
|
72999
|
+
const invLen = 1.0 / len;
|
|
73000
|
+
const n = normals[numNormals];
|
|
73001
|
+
n.x = push.x * invLen;
|
|
73002
|
+
n.y = push.y * invLen;
|
|
73003
|
+
n.z = push.z * invLen;
|
|
73004
|
+
numNormals++;
|
|
73005
|
+
}
|
|
73006
|
+
resolvedX += px;
|
|
73007
|
+
resolvedY += py;
|
|
73008
|
+
resolvedZ += pz;
|
|
73009
|
+
totalPushX += px;
|
|
73010
|
+
totalPushY += py;
|
|
73011
|
+
totalPushZ += pz;
|
|
73012
|
+
}
|
|
73013
|
+
// Only report collision if the total push is meaningful
|
|
73014
|
+
const totalPushSq = totalPushX * totalPushX + totalPushY * totalPushY + totalPushZ * totalPushZ;
|
|
73015
|
+
const hasSignificantPush = hadCollision && totalPushSq > PENETRATION_EPSILON * PENETRATION_EPSILON;
|
|
73016
|
+
if (hasSignificantPush) {
|
|
73017
|
+
out.x = totalPushX;
|
|
73018
|
+
out.y = totalPushY;
|
|
73019
|
+
out.z = totalPushZ;
|
|
73020
|
+
}
|
|
73021
|
+
return hasSignificantPush;
|
|
73022
|
+
}
|
|
73023
|
+
/**
|
|
73024
|
+
* Query a vertical capsule against the voxel grid and write a push-out vector to resolve
|
|
73025
|
+
* penetration. The capsule is a line segment from (cx, cy - halfHeight, cz) to
|
|
73026
|
+
* (cx, cy + halfHeight, cz) swept by radius. Uses the same iterative deepest-penetration
|
|
73027
|
+
* approach as querySphere.
|
|
73028
|
+
*
|
|
73029
|
+
* @param cx - Capsule center X in world units.
|
|
73030
|
+
* @param cy - Capsule center Y in world units.
|
|
73031
|
+
* @param cz - Capsule center Z in world units.
|
|
73032
|
+
* @param halfHeight - Half-height of the capsule's inner line segment in world units.
|
|
73033
|
+
* @param radius - Capsule radius in world units.
|
|
73034
|
+
* @param out - Object to receive the push-out vector.
|
|
73035
|
+
* @returns True if a collision was detected and out was written.
|
|
73036
|
+
*/
|
|
73037
|
+
queryCapsule(cx, cy, cz, halfHeight, radius, out) {
|
|
73038
|
+
if (this.nodes.length === 0) {
|
|
73039
|
+
return false;
|
|
73040
|
+
}
|
|
73041
|
+
const maxIterations = 4;
|
|
73042
|
+
let resolvedX = cx;
|
|
73043
|
+
let resolvedY = cy;
|
|
73044
|
+
let resolvedZ = cz;
|
|
73045
|
+
let totalPushX = 0;
|
|
73046
|
+
let totalPushY = 0;
|
|
73047
|
+
let totalPushZ = 0;
|
|
73048
|
+
let hadCollision = false;
|
|
73049
|
+
const push = this._push;
|
|
73050
|
+
// Constraint normals from previous iterations - prevents oscillation at corners
|
|
73051
|
+
// by ensuring subsequent pushes don't undo previous ones
|
|
73052
|
+
const normals = this._constraintNormals;
|
|
73053
|
+
let numNormals = 0;
|
|
73054
|
+
for (let iter = 0; iter < maxIterations; iter++) {
|
|
73055
|
+
if (!this.resolveDeepestPenetrationCapsule(resolvedX, resolvedY, resolvedZ, halfHeight, radius)) {
|
|
73056
|
+
break;
|
|
73057
|
+
}
|
|
73058
|
+
hadCollision = true;
|
|
73059
|
+
let px = push.x;
|
|
73060
|
+
let py = push.y;
|
|
73061
|
+
let pz = push.z;
|
|
73062
|
+
// Project out components that contradict previous constraint normals
|
|
73063
|
+
for (let i = 0; i < numNormals; i++) {
|
|
73064
|
+
const n = normals[i];
|
|
73065
|
+
const dot = px * n.x + py * n.y + pz * n.z;
|
|
73066
|
+
if (dot < 0) {
|
|
73067
|
+
px -= dot * n.x;
|
|
73068
|
+
py -= dot * n.y;
|
|
73069
|
+
pz -= dot * n.z;
|
|
73070
|
+
}
|
|
73071
|
+
}
|
|
73072
|
+
// Record this push direction as a constraint normal
|
|
73073
|
+
const len = Math.sqrt(push.x * push.x + push.y * push.y + push.z * push.z);
|
|
73074
|
+
if (len > PENETRATION_EPSILON && numNormals < 3) {
|
|
73075
|
+
const invLen = 1.0 / len;
|
|
73076
|
+
const n = normals[numNormals];
|
|
73077
|
+
n.x = push.x * invLen;
|
|
73078
|
+
n.y = push.y * invLen;
|
|
73079
|
+
n.z = push.z * invLen;
|
|
73080
|
+
numNormals++;
|
|
73081
|
+
}
|
|
73082
|
+
resolvedX += px;
|
|
73083
|
+
resolvedY += py;
|
|
73084
|
+
resolvedZ += pz;
|
|
73085
|
+
totalPushX += px;
|
|
73086
|
+
totalPushY += py;
|
|
73087
|
+
totalPushZ += pz;
|
|
73088
|
+
}
|
|
73089
|
+
// Only report collision if the total push is meaningful
|
|
73090
|
+
const totalPushSq = totalPushX * totalPushX + totalPushY * totalPushY + totalPushZ * totalPushZ;
|
|
73091
|
+
const hasSignificantPush = hadCollision && totalPushSq > PENETRATION_EPSILON * PENETRATION_EPSILON;
|
|
73092
|
+
if (hasSignificantPush) {
|
|
73093
|
+
out.x = totalPushX;
|
|
73094
|
+
out.y = totalPushY;
|
|
73095
|
+
out.z = totalPushZ;
|
|
73096
|
+
}
|
|
73097
|
+
return hasSignificantPush;
|
|
73098
|
+
}
|
|
73099
|
+
/**
|
|
73100
|
+
* Find the single deepest penetrating voxel for the given sphere.
|
|
73101
|
+
* Writes the push-out vector into this._push.
|
|
73102
|
+
*
|
|
73103
|
+
* @param cx - Sphere center X.
|
|
73104
|
+
* @param cy - Sphere center Y.
|
|
73105
|
+
* @param cz - Sphere center Z.
|
|
73106
|
+
* @param radius - Sphere radius.
|
|
73107
|
+
* @returns True if a penetrating voxel was found.
|
|
73108
|
+
*/
|
|
73109
|
+
resolveDeepestPenetration(cx, cy, cz, radius) {
|
|
73110
|
+
const { voxelResolution, gridMinX, gridMinY, gridMinZ } = this;
|
|
73111
|
+
const radiusSq = radius * radius;
|
|
73112
|
+
// Compute bounding box of the sphere in voxel indices
|
|
73113
|
+
const ixMin = Math.floor((cx - radius - gridMinX) / voxelResolution);
|
|
73114
|
+
const iyMin = Math.floor((cy - radius - gridMinY) / voxelResolution);
|
|
73115
|
+
const izMin = Math.floor((cz - radius - gridMinZ) / voxelResolution);
|
|
73116
|
+
const ixMax = Math.floor((cx + radius - gridMinX) / voxelResolution);
|
|
73117
|
+
const iyMax = Math.floor((cy + radius - gridMinY) / voxelResolution);
|
|
73118
|
+
const izMax = Math.floor((cz + radius - gridMinZ) / voxelResolution);
|
|
73119
|
+
let bestPushX = 0;
|
|
73120
|
+
let bestPushY = 0;
|
|
73121
|
+
let bestPushZ = 0;
|
|
73122
|
+
let bestPenetration = PENETRATION_EPSILON;
|
|
73123
|
+
let found = false;
|
|
73124
|
+
for (let iz = izMin; iz <= izMax; iz++) {
|
|
73125
|
+
for (let iy = iyMin; iy <= iyMax; iy++) {
|
|
73126
|
+
for (let ix = ixMin; ix <= ixMax; ix++) {
|
|
73127
|
+
if (!this.isVoxelSolid(ix, iy, iz)) {
|
|
73128
|
+
continue;
|
|
73129
|
+
}
|
|
73130
|
+
// Compute the world-space AABB of this voxel
|
|
73131
|
+
const vMinX = gridMinX + ix * voxelResolution;
|
|
73132
|
+
const vMinY = gridMinY + iy * voxelResolution;
|
|
73133
|
+
const vMinZ = gridMinZ + iz * voxelResolution;
|
|
73134
|
+
const vMaxX = vMinX + voxelResolution;
|
|
73135
|
+
const vMaxY = vMinY + voxelResolution;
|
|
73136
|
+
const vMaxZ = vMinZ + voxelResolution;
|
|
73137
|
+
// Find the nearest point on the voxel AABB to the sphere center
|
|
73138
|
+
const nearX = Math.max(vMinX, Math.min(cx, vMaxX));
|
|
73139
|
+
const nearY = Math.max(vMinY, Math.min(cy, vMaxY));
|
|
73140
|
+
const nearZ = Math.max(vMinZ, Math.min(cz, vMaxZ));
|
|
73141
|
+
// Vector from nearest point to sphere center
|
|
73142
|
+
const dx = cx - nearX;
|
|
73143
|
+
const dy = cy - nearY;
|
|
73144
|
+
const dz = cz - nearZ;
|
|
73145
|
+
const distSq = dx * dx + dy * dy + dz * dz;
|
|
73146
|
+
if (distSq >= radiusSq) {
|
|
73147
|
+
continue;
|
|
73148
|
+
}
|
|
73149
|
+
let px;
|
|
73150
|
+
let py;
|
|
73151
|
+
let pz;
|
|
73152
|
+
let penetration;
|
|
73153
|
+
if (distSq > 1e-12) {
|
|
73154
|
+
// Center is outside the voxel: push radially outward
|
|
73155
|
+
const dist = Math.sqrt(distSq);
|
|
73156
|
+
penetration = radius - dist;
|
|
73157
|
+
const invDist = 1.0 / dist;
|
|
73158
|
+
px = dx * invDist * penetration;
|
|
73159
|
+
py = dy * invDist * penetration;
|
|
73160
|
+
pz = dz * invDist * penetration;
|
|
73161
|
+
}
|
|
73162
|
+
else {
|
|
73163
|
+
// Center is inside the voxel: push to nearest face + radius
|
|
73164
|
+
// so the sphere surface ends up flush with the face
|
|
73165
|
+
const distNegX = cx - vMinX;
|
|
73166
|
+
const distPosX = vMaxX - cx;
|
|
73167
|
+
const distNegY = cy - vMinY;
|
|
73168
|
+
const distPosY = vMaxY - cy;
|
|
73169
|
+
const distNegZ = cz - vMinZ;
|
|
73170
|
+
const distPosZ = vMaxZ - cz;
|
|
73171
|
+
const escapeX = distNegX < distPosX ? -(distNegX + radius) : (distPosX + radius);
|
|
73172
|
+
const escapeY = distNegY < distPosY ? -(distNegY + radius) : (distPosY + radius);
|
|
73173
|
+
const escapeZ = distNegZ < distPosZ ? -(distNegZ + radius) : (distPosZ + radius);
|
|
73174
|
+
const absX = Math.abs(escapeX);
|
|
73175
|
+
const absY = Math.abs(escapeY);
|
|
73176
|
+
const absZ = Math.abs(escapeZ);
|
|
73177
|
+
px = 0;
|
|
73178
|
+
py = 0;
|
|
73179
|
+
pz = 0;
|
|
73180
|
+
if (absX <= absY && absX <= absZ) {
|
|
73181
|
+
px = escapeX;
|
|
73182
|
+
penetration = absX;
|
|
73183
|
+
}
|
|
73184
|
+
else if (absY <= absZ) {
|
|
73185
|
+
py = escapeY;
|
|
73186
|
+
penetration = absY;
|
|
73187
|
+
}
|
|
73188
|
+
else {
|
|
73189
|
+
pz = escapeZ;
|
|
73190
|
+
penetration = absZ;
|
|
73191
|
+
}
|
|
73192
|
+
}
|
|
73193
|
+
if (penetration > bestPenetration) {
|
|
73194
|
+
bestPenetration = penetration;
|
|
73195
|
+
bestPushX = px;
|
|
73196
|
+
bestPushY = py;
|
|
73197
|
+
bestPushZ = pz;
|
|
73198
|
+
found = true;
|
|
73199
|
+
}
|
|
73200
|
+
}
|
|
73201
|
+
}
|
|
73202
|
+
}
|
|
73203
|
+
if (found) {
|
|
73204
|
+
this._push.x = bestPushX;
|
|
73205
|
+
this._push.y = bestPushY;
|
|
73206
|
+
this._push.z = bestPushZ;
|
|
73207
|
+
}
|
|
73208
|
+
return found;
|
|
73209
|
+
}
|
|
73210
|
+
/**
|
|
73211
|
+
* Find the single deepest penetrating voxel for the given vertical capsule.
|
|
73212
|
+
* The capsule is a line segment from (cx, cy - halfHeight, cz) to (cx, cy + halfHeight, cz)
|
|
73213
|
+
* swept by radius. For each voxel, the closest point on the segment to the AABB is found,
|
|
73214
|
+
* then a sphere-AABB penetration test is performed from that point.
|
|
73215
|
+
* Writes the push-out vector into this._push.
|
|
73216
|
+
*
|
|
73217
|
+
* @param cx - Capsule center X.
|
|
73218
|
+
* @param cy - Capsule center Y.
|
|
73219
|
+
* @param cz - Capsule center Z.
|
|
73220
|
+
* @param halfHeight - Half-height of the capsule's inner line segment.
|
|
73221
|
+
* @param radius - Capsule radius.
|
|
73222
|
+
* @returns True if a penetrating voxel was found.
|
|
73223
|
+
*/
|
|
73224
|
+
resolveDeepestPenetrationCapsule(cx, cy, cz, halfHeight, radius) {
|
|
73225
|
+
const { voxelResolution, gridMinX, gridMinY, gridMinZ } = this;
|
|
73226
|
+
const radiusSq = radius * radius;
|
|
73227
|
+
const segBottomY = cy - halfHeight;
|
|
73228
|
+
const segTopY = cy + halfHeight;
|
|
73229
|
+
// Compute bounding box of the capsule in voxel indices
|
|
73230
|
+
const ixMin = Math.floor((cx - radius - gridMinX) / voxelResolution);
|
|
73231
|
+
const iyMin = Math.floor((segBottomY - radius - gridMinY) / voxelResolution);
|
|
73232
|
+
const izMin = Math.floor((cz - radius - gridMinZ) / voxelResolution);
|
|
73233
|
+
const ixMax = Math.floor((cx + radius - gridMinX) / voxelResolution);
|
|
73234
|
+
const iyMax = Math.floor((segTopY + radius - gridMinY) / voxelResolution);
|
|
73235
|
+
const izMax = Math.floor((cz + radius - gridMinZ) / voxelResolution);
|
|
73236
|
+
let bestPushX = 0;
|
|
73237
|
+
let bestPushY = 0;
|
|
73238
|
+
let bestPushZ = 0;
|
|
73239
|
+
let bestPenetration = PENETRATION_EPSILON;
|
|
73240
|
+
let found = false;
|
|
73241
|
+
for (let iz = izMin; iz <= izMax; iz++) {
|
|
73242
|
+
for (let iy = iyMin; iy <= iyMax; iy++) {
|
|
73243
|
+
for (let ix = ixMin; ix <= ixMax; ix++) {
|
|
73244
|
+
if (!this.isVoxelSolid(ix, iy, iz)) {
|
|
73245
|
+
continue;
|
|
73246
|
+
}
|
|
73247
|
+
// Compute the world-space AABB of this voxel
|
|
73248
|
+
const vMinX = gridMinX + ix * voxelResolution;
|
|
73249
|
+
const vMinY = gridMinY + iy * voxelResolution;
|
|
73250
|
+
const vMinZ = gridMinZ + iz * voxelResolution;
|
|
73251
|
+
const vMaxX = vMinX + voxelResolution;
|
|
73252
|
+
const vMaxY = vMinY + voxelResolution;
|
|
73253
|
+
const vMaxZ = vMinZ + voxelResolution;
|
|
73254
|
+
// Find the closest Y on the capsule segment to this AABB.
|
|
73255
|
+
// For a vertical segment, X and Z are fixed so we only optimize Y.
|
|
73256
|
+
let segY;
|
|
73257
|
+
if (segTopY < vMinY) {
|
|
73258
|
+
// segment entirely below AABB
|
|
73259
|
+
segY = segTopY;
|
|
73260
|
+
}
|
|
73261
|
+
else if (segBottomY > vMaxY) {
|
|
73262
|
+
// segment entirely above AABB
|
|
73263
|
+
segY = segBottomY;
|
|
73264
|
+
}
|
|
73265
|
+
else {
|
|
73266
|
+
// ranges overlap - pick segment Y closest to AABB center
|
|
73267
|
+
const aabbCenterY = (vMinY + vMaxY) * 0.5;
|
|
73268
|
+
segY = Math.max(segBottomY, Math.min(segTopY, aabbCenterY));
|
|
73269
|
+
}
|
|
73270
|
+
// Now do sphere-AABB penetration from (cx, segY, cz)
|
|
73271
|
+
const nearX = Math.max(vMinX, Math.min(cx, vMaxX));
|
|
73272
|
+
const nearY = Math.max(vMinY, Math.min(segY, vMaxY));
|
|
73273
|
+
const nearZ = Math.max(vMinZ, Math.min(cz, vMaxZ));
|
|
73274
|
+
// Vector from nearest point to sphere center on segment
|
|
73275
|
+
const dx = cx - nearX;
|
|
73276
|
+
const dy = segY - nearY;
|
|
73277
|
+
const dz = cz - nearZ;
|
|
73278
|
+
const distSq = dx * dx + dy * dy + dz * dz;
|
|
73279
|
+
if (distSq >= radiusSq) {
|
|
73280
|
+
continue;
|
|
73281
|
+
}
|
|
73282
|
+
let px;
|
|
73283
|
+
let py;
|
|
73284
|
+
let pz;
|
|
73285
|
+
let penetration;
|
|
73286
|
+
if (distSq > 1e-12) {
|
|
73287
|
+
// Sphere center is outside the voxel: push radially outward
|
|
73288
|
+
const dist = Math.sqrt(distSq);
|
|
73289
|
+
penetration = radius - dist;
|
|
73290
|
+
const invDist = 1.0 / dist;
|
|
73291
|
+
px = dx * invDist * penetration;
|
|
73292
|
+
py = dy * invDist * penetration;
|
|
73293
|
+
pz = dz * invDist * penetration;
|
|
73294
|
+
}
|
|
73295
|
+
else {
|
|
73296
|
+
// Segment point is inside the voxel: push to nearest face + radius
|
|
73297
|
+
// so the capsule surface ends up flush with the face
|
|
73298
|
+
const distNegX = cx - vMinX;
|
|
73299
|
+
const distPosX = vMaxX - cx;
|
|
73300
|
+
const distNegY = segY - vMinY;
|
|
73301
|
+
const distPosY = vMaxY - segY;
|
|
73302
|
+
const distNegZ = cz - vMinZ;
|
|
73303
|
+
const distPosZ = vMaxZ - cz;
|
|
73304
|
+
const escapeX = distNegX < distPosX ? -(distNegX + radius) : (distPosX + radius);
|
|
73305
|
+
const escapeY = distNegY < distPosY ? -(distNegY + radius) : (distPosY + radius);
|
|
73306
|
+
const escapeZ = distNegZ < distPosZ ? -(distNegZ + radius) : (distPosZ + radius);
|
|
73307
|
+
const absX = Math.abs(escapeX);
|
|
73308
|
+
const absY = Math.abs(escapeY);
|
|
73309
|
+
const absZ = Math.abs(escapeZ);
|
|
73310
|
+
px = 0;
|
|
73311
|
+
py = 0;
|
|
73312
|
+
pz = 0;
|
|
73313
|
+
if (absX <= absY && absX <= absZ) {
|
|
73314
|
+
px = escapeX;
|
|
73315
|
+
penetration = absX;
|
|
73316
|
+
}
|
|
73317
|
+
else if (absY <= absZ) {
|
|
73318
|
+
py = escapeY;
|
|
73319
|
+
penetration = absY;
|
|
73320
|
+
}
|
|
73321
|
+
else {
|
|
73322
|
+
pz = escapeZ;
|
|
73323
|
+
penetration = absZ;
|
|
73324
|
+
}
|
|
73325
|
+
}
|
|
73326
|
+
if (penetration > bestPenetration) {
|
|
73327
|
+
bestPenetration = penetration;
|
|
73328
|
+
bestPushX = px;
|
|
73329
|
+
bestPushY = py;
|
|
73330
|
+
bestPushZ = pz;
|
|
73331
|
+
found = true;
|
|
73332
|
+
}
|
|
73333
|
+
}
|
|
73334
|
+
}
|
|
73335
|
+
}
|
|
73336
|
+
if (found) {
|
|
73337
|
+
this._push.x = bestPushX;
|
|
73338
|
+
this._push.y = bestPushY;
|
|
73339
|
+
this._push.z = bestPushZ;
|
|
73340
|
+
}
|
|
73341
|
+
return found;
|
|
73342
|
+
}
|
|
73343
|
+
/**
|
|
73344
|
+
* Test whether a voxel at the given grid indices is solid.
|
|
73345
|
+
*
|
|
73346
|
+
* @param ix - Global voxel X index.
|
|
73347
|
+
* @param iy - Global voxel Y index.
|
|
73348
|
+
* @param iz - Global voxel Z index.
|
|
73349
|
+
* @returns True if the voxel is solid.
|
|
73350
|
+
*/
|
|
73351
|
+
isVoxelSolid(ix, iy, iz) {
|
|
73352
|
+
if (this.nodes.length === 0 ||
|
|
73353
|
+
ix < 0 || iy < 0 || iz < 0 ||
|
|
73354
|
+
ix >= this.numVoxelsX || iy >= this.numVoxelsY || iz >= this.numVoxelsZ) {
|
|
73355
|
+
return false;
|
|
73356
|
+
}
|
|
73357
|
+
const { leafSize, treeDepth } = this;
|
|
73358
|
+
// Convert voxel indices to block coordinates
|
|
73359
|
+
const blockX = Math.floor(ix / leafSize);
|
|
73360
|
+
const blockY = Math.floor(iy / leafSize);
|
|
73361
|
+
const blockZ = Math.floor(iz / leafSize);
|
|
73362
|
+
// Traverse octree from root to leaf
|
|
73363
|
+
let nodeIndex = 0;
|
|
73364
|
+
for (let level = treeDepth - 1; level >= 0; level--) {
|
|
73365
|
+
const node = this.nodes[nodeIndex] >>> 0;
|
|
73366
|
+
// Check for solid leaf sentinel first (has nonzero high byte)
|
|
73367
|
+
if (node === SOLID_LEAF_MARKER) {
|
|
73368
|
+
return true;
|
|
73369
|
+
}
|
|
73370
|
+
const childMask = (node >>> 24) & 0xFF;
|
|
73371
|
+
// If childMask is 0, this is a mixed leaf node
|
|
73372
|
+
if (childMask === 0) {
|
|
73373
|
+
return this.checkLeafByIndex(node, ix, iy, iz);
|
|
73374
|
+
}
|
|
73375
|
+
// Determine which octant the block falls into at this level
|
|
73376
|
+
const bitX = (blockX >>> level) & 1;
|
|
73377
|
+
const bitY = (blockY >>> level) & 1;
|
|
73378
|
+
const bitZ = (blockZ >>> level) & 1;
|
|
73379
|
+
const octant = (bitZ << 2) | (bitY << 1) | bitX;
|
|
73380
|
+
// Check if this octant has a child
|
|
73381
|
+
if ((childMask & (1 << octant)) === 0) {
|
|
73382
|
+
return false;
|
|
73383
|
+
}
|
|
73384
|
+
// Calculate child offset using popcount of lower bits
|
|
73385
|
+
const baseOffset = node & 0x00FFFFFF;
|
|
73386
|
+
const prefix = (1 << octant) - 1;
|
|
73387
|
+
const childOffset = popcount(childMask & prefix);
|
|
73388
|
+
nodeIndex = baseOffset + childOffset;
|
|
73389
|
+
}
|
|
73390
|
+
// We've reached the leaf level
|
|
73391
|
+
const node = this.nodes[nodeIndex] >>> 0;
|
|
73392
|
+
if (node === SOLID_LEAF_MARKER) {
|
|
73393
|
+
return true;
|
|
73394
|
+
}
|
|
73395
|
+
return this.checkLeafByIndex(node, ix, iy, iz);
|
|
73396
|
+
}
|
|
73397
|
+
/**
|
|
73398
|
+
* Check a mixed leaf node using voxel grid indices.
|
|
73399
|
+
* The solid leaf sentinel must be checked before calling this method.
|
|
73400
|
+
*
|
|
73401
|
+
* @param node - The mixed leaf node value (lower 24 bits = leafData index).
|
|
73402
|
+
* @param ix - Global voxel X index.
|
|
73403
|
+
* @param iy - Global voxel Y index.
|
|
73404
|
+
* @param iz - Global voxel Z index.
|
|
73405
|
+
* @returns True if the voxel is solid.
|
|
73406
|
+
*/
|
|
73407
|
+
checkLeafByIndex(node, ix, iy, iz) {
|
|
73408
|
+
const leafDataIndex = node & 0x00FFFFFF;
|
|
73409
|
+
// Compute voxel coordinates within the 4x4x4 block
|
|
73410
|
+
const vx = ix & 3;
|
|
73411
|
+
const vy = iy & 3;
|
|
73412
|
+
const vz = iz & 3;
|
|
73413
|
+
// Bit index within the 64-bit mask: z * 16 + y * 4 + x
|
|
73414
|
+
const bitIndex = vz * 16 + vy * 4 + vx;
|
|
73415
|
+
// Read the appropriate 32-bit word (lo or hi)
|
|
73416
|
+
if (bitIndex < 32) {
|
|
73417
|
+
const lo = this.leafData[leafDataIndex * 2] >>> 0;
|
|
73418
|
+
return ((lo >>> bitIndex) & 1) === 1;
|
|
73419
|
+
}
|
|
73420
|
+
const hi = this.leafData[leafDataIndex * 2 + 1] >>> 0;
|
|
73421
|
+
return ((hi >>> (bitIndex - 32)) & 1) === 1;
|
|
73422
|
+
}
|
|
73423
|
+
}
|
|
73424
|
+
|
|
71837
73425
|
/** @import { XrInputSource } from 'playcanvas' */
|
|
71838
73426
|
|
|
71839
73427
|
/**
|
|
@@ -72740,7 +74328,7 @@ const initXr = (global) => {
|
|
|
72740
74328
|
});
|
|
72741
74329
|
};
|
|
72742
74330
|
|
|
72743
|
-
var version = "1.
|
|
74331
|
+
var version = "1.15.1";
|
|
72744
74332
|
|
|
72745
74333
|
const loadGsplat = async (app, config, progressCallback) => {
|
|
72746
74334
|
const { contents, contentUrl, unified, aa } = config;
|
|
@@ -72898,6 +74486,9 @@ const main = async (canvas, settingsJson, config) => {
|
|
|
72898
74486
|
animationPaused: true,
|
|
72899
74487
|
hasAR: false,
|
|
72900
74488
|
hasVR: false,
|
|
74489
|
+
hasCollision: false,
|
|
74490
|
+
hasVoxelOverlay: false,
|
|
74491
|
+
voxelOverlayEnabled: false,
|
|
72901
74492
|
isFullscreen: false,
|
|
72902
74493
|
controlsHidden: false
|
|
72903
74494
|
});
|
|
@@ -72932,6 +74523,12 @@ const main = async (canvas, settingsJson, config) => {
|
|
|
72932
74523
|
loadSkybox(app, config.skyboxUrl).then((asset) => {
|
|
72933
74524
|
app.scene.envAtlas = asset.resource;
|
|
72934
74525
|
});
|
|
74526
|
+
// Load voxel collision data
|
|
74527
|
+
const voxelLoad = config.voxelUrl &&
|
|
74528
|
+
VoxelCollider.load(config.voxelUrl).catch((err) => {
|
|
74529
|
+
console.warn('Failed to load voxel data:', err);
|
|
74530
|
+
return null;
|
|
74531
|
+
});
|
|
72935
74532
|
// Load and play sound
|
|
72936
74533
|
if (global.settings.soundUrl) {
|
|
72937
74534
|
const sound = new Audio(global.settings.soundUrl);
|
|
@@ -72946,7 +74543,7 @@ const main = async (canvas, settingsJson, config) => {
|
|
|
72946
74543
|
});
|
|
72947
74544
|
}
|
|
72948
74545
|
// Create the viewer
|
|
72949
|
-
return new Viewer(global, gsplatLoad, skyboxLoad);
|
|
74546
|
+
return new Viewer(global, gsplatLoad, skyboxLoad, voxelLoad);
|
|
72950
74547
|
};
|
|
72951
74548
|
console.log(`SuperSplat Viewer v${version} | Engine v${version$1} (${revision})`);
|
|
72952
74549
|
|