matrix-engine-wgpu 1.3.16 → 1.3.18
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/.codesandbox/tasks.json +46 -0
- package/.devcontainer/devcontainer.json +22 -0
- package/.github/dependabot.yml +12 -0
- package/REFERENCE.md +59 -0
- package/app-worker.js +45 -0
- package/dev.md +541 -0
- package/empty.js +17 -0
- package/examples/camera-texture.js +60 -0
- package/examples/games/jamb/html-content.js +128 -0
- package/examples/games/jamb/jamb.js +1341 -0
- package/examples/games/jamb/readme.md +3 -0
- package/examples/load-jamb.js +6 -0
- package/examples/load-obj-file.js +95 -0
- package/examples/load-objs-sequence.js +68 -0
- package/examples/unlit-textures.js +31 -0
- package/examples/video-texture.js +61 -0
- package/examples.js +73 -0
- package/main.js +635 -0
- package/non-project-files/cubebuffer-example.js +51 -0
- package/non-project-files/dev.txt +21 -0
- package/non-project-files/image1.png +0 -0
- package/non-project-files/image6.png +0 -0
- package/package.json +2 -5
- package/public/ammojs/ammo.js +957 -0
- package/public/ammojs/ammo.wasm.js +921 -0
- package/public/ammojs/ammo.wasm.wasm +0 -0
- package/public/app-worker.js +47 -0
- package/public/app.js +12744 -0
- package/public/css/style.css +711 -0
- package/public/empty.html +25 -0
- package/public/empty.js +10453 -0
- package/public/examples.html +30 -0
- package/public/examples.js +11217 -0
- package/public/index.html +20 -0
- package/public/manifest copy.web +35 -0
- package/public/manifest.web +25 -0
- package/public/res/audios/block.mp3 +0 -0
- package/public/res/audios/dice-roll.mp3 +0 -0
- package/public/res/audios/dice1.mp3 +0 -0
- package/public/res/audios/dice2.mp3 +0 -0
- package/public/res/audios/kenney/Kenney.url +2 -0
- package/public/res/audios/kenney/License.txt +22 -0
- package/public/res/audios/kenney/Patreon.url +2 -0
- package/public/res/audios/kenney/audios/back_001.ogg +0 -0
- package/public/res/audios/kenney/audios/back_002.ogg +0 -0
- package/public/res/audios/kenney/audios/back_003.ogg +0 -0
- package/public/res/audios/kenney/audios/back_004.ogg +0 -0
- package/public/res/audios/kenney/audios/bong_001.ogg +0 -0
- package/public/res/audios/kenney/audios/click_001.ogg +0 -0
- package/public/res/audios/kenney/audios/click_002.ogg +0 -0
- package/public/res/audios/kenney/audios/click_003.ogg +0 -0
- package/public/res/audios/kenney/audios/click_004.ogg +0 -0
- package/public/res/audios/kenney/audios/click_005.ogg +0 -0
- package/public/res/audios/kenney/audios/close_001.ogg +0 -0
- package/public/res/audios/kenney/audios/close_002.ogg +0 -0
- package/public/res/audios/kenney/audios/close_003.ogg +0 -0
- package/public/res/audios/kenney/audios/close_004.ogg +0 -0
- package/public/res/audios/kenney/audios/confirmation_001.ogg +0 -0
- package/public/res/audios/kenney/audios/confirmation_002.ogg +0 -0
- package/public/res/audios/kenney/audios/confirmation_003.ogg +0 -0
- package/public/res/audios/kenney/audios/confirmation_004.ogg +0 -0
- package/public/res/audios/kenney/audios/drop_001.ogg +0 -0
- package/public/res/audios/kenney/audios/drop_002.ogg +0 -0
- package/public/res/audios/kenney/audios/drop_003.ogg +0 -0
- package/public/res/audios/kenney/audios/drop_004.ogg +0 -0
- package/public/res/audios/kenney/audios/error_001.ogg +0 -0
- package/public/res/audios/kenney/audios/error_002.ogg +0 -0
- package/public/res/audios/kenney/audios/error_003.ogg +0 -0
- package/public/res/audios/kenney/audios/error_004.ogg +0 -0
- package/public/res/audios/kenney/audios/error_005.ogg +0 -0
- package/public/res/audios/kenney/audios/error_006.ogg +0 -0
- package/public/res/audios/kenney/audios/error_007.ogg +0 -0
- package/public/res/audios/kenney/audios/error_008.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_001.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_002.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_003.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_004.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_005.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_006.ogg +0 -0
- package/public/res/audios/kenney/audios/glitch_001.ogg +0 -0
- package/public/res/audios/kenney/audios/glitch_002.ogg +0 -0
- package/public/res/audios/kenney/audios/glitch_003.ogg +0 -0
- package/public/res/audios/kenney/audios/glitch_004.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_001.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_002.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_003.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_004.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_005.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_006.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_007.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_008.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_009.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_001.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_002.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_003.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_004.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_005.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_006.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_007.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_008.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_009.ogg +0 -0
- package/public/res/audios/kenney/audios/open_001.ogg +0 -0
- package/public/res/audios/kenney/audios/open_002.ogg +0 -0
- package/public/res/audios/kenney/audios/open_003.ogg +0 -0
- package/public/res/audios/kenney/audios/open_004.ogg +0 -0
- package/public/res/audios/kenney/audios/pluck_001.ogg +0 -0
- package/public/res/audios/kenney/audios/pluck_002.ogg +0 -0
- package/public/res/audios/kenney/audios/question_001.ogg +0 -0
- package/public/res/audios/kenney/audios/question_002.ogg +0 -0
- package/public/res/audios/kenney/audios/question_003.ogg +0 -0
- package/public/res/audios/kenney/audios/question_004.ogg +0 -0
- package/public/res/audios/kenney/audios/scratch_001.ogg +0 -0
- package/public/res/audios/kenney/audios/scratch_002.ogg +0 -0
- package/public/res/audios/kenney/audios/scratch_003.ogg +0 -0
- package/public/res/audios/kenney/audios/scratch_004.ogg +0 -0
- package/public/res/audios/kenney/audios/scratch_005.ogg +0 -0
- package/public/res/audios/kenney/audios/scroll_001.ogg +0 -0
- package/public/res/audios/kenney/audios/scroll_002.ogg +0 -0
- package/public/res/audios/kenney/audios/scroll_003.ogg +0 -0
- package/public/res/audios/kenney/audios/scroll_004.ogg +0 -0
- package/public/res/audios/kenney/audios/scroll_005.ogg +0 -0
- package/public/res/audios/kenney/audios/select_001.ogg +0 -0
- package/public/res/audios/kenney/audios/select_002.ogg +0 -0
- package/public/res/audios/kenney/audios/select_003.ogg +0 -0
- package/public/res/audios/kenney/audios/select_004.ogg +0 -0
- package/public/res/audios/kenney/audios/select_005.ogg +0 -0
- package/public/res/audios/kenney/audios/select_006.ogg +0 -0
- package/public/res/audios/kenney/audios/select_007.ogg +0 -0
- package/public/res/audios/kenney/audios/select_008.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_001.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_002.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_003.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_004.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_005.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_006.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_007.ogg +0 -0
- package/public/res/audios/kenney/audios/tick_001.ogg +0 -0
- package/public/res/audios/kenney/audios/tick_002.ogg +0 -0
- package/public/res/audios/kenney/audios/tick_004.ogg +0 -0
- package/public/res/audios/kenney/audios/toggle_001.ogg +0 -0
- package/public/res/audios/kenney/audios/toggle_002.ogg +0 -0
- package/public/res/audios/kenney/audios/toggle_003.ogg +0 -0
- package/public/res/audios/kenney/audios/toggle_004.ogg +0 -0
- package/public/res/audios/start.mp3 +0 -0
- package/public/res/audios/toggle_002.mp3 +0 -0
- package/public/res/fonts/Accuratist.ttf +0 -0
- package/public/res/fonts/Closeness.ttf +0 -0
- package/public/res/fonts/WARGAMES.TTF +0 -0
- package/public/res/fonts/readme.txt +5 -0
- package/public/res/fonts/stormfaze.ttf +0 -0
- package/public/res/icons/512.png +0 -0
- package/public/res/icons/webgpu-horizontal.svg +45 -0
- package/public/res/meshes/blender/cube.blend +0 -0
- package/public/res/meshes/blender/cube.blend1 +0 -0
- package/public/res/meshes/blender/cube.mtl +12 -0
- package/public/res/meshes/blender/cube.obj +46 -0
- package/public/res/meshes/blender/cube.png +0 -0
- package/public/res/meshes/blender/cubeSmartUV.blend +0 -0
- package/public/res/meshes/blender/cubeSmartUV.mtl +12 -0
- package/public/res/meshes/blender/cubeSmartUV.obj +46 -0
- package/public/res/meshes/blender/lopta.mtl +10 -0
- package/public/res/meshes/blender/lopta.obj +3402 -0
- package/public/res/meshes/blender/piramyd.blend +0 -0
- package/public/res/meshes/blender/piramyd.blend1 +0 -0
- package/public/res/meshes/blender/piramyd.js +42 -0
- package/public/res/meshes/blender/piramyd.mtl +10 -0
- package/public/res/meshes/blender/piramyd.obj +18696 -0
- package/public/res/meshes/blender/piramyd1.js +42 -0
- package/public/res/meshes/blender/sphepe.blend +0 -0
- package/public/res/meshes/blender/sphepe.blend1 +0 -0
- package/public/res/meshes/blender/sphere.mtl +10 -0
- package/public/res/meshes/blender/sphere.obj +3402 -0
- package/public/res/meshes/blender/welcomeTextblend.blend +0 -0
- package/public/res/meshes/dragon/stanfordDragonData.js +5 -0
- package/public/res/meshes/jamb/bg.blend +0 -0
- package/public/res/meshes/jamb/bg.blend1 +0 -0
- package/public/res/meshes/jamb/bg.mtl +12 -0
- package/public/res/meshes/jamb/bg.obj +17 -0
- package/public/res/meshes/jamb/bg.png +0 -0
- package/public/res/meshes/jamb/dice-default.png +0 -0
- package/public/res/meshes/jamb/dice-mark.png +0 -0
- package/public/res/meshes/jamb/dice.mtl +12 -0
- package/public/res/meshes/jamb/dice.obj +40 -0
- package/public/res/meshes/jamb/dice.png +0 -0
- package/public/res/meshes/jamb/jamb-title.mtl +12 -0
- package/public/res/meshes/jamb/jamb-title.obj +26008 -0
- package/public/res/meshes/jamb/jamb.blend +0 -0
- package/public/res/meshes/jamb/jamb.blend1 +0 -0
- package/public/res/meshes/jamb/logo.png +0 -0
- package/public/res/meshes/jamb/nidzaDice.blend +0 -0
- package/public/res/meshes/jamb/nidzaDice.blend1 +0 -0
- package/public/res/meshes/jamb/pile.blend +0 -0
- package/public/res/meshes/jamb/simpleCube.blend +0 -0
- package/public/res/meshes/jamb/simpleCube.blend1 +0 -0
- package/public/res/meshes/jamb/sounds/roll1.wav +0 -0
- package/public/res/meshes/jamb/text.png +0 -0
- package/public/res/meshes/obj/armor.obj +319 -0
- package/public/res/meshes/obj/armor.png +0 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000001.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000001.obj +23264 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000002.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000002.obj +23261 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000003.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000003.obj +23264 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000004.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000004.obj +23261 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000005.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000005.obj +23261 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000006.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000006.obj +23261 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000007.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000007.obj +23264 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000008.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000008.obj +23263 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000009.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000009.obj +23264 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000010.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000010.obj +23260 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000011.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000011.obj +23262 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000012.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000012.obj +23262 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000013.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000013.obj +23263 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000014.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000014.obj +23262 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000015.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000015.obj +23263 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000016.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000016.obj +23264 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000017.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000017.obj +23263 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000018.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000018.obj +23261 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000019.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000019.obj +23263 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000020.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000020.obj +23261 -0
- package/public/res/meshes/shapes/star1.obj +60 -0
- package/public/res/multilang/en.json +39 -0
- package/public/res/multilang/sr.json +39 -0
- package/public/res/textures/default.png +0 -0
- package/public/res/textures/rust.jpg +0 -0
- package/public/res/textures/tex1.jpg +0 -0
- package/public/res/videos/readme.txt +2 -0
- package/public/res/videos/tunel.mp4 +0 -0
- package/public/test.html +636 -0
- package/public/three-test.js +165 -0
- package/public/worker.html +25 -0
- package/src/engine/ball.js +482 -0
- package/src/engine/cube.js +496 -0
- package/src/engine/engine.js +404 -0
- package/src/engine/final/adaptJSON1.js +53 -0
- package/src/engine/final/utils2.js +63 -0
- package/src/engine/lights.js +153 -0
- package/src/engine/loader-obj.js +473 -0
- package/src/engine/materials.js +295 -0
- package/src/engine/matrix-class.js +252 -0
- package/src/engine/mesh-obj.js +574 -0
- package/src/engine/raycast.js +218 -0
- package/src/engine/utils.js +881 -0
- package/src/libs/mat.js +0 -0
- package/src/multilang/lang.js +35 -0
- package/src/physics/matrix-ammo.js +363 -0
- package/src/shaders/fragment.video.wgsl.js +83 -0
- package/src/shaders/fragment.wgsl.js +75 -0
- package/src/shaders/shaders.js +51 -0
- package/src/shaders/standard-matrix-engine-shaders/standard-matrix-engine-fs.glsl +56 -0
- package/src/shaders/standard-matrix-engine-shaders/standard-matrix-engine-vs.glsl +75 -0
- package/src/shaders/vertex.wgsl.js +54 -0
- package/src/shaders/vertexShadow.wgsl.js +20 -0
- package/src/sounds/sounds.js +69 -0
- package/src/world.js +474 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Nikola Lukic
|
|
3
|
+
* @email zlatnaspirala@gmail.com
|
|
4
|
+
* @site https://maximumroulette.com
|
|
5
|
+
* @Licence GPL v3
|
|
6
|
+
* @credits chatgpt used for this script adaptation.
|
|
7
|
+
* @Note matrix-engine-wgpu adaptation test
|
|
8
|
+
* default for now:
|
|
9
|
+
* app.cameras['WASD']
|
|
10
|
+
* Only tested for WASD type of camera.
|
|
11
|
+
* app is global - will be fixed in future
|
|
12
|
+
*/
|
|
13
|
+
import {mat4, vec3, vec4} from "wgpu-matrix";
|
|
14
|
+
let rayHitEvent;
|
|
15
|
+
|
|
16
|
+
export let touchCoordinate = {
|
|
17
|
+
enabled: false,
|
|
18
|
+
x: 0,
|
|
19
|
+
y: 0,
|
|
20
|
+
stopOnFirstDetectedHit: false
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function multiplyMatrixVector(matrix, vector) {
|
|
24
|
+
return vec4.transformMat4(vector, matrix);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function getRayFromMouse(event, canvas, camera) {
|
|
28
|
+
const rect = canvas.getBoundingClientRect();
|
|
29
|
+
let x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
|
|
30
|
+
let y = ((event.clientY - rect.top) / rect.height) * 2 - 1;
|
|
31
|
+
// simple invert
|
|
32
|
+
x = -x;
|
|
33
|
+
y = -y;
|
|
34
|
+
const fov = Math.PI / 4;
|
|
35
|
+
const aspect = canvas.width / canvas.height;
|
|
36
|
+
const near = 0.1;
|
|
37
|
+
const far = 1000;
|
|
38
|
+
camera.projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 1000.0);
|
|
39
|
+
const invProjection = mat4.inverse(camera.projectionMatrix);
|
|
40
|
+
const invView = mat4.inverse(camera.view);
|
|
41
|
+
const ndc = [x, y, 1, 1];
|
|
42
|
+
let worldPos = multiplyMatrixVector(invProjection, ndc);
|
|
43
|
+
worldPos = multiplyMatrixVector(invView, worldPos);
|
|
44
|
+
let world;
|
|
45
|
+
if(worldPos[3] !== 0) {
|
|
46
|
+
world = [
|
|
47
|
+
worldPos[0] / worldPos[3],
|
|
48
|
+
worldPos[2] / worldPos[3],
|
|
49
|
+
worldPos[1] / worldPos[3]
|
|
50
|
+
];
|
|
51
|
+
} else {
|
|
52
|
+
console.log("[raycaster]special case 0.");
|
|
53
|
+
world = [
|
|
54
|
+
worldPos[0],
|
|
55
|
+
worldPos[1],
|
|
56
|
+
worldPos[2]
|
|
57
|
+
];
|
|
58
|
+
}
|
|
59
|
+
const rayOrigin = [camera.position[0], camera.position[1], camera.position[2]];
|
|
60
|
+
const rayDirection = vec3.normalize(vec3.subtract(world, rayOrigin));
|
|
61
|
+
rayDirection[2] = -rayDirection[2];
|
|
62
|
+
return {rayOrigin, rayDirection};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function getRayFromMouse2(event, canvas, camera) {
|
|
66
|
+
const rect = canvas.getBoundingClientRect();
|
|
67
|
+
let x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
|
|
68
|
+
let y = ((event.clientY - rect.top) / rect.height) * 2 - 1;
|
|
69
|
+
// simple invert
|
|
70
|
+
y = -y;
|
|
71
|
+
const fov = Math.PI / 4;
|
|
72
|
+
const aspect = canvas.width / canvas.height;
|
|
73
|
+
const near = 0.1;
|
|
74
|
+
const far = 1000;
|
|
75
|
+
camera.projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 1000.0);
|
|
76
|
+
const invProjection = mat4.inverse(camera.projectionMatrix);
|
|
77
|
+
const invView = mat4.inverse(camera.view);
|
|
78
|
+
const ndc = [x, y, 1, 1];
|
|
79
|
+
let worldPos = multiplyMatrixVector(invProjection, ndc);
|
|
80
|
+
worldPos = multiplyMatrixVector(invView, worldPos);
|
|
81
|
+
let world;
|
|
82
|
+
if(worldPos[3] !== 0) {
|
|
83
|
+
world = [
|
|
84
|
+
worldPos[0] / worldPos[3],
|
|
85
|
+
worldPos[1] / worldPos[3],
|
|
86
|
+
worldPos[2] / worldPos[3]
|
|
87
|
+
];
|
|
88
|
+
} else {
|
|
89
|
+
console.log("[raycaster]special case 0.");
|
|
90
|
+
world = [
|
|
91
|
+
worldPos[0],
|
|
92
|
+
worldPos[1],
|
|
93
|
+
worldPos[2]
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
const rayOrigin = [camera.position[0], camera.position[1], camera.position[2]];
|
|
97
|
+
const rayDirection = vec3.normalize(vec3.subtract(world, rayOrigin));
|
|
98
|
+
return {rayOrigin, rayDirection};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function rayIntersectsSphere(rayOrigin, rayDirection, sphereCenter, sphereRadius) {
|
|
102
|
+
const pos = [sphereCenter.x, sphereCenter.y, sphereCenter.z];
|
|
103
|
+
const oc = vec3.subtract(rayOrigin, pos);
|
|
104
|
+
const a = vec3.dot(rayDirection, rayDirection);
|
|
105
|
+
const b = 2.0 * vec3.dot(oc, rayDirection);
|
|
106
|
+
const c = vec3.dot(oc, oc) - sphereRadius * sphereRadius;
|
|
107
|
+
const discriminant = b * b - 4 * a * c;
|
|
108
|
+
return discriminant > 0;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function addRaycastListener(canvasId = "canvas1") {
|
|
112
|
+
let canvasDom = document.getElementById(canvasId);
|
|
113
|
+
canvasDom.addEventListener('click', (event) => {
|
|
114
|
+
let camera = app.cameras.WASD;
|
|
115
|
+
const {rayOrigin, rayDirection} = getRayFromMouse(event, canvasDom, camera);
|
|
116
|
+
for(const object of app.mainRenderBundle) {
|
|
117
|
+
if(object.raycast.enabled == false) return;
|
|
118
|
+
if(rayIntersectsSphere(rayOrigin, rayDirection, object.position, object.raycast.radius)) {
|
|
119
|
+
console.log('Object clicked:', object.name);
|
|
120
|
+
// Just like in matrix-engine webGL version "ray.hit.event"
|
|
121
|
+
dispatchEvent(new CustomEvent('ray.hit.event', {
|
|
122
|
+
detail: {
|
|
123
|
+
hitObject: object,
|
|
124
|
+
rayOrigin: rayOrigin,
|
|
125
|
+
rayDirection: rayDirection
|
|
126
|
+
}
|
|
127
|
+
}))
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Compute AABB from flat vertices array [x,y,z, x,y,z, ...]
|
|
134
|
+
export function computeAABB(vertices) {
|
|
135
|
+
const min = [Infinity, Infinity, Infinity];
|
|
136
|
+
const max = [-Infinity, -Infinity, -Infinity];
|
|
137
|
+
|
|
138
|
+
for(let i = 0;i < vertices.length;i += 3) {
|
|
139
|
+
min[0] = Math.min(min[0], vertices[i]);
|
|
140
|
+
min[1] = Math.min(min[1], vertices[i + 1]);
|
|
141
|
+
min[2] = Math.min(min[2], vertices[i + 2]);
|
|
142
|
+
|
|
143
|
+
max[0] = Math.max(max[0], vertices[i]);
|
|
144
|
+
max[1] = Math.max(max[1], vertices[i + 1]);
|
|
145
|
+
max[2] = Math.max(max[2], vertices[i + 2]);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return [min, max];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Ray-AABB intersection using slabs method
|
|
152
|
+
export function rayIntersectsAABB(rayOrigin, rayDirection, boxMin, boxMax) {
|
|
153
|
+
let tmin = (boxMin[0] - rayOrigin[0]) / rayDirection[0];
|
|
154
|
+
let tmax = (boxMax[0] - rayOrigin[0]) / rayDirection[0];
|
|
155
|
+
if(tmin > tmax) [tmin, tmax] = [tmax, tmin];
|
|
156
|
+
|
|
157
|
+
let tymin = (boxMin[1] - rayOrigin[1]) / rayDirection[1];
|
|
158
|
+
let tymax = (boxMax[1] - rayOrigin[1]) / rayDirection[1];
|
|
159
|
+
if(tymin > tymax) [tymin, tymax] = [tymax, tymin];
|
|
160
|
+
|
|
161
|
+
if((tmin > tymax) || (tymin > tmax)) return false;
|
|
162
|
+
if(tymin > tmin) tmin = tymin;
|
|
163
|
+
if(tymax < tmax) tmax = tymax;
|
|
164
|
+
|
|
165
|
+
let tzmin = (boxMin[2] - rayOrigin[2]) / rayDirection[2];
|
|
166
|
+
let tzmax = (boxMax[2] - rayOrigin[2]) / rayDirection[2];
|
|
167
|
+
if(tzmin > tzmax) [tzmin, tzmax] = [tzmax, tzmin];
|
|
168
|
+
|
|
169
|
+
if((tmin > tzmax) || (tzmin > tmax)) return false;
|
|
170
|
+
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function computeWorldVertsAndAABB(object) {
|
|
175
|
+
const modelMatrix = object.getModelMatrix(object.position);
|
|
176
|
+
const worldVerts = [];
|
|
177
|
+
for(let i = 0;i < object.mesh.vertices.length;i += 3) {
|
|
178
|
+
const local = vec3.fromValues(
|
|
179
|
+
object.mesh.vertices[i],
|
|
180
|
+
object.mesh.vertices[i + 1],
|
|
181
|
+
object.mesh.vertices[i + 2]
|
|
182
|
+
);
|
|
183
|
+
const world = vec3.transformMat4(local, modelMatrix); // OK
|
|
184
|
+
worldVerts.push(world[0], world[1], world[2]);
|
|
185
|
+
}
|
|
186
|
+
const [boxMin, boxMax] = computeAABB(worldVerts);
|
|
187
|
+
return {
|
|
188
|
+
modelMatrix,
|
|
189
|
+
worldVerts,
|
|
190
|
+
boxMin,
|
|
191
|
+
boxMax,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function addRaycastsAABBListener(canvasId = "canvas1") {
|
|
196
|
+
const canvasDom = document.getElementById(canvasId);
|
|
197
|
+
if(!canvasDom) {
|
|
198
|
+
console.warn(`Canvas with id ${canvasId} not found`);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
canvasDom.addEventListener('click', (event) => {
|
|
203
|
+
const camera = app.cameras.WASD;
|
|
204
|
+
const {rayOrigin, rayDirection} = getRayFromMouse2(event, canvasDom, camera);
|
|
205
|
+
for(const object of app.mainRenderBundle) {
|
|
206
|
+
const {boxMin, boxMax} = computeWorldVertsAndAABB(object);
|
|
207
|
+
if(object.raycast.enabled == false) return;
|
|
208
|
+
if(rayIntersectsAABB(rayOrigin, rayDirection, boxMin, boxMax)) {
|
|
209
|
+
// console.log('AABB hit:', object.name);
|
|
210
|
+
canvasDom.dispatchEvent(new CustomEvent('ray.hit.event', {
|
|
211
|
+
detail: {hitObject: object},
|
|
212
|
+
rayOrigin: rayOrigin,
|
|
213
|
+
rayDirection: rayDirection
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
}
|