forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
60 lines (53 loc) · 2.12 KB
/
api.js
File metadata and controls
60 lines (53 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as THREE from 'three';
import React from 'react';
const {useEffect} = React;
import physicsManager from './physics-manager.js';
const localVector = new THREE.Vector3();
const localVector2 = new THREE.Vector3();
const localQuaternion = new THREE.Quaternion();
const localMatrix = new THREE.Matrix4();
const makeAppContextObject = app => {
function Box(props) {
useEffect(() => {
const position = props.position ? new THREE.Vector3().fromArray(props.position) : new THREE.Vector3();
const quaternion = props.quaternion ? new THREE.Quaternion().fromArray(props.quaternion) : new THREE.Quaternion();
const scale = props.scale ? new THREE.Vector3().fromArray(props.scale) : new THREE.Vector3();
const physicsId = physics.addBoxGeometry(position, quaternion, scale.clone().multiplyScalar(0.5), false);
// console.log('got floor', position, quaternion, scale.clone().multiplyScalar(0.5), physicsId);
return () => {
// debugger;
// console.log('render props 2', position.toArray(), quaternion.toArray(), scale.toArray(), physicsId);
physicsManager.removeGeometry(physicsId);
};
}, []);
return React.createElement('object3D', {}, props.children || []);
}
const physics = {
addBoxGeometry(position, quaternion, size, dynamic) {
app.rootObject.updateMatrixWorld();
localMatrix
.compose(position, quaternion, localVector2.set(1, 1, 1))
.premultiply(app.rootObject.matrixWorld)
.decompose(localVector, localQuaternion, localVector2);
position = localVector;
quaternion = localQuaternion;
const physicsId = physicsManager.addBoxGeometry.call(this, position, quaternion, size, dynamic);
app.physicsIds.push(physicsId);
return physicsId;
},
removeGeometry(physicsId) {
physicsManager.removeGeometry.apply(this, arguments);
const index = app.physicsIds.indexOf(physicsId);
if (index !== -1) {
app.physicsIds.splice(index);
}
},
Box,
};
return {
physics,
};
};
export {
makeAppContextObject,
};