@needle-tools/engine 2.37.0-pre → 2.38.0-pre

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.
Files changed (82) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/needle-engine.d.ts +80 -13
  3. package/dist/needle-engine.js +349 -349
  4. package/dist/needle-engine.js.map +4 -4
  5. package/dist/needle-engine.min.js +25 -25
  6. package/dist/needle-engine.min.js.map +4 -4
  7. package/lib/engine/engine_addressables.d.ts +3 -1
  8. package/lib/engine/engine_addressables.js +12 -5
  9. package/lib/engine/engine_addressables.js.map +1 -1
  10. package/lib/engine/engine_element.js +3 -2
  11. package/lib/engine/engine_element.js.map +1 -1
  12. package/lib/engine/engine_element_overlay.js +4 -3
  13. package/lib/engine/engine_element_overlay.js.map +1 -1
  14. package/lib/engine/engine_input.d.ts +2 -0
  15. package/lib/engine/engine_input.js +14 -3
  16. package/lib/engine/engine_input.js.map +1 -1
  17. package/lib/engine/engine_physics.d.ts +2 -4
  18. package/lib/engine/engine_physics.js +75 -30
  19. package/lib/engine/engine_physics.js.map +1 -1
  20. package/lib/engine/engine_physics.types.d.ts +7 -0
  21. package/lib/engine/engine_physics.types.js +8 -0
  22. package/lib/engine/engine_physics.types.js.map +1 -1
  23. package/lib/engine/engine_setup.d.ts +7 -1
  24. package/lib/engine/engine_setup.js +10 -2
  25. package/lib/engine/engine_setup.js.map +1 -1
  26. package/lib/engine/engine_types.d.ts +1 -1
  27. package/lib/engine/engine_types.js.map +1 -1
  28. package/lib/engine-components/Animation.d.ts +1 -0
  29. package/lib/engine-components/Animation.js +7 -0
  30. package/lib/engine-components/Animation.js.map +1 -1
  31. package/lib/engine-components/AnimatorController.js +14 -7
  32. package/lib/engine-components/AnimatorController.js.map +1 -1
  33. package/lib/engine-components/Camera.d.ts +1 -0
  34. package/lib/engine-components/Camera.js +20 -5
  35. package/lib/engine-components/Camera.js.map +1 -1
  36. package/lib/engine-components/CharacterController.d.ts +31 -0
  37. package/lib/engine-components/CharacterController.js +167 -0
  38. package/lib/engine-components/CharacterController.js.map +1 -0
  39. package/lib/engine-components/Collider.d.ts +10 -4
  40. package/lib/engine-components/Collider.js +18 -8
  41. package/lib/engine-components/Collider.js.map +1 -1
  42. package/lib/engine-components/Light.d.ts +2 -0
  43. package/lib/engine-components/Light.js +13 -2
  44. package/lib/engine-components/Light.js.map +1 -1
  45. package/lib/engine-components/Renderer.js +4 -0
  46. package/lib/engine-components/Renderer.js.map +1 -1
  47. package/lib/engine-components/RigidBody.d.ts +6 -1
  48. package/lib/engine-components/RigidBody.js +62 -25
  49. package/lib/engine-components/RigidBody.js.map +1 -1
  50. package/lib/engine-components/SmoothFollow.d.ts +2 -1
  51. package/lib/engine-components/SmoothFollow.js +25 -17
  52. package/lib/engine-components/SmoothFollow.js.map +1 -1
  53. package/lib/engine-components/WebXR.js +3 -4
  54. package/lib/engine-components/WebXR.js.map +1 -1
  55. package/lib/engine-components/codegen/components.d.ts +3 -0
  56. package/lib/engine-components/codegen/components.js +3 -0
  57. package/lib/engine-components/codegen/components.js.map +1 -1
  58. package/package.json +1 -1
  59. package/src/engine/codegen/register_types.js +12 -0
  60. package/src/engine/dist/engine_physics.js +739 -0
  61. package/src/engine/dist/engine_setup.js +777 -0
  62. package/src/engine/engine_addressables.ts +18 -8
  63. package/src/engine/engine_element.ts +3 -2
  64. package/src/engine/engine_element_overlay.ts +4 -3
  65. package/src/engine/engine_input.ts +12 -3
  66. package/src/engine/engine_physics.ts +82 -47
  67. package/src/engine/engine_physics.types.ts +9 -0
  68. package/src/engine/engine_setup.ts +26 -17
  69. package/src/engine/engine_types.ts +2 -1
  70. package/src/engine-components/Animation.ts +8 -0
  71. package/src/engine-components/AnimatorController.ts +16 -11
  72. package/src/engine-components/Camera.ts +21 -4
  73. package/src/engine-components/CharacterController.ts +171 -0
  74. package/src/engine-components/Collider.ts +22 -12
  75. package/src/engine-components/Light.ts +17 -3
  76. package/src/engine-components/Renderer.ts +5 -1
  77. package/src/engine-components/RigidBody.ts +63 -29
  78. package/src/engine-components/SmoothFollow.ts +21 -18
  79. package/src/engine-components/WebXR.ts +3 -4
  80. package/src/engine-components/codegen/components.ts +3 -0
  81. package/src/engine-components/dist/CharacterController.js +123 -0
  82. package/src/engine-components/dist/RigidBody.js +458 -0
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
16
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
17
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
18
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
19
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
20
+ };
21
+ exports.__esModule = true;
22
+ exports.CharacterControllerInput = exports.CharacterController = void 0;
23
+ var three_1 = require("three");
24
+ var engine_math_1 = require("../engine/engine_math");
25
+ var engine_serialization_1 = require("../engine/engine_serialization");
26
+ var Collider_1 = require("./Collider");
27
+ var Component_1 = require("./Component");
28
+ var RigidBody_1 = require("./RigidBody");
29
+ var CharacterController = /** @class */ (function (_super) {
30
+ __extends(CharacterController, _super);
31
+ function CharacterController() {
32
+ var _this = _super !== null && _super.apply(this, arguments) || this;
33
+ _this.center = new three_1.Vector3(0, 0, 0);
34
+ _this.radius = .5;
35
+ _this.height = 2;
36
+ return _this;
37
+ }
38
+ CharacterController.prototype.onEnable = function () {
39
+ var rb = this.gameObject.getComponent(RigidBody_1.Rigidbody);
40
+ var collider = this.gameObject.getComponent(Collider_1.CapsuleCollider);
41
+ if (!rb)
42
+ rb = this.gameObject.addNewComponent(RigidBody_1.Rigidbody);
43
+ if (!collider)
44
+ collider = this.gameObject.addNewComponent(Collider_1.CapsuleCollider);
45
+ // rb.isKinematic = true;
46
+ collider.center.copy(this.center);
47
+ collider.radius = this.radius;
48
+ collider.height = this.height;
49
+ this.gameObject.rotation.x = 0;
50
+ this.gameObject.rotation.z = 0;
51
+ rb.lockRotationX = true;
52
+ rb.lockRotationY = true;
53
+ rb.lockRotationZ = true;
54
+ };
55
+ CharacterController.prototype.move = function (vec) {
56
+ this.gameObject.position.add(vec);
57
+ };
58
+ CharacterController.prototype.onCollisionEnter = function (col) {
59
+ // console.log(col);
60
+ };
61
+ CharacterController.prototype.onCollisionExit = function (col) {
62
+ };
63
+ CharacterController.prototype.isGrounded = function () {
64
+ };
65
+ __decorate([
66
+ engine_serialization_1.serializeable(three_1.Vector3)
67
+ ], CharacterController.prototype, "center");
68
+ __decorate([
69
+ engine_serialization_1.serializeable()
70
+ ], CharacterController.prototype, "radius");
71
+ __decorate([
72
+ engine_serialization_1.serializeable()
73
+ ], CharacterController.prototype, "height");
74
+ return CharacterController;
75
+ }(Component_1.Behaviour));
76
+ exports.CharacterController = CharacterController;
77
+ var CharacterControllerInput = /** @class */ (function (_super) {
78
+ __extends(CharacterControllerInput, _super);
79
+ function CharacterControllerInput() {
80
+ var _this = _super !== null && _super.apply(this, arguments) || this;
81
+ _this.movementSpeed = 2;
82
+ _this.rotationSpeed = 2;
83
+ _this.jumpHeight = 1;
84
+ _this._currentSpeed = new three_1.Vector3(0, 0, 0);
85
+ _this._currentAngularSpeed = new three_1.Vector3(0, 0, 0);
86
+ _this._temp = new three_1.Vector3(0, 0, 0);
87
+ return _this;
88
+ }
89
+ CharacterControllerInput.prototype.update = function () {
90
+ var forward = this.context.input.isKeyPressed("w");
91
+ var backward = this.context.input.isKeyPressed("s");
92
+ var rotateLeft = this.context.input.isKeyPressed("a");
93
+ var rotateRight = this.context.input.isKeyPressed("d");
94
+ var jump = this.context.input.isKeyPressed("space");
95
+ var step = forward ? 1 : 0 + backward ? -1 : 0;
96
+ this._currentSpeed.z += step * this.movementSpeed * this.context.time.deltaTime;
97
+ this._temp.copy(this._currentSpeed);
98
+ this._temp.applyQuaternion(this.gameObject.quaternion);
99
+ if (this.controller)
100
+ this.controller.move(this._temp);
101
+ else
102
+ this.gameObject.position.add(this._temp);
103
+ var rotation = rotateLeft ? 1 : 0 + rotateRight ? -1 : 0;
104
+ this._currentAngularSpeed.y += engine_math_1.Mathf.toRadians(rotation * this.rotationSpeed) * this.context.time.deltaTime;
105
+ this.gameObject.rotateY(this._currentAngularSpeed.y);
106
+ this._currentSpeed.multiplyScalar(1 - this.context.time.deltaTime * 10);
107
+ this._currentAngularSpeed.y *= 1 - this.context.time.deltaTime * 10;
108
+ };
109
+ __decorate([
110
+ engine_serialization_1.serializeable(CharacterController)
111
+ ], CharacterControllerInput.prototype, "controller");
112
+ __decorate([
113
+ engine_serialization_1.serializeable()
114
+ ], CharacterControllerInput.prototype, "movementSpeed");
115
+ __decorate([
116
+ engine_serialization_1.serializeable()
117
+ ], CharacterControllerInput.prototype, "rotationSpeed");
118
+ __decorate([
119
+ engine_serialization_1.serializeable()
120
+ ], CharacterControllerInput.prototype, "jumpHeight");
121
+ return CharacterControllerInput;
122
+ }(Component_1.Behaviour));
123
+ exports.CharacterControllerInput = CharacterControllerInput;
@@ -0,0 +1,458 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
16
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
17
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
18
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
19
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
20
+ };
21
+ var __generator = (this && this.__generator) || function (thisArg, body) {
22
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
+ function verb(n) { return function (v) { return step([n, v]); }; }
25
+ function step(op) {
26
+ if (f) throw new TypeError("Generator is already executing.");
27
+ while (_) try {
28
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
+ if (y = 0, t) op = [op[0] & 2, t.value];
30
+ switch (op[0]) {
31
+ case 0: case 1: t = op; break;
32
+ case 4: _.label++; return { value: op[1], done: false };
33
+ case 5: _.label++; y = op[1]; op = [0]; continue;
34
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
+ default:
36
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
+ if (t[2]) _.ops.pop();
41
+ _.trys.pop(); continue;
42
+ }
43
+ op = body.call(thisArg, _);
44
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
+ }
47
+ };
48
+ exports.__esModule = true;
49
+ exports.Rigidbody = void 0;
50
+ var Component_1 = require("./Component");
51
+ var THREE = require("three");
52
+ var engine_three_utils_1 = require("../engine/engine_three_utils");
53
+ var engine_serialization_decorator_1 = require("../engine/engine_serialization_decorator");
54
+ var engine_utils_1 = require("../engine/engine_utils");
55
+ var three_1 = require("three");
56
+ var engine_physics_types_1 = require("../engine/engine_physics.types");
57
+ var engine_util_decorator_1 = require("../engine/engine_util_decorator");
58
+ var engine_setup_1 = require("../engine/engine_setup");
59
+ var TransformWatch = /** @class */ (function () {
60
+ function TransformWatch(obj, context) {
61
+ this.positionChanged = false;
62
+ this.rotationChanged = false;
63
+ this._positionKeys = ["x", "y", "z"];
64
+ this._quaternionKeys = ["_x", "_y", "_z", "_w"];
65
+ this.mute = false;
66
+ this.context = context;
67
+ this.obj = obj;
68
+ }
69
+ Object.defineProperty(TransformWatch.prototype, "isDirty", {
70
+ get: function () {
71
+ return this.positionChanged || this.rotationChanged;
72
+ },
73
+ enumerable: false,
74
+ configurable: true
75
+ });
76
+ TransformWatch.prototype.reset = function () {
77
+ this.positionChanged = false;
78
+ this.rotationChanged = false;
79
+ this.mute = false;
80
+ // on reset delete the previously received values
81
+ // if (this.position)
82
+ // for (const key of this._positionKeys)
83
+ // delete this.position[key];
84
+ // if (this.quaternion)
85
+ // for (const key of this._quaternionKeys)
86
+ // delete this.quaternion[key];
87
+ };
88
+ TransformWatch.prototype.applyValues = function () {
89
+ // only apply the values that actually changed
90
+ // since we want to still control all the other values via physics
91
+ if (this.positionChanged && this.position) {
92
+ for (var _i = 0, _a = this._positionKeys; _i < _a.length; _i++) {
93
+ var key = _a[_i];
94
+ var val = this.position[key];
95
+ if (val !== undefined)
96
+ this.obj.position[key] = val;
97
+ }
98
+ }
99
+ if (this.rotationChanged) {
100
+ if (this.quaternion) {
101
+ for (var _b = 0, _c = this._quaternionKeys; _b < _c.length; _b++) {
102
+ var key = _c[_b];
103
+ var val = this.quaternion[key];
104
+ if (val !== undefined)
105
+ this.obj.quaternion[key] = val;
106
+ }
107
+ }
108
+ }
109
+ };
110
+ TransformWatch.prototype.start = function (position, rotation) {
111
+ var _this = this;
112
+ this.reset();
113
+ if (position) {
114
+ if (!this._positionWatch)
115
+ this._positionWatch = new engine_utils_1.Watch(this.obj.position, ["x", "y", "z"]);
116
+ this._positionWatch.apply();
117
+ this.position = {};
118
+ // this.position = this.obj.position.clone();
119
+ this._positionWatch.subscribeWrite(function (val, prop) {
120
+ if (_this.context.physics.isUpdating || _this.mute)
121
+ return;
122
+ var prev = _this.position[prop];
123
+ if (Math.abs(prev - val) < .00001)
124
+ return;
125
+ _this.position[prop] = val;
126
+ _this.positionChanged = true;
127
+ });
128
+ }
129
+ if (rotation) {
130
+ if (!this._rotationWatch)
131
+ this._rotationWatch = new engine_utils_1.Watch(this.obj.quaternion, ["_x", "_y", "_z", "_w"]);
132
+ this._rotationWatch.apply();
133
+ this.quaternion = {};
134
+ // this.quaternion = this.obj.quaternion.clone();
135
+ this._rotationWatch.subscribeWrite(function (val, prop) {
136
+ if (_this.context.physics.isUpdating || _this.mute)
137
+ return;
138
+ var prev = _this.quaternion[prop];
139
+ if (Math.abs(prev - val) < .00001)
140
+ return;
141
+ _this.quaternion[prop] = val;
142
+ _this.rotationChanged = true;
143
+ });
144
+ }
145
+ };
146
+ TransformWatch.prototype.stop = function () {
147
+ var _a, _b;
148
+ (_a = this._positionWatch) === null || _a === void 0 ? void 0 : _a.revoke();
149
+ (_b = this._rotationWatch) === null || _b === void 0 ? void 0 : _b.revoke();
150
+ };
151
+ return TransformWatch;
152
+ }());
153
+ var Rigidbody = /** @class */ (function (_super) {
154
+ __extends(Rigidbody, _super);
155
+ function Rigidbody() {
156
+ var _this = _super !== null && _super.apply(this, arguments) || this;
157
+ _this.mass = 1;
158
+ _this.useGravity = true;
159
+ _this.constraints = engine_physics_types_1.RigidbodyConstraints.None;
160
+ _this.isKinematic = false;
161
+ _this.drag = 0;
162
+ _this.angularDrag = 1;
163
+ _this.detectCollisions = true;
164
+ _this.sleepThreshold = 0.01;
165
+ _this.collisionDetectionMode = engine_physics_types_1.CollisionDetectionMode.Discrete;
166
+ _this._propertiesChanged = false;
167
+ return _this;
168
+ }
169
+ Object.defineProperty(Rigidbody.prototype, "lockPositionX", {
170
+ get: function () {
171
+ return (this.constraints & engine_physics_types_1.RigidbodyConstraints.FreezePositionX) !== 0;
172
+ },
173
+ set: function (v) {
174
+ if (v)
175
+ this.constraints |= engine_physics_types_1.RigidbodyConstraints.FreezePositionX;
176
+ else
177
+ this.constraints &= ~engine_physics_types_1.RigidbodyConstraints.FreezePositionX;
178
+ },
179
+ enumerable: false,
180
+ configurable: true
181
+ });
182
+ Object.defineProperty(Rigidbody.prototype, "lockPositionY", {
183
+ get: function () {
184
+ return (this.constraints & engine_physics_types_1.RigidbodyConstraints.FreezePositionY) !== 0;
185
+ },
186
+ set: function (v) {
187
+ if (v)
188
+ this.constraints |= engine_physics_types_1.RigidbodyConstraints.FreezePositionY;
189
+ else
190
+ this.constraints &= ~engine_physics_types_1.RigidbodyConstraints.FreezePositionY;
191
+ },
192
+ enumerable: false,
193
+ configurable: true
194
+ });
195
+ Object.defineProperty(Rigidbody.prototype, "lockPositionZ", {
196
+ get: function () {
197
+ return (this.constraints & engine_physics_types_1.RigidbodyConstraints.FreezePositionZ) !== 0;
198
+ },
199
+ set: function (v) {
200
+ if (v)
201
+ this.constraints |= engine_physics_types_1.RigidbodyConstraints.FreezePositionZ;
202
+ else
203
+ this.constraints &= ~engine_physics_types_1.RigidbodyConstraints.FreezePositionZ;
204
+ },
205
+ enumerable: false,
206
+ configurable: true
207
+ });
208
+ Object.defineProperty(Rigidbody.prototype, "lockRotationX", {
209
+ get: function () {
210
+ return (this.constraints & engine_physics_types_1.RigidbodyConstraints.FreezeRotationX) !== 0;
211
+ },
212
+ set: function (v) {
213
+ if (v)
214
+ this.constraints |= engine_physics_types_1.RigidbodyConstraints.FreezeRotationX;
215
+ else
216
+ this.constraints &= ~engine_physics_types_1.RigidbodyConstraints.FreezeRotationX;
217
+ },
218
+ enumerable: false,
219
+ configurable: true
220
+ });
221
+ Object.defineProperty(Rigidbody.prototype, "lockRotationY", {
222
+ get: function () {
223
+ return (this.constraints & engine_physics_types_1.RigidbodyConstraints.FreezeRotationY) !== 0;
224
+ },
225
+ set: function (v) {
226
+ if (v)
227
+ this.constraints |= engine_physics_types_1.RigidbodyConstraints.FreezeRotationY;
228
+ else
229
+ this.constraints &= ~engine_physics_types_1.RigidbodyConstraints.FreezeRotationY;
230
+ },
231
+ enumerable: false,
232
+ configurable: true
233
+ });
234
+ Object.defineProperty(Rigidbody.prototype, "lockRotationZ", {
235
+ get: function () {
236
+ return (this.constraints & engine_physics_types_1.RigidbodyConstraints.FreezeRotationZ) !== 0;
237
+ },
238
+ set: function (v) {
239
+ if (v)
240
+ this.constraints |= engine_physics_types_1.RigidbodyConstraints.FreezeRotationZ;
241
+ else
242
+ this.constraints &= ~engine_physics_types_1.RigidbodyConstraints.FreezeRotationZ;
243
+ },
244
+ enumerable: false,
245
+ configurable: true
246
+ });
247
+ Rigidbody.prototype.awake = function () {
248
+ this._watch = undefined;
249
+ this._currentVelocity = new THREE.Vector3();
250
+ this._smoothedVelocity = new THREE.Vector3();
251
+ this._smoothedVelocityGetter = new THREE.Vector3();
252
+ this._lastPosition = new THREE.Vector3();
253
+ };
254
+ Rigidbody.prototype.onEnable = function () {
255
+ if (!this._watch) {
256
+ this._watch = new TransformWatch(this.gameObject, this.context);
257
+ }
258
+ this._watch.start(true, true);
259
+ this.startCoroutine(this.beforePhysics(), engine_setup_1.FrameEvent.LateUpdate);
260
+ };
261
+ Rigidbody.prototype.onDisable = function () {
262
+ var _a;
263
+ (_a = this._watch) === null || _a === void 0 ? void 0 : _a.stop();
264
+ this.context.physics.removeBody(this);
265
+ };
266
+ Rigidbody.prototype.onDestroy = function () {
267
+ this.context.physics.removeBody(this);
268
+ };
269
+ Rigidbody.prototype.onValidate = function () {
270
+ this._propertiesChanged = true;
271
+ };
272
+ // need to do this right before updating physics to prevent rendered object glitching through physical bodies
273
+ Rigidbody.prototype.beforePhysics = function () {
274
+ var _a;
275
+ return __generator(this, function (_b) {
276
+ switch (_b.label) {
277
+ case 0:
278
+ if (!true) return [3 /*break*/, 2];
279
+ if (this._propertiesChanged) {
280
+ this._propertiesChanged = false;
281
+ this.context.physics.updateProperties(this);
282
+ }
283
+ if ((_a = this._watch) === null || _a === void 0 ? void 0 : _a.isDirty) {
284
+ this._watch.mute = true;
285
+ this._watch.applyValues();
286
+ this.context.physics.updateBody(this, this._watch.positionChanged, this._watch.rotationChanged);
287
+ this._watch.reset();
288
+ }
289
+ this.captureVelocity();
290
+ return [4 /*yield*/];
291
+ case 1:
292
+ _b.sent();
293
+ return [3 /*break*/, 0];
294
+ case 2: return [2 /*return*/];
295
+ }
296
+ });
297
+ };
298
+ Object.defineProperty(Rigidbody.prototype, "body", {
299
+ get: function () {
300
+ return this.context.physics.internal_getRigidbody(this);
301
+ },
302
+ enumerable: false,
303
+ configurable: true
304
+ });
305
+ // TODO: we need to differentiate between setting a physics body to a new position and moving it where we also want to affect the velocity
306
+ // public teleport(){
307
+ // this.context.physics.updateBody(this);
308
+ // }
309
+ Rigidbody.prototype.resetForces = function () {
310
+ var _a;
311
+ (_a = this.body) === null || _a === void 0 ? void 0 : _a.resetForces(true);
312
+ };
313
+ Rigidbody.prototype.resetTorques = function () {
314
+ var _a;
315
+ (_a = this.body) === null || _a === void 0 ? void 0 : _a.resetTorques(true);
316
+ };
317
+ Rigidbody.prototype.resetVelocities = function () {
318
+ this.setVelocity(0, 0, 0);
319
+ this.setAngularVelocity(0, 0, 0);
320
+ };
321
+ Rigidbody.prototype.resetForcesAndTorques = function () {
322
+ this.resetForces();
323
+ this.resetTorques();
324
+ };
325
+ Rigidbody.prototype.wakeUp = function () {
326
+ var _a;
327
+ (_a = this.body) === null || _a === void 0 ? void 0 : _a.wakeUp();
328
+ };
329
+ Rigidbody.prototype.applyForce = function (vec, _rel) {
330
+ var _a;
331
+ (_a = this.body) === null || _a === void 0 ? void 0 : _a.addForce(vec, true);
332
+ };
333
+ Rigidbody.prototype.applyImpulse = function (vec) {
334
+ var _a;
335
+ (_a = this.body) === null || _a === void 0 ? void 0 : _a.applyImpulse(vec, true);
336
+ };
337
+ Rigidbody.prototype.setForce = function (x, y, z) {
338
+ var _a, _b;
339
+ (_a = this.body) === null || _a === void 0 ? void 0 : _a.resetForces(true);
340
+ (_b = this.body) === null || _b === void 0 ? void 0 : _b.addForce({ x: x, y: y, z: z }, true);
341
+ };
342
+ Rigidbody.prototype.getVelocity = function () {
343
+ var _a;
344
+ var vel = (_a = this.body) === null || _a === void 0 ? void 0 : _a.linvel();
345
+ if (!vel)
346
+ return this._currentVelocity.set(0, 0, 0);
347
+ this._currentVelocity.x = vel.x;
348
+ this._currentVelocity.y = vel.y;
349
+ this._currentVelocity.z = vel.z;
350
+ return this._currentVelocity;
351
+ };
352
+ Rigidbody.prototype.setVelocity = function (x, y, z) {
353
+ var _a, _b;
354
+ if (x instanceof three_1.Vector3) {
355
+ var vec = x;
356
+ (_a = this.body) === null || _a === void 0 ? void 0 : _a.setLinvel(vec, true);
357
+ return;
358
+ }
359
+ if (y === undefined || z === undefined)
360
+ return;
361
+ (_b = this.body) === null || _b === void 0 ? void 0 : _b.setLinvel({ x: x, y: y, z: z }, true);
362
+ };
363
+ Rigidbody.prototype.setAngularVelocity = function (x, y, z) {
364
+ var _a, _b;
365
+ if (x instanceof three_1.Vector3) {
366
+ var vec = x;
367
+ (_a = this.body) === null || _a === void 0 ? void 0 : _a.setAngvel(vec, true);
368
+ return;
369
+ }
370
+ if (y === undefined || z === undefined)
371
+ return;
372
+ (_b = this.body) === null || _b === void 0 ? void 0 : _b.setAngvel({ x: x, y: y, z: z }, true);
373
+ };
374
+ Rigidbody.prototype.setTorque = function (x, y, z) {
375
+ this.setAngularVelocity(x, y, z);
376
+ };
377
+ Object.defineProperty(Rigidbody.prototype, "smoothedVelocity", {
378
+ get: function () {
379
+ this._smoothedVelocityGetter.copy(this._smoothedVelocity);
380
+ return this._smoothedVelocityGetter.multiplyScalar(1 / this.context.time.deltaTime);
381
+ },
382
+ enumerable: false,
383
+ configurable: true
384
+ });
385
+ // public get smoothedVelocity() { return this._smoothedVelocity; }
386
+ /**d
387
+ * @deprecated not used anymore
388
+ */
389
+ Rigidbody.prototype.setBodyFromGameObject = function (_velocity) {
390
+ if (_velocity === void 0) { _velocity = null; }
391
+ if (this.gameObject && !this.destroyed) {
392
+ // this.context.physics.updateBody(this);
393
+ // this._ignoreChange = true;
394
+ // const wp = this.worldPosition;
395
+ // this.body.position.set(wp.x, wp.y, wp.z);
396
+ // const wr = this.worldQuaternion;
397
+ // this.body.quaternion.set(wr.x, wr.y, wr.z, wr.w);
398
+ // if (velocity) {
399
+ // Rigidbody.copyVector3.set(velocity.x, velocity.y, velocity.z);
400
+ // this._smoothedVelocity.lerp(Rigidbody.copyVector3, this.context.time.deltaTime / .1);
401
+ // const sm = this._smoothedVelocity;
402
+ // this.body.velocity.x = sm.x;
403
+ // this.body.velocity.y = sm.y;
404
+ // this.body.velocity.z = sm.z;
405
+ // }
406
+ // this._ignoreChange = false;
407
+ }
408
+ };
409
+ Rigidbody.prototype.captureVelocity = function () {
410
+ if (this.body) {
411
+ var wp = engine_three_utils_1.getWorldPosition(this.gameObject);
412
+ Rigidbody.tempPosition.copy(wp);
413
+ var vel = wp.sub(this._lastPosition);
414
+ this._lastPosition.copy(Rigidbody.tempPosition);
415
+ this._smoothedVelocity.lerp(vel, this.context.time.deltaTime / .1);
416
+ // this._smoothedVelocity.set(0, 1 / this.context.time.deltaTime, 0);
417
+ }
418
+ };
419
+ Rigidbody.tempPosition = new THREE.Vector3();
420
+ __decorate([
421
+ engine_util_decorator_1.validate(),
422
+ engine_serialization_decorator_1.serializeable()
423
+ ], Rigidbody.prototype, "mass");
424
+ __decorate([
425
+ engine_util_decorator_1.validate(),
426
+ engine_serialization_decorator_1.serializeable()
427
+ ], Rigidbody.prototype, "useGravity");
428
+ __decorate([
429
+ engine_util_decorator_1.validate(),
430
+ engine_serialization_decorator_1.serializeable()
431
+ ], Rigidbody.prototype, "constraints");
432
+ __decorate([
433
+ engine_util_decorator_1.validate(),
434
+ engine_serialization_decorator_1.serializeable()
435
+ ], Rigidbody.prototype, "isKinematic");
436
+ __decorate([
437
+ engine_util_decorator_1.validate(),
438
+ engine_serialization_decorator_1.serializeable()
439
+ ], Rigidbody.prototype, "drag");
440
+ __decorate([
441
+ engine_util_decorator_1.validate(),
442
+ engine_serialization_decorator_1.serializeable()
443
+ ], Rigidbody.prototype, "angularDrag");
444
+ __decorate([
445
+ engine_util_decorator_1.validate(),
446
+ engine_serialization_decorator_1.serializeable()
447
+ ], Rigidbody.prototype, "detectCollisions");
448
+ __decorate([
449
+ engine_util_decorator_1.validate(),
450
+ engine_serialization_decorator_1.serializeable()
451
+ ], Rigidbody.prototype, "sleepThreshold");
452
+ __decorate([
453
+ engine_util_decorator_1.validate(),
454
+ engine_serialization_decorator_1.serializeable()
455
+ ], Rigidbody.prototype, "collisionDetectionMode");
456
+ return Rigidbody;
457
+ }(Component_1.Behaviour));
458
+ exports.Rigidbody = Rigidbody;