Spaces:
Runtime error
Runtime error
File size: 22,634 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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
const formatMessage = require('format-message');
const BlockType = require('../../extension-support/block-type');
const ArgumentType = require('../../extension-support/argument-type');
const Cast = require('../../util/cast');
const Icon = require('./icon.png');
const IconController = require('./controller.png');
const SESSION_TYPE = "immersive-vr";
// 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)));
return [Rx, Ry, Rz];
};
function toRad(deg) {
return deg * (Math.PI / 180);
}
function toDeg(rad) {
return rad * (180 / Math.PI);
}
function toDegRounding(rad) {
const result = toDeg(rad);
if (!String(result).includes('.')) return result;
const split = String(result).split('.');
const endingDecimals = split[1].substring(0, 3);
if ((endingDecimals === '999') && (split[1].charAt(3) === '9')) return Number(split[0]) + 1;
return Number(split[0] + '.' + endingDecimals);
}
/**
* Class for 3D VR blocks
*/
class Jg3DVrBlocks {
constructor(runtime) {
/**
* The runtime instantiating this block package.
*/
this.runtime = runtime;
this.open = false;
this._3d = {};
this.three = {};
// We'll store a wake lock reference here:
this.wakeLock = null;
if (!this.runtime.ext_jg3d) {
vm.extensionManager.loadExtensionURL('jg3d')
.then(() => {
this._3d = this.runtime.ext_jg3d;
this.three = this._3d.three;
});
} else {
this._3d = this.runtime.ext_jg3d;
this.three = this._3d.three;
}
}
/**
* Metadata for this extension and its blocks.
* @returns {object}
*/
getInfo() {
return {
id: 'jg3dVr',
name: '3D VR',
color1: '#B100FE',
color2: '#8000BC',
blockIconURI: Icon,
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
},
'---',
{
opcode: 'attachObject',
text: 'attach camera to object named [OBJECT]',
blockType: BlockType.COMMAND,
arguments: {
OBJECT: {
type: ArgumentType.STRING,
defaultValue: "Object1"
}
}
},
{
opcode: 'detachObject',
text: 'detach camera from object',
blockType: BlockType.COMMAND
},
'---',
{
opcode: 'getControllerPosition',
text: 'controller #[INDEX] position [VECTOR3]',
blockType: BlockType.REPORTER,
blockIconURI: IconController,
disableMonitor: true,
arguments: {
INDEX: {
type: ArgumentType.NUMBER,
menu: 'count'
},
VECTOR3: {
type: ArgumentType.STRING,
menu: 'vector3'
}
}
},
{
opcode: 'getControllerRotation',
text: 'controller #[INDEX] rotation [VECTOR3]',
blockType: BlockType.REPORTER,
blockIconURI: IconController,
disableMonitor: true,
arguments: {
INDEX: {
type: ArgumentType.NUMBER,
menu: 'count'
},
VECTOR3: {
type: ArgumentType.STRING,
menu: 'vector3'
}
}
},
{
opcode: 'getControllerSide',
text: 'side of controller #[INDEX]',
blockType: BlockType.REPORTER,
blockIconURI: IconController,
disableMonitor: true,
arguments: {
INDEX: {
type: ArgumentType.NUMBER,
menu: 'count'
}
}
},
'---',
{
opcode: 'getControllerStick',
text: 'joystick axis [XY] of controller #[INDEX]',
blockType: BlockType.REPORTER,
blockIconURI: IconController,
disableMonitor: true,
arguments: {
XY: {
type: ArgumentType.STRING,
menu: 'vector2'
},
INDEX: {
type: ArgumentType.NUMBER,
menu: 'count'
}
}
},
{
opcode: 'getControllerTrig',
text: 'analog value of [TRIGGER] trigger on controller #[INDEX]',
blockType: BlockType.REPORTER,
blockIconURI: IconController,
disableMonitor: true,
arguments: {
TRIGGER: {
type: ArgumentType.STRING,
menu: 'trig'
},
INDEX: {
type: ArgumentType.NUMBER,
menu: 'count'
}
}
},
{
opcode: 'getControllerButton',
text: 'button [BUTTON] on controller #[INDEX] pressed?',
blockType: BlockType.BOOLEAN,
blockIconURI: IconController,
disableMonitor: true,
arguments: {
BUTTON: {
type: ArgumentType.STRING,
menu: 'butt'
},
INDEX: {
type: ArgumentType.NUMBER,
menu: 'count'
}
}
},
{
opcode: 'getControllerTouching',
text: '[BUTTON] on controller #[INDEX] touched?',
blockType: BlockType.BOOLEAN,
blockIconURI: IconController,
disableMonitor: true,
arguments: {
BUTTON: {
type: ArgumentType.STRING,
menu: 'buttAll'
},
INDEX: {
type: ArgumentType.NUMBER,
menu: 'count'
}
}
},
],
menus: {
vector3: {
acceptReporters: true,
items: [
"x",
"y",
"z",
].map(item => ({ text: item, value: item }))
},
vector2: {
acceptReporters: true,
items: [
"x",
"y",
].map(item => ({ text: item, value: item }))
},
butt: {
acceptReporters: true,
items: [
"a",
"b",
"x",
"y",
"joystick",
].map(item => ({ text: item, value: item }))
},
trig: {
acceptReporters: true,
items: [
"back",
"side",
].map(item => ({ text: item, value: item }))
},
buttAll: {
acceptReporters: true,
items: [
"a button",
"b button",
"x button",
"y button",
"joystick",
"back trigger",
"side trigger",
].map(item => ({ text: item, value: item }))
},
count: {
acceptReporters: true,
items: [
"1",
"2",
].map(item => ({ text: item, value: item }))
},
}
};
}
// util
_getRenderer() {
if (!this._3d) return;
return this._3d.renderer;
}
_getGamepad(indexFrom1) {
const index = Cast.toNumber(indexFrom1) - 1;
const three = this._3d;
if (!three.scene) return;
const renderer = this._getRenderer();
if (!renderer) return;
const session = renderer.xr.getSession();
if (!session) return;
const sources = session.inputSources;
const controller = sources[index];
if (!controller) return;
const gamepad = controller.gamepad;
return gamepad;
}
_getController(index) {
const renderer = this._getRenderer();
if (!renderer) return null;
// try to use grip first (which typically has position/quaternion)
const grip = renderer.xr.getControllerGrip(index);
return grip || renderer.xr.getController(index);
}
_getInputSource(index) {
const renderer = this._getRenderer();
if (!renderer) return null;
const session = renderer.xr.getSession();
if (!session) return null;
const sources = session.inputSources;
return sources[index] || null;
}
_disposeImmersive() {
this.session = null;
const renderer = this._getRenderer();
if (!renderer) return;
renderer.xr.enabled = false;
// Clear the animation loop so Three.js stops calling it
renderer.setAnimationLoop(null);
}
async _requestWakeLock() {
if ('wakeLock' in navigator) {
try {
// Request a screen wake lock to prevent idling
this.wakeLock = await navigator.wakeLock.request('screen');
this.wakeLock.addEventListener('release', () => {
console.log('Wake Lock was released');
});
console.log('Wake Lock is active');
} catch (err) {
console.error('Failed to acquire wake lock:', err);
}
}
}
_releaseWakeLock() {
if (this.wakeLock) {
this.wakeLock.release();
this.wakeLock = null;
}
}
async _createImmersive() {
if (!('xr' in navigator)) return false;
const renderer = this._getRenderer();
if (!renderer) return false;
const sessionInit = {
optionalFeatures: ['local-floor', 'bounded-floor', 'hand-tracking', 'layers']
};
const session = await navigator.xr.requestSession(SESSION_TYPE, sessionInit);
this.session = session;
this.open = true;
renderer.xr.enabled = true;
await renderer.xr.setSession(session);
// Request the wake lock so that the session keeps updating even when idle
await this._requestWakeLock();
// When session ends, reset state and release wake lock.
session.addEventListener("end", () => {
this.open = false;
this._disposeImmersive();
this._releaseWakeLock();
});
// Request a reference space (store it so we can use it for the poses)
session.requestReferenceSpace("local").then(space => {
this.localSpace = space;
});
// Use Three.js's setAnimationLoop to drive the render loop
renderer.setAnimationLoop((time, frame) => {
if (!this.open) return;
const threed = this._3d;
if (!threed.camera || !threed.scene) return;
// Render the scene
renderer.render(threed.scene, threed.camera);
// Update controller poses if available
if (this.localSpace && frame) {
this.controllerPoses = {};
const sources = session.inputSources;
for (let i = 0; i < sources.length; i++) {
const inputSource = sources[i];
const pose = frame.getPose(inputSource.targetRaySpace, this.localSpace);
if (pose) {
this.controllerPoses[i] = {
position: pose.transform.position, // {x, y, z}
orientation: pose.transform.orientation // {x, y, z, w}
};
}
}
}
});
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();
}
// extra: attach/detach camera to/from an object in the scene
attachObject(args) {
const three = this._3d;
if (!three.scene) return;
if (!three.camera) return;
const name = Cast.toString(args.OBJECT);
const object = three.scene.getObjectByName(name);
if (!object) return;
object.add(three.camera);
}
detachObject() {
const three = this._3d;
if (!three.scene) return;
if (!three.camera) return;
three.scene.add(three.camera);
}
// Controller input blocks follow
getControllerPosition(args) {
if (!this._3d || !this._3d.scene) return "";
const index = Cast.toNumber(args.INDEX) - 1;
const v = args.VECTOR3;
if (!v || !["x", "y", "z"].includes(v)) return "";
// Use stored pose information if available
if (this.controllerPoses && this.controllerPoses[index]) {
return Cast.toNumber(this.controllerPoses[index].position[v]);
}
const renderer = this._getRenderer();
if (!renderer) return "";
const controller = this._getController(index);
if (!controller) return "";
controller.updateMatrixWorld(true);
// Fallback: get world position via Three.js
const Vector3 = (this.three && this.three.three && this.three.three.Vector3)
? this.three.three.Vector3
: this.three.Vector3;
const position = new Vector3();
controller.getWorldPosition(position);
return Cast.toNumber(position[v]);
}
getControllerRotation(args) {
if (!this._3d || !this._3d.scene) return "";
const index = Cast.toNumber(args.INDEX) - 1;
const v = args.VECTOR3;
if (!v || !["x", "y", "z"].includes(v)) return "";
// Use stored orientation if available
if (this.controllerPoses && this.controllerPoses[index]) {
const o = this.controllerPoses[index].orientation;
const Quaternion = (this.three && this.three.three && this.three.three.Quaternion)
? this.three.three.Quaternion
: this.three.Quaternion;
const quaternion = new Quaternion(o.x, o.y, o.z, o.w);
const Euler = (this.three && this.three.three && this.three.three.Euler)
? this.three.three.Euler
: this.three.Euler;
const euler = new Euler(0, 0, 0, 'YXZ');
euler.setFromQuaternion(quaternion, 'YXZ');
return toDegRounding(euler[v]);
}
const renderer = this._getRenderer();
if (!renderer) return "";
const controller = this._getController(index);
if (!controller) return "";
controller.updateMatrixWorld(true);
const Quaternion = (this.three && this.three.three && this.three.three.Quaternion)
? this.three.three.Quaternion
: this.three.Quaternion;
const quaternion = new Quaternion();
controller.getWorldQuaternion(quaternion);
const Euler = (this.three && this.three.three && this.three.three.Euler)
? this.three.three.Euler
: this.three.Euler;
const euler = new Euler(0, 0, 0, 'YXZ');
euler.setFromQuaternion(quaternion, 'YXZ');
return toDegRounding(euler[v]);
}
getControllerSide(args) {
const three = this._3d;
if (!three.scene) return "";
const renderer = this._getRenderer();
if (!renderer) return "";
const session = renderer.xr.getSession();
if (!session) return "";
const sources = session.inputSources;
const index = Cast.toNumber(args.INDEX) - 1;
const controller = sources[index];
if (!controller) return "";
return controller.handedness;
}
getControllerStick(args) {
const gamepad = this._getGamepad(args.INDEX);
if (!gamepad) return 0;
// For 'y', use axis index 3, otherwise default to index 2.
if (Cast.toString(args.XY) === "y") {
return gamepad.axes[3];
} else {
return gamepad.axes[2];
}
}
getControllerTrig(args) {
const gamepad = this._getGamepad(args.INDEX);
if (!gamepad) return 0;
if (Cast.toString(args.TRIGGER) === "side") {
return gamepad.buttons[1] ? gamepad.buttons[1].value : 0;
} else {
return gamepad.buttons[0] ? gamepad.buttons[0].value : 0;
}
}
getControllerButton(args) {
const gamepad = this._getGamepad(args.INDEX);
if (!gamepad) return false;
const inputSource = this._getInputSource(Cast.toNumber(args.INDEX) - 1);
let handedness = 'right';
if (inputSource && inputSource.handedness) {
handedness = inputSource.handedness;
}
const button = Cast.toString(args.BUTTON).toLowerCase();
if (handedness === 'right') {
switch (button) {
case 'a':
return gamepad.buttons[4] && gamepad.buttons[4].pressed;
case 'b':
return gamepad.buttons[5] && gamepad.buttons[5].pressed;
case 'joystick':
return gamepad.buttons[3] && gamepad.buttons[3].pressed;
}
} else if (handedness === 'left') {
switch (button) {
case 'x':
return gamepad.buttons[4] && gamepad.buttons[4].pressed;
case 'y':
return gamepad.buttons[5] && gamepad.buttons[5].pressed;
case 'joystick':
return gamepad.buttons[3] && gamepad.buttons[3].pressed;
}
}
return false;
}
getControllerTouching(args) {
const gamepad = this._getGamepad(args.INDEX);
if (!gamepad) return false;
const inputSource = this._getInputSource(Cast.toNumber(args.INDEX) - 1);
let handedness = 'right';
if (inputSource && inputSource.handedness) {
handedness = inputSource.handedness;
}
const button = Cast.toString(args.BUTTON).toLowerCase();
if (handedness === 'right') {
switch (button) {
case 'a button':
return gamepad.buttons[4] && gamepad.buttons[4].touched;
case 'b button':
return gamepad.buttons[5] && gamepad.buttons[5].touched;
case 'joystick':
return gamepad.buttons[3] && gamepad.buttons[3].touched;
case 'back trigger':
return gamepad.buttons[0] && gamepad.buttons[0].touched;
case 'side trigger':
return gamepad.buttons[1] && gamepad.buttons[1].touched;
}
} else if (handedness === 'left') {
switch (button) {
case 'x button':
return gamepad.buttons[4] && gamepad.buttons[4].touched;
case 'y button':
return gamepad.buttons[5] && gamepad.buttons[5].touched;
case 'joystick':
return gamepad.buttons[3] && gamepad.buttons[3].touched;
case 'back trigger':
return gamepad.buttons[0] && gamepad.buttons[0].touched;
case 'side trigger':
return gamepad.buttons[1] && gamepad.buttons[1].touched;
}
}
return false;
}
}
module.exports = Jg3DVrBlocks;
|