Skip to content
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

Adding onHit event for renderList items #33

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions js/immersive-pre.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ if (QueryArgs.getBool("usePolyfill", true)) {
let polyfill = new WebXRPolyfill();
}

import { setFrameInfo } from "./util/hitTest.js";

// XR globals.
let xrButton = null;
let xrImmersiveRefSpace = null;
Expand Down Expand Up @@ -296,6 +298,8 @@ function updateInputSources(session, frame, refSpace) {
}
}
}
setFrameInfo(session.inputSources, frame, refSpace);

}

function hitTest(inputSource, frame, refSpace) {
Expand Down
6 changes: 4 additions & 2 deletions js/primitive/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SyncObject, updateObject } from "../util/object-sync.js";
import { corelink_message } from "../util/corelink_sender.js";
import { metaroomSyncSender, metaroomWebrtcSender, metaroomEventSender, metaroomInitSender } from "../corelink_handler.js"
import { left_controller_trigger, right_controller_trigger } from "../util/input_event_handler.js"
import { addToPressStates } from "../render/core/renderListScene.js"

export function initSelfAvatar(id) {
if (!window.avatars) {
Expand Down Expand Up @@ -121,6 +122,7 @@ export function init() {
console.log("left already");
} else {
initAvatar(payload["user"]);
addToPressStates(payload["user"]);
}
}
// }
Expand Down Expand Up @@ -363,11 +365,11 @@ export function init() {
case "lefttrigger":
//left trigger
// console.log("call left_controller_trigger", json["state"]['operation']);
left_controller_trigger(json["state"]['operation']);
left_controller_trigger(json["state"]['operation'],json["uid"]);
break;
case "righttrigger":
//left trigger
right_controller_trigger(json["state"]['operation']);
right_controller_trigger(json["state"]['operation'],json["uid"]);
break;
default:
break;
Expand Down
149 changes: 144 additions & 5 deletions js/render/core/renderList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { CG, Matrix } from "./CG.js";
import { ImprovedNoise } from "../math/improvedNoise.js";
import { buttonState, controllerMatrix, leftHandState, rightHandState, pressStates } from "../core/renderListScene.js";
import { pHitTest, pHitTestNew } from "../../util/hitTest.js";

export let m = new Matrix();

Expand Down Expand Up @@ -107,6 +109,25 @@ let RenderList = function () {
return this;
};

this.hitEvent = (f, isShared) => {
this.hit = f;
this.shared = isShared;
return this;
}

this.group = (n) => {
if (groups[n] === undefined) {
groups[n] = [this];
this.groupInd = 0;
}
else if (!groups[n].includes(this)) {
groups[n].push(this);
this.groupInd = groups[n].length - 1;
}
this.groupId = n;
return this;
}

this.setBaseTexture = (url) => {
this.texture = url;
}
Expand Down Expand Up @@ -168,12 +189,124 @@ let RenderList = function () {
this.beginBuild = () => ((n = 0), (this.num = 0));
this.endBuild = () => {
/* do something */
for (let i=0; i<pressStates.length; i++) {
let id = pressStates[i].id;
let avt = window.avatars[id];
if (avt.leftController.buttons === null) continue;
let bst = {left: [], right: []};

//prevButtons do not seem to update correctly
/*
let stL = "released";
const bL = avt.leftController.buttons;
const bLPrev = avt.leftController.prevButtons;
for (let i = 0; i < 7; i++) {
if (bL[i].pressed && !bLPrev[i].pressed) stL = "pressed";
else
if (bL[i].pressed && bLPrev[i].pressed) stL = "dragged";
else
if (!bL[i].pressed) stL = "released";
bst["left"][i] = bL[i].pressed;
}
let stR = "released";
const bR = avt.rightController.buttons;
const bRPrev = avt.rightController.prevButtons;
for (let i = 0; i < 7; i++) {
if (bR[i].pressed && !bRPrev[i].pressed) stR = "pressed";
else
if (bR[i].pressed && bRPrev[i].pressed) stR = "dragged";
else
if (!bR[i].pressed) stR = "released";
bst["right"][i] = bR[i].pressed;
}
*/
let stL = pressStates[i].lState;
const bL = avt.leftController.buttons;
for (let i = 0; i < 7; i++) {
bst["left"][i] = bL[i].pressed;
}
let stR = pressStates[i].rState;
const bR = avt.rightController.buttons;
for (let i = 0; i < 7; i++) {
bst["right"][i] = bR[i].pressed;
}

let hmR = pHitTestNew(0, id);
let evR = {handedness: "right",
state: stR,
buttonState: bst.right,
hitItem: hmR};
let hmL = pHitTestNew(1, id);
let evL = {handedness: "left",
state: stL,
buttonState: bst.left,
hitItem: hmL};
if (hmR && hmR.hit !== undefined) {
hmR.hit(evR);
if (hmR.shared && hmR.groupId !== undefined) {
evR.indirect = true;
for (let i=0; i<groups[hmR.groupId].length; i++) {
if (i === hmR.groupInd) continue;
let obj = groups[hmR.groupId][i];
evR.hitItem = obj;
obj.hit(evR);
}
}
}
if (hmL && hmL.hit !== undefined) {
hmL.hit(evL);
if (hmL.shared && hmL.groupId !== undefined) {
evL.indirect = true;
for (let i=0; i<groups[hmL.groupId].length; i++) {
if (i === hmL.groupInd) continue;
let obj = groups[hmL.groupId][i];
evL.hitItem = obj;
obj.hit(evL);
}
}
}
}
let hmR = pHitTest(0);
let evR = {handedness: "right",
state: rightHandState,
buttonState: buttonState.right,
hitItem: hmR};
let hmL = pHitTest(1);
let evL = {handedness: "left",
state: leftHandState,
buttonState: buttonState.left,
hitItem: hmL};
if (hmR && hmR.hit !== undefined) {
hmR.hit(evR);
if (hmR.shared && hmR.groupId !== undefined) {
evR.indirect = true;
for (let i=0; i<groups[hmR.groupId].length; i++) {
if (i === hmR.groupInd) continue;
let obj = groups[hmR.groupId][i];
evR.hitItem = obj;
obj.hit(evR);
}
}
}
if (hmL && hmL.hit !== undefined) {
hmL.hit(evL);
if (hmL.shared && hmL.groupId !== undefined) {
evL.indirect = true;
for (let i=0; i<groups[hmL.groupId].length; i++) {
if (i === hmL.groupInd) continue;
let obj = groups[hmL.groupId][i];
evL.hitItem = obj;
obj.hit(evL);
}
}
}
};
this.add = (shape) => {
this.add = (shape, name) => {
if (items[n]) items[n].init();
else items[n] = new Item();
items[n].shape = shape;
items[n].matrix = m.value().slice();
if (name) items[n].mesh = name;
this.num++;
return items[n++];
};
Expand Down Expand Up @@ -245,6 +378,7 @@ let RenderList = function () {
item.sz
)
);
item.matrix = mat;
switch (item.type) {
// render list
case 1: {
Expand Down Expand Up @@ -322,7 +456,7 @@ let RenderList = function () {
item.sz
)
);

items[i].matrix = mat;
switch (item.type) {
// render list
case 1: {
Expand Down Expand Up @@ -366,16 +500,21 @@ let RenderList = function () {
this.program = null;
this.num = 0;

this.getItems = () => {
return items;
}

let items = [],
n = 0,
improvedNoise = new ImprovedNoise();
let groups = [];
};

RenderList.prototype.mMesh = function (V) {
return this.add(V);
};
RenderList.prototype.mCube = function () {
return this.add(CG.cube);
return this.add(CG.cube, "cube");
};
RenderList.prototype.mPoly4 = function (V) {
return this.add(CG.createPoly4Vertices(V));
Expand All @@ -390,10 +529,10 @@ RenderList.prototype.mSquare = function () {
return this.add(CG.quad);
};
RenderList.prototype.mSphere = function () {
return this.add(CG.sphere);
return this.add(CG.sphere, "sphere");
};
RenderList.prototype.mCylinder = function () {
return this.add(CG.cylinder);
return this.add(CG.cylinder, "cylinder");
};
RenderList.prototype.mRoundedCylinder = function () {
return this.add(CG.roundedCylinder);
Expand Down
35 changes: 32 additions & 3 deletions js/render/core/renderListScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { mainScene } from "../scenes/mainScene.js";
import { corelink_event } from "../../util/corelink_sender.js";

export let pressStates = [];
export let leftHandState = "released", rightHandState = "released";
export let viewMatrix = [], time = 0;
window.isPressed = false;
window.isDragged = false;
Expand Down Expand Up @@ -36,7 +38,7 @@ export let updateController = (avatar, buttonInfo) => {
}
};

export let onPress = (hand, button) => {
export let onPress = (hand, button, id) => {

window.isPressed = true;
window.isReleased = false;
Expand All @@ -46,24 +48,35 @@ export let onPress = (hand, button) => {
//ZH
// console.log("handleSelect");
// corelink_event({ it: "lefttrigger", op: "press" });
updatePressState(id, hand, "pressed");
if (hand === "left") leftHandState = "pressed";
else if (hand === "right") rightHandState = "pressed";
};

export let onDrag = (hand, button) => {
export let onDrag = (hand, button, id) => {
// console.log("onDrag", hand, "button", button, isPressed, isReleased, isDragged);
if (window.isPressed && window.justReleased) {
window.isDragged = true;
window.isReleased = false;
window.isPressed = false;

updatePressState(id, hand, "dragged");
if (hand === "left") leftHandState = "dragged";
else if (hand === "right") rightHandState = "dragged";
}
};

export let onRelease = (hand, button) => {
export let onRelease = (hand, button, id) => {
if (window.isDragged) {
window.isReleased = true;
window.isPressed = false;
window.isDragged = false;
console.log("onRelease", hand, "button", button,
window.isPressed, window.isReleased, window.isDragged);

updatePressState(id, hand, "released");
if (hand === "left") leftHandState = "released";
else if (hand === "right") rightHandState = "released";
}

//ZH
Expand All @@ -76,6 +89,22 @@ export let getViews = (views) => {
for (let view of views) viewMatrix.push(view.viewMatrix);
};

export let addToPressStates = (id) => {
if (!pressStates.includes(id)) {
if (pressStates[0] === undefined) pressStates[0] = {id: id, lState: "released", rState: "released"};
else pressStates.push({id: id, lState: "released", rState: "released"});
}
}

let updatePressState = (id, hand, state) => {
for (let i=0; i<pressStates.length; i++) {
if (pressStates[i].id === id) {
if (hand === "left") pressStates[i].lState = state;
else if (hand === "right") pressStates[i].rState = state;
}
}
}

export function renderListScene(_time) {
time = _time;
mainScene();
Expand Down
10 changes: 9 additions & 1 deletion js/render/core/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1314,10 +1314,18 @@ export class Renderer {
renderList.setTextureCatalogue(window.textureCatalogue);
renderList.beginFrame();
renderListScene(time);
//renderList.endBuild();
if (renderList.num > 0) {
// console.log('-------------------');
let tmp = [];
for (let i = 0; i < renderList.num; i++) {
this._drawRenderListPrimitive(views, ...renderList.endFrame(i));
//this._drawRenderListPrimitive(views, ...renderList.endFrame(i));
// console.log(...renderList.endFrame(i));
tmp[i] = renderList.endFrame(i);
}
renderList.endBuild();
for (let i = 0; i < renderList.num; i++) {
this._drawRenderListPrimitive(views, ...tmp[i]);
// console.log(...renderList.endFrame(i));
}
}
Expand Down
Loading