xeokit / xeokit-sdk

Open source JavaScript SDK for viewing high-detail, full-precision 3D BIM and AEC models in the Web browser.
https://xeokit.io
Other
714 stars 286 forks source link

Ray pick don't work in different directions #1494

Closed g-rodigy closed 3 months ago

g-rodigy commented 4 months ago

Describe the bug Based on example pick_ray_surface, test some case, code below. Ray pick does not work in same conditions.

To Reproduce

const viewer = new Viewer({
    canvasId: "myCanvas"
});

viewer.camera.eye = [-9.54, 13.47, 29.97];
viewer.camera.look = [4.40, 3.72, 8.89];
viewer.camera.up = [0.21, 0.91, -0.31];

const xktLoader = new XKTLoaderPlugin(viewer);

const sceneModel = xktLoader.load({
    id: "myModel",
    src: "../../assets/models/xkt/v10/glTF-Embedded/Duplex_A_20110505.glTFEmbedded.xkt",
   edges: true
});

function rayPick(origin, direction) {
    var hitInternal = viewer.scene.pick({
        pickSurface: true,
        // pickSurfacePrecision: true,
        origin,
        direction,
    }, new PickResult())

    new Mesh(viewer.scene, {
            geometry: new ReadableGeometry(viewer.scene, {
                primitive: "lines",
                positions: [...origin, ...direction],
                indices: [0, 1]
            }),
            material: new PhongMaterial(viewer.scene, {
                emissive: [1, 0.3, 0.3],
                diffuse: [0, 0, 0],
                ambient: [0, 0, 0],
                lineWidth: 2
            }),
            pickable: false
    });

    return hitInternal
}

sceneModel.on("loaded", function () {
    var start = [-16, 5, 8]
    var end = [30, 0, 0]
    console.log(rayPick(start, end ));
    console.log(rayPick(end, start)); // return null
})

Expected behavior Will return pickResult on both variants.

Desktop (please complete the following information):

Additional context Xeokit-sdk version v2.6.9

paireks commented 3 months ago

Hey,

just to make sure: isn't it null, because you switched origin point with direction vector?

It should work if there will be start and end point, but it looks to me, that there is a starting point and direction vector instead.

g-rodigy commented 3 months ago

It should work if there will be start and end point, but it looks to me, that there is a starting point and direction vector instead.

So, looks like I misunderstood. But if I have 2 points need get direction from one to other. Could you provide some small example?

paireks commented 3 months ago

Direction vector from point A to B is a B - A, meaning: directionVectorX = B.X - A.X directionVectorY = B.Y - A.Y directionVectorZ = B.Z - A.Z

from this you should be able to create directionVector = [directionVectorX, directionVectorY, directionVectorZ]