matrix-engine-wgpu 1.0.6 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.codesandbox/tasks.json +46 -0
- package/.devcontainer/devcontainer.json +22 -0
- package/.github/dependabot.yml +12 -0
- package/REFERENCE.md +3 -5
- package/dev.md +460 -0
- package/empty.js +7 -6
- package/examples/games/jamb/jamb.js +1127 -0
- package/examples/load-obj-file.js +65 -28
- package/examples/unlit-textures.js +26 -23
- package/examples.js +35 -3
- package/main.js +442 -48
- 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 +28 -13
- package/public/app.js +11405 -9375
- package/public/css/style.css +371 -110
- package/public/empty.html +1 -1
- package/public/empty.js +9887 -9264
- package/public/examples.html +10 -8
- package/public/examples.js +553 -193
- package/public/index.html +3 -5
- package/public/manifest copy.web +35 -0
- package/public/res/audios/block.mp3 +0 -0
- package/public/res/audios/dice1.mp3 +0 -0
- package/public/res/audios/dice2.mp3 +0 -0
- package/public/res/audios/start.mp3 +0 -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/multilang/en.json +27 -0
- package/public/res/multilang/sr.json +27 -0
- package/public/test.html +636 -0
- package/public/three-test.js +165 -0
- package/public/worker.html +1 -1
- package/readme.md +189 -115
- package/src/engine/cube.js +10 -1
- package/src/engine/engine.js +1 -5
- package/src/engine/loader-obj.js +9 -6
- package/src/engine/matrix-class.js +237 -204
- package/src/engine/mesh-obj.js +605 -515
- package/src/engine/raycast-test.js +93 -0
- package/src/engine/utils.js +69 -3
- package/src/multilang/lang.js +35 -0
- package/src/physics/matrix-ammo.js +168 -15
- package/src/shaders/fragment.wgsl.js +4 -2
- package/src/shaders/shaders.js +1 -1
- package/src/shaders/vertexShadow.wgsl.js +1 -1
- package/src/sounds/sounds.js +47 -0
- package/src/world.js +311 -248
|
@@ -0,0 +1,93 @@
|
|
|
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
|
+
|
|
15
|
+
let rayHitEvent;
|
|
16
|
+
|
|
17
|
+
export let touchCoordinate = {
|
|
18
|
+
enabled: false,
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0,
|
|
21
|
+
stopOnFirstDetectedHit: false
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function multiplyMatrixVector(matrix, vector) {
|
|
25
|
+
return vec4.transformMat4(vector, matrix);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getRayFromMouse(event, canvas, camera) {
|
|
29
|
+
const rect = canvas.getBoundingClientRect();
|
|
30
|
+
let x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
|
|
31
|
+
let y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
|
|
32
|
+
// simple invert
|
|
33
|
+
x = -x;
|
|
34
|
+
const fov = Math.PI / 4;
|
|
35
|
+
const aspect = canvas.width / canvas.height;
|
|
36
|
+
const near = 0.1;
|
|
37
|
+
const far = 100;
|
|
38
|
+
camera.projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 1000.0);
|
|
39
|
+
const invProjection = mat4.inverse(camera.projectionMatrix);
|
|
40
|
+
const correctedView = mat4.clone(camera.view_);
|
|
41
|
+
correctedView[2] *= -1;
|
|
42
|
+
correctedView[6] *= -1;
|
|
43
|
+
correctedView[10] *= -1;
|
|
44
|
+
const invView = mat4.inverse(correctedView);
|
|
45
|
+
const ndc = [x, y, 1, 1];
|
|
46
|
+
let worldPos = multiplyMatrixVector(invProjection, ndc);
|
|
47
|
+
worldPos = multiplyMatrixVector(invView, worldPos);
|
|
48
|
+
let world;
|
|
49
|
+
if (worldPos[3] !== 0) {
|
|
50
|
+
world = [
|
|
51
|
+
worldPos[0] / worldPos[3],
|
|
52
|
+
worldPos[2] / worldPos[3],
|
|
53
|
+
worldPos[1] / worldPos[3]
|
|
54
|
+
];
|
|
55
|
+
} else {
|
|
56
|
+
console.log("[raycaster]special case 0.")
|
|
57
|
+
world = [
|
|
58
|
+
worldPos[0],
|
|
59
|
+
worldPos[1],
|
|
60
|
+
worldPos[2]
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
const rayOrigin = [camera.position[0], camera.position[1], camera.position[2]];
|
|
64
|
+
const rayDirection = vec3.normalize(vec3.subtract(world, rayOrigin));
|
|
65
|
+
return {rayOrigin, rayDirection};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function rayIntersectsSphere(rayOrigin, rayDirection, sphereCenter, sphereRadius) {
|
|
69
|
+
const pos = [sphereCenter.x, sphereCenter.y, sphereCenter.z];
|
|
70
|
+
const oc = vec3.subtract(rayOrigin, pos);
|
|
71
|
+
const a = vec3.dot(rayDirection, rayDirection);
|
|
72
|
+
const b = 2.0 * vec3.dot(oc, rayDirection);
|
|
73
|
+
const c = vec3.dot(oc, oc) - sphereRadius * sphereRadius;
|
|
74
|
+
const discriminant = b * b - 4 * a * c;
|
|
75
|
+
return discriminant > 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function addRaycastListener () {
|
|
79
|
+
window.addEventListener('click', (event) => {
|
|
80
|
+
let canvas = document.getElementsByTagName('canvas')[0];
|
|
81
|
+
let camera = app.cameras.WASD;
|
|
82
|
+
const { rayOrigin, rayDirection } = getRayFromMouse(event, canvas, camera);
|
|
83
|
+
for (const object of app.mainRenderBundle) {
|
|
84
|
+
if (rayIntersectsSphere(rayOrigin, rayDirection, object.position, 2)) {
|
|
85
|
+
console.log('Object clicked:', object.name);
|
|
86
|
+
// Just like in matrix-engine webGL version "ray.hit.event"
|
|
87
|
+
dispatchEvent(new CustomEvent('ray.hit.event', {detail: {
|
|
88
|
+
hitObject: object
|
|
89
|
+
}}))
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
package/src/engine/utils.js
CHANGED
|
@@ -523,7 +523,7 @@ export function randomIntFromTo(min, max) {
|
|
|
523
523
|
}
|
|
524
524
|
}
|
|
525
525
|
|
|
526
|
-
export var
|
|
526
|
+
export var urlQuery = (function() {
|
|
527
527
|
var query_string = {};
|
|
528
528
|
var query = window.location.search.substring(1);
|
|
529
529
|
var vars = query.split('&');
|
|
@@ -652,5 +652,71 @@ export function quaternion_rotation_matrix(Q) {
|
|
|
652
652
|
// copnsole log graphics
|
|
653
653
|
export const LOG_WARN = 'background: gray; color: yellow; font-size:10px';
|
|
654
654
|
export const LOG_INFO = 'background: green; color: white; font-size:11px';
|
|
655
|
-
export const LOG_MATRIX = "font-family:
|
|
656
|
-
export const LOG_FUNNY = "font-family: stormfaze;color: #f1f033; font-size:14px;text-shadow: 2px 2px 4px #f335f4, 4px 4px 4px #d64444, 2px 2px 4px #c160a6, 6px 2px 0px #123de3;background: black;";
|
|
655
|
+
export const LOG_MATRIX = "font-family: stormfaze;color: #lime; font-size:11px;text-shadow: 2px 2px 4px orangered;background: black;";
|
|
656
|
+
export const LOG_FUNNY = "font-family: stormfaze;color: #f1f033; font-size:14px;text-shadow: 2px 2px 4px #f335f4, 4px 4px 4px #d64444, 2px 2px 4px #c160a6, 6px 2px 0px #123de3;background: black;";
|
|
657
|
+
export const LOG_FUNNY_SMALL = "font-family: stormfaze;color: #f1f033; font-size:10px;text-shadow: 2px 2px 4px #f335f4, 4px 4px 4px #d64444, 1px 1px 2px #c160a6, 3px 1px 0px #123de3;background: black;";
|
|
658
|
+
|
|
659
|
+
export function genName(length) {
|
|
660
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
661
|
+
let result = "";
|
|
662
|
+
for(let i = 0;i < length;i++) {
|
|
663
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
664
|
+
}
|
|
665
|
+
return result;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
export let mb = {
|
|
669
|
+
root: () => byId('msgBox'),
|
|
670
|
+
pContent: () => byId('not-content'),
|
|
671
|
+
copy: function() {
|
|
672
|
+
navigator.clipboard.writeText(mb.root().children[0].innerText);
|
|
673
|
+
},
|
|
674
|
+
c: 0, ic: 0, t: {},
|
|
675
|
+
setContent: function(content, t) {
|
|
676
|
+
var iMsg = document.createElement('div');
|
|
677
|
+
iMsg.innerHTML = content;
|
|
678
|
+
iMsg.id = `msgbox-loc-${mb.c}`;
|
|
679
|
+
mb.root().appendChild(iMsg);
|
|
680
|
+
iMsg.classList.add('animate1')
|
|
681
|
+
if(t == 'ok') {
|
|
682
|
+
iMsg.style = 'font-family: stormfaze;color:white;padding:7px;margin:2px';
|
|
683
|
+
} else {
|
|
684
|
+
iMsg.style = 'font-family: stormfaze;color:white;padding:7px;margin:2px';
|
|
685
|
+
}
|
|
686
|
+
},
|
|
687
|
+
kill: function() {
|
|
688
|
+
mb.root().remove();
|
|
689
|
+
},
|
|
690
|
+
show: function(content, t) {
|
|
691
|
+
mb.setContent(content, t);
|
|
692
|
+
mb.root().style.display = "block";
|
|
693
|
+
var loc2 = mb.c;
|
|
694
|
+
setTimeout(function() {
|
|
695
|
+
byId(`msgbox-loc-${loc2}`).classList.remove("fadeInDown");
|
|
696
|
+
byId(`msgbox-loc-${loc2}`).classList.add("fadeOut");
|
|
697
|
+
setTimeout(function() {
|
|
698
|
+
byId(`msgbox-loc-${loc2}`).style.display = "none";
|
|
699
|
+
byId(`msgbox-loc-${loc2}`).classList.remove("fadeOut");
|
|
700
|
+
|
|
701
|
+
byId(`msgbox-loc-${loc2}`).remove();
|
|
702
|
+
mb.ic++;
|
|
703
|
+
if(mb.c == mb.ic) {
|
|
704
|
+
mb.root().style.display = 'none';
|
|
705
|
+
}
|
|
706
|
+
}, 1000)
|
|
707
|
+
}, 3000);
|
|
708
|
+
mb.c++;
|
|
709
|
+
},
|
|
710
|
+
error: function(content) {
|
|
711
|
+
mb.root().classList.remove("success")
|
|
712
|
+
mb.root().classList.add("error")
|
|
713
|
+
mb.root().classList.add("fadeInDown");
|
|
714
|
+
mb.show(content, 'err');
|
|
715
|
+
},
|
|
716
|
+
success: function(content) {
|
|
717
|
+
mb.root().classList.remove("error")
|
|
718
|
+
mb.root().classList.add("success")
|
|
719
|
+
mb.root().classList.add("fadeInDown");
|
|
720
|
+
mb.show(content, 'ok');
|
|
721
|
+
}
|
|
722
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {LOG_MATRIX} from "../engine/utils";
|
|
2
|
+
|
|
3
|
+
export class MultiLang {
|
|
4
|
+
|
|
5
|
+
constructor() {
|
|
6
|
+
addEventListener('updateLang', () => {
|
|
7
|
+
console.log('Multilang updated.')
|
|
8
|
+
this.update();
|
|
9
|
+
})
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
update = function() {
|
|
13
|
+
var allTranDoms = document.querySelectorAll('[data-label]');
|
|
14
|
+
allTranDoms.forEach((i) => {
|
|
15
|
+
i.innerHTML = this.get[i.getAttribute('data-label')]
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
loadMultilang = async function(lang = 'en') {
|
|
20
|
+
lang = 'res/multilang/' + lang + '.json';
|
|
21
|
+
console.info(`%cMultilang: ${lang}` , LOG_MATRIX);
|
|
22
|
+
try {
|
|
23
|
+
const r = await fetch(lang, {
|
|
24
|
+
headers: {
|
|
25
|
+
'Accept': 'application/json',
|
|
26
|
+
'Content-Type': 'application/json'
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return await r.json()
|
|
30
|
+
} catch(err) {
|
|
31
|
+
console.warn('Not possible to access multilang json asset! Err => ', err);
|
|
32
|
+
return {}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
// import {vec3} from "wgpu-matrix";
|
|
2
|
+
import {LOG_FUNNY, degToRad, quaternion_rotation_matrix, radToDeg, scriptManager} from "../engine/utils";
|
|
2
3
|
|
|
3
4
|
export default class MatrixAmmo {
|
|
4
5
|
constructor() {
|
|
@@ -10,9 +11,13 @@ export default class MatrixAmmo {
|
|
|
10
11
|
undefined,
|
|
11
12
|
this.init,
|
|
12
13
|
);
|
|
14
|
+
|
|
15
|
+
this.lastRoll = '';
|
|
16
|
+
this.presentScore = '';
|
|
13
17
|
}
|
|
14
18
|
|
|
15
19
|
init = () => {
|
|
20
|
+
// console.log('pre ammo')
|
|
16
21
|
Ammo().then(Ammo => {
|
|
17
22
|
// Physics variables
|
|
18
23
|
this.dynamicsWorld = null;
|
|
@@ -21,13 +26,13 @@ export default class MatrixAmmo {
|
|
|
21
26
|
this.lastUpdate = 0
|
|
22
27
|
console.log("%c Ammo core loaded.", LOG_FUNNY);
|
|
23
28
|
this.initPhysics();
|
|
24
|
-
|
|
29
|
+
// simulate async
|
|
30
|
+
setTimeout(() => dispatchEvent(new CustomEvent('AmmoReady', {})), 100);
|
|
25
31
|
});
|
|
26
32
|
};
|
|
27
33
|
|
|
28
34
|
initPhysics() {
|
|
29
35
|
let Ammo = this.Ammo;
|
|
30
|
-
|
|
31
36
|
// Physics configuration
|
|
32
37
|
var collisionConfiguration = new Ammo.btDefaultCollisionConfiguration(),
|
|
33
38
|
dispatcher = new Ammo.btCollisionDispatcher(collisionConfiguration),
|
|
@@ -35,26 +40,27 @@ export default class MatrixAmmo {
|
|
|
35
40
|
solver = new Ammo.btSequentialImpulseConstraintSolver();
|
|
36
41
|
|
|
37
42
|
this.dynamicsWorld = new Ammo.btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
|
|
38
|
-
|
|
39
43
|
this.dynamicsWorld.setGravity(new Ammo.btVector3(0, -10, 0));
|
|
40
44
|
|
|
41
|
-
var groundShape = new Ammo.btBoxShape(new Ammo.btVector3(
|
|
45
|
+
var groundShape = new Ammo.btBoxShape(new Ammo.btVector3(70, 1, 70)),
|
|
42
46
|
groundTransform = new Ammo.btTransform();
|
|
43
47
|
groundTransform.setIdentity();
|
|
44
|
-
groundTransform.setOrigin(new Ammo.btVector3(0, -
|
|
48
|
+
groundTransform.setOrigin(new Ammo.btVector3(0, -4.45, 0));
|
|
45
49
|
var mass = 0,
|
|
46
50
|
isDynamic = (mass !== 0),
|
|
47
51
|
localInertia = new Ammo.btVector3(0, 0, 0);
|
|
48
52
|
|
|
49
|
-
if(isDynamic)
|
|
50
|
-
groundShape.calculateLocalInertia(mass, localInertia);
|
|
53
|
+
if(isDynamic) groundShape.calculateLocalInertia(mass, localInertia);
|
|
51
54
|
|
|
52
55
|
var myMotionState = new Ammo.btDefaultMotionState(groundTransform),
|
|
53
56
|
rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, myMotionState, groundShape, localInertia),
|
|
54
57
|
body = new Ammo.btRigidBody(rbInfo);
|
|
55
|
-
|
|
58
|
+
body.name = 'ground';
|
|
59
|
+
this.ground = body;
|
|
56
60
|
this.dynamicsWorld.addRigidBody(body);
|
|
57
61
|
// this.rigidBodies.push(body);
|
|
62
|
+
// add collide event
|
|
63
|
+
this.detectCollision()
|
|
58
64
|
}
|
|
59
65
|
|
|
60
66
|
addPhysics(MEObject, pOptions) {
|
|
@@ -87,18 +93,48 @@ export default class MatrixAmmo {
|
|
|
87
93
|
}
|
|
88
94
|
|
|
89
95
|
addPhysicsBox(MEObject, pOptions) {
|
|
96
|
+
|
|
97
|
+
const FLAGS = {
|
|
98
|
+
TEST_NIDZA: 3,
|
|
99
|
+
CF_KINEMATIC_OBJECT: 2
|
|
100
|
+
}
|
|
101
|
+
|
|
90
102
|
let Ammo = this.Ammo;
|
|
91
|
-
|
|
103
|
+
// improve this - scale by comp
|
|
104
|
+
var colShape = new Ammo.btBoxShape(new Ammo.btVector3(pOptions.scale[0], pOptions.scale[1], pOptions.scale[2])),
|
|
92
105
|
startTransform = new Ammo.btTransform();
|
|
93
106
|
startTransform.setIdentity();
|
|
94
|
-
var mass =
|
|
107
|
+
var mass = pOptions.mass;
|
|
95
108
|
var localInertia = new Ammo.btVector3(0, 0, 0);
|
|
96
109
|
colShape.calculateLocalInertia(mass, localInertia);
|
|
97
110
|
startTransform.setOrigin(new Ammo.btVector3(pOptions.position.x, pOptions.position.y, pOptions.position.z));
|
|
111
|
+
//rotation
|
|
112
|
+
// console.log('startTransform.setRotation', startTransform.setRotation)
|
|
113
|
+
var t = startTransform.getRotation()
|
|
114
|
+
t.setX(degToRad(pOptions.rotation.x))
|
|
115
|
+
t.setY(degToRad(pOptions.rotation.y))
|
|
116
|
+
t.setZ(degToRad(pOptions.rotation.z))
|
|
117
|
+
startTransform.setRotation(t)
|
|
118
|
+
|
|
119
|
+
// startTransform.setRotation(pOptions.rotation.x, pOptions.rotation.y, pOptions.rotation.z);
|
|
120
|
+
|
|
98
121
|
var myMotionState = new Ammo.btDefaultMotionState(startTransform),
|
|
99
122
|
rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia),
|
|
100
123
|
body = new Ammo.btRigidBody(rbInfo);
|
|
101
|
-
|
|
124
|
+
|
|
125
|
+
if(pOptions.mass == 0 && typeof pOptions.state == 'undefined' && typeof pOptions.collide == 'undefined') {
|
|
126
|
+
body.setActivationState(2)
|
|
127
|
+
body.setCollisionFlags(FLAGS.CF_KINEMATIC_OBJECT);
|
|
128
|
+
// console.log('what is pOptions.mass and state is 2 ....', pOptions.mass)
|
|
129
|
+
} else if(typeof pOptions.collide != 'undefined' && pOptions.collide == false) {
|
|
130
|
+
// idea not work for now - eliminate collide effect
|
|
131
|
+
body.setActivationState(4)
|
|
132
|
+
body.setCollisionFlags(FLAGS.TEST_NIDZA);
|
|
133
|
+
} else {
|
|
134
|
+
body.setActivationState(4)
|
|
135
|
+
}
|
|
136
|
+
// console.log('what is name.', pOptions.name)
|
|
137
|
+
body.name = pOptions.name;
|
|
102
138
|
body.MEObject = MEObject;
|
|
103
139
|
this.dynamicsWorld.addRigidBody(body);
|
|
104
140
|
this.rigidBodies.push(body);
|
|
@@ -111,6 +147,124 @@ export default class MatrixAmmo {
|
|
|
111
147
|
body.setLinearVelocity(tbv30);
|
|
112
148
|
}
|
|
113
149
|
|
|
150
|
+
setKinematicTransform(body, x, y, z, rx, ry, rz) {
|
|
151
|
+
if(typeof rx == 'undefined') {var rx = 0;}
|
|
152
|
+
if(typeof ry == 'undefined') {var ry = 0;}
|
|
153
|
+
if(typeof rz == 'undefined') {var rz = 0;}
|
|
154
|
+
let pos = new Ammo.btVector3();
|
|
155
|
+
// let quat = new Ammo.btQuaternion();
|
|
156
|
+
pos = body.getWorldTransform().getOrigin();
|
|
157
|
+
let localRot = body.getWorldTransform().getRotation();
|
|
158
|
+
// console.log('pre pos x:', pos.x(), " y : ", pos.y(), " z:", pos.z())
|
|
159
|
+
pos.setX(pos.x() + x)
|
|
160
|
+
pos.setY(pos.y() + y)
|
|
161
|
+
pos.setZ(pos.z() + z)
|
|
162
|
+
// console.log('position kinematic move : ', pos)
|
|
163
|
+
// console.log('position localRot : ', localRot)
|
|
164
|
+
localRot.setX(rx)
|
|
165
|
+
localRot.setY(ry)
|
|
166
|
+
localRot.setZ(rz)
|
|
167
|
+
let physicsBody = body;
|
|
168
|
+
let ms = physicsBody.getMotionState();
|
|
169
|
+
if(ms) {
|
|
170
|
+
var tmpTrans = new Ammo.btTransform();
|
|
171
|
+
|
|
172
|
+
// quat.setValue(quat.x(), quat.y(), quat.z(), quat.w());
|
|
173
|
+
tmpTrans.setIdentity();
|
|
174
|
+
tmpTrans.setOrigin(pos);
|
|
175
|
+
tmpTrans.setRotation(localRot);
|
|
176
|
+
ms.setWorldTransform(tmpTrans);
|
|
177
|
+
}
|
|
178
|
+
// console.log('body, ', body)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
getBodyByName(name) {
|
|
182
|
+
var b = null;
|
|
183
|
+
this.rigidBodies.forEach((item, index, array) => {
|
|
184
|
+
if(item.name == name) {
|
|
185
|
+
b = array[index];
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
return b;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
getNameByBody(body) {
|
|
192
|
+
var b = null;
|
|
193
|
+
this.rigidBodies.forEach((item, index, array) => {
|
|
194
|
+
if(item.kB == body.kB) {
|
|
195
|
+
b = array[index].name;
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
return b;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
detectCollision() {
|
|
202
|
+
console.log('override this')
|
|
203
|
+
return;
|
|
204
|
+
|
|
205
|
+
this.lastRoll = '';
|
|
206
|
+
this.presentScore = '';
|
|
207
|
+
|
|
208
|
+
let dispatcher = this.dynamicsWorld.getDispatcher();
|
|
209
|
+
let numManifolds = dispatcher.getNumManifolds();
|
|
210
|
+
|
|
211
|
+
for(let i = 0;i < numManifolds;i++) {
|
|
212
|
+
let contactManifold = dispatcher.getManifoldByIndexInternal(i);
|
|
213
|
+
// let numContacts = contactManifold.getNumContacts();
|
|
214
|
+
// this.rigidBodies.forEach((item) => {
|
|
215
|
+
// if(item.kB == contactManifold.getBody0().kB) {
|
|
216
|
+
// // console.log('Detected body0 =', item.name)
|
|
217
|
+
// }
|
|
218
|
+
// if(item.kB == contactManifold.getBody1().kB) {
|
|
219
|
+
// // console.log('Detected body1 =', item.name)
|
|
220
|
+
// }
|
|
221
|
+
// })
|
|
222
|
+
|
|
223
|
+
if(this.ground.kB == contactManifold.getBody0().kB &&
|
|
224
|
+
this.getNameByBody(contactManifold.getBody1()) == 'CubePhysics1') {
|
|
225
|
+
// console.log(this.ground ,'GROUND IS IN CONTACT WHO IS BODY1 ', contactManifold.getBody1())
|
|
226
|
+
// console.log('GROUND IS IN CONTACT WHO IS BODY1 getNameByBody ', this.getNameByBody(contactManifold.getBody1()))
|
|
227
|
+
// CHECK ROTATION
|
|
228
|
+
var testR = contactManifold.getBody1().getWorldTransform().getRotation();
|
|
229
|
+
if(Math.abs(testR.y()) < 0.00001) {
|
|
230
|
+
this.lastRoll += " 4 +";
|
|
231
|
+
this.presentScore += 4;
|
|
232
|
+
dispatchEvent(new CustomEvent('dice-1', {}));
|
|
233
|
+
}
|
|
234
|
+
if(Math.abs(testR.x()) < 0.00001) {
|
|
235
|
+
this.lastRoll += " 3 +";
|
|
236
|
+
this.presentScore += 3;
|
|
237
|
+
dispatchEvent(new CustomEvent('dice-4', {}));
|
|
238
|
+
}
|
|
239
|
+
if(testR.x().toString().substring(0, 5) == testR.y().toString().substring(1, 6)) {
|
|
240
|
+
this.lastRoll += " 2 +";
|
|
241
|
+
this.presentScore += 2;
|
|
242
|
+
dispatchEvent(new CustomEvent('dice-6', {}));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if(testR.x().toString().substring(0, 5) == testR.y().toString().substring(0, 5)) {
|
|
246
|
+
this.lastRoll += " 1 +";
|
|
247
|
+
this.presentScore += 1;
|
|
248
|
+
dispatchEvent(new CustomEvent('dice-2', {}));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if(testR.z().toString().substring(0, 5) == testR.y().toString().substring(1, 6)) {
|
|
252
|
+
this.lastRoll += " 6 +";
|
|
253
|
+
this.presentScore += 6;
|
|
254
|
+
dispatchEvent(new CustomEvent('dice-5', {}));
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if(testR.z().toString().substring(0, 5) == testR.y().toString().substring(0, 5)) {
|
|
258
|
+
this.lastRoll += " 5 +";
|
|
259
|
+
this.presentScore += 5;
|
|
260
|
+
dispatchEvent(new CustomEvent('dice-3', {}));
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
console.log('this.lastRoll = ', this.lastRoll, ' presentScore = ', this.presentScore)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
114
268
|
updatePhysics() {
|
|
115
269
|
// Step world
|
|
116
270
|
this.dynamicsWorld.stepSimulation(1 / 60, 10);
|
|
@@ -129,12 +283,11 @@ export default class MatrixAmmo {
|
|
|
129
283
|
body.MEObject.rotation.axis.x = testAxis.x()
|
|
130
284
|
body.MEObject.rotation.axis.y = testAxis.y()
|
|
131
285
|
body.MEObject.rotation.axis.z = testAxis.z()
|
|
132
|
-
// var tx = radToDeg(parseFloat(test.getAngle().toFixed(2)) * testAxis.x().toFixed(2))
|
|
133
|
-
// var ty = radToDeg(parseFloat(test.getAngle().toFixed(2)) * testAxis.y().toFixed(2))
|
|
134
|
-
// var tz = radToDeg(parseFloat(test.getAngle().toFixed(2)) * testAxis.z().toFixed(2))
|
|
135
286
|
body.MEObject.rotation.matrixRotation = quaternion_rotation_matrix(test);
|
|
136
287
|
body.MEObject.rotation.angle = radToDeg(parseFloat(test.getAngle().toFixed(2)))
|
|
137
288
|
}
|
|
138
289
|
})
|
|
290
|
+
// collision detect
|
|
291
|
+
this.detectCollision()
|
|
139
292
|
}
|
|
140
293
|
}
|
|
@@ -20,7 +20,7 @@ struct FragmentInput {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
const albedo = vec3f(0.9);
|
|
23
|
-
const ambientFactor = 0.
|
|
23
|
+
const ambientFactor = 0.7;
|
|
24
24
|
|
|
25
25
|
@fragment
|
|
26
26
|
fn main(input : FragmentInput) -> @location(0) vec4f {
|
|
@@ -39,10 +39,12 @@ fn main(input : FragmentInput) -> @location(0) vec4f {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
visibility /= 9.0;
|
|
42
|
-
|
|
43
42
|
let lambertFactor = max(dot(normalize(scene.lightPos - input.fragPos), normalize(input.fragNorm)), 0.0);
|
|
44
43
|
let lightingFactor = min(ambientFactor + visibility * lambertFactor, 1.0);
|
|
45
44
|
let textureColor = textureSample(meshTexture, meshSampler, input.uv);
|
|
46
45
|
|
|
47
46
|
return vec4(textureColor.rgb * lightingFactor * albedo, 1.0);
|
|
47
|
+
// return vec4f(input.fragNorm * 0.5 + 0.5, 1)
|
|
48
|
+
// return vec4f(input.uv, 0, 1)
|
|
49
|
+
// return vec4(textureColor.rgb , 0.5);
|
|
48
50
|
}`
|
package/src/shaders/shaders.js
CHANGED
|
@@ -35,7 +35,7 @@ fn vertexMain(input: VertexInput) -> VertexOutput {
|
|
|
35
35
|
@group(1) @binding(2) var meshTexture: texture_2d<f32>;
|
|
36
36
|
|
|
37
37
|
// Static directional lighting
|
|
38
|
-
const lightDir = vec3f(
|
|
38
|
+
const lightDir = vec3f(0, 1, 0);
|
|
39
39
|
const dirColor = vec3(1);
|
|
40
40
|
const ambientColor = vec3f(0.05);
|
|
41
41
|
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
|
|
2
|
+
export class MatrixSounds {
|
|
3
|
+
|
|
4
|
+
constructor() {
|
|
5
|
+
this.volume = 0.5;
|
|
6
|
+
this.audios = {};
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
createClones(c, name, path) {
|
|
10
|
+
for(var x = 1;x < c;x++) {
|
|
11
|
+
let a = new Audio(path);
|
|
12
|
+
a.id = name + x;
|
|
13
|
+
a.volume = this.volume;
|
|
14
|
+
this.audios[name + x] = a;
|
|
15
|
+
document.body.append(a);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
createAudio(name, path, useClones) {
|
|
20
|
+
let a = new Audio(path);
|
|
21
|
+
a.id = name;
|
|
22
|
+
a.volume = this.volume;
|
|
23
|
+
this.audios[name] = a;
|
|
24
|
+
document.body.append(a);
|
|
25
|
+
if(typeof useClones !== 'undefined') {
|
|
26
|
+
this.createClones(useClones, name, path);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
play(name) {
|
|
31
|
+
if(this.audios[name].paused == true) {
|
|
32
|
+
this.audios[name].play()
|
|
33
|
+
} else {
|
|
34
|
+
this.tryClone(name);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
tryClone(name) {
|
|
39
|
+
var cc = 1;
|
|
40
|
+
try {
|
|
41
|
+
while(this.audios[name + cc].paused == false) {
|
|
42
|
+
cc++;
|
|
43
|
+
}
|
|
44
|
+
if (this.audios[name + cc]) this.audios[name + cc].play();
|
|
45
|
+
} catch(err) {}
|
|
46
|
+
}
|
|
47
|
+
}
|