0

For matrix transformation i use wgpu-matrix library.

I already have implemented raycast (hit object trigger) in other project. I wanna implement it also in my new project matrix-engine-wgpu.

In this moment input values are correct.


let projectionMatrix = [...object.projectionMatrix]
// after this line i have also object.projectionMatrix filled with NaN  so strange ... I use debugger watch in first pass 

let modelViewProjectionMatrix = [...object.modelViewProjectionMatrix]

ray = unproject([touchCoordinate.x, touchCoordinate.y], 
       [0, 0, touchCoordinate.w, touchCoordinate.h], 
    mat4.invert(outp,object.projectionMatrix),
    mat4.invert(outv,modelViewProjectionMatrix));

Debugger screenshot

I got NaN filled array in unproject func.

export function unproject(
  screenCoord,   // [number, number]
  viewport,      // [number, number, number, number]
  invProjection, // mat4
  invView) {
  // return vec3
  const [left, top, width, height] = viewport;
  const [x, y] = screenCoord;
  console.log("test out x ", x)
  const out = vec4.fromValues((2 * x) / width - 1 - left, (2 * (height - y - 1)) / height - 1, 1, 1);
  vec4.transformMat4(out, out, invProjection);
  out[3] = 0;
  vec4.transformMat4(out, out, invView);
  return vec3.normalize(vec3.create(), out);
}

This is special branch with this problem: https://github.com/zlatnaspirala/matrix-engine-wgpu/tree/RAYCASTER

Instruction for install and running

npm i
npm run main

This line is critical :

    var TEST1 = mat4.invert(outv, modelViewProjectionMatrix)
     console.log("test1 ;; ", TEST1)
     console.log("test2 ;; ", outv)    

output for test1

[ null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ]

output for test2

{ "0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0, "11": 0, "12": 0, "13": 0, "14": 0, "15": 0 }

If i switch mat4.invert(modelViewProjectionMatrix, outv) output :

{ "0": null, "1": null, "2": null, "3": null, "4": 0, "5": 0.7265425324440002, "6": 0, "7": 0, "8": -4.997498035430908, "9": -6.150917053222656, "10": -13.99299430847168, "11": -0.9994995594024658, "12": 4.999997615814209, "13": 6.153993606567383, "14": 12.999993324279785, "15": 0.9999995231628418 }

https://codesandbox.io/p/github/zlatnaspirala/matrix-engine-wgpu/RAYCASTER?file=%2Fsrc%2Fengine%2Fraycast-test.js%3A56%2C31&workspaceId=cd4f340e-85cd-4d71-9715-f9a263cdc951

Any suggestion ?

4
  • 1
    There's not enough code to debug your issue. You haven't stated what library you're using or what version. If you really want your issue solved you should make a MCVE Commented May 13, 2024 at 10:59
  • Mr. @gman this line mat4.invert is critical i also tried inverse... Commented May 15, 2024 at 20:28
  • 2
    posting a link to code offsite is off topic for stack overflow. you need to put enough code in the question itself to repo the issue. Further, You didn't show the contents of outv nor of modelViewProjectionMatrix before the call to mat.inverse nor did you state which library your using. If you're using wgpu-matrix, the last argument is the destination. if you're using gl-matrix, the first argument is the destination. If you're using some other library you'll need to check the docs for that library Commented May 16, 2024 at 1:20
  • @gman For now i can provide codesandbox.io/p/github/zlatnaspirala/matrix-engine-wgpu/… I need step by step to compare whole procedure... If you have some demos for simpe raycast click detect I would be glad, Thnks for cooperation like always. Commented May 24, 2024 at 19:33

1 Answer 1

1

It seems that

ray = unproject(
    [touchCoordinate.x, touchCoordinate.y],
    [0, 0, touchCoordinate.w, touchCoordinate.h],
    mat4.inverse(object.projectionMatrix),
    mat4.inverse(object.modelViewProjectionMatrix)
);

should be sufficient. If you only provide a single (matrix) parameter to inverse, it will simply return with the inverse of that parameter. If you believe inverse is producing the wrong output, you would need to show the matrix being passed in for us to see if that is the case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.