Spaces:
Build error
Build error
File size: 13,764 Bytes
30c32c8 |
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
const BlockType = require('../../extension-support/block-type');
const ArgumentType = require('../../extension-support/argument-type');
const Cast = require('../../util/cast');
const SESSION_TYPE = "immersive-vr";
// WebXR unfortunately does not give us Euler angles easily
// so lets do it ourselves
// thanks to twoerner94 for quaternion-to-euler on npm
function quaternionToEuler(quat) {
const q0 = quat[0];
const q1 = quat[1];
const q2 = quat[2];
const q3 = quat[3];
const Rx = Math.atan2(2 * (q0 * q1 + q2 * q3), 1 - (2 * (q1 * q1 + q2 * q2)));
const Ry = Math.asin(2 * (q0 * q2 - q3 * q1));
const Rz = Math.atan2(2 * (q0 * q3 + q1 * q2), 1 - (2 * (q2 * q2 + q3 * q3)));
const euler = [Rx, Ry, Rz];
return euler;
};
function toRad(deg) {
return deg * (Math.PI / 180);
}
function toDeg(rad) {
return rad * (180 / Math.PI);
}
/**
* Class of 2025
* @constructor
*/
class jgVr {
constructor(runtime) {
/**
* The runtime instantiating this block package.
* @type {runtime}
*/
this.runtime = runtime;
this.open = false;
this.session = null;
this.view = null;
this.localSpace = null;
/**
* If true, VR sessions will begin split
* If false, VR sessions will begin with no split
*/
this.splitState = false;
}
/**
* @returns {object} metadata for this extension and its blocks.
*/
getInfo() {
return {
id: 'jgVr',
name: 'Virtual Reality',
color1: '#3888cf',
color2: '#2f72ad',
blocks: [
// CORE
{
opcode: 'isSupported',
text: 'is vr supported?',
blockType: BlockType.BOOLEAN,
disableMonitor: true
},
{
opcode: 'createSession',
text: 'create vr session',
blockType: BlockType.COMMAND
},
{
opcode: 'closeSession',
text: 'close vr session',
blockType: BlockType.COMMAND
},
{
opcode: 'isOpened',
text: 'is vr open?',
blockType: BlockType.BOOLEAN,
disableMonitor: true
},
'---', // SCREEN SPLITTING SETTINGS
{
opcode: 'enableDisableSplitting',
text: 'turn auto-splitting [ONOFF]',
blockType: BlockType.COMMAND,
arguments: {
ONOFF: {
type: ArgumentType.STRING,
menu: 'onoff'
}
}
},
{
opcode: 'splittingOffset',
text: 'set auto-split offset to [PX] pixels',
blockType: BlockType.COMMAND,
arguments: {
PX: {
type: ArgumentType.NUMBER,
defaultValue: 40
}
}
},
{
opcode: 'placement169',
text: '[SIDE] x placement',
blockType: BlockType.REPORTER,
disableMonitor: true,
arguments: {
SIDE: {
type: ArgumentType.STRING,
menu: 'side'
}
}
},
'---', // HEADSET POSITION
{
opcode: 'headsetPosition',
text: 'headset position [VECTOR3]',
blockType: BlockType.REPORTER,
disableMonitor: true,
arguments: {
VECTOR3: {
type: ArgumentType.STRING,
menu: 'vector3'
}
}
},
{
opcode: 'headsetRotation',
text: 'headset rotation [VECTOR3]',
blockType: BlockType.REPORTER,
disableMonitor: true,
arguments: {
VECTOR3: {
type: ArgumentType.STRING,
menu: 'vector3'
}
}
},
'---', // CONTROLLER INPUT
{
opcode: 'controllerPosition',
text: 'controller #[COUNT] position [VECTOR3]',
blockType: BlockType.REPORTER,
disableMonitor: true,
arguments: {
COUNT: {
type: ArgumentType.NUMBER,
menu: 'count'
},
VECTOR3: {
type: ArgumentType.STRING,
menu: 'vector3'
}
}
},
{
opcode: 'controllerRotation',
text: 'controller #[COUNT] rotation [VECTOR3]',
blockType: BlockType.REPORTER,
disableMonitor: true,
arguments: {
COUNT: {
type: ArgumentType.NUMBER,
menu: 'count'
},
VECTOR3: {
type: ArgumentType.STRING,
menu: 'vector3'
}
}
},
],
menus: {
vector3: {
acceptReporters: true,
items: [
"x",
"y",
"z",
].map(item => ({ text: item, value: item }))
},
count: {
acceptReporters: true,
items: [
"1",
"2",
].map(item => ({ text: item, value: item }))
},
side: {
acceptReporters: false,
items: [
"left",
"right",
].map(item => ({ text: item, value: item }))
},
onoff: {
acceptReporters: false,
items: [
"on",
"off",
].map(item => ({ text: item, value: item }))
},
}
};
}
// menus
_isVector3Menu(option) {
const normalized = Cast.toString(option).toLowerCase().trim();
return ['x', 'y', 'z'].includes(normalized);
}
_onOffBoolean(onoff) {
const normalized = Cast.toString(onoff).toLowerCase().trim();
return normalized === 'on';
}
// util
_getCanvas() {
if (!this.runtime) return;
if (!this.runtime.renderer) return;
return this.runtime.renderer.canvas;
}
_getContext() {
if (!this.runtime) return;
if (!this.runtime.renderer) return;
return this.runtime.renderer.gl;
}
_getRenderer() {
if (!this.runtime) return;
return this.runtime.renderer;
}
_disposeImmersive() {
this.session = null;
const gl = this._getContext();
if (!gl) return;
// bind frame buffer to canvas
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
// reset renderer info
const renderer = this._getRenderer();
if (!renderer) return;
renderer.xrEnabled = false;
renderer.xrSplitting = false;
renderer.xrLayer = null;
}
async _createImmersive() {
if (!('xr' in navigator)) return false;
const gl = this._getContext();
if (!gl) return;
const renderer = this._getRenderer();
if (!renderer) return;
await gl.makeXRCompatible();
const session = await navigator.xr.requestSession(SESSION_TYPE);
this.session = session;
this.open = true;
renderer.xrEnabled = true;
renderer.xrSplitting = this.splitState;
// we need to make sure stuff is back to normal once the vr session is done
// but this isnt always triggered by the close session block
// the user can also close it themselves, so we need to handle that
// this is also triggered by the close session block btw so we dont need
// to repeat
session.addEventListener("end", () => {
this.open = false;
this._disposeImmersive();
});
// set render state to use a new layer for the vr session
// renderer will handle this
const layer = new XRWebGLLayer(session, gl, {
alpha: true,
stencil: true,
antialias: false,
});
session.updateRenderState({
baseLayer: layer
});
renderer.xrLayer = layer;
// for debugging & other extensions, never used by the renderer
renderer._xrSession = session;
// setup render loop
const drawFrame = (_, frame) => {
// breaks the loop once the session has ended
if (!this.open) return;
// get view info
const viewerPose = frame.getViewerPose(this.localSpace);
const transform = viewerPose.transform;
// set view info
this.view = {
position: [
transform.position.x,
transform.position.y,
transform.position.z
],
quaternion: [
transform.orientation.w,
transform.orientation.y,
transform.orientation.x,
transform.orientation.z
]
}
// force renderer to draw a new frame
// otherwise we would only actually draw outside of this loop
// which just ends up showing nothing
// since rendering only happens in session.requestAnimationFrame
renderer.dirty = true;
renderer.draw();
// loop again
session.requestAnimationFrame(drawFrame);
}
session.requestAnimationFrame(drawFrame);
// reference space
session.requestReferenceSpace("local").then(space => {
this.localSpace = space;
// TODO: add "when position reset" hat?
// done with space.addEventListener("reset")
});
return session;
}
// blocks
isSupported() {
if (!('xr' in navigator)) return false;
return navigator.xr.isSessionSupported(SESSION_TYPE);
}
isOpened() {
return this.open;
}
createSession() {
if (this.open) return;
if (this.session) return;
return this._createImmersive();
}
closeSession() {
this.open = false;
if (!this.session) return;
return this.session.end();
}
// splitting blocks
enableDisableSplitting(args) {
const renderer = this._getRenderer();
if (!renderer) return;
const boolean = this._onOffBoolean(args.ONOFF);
this.splitState = boolean;
// setting xrSplitting outside of XR mode WILL work
// so prevent this by just checking if we ARE in XR rendering mode
if (!renderer.xrEnabled) return;
renderer.xrSplitting = this.splitState;
}
splittingOffset(args) {
const renderer = this._getRenderer();
if (!renderer) return;
// pixels should be negative
// otherwise we push away from the center
const pixels = Cast.toNumber(args.PX);
renderer.xrSplitOffset = 0 - pixels;
}
// inputs
headsetPosition(args) {
if (!this.open) return 0;
if (!this.session) return 0;
if (!this.view) return 0;
const vector3 = Cast.toString(args.VECTOR3).toLowerCase().trim();
if (!this._isVector3Menu(vector3)) return 0;
const axisArray = ['x', 'y', 'z'];
const idx = axisArray.indexOf(vector3);
return this.view.position[idx] * 100;
}
headsetRotation(args) {
if (!this.open) return 0;
if (!this.session) return 0;
if (!this.view) return 0;
const vector3 = Cast.toString(args.VECTOR3).toLowerCase().trim();
if (!this._isVector3Menu(vector3)) return 0;
const axisArray = ['x', 'y', 'z'];
const idx = axisArray.indexOf(vector3);
const quaternion = this.view.quaternion;
const euler = quaternionToEuler(quaternion);
return toDeg(euler[idx]);
}
// helper
placement169(args) {
const side = Cast.toString(args.SIDE).toLowerCase().trim();
const width = this.runtime.stageWidth;
const multX = width / 640;
// this was found with experimentation
// please tell me if stuff needs to be added for certain cases
const valueR = ((640 / 4) - 40) * multX;
const valueL = 0 - valueR;
if (side === 'right') {
return valueR;
}
return valueL;
}
}
module.exports = jgVr;
|