-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
208 lines (167 loc) · 6.62 KB
/
index.ts
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/// <reference path="webxr.d.ts" />
import * as pc from "playcanvas";
import {startApplication} from "./__start__";
import {createLoadingScreen} from "./__loading__";
import {WebXRButton} from "./webxr-button";
// give game scripts access to playcanvas namespace
(window as any).pc = pc;
const options = {
ASSET_PREFIX: "",
SCRIPT_PREFIX: "",
SCENE_PATH: "802005.json",
CONTEXT_OPTIONS: {
'antialias': true,
'alpha': false,
'preserveDrawingBuffer': false,
'preferWebGl2': true,
'xrCompatible': true
},
SCRIPTS: [ ],
CONFIG_FILENAME: "config.json",
INPUT_SETTINGS: {
useKeyboard: true,
useMouse: true,
useGamepads: false,
useTouch: true
},
PRELOAD_MODULES: [
],
}
let app = startApplication(options)!;
pc.script.createLoadingScreen(app => createLoadingScreen(app, options));
function onSessionStarted(session: XRSession) {
session.addEventListener('end', onSessionEnded);
if ("updateWorldTrackingState" in session) {
(session as any).updateWorldTrackingState({
planeDetectionState : {
enabled : true
}
});
}
let gl = (app.graphicsDevice as any).gl as WebGLRenderingContext;
let baseLayer = new XRWebGLLayer(session, gl);
session.updateRenderState({ baseLayer });
let renderTarget = new pc.RenderTarget({});
app.scene.layers.getLayerById(pc.LAYERID_WORLD).renderTarget = renderTarget;
app.scene.layers.getLayerById(pc.LAYERID_IMMEDIATE).renderTarget = renderTarget;
xrRenderTarget = renderTarget as any;
xrRenderTarget._glFrameBuffer = baseLayer.framebuffer;
xrCameraEntity = app.root.findOne(node => 'camera' in node) as pc.Entity;
session.requestReferenceSpace('local').then(refSpace => xrLocalRefSpace = refSpace);
xrCameraEntity.camera!.calculateProjection = (matrix) => {
if (xrCameraView) matrix.set(xrCameraView.projectionMatrix as any);
};
app.autoRender = false;
session.requestAnimationFrame(onXRFrame);
session.addEventListener('selectstart', onTouchStart);
session.addEventListener('selectend', onTouchEnd);
}
function onXRFrame(timestamp: number, frame: XRFrame) {
let session = frame.session;
let cameraPose = xrLocalRefSpace && frame.getViewerPose(xrLocalRefSpace);
if (cameraPose) {
let cameraTransform = cameraPose.transform;
let pos = cameraTransform.position;
let rot = cameraTransform.orientation;
xrCameraEntity.setPosition(pos.x, pos.y, pos.z);
xrCameraEntity.setRotation(rot.x, rot.y, rot.z, rot.w);
xrCameraView = cameraPose.views[0];
let viewport = session.renderState.baseLayer!.getViewport(xrCameraView);
xrRenderTarget._colorBuffer = viewport;
renderPlanes(frame);
app.render();
}
session.requestAnimationFrame(onXRFrame);
}
function renderPlanes(frame: XRFrame) {
if ("worldInformation" in frame) {
let worldInformation = (frame as any).worldInformation;
if ("detectedPlanes" in worldInformation) {
let detectedPlanes = worldInformation.detectedPlanes;
let floorY = Number.POSITIVE_INFINITY;
detectedPlanes.forEach(plane => {
let planeTransform = (frame as XRFrame).getPose(plane.planeSpace, xrLocalRefSpace)!.transform;
let planeVertices = plane.polygon as pc.Vec3[];
renderPlane(planeVertices, planeTransform);
if (plane.orientation === "Horizontal") {
let planeY = planeTransform.position.y;
if (floorY > planeY) floorY = planeTransform.position.y;
}
});
if (floorY !== Number.POSITIVE_INFINITY) {
let floorEntity = app.root.findByName("Floor") as pc.Entity;
floorEntity.rigidbody!.teleport(0, floorY - 0.5, 0);
}
}
}
}
function renderPlane(planeVertices: pc.Vec3[], planeTransform: XRRigidTransform) {
let worldPos = planeTransform.position as any;
let worldRot = planeTransform.orientation as any;
let worldTransform = new pc.Mat4().setTRS(worldPos, worldRot, pc.Vec3.ONE);
let worldVertices = planeVertices.map(vertex => worldTransform.transformPoint(vertex));
let lines = worldVertices.reduce<pc.Vec3[]>((lines, currentVertex, i, vertices) => {
var previousVertex = i > 0 ? vertices[i - 1] : vertices[vertices.length - 1];
lines.push(previousVertex);
lines.push(currentVertex);
return lines;
}, []);
let color = new pc.Color(0, 1, 0) as any;
app.renderLines(lines, color);
}
function onEndSession(session) {
session.end();
}
function onSessionEnded(event) {
xrButton.setSession(null);
}
function initXR() {
xrButton = new WebXRButton({
onRequestSession: onRequestSession,
onEndSession: onEndSession,
textEnterXRTitle: "START AR",
textXRNotFoundTitle: "AR NOT FOUND",
textExitXRTitle: "EXIT AR",
});
if (navigator.xr) {
// Checks to ensure that environment integration (AR) is available,
// and only enables the button if so.
navigator.xr.isSessionSupported('immersive-ar').then(() => {
xrButton.enabled = true;
});
}
document.getElementById('ar-button')!.appendChild(xrButton.domElement);
let touch = app.touch;
if (touch) {
touch.on(pc.EVENT_TOUCHSTART, onTouchStart);
touch.on(pc.EVENT_TOUCHEND, onTouchEnd);
}
}
function onRequestSession() {
// Requests an inline (non-immersive) session with environment integration
// to get AR via video passthrough.
// Even though this is a non-immersive session, the fact that it's
// using environment integration means it must be requested in a user
// activation event so that appropriate permissions can be granted.
// This will likely prompt the user to allow camera use, so the promise
// may remain outstanding for a while.
navigator.xr!.requestSession('immersive-ar')
.then((session) => {
xrButton.setSession(session);
onSessionStarted(session);
});
}
function onTouchStart() {
let entity = app.root.findOne(node => node.name === 'LetItSnow') as pc.Entity;
entity.enabled = true;
}
function onTouchEnd() {
let entity = app.root.findOne(node => node.name === 'LetItSnow') as pc.Entity;
entity.enabled = false;
}
let xrCameraView: XRView;
let xrCameraEntity: pc.Entity;
let xrRenderTarget: { _glFrameBuffer: WebGLFramebuffer, _colorBuffer?: XRViewport };
let xrLocalRefSpace: XRReferenceSpace;
let xrButton: WebXRButton;
app.on("start", initXR);