matrix-engine-wgpu 1.0.5 → 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 +5 -2
- package/dev.md +460 -0
- package/empty.js +7 -6
- package/examples/games/jamb/jamb.js +1127 -0
- package/examples/load-obj-file.js +67 -37
- package/examples/unlit-textures.js +26 -23
- package/examples.js +35 -3
- package/main.js +441 -43
- 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 +50 -41
- package/public/app.js +11409 -8893
- package/public/css/style.css +376 -112
- package/public/empty.html +1 -1
- package/public/empty.js +9890 -8885
- package/public/examples.html +10 -8
- package/public/examples.js +945 -203
- package/public/index.html +5 -7
- 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/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/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/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/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 +232 -116
- package/src/engine/cube.js +10 -5
- package/src/engine/engine.js +3 -9
- package/src/engine/loader-obj.js +9 -6
- package/src/engine/matrix-class.js +240 -202
- package/src/engine/mesh-obj.js +605 -525
- package/src/engine/mesh.js +476 -0
- package/src/engine/raycast-test.js +93 -0
- package/src/engine/utils.js +129 -15
- package/src/multilang/lang.js +35 -0
- package/src/physics/matrix-ammo.js +204 -30
- 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 +312 -236
- package/src/engine/matrix-mesh.js +0 -49
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('&');
|
|
@@ -543,17 +543,18 @@ export var QueryString = (function() {
|
|
|
543
543
|
|
|
544
544
|
|
|
545
545
|
export function getAxisRot(q1) {
|
|
546
|
-
var x,y,z;
|
|
546
|
+
var x, y, z;
|
|
547
547
|
|
|
548
548
|
// if w>1 acos and sqrt will produce errors, this cant happen if quaternion is normalised
|
|
549
|
-
if
|
|
549
|
+
if(q1.w > 1) q1.normalise();
|
|
550
550
|
var angle = 2 * Math.acos(q1.w);
|
|
551
551
|
// assuming quaternion normalised then w is less than 1, so term always positive.
|
|
552
|
-
var s = Math.sqrt(1-q1.w*q1.w);
|
|
553
|
-
|
|
554
|
-
if
|
|
552
|
+
var s = Math.sqrt(1 - q1.w * q1.w);
|
|
553
|
+
// test to avoid divide by zero, s is always positive due to sqrt
|
|
554
|
+
if(s < 0.001) {
|
|
555
555
|
// if s close to zero then direction of axis not important
|
|
556
|
-
|
|
556
|
+
// if it is important that axis is normalised then replace with x=1; y=z=0;
|
|
557
|
+
|
|
557
558
|
x = q1.x;
|
|
558
559
|
y = q1.y;
|
|
559
560
|
z = q1.z;
|
|
@@ -562,12 +563,53 @@ export function getAxisRot(q1) {
|
|
|
562
563
|
y = q1.y / s;
|
|
563
564
|
z = q1.z / s;
|
|
564
565
|
}
|
|
565
|
-
return [radToDeg(x),radToDeg(y), radToDeg(z)]
|
|
566
|
+
return [radToDeg(x), radToDeg(y), radToDeg(z)]
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
export function getAxisRot2(targetAxis, Q) {
|
|
570
|
+
Q.normalize(); // if w>1 acos and sqrt will produce errors, this cant happen if quaternion is normalised
|
|
571
|
+
var angle = 2 * Math.acos(Q.w());
|
|
572
|
+
var s = Math.sqrt(1 - Q.w() * Q.w()); // assuming quaternion normalised then w is less than 1, so term always positive.
|
|
573
|
+
if(s < 0.001) { // test to avoid divide by zero, s is always positive due to sqrt
|
|
574
|
+
// if s close to zero then direction of axis not important
|
|
575
|
+
// if it is important that axis is normalised then replace with x=1; y=z=0;
|
|
576
|
+
// targetAxis.x = 1;
|
|
577
|
+
// targetAxis.y = 0;
|
|
578
|
+
// targetAxis.z = 0;
|
|
579
|
+
targetAxis.x = Q.x();
|
|
580
|
+
targetAxis.y = Q.y();
|
|
581
|
+
targetAxis.z = Q.z();
|
|
582
|
+
} else {
|
|
583
|
+
targetAxis.x = Q.x() / s; // normalise axis
|
|
584
|
+
targetAxis.y = Q.y() / s;
|
|
585
|
+
targetAxis.z = Q.z() / s;
|
|
586
|
+
}
|
|
587
|
+
return [targetAxis, angle];
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
export function getAxisRot3(Q) {
|
|
591
|
+
|
|
592
|
+
var angle = Math.acos(Q.w) * 2;
|
|
593
|
+
var axis = {};
|
|
594
|
+
|
|
595
|
+
if(Math.sin(Math.acos(angle)) > 0) {
|
|
596
|
+
|
|
597
|
+
axis.x = Q.x / Math.sin(Math.acos(angle / 2));
|
|
598
|
+
axis.y = Q.y / Math.sin(Math.acos(angle / 2));
|
|
599
|
+
axis.z = Q.z / Math.sin(Math.acos(angle / 2));
|
|
600
|
+
|
|
601
|
+
} else {
|
|
602
|
+
axis.x = 0;
|
|
603
|
+
axis.y = 0;
|
|
604
|
+
axis.z = 0;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
return axis;
|
|
566
608
|
}
|
|
567
609
|
|
|
568
610
|
// NTO TESTED
|
|
569
611
|
export function quaternion_rotation_matrix(Q) {
|
|
570
|
-
|
|
612
|
+
|
|
571
613
|
// Covert a quaternion into a full three-dimensional rotation matrix.
|
|
572
614
|
|
|
573
615
|
// Input
|
|
@@ -583,26 +625,98 @@ export function quaternion_rotation_matrix(Q) {
|
|
|
583
625
|
var q1 = Q[1]
|
|
584
626
|
var q2 = Q[2]
|
|
585
627
|
var q3 = Q[3]
|
|
586
|
-
|
|
628
|
+
|
|
587
629
|
// # First row of the rotation matrix
|
|
588
630
|
var r00 = 2 * (q0 * q0 + q1 * q1) - 1
|
|
589
631
|
var r01 = 2 * (q1 * q2 - q0 * q3)
|
|
590
632
|
var r02 = 2 * (q1 * q3 + q0 * q2)
|
|
591
|
-
|
|
633
|
+
|
|
592
634
|
// # Second row of the rotation matrix
|
|
593
635
|
var r10 = 2 * (q1 * q2 + q0 * q3)
|
|
594
636
|
var r11 = 2 * (q0 * q0 + q2 * q2) - 1
|
|
595
637
|
var r12 = 2 * (q2 * q3 - q0 * q1)
|
|
596
|
-
|
|
638
|
+
|
|
597
639
|
// # Third row of the rotation matrix
|
|
598
640
|
var r20 = 2 * (q1 * q3 - q0 * q2)
|
|
599
641
|
var r21 = 2 * (q2 * q3 + q0 * q1)
|
|
600
642
|
var r22 = 2 * (q0 * q0 + q3 * q3) - 1
|
|
601
|
-
|
|
643
|
+
|
|
602
644
|
// # 3x3 rotation matrix
|
|
603
645
|
var rot_matrix = [[r00, r01, r02],
|
|
604
|
-
|
|
605
|
-
|
|
646
|
+
[r10, r11, r12],
|
|
647
|
+
[r20, r21, r22]]
|
|
606
648
|
|
|
607
649
|
return rot_matrix;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// copnsole log graphics
|
|
653
|
+
export const LOG_WARN = 'background: gray; color: yellow; font-size:10px';
|
|
654
|
+
export const LOG_INFO = 'background: green; color: white; font-size:11px';
|
|
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
|
+
}
|
|
608
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,5 +1,5 @@
|
|
|
1
|
-
import {vec3
|
|
2
|
-
import {
|
|
1
|
+
// import {vec3} from "wgpu-matrix";
|
|
2
|
+
import {LOG_FUNNY, degToRad, quaternion_rotation_matrix, radToDeg, scriptManager} from "../engine/utils";
|
|
3
3
|
|
|
4
4
|
export default class MatrixAmmo {
|
|
5
5
|
constructor() {
|
|
@@ -11,25 +11,28 @@ export default class MatrixAmmo {
|
|
|
11
11
|
undefined,
|
|
12
12
|
this.init,
|
|
13
13
|
);
|
|
14
|
+
|
|
15
|
+
this.lastRoll = '';
|
|
16
|
+
this.presentScore = '';
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
init = () => {
|
|
17
|
-
//
|
|
20
|
+
// console.log('pre ammo')
|
|
18
21
|
Ammo().then(Ammo => {
|
|
19
22
|
// Physics variables
|
|
20
23
|
this.dynamicsWorld = null;
|
|
21
24
|
this.rigidBodies = [];
|
|
22
25
|
this.Ammo = Ammo;
|
|
23
26
|
this.lastUpdate = 0
|
|
24
|
-
console.log("Ammo core loaded.");
|
|
27
|
+
console.log("%c Ammo core loaded.", LOG_FUNNY);
|
|
25
28
|
this.initPhysics();
|
|
26
|
-
|
|
29
|
+
// simulate async
|
|
30
|
+
setTimeout(() => dispatchEvent(new CustomEvent('AmmoReady', {})), 100);
|
|
27
31
|
});
|
|
28
32
|
};
|
|
29
33
|
|
|
30
34
|
initPhysics() {
|
|
31
35
|
let Ammo = this.Ammo;
|
|
32
|
-
|
|
33
36
|
// Physics configuration
|
|
34
37
|
var collisionConfiguration = new Ammo.btDefaultCollisionConfiguration(),
|
|
35
38
|
dispatcher = new Ammo.btCollisionDispatcher(collisionConfiguration),
|
|
@@ -37,57 +40,231 @@ export default class MatrixAmmo {
|
|
|
37
40
|
solver = new Ammo.btSequentialImpulseConstraintSolver();
|
|
38
41
|
|
|
39
42
|
this.dynamicsWorld = new Ammo.btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
|
|
40
|
-
|
|
41
43
|
this.dynamicsWorld.setGravity(new Ammo.btVector3(0, -10, 0));
|
|
42
44
|
|
|
43
|
-
var groundShape = new Ammo.btBoxShape(new Ammo.btVector3(
|
|
45
|
+
var groundShape = new Ammo.btBoxShape(new Ammo.btVector3(70, 1, 70)),
|
|
44
46
|
groundTransform = new Ammo.btTransform();
|
|
45
47
|
groundTransform.setIdentity();
|
|
46
|
-
groundTransform.setOrigin(new Ammo.btVector3(0, -
|
|
48
|
+
groundTransform.setOrigin(new Ammo.btVector3(0, -4.45, 0));
|
|
47
49
|
var mass = 0,
|
|
48
50
|
isDynamic = (mass !== 0),
|
|
49
51
|
localInertia = new Ammo.btVector3(0, 0, 0);
|
|
50
52
|
|
|
51
|
-
if(isDynamic)
|
|
52
|
-
groundShape.calculateLocalInertia(mass, localInertia);
|
|
53
|
+
if(isDynamic) groundShape.calculateLocalInertia(mass, localInertia);
|
|
53
54
|
|
|
54
55
|
var myMotionState = new Ammo.btDefaultMotionState(groundTransform),
|
|
55
56
|
rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, myMotionState, groundShape, localInertia),
|
|
56
57
|
body = new Ammo.btRigidBody(rbInfo);
|
|
57
|
-
|
|
58
|
+
body.name = 'ground';
|
|
59
|
+
this.ground = body;
|
|
58
60
|
this.dynamicsWorld.addRigidBody(body);
|
|
59
61
|
// this.rigidBodies.push(body);
|
|
62
|
+
// add collide event
|
|
63
|
+
this.detectCollision()
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
addPhysics(MEObject, pOptions) {
|
|
63
|
-
if
|
|
64
|
-
|
|
65
|
-
} else if
|
|
66
|
-
|
|
67
|
+
if(pOptions.geometry == "Sphere") {
|
|
68
|
+
this.addPhysicsSphere(MEObject, pOptions)
|
|
69
|
+
} else if(pOptions.geometry == "Cube") {
|
|
70
|
+
this.addPhysicsBox(MEObject, pOptions)
|
|
67
71
|
}
|
|
68
72
|
}
|
|
69
73
|
|
|
70
74
|
addPhysicsSphere(MEObject, pOptions) {
|
|
71
75
|
let Ammo = this.Ammo;
|
|
72
|
-
var colShape = new Ammo.btSphereShape(
|
|
76
|
+
var colShape = new Ammo.btSphereShape(pOptions.radius),
|
|
73
77
|
startTransform = new Ammo.btTransform();
|
|
74
78
|
startTransform.setIdentity();
|
|
75
79
|
var mass = 1;
|
|
76
80
|
var localInertia = new Ammo.btVector3(0, 0, 0);
|
|
77
81
|
colShape.calculateLocalInertia(mass, localInertia);
|
|
78
82
|
|
|
79
|
-
startTransform.setOrigin(new Ammo.btVector3(
|
|
83
|
+
startTransform.setOrigin(new Ammo.btVector3(pOptions.position.x, pOptions.position.y, pOptions.position.z));
|
|
84
|
+
|
|
85
|
+
var myMotionState = new Ammo.btDefaultMotionState(startTransform),
|
|
86
|
+
rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia),
|
|
87
|
+
body = new Ammo.btRigidBody(rbInfo);
|
|
88
|
+
|
|
89
|
+
body.MEObject = MEObject;
|
|
90
|
+
this.dynamicsWorld.addRigidBody(body);
|
|
91
|
+
this.rigidBodies.push(body);
|
|
92
|
+
return body;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
addPhysicsBox(MEObject, pOptions) {
|
|
96
|
+
|
|
97
|
+
const FLAGS = {
|
|
98
|
+
TEST_NIDZA: 3,
|
|
99
|
+
CF_KINEMATIC_OBJECT: 2
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let Ammo = this.Ammo;
|
|
103
|
+
// improve this - scale by comp
|
|
104
|
+
var colShape = new Ammo.btBoxShape(new Ammo.btVector3(pOptions.scale[0], pOptions.scale[1], pOptions.scale[2])),
|
|
105
|
+
startTransform = new Ammo.btTransform();
|
|
106
|
+
startTransform.setIdentity();
|
|
107
|
+
var mass = pOptions.mass;
|
|
108
|
+
var localInertia = new Ammo.btVector3(0, 0, 0);
|
|
109
|
+
colShape.calculateLocalInertia(mass, localInertia);
|
|
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);
|
|
80
120
|
|
|
81
121
|
var myMotionState = new Ammo.btDefaultMotionState(startTransform),
|
|
82
122
|
rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia),
|
|
83
123
|
body = new Ammo.btRigidBody(rbInfo);
|
|
84
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;
|
|
85
138
|
body.MEObject = MEObject;
|
|
86
139
|
this.dynamicsWorld.addRigidBody(body);
|
|
87
140
|
this.rigidBodies.push(body);
|
|
88
141
|
return body;
|
|
89
142
|
}
|
|
90
143
|
|
|
144
|
+
setBodyVelocity(body, x, y, z) {
|
|
145
|
+
var tbv30 = new Ammo.btVector3();
|
|
146
|
+
tbv30.setValue(x, y, z);
|
|
147
|
+
body.setLinearVelocity(tbv30);
|
|
148
|
+
}
|
|
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
|
+
|
|
91
268
|
updatePhysics() {
|
|
92
269
|
// Step world
|
|
93
270
|
this.dynamicsWorld.stepSimulation(1 / 60, 10);
|
|
@@ -100,20 +277,17 @@ export default class MatrixAmmo {
|
|
|
100
277
|
var _y = trans.getOrigin().y().toFixed(2);
|
|
101
278
|
var _z = trans.getOrigin().z().toFixed(2);
|
|
102
279
|
body.MEObject.position.setPosition(_x, _y, _z)
|
|
103
|
-
|
|
104
280
|
var test = trans.getRotation();
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
console.log("world axis W = " + test.w());
|
|
113
|
-
var bug = getAxisRot({x: test.x(), y: test.y(), z: test.z(), w: test.w()})
|
|
114
|
-
console.log('bug:', bug)
|
|
115
|
-
// transform.setRotation(new Ammo.btQuaternion(quat.x, quat.y, quat.z, quat.w));
|
|
281
|
+
var testAxis = test.getAxis();
|
|
282
|
+
test.normalize()
|
|
283
|
+
body.MEObject.rotation.axis.x = testAxis.x()
|
|
284
|
+
body.MEObject.rotation.axis.y = testAxis.y()
|
|
285
|
+
body.MEObject.rotation.axis.z = testAxis.z()
|
|
286
|
+
body.MEObject.rotation.matrixRotation = quaternion_rotation_matrix(test);
|
|
287
|
+
body.MEObject.rotation.angle = radToDeg(parseFloat(test.getAngle().toFixed(2)))
|
|
116
288
|
}
|
|
117
289
|
})
|
|
290
|
+
// collision detect
|
|
291
|
+
this.detectCollision()
|
|
118
292
|
}
|
|
119
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
|
+
}
|