-
Notifications
You must be signed in to change notification settings - Fork 97
/
virtual-reality-phaser.html
122 lines (102 loc) · 4.37 KB
/
virtual-reality-phaser.html
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Virtual Reality (with Phaser)</title>
<link rel="stylesheet" href="/css/examples.css?ver=1.0.0" />
<script src="/js/examples.js?ver=1.1.1"></script>
<!-- https://github.com/immersive-web/webxr-polyfill -->
<script src="https://cdn.jsdelivr.net/npm/webxr-polyfill@latest/build/webxr-polyfill.js"></script>
<script src="/lib/phaser.min.js?ver=3.52.0"></script>
<script src="/lib/enable3d/enable3d.phaserExtension.0.25.4.min.js"></script>
</head>
<body>
<div id="info-text">Get your Phone, click Enter VR, tab on the screen and look around.</div>
<script>
const { enable3d, Scene3D, Canvas, THREE } = ENABLE3D
class MainScene extends Scene3D {
move = false
constructor() {
super({ key: 'MainScene' })
}
init() {
this.accessThirdDimension({ enableXR: true, antialias: false })
}
// The normal Phaser update method will not run while you are in WebXR mode.
update() {}
// Use updateXR as update loop in WebXR mode.
updateXR() {
if (this.third.webXR.isPresenting && this.move) {
// move 0.02 units toward view
const v3 = new THREE.Vector3()
const distance = 0.02
this.third.renderer.xr.getCamera().getWorldDirection(v3)
const pos = v3.multiplyScalar(distance)
// move xr camera
const offsetPosition = new THREE.Quaternion(-pos.x, -pos.y, -pos.z, 1)
const offsetRotation = new THREE.Quaternion()
const transform = new XRRigidTransform(offsetPosition, offsetRotation)
const teleportSpaceOffset = this.third.renderer.xr.getReferenceSpace()?.getOffsetReferenceSpace(transform)
if (teleportSpaceOffset) this.third.renderer.xr.setReferenceSpace(teleportSpaceOffset)
}
}
create() {
this.third.warpSpeed('-orbitControls')
// 2d element will not be visible in WebXR mode.
// const rect = this.add.rectangle(50, 50, 10, 10, 0xff00ff)
const controller = this.third.webXR.getController(0)
// will be available in the next version
const controllerGrip = this.third.webXR.getControllerGrip ? this.third.webXR.getControllerGrip(0) : null
setInterval(() => {
const x = Math.random() * 10 - 5
const z = Math.random() * 10 - 5
const ball = this.third.physics.add.sphere({ y: 50, x, z, radius: 0.5 }, { lambert: { color: 'red' } })
ball.body.setBounciness(0.7)
setTimeout(() => {
this.third.destroy(ball)
}, 20000)
}, 2000)
const onSelectStart = () => {
this.move = true
}
const onSelectEnd = () => {
this.move = false
}
controller.addEventListener('select', () => {})
controller.addEventListener('selectstart', onSelectStart)
controller.addEventListener('selectend', onSelectEnd)
controller.addEventListener('squeeze', () => {})
controller.addEventListener('squeezestart', onSelectStart)
controller.addEventListener('squeezeend', onSelectEnd)
controller.addEventListener('connected', event => {
const ray = this.third.webXR.getControllerRay(event.data)
if (ray) controller.add(ray)
})
controller.addEventListener('disconnected', () => {
controller.remove(controller.children[0])
})
}
}
const config = {
type: Phaser.WEBGL,
transparent: true,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
width: window.innerWidth * Math.max(1, window.devicePixelRatio / 2),
height: window.innerHeight * Math.max(1, window.devicePixelRatio / 2)
},
scene: [MainScene],
...Canvas({ antialias: true })
}
window.addEventListener('load', () => {
enable3d(() => new Phaser.Game(config)).withPhysics('/lib/ammo/kripken')
})
</script>
</body>
</html>