diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2de5d78 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,61 @@ +# 1.0.0 (2025-01-14) + + +### Bug Fixes + +* add jump up, jump down feature, delete jump asset ([#20](https://github.com/Byongho96/three-game-controls/issues/20)) ([984a75a](https://github.com/Byongho96/three-game-controls/commit/984a75a2687665f5ef5715ee4eb08ee170052708)) +* change default rotateSpeed ([22cd0ee](https://github.com/Byongho96/three-game-controls/commit/22cd0eeeb7e96447d54dca46e6a4971b3d001f38)) +* check if jump key is pressed to prevent unintended jump animation ([98ad67d](https://github.com/Byongho96/three-game-controls/commit/98ad67df36e4e0f3ed781e685e1aae050b36a4c8)) +* correct incorrect file paths ([#35](https://github.com/Byongho96/three-game-controls/issues/35)) ([befa87b](https://github.com/Byongho96/three-game-controls/commit/befa87b8a036cdba944036e22bd7de43a35796d9)) +* fix handler bindings and remove unnecessary code ([a0739ae](https://github.com/Byongho96/three-game-controls/commit/a0739ae6d9756487d19a969df6cfa768997a909a)) +* fix option name, avoid redundant object creation ([134a396](https://github.com/Byongho96/three-game-controls/commit/134a3963b1ac28a056040afb782c71c7ad60934e)) +* fix updateSync, updateCamera function ([e98840e](https://github.com/Byongho96/three-game-controls/commit/e98840e078db3f0fc4eabd6036ff3b366dc21f3b)) +* fixed issue with axisSync never not working in ThirdPersonMouseDragControls ([5204e86](https://github.com/Byongho96/three-game-controls/commit/5204e866575fc65cfa822eb9560d27ffe2912c80)) +* fixed issue with axisSync never not working in ThirdPersonPointerLockControls ([32e0098](https://github.com/Byongho96/three-game-controls/commit/32e00983327aba1b6f35c96acb94076c91980e0f)) +* prevent monsters from running to target while dying ([f12a42c](https://github.com/Byongho96/three-game-controls/commit/f12a42c01618160c61b8135a4b0405ed4912d8e6)) +* remove redundant parameter descriptions from comments ([6f4a210](https://github.com/Byongho96/three-game-controls/commit/6f4a2109553fdff700397b98ebe48b96e28cc2ab)) +* remove unnecessary variable ([b70e57d](https://github.com/Byongho96/three-game-controls/commit/b70e57d4e7461ed36ddc0660d98abc7fa427bc2c)) +* resolve issue where monsters do not disappear from the scene upon death ([50bbaf8](https://github.com/Byongho96/three-game-controls/commit/50bbaf8654ec7007b6766a6a85e8a348179078b9)) +* resolve rendering issue when the gamepad inputsource cannot be found ([#19](https://github.com/Byongho96/three-game-controls/issues/19)) ([5cd1d09](https://github.com/Byongho96/three-game-controls/commit/5cd1d09d8b2fba4d559d680d2f7248b8e594c714)) +* shoot example page enable VR ([#16](https://github.com/Byongho96/three-game-controls/issues/16)) ([0a2c314](https://github.com/Byongho96/three-game-controls/commit/0a2c31468e5097e043053f1d1d60f3b7e98776eb)) +* unify key input types and rename function ([ac2a04d](https://github.com/Byongho96/three-game-controls/commit/ac2a04d46251147acf1940460f6c5528bbc8848f)) +* update conditions for playing idle animation ([25e4f5e](https://github.com/Byongho96/three-game-controls/commit/25e4f5edaca2750b9d1d2215d950a67161920cc6)) +* update rotation direction for third-person keyboard controls ([6886d3d](https://github.com/Byongho96/three-game-controls/commit/6886d3da1e4dd6ff05827aff73b928e249baf101)) + + +### Features + +* add acceleration feature to controls and animations ([#14](https://github.com/Byongho96/three-game-controls/issues/14)) ([33fcf2b](https://github.com/Byongho96/three-game-controls/commit/33fcf2b1618da3239230b95a46fdee080a5b91fb)) +* add camera position Lerp (third person mouse drag controls) ([6a15d02](https://github.com/Byongho96/three-game-controls/commit/6a15d020bbc83e8415138ba4c4e1ff8e439acdd3)) +* add class for managing character animations ([3da9dac](https://github.com/Byongho96/three-game-controls/commit/3da9dacaea8217fc603825dcf10deed0584576e4)) +* add collision detection between user and monster ([1957e71](https://github.com/Byongho96/three-game-controls/commit/1957e7105026bceaca5291d5f5705f983c1ecd05)) +* add collision detection between worldMap and moster ([8163cb9](https://github.com/Byongho96/three-game-controls/commit/8163cb998e51b58068ece6be0a952bc3895edbce)) +* add control features, fix issues, and improve code readability ([75d7903](https://github.com/Byongho96/three-game-controls/commit/75d79035e67425c3cf2a8949b4782606b6f17636)) +* add dispose method to healthbar ([87985d3](https://github.com/Byongho96/three-game-controls/commit/87985d3e81cb522790364c44407992eb9757c298)) +* add example page for third-person pointer lock controls ([a4424d5](https://github.com/Byongho96/three-game-controls/commit/a4424d54df586f177e624e482c5e3b0b7fe7a61f)) +* add jump up, jump down animation ([#17](https://github.com/Byongho96/three-game-controls/issues/17)) ([c798795](https://github.com/Byongho96/three-game-controls/commit/c7987950fad9a5f950ef9657bf9a900a7dc13e4b)) +* add keyboard input parameters for first-person controls ([2edbc17](https://github.com/Byongho96/three-game-controls/commit/2edbc17ec7b617ef82ae13b666b99f0f241570a9)) +* add min and max zoom constraints ([37b6125](https://github.com/Byongho96/three-game-controls/commit/37b61259d3d4c510b5a87c95ceb34feb6b4515db)) +* add monster attack action ([89d2d37](https://github.com/Byongho96/three-game-controls/commit/89d2d37cdf5fd90eeb2d37441edd327534069309)) +* add monster attackCollider ([f8a1226](https://github.com/Byongho96/three-game-controls/commit/f8a1226a7357899f88fe35a6650a7f147f418577)) +* add monster collider on shoot example page ([2477b1c](https://github.com/Byongho96/three-game-controls/commit/2477b1cb8678e0bbccb7091763bbc4f95155a2aa)) +* add monster health bar ([95a3895](https://github.com/Byongho96/three-game-controls/commit/95a38953ef5a0bc539e0df4b083f466daafccc03)) +* add monsterStore ([ce1c780](https://github.com/Byongho96/three-game-controls/commit/ce1c7800e24b1c3418063743d1e4356a4942aa30)) +* add rotation constraints for the controls ([e6fd220](https://github.com/Byongho96/three-game-controls/commit/e6fd2205e30268a7cb17717a117e2c9f0f36ae6b)) +* add separate clamping for camera angles on upward and downward mouse movements ([e086b71](https://github.com/Byongho96/three-game-controls/commit/e086b71596cc6244927c24c1e74081932725216f)) +* add VR camera control functionality ([#18](https://github.com/Byongho96/three-game-controls/issues/18)) ([0bdcae4](https://github.com/Byongho96/three-game-controls/commit/0bdcae4ac61f5f769e29bdbb0577b9d7a1764453)) +* create physics-based character controller with keyboard inputs ([dd917f7](https://github.com/Byongho96/three-game-controls/commit/dd917f787514167b6bccf34d4e15b62ea049ed7d)) +* create PhysicsCharacterControls class ([a73d609](https://github.com/Byongho96/three-game-controls/commit/a73d609ece1099dad28231a30a145e70f3c110ce)) +* create shooting game example pages ([#15](https://github.com/Byongho96/three-game-controls/issues/15)) ([a39b0a2](https://github.com/Byongho96/three-game-controls/commit/a39b0a20f757eb4926ada2494f8e472200ed5932)) +* first person mouse drag controls ([#7](https://github.com/Byongho96/three-game-controls/issues/7)) ([4394591](https://github.com/Byongho96/three-game-controls/commit/43945919a1757ff4e8f72bb61952afdb7ffbb85c)) +* first person pointerlock controls ([37e15de](https://github.com/Byongho96/three-game-controls/commit/37e15de7c7adf0086d7cc2a507413876d22d25ed)) +* implement gun change and shooting logic with damage handling ([#21](https://github.com/Byongho96/three-game-controls/issues/21)) ([e1654fd](https://github.com/Byongho96/three-game-controls/commit/e1654fddfe4733590efc1bfdb9467ee252d3199a)) +* implement physics controller using octree structure ([edcebae](https://github.com/Byongho96/three-game-controls/commit/edcebae51b86fd00cee82da1b8c4570b4c19b23a)) +* implement precise landing detection for jumpDown execution ([#31](https://github.com/Byongho96/three-game-controls/issues/31)) ([226241c](https://github.com/Byongho96/three-game-controls/commit/226241c8dea307865017f3fc379e2672f764ecef)) +* implement third-person pointer lock controls ([0e887db](https://github.com/Byongho96/three-game-controls/commit/0e887db80d6e6199107c268594cf645b8248b8f0)) +* implement x-axis rotation clamp ([be6f1ac](https://github.com/Byongho96/three-game-controls/commit/be6f1ac461c5ddd321769aeef0c9bef58f32521e)) +* improve PhysicsControls functionality ([1be9af5](https://github.com/Byongho96/three-game-controls/commit/1be9af5c151772b9c90b325cda7c2c00bab9aa45)) +* monster attack ([7262f48](https://github.com/Byongho96/three-game-controls/commit/7262f48cfdc76a09b3bd4d0a68496c1ecebf57f3)) +* moster hit action ([bd364ea](https://github.com/Byongho96/three-game-controls/commit/bd364eaa4f5c8daa5fb0fa41a0da9e607520f694)) +* remove unnecessary getContext, replace hardcoded width and height ([e0aaec0](https://github.com/Byongho96/three-game-controls/commit/e0aaec07604e374bdf1630d1b5605551d2991695)) +* temp ([a9471a4](https://github.com/Byongho96/three-game-controls/commit/a9471a46b9c4e5b17084c265e06067abf87d6d3d)) diff --git a/README.md b/README.md index 9384f13..25432b8 100644 --- a/README.md +++ b/README.md @@ -166,8 +166,8 @@ Promise.all( [ gltfLoader.loadAsync( 'collision-world.glb' ), gltfLoader.loadAsync( 'x-bot.glb' ), gltfLoader.loadAsync( 'ani-idle.glb' ), - gltfLoader.loadAsync( 'run_forward.glb' ), gltfLoader.loadAsync( 'ani-forward.glb' ), + gltfLoader.loadAsync( 'ani-forward-fast.glb' ), gltfLoader.loadAsync( 'ani-jump-up.glb' ), gltfLoader.loadAsync( 'ani-land.glb' ), gltfLoader.loadAsync( 'ani-fall.glb' ) diff --git a/package-lock.json b/package-lock.json index 4d054c4..7402626 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "three-game-controls", - "version": "0.0.0", + "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "three-game-controls", - "version": "0.0.0", + "version": "1.0.0", "license": "MIT", "devDependencies": { "@babel/core": "^7.25.7", diff --git a/package.json b/package.json index 88829f4..ffb112b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "three-game-controls", - "version": "0.0.0", + "version": "1.0.0", "description": "game controls for three.js", "main": "dist/bundle.cjs.js", "module": "dist/bundle.esm.js", diff --git a/public/docs/api/core/PhysicsControls.html b/public/docs/api/core/PhysicsControls.html index 1ecbd6f..a60fcf7 100644 --- a/public/docs/api/core/PhysicsControls.html +++ b/public/docs/api/core/PhysicsControls.html @@ -55,7 +55,7 @@

[property:Number landTimeThreshold]

[property:Number landTolerance]

- The distance tolerance for landing detection, measured as the vertical distance from the ground surface. Default is `0.2`. + The distance tolerance for landing detection, measured as the vertical distance from the ground surface. Default is `0.6`.

[property:Number maxFallSpeed]

diff --git a/public/docs/api/first-person/FPKeyDragControls.html b/public/docs/api/first-person/FPKeyDragControls.html index 0b7c46f..7c4e189 100644 --- a/public/docs/api/first-person/FPKeyDragControls.html +++ b/public/docs/api/first-person/FPKeyDragControls.html @@ -33,8 +33,7 @@

Mixins

[page:KeyboardMixin KeyboardMixin]

- Provide Keyboard API for controlling the object.
- `keyToActions` is overwritten: + Provide Keyboard API for controlling the object.

[page:DragMixin DragMixin]

diff --git a/public/docs/api/first-person/FPKeyPointerLockControls.html b/public/docs/api/first-person/FPKeyPointerLockControls.html index bca5259..ddeb22e 100644 --- a/public/docs/api/first-person/FPKeyPointerLockControls.html +++ b/public/docs/api/first-person/FPKeyPointerLockControls.html @@ -36,8 +36,7 @@

Mixins

[page:KeyboardMixin KeyboardMixin]

- Provide Keyboard API for controlling the object.
- `keyToActions` is overwritten: + Provide Keyboard API for controlling the object.

[page:PointerLockMixin PointerLockMixin]

diff --git a/public/docs/api/mixins/GyroMixin.html b/public/docs/api/mixins/GyroMixin.html new file mode 100644 index 0000000..ca6d1a0 --- /dev/null +++ b/public/docs/api/mixins/GyroMixin.html @@ -0,0 +1,97 @@ + + + + + + + + + +

[name]

+ +

+ A mixin that extends `FirstPersonControls` to add device orientation-based controls. This mixin uses device gyroscope data to control actions along the X, Y, and Z axes, enabling motion-based interactions. +

+ +

Constructor

+ +

[name]( [param:FirstPersonControls base] )

+

+ [page:FirstPersonControls base] — A class that extends `FirstPersonControls`. +

+ +

Properties

+ +

[property:Array gyroXActions]

+

+ An array of actions triggered by gyroscope movement along the X-axis. Default is `['MOVE_BACKWARD']`. +

+ +

[property:Number gyroXOffset]

+

+ Reference value for the gyroscope's X-axis orientation. Default: `0`. +

+ +

[property:Number gyroXMultiplier]

+

+ The multiplier for the x-axis orientation. Default: `0.1`. +

+ +

[property:Array gyroYActions]

+

+ An array of actions triggered by gyroscope movement along the Y-axis. Default is `['MOVE_RIGHTWARD']`. +

+ +

[property:Number gyroYOffset]

+

+ Reference value for the gyroscope's Y-axis orientation. Default: `0`. +

+ +

[property:Number gyroYMultiplier]

+

+ The multiplier for the y-axis orientation. Default: `0.1`. +

+ +

[property:Array gyroZActions]

+

+ An array of actions triggered by gyroscope movement along the Z-axis. Default is `[]`. +

+ +

[property:Number gyroZOffset]

+

+ Reference value for the gyroscope's Z-axis orientation. Default: `0`. +

+ + +

[property:Number gyroZMultiplier]

+

+ The multiplier for the z-axis orientation. Default: `0.1`. +

+ +

[property:Boolean needsResetOrientation]

+

+ Determines whether the reference values for orientation (`gyroXOffset`, `gyroYOffset`, `gyroZOffset`) should be reset on the next device orientation event. Default: `true`. +

+ +

Methods

+ +

[method:void connect]()

+

+ Connects the drag functionality by adding event listeners. +

+ +

[method:void disconnect]()

+

+ Disconnects the drag functionality by removing event listeners. +

+ +

[method:void dispose]()

+

+ Disposes of the drag functionality by cleaning up event listeners and resources. +

+ +

Source

+ +

[link:https://github.com/Byongho96/three-game-controls/blob/master/src/mixins/GyroMixin.ts mixins/GyroMixin.ts]

+ + diff --git a/public/docs/api/third-person/TPKeyDragControls.html b/public/docs/api/third-person/TPKeyDragControls.html index 9598e9e..a6a7578 100644 --- a/public/docs/api/third-person/TPKeyDragControls.html +++ b/public/docs/api/third-person/TPKeyDragControls.html @@ -48,10 +48,9 @@

Mixins

[page:KeyboardMixin KeyboardMixin]

- Provide Keyboard API for controlling the object.
- `keyToActions` is overwritten: + Provide Keyboard API for controlling the object.

- +

[page:DragMixin DragMixin]

diff --git a/public/docs/api/third-person/TPKeyPointerLockControls.html b/public/docs/api/third-person/TPKeyPointerLockControls.html index 6b07172..368ec1f 100644 --- a/public/docs/api/third-person/TPKeyPointerLockControls.html +++ b/public/docs/api/third-person/TPKeyPointerLockControls.html @@ -48,8 +48,7 @@

Mixins

[page:KeyboardMixin KeyboardMixin]

- Provide Keyboard API for controlling the object.
- `keyToActions` is overwritten: + Provide Keyboard API for controlling the object.

[page:PointerLockMixin PointerLockMixin]

diff --git a/public/docs/list.json b/public/docs/list.json index 3876ffc..33d9708 100644 --- a/public/docs/list.json +++ b/public/docs/list.json @@ -37,6 +37,7 @@ "Mixins": { "DragMixin": "api/mixins/DragMixin", + "GyroMixin": "api/mixins/GyroMixin", "KeyboardMixin": "api/mixins/KeyboardMixin", "PointerLockMixin": "api/mixins/PointerLockMixin" } diff --git a/public/docs/manual/Quick-Start.html b/public/docs/manual/Quick-Start.html index a1a6d44..fbc8a4a 100644 --- a/public/docs/manual/Quick-Start.html +++ b/public/docs/manual/Quick-Start.html @@ -153,8 +153,8 @@

ThirdPersonControls

gltfLoader.loadAsync( 'collision-world.glb' ), gltfLoader.loadAsync( 'x-bot.glb' ), gltfLoader.loadAsync( 'ani-idle.glb' ), - gltfLoader.loadAsync( 'run_forward.glb' ), gltfLoader.loadAsync( 'ani-forward.glb' ), + gltfLoader.loadAsync( 'ani-forward-fast.glb' ), gltfLoader.loadAsync( 'ani-jump-up.glb' ), gltfLoader.loadAsync( 'ani-land.glb' ), gltfLoader.loadAsync( 'ani-fall.glb' ) diff --git a/public/examples/files.json b/public/examples/files.json index 7157a9b..e1385da 100644 --- a/public/examples/files.json +++ b/public/examples/files.json @@ -8,6 +8,7 @@ "ThirdPerson": [ "third-person_keyboard_controls", "third-person_mousedrag_controls", - "third-person_pointerlock_controls" + "third-person_pointerlock_controls", + "third-person_gyroscope_mixins" ] } diff --git a/public/examples/first-person_keyboard_controls.html b/public/examples/first-person_keyboard_controls.html index ae40680..266396f 100644 --- a/public/examples/first-person_keyboard_controls.html +++ b/public/examples/first-person_keyboard_controls.html @@ -18,7 +18,7 @@ "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.168.0/build/three.module.js", "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.168.0/examples/jsm/", - "three-game-controls": "https://cdn.jsdelivr.net/npm/three-game-controls/dist/bundle.esm.js" + "three-game-controls": "../../dist/bundle.esm.js" } } diff --git a/public/examples/first-person_mousedrag_controls.html b/public/examples/first-person_mousedrag_controls.html index 8f9a65b..51fd759 100644 --- a/public/examples/first-person_mousedrag_controls.html +++ b/public/examples/first-person_mousedrag_controls.html @@ -18,7 +18,7 @@ "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.168.0/build/three.module.js", "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.168.0/examples/jsm/", - "three-game-controls": "https://cdn.jsdelivr.net/npm/three-game-controls/dist/bundle.esm.js" + "three-game-controls": "../../dist/bundle.esm.js" } } diff --git a/public/examples/first-person_pointerlock_controls.html b/public/examples/first-person_pointerlock_controls.html index 9e9a970..9d02376 100644 --- a/public/examples/first-person_pointerlock_controls.html +++ b/public/examples/first-person_pointerlock_controls.html @@ -18,7 +18,7 @@ "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.168.0/build/three.module.js", "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.168.0/examples/jsm/", - "three-game-controls": "https://cdn.jsdelivr.net/npm/three-game-controls/dist/bundle.esm.js" + "three-game-controls": "../../dist/bundle.esm.js" } } diff --git a/public/examples/screenshots/third-person_gyroscope_mixins.jpg b/public/examples/screenshots/third-person_gyroscope_mixins.jpg new file mode 100644 index 0000000..dd9ccd8 Binary files /dev/null and b/public/examples/screenshots/third-person_gyroscope_mixins.jpg differ diff --git a/public/examples/shoot/first-person_pointerlock_shooting.html b/public/examples/shoot/first-person_pointerlock_shooting.html index 1a048dd..b70011e 100644 --- a/public/examples/shoot/first-person_pointerlock_shooting.html +++ b/public/examples/shoot/first-person_pointerlock_shooting.html @@ -5,6 +5,7 @@ +
Kill the monsters by clicking on them
@@ -16,7 +17,7 @@ "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.168.0/build/three.module.js", "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.168.0/examples/jsm/", - "three-game-controls": "https://cdn.jsdelivr.net/npm/three-game-controls/dist/bundle.esm.js" + "three-game-controls": "../../../dist/bundle.esm.js" } } diff --git a/public/examples/shoot/first-person_pointerlock_shooting_vr.html b/public/examples/shoot/first-person_pointerlock_shooting_vr.html index c35fbde..f5e5797 100644 --- a/public/examples/shoot/first-person_pointerlock_shooting_vr.html +++ b/public/examples/shoot/first-person_pointerlock_shooting_vr.html @@ -5,6 +5,7 @@ +
Kill the monsters by clicking on them
@@ -16,7 +17,7 @@ "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.168.0/build/three.module.js", "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.168.0/examples/jsm/", - "three-game-controls": "https://cdn.jsdelivr.net/npm/three-game-controls/dist/bundle.esm.js" + "three-game-controls": "../../../dist/bundle.esm.js" } } diff --git a/public/examples/shoot/styles.css b/public/examples/shoot/styles.css new file mode 100644 index 0000000..fc9ffd5 --- /dev/null +++ b/public/examples/shoot/styles.css @@ -0,0 +1,29 @@ +#cross { + position: fixed; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + width: 50px; + height: 50px; + pointer-events: none; +} +#cross::before, +#cross::after { + content: ''; + position: absolute; + background-color: rgba(68, 68, 68, 0.61); +} +#cross::before { + top: 0; + left: 50%; + transform: translateX(-50%); + width: 4px; + height: 100%; +} +#cross::after { + top: 50%; + left: 0; + transform: translateY(-50%); + width: 100%; + height: 4px; +} diff --git a/public/examples/third-person_gyroscope_mixins.html b/public/examples/third-person_gyroscope_mixins.html new file mode 100644 index 0000000..b5ac5a2 --- /dev/null +++ b/public/examples/third-person_gyroscope_mixins.html @@ -0,0 +1,299 @@ + + + + Third-person gyroscope control + + + + + +
+ Third-person gyroscope controls demo
+ Tilt your device to move the character
+ ( ios 13+: Click the screen to grant DeviceOrientationEvent )
+ +
+
+ + + + + diff --git a/public/examples/third-person_keyboard_controls.html b/public/examples/third-person_keyboard_controls.html index bdc9871..b19cd36 100644 --- a/public/examples/third-person_keyboard_controls.html +++ b/public/examples/third-person_keyboard_controls.html @@ -18,7 +18,7 @@ "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.168.0/build/three.module.js", "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.168.0/examples/jsm/", - "three-game-controls": "https://cdn.jsdelivr.net/npm/three-game-controls/dist/bundle.esm.js" + "three-game-controls": "../../dist/bundle.esm.js" } } diff --git a/public/examples/third-person_mousedrag_controls.html b/public/examples/third-person_mousedrag_controls.html index fd3f706..0ec22bf 100644 --- a/public/examples/third-person_mousedrag_controls.html +++ b/public/examples/third-person_mousedrag_controls.html @@ -18,7 +18,7 @@ "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.168.0/build/three.module.js", "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.168.0/examples/jsm/", - "three-game-controls": "https://cdn.jsdelivr.net/npm/three-game-controls/dist/bundle.esm.js" + "three-game-controls": "../../dist/bundle.esm.js" } } diff --git a/public/examples/third-person_pointerlock_controls.html b/public/examples/third-person_pointerlock_controls.html index ffd441c..a458146 100644 --- a/public/examples/third-person_pointerlock_controls.html +++ b/public/examples/third-person_pointerlock_controls.html @@ -18,7 +18,7 @@ "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.168.0/build/three.module.js", "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.168.0/examples/jsm/", - "three-game-controls": "https://cdn.jsdelivr.net/npm/three-game-controls/dist/bundle.esm.js" + "three-game-controls": "../../dist/bundle.esm.js" } } diff --git a/src/controls/core/PhysicsControls.ts b/src/controls/core/PhysicsControls.ts index d79f4e4..1d18c32 100644 --- a/src/controls/core/PhysicsControls.ts +++ b/src/controls/core/PhysicsControls.ts @@ -50,9 +50,9 @@ class PhysicsControls extends Controls { landTimeThreshold: number = 0.3; /** Distance tolerance for landing detection. - * @default 0.2 + * @default 0.6 */ - landTolerance: number = 0.2; + landTolerance: number = 0.6; /** Collider for the object. * @default `new Collider()` @@ -189,7 +189,7 @@ class PhysicsControls extends Controls { if ( collisionResult.depth >= 1e-10 ) { - this.collider.translate( collisionResult.normal.multiplyScalar( collisionResult.depth ) ); + this.collider.translate( collisionResult.normal.multiplyScalar( collisionResult.depth * 1.01 ) ); this.dispatchEvent( { ..._collideEvent, normal: collisionResult.normal.normalize() as Vector3 } ); } diff --git a/src/index.ts b/src/index.ts index dc3d063..6d80931 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,3 +17,4 @@ export * from './math/Collider'; export * from './mixins/DragMixin'; export * from './mixins/KeyboardMixin'; export * from './mixins/PointerLockMixin'; +export * from './mixins/GyroMixin'; diff --git a/src/mixins/GyroMixin.ts b/src/mixins/GyroMixin.ts new file mode 100644 index 0000000..d7b01d2 --- /dev/null +++ b/src/mixins/GyroMixin.ts @@ -0,0 +1,222 @@ +import { type FirstPersonControls, type Action } from '../controls/core/FirstPersonControls'; + +export interface HasGyroMethod { + gyroXActions: Action[], + gyroYActions: Action[], + gyroZActions: Action[], + + gyroXOffset: number, + gyroYOffset: number, + gyroZOffset: number, + + gyroXMultiplier: number, + gyroYMultiplier: number, + gyroZMultiplier: number, +} + +function GyroMixin>( Base: T ): Constructor & T { + + return class GyroMixin extends Base { + + /** + * The actions to be performed when gyro is accelerated along the x-axis. + * @default [ 'MOVE_BACKWARD' ] + */ + gyroXActions: Action[] = [ 'MOVE_BACKWARD' ]; + + /** + * The reference value of x-axis orientation. + * @default 0 + */ + gyroXOffset: number = 0; + + /** + * The multiplier for the x-axis orientation. + * @default 0.1 + */ + gyroXMultiplier: number = 0.1; + + /** + * The actions to be performed when gyro is accelerated along the y-axis. + * @default [ 'MOVE_RIGHTWARD' ] + */ + gyroYActions: Action[] = [ 'MOVE_RIGHTWARD' ]; + + /** + * The reference value of y-axis orientation. + * @default 0 + */ + gyroYOffset: number = 0; + + /** + * The multiplier for the y-axis orientation. + * @default 0.1 + */ + gyroYMultiplier: number = 0.1; + + /** + * The actions to be performed when gyro is accelerated along the z-axis. + * @default [ ] + */ + gyroZActions: Action[] = [ ]; + + /** + * The reference value of z-axis orientation. + * @default 0 + */ + gyroZOffset: number = 0; + + /** + * The multiplier for the z-axis orientation. + * @default 0.1 + */ + gyroZMultiplier: number = 0.1; + + /** + * Whether the reset the reference values for orientation (gyroXOffset, gyroYOffset, gyroZOffset) on the next device orientation event. + */ + needsResetOrientation: boolean = true; + + // Internals + private _bindOnDeviceOrientation: ( event: DeviceOrientationEvent ) => void; + private _bindRequestDeviceOrientationEvent: () => void; + + + constructor( ...args: any[] ) { + + super( ...args ); + + this._bindOnDeviceOrientation = this._onDeviceOrientation.bind( this ); + this._bindRequestDeviceOrientationEvent = this._requestDeviceOrientationEvent.bind( this ); + + this.connect(); + + } + + /** + * Connects the controls. + */ + connect(): void { + + super.connect(); + + if ( ! ( 'DeviceOrientationEvent' in window ) ) { + + console.warn( 'DeviceOrientationEvent is not supported' ); + return; + + } + + if ( 'requestPermission' in DeviceOrientationEvent && typeof DeviceOrientationEvent.requestPermission === 'function' ) { // https://stackoverflow.com/questions/56514116/how-do-i-get-deviceorientationevent-and-devicemotionevent-to-work-on-safari + + // ios 13+ + this.domElement?.addEventListener( 'click', this._bindRequestDeviceOrientationEvent ); + + } else { + + // non-ios + window.addEventListener( 'deviceorientation', this._bindOnDeviceOrientation ); + + } + + } + + /** + * Disconnects the controls. + */ + disconnect(): void { + + super.disconnect(); + + this.domElement?.removeEventListener( 'click', this._bindRequestDeviceOrientationEvent ); + window.removeEventListener( 'deviceorientation', this._bindOnDeviceOrientation ); + + } + + /** + * Disposes the controls. + */ + dispose(): void { + + super.dispose(); + + this.disconnect(); + + } + + // Handles the device orientation event and updates the action states. + protected _onDeviceOrientation( event: DeviceOrientationEvent ): void { + + let { alpha, beta, gamma } = event; + + + if ( alpha === null || beta === null || gamma === null ) return; + + if ( this.needsResetOrientation ) { + + this.gyroXOffset = beta; + this.gyroYOffset = gamma; + this.gyroZOffset = alpha; + + this.needsResetOrientation = false; + + } + + beta = ( beta - this.gyroXOffset ) * this.gyroXMultiplier; + gamma = ( gamma - this.gyroYOffset ) * this.gyroYMultiplier; + alpha = ( alpha - this.gyroZOffset ) * this.gyroZMultiplier; + + this.gyroXActions.forEach( action => { + + this.actionStates[ action ] = beta; + + } ); + + this.gyroYActions.forEach( action => { + + this.actionStates[ action ] = gamma; + + } ); + + this.gyroZActions.forEach( action => { + + this.actionStates[ action ] = alpha; + + } ); + + } + + // Requests the device orientation event permission. (ios 13+) + protected _requestDeviceOrientationEvent(): void { + + if ( 'requestPermission' in DeviceOrientationEvent && typeof DeviceOrientationEvent.requestPermission === 'function' ) { + + DeviceOrientationEvent.requestPermission() + .then( ( permissionState: any ) => { + + if ( permissionState === 'granted' ) { + + window.addEventListener( 'deviceorientation', this._bindOnDeviceOrientation ); + + } else { + + console.warn( 'DeviceOrientationEvent.requestPermission() was not granted' ); + + } + + } ) + .catch( ( error: any ) => { + + console.error( error ); + + } ); + + } + + } + + }; + +} + +export { GyroMixin };