-
Notifications
You must be signed in to change notification settings - Fork 73
Pointerlocks #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Myrmod
wants to merge
6
commits into
Rich-Harris:main
Choose a base branch
from
Myrmod:pointerlocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Pointerlocks #37
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4b84be3
add first try on pointerlockcontrols
Myrmod 14a6d0a
update pointerlockcontrols
Myrmod 2c5cc57
add basic pointer lock controls
Myrmod 358d37f
add preview image
Myrmod 50991ea
Add placeholder for pointerlockcontrols in docs
Myrmod 4999dfb
add keyboard movement to pointerlockcontrols
Myrmod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <script> | ||
| import { setup } from '../../utils/context.js'; | ||
| import { PointerLockControls } from 'three/examples/jsm/controls/PointerLockControls.js'; | ||
| import { createEventDispatcher } from 'svelte'; | ||
|
|
||
| export let maxPolarAngle = Math.PI; | ||
| export let minPolarAngle = 0; | ||
|
|
||
| let isLocked = false; | ||
|
|
||
| const { root } = setup(); | ||
| const dispatch = createEventDispatcher(); | ||
|
|
||
| /** @type {PointerLockControls} */ | ||
| let controls; | ||
|
|
||
| root.controls.set((camera, canvas) => { | ||
|
|
||
| controls = new PointerLockControls(camera, canvas); | ||
|
|
||
| controls.addEventListener('start', (e) => { | ||
| dispatch('start', e); | ||
| }); | ||
|
|
||
| controls.addEventListener('end', (e) => { | ||
| dispatch('end', e); | ||
| }); | ||
|
|
||
| controls.addEventListener('lock', (e) => { | ||
| isLocked = true; | ||
| dispatch('lock', e); | ||
| }); | ||
|
|
||
| controls.addEventListener('unlock', (e) => { | ||
| isLocked = false; | ||
| dispatch('unlock', e); | ||
| }); | ||
|
|
||
| // allows rotating the camera | ||
| controls.addEventListener('change', (e) => { | ||
| dispatch('change', e); | ||
|
|
||
| root.invalidate(); | ||
| }); | ||
|
|
||
| canvas.addEventListener('click', () => { | ||
| controls.lock(); | ||
| }); | ||
|
|
||
| return controls; | ||
| }); | ||
|
|
||
| $: if (controls) { | ||
| controls.isLocked = isLocked; | ||
| controls.maxPolarAngle = maxPolarAngle; | ||
| controls.minPolarAngle = minPolarAngle; | ||
| } | ||
| </script> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/routes/examples/_content/03-first-person-camera/index.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <script> | ||
| import * as THREE from 'three'; | ||
| import * as SC from 'svelte-cubed'; | ||
|
|
||
| let floor | ||
| let cube | ||
| </script> | ||
|
|
||
| <SC.Canvas | ||
| antialias | ||
| background={new THREE.Color('papayawhip')} | ||
| fog={new THREE.FogExp2('papayawhip', 0.1)} | ||
| shadows | ||
| > | ||
| <SC.Group position={[0, -1 / 2, 0]}> | ||
| <SC.Mesh | ||
| bind:this={floor} | ||
| geometry={new THREE.PlaneGeometry(50, 50)} | ||
| material={new THREE.MeshStandardMaterial({ color: 'burlywood' })} | ||
| rotation={[-Math.PI / 2, 0, 0]} | ||
| receiveShadow | ||
| /> | ||
| <SC.Primitive | ||
| object={new THREE.GridHelper(50, 50, 'papayawhip', 'papayawhip')} | ||
| position={[0, 0.001, 0]} | ||
| /> | ||
| </SC.Group> | ||
|
|
||
| <SC.Mesh | ||
| bind:this={cube} | ||
| geometry={new THREE.BoxGeometry()} | ||
| material={new THREE.MeshStandardMaterial({ color: 0xff3e00 })} | ||
| scale={[1, 1, 1]} | ||
| castShadow | ||
| /> | ||
|
|
||
| <SC.PerspectiveCamera position={[1, 1, 3]} /> | ||
| <SC.PointerLockControls /> | ||
| <SC.AmbientLight intensity={0.6} /> | ||
| <SC.DirectionalLight intensity={0.6} position={[-2, 3, 2]} shadow={{ mapSize: [2048, 2048] }} /> | ||
| </SC.Canvas> |
3 changes: 3 additions & 0 deletions
3
src/routes/examples/_content/03-first-person-camera/meta.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "title": "First Person Controls" | ||
| } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should remove all event listeners onDestroy https://svelte.dev/docs#onDestroy and also reset camera properties if needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it's not necessary, because the event listenders should get removed automatically as soon as the component is unmounted, because the object, on which the event listenters have been added to, doesn't exist any more.
If they really stay there I totally agree, that they should be removed.
In what case would you reset the camera properties? Either I am too sleepy or I haven't changed any camera properties in the current pull request?